xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/kex.c (revision 9600:113cf06ae502)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  *
248658SJan.Pechanec@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "includes.h"
290Sstevel@tonic-gate RCSID("$OpenBSD: kex.c,v 1.51 2002/06/24 14:55:38 markus Exp $");
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <locale.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <openssl/crypto.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "ssh2.h"
360Sstevel@tonic-gate #include "xmalloc.h"
370Sstevel@tonic-gate #include "buffer.h"
380Sstevel@tonic-gate #include "bufaux.h"
390Sstevel@tonic-gate #include "packet.h"
400Sstevel@tonic-gate #include "compat.h"
410Sstevel@tonic-gate #include "cipher.h"
420Sstevel@tonic-gate #include "kex.h"
430Sstevel@tonic-gate #include "key.h"
440Sstevel@tonic-gate #include "log.h"
450Sstevel@tonic-gate #include "mac.h"
460Sstevel@tonic-gate #include "match.h"
470Sstevel@tonic-gate #include "dispatch.h"
480Sstevel@tonic-gate #include "g11n.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef GSSAPI
510Sstevel@tonic-gate #include "ssh-gss.h"
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define KEX_COOKIE_LEN	16
550Sstevel@tonic-gate 
560Sstevel@tonic-gate char *session_lang = NULL;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* prototype */
600Sstevel@tonic-gate static void kex_do_hook(Kex *kex);
610Sstevel@tonic-gate static void kex_kexinit_finish(Kex *);
620Sstevel@tonic-gate static void kex_choose_conf(Kex *);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* put algorithm proposal into buffer */
650Sstevel@tonic-gate static
660Sstevel@tonic-gate void
kex_prop2buf(Buffer * b,char * proposal[PROPOSAL_MAX])670Sstevel@tonic-gate kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	int i;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	buffer_clear(b);
720Sstevel@tonic-gate 	/*
730Sstevel@tonic-gate 	 * add a dummy cookie, the cookie will be overwritten by
740Sstevel@tonic-gate 	 * kex_send_kexinit(), each time a kexinit is set
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
770Sstevel@tonic-gate 		buffer_put_char(b, 0);
780Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
790Sstevel@tonic-gate 		buffer_put_cstring(b, proposal[i]);
800Sstevel@tonic-gate 	buffer_put_char(b, 0);			/* first_kex_packet_follows */
810Sstevel@tonic-gate 	buffer_put_int(b, 0);			/* uint32 reserved */
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /* parse buffer and return algorithm proposal */
850Sstevel@tonic-gate static
860Sstevel@tonic-gate char **
kex_buf2prop(Buffer * raw,int * first_kex_follows)870Sstevel@tonic-gate kex_buf2prop(Buffer *raw, int *first_kex_follows)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	Buffer b;
900Sstevel@tonic-gate 	int i;
910Sstevel@tonic-gate 	char **proposal;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	proposal = xmalloc(PROPOSAL_MAX * sizeof(char *));
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	buffer_init(&b);
960Sstevel@tonic-gate 	buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
970Sstevel@tonic-gate 	/* skip cookie */
980Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
990Sstevel@tonic-gate 		buffer_get_char(&b);
1000Sstevel@tonic-gate 	/* extract kex init proposal strings */
1010Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++) {
1020Sstevel@tonic-gate 		proposal[i] = buffer_get_string(&b,NULL);
1030Sstevel@tonic-gate 		debug2("kex_parse_kexinit: %s", proposal[i]);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	/* first kex follows / reserved */
1060Sstevel@tonic-gate 	i = buffer_get_char(&b);
1070Sstevel@tonic-gate 	if (first_kex_follows != NULL)
1080Sstevel@tonic-gate 		*first_kex_follows = i;
1090Sstevel@tonic-gate 	debug2("kex_parse_kexinit: first_kex_follows %d ", i);
1100Sstevel@tonic-gate 	i = buffer_get_int(&b);
1110Sstevel@tonic-gate 	debug2("kex_parse_kexinit: reserved %d ", i);
1120Sstevel@tonic-gate 	buffer_free(&b);
1130Sstevel@tonic-gate 	return proposal;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static
1170Sstevel@tonic-gate void
kex_prop_free(char ** proposal)1180Sstevel@tonic-gate kex_prop_free(char **proposal)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int i;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
1230Sstevel@tonic-gate 		xfree(proposal[i]);
1240Sstevel@tonic-gate 	xfree(proposal);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static void
kex_protocol_error(int type,u_int32_t seq,void * ctxt)1280Sstevel@tonic-gate kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	error("Hm, kex protocol error: type %d seq %u", type, seq);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static void
kex_reset_dispatch(void)1340Sstevel@tonic-gate kex_reset_dispatch(void)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate #ifdef ALTPRIVSEP
1370Sstevel@tonic-gate 	/* unprivileged sshd has a kex packet handler that must not be reset */
1380Sstevel@tonic-gate 	debug3("kex_reset_dispatch -- should we dispatch_set(KEXINIT) here? %d && !%d",
1390Sstevel@tonic-gate 		packet_is_server(), packet_is_monitor());
1400Sstevel@tonic-gate 	if (packet_is_server() && !packet_is_monitor()) {
1410Sstevel@tonic-gate 		debug3("kex_reset_dispatch -- skipping dispatch_set(KEXINIT) in unpriv proc");
1420Sstevel@tonic-gate 		return;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	dispatch_range(SSH2_MSG_TRANSPORT_MIN,
1470Sstevel@tonic-gate 	    SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1480Sstevel@tonic-gate 	dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate void
kex_finish(Kex * kex)1520Sstevel@tonic-gate kex_finish(Kex *kex)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	kex_reset_dispatch();
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	packet_start(SSH2_MSG_NEWKEYS);
1570Sstevel@tonic-gate 	packet_send();
1580Sstevel@tonic-gate 	/* packet_write_wait(); */
1590Sstevel@tonic-gate 	debug("SSH2_MSG_NEWKEYS sent");
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate #ifdef ALTPRIVSEP
1620Sstevel@tonic-gate 	if (packet_is_monitor())
1630Sstevel@tonic-gate 		goto skip_newkeys;
1640Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1650Sstevel@tonic-gate 	debug("expecting SSH2_MSG_NEWKEYS");
1660Sstevel@tonic-gate 	packet_read_expect(SSH2_MSG_NEWKEYS);
1670Sstevel@tonic-gate 	packet_check_eom();
1680Sstevel@tonic-gate 	debug("SSH2_MSG_NEWKEYS received");
1690Sstevel@tonic-gate #ifdef ALTPRIVSEP
1700Sstevel@tonic-gate skip_newkeys:
1710Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	kex->done = 1;
1740Sstevel@tonic-gate 	kex->initial_kex_done = 1; /* never to be cleared once set */
1750Sstevel@tonic-gate 	buffer_clear(&kex->peer);
1760Sstevel@tonic-gate 	/* buffer_clear(&kex->my); */
1770Sstevel@tonic-gate 	kex->flags &= ~KEX_INIT_SENT;
1780Sstevel@tonic-gate 	xfree(kex->name);
1790Sstevel@tonic-gate 	kex->name = NULL;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate void
kex_send_kexinit(Kex * kex)1830Sstevel@tonic-gate kex_send_kexinit(Kex *kex)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	u_int32_t rand = 0;
1860Sstevel@tonic-gate 	u_char *cookie;
1870Sstevel@tonic-gate 	int i;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (kex == NULL) {
1900Sstevel@tonic-gate 		error("kex_send_kexinit: no kex, cannot rekey");
1910Sstevel@tonic-gate 		return;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	if (kex->flags & KEX_INIT_SENT) {
1940Sstevel@tonic-gate 		debug("KEX_INIT_SENT");
1950Sstevel@tonic-gate 		return;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 	kex->done = 0;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/* update my proposal -- e.g., add/remove GSS kexalgs */
2000Sstevel@tonic-gate 	kex_do_hook(kex);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* generate a random cookie */
2030Sstevel@tonic-gate 	if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
2040Sstevel@tonic-gate 		fatal("kex_send_kexinit: kex proposal too short");
2050Sstevel@tonic-gate 	cookie = buffer_ptr(&kex->my);
2060Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++) {
2070Sstevel@tonic-gate 		if (i % 4 == 0)
2080Sstevel@tonic-gate 			rand = arc4random();
2090Sstevel@tonic-gate 		cookie[i] = rand;
2100Sstevel@tonic-gate 		rand >>= 8;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	packet_start(SSH2_MSG_KEXINIT);
2130Sstevel@tonic-gate 	packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
2140Sstevel@tonic-gate 	packet_send();
2150Sstevel@tonic-gate 	debug("SSH2_MSG_KEXINIT sent");
2160Sstevel@tonic-gate 	kex->flags |= KEX_INIT_SENT;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate void
kex_input_kexinit(int type,u_int32_t seq,void * ctxt)2200Sstevel@tonic-gate kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	char *ptr;
2230Sstevel@tonic-gate 	u_int dlen;
2240Sstevel@tonic-gate 	int i;
2250Sstevel@tonic-gate 	Kex *kex = (Kex *)ctxt;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	debug("SSH2_MSG_KEXINIT received");
2280Sstevel@tonic-gate 	if (kex == NULL)
2290Sstevel@tonic-gate 		fatal("kex_input_kexinit: no kex, cannot rekey");
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	ptr = packet_get_raw(&dlen);
2320Sstevel@tonic-gate 	buffer_append(&kex->peer, ptr, dlen);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* discard packet */
2350Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
2360Sstevel@tonic-gate 		packet_get_char();
2370Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
2380Sstevel@tonic-gate 		xfree(packet_get_string(NULL));
2390Sstevel@tonic-gate 	(void) packet_get_char();
2400Sstevel@tonic-gate 	(void) packet_get_int();
2410Sstevel@tonic-gate 	packet_check_eom();
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	kex_kexinit_finish(kex);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * This is for GSS keyex, where actual KEX offer can change at rekey
2480Sstevel@tonic-gate  * time due to credential expiration/renewal...
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static
2510Sstevel@tonic-gate void
kex_do_hook(Kex * kex)2520Sstevel@tonic-gate kex_do_hook(Kex *kex)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	char    **prop;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (kex->kex_hook == NULL)
2570Sstevel@tonic-gate 		return;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* Unmarshall my proposal, let the hook modify it, remarshall it */
2600Sstevel@tonic-gate 	prop = kex_buf2prop(&kex->my, NULL);
2610Sstevel@tonic-gate 	buffer_clear(&kex->my);
2620Sstevel@tonic-gate 	(kex->kex_hook)(kex, prop);
2630Sstevel@tonic-gate 	kex_prop2buf(&kex->my, prop);
2640Sstevel@tonic-gate 	kex_prop_free(prop);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2677574SJan.Pechanec@Sun.COM /* Initiate the key exchange by sending the SSH2_MSG_KEXINIT message. */
2687574SJan.Pechanec@Sun.COM void
kex_start(Kex * kex)2697574SJan.Pechanec@Sun.COM kex_start(Kex *kex)
2707574SJan.Pechanec@Sun.COM {
2717574SJan.Pechanec@Sun.COM 	kex_send_kexinit(kex);
2727574SJan.Pechanec@Sun.COM 	kex_reset_dispatch();
2737574SJan.Pechanec@Sun.COM }
2747574SJan.Pechanec@Sun.COM 
2757574SJan.Pechanec@Sun.COM /*
2767574SJan.Pechanec@Sun.COM  * Allocate a key exchange structure and populate it with a proposal we are
2777574SJan.Pechanec@Sun.COM  * going to use. This function does not start the actual key exchange.
2787574SJan.Pechanec@Sun.COM  */
2790Sstevel@tonic-gate Kex *
kex_setup(const char * host,char * proposal[PROPOSAL_MAX],Kex_hook_func hook)2800Sstevel@tonic-gate kex_setup(const char *host, char *proposal[PROPOSAL_MAX], Kex_hook_func hook)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	Kex	*kex;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	kex = xmalloc(sizeof(*kex));
2850Sstevel@tonic-gate 	memset(kex, 0, sizeof(*kex));
2860Sstevel@tonic-gate 	buffer_init(&kex->peer);
2870Sstevel@tonic-gate 	buffer_init(&kex->my);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	kex->kex_hook = hook; /* called by kex_send_kexinit() */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (host != NULL && *host != '\0')
2920Sstevel@tonic-gate 		kex->serverhost = xstrdup(host);
2930Sstevel@tonic-gate 	else
2940Sstevel@tonic-gate 		kex->server = 1;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	kex_prop2buf(&kex->my, proposal);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return kex;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static void
kex_kexinit_finish(Kex * kex)3020Sstevel@tonic-gate kex_kexinit_finish(Kex *kex)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	if (!(kex->flags & KEX_INIT_SENT))
3050Sstevel@tonic-gate 		kex_send_kexinit(kex);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	kex_choose_conf(kex);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
3100Sstevel@tonic-gate 	    kex->kex[kex->kex_type] != NULL)
3110Sstevel@tonic-gate 		(kex->kex[kex->kex_type])(kex);
3120Sstevel@tonic-gate 	else
3130Sstevel@tonic-gate 		fatal("Unsupported key exchange %d", kex->kex_type);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static void
choose_lang(char ** lang,char * client,char * server)3170Sstevel@tonic-gate choose_lang(char **lang, char *client, char *server)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	if (datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)
3200Sstevel@tonic-gate 		*lang = match_list(client, server, NULL);
3210Sstevel@tonic-gate 	else
3220Sstevel@tonic-gate 		*lang = g11n_srvr_locale_negotiate(client, NULL);
3230Sstevel@tonic-gate }
3248658SJan.Pechanec@Sun.COM 
3258658SJan.Pechanec@Sun.COM /*
3268658SJan.Pechanec@Sun.COM  * Make the message clear enough so that if this happens the user can figure out
3278658SJan.Pechanec@Sun.COM  * the workaround of changing the Ciphers option.
3288658SJan.Pechanec@Sun.COM  */
3298658SJan.Pechanec@Sun.COM #define	CLIENT_ERR_MSG							       \
3308658SJan.Pechanec@Sun.COM   "Client and server could not agree on a common cipher:\n"		       \
3318658SJan.Pechanec@Sun.COM   "  client: %s\n"							       \
3328658SJan.Pechanec@Sun.COM   "  server: %s\n"							       \
3338658SJan.Pechanec@Sun.COM   "\n"									       \
3348658SJan.Pechanec@Sun.COM   "The client cipher list can be controlled using the \"Ciphers\" option, \n"  \
3358658SJan.Pechanec@Sun.COM   "see ssh_config(4) for more information. The \"-o Ciphers=<cipher-list>\"\n" \
3368658SJan.Pechanec@Sun.COM   "option may be used to temporarily override the ciphers the client\n"	       \
3378658SJan.Pechanec@Sun.COM   "offers."
3388658SJan.Pechanec@Sun.COM 
3398658SJan.Pechanec@Sun.COM /*
3408658SJan.Pechanec@Sun.COM  * The server side message goes to syslogd and we do not want to send multiline
3418658SJan.Pechanec@Sun.COM  * messages there. What's more, the server side notification may be shorter
3428658SJan.Pechanec@Sun.COM  * since we expect that an administrator will deal with that, not the user.
3438658SJan.Pechanec@Sun.COM  */
3448658SJan.Pechanec@Sun.COM #define	SERVER_ERR_MSG							       \
3458658SJan.Pechanec@Sun.COM   "Client and server could not agree on a common cipher: client \"%s\", "      \
3468658SJan.Pechanec@Sun.COM   "server \"%s\". The server cipher list can be controlled using the "	       \
3478658SJan.Pechanec@Sun.COM   "\"Ciphers\" option, see sshd_config(4) for more information."
3488658SJan.Pechanec@Sun.COM 
3490Sstevel@tonic-gate static void
choose_enc(int is_server,Enc * enc,char * client,char * server)3508658SJan.Pechanec@Sun.COM choose_enc(int is_server, Enc *enc, char *client, char *server)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
3538658SJan.Pechanec@Sun.COM 
3548658SJan.Pechanec@Sun.COM 	if (name == NULL) {
3558658SJan.Pechanec@Sun.COM 		if (is_server == 1)
3568658SJan.Pechanec@Sun.COM 			fatal(SERVER_ERR_MSG, client, server);
3578658SJan.Pechanec@Sun.COM 		else
3588658SJan.Pechanec@Sun.COM 			fatal(CLIENT_ERR_MSG, client, server);
3598658SJan.Pechanec@Sun.COM 	}
3608658SJan.Pechanec@Sun.COM 
3610Sstevel@tonic-gate 	if ((enc->cipher = cipher_by_name(name)) == NULL)
3620Sstevel@tonic-gate 		fatal("matching cipher is not supported: %s", name);
3638658SJan.Pechanec@Sun.COM 
3640Sstevel@tonic-gate 	enc->name = name;
3650Sstevel@tonic-gate 	enc->enabled = 0;
3660Sstevel@tonic-gate 	enc->iv = NULL;
3670Sstevel@tonic-gate 	enc->key = NULL;
3680Sstevel@tonic-gate 	enc->key_len = cipher_keylen(enc->cipher);
3690Sstevel@tonic-gate 	enc->block_size = cipher_blocksize(enc->cipher);
3700Sstevel@tonic-gate }
3718658SJan.Pechanec@Sun.COM 
3720Sstevel@tonic-gate static void
choose_mac(Mac * mac,char * client,char * server)3730Sstevel@tonic-gate choose_mac(Mac *mac, char *client, char *server)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
3760Sstevel@tonic-gate 	if (name == NULL)
3779486SJan.Pechanec@Sun.COM 		fatal("no matching mac found: client %s server %s",
3789486SJan.Pechanec@Sun.COM 		    client, server);
3799486SJan.Pechanec@Sun.COM 	if (mac_setup(mac, name) < 0)
3800Sstevel@tonic-gate 		fatal("unsupported mac %s", name);
3810Sstevel@tonic-gate 	/* truncate the key */
3820Sstevel@tonic-gate 	if (datafellows & SSH_BUG_HMAC)
3830Sstevel@tonic-gate 		mac->key_len = 16;
3840Sstevel@tonic-gate 	mac->name = name;
3850Sstevel@tonic-gate 	mac->key = NULL;
3860Sstevel@tonic-gate 	mac->enabled = 0;
3870Sstevel@tonic-gate }
3889486SJan.Pechanec@Sun.COM 
3890Sstevel@tonic-gate static void
choose_comp(Comp * comp,char * client,char * server)3900Sstevel@tonic-gate choose_comp(Comp *comp, char *client, char *server)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
3930Sstevel@tonic-gate 	if (name == NULL)
3940Sstevel@tonic-gate 		fatal("no matching comp found: client %s server %s", client, server);
3950Sstevel@tonic-gate 	if (strcmp(name, "zlib") == 0) {
3960Sstevel@tonic-gate 		comp->type = 1;
3970Sstevel@tonic-gate 	} else if (strcmp(name, "none") == 0) {
3980Sstevel@tonic-gate 		comp->type = 0;
3990Sstevel@tonic-gate 	} else {
4000Sstevel@tonic-gate 		fatal("unsupported comp %s", name);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	comp->name = name;
4030Sstevel@tonic-gate }
4049486SJan.Pechanec@Sun.COM 
4050Sstevel@tonic-gate static void
choose_kex(Kex * k,char * client,char * server)4060Sstevel@tonic-gate choose_kex(Kex *k, char *client, char *server)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	k->name = match_list(client, server, NULL);
4090Sstevel@tonic-gate 	if (k->name == NULL)
4107574SJan.Pechanec@Sun.COM 		fatal("no common kex alg: client '%s', server '%s'", client,
4117574SJan.Pechanec@Sun.COM 		    server);
4120Sstevel@tonic-gate 	/* XXX Finish 3.6/7 merge of kex stuff -- choose_kex() done */
4130Sstevel@tonic-gate 	if (strcmp(k->name, KEX_DH1) == 0) {
4140Sstevel@tonic-gate 		k->kex_type = KEX_DH_GRP1_SHA1;
4150Sstevel@tonic-gate 	} else if (strcmp(k->name, KEX_DHGEX) == 0) {
4160Sstevel@tonic-gate 		k->kex_type = KEX_DH_GEX_SHA1;
4170Sstevel@tonic-gate #ifdef GSSAPI
4180Sstevel@tonic-gate 	} else if (strncmp(k->name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) == 0) {
4190Sstevel@tonic-gate 		k->kex_type = KEX_GSS_GRP1_SHA1;
4200Sstevel@tonic-gate #endif
4210Sstevel@tonic-gate 	} else
4220Sstevel@tonic-gate 		fatal("bad kex alg %s", k->name);
4230Sstevel@tonic-gate }
4249486SJan.Pechanec@Sun.COM 
4250Sstevel@tonic-gate static void
choose_hostkeyalg(Kex * k,char * client,char * server)4260Sstevel@tonic-gate choose_hostkeyalg(Kex *k, char *client, char *server)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	char *hostkeyalg = match_list(client, server, NULL);
4290Sstevel@tonic-gate 	if (hostkeyalg == NULL)
4300Sstevel@tonic-gate 		fatal("no hostkey alg");
4310Sstevel@tonic-gate 	k->hostkey_type = key_type_from_name(hostkeyalg);
4320Sstevel@tonic-gate 	if (k->hostkey_type == KEY_UNSPEC)
4330Sstevel@tonic-gate 		fatal("bad hostkey alg '%s'", hostkeyalg);
4340Sstevel@tonic-gate 	xfree(hostkeyalg);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static int
proposals_match(char * my[PROPOSAL_MAX],char * peer[PROPOSAL_MAX])4380Sstevel@tonic-gate proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	static int check[] = {
4410Sstevel@tonic-gate 		PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
4420Sstevel@tonic-gate 	};
4430Sstevel@tonic-gate 	int *idx;
4440Sstevel@tonic-gate 	char *p;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	for (idx = &check[0]; *idx != -1; idx++) {
4470Sstevel@tonic-gate 		if ((p = strchr(my[*idx], ',')) != NULL)
4480Sstevel@tonic-gate 			*p = '\0';
4490Sstevel@tonic-gate 		if ((p = strchr(peer[*idx], ',')) != NULL)
4500Sstevel@tonic-gate 			*p = '\0';
4510Sstevel@tonic-gate 		if (strcmp(my[*idx], peer[*idx]) != 0) {
4520Sstevel@tonic-gate 			debug2("proposal mismatch: my %s peer %s",
4530Sstevel@tonic-gate 			    my[*idx], peer[*idx]);
4540Sstevel@tonic-gate 			return (0);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	debug2("proposals match");
4580Sstevel@tonic-gate 	return (1);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate static void
kex_choose_conf(Kex * kex)4620Sstevel@tonic-gate kex_choose_conf(Kex *kex)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	Newkeys *newkeys;
4650Sstevel@tonic-gate 	char **my, **peer;
4660Sstevel@tonic-gate 	char **cprop, **sprop;
4670Sstevel@tonic-gate 	char *p_langs_c2s, *p_langs_s2c; /* peer's langs */
4680Sstevel@tonic-gate 	char *plangs = NULL;		 /* peer's langs*/
4690Sstevel@tonic-gate 	char *mlangs = NULL;		 /* my langs */
4700Sstevel@tonic-gate 	int nenc, nmac, ncomp;
4710Sstevel@tonic-gate 	int mode;
4720Sstevel@tonic-gate 	int ctos;				/* direction: if true client-to-server */
4730Sstevel@tonic-gate 	int need;
4740Sstevel@tonic-gate 	int first_kex_follows, type;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	my   = kex_buf2prop(&kex->my, NULL);
4770Sstevel@tonic-gate 	peer = kex_buf2prop(&kex->peer, &first_kex_follows);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if (kex->server) {
4800Sstevel@tonic-gate 		cprop=peer;
4810Sstevel@tonic-gate 		sprop=my;
4820Sstevel@tonic-gate 	} else {
4830Sstevel@tonic-gate 		cprop=my;
4840Sstevel@tonic-gate 		sprop=peer;
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* Algorithm Negotiation */
4880Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
4890Sstevel@tonic-gate 		newkeys = xmalloc(sizeof(*newkeys));
4900Sstevel@tonic-gate 		memset(newkeys, 0, sizeof(*newkeys));
4910Sstevel@tonic-gate 		kex->newkeys[mode] = newkeys;
4920Sstevel@tonic-gate 		ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
4930Sstevel@tonic-gate 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
4940Sstevel@tonic-gate 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
4950Sstevel@tonic-gate 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
4968658SJan.Pechanec@Sun.COM 		choose_enc(kex->server, &newkeys->enc,  cprop[nenc],  sprop[nenc]);
4978658SJan.Pechanec@Sun.COM 		choose_mac(&newkeys->mac,  cprop[nmac],  sprop[nmac]);
4980Sstevel@tonic-gate 		choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
4990Sstevel@tonic-gate 		debug("kex: %s %s %s %s",
5000Sstevel@tonic-gate 		    ctos ? "client->server" : "server->client",
5010Sstevel@tonic-gate 		    newkeys->enc.name,
5020Sstevel@tonic-gate 		    newkeys->mac.name,
5030Sstevel@tonic-gate 		    newkeys->comp.name);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
5060Sstevel@tonic-gate 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
5070Sstevel@tonic-gate 	    sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
5080Sstevel@tonic-gate 	need = 0;
5090Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
5100Sstevel@tonic-gate 		newkeys = kex->newkeys[mode];
5110Sstevel@tonic-gate 		if (need < newkeys->enc.key_len)
5120Sstevel@tonic-gate 			need = newkeys->enc.key_len;
5130Sstevel@tonic-gate 		if (need < newkeys->enc.block_size)
5140Sstevel@tonic-gate 			need = newkeys->enc.block_size;
5150Sstevel@tonic-gate 		if (need < newkeys->mac.key_len)
5160Sstevel@tonic-gate 			need = newkeys->mac.key_len;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 	/* XXX need runden? */
5190Sstevel@tonic-gate 	kex->we_need = need;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* ignore the next message if the proposals do not match */
5220Sstevel@tonic-gate 	if (first_kex_follows && !proposals_match(my, peer) &&
5230Sstevel@tonic-gate 	    !(datafellows & SSH_BUG_FIRSTKEX)) {
5240Sstevel@tonic-gate 		type = packet_read();
5250Sstevel@tonic-gate 		debug2("skipping next packet (type %u)", type);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/* Language/locale negotiation -- not worth doing on re-key */
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (!kex->initial_kex_done) {
5310Sstevel@tonic-gate 		p_langs_c2s = peer[PROPOSAL_LANG_CTOS];
5320Sstevel@tonic-gate 		p_langs_s2c = peer[PROPOSAL_LANG_STOC];
5330Sstevel@tonic-gate 		debug("Peer sent proposed langtags, ctos: %s", p_langs_c2s);
5340Sstevel@tonic-gate 		debug("Peer sent proposed langtags, stoc: %s", p_langs_s2c);
5350Sstevel@tonic-gate 		plangs = NULL;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		/* We propose the same langs for each protocol direction */
5380Sstevel@tonic-gate 		mlangs = my[PROPOSAL_LANG_STOC];
5390Sstevel@tonic-gate 		debug("We proposed langtags, ctos: %s", my[PROPOSAL_LANG_CTOS]);
5400Sstevel@tonic-gate 		debug("We proposed langtags, stoc: %s", mlangs);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		/*
5430Sstevel@tonic-gate 		 * Why oh why did they bother with negotiating langs for
5440Sstevel@tonic-gate 		 * each protocol direction?!
5450Sstevel@tonic-gate 		 *
5460Sstevel@tonic-gate 		 * The semantics of this are vaguely specified, but one can
5470Sstevel@tonic-gate 		 * imagine using one language (locale) for the whole session and
5480Sstevel@tonic-gate 		 * a different one for message localization (e.g., 'en_US.UTF-8'
5490Sstevel@tonic-gate 		 * overall and 'fr' for messages).  Weird?  Maybe.  But lang
5500Sstevel@tonic-gate 		 * tags don't include codeset info, like locales do...
5510Sstevel@tonic-gate 		 *
5520Sstevel@tonic-gate 		 * So, server-side we want:
5530Sstevel@tonic-gate 		 *  - setlocale(LC_ALL, c2s_locale);
5540Sstevel@tonic-gate 		 *  and
5550Sstevel@tonic-gate 		 *  - setlocale(LC_MESSAGES, s2c_locale);
5560Sstevel@tonic-gate 		 *
5570Sstevel@tonic-gate 		 * Client-side we don't really care.  But we could do:
5580Sstevel@tonic-gate 		 *
5590Sstevel@tonic-gate 		 *  - when very verbose, tell the use what lang the server's
5600Sstevel@tonic-gate 		 *    messages are in, if left out in the protocol
5610Sstevel@tonic-gate 		 *  - when sending messages to the server, and if applicable, we
5620Sstevel@tonic-gate 		 *    can localize them according to the language negotiated for
5630Sstevel@tonic-gate 		 *    that direction.
5640Sstevel@tonic-gate 		 *
5650Sstevel@tonic-gate 		 * But for now we do nothing on the client side.
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 		if ((p_langs_c2s && *p_langs_c2s) && !(p_langs_s2c && *p_langs_s2c))
5680Sstevel@tonic-gate 			plangs = p_langs_c2s;
5690Sstevel@tonic-gate 		else if ((p_langs_s2c && *p_langs_s2c) && !(p_langs_c2s && *p_langs_c2s))
5700Sstevel@tonic-gate 			plangs = p_langs_s2c;
5710Sstevel@tonic-gate 		else
5720Sstevel@tonic-gate 			plangs = p_langs_c2s;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 		if (kex->server) {
5750Sstevel@tonic-gate 			if (plangs && mlangs && *plangs && *mlangs) {
5760Sstevel@tonic-gate 				char *locale;
5770Sstevel@tonic-gate 
578*9600SNobutomo.Nakano@Sun.COM 				g11n_test_langtag(plangs, 1);
579*9600SNobutomo.Nakano@Sun.COM 
5800Sstevel@tonic-gate 				choose_lang(&locale, plangs, mlangs);
5810Sstevel@tonic-gate 				if (locale) {
5820Sstevel@tonic-gate 					g11n_setlocale(LC_ALL, locale);
5830Sstevel@tonic-gate 					debug("Negotiated main locale: %s", locale);
5840Sstevel@tonic-gate 					packet_send_debug("Negotiated main locale: %s", locale);
5855562Sjp161948 					xfree(locale);
5860Sstevel@tonic-gate 				}
5870Sstevel@tonic-gate 				if (plangs != p_langs_s2c &&
5880Sstevel@tonic-gate 				    p_langs_s2c && *p_langs_s2c) {
5890Sstevel@tonic-gate 					choose_lang(&locale, p_langs_s2c, mlangs);
5900Sstevel@tonic-gate 					if (locale) {
5910Sstevel@tonic-gate 						g11n_setlocale(LC_MESSAGES, locale);
5920Sstevel@tonic-gate 						debug("Negotiated messages locale: %s", locale);
5935562Sjp161948 						packet_send_debug("Negotiated "
5945562Sjp161948 						    "messages locale: %s", locale);
5955562Sjp161948 						xfree(locale);
5960Sstevel@tonic-gate 					}
5970Sstevel@tonic-gate 				}
5980Sstevel@tonic-gate 			}
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 		else {
6010Sstevel@tonic-gate 			if (plangs && mlangs && *plangs && *mlangs &&
6020Sstevel@tonic-gate 			    !(datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)) {
6030Sstevel@tonic-gate 				char *lang;
6040Sstevel@tonic-gate 				lang = g11n_clnt_langtag_negotiate(mlangs, plangs);
6050Sstevel@tonic-gate 				if (lang) {
6060Sstevel@tonic-gate 					session_lang = lang;
6070Sstevel@tonic-gate 					debug("Negotiated lang: %s", lang);
608*9600SNobutomo.Nakano@Sun.COM 					g11n_test_langtag(lang, 0);
6090Sstevel@tonic-gate 				}
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	kex_prop_free(my);
6150Sstevel@tonic-gate 	kex_prop_free(peer);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate static u_char *
derive_key(Kex * kex,int id,int need,u_char * hash,BIGNUM * shared_secret)6190Sstevel@tonic-gate derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	Buffer b;
6220Sstevel@tonic-gate 	const EVP_MD *evp_md = EVP_sha1();
6230Sstevel@tonic-gate 	EVP_MD_CTX md;
6240Sstevel@tonic-gate 	char c = id;
6250Sstevel@tonic-gate 	int have;
6260Sstevel@tonic-gate 	int mdsz = EVP_MD_size(evp_md);
6270Sstevel@tonic-gate 	u_char *digest = xmalloc(roundup(need, mdsz));
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	buffer_init(&b);
6300Sstevel@tonic-gate 	buffer_put_bignum2(&b, shared_secret);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/* K1 = HASH(K || H || "A" || session_id) */
6330Sstevel@tonic-gate 	EVP_DigestInit(&md, evp_md);
6340Sstevel@tonic-gate 	if (!(datafellows & SSH_BUG_DERIVEKEY))
6350Sstevel@tonic-gate 		EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
6360Sstevel@tonic-gate 	EVP_DigestUpdate(&md, hash, mdsz);
6370Sstevel@tonic-gate 	EVP_DigestUpdate(&md, &c, 1);
6380Sstevel@tonic-gate 	EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
6390Sstevel@tonic-gate 	EVP_DigestFinal(&md, digest, NULL);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * expand key:
6430Sstevel@tonic-gate 	 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
6440Sstevel@tonic-gate 	 * Key = K1 || K2 || ... || Kn
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	for (have = mdsz; need > have; have += mdsz) {
6470Sstevel@tonic-gate 		EVP_DigestInit(&md, evp_md);
6480Sstevel@tonic-gate 		if (!(datafellows & SSH_BUG_DERIVEKEY))
6490Sstevel@tonic-gate 			EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
6500Sstevel@tonic-gate 		EVP_DigestUpdate(&md, hash, mdsz);
6510Sstevel@tonic-gate 		EVP_DigestUpdate(&md, digest, have);
6520Sstevel@tonic-gate 		EVP_DigestFinal(&md, digest + have, NULL);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 	buffer_free(&b);
6550Sstevel@tonic-gate #ifdef DEBUG_KEX
6560Sstevel@tonic-gate 	fprintf(stderr, "key '%c'== ", c);
6570Sstevel@tonic-gate 	dump_digest("key", digest, need);
6580Sstevel@tonic-gate #endif
6590Sstevel@tonic-gate 	return digest;
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate Newkeys *current_keys[MODE_MAX];
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate #define NKEYS	6
6650Sstevel@tonic-gate void
kex_derive_keys(Kex * kex,u_char * hash,BIGNUM * shared_secret)6660Sstevel@tonic-gate kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	u_char *keys[NKEYS];
6690Sstevel@tonic-gate 	int i, mode, ctos;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	for (i = 0; i < NKEYS; i++)
6720Sstevel@tonic-gate 		keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	debug2("kex_derive_keys");
6750Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
6760Sstevel@tonic-gate 		current_keys[mode] = kex->newkeys[mode];
6770Sstevel@tonic-gate 		kex->newkeys[mode] = NULL;
6780Sstevel@tonic-gate 		ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
6790Sstevel@tonic-gate 		current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
6800Sstevel@tonic-gate 		current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
6810Sstevel@tonic-gate 		current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate Newkeys *
kex_get_newkeys(int mode)6860Sstevel@tonic-gate kex_get_newkeys(int mode)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	Newkeys *ret;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	ret = current_keys[mode];
6910Sstevel@tonic-gate 	current_keys[mode] = NULL;
6920Sstevel@tonic-gate 	return ret;
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
6960Sstevel@tonic-gate void
dump_digest(char * msg,u_char * digest,int len)6970Sstevel@tonic-gate dump_digest(char *msg, u_char *digest, int len)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	int i;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	fprintf(stderr, "%s\n", msg);
7020Sstevel@tonic-gate 	for (i = 0; i< len; i++) {
7030Sstevel@tonic-gate 		fprintf(stderr, "%02x", digest[i]);
7040Sstevel@tonic-gate 		if (i%32 == 31)
7050Sstevel@tonic-gate 			fprintf(stderr, "\n");
7060Sstevel@tonic-gate 		else if (i%8 == 7)
7070Sstevel@tonic-gate 			fprintf(stderr, " ");
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	fprintf(stderr, "\n");
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate #endif
712