Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.9
1.9 ! deraadt 1: /* $OpenBSD: kexgexc.c,v 1.8 2006/08/01 23:22:47 stevesk Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Niels Provos. All rights reserved.
4: * Copyright (c) 2001 Markus Friedl. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.9 ! deraadt 27: #include <sys/types.h>
1.7 stevesk 28:
1.8 stevesk 29: #include <stdio.h>
1.7 stevesk 30: #include <string.h>
1.9 ! deraadt 31: #include <signal.h>
1.1 markus 32:
33: #include "xmalloc.h"
1.9 ! deraadt 34: #include "buffer.h"
1.1 markus 35: #include "key.h"
1.9 ! deraadt 36: #include "cipher.h"
1.1 markus 37: #include "kex.h"
38: #include "log.h"
39: #include "packet.h"
40: #include "dh.h"
41: #include "ssh2.h"
42: #include "compat.h"
43:
44: void
45: kexgex_client(Kex *kex)
46: {
47: BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
48: BIGNUM *p = NULL, *g = NULL;
49: Key *server_host_key;
50: u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.3 djm 51: u_int klen, kout, slen, sbloblen, hashlen;
1.1 markus 52: int min, max, nbits;
53: DH *dh;
54:
55: nbits = dh_estimate(kex->we_need * 8);
56:
57: if (datafellows & SSH_OLD_DHGEX) {
58: /* Old GEX request */
59: packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
60: packet_put_int(nbits);
61: min = DH_GRP_MIN;
62: max = DH_GRP_MAX;
1.2 markus 63:
64: debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
1.1 markus 65: } else {
66: /* New GEX request */
67: min = DH_GRP_MIN;
68: max = DH_GRP_MAX;
69: packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
70: packet_put_int(min);
71: packet_put_int(nbits);
72: packet_put_int(max);
1.2 markus 73:
74: debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
75: min, nbits, max);
1.1 markus 76: }
77: #ifdef DEBUG_KEXDH
78: fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
79: min, nbits, max);
80: #endif
81: packet_send();
82:
83: debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
84: packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
85:
86: if ((p = BN_new()) == NULL)
87: fatal("BN_new");
88: packet_get_bignum2(p);
89: if ((g = BN_new()) == NULL)
90: fatal("BN_new");
91: packet_get_bignum2(g);
92: packet_check_eom();
93:
94: if (BN_num_bits(p) < min || BN_num_bits(p) > max)
95: fatal("DH_GEX group out of range: %d !< %d !< %d",
96: min, BN_num_bits(p), max);
97:
98: dh = dh_new_group(g, p);
99: dh_gen_key(dh, kex->we_need * 8);
100:
101: #ifdef DEBUG_KEXDH
102: DHparams_print_fp(stderr, dh);
103: fprintf(stderr, "pub= ");
104: BN_print_fp(stderr, dh->pub_key);
105: fprintf(stderr, "\n");
106: #endif
107:
108: debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
109: /* generate and send 'e', client DH public key */
110: packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
111: packet_put_bignum2(dh->pub_key);
112: packet_send();
113:
114: debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
115: packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
116:
117: /* key, cert */
118: server_host_key_blob = packet_get_string(&sbloblen);
119: server_host_key = key_from_blob(server_host_key_blob, sbloblen);
120: if (server_host_key == NULL)
121: fatal("cannot decode server_host_key_blob");
122: if (server_host_key->type != kex->hostkey_type)
123: fatal("type mismatch for decoded server_host_key_blob");
124: if (kex->verify_host_key == NULL)
125: fatal("cannot verify server_host_key");
126: if (kex->verify_host_key(server_host_key) == -1)
127: fatal("server_host_key verification failed");
128:
1.6 miod 129: /* DH parameter f, server public DH key */
1.1 markus 130: if ((dh_server_pub = BN_new()) == NULL)
131: fatal("dh_server_pub == NULL");
132: packet_get_bignum2(dh_server_pub);
133:
134: #ifdef DEBUG_KEXDH
135: fprintf(stderr, "dh_server_pub= ");
136: BN_print_fp(stderr, dh_server_pub);
137: fprintf(stderr, "\n");
138: debug("bits %d", BN_num_bits(dh_server_pub));
139: #endif
140:
141: /* signed H */
142: signature = packet_get_string(&slen);
143: packet_check_eom();
144:
145: if (!dh_pub_is_valid(dh, dh_server_pub))
146: packet_disconnect("bad server public DH value");
147:
148: klen = DH_size(dh);
149: kbuf = xmalloc(klen);
150: kout = DH_compute_key(kbuf, dh_server_pub, dh);
151: #ifdef DEBUG_KEXDH
152: dump_digest("shared secret", kbuf, kout);
153: #endif
154: if ((shared_secret = BN_new()) == NULL)
155: fatal("kexgex_client: BN_new failed");
156: BN_bin2bn(kbuf, kout, shared_secret);
157: memset(kbuf, 0, klen);
158: xfree(kbuf);
159:
160: if (datafellows & SSH_OLD_DHGEX)
161: min = max = -1;
162:
163: /* calc and verify H */
1.3 djm 164: kexgex_hash(
165: kex->evp_md,
1.1 markus 166: kex->client_version_string,
167: kex->server_version_string,
168: buffer_ptr(&kex->my), buffer_len(&kex->my),
169: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
170: server_host_key_blob, sbloblen,
171: min, nbits, max,
172: dh->p, dh->g,
173: dh->pub_key,
174: dh_server_pub,
1.3 djm 175: shared_secret,
176: &hash, &hashlen
1.1 markus 177: );
1.3 djm 178:
1.1 markus 179: /* have keys, free DH */
180: DH_free(dh);
181: xfree(server_host_key_blob);
182: BN_clear_free(dh_server_pub);
183:
1.3 djm 184: if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
1.1 markus 185: fatal("key_verify failed for server_host_key");
186: key_free(server_host_key);
187: xfree(signature);
188:
189: /* save session id */
190: if (kex->session_id == NULL) {
1.3 djm 191: kex->session_id_len = hashlen;
1.1 markus 192: kex->session_id = xmalloc(kex->session_id_len);
193: memcpy(kex->session_id, hash, kex->session_id_len);
194: }
1.3 djm 195: kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1 markus 196: BN_clear_free(shared_secret);
197:
198: kex_finish(kex);
199: }