Annotation of src/usr.bin/ssh/kexdhs.c, Revision 1.6
1.6 ! stevesk 1: /* $OpenBSD: kexdhs.c,v 1.5 2006/03/25 13:17:02 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
25:
26: #include "includes.h"
1.6 ! stevesk 27:
! 28: #include <string.h>
1.1 markus 29:
30: #include "xmalloc.h"
31: #include "key.h"
32: #include "kex.h"
33: #include "log.h"
34: #include "packet.h"
35: #include "dh.h"
36: #include "ssh2.h"
37: #include "monitor_wrap.h"
38:
39: void
40: kexdh_server(Kex *kex)
41: {
42: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
43: DH *dh;
44: Key *server_host_key;
45: u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.3 djm 46: u_int sbloblen, klen, kout, hashlen;
1.1 markus 47: u_int slen;
48:
49: /* generate server DH public key */
1.2 djm 50: switch (kex->kex_type) {
51: case KEX_DH_GRP1_SHA1:
52: dh = dh_new_group1();
53: break;
54: case KEX_DH_GRP14_SHA1:
55: dh = dh_new_group14();
56: break;
57: default:
58: fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
59: }
1.1 markus 60: dh_gen_key(dh, kex->we_need * 8);
61:
62: debug("expecting SSH2_MSG_KEXDH_INIT");
63: packet_read_expect(SSH2_MSG_KEXDH_INIT);
64:
65: if (kex->load_host_key == NULL)
66: fatal("Cannot load hostkey");
67: server_host_key = kex->load_host_key(kex->hostkey_type);
68: if (server_host_key == NULL)
69: fatal("Unsupported hostkey type %d", kex->hostkey_type);
70:
71: /* key, cert */
72: if ((dh_client_pub = BN_new()) == NULL)
73: fatal("dh_client_pub == NULL");
74: packet_get_bignum2(dh_client_pub);
75: packet_check_eom();
76:
77: #ifdef DEBUG_KEXDH
78: fprintf(stderr, "dh_client_pub= ");
79: BN_print_fp(stderr, dh_client_pub);
80: fprintf(stderr, "\n");
81: debug("bits %d", BN_num_bits(dh_client_pub));
82: #endif
83:
84: #ifdef DEBUG_KEXDH
85: DHparams_print_fp(stderr, dh);
86: fprintf(stderr, "pub= ");
87: BN_print_fp(stderr, dh->pub_key);
88: fprintf(stderr, "\n");
89: #endif
90: if (!dh_pub_is_valid(dh, dh_client_pub))
91: packet_disconnect("bad client public DH value");
92:
93: klen = DH_size(dh);
94: kbuf = xmalloc(klen);
95: kout = DH_compute_key(kbuf, dh_client_pub, dh);
96: #ifdef DEBUG_KEXDH
97: dump_digest("shared secret", kbuf, kout);
98: #endif
99: if ((shared_secret = BN_new()) == NULL)
100: fatal("kexdh_server: BN_new failed");
101: BN_bin2bn(kbuf, kout, shared_secret);
102: memset(kbuf, 0, klen);
103: xfree(kbuf);
104:
105: key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
106:
107: /* calc H */
1.3 djm 108: kex_dh_hash(
1.1 markus 109: kex->client_version_string,
110: kex->server_version_string,
111: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
112: buffer_ptr(&kex->my), buffer_len(&kex->my),
113: server_host_key_blob, sbloblen,
114: dh_client_pub,
115: dh->pub_key,
1.3 djm 116: shared_secret,
117: &hash, &hashlen
1.1 markus 118: );
119: BN_clear_free(dh_client_pub);
120:
121: /* save session id := H */
122: if (kex->session_id == NULL) {
1.3 djm 123: kex->session_id_len = hashlen;
1.1 markus 124: kex->session_id = xmalloc(kex->session_id_len);
125: memcpy(kex->session_id, hash, kex->session_id_len);
126: }
127:
128: /* sign H */
1.3 djm 129: PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
1.1 markus 130:
131: /* destroy_sensitive_data(); */
132:
133: /* send server hostkey, DH pubkey 'f' and singed H */
134: packet_start(SSH2_MSG_KEXDH_REPLY);
135: packet_put_string(server_host_key_blob, sbloblen);
136: packet_put_bignum2(dh->pub_key); /* f */
137: packet_put_string(signature, slen);
138: packet_send();
139:
140: xfree(signature);
141: xfree(server_host_key_blob);
142: /* have keys, free DH */
143: DH_free(dh);
144:
1.3 djm 145: kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1 markus 146: BN_clear_free(shared_secret);
147: kex_finish(kex);
148: }