Annotation of src/usr.bin/ssh/kexgex.c, Revision 1.5
1.1 markus 1: /*
2: * Copyright (c) 2000 Niels Provos. All rights reserved.
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.5 ! markus 27: RCSID("$OpenBSD: kexgex.c,v 1.4 2001/04/04 22:04:35 markus Exp $");
1.1 markus 28:
29: #include <openssl/bn.h>
30:
31: #include "xmalloc.h"
32: #include "buffer.h"
33: #include "bufaux.h"
34: #include "key.h"
35: #include "kex.h"
36: #include "log.h"
37: #include "packet.h"
38: #include "dh.h"
39: #include "ssh2.h"
40: #include "compat.h"
41:
42: u_char *
43: kexgex_hash(
44: char *client_version_string,
45: char *server_version_string,
46: char *ckexinit, int ckexinitlen,
47: char *skexinit, int skexinitlen,
48: char *serverhostkeyblob, int sbloblen,
49: int min, int wantbits, int max, BIGNUM *prime, BIGNUM *gen,
50: BIGNUM *client_dh_pub,
51: BIGNUM *server_dh_pub,
52: BIGNUM *shared_secret)
53: {
54: Buffer b;
55: static u_char digest[EVP_MAX_MD_SIZE];
56: EVP_MD *evp_md = EVP_sha1();
57: EVP_MD_CTX md;
58:
59: buffer_init(&b);
60: buffer_put_string(&b, client_version_string, strlen(client_version_string));
61: buffer_put_string(&b, server_version_string, strlen(server_version_string));
62:
63: /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
64: buffer_put_int(&b, ckexinitlen+1);
65: buffer_put_char(&b, SSH2_MSG_KEXINIT);
66: buffer_append(&b, ckexinit, ckexinitlen);
67: buffer_put_int(&b, skexinitlen+1);
68: buffer_put_char(&b, SSH2_MSG_KEXINIT);
69: buffer_append(&b, skexinit, skexinitlen);
70:
71: buffer_put_string(&b, serverhostkeyblob, sbloblen);
1.5 ! markus 72: if (min == -1 || max == -1)
1.1 markus 73: buffer_put_int(&b, wantbits);
74: else {
75: buffer_put_int(&b, min);
76: buffer_put_int(&b, wantbits);
77: buffer_put_int(&b, max);
78: }
79: buffer_put_bignum2(&b, prime);
80: buffer_put_bignum2(&b, gen);
81: buffer_put_bignum2(&b, client_dh_pub);
82: buffer_put_bignum2(&b, server_dh_pub);
83: buffer_put_bignum2(&b, shared_secret);
84:
85: #ifdef DEBUG_KEXDH
86: buffer_dump(&b);
87: #endif
88: EVP_DigestInit(&md, evp_md);
89: EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
90: EVP_DigestFinal(&md, digest, NULL);
91:
92: buffer_free(&b);
93:
94: #ifdef DEBUG_KEXDH
95: dump_digest("hash", digest, evp_md->md_size);
96: #endif
97: return digest;
98: }
99:
100: /* client */
101:
102: void
103: kexgex_client(Kex *kex)
104: {
1.2 markus 105: BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
106: BIGNUM *p = NULL, *g = NULL;
107: Key *server_host_key;
108: u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
109: u_int klen, kout, slen, sbloblen;
110: int dlen, plen, min, max, nbits;
111: DH *dh;
1.1 markus 112:
1.2 markus 113: nbits = dh_estimate(kex->we_need * 8);
1.1 markus 114:
115: if (datafellows & SSH_OLD_DHGEX) {
116: debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent");
117:
118: /* Old GEX request */
119: packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
1.2 markus 120: packet_put_int(nbits);
121: min = DH_GRP_MIN;
122: max = DH_GRP_MAX;
1.1 markus 123: } else {
124: debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent");
125:
126: /* New GEX request */
1.2 markus 127: min = DH_GRP_MIN;
128: max = DH_GRP_MAX;
1.1 markus 129: packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
1.2 markus 130: packet_put_int(min);
131: packet_put_int(nbits);
132: packet_put_int(max);
1.1 markus 133: }
134: #ifdef DEBUG_KEXDH
135: fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
1.2 markus 136: min, nbits, max);
1.1 markus 137: #endif
138: packet_send();
139:
1.2 markus 140: debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
141: packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
1.1 markus 142:
143: if ((p = BN_new()) == NULL)
144: fatal("BN_new");
145: packet_get_bignum2(p, &dlen);
146: if ((g = BN_new()) == NULL)
147: fatal("BN_new");
148: packet_get_bignum2(g, &dlen);
1.2 markus 149: packet_done();
1.1 markus 150:
1.2 markus 151: if (BN_num_bits(p) < min || BN_num_bits(p) > max)
1.1 markus 152: fatal("DH_GEX group out of range: %d !< %d !< %d",
1.2 markus 153: min, BN_num_bits(p), max);
1.1 markus 154:
155: dh = dh_new_group(g, p);
156: dh_gen_key(dh, kex->we_need * 8);
157:
158: #ifdef DEBUG_KEXDH
159: DHparams_print_fp(stderr, dh);
160: fprintf(stderr, "pub= ");
161: BN_print_fp(stderr, dh->pub_key);
162: fprintf(stderr, "\n");
163: #endif
164:
165: debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
166: /* generate and send 'e', client DH public key */
167: packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
168: packet_put_bignum2(dh->pub_key);
169: packet_send();
170:
1.2 markus 171: debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
172: packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
1.1 markus 173:
174: /* key, cert */
175: server_host_key_blob = packet_get_string(&sbloblen);
176: server_host_key = key_from_blob(server_host_key_blob, sbloblen);
177: if (server_host_key == NULL)
178: fatal("cannot decode server_host_key_blob");
179:
180: if (kex->check_host_key == NULL)
181: fatal("cannot check server_host_key");
182: kex->check_host_key(server_host_key);
183:
184: /* DH paramter f, server public DH key */
185: dh_server_pub = BN_new();
186: if (dh_server_pub == NULL)
187: fatal("dh_server_pub == NULL");
188: packet_get_bignum2(dh_server_pub, &dlen);
189:
190: #ifdef DEBUG_KEXDH
191: fprintf(stderr, "dh_server_pub= ");
192: BN_print_fp(stderr, dh_server_pub);
193: fprintf(stderr, "\n");
194: debug("bits %d", BN_num_bits(dh_server_pub));
195: #endif
196:
197: /* signed H */
198: signature = packet_get_string(&slen);
199: packet_done();
200:
201: if (!dh_pub_is_valid(dh, dh_server_pub))
202: packet_disconnect("bad server public DH value");
203:
204: klen = DH_size(dh);
205: kbuf = xmalloc(klen);
206: kout = DH_compute_key(kbuf, dh_server_pub, dh);
207: #ifdef DEBUG_KEXDH
1.5 ! markus 208: dump_digest("shared secret", kbuf, kout);
1.1 markus 209: #endif
210: shared_secret = BN_new();
211: BN_bin2bn(kbuf, kout, shared_secret);
212: memset(kbuf, 0, klen);
213: xfree(kbuf);
214:
1.2 markus 215: if (datafellows & SSH_OLD_DHGEX)
1.1 markus 216: min = max = -1;
217:
218: /* calc and verify H */
219: hash = kexgex_hash(
220: kex->client_version_string,
221: kex->server_version_string,
222: buffer_ptr(&kex->my), buffer_len(&kex->my),
223: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
224: server_host_key_blob, sbloblen,
1.2 markus 225: min, nbits, max,
1.1 markus 226: dh->p, dh->g,
227: dh->pub_key,
228: dh_server_pub,
229: shared_secret
230: );
1.3 markus 231: /* have keys, free DH */
232: DH_free(dh);
1.1 markus 233: xfree(server_host_key_blob);
234: BN_free(dh_server_pub);
235:
236: if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
237: fatal("key_verify failed for server_host_key");
238: key_free(server_host_key);
239: xfree(signature);
240:
1.2 markus 241: /* save session id */
242: if (kex->session_id == NULL) {
243: kex->session_id_len = 20;
244: kex->session_id = xmalloc(kex->session_id_len);
245: memcpy(kex->session_id, hash, kex->session_id_len);
246: }
1.1 markus 247: kex_derive_keys(kex, hash, shared_secret);
248: BN_clear_free(shared_secret);
249:
1.3 markus 250: kex_finish(kex);
1.1 markus 251: }
252:
253: /* server */
254:
255: void
256: kexgex_server(Kex *kex)
257: {
1.2 markus 258: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
259: Key *server_host_key;
260: DH *dh = dh;
261: u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
262: u_int sbloblen, klen, kout;
263: int min = -1, max = -1, nbits = -1, type, plen, dlen, slen;
1.1 markus 264:
1.2 markus 265: if (kex->load_host_key == NULL)
266: fatal("Cannot load hostkey");
267: server_host_key = kex->load_host_key(kex->hostkey_type);
268: if (server_host_key == NULL)
269: fatal("Unsupported hostkey type %d", kex->hostkey_type);
1.1 markus 270:
1.2 markus 271: type = packet_read(&plen);
1.1 markus 272: switch(type){
273: case SSH2_MSG_KEX_DH_GEX_REQUEST:
274: debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
275: min = packet_get_int();
1.2 markus 276: nbits = packet_get_int();
1.1 markus 277: max = packet_get_int();
278: min = MAX(DH_GRP_MIN, min);
279: max = MIN(DH_GRP_MAX, max);
280: break;
281: case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
282: debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
1.2 markus 283: nbits = packet_get_int();
1.1 markus 284: min = DH_GRP_MIN;
285: max = DH_GRP_MAX;
286: /* unused for old GEX */
287: break;
1.2 markus 288: default:
1.4 markus 289: fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
1.1 markus 290: }
291: packet_done();
292:
1.2 markus 293: if (max < min || nbits < min || max < nbits)
1.1 markus 294: fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
1.2 markus 295: min, nbits, max);
1.1 markus 296:
1.2 markus 297: dh = choose_dh(min, nbits, max);
298: if (dh == NULL)
1.1 markus 299: packet_disconnect("Protocol error: no matching DH grp found");
300:
301: debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
302: packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
1.2 markus 303: packet_put_bignum2(dh->p);
304: packet_put_bignum2(dh->g);
1.1 markus 305: packet_send();
306:
307: /* flush */
308: packet_write_wait();
309:
310: /* Compute our exchange value in parallel with the client */
1.2 markus 311: dh_gen_key(dh, kex->we_need * 8);
1.1 markus 312:
1.2 markus 313: debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
314: packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_INIT);
1.1 markus 315:
316: /* key, cert */
317: dh_client_pub = BN_new();
318: if (dh_client_pub == NULL)
319: fatal("dh_client_pub == NULL");
320: packet_get_bignum2(dh_client_pub, &dlen);
321:
322: #ifdef DEBUG_KEXDH
323: fprintf(stderr, "dh_client_pub= ");
324: BN_print_fp(stderr, dh_client_pub);
325: fprintf(stderr, "\n");
326: debug("bits %d", BN_num_bits(dh_client_pub));
327: #endif
328:
329: #ifdef DEBUG_KEXDH
330: DHparams_print_fp(stderr, dh);
331: fprintf(stderr, "pub= ");
332: BN_print_fp(stderr, dh->pub_key);
333: fprintf(stderr, "\n");
334: #endif
335: if (!dh_pub_is_valid(dh, dh_client_pub))
336: packet_disconnect("bad client public DH value");
337:
338: klen = DH_size(dh);
339: kbuf = xmalloc(klen);
340: kout = DH_compute_key(kbuf, dh_client_pub, dh);
341: #ifdef DEBUG_KEXDH
1.5 ! markus 342: dump_digest("shared secret", kbuf, kout);
1.1 markus 343: #endif
344: shared_secret = BN_new();
345: BN_bin2bn(kbuf, kout, shared_secret);
346: memset(kbuf, 0, klen);
347: xfree(kbuf);
348:
349: key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
350:
1.2 markus 351: if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
352: min = max = -1;
353:
1.1 markus 354: /* calc H */ /* XXX depends on 'kex' */
355: hash = kexgex_hash(
356: kex->client_version_string,
357: kex->server_version_string,
358: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
359: buffer_ptr(&kex->my), buffer_len(&kex->my),
360: (char *)server_host_key_blob, sbloblen,
1.2 markus 361: min, nbits, max,
1.1 markus 362: dh->p, dh->g,
363: dh_client_pub,
364: dh->pub_key,
365: shared_secret
366: );
367: BN_free(dh_client_pub);
368:
369: /* save session id := H */
370: /* XXX hashlen depends on KEX */
1.2 markus 371: if (kex->session_id == NULL) {
372: kex->session_id_len = 20;
373: kex->session_id = xmalloc(kex->session_id_len);
374: memcpy(kex->session_id, hash, kex->session_id_len);
375: }
1.1 markus 376:
377: /* sign H */
378: /* XXX hashlen depends on KEX */
379: key_sign(server_host_key, &signature, &slen, hash, 20);
380:
381: /* destroy_sensitive_data(); */
382:
383: /* send server hostkey, DH pubkey 'f' and singed H */
384: debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
385: packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
386: packet_put_string((char *)server_host_key_blob, sbloblen);
387: packet_put_bignum2(dh->pub_key); /* f */
388: packet_put_string((char *)signature, slen);
389: packet_send();
390: xfree(signature);
391: xfree(server_host_key_blob);
1.3 markus 392: /* have keys, free DH */
393: DH_free(dh);
1.1 markus 394:
395: kex_derive_keys(kex, hash, shared_secret);
396: BN_clear_free(shared_secret);
397:
1.3 markus 398: kex_finish(kex);
1.1 markus 399: }
400:
401: void
402: kexgex(Kex *kex)
403: {
404: if (kex->server)
405: kexgex_server(kex);
406: else
407: kexgex_client(kex);
408: }