xref: /freebsd/crypto/openssh/kex.c (revision 1a61beb0549e05b33df31380e427d90f6e46ff7e)
1 /* $OpenBSD: kex.c,v 1.97 2014/01/25 20:35:37 markus Exp $ */
2 /* $FreeBSD$ */
3 /*
4  * Copyright (c) 2000, 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 
27 #include "includes.h"
28 
29 #include <sys/param.h>
30 
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include <openssl/crypto.h>
38 
39 #include "xmalloc.h"
40 #include "ssh2.h"
41 #include "buffer.h"
42 #include "packet.h"
43 #include "compat.h"
44 #include "cipher.h"
45 #include "key.h"
46 #include "kex.h"
47 #include "log.h"
48 #include "mac.h"
49 #include "match.h"
50 #include "dispatch.h"
51 #include "monitor.h"
52 #include "roaming.h"
53 #include "digest.h"
54 
55 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
56 # if defined(HAVE_EVP_SHA256)
57 # define evp_ssh_sha256 EVP_sha256
58 # else
59 extern const EVP_MD *evp_ssh_sha256(void);
60 # endif
61 #endif
62 
63 /* prototype */
64 static void kex_kexinit_finish(Kex *);
65 static void kex_choose_conf(Kex *);
66 
67 struct kexalg {
68 	char *name;
69 	int type;
70 	int ec_nid;
71 	int hash_alg;
72 };
73 static const struct kexalg kexalgs[] = {
74 	{ KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
75 	{ KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
76 	{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
77 #ifdef HAVE_EVP_SHA256
78 	{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
79 #endif
80 #ifdef OPENSSL_HAS_ECC
81 	{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
82 	    NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
83 	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
84 	    SSH_DIGEST_SHA384 },
85 # ifdef OPENSSL_HAS_NISTP521
86 	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
87 	    SSH_DIGEST_SHA512 },
88 # endif
89 #endif
90 	{ KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
91 #ifdef HAVE_EVP_SHA256
92 	{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
93 #endif
94 	{ NULL, -1, -1, -1},
95 };
96 
97 char *
98 kex_alg_list(char sep)
99 {
100 	char *ret = NULL;
101 	size_t nlen, rlen = 0;
102 	const struct kexalg *k;
103 
104 	for (k = kexalgs; k->name != NULL; k++) {
105 		if (ret != NULL)
106 			ret[rlen++] = sep;
107 		nlen = strlen(k->name);
108 		ret = xrealloc(ret, 1, rlen + nlen + 2);
109 		memcpy(ret + rlen, k->name, nlen + 1);
110 		rlen += nlen;
111 	}
112 	return ret;
113 }
114 
115 static const struct kexalg *
116 kex_alg_by_name(const char *name)
117 {
118 	const struct kexalg *k;
119 
120 	for (k = kexalgs; k->name != NULL; k++) {
121 		if (strcmp(k->name, name) == 0)
122 			return k;
123 	}
124 	return NULL;
125 }
126 
127 /* Validate KEX method name list */
128 int
129 kex_names_valid(const char *names)
130 {
131 	char *s, *cp, *p;
132 
133 	if (names == NULL || strcmp(names, "") == 0)
134 		return 0;
135 	s = cp = xstrdup(names);
136 	for ((p = strsep(&cp, ",")); p && *p != '\0';
137 	    (p = strsep(&cp, ","))) {
138 		if (kex_alg_by_name(p) == NULL) {
139 			error("Unsupported KEX algorithm \"%.100s\"", p);
140 			free(s);
141 			return 0;
142 		}
143 	}
144 	debug3("kex names ok: [%s]", names);
145 	free(s);
146 	return 1;
147 }
148 
149 /* put algorithm proposal into buffer. */
150 #ifndef NONE_CIPHER_ENABLED
151 static void
152 #else
153 /* Also used in sshconnect2.c. */
154 void
155 #endif
156 kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
157 {
158 	u_int i;
159 
160 	buffer_clear(b);
161 	/*
162 	 * add a dummy cookie, the cookie will be overwritten by
163 	 * kex_send_kexinit(), each time a kexinit is set
164 	 */
165 	for (i = 0; i < KEX_COOKIE_LEN; i++)
166 		buffer_put_char(b, 0);
167 	for (i = 0; i < PROPOSAL_MAX; i++)
168 		buffer_put_cstring(b, proposal[i]);
169 	buffer_put_char(b, 0);			/* first_kex_packet_follows */
170 	buffer_put_int(b, 0);			/* uint32 reserved */
171 }
172 
173 /* parse buffer and return algorithm proposal */
174 static char **
175 kex_buf2prop(Buffer *raw, int *first_kex_follows)
176 {
177 	Buffer b;
178 	u_int i;
179 	char **proposal;
180 
181 	proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
182 
183 	buffer_init(&b);
184 	buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
185 	/* skip cookie */
186 	for (i = 0; i < KEX_COOKIE_LEN; i++)
187 		buffer_get_char(&b);
188 	/* extract kex init proposal strings */
189 	for (i = 0; i < PROPOSAL_MAX; i++) {
190 		proposal[i] = buffer_get_cstring(&b,NULL);
191 		debug2("kex_parse_kexinit: %s", proposal[i]);
192 	}
193 	/* first kex follows / reserved */
194 	i = buffer_get_char(&b);
195 	if (first_kex_follows != NULL)
196 		*first_kex_follows = i;
197 	debug2("kex_parse_kexinit: first_kex_follows %d ", i);
198 	i = buffer_get_int(&b);
199 	debug2("kex_parse_kexinit: reserved %u ", i);
200 	buffer_free(&b);
201 	return proposal;
202 }
203 
204 static void
205 kex_prop_free(char **proposal)
206 {
207 	u_int i;
208 
209 	for (i = 0; i < PROPOSAL_MAX; i++)
210 		free(proposal[i]);
211 	free(proposal);
212 }
213 
214 /* ARGSUSED */
215 static void
216 kex_protocol_error(int type, u_int32_t seq, void *ctxt)
217 {
218 	error("Hm, kex protocol error: type %d seq %u", type, seq);
219 }
220 
221 static void
222 kex_reset_dispatch(void)
223 {
224 	dispatch_range(SSH2_MSG_TRANSPORT_MIN,
225 	    SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
226 	dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
227 }
228 
229 void
230 kex_finish(Kex *kex)
231 {
232 	kex_reset_dispatch();
233 
234 	packet_start(SSH2_MSG_NEWKEYS);
235 	packet_send();
236 	/* packet_write_wait(); */
237 	debug("SSH2_MSG_NEWKEYS sent");
238 
239 	debug("expecting SSH2_MSG_NEWKEYS");
240 	packet_read_expect(SSH2_MSG_NEWKEYS);
241 	packet_check_eom();
242 	debug("SSH2_MSG_NEWKEYS received");
243 
244 	kex->done = 1;
245 	buffer_clear(&kex->peer);
246 	/* buffer_clear(&kex->my); */
247 	kex->flags &= ~KEX_INIT_SENT;
248 	free(kex->name);
249 	kex->name = NULL;
250 }
251 
252 void
253 kex_send_kexinit(Kex *kex)
254 {
255 	u_int32_t rnd = 0;
256 	u_char *cookie;
257 	u_int i;
258 
259 	if (kex == NULL) {
260 		error("kex_send_kexinit: no kex, cannot rekey");
261 		return;
262 	}
263 	if (kex->flags & KEX_INIT_SENT) {
264 		debug("KEX_INIT_SENT");
265 		return;
266 	}
267 	kex->done = 0;
268 
269 	/* generate a random cookie */
270 	if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
271 		fatal("kex_send_kexinit: kex proposal too short");
272 	cookie = buffer_ptr(&kex->my);
273 	for (i = 0; i < KEX_COOKIE_LEN; i++) {
274 		if (i % 4 == 0)
275 			rnd = arc4random();
276 		cookie[i] = rnd;
277 		rnd >>= 8;
278 	}
279 	packet_start(SSH2_MSG_KEXINIT);
280 	packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
281 	packet_send();
282 	debug("SSH2_MSG_KEXINIT sent");
283 	kex->flags |= KEX_INIT_SENT;
284 }
285 
286 /* ARGSUSED */
287 void
288 kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
289 {
290 	char *ptr;
291 	u_int i, dlen;
292 	Kex *kex = (Kex *)ctxt;
293 
294 	debug("SSH2_MSG_KEXINIT received");
295 	if (kex == NULL)
296 		fatal("kex_input_kexinit: no kex, cannot rekey");
297 
298 	ptr = packet_get_raw(&dlen);
299 	buffer_append(&kex->peer, ptr, dlen);
300 
301 	/* discard packet */
302 	for (i = 0; i < KEX_COOKIE_LEN; i++)
303 		packet_get_char();
304 	for (i = 0; i < PROPOSAL_MAX; i++)
305 		free(packet_get_string(NULL));
306 	/*
307 	 * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
308 	 * KEX method has the server move first, but a server might be using
309 	 * a custom method or one that we otherwise don't support. We should
310 	 * be prepared to remember first_kex_follows here so we can eat a
311 	 * packet later.
312 	 * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
313 	 * for cases where the server *doesn't* go first. I guess we should
314 	 * ignore it when it is set for these cases, which is what we do now.
315 	 */
316 	(void) packet_get_char();	/* first_kex_follows */
317 	(void) packet_get_int();	/* reserved */
318 	packet_check_eom();
319 
320 	kex_kexinit_finish(kex);
321 }
322 
323 Kex *
324 kex_setup(char *proposal[PROPOSAL_MAX])
325 {
326 	Kex *kex;
327 
328 	kex = xcalloc(1, sizeof(*kex));
329 	buffer_init(&kex->peer);
330 	buffer_init(&kex->my);
331 	kex_prop2buf(&kex->my, proposal);
332 	kex->done = 0;
333 
334 	kex_send_kexinit(kex);					/* we start */
335 	kex_reset_dispatch();
336 
337 	return kex;
338 }
339 
340 static void
341 kex_kexinit_finish(Kex *kex)
342 {
343 	if (!(kex->flags & KEX_INIT_SENT))
344 		kex_send_kexinit(kex);
345 
346 	kex_choose_conf(kex);
347 
348 	if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
349 	    kex->kex[kex->kex_type] != NULL) {
350 		(kex->kex[kex->kex_type])(kex);
351 	} else {
352 		fatal("Unsupported key exchange %d", kex->kex_type);
353 	}
354 }
355 
356 static void
357 choose_enc(Enc *enc, char *client, char *server)
358 {
359 	char *name = match_list(client, server, NULL);
360 	if (name == NULL)
361 		fatal("no matching cipher found: client %s server %s",
362 		    client, server);
363 	if ((enc->cipher = cipher_by_name(name)) == NULL)
364 		fatal("matching cipher is not supported: %s", name);
365 	enc->name = name;
366 	enc->enabled = 0;
367 	enc->iv = NULL;
368 	enc->iv_len = cipher_ivlen(enc->cipher);
369 	enc->key = NULL;
370 	enc->key_len = cipher_keylen(enc->cipher);
371 	enc->block_size = cipher_blocksize(enc->cipher);
372 }
373 
374 static void
375 choose_mac(Mac *mac, char *client, char *server)
376 {
377 	char *name = match_list(client, server, NULL);
378 	if (name == NULL)
379 		fatal("no matching mac found: client %s server %s",
380 		    client, server);
381 	if (mac_setup(mac, name) < 0)
382 		fatal("unsupported mac %s", name);
383 	/* truncate the key */
384 	if (datafellows & SSH_BUG_HMAC)
385 		mac->key_len = 16;
386 	mac->name = name;
387 	mac->key = NULL;
388 	mac->enabled = 0;
389 }
390 
391 static void
392 choose_comp(Comp *comp, char *client, char *server)
393 {
394 	char *name = match_list(client, server, NULL);
395 	if (name == NULL)
396 		fatal("no matching comp found: client %s server %s", client, server);
397 	if (strcmp(name, "zlib@openssh.com") == 0) {
398 		comp->type = COMP_DELAYED;
399 	} else if (strcmp(name, "zlib") == 0) {
400 		comp->type = COMP_ZLIB;
401 	} else if (strcmp(name, "none") == 0) {
402 		comp->type = COMP_NONE;
403 	} else {
404 		fatal("unsupported comp %s", name);
405 	}
406 	comp->name = name;
407 }
408 
409 static void
410 choose_kex(Kex *k, char *client, char *server)
411 {
412 	const struct kexalg *kexalg;
413 
414 	k->name = match_list(client, server, NULL);
415 	if (k->name == NULL)
416 		fatal("Unable to negotiate a key exchange method");
417 	if ((kexalg = kex_alg_by_name(k->name)) == NULL)
418 		fatal("unsupported kex alg %s", k->name);
419 	k->kex_type = kexalg->type;
420 	k->hash_alg = kexalg->hash_alg;
421 	k->ec_nid = kexalg->ec_nid;
422 }
423 
424 static void
425 choose_hostkeyalg(Kex *k, char *client, char *server)
426 {
427 	char *hostkeyalg = match_list(client, server, NULL);
428 	if (hostkeyalg == NULL)
429 		fatal("no hostkey alg");
430 	k->hostkey_type = key_type_from_name(hostkeyalg);
431 	if (k->hostkey_type == KEY_UNSPEC)
432 		fatal("bad hostkey alg '%s'", hostkeyalg);
433 	free(hostkeyalg);
434 }
435 
436 static int
437 proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
438 {
439 	static int check[] = {
440 		PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
441 	};
442 	int *idx;
443 	char *p;
444 
445 	for (idx = &check[0]; *idx != -1; idx++) {
446 		if ((p = strchr(my[*idx], ',')) != NULL)
447 			*p = '\0';
448 		if ((p = strchr(peer[*idx], ',')) != NULL)
449 			*p = '\0';
450 		if (strcmp(my[*idx], peer[*idx]) != 0) {
451 			debug2("proposal mismatch: my %s peer %s",
452 			    my[*idx], peer[*idx]);
453 			return (0);
454 		}
455 	}
456 	debug2("proposals match");
457 	return (1);
458 }
459 
460 static void
461 kex_choose_conf(Kex *kex)
462 {
463 	Newkeys *newkeys;
464 	char **my, **peer;
465 	char **cprop, **sprop;
466 	int nenc, nmac, ncomp;
467 	u_int mode, ctos, need, dh_need, authlen;
468 	int first_kex_follows, type;
469 #ifdef	NONE_CIPHER_ENABLED
470 	int auth_flag;
471 #endif
472 
473 	my   = kex_buf2prop(&kex->my, NULL);
474 	peer = kex_buf2prop(&kex->peer, &first_kex_follows);
475 
476 	if (kex->server) {
477 		cprop=peer;
478 		sprop=my;
479 	} else {
480 		cprop=my;
481 		sprop=peer;
482 	}
483 
484 	/* Check whether server offers roaming */
485 	if (!kex->server) {
486 		char *roaming;
487 		roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
488 		if (roaming) {
489 			kex->roaming = 1;
490 			free(roaming);
491 		}
492 	}
493 
494 	/* Algorithm Negotiation */
495 #ifdef	NONE_CIPHER_ENABLED
496 	auth_flag = packet_get_authentication_state();
497 	debug ("AUTH STATE is %d", auth_flag);
498 #endif
499 	for (mode = 0; mode < MODE_MAX; mode++) {
500 		newkeys = xcalloc(1, sizeof(*newkeys));
501 		kex->newkeys[mode] = newkeys;
502 		ctos = (!kex->server && mode == MODE_OUT) ||
503 		    (kex->server && mode == MODE_IN);
504 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
505 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
506 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
507 		choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
508 		/* ignore mac for authenticated encryption */
509 		authlen = cipher_authlen(newkeys->enc.cipher);
510 		if (authlen == 0)
511 			choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
512 		choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
513 #ifdef	NONE_CIPHER_ENABLED
514 		debug("REQUESTED ENC.NAME is '%s'", newkeys->enc.name);
515 		if (strcmp(newkeys->enc.name, "none") == 0) {
516 			debug("Requesting NONE. Authflag is %d", auth_flag);
517 			if (auth_flag == 1)
518 				debug("None requested post authentication.");
519 			else
520 				fatal("Pre-authentication none cipher requests "
521 				    "are not allowed.");
522 		}
523 #endif
524 		debug("kex: %s %s %s %s",
525 		    ctos ? "client->server" : "server->client",
526 		    newkeys->enc.name,
527 		    authlen == 0 ? newkeys->mac.name : "<implicit>",
528 		    newkeys->comp.name);
529 	}
530 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
531 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
532 	    sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
533 	need = dh_need = 0;
534 	for (mode = 0; mode < MODE_MAX; mode++) {
535 		newkeys = kex->newkeys[mode];
536 		need = MAX(need, newkeys->enc.key_len);
537 		need = MAX(need, newkeys->enc.block_size);
538 		need = MAX(need, newkeys->enc.iv_len);
539 		need = MAX(need, newkeys->mac.key_len);
540 		dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher));
541 		dh_need = MAX(dh_need, newkeys->enc.block_size);
542 		dh_need = MAX(dh_need, newkeys->enc.iv_len);
543 		dh_need = MAX(dh_need, newkeys->mac.key_len);
544 	}
545 	/* XXX need runden? */
546 	kex->we_need = need;
547 	kex->dh_need = dh_need;
548 
549 	/* ignore the next message if the proposals do not match */
550 	if (first_kex_follows && !proposals_match(my, peer) &&
551 	    !(datafellows & SSH_BUG_FIRSTKEX)) {
552 		type = packet_read();
553 		debug2("skipping next packet (type %u)", type);
554 	}
555 
556 	kex_prop_free(my);
557 	kex_prop_free(peer);
558 }
559 
560 static u_char *
561 derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
562     const u_char *shared_secret, u_int slen)
563 {
564 	Buffer b;
565 	struct ssh_digest_ctx *hashctx;
566 	char c = id;
567 	u_int have;
568 	size_t mdsz;
569 	u_char *digest;
570 
571 	if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
572 		fatal("bad kex md size %zu", mdsz);
573 	digest = xmalloc(roundup(need, mdsz));
574 
575 	buffer_init(&b);
576 	buffer_append(&b, shared_secret, slen);
577 
578 	/* K1 = HASH(K || H || "A" || session_id) */
579 	if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL)
580 		fatal("%s: ssh_digest_start failed", __func__);
581 	if (ssh_digest_update_buffer(hashctx, &b) != 0 ||
582 	    ssh_digest_update(hashctx, hash, hashlen) != 0 ||
583 	    ssh_digest_update(hashctx, &c, 1) != 0 ||
584 	    ssh_digest_update(hashctx, kex->session_id,
585 	    kex->session_id_len) != 0)
586 		fatal("%s: ssh_digest_update failed", __func__);
587 	if (ssh_digest_final(hashctx, digest, mdsz) != 0)
588 		fatal("%s: ssh_digest_final failed", __func__);
589 	ssh_digest_free(hashctx);
590 
591 	/*
592 	 * expand key:
593 	 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
594 	 * Key = K1 || K2 || ... || Kn
595 	 */
596 	for (have = mdsz; need > have; have += mdsz) {
597 		if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL)
598 			fatal("%s: ssh_digest_start failed", __func__);
599 		if (ssh_digest_update_buffer(hashctx, &b) != 0 ||
600 		    ssh_digest_update(hashctx, hash, hashlen) != 0 ||
601 		    ssh_digest_update(hashctx, digest, have) != 0)
602 			fatal("%s: ssh_digest_update failed", __func__);
603 		if (ssh_digest_final(hashctx, digest + have, mdsz) != 0)
604 			fatal("%s: ssh_digest_final failed", __func__);
605 		ssh_digest_free(hashctx);
606 	}
607 	buffer_free(&b);
608 #ifdef DEBUG_KEX
609 	fprintf(stderr, "key '%c'== ", c);
610 	dump_digest("key", digest, need);
611 #endif
612 	return digest;
613 }
614 
615 Newkeys *current_keys[MODE_MAX];
616 
617 #define NKEYS	6
618 void
619 kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen,
620     const u_char *shared_secret, u_int slen)
621 {
622 	u_char *keys[NKEYS];
623 	u_int i, mode, ctos;
624 
625 	for (i = 0; i < NKEYS; i++) {
626 		keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
627 		    shared_secret, slen);
628 	}
629 
630 	debug2("kex_derive_keys");
631 	for (mode = 0; mode < MODE_MAX; mode++) {
632 		current_keys[mode] = kex->newkeys[mode];
633 		kex->newkeys[mode] = NULL;
634 		ctos = (!kex->server && mode == MODE_OUT) ||
635 		    (kex->server && mode == MODE_IN);
636 		current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
637 		current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
638 		current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
639 	}
640 }
641 
642 void
643 kex_derive_keys_bn(Kex *kex, u_char *hash, u_int hashlen, const BIGNUM *secret)
644 {
645 	Buffer shared_secret;
646 
647 	buffer_init(&shared_secret);
648 	buffer_put_bignum2(&shared_secret, secret);
649 	kex_derive_keys(kex, hash, hashlen,
650 	    buffer_ptr(&shared_secret), buffer_len(&shared_secret));
651 	buffer_free(&shared_secret);
652 }
653 
654 Newkeys *
655 kex_get_newkeys(int mode)
656 {
657 	Newkeys *ret;
658 
659 	ret = current_keys[mode];
660 	current_keys[mode] = NULL;
661 	return ret;
662 }
663 
664 void
665 derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
666     u_int8_t cookie[8], u_int8_t id[16])
667 {
668 	u_int8_t nbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
669 	int len;
670 	struct ssh_digest_ctx *hashctx;
671 
672 	if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL)
673 		fatal("%s: ssh_digest_start", __func__);
674 
675 	len = BN_num_bytes(host_modulus);
676 	if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
677 		fatal("%s: bad host modulus (len %d)", __func__, len);
678 	BN_bn2bin(host_modulus, nbuf);
679 	if (ssh_digest_update(hashctx, nbuf, len) != 0)
680 		fatal("%s: ssh_digest_update failed", __func__);
681 
682 	len = BN_num_bytes(server_modulus);
683 	if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
684 		fatal("%s: bad server modulus (len %d)", __func__, len);
685 	BN_bn2bin(server_modulus, nbuf);
686 	if (ssh_digest_update(hashctx, nbuf, len) != 0 ||
687 	    ssh_digest_update(hashctx, cookie, 8) != 0)
688 		fatal("%s: ssh_digest_update failed", __func__);
689 	if (ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0)
690 		fatal("%s: ssh_digest_final failed", __func__);
691 	memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
692 
693 	memset(nbuf, 0, sizeof(nbuf));
694 	memset(obuf, 0, sizeof(obuf));
695 }
696 
697 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
698 void
699 dump_digest(char *msg, u_char *digest, int len)
700 {
701 	int i;
702 
703 	fprintf(stderr, "%s\n", msg);
704 	for (i = 0; i < len; i++) {
705 		fprintf(stderr, "%02x", digest[i]);
706 		if (i%32 == 31)
707 			fprintf(stderr, "\n");
708 		else if (i%8 == 7)
709 			fprintf(stderr, " ");
710 	}
711 	fprintf(stderr, "\n");
712 }
713 #endif
714