xref: /titanic_41/usr/src/cmd/ssh/libssh/common/kex.c (revision 6f786ace10b9c0c7c5515e525fb660fbccfda6a3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  *
24e63a6e29SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "includes.h"
297c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: kex.c,v 1.51 2002/06/24 14:55:38 markus Exp $");
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "ssh2.h"
367c478bd9Sstevel@tonic-gate #include "xmalloc.h"
377c478bd9Sstevel@tonic-gate #include "buffer.h"
387c478bd9Sstevel@tonic-gate #include "bufaux.h"
397c478bd9Sstevel@tonic-gate #include "packet.h"
407c478bd9Sstevel@tonic-gate #include "compat.h"
417c478bd9Sstevel@tonic-gate #include "cipher.h"
427c478bd9Sstevel@tonic-gate #include "kex.h"
437c478bd9Sstevel@tonic-gate #include "key.h"
447c478bd9Sstevel@tonic-gate #include "log.h"
457c478bd9Sstevel@tonic-gate #include "mac.h"
467c478bd9Sstevel@tonic-gate #include "match.h"
477c478bd9Sstevel@tonic-gate #include "dispatch.h"
487c478bd9Sstevel@tonic-gate #include "g11n.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifdef GSSAPI
517c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define KEX_COOKIE_LEN	16
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate char *session_lang = NULL;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* prototype */
607c478bd9Sstevel@tonic-gate static void kex_do_hook(Kex *kex);
617c478bd9Sstevel@tonic-gate static void kex_kexinit_finish(Kex *);
627c478bd9Sstevel@tonic-gate static void kex_choose_conf(Kex *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* put algorithm proposal into buffer */
657c478bd9Sstevel@tonic-gate static
667c478bd9Sstevel@tonic-gate void
kex_prop2buf(Buffer * b,char * proposal[PROPOSAL_MAX])677c478bd9Sstevel@tonic-gate kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int i;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	buffer_clear(b);
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * add a dummy cookie, the cookie will be overwritten by
747c478bd9Sstevel@tonic-gate 	 * kex_send_kexinit(), each time a kexinit is set
757c478bd9Sstevel@tonic-gate 	 */
767c478bd9Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
777c478bd9Sstevel@tonic-gate 		buffer_put_char(b, 0);
787c478bd9Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
797c478bd9Sstevel@tonic-gate 		buffer_put_cstring(b, proposal[i]);
807c478bd9Sstevel@tonic-gate 	buffer_put_char(b, 0);			/* first_kex_packet_follows */
817c478bd9Sstevel@tonic-gate 	buffer_put_int(b, 0);			/* uint32 reserved */
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* parse buffer and return algorithm proposal */
857c478bd9Sstevel@tonic-gate static
867c478bd9Sstevel@tonic-gate char **
kex_buf2prop(Buffer * raw,int * first_kex_follows)877c478bd9Sstevel@tonic-gate kex_buf2prop(Buffer *raw, int *first_kex_follows)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	Buffer b;
907c478bd9Sstevel@tonic-gate 	int i;
917c478bd9Sstevel@tonic-gate 	char **proposal;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	proposal = xmalloc(PROPOSAL_MAX * sizeof(char *));
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	buffer_init(&b);
967c478bd9Sstevel@tonic-gate 	buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
977c478bd9Sstevel@tonic-gate 	/* skip cookie */
987c478bd9Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
997c478bd9Sstevel@tonic-gate 		buffer_get_char(&b);
1007c478bd9Sstevel@tonic-gate 	/* extract kex init proposal strings */
1017c478bd9Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++) {
1027c478bd9Sstevel@tonic-gate 		proposal[i] = buffer_get_string(&b,NULL);
1037c478bd9Sstevel@tonic-gate 		debug2("kex_parse_kexinit: %s", proposal[i]);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	/* first kex follows / reserved */
1067c478bd9Sstevel@tonic-gate 	i = buffer_get_char(&b);
1077c478bd9Sstevel@tonic-gate 	if (first_kex_follows != NULL)
1087c478bd9Sstevel@tonic-gate 		*first_kex_follows = i;
1097c478bd9Sstevel@tonic-gate 	debug2("kex_parse_kexinit: first_kex_follows %d ", i);
1107c478bd9Sstevel@tonic-gate 	i = buffer_get_int(&b);
1117c478bd9Sstevel@tonic-gate 	debug2("kex_parse_kexinit: reserved %d ", i);
1127c478bd9Sstevel@tonic-gate 	buffer_free(&b);
1137c478bd9Sstevel@tonic-gate 	return proposal;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static
1177c478bd9Sstevel@tonic-gate void
kex_prop_free(char ** proposal)1187c478bd9Sstevel@tonic-gate kex_prop_free(char **proposal)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int i;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
1237c478bd9Sstevel@tonic-gate 		xfree(proposal[i]);
1247c478bd9Sstevel@tonic-gate 	xfree(proposal);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate static void
kex_protocol_error(int type,u_int32_t seq,void * ctxt)1287c478bd9Sstevel@tonic-gate kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	error("Hm, kex protocol error: type %d seq %u", type, seq);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static void
kex_reset_dispatch(void)1347c478bd9Sstevel@tonic-gate kex_reset_dispatch(void)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
1377c478bd9Sstevel@tonic-gate 	/* unprivileged sshd has a kex packet handler that must not be reset */
1387c478bd9Sstevel@tonic-gate 	debug3("kex_reset_dispatch -- should we dispatch_set(KEXINIT) here? %d && !%d",
1397c478bd9Sstevel@tonic-gate 		packet_is_server(), packet_is_monitor());
1407c478bd9Sstevel@tonic-gate 	if (packet_is_server() && !packet_is_monitor()) {
1417c478bd9Sstevel@tonic-gate 		debug3("kex_reset_dispatch -- skipping dispatch_set(KEXINIT) in unpriv proc");
1427c478bd9Sstevel@tonic-gate 		return;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	dispatch_range(SSH2_MSG_TRANSPORT_MIN,
1477c478bd9Sstevel@tonic-gate 	    SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1487c478bd9Sstevel@tonic-gate 	dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate void
kex_finish(Kex * kex)1527c478bd9Sstevel@tonic-gate kex_finish(Kex *kex)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	kex_reset_dispatch();
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	packet_start(SSH2_MSG_NEWKEYS);
1577c478bd9Sstevel@tonic-gate 	packet_send();
1587c478bd9Sstevel@tonic-gate 	/* packet_write_wait(); */
1597c478bd9Sstevel@tonic-gate 	debug("SSH2_MSG_NEWKEYS sent");
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
1627c478bd9Sstevel@tonic-gate 	if (packet_is_monitor())
1637c478bd9Sstevel@tonic-gate 		goto skip_newkeys;
1647c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1657c478bd9Sstevel@tonic-gate 	debug("expecting SSH2_MSG_NEWKEYS");
1667c478bd9Sstevel@tonic-gate 	packet_read_expect(SSH2_MSG_NEWKEYS);
1677c478bd9Sstevel@tonic-gate 	packet_check_eom();
1687c478bd9Sstevel@tonic-gate 	debug("SSH2_MSG_NEWKEYS received");
1697c478bd9Sstevel@tonic-gate #ifdef ALTPRIVSEP
1707c478bd9Sstevel@tonic-gate skip_newkeys:
1717c478bd9Sstevel@tonic-gate #endif /* ALTPRIVSEP */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	kex->done = 1;
1747c478bd9Sstevel@tonic-gate 	kex->initial_kex_done = 1; /* never to be cleared once set */
1757c478bd9Sstevel@tonic-gate 	buffer_clear(&kex->peer);
1767c478bd9Sstevel@tonic-gate 	/* buffer_clear(&kex->my); */
1777c478bd9Sstevel@tonic-gate 	kex->flags &= ~KEX_INIT_SENT;
1787c478bd9Sstevel@tonic-gate 	xfree(kex->name);
1797c478bd9Sstevel@tonic-gate 	kex->name = NULL;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate void
kex_send_kexinit(Kex * kex)1837c478bd9Sstevel@tonic-gate kex_send_kexinit(Kex *kex)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	u_int32_t rand = 0;
1867c478bd9Sstevel@tonic-gate 	u_char *cookie;
1877c478bd9Sstevel@tonic-gate 	int i;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (kex == NULL) {
1907c478bd9Sstevel@tonic-gate 		error("kex_send_kexinit: no kex, cannot rekey");
1917c478bd9Sstevel@tonic-gate 		return;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	if (kex->flags & KEX_INIT_SENT) {
1947c478bd9Sstevel@tonic-gate 		debug("KEX_INIT_SENT");
1957c478bd9Sstevel@tonic-gate 		return;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	kex->done = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* update my proposal -- e.g., add/remove GSS kexalgs */
2007c478bd9Sstevel@tonic-gate 	kex_do_hook(kex);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* generate a random cookie */
2037c478bd9Sstevel@tonic-gate 	if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
2047c478bd9Sstevel@tonic-gate 		fatal("kex_send_kexinit: kex proposal too short");
2057c478bd9Sstevel@tonic-gate 	cookie = buffer_ptr(&kex->my);
2067c478bd9Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++) {
2077c478bd9Sstevel@tonic-gate 		if (i % 4 == 0)
2087c478bd9Sstevel@tonic-gate 			rand = arc4random();
2097c478bd9Sstevel@tonic-gate 		cookie[i] = rand;
2107c478bd9Sstevel@tonic-gate 		rand >>= 8;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	packet_start(SSH2_MSG_KEXINIT);
2137c478bd9Sstevel@tonic-gate 	packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
2147c478bd9Sstevel@tonic-gate 	packet_send();
2157c478bd9Sstevel@tonic-gate 	debug("SSH2_MSG_KEXINIT sent");
2167c478bd9Sstevel@tonic-gate 	kex->flags |= KEX_INIT_SENT;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate void
kex_input_kexinit(int type,u_int32_t seq,void * ctxt)2207c478bd9Sstevel@tonic-gate kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	char *ptr;
2237c478bd9Sstevel@tonic-gate 	u_int dlen;
2247c478bd9Sstevel@tonic-gate 	int i;
2257c478bd9Sstevel@tonic-gate 	Kex *kex = (Kex *)ctxt;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	debug("SSH2_MSG_KEXINIT received");
2287c478bd9Sstevel@tonic-gate 	if (kex == NULL)
2297c478bd9Sstevel@tonic-gate 		fatal("kex_input_kexinit: no kex, cannot rekey");
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	ptr = packet_get_raw(&dlen);
2327c478bd9Sstevel@tonic-gate 	buffer_append(&kex->peer, ptr, dlen);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* discard packet */
2357c478bd9Sstevel@tonic-gate 	for (i = 0; i < KEX_COOKIE_LEN; i++)
2367c478bd9Sstevel@tonic-gate 		packet_get_char();
2377c478bd9Sstevel@tonic-gate 	for (i = 0; i < PROPOSAL_MAX; i++)
2387c478bd9Sstevel@tonic-gate 		xfree(packet_get_string(NULL));
2397c478bd9Sstevel@tonic-gate 	(void) packet_get_char();
2407c478bd9Sstevel@tonic-gate 	(void) packet_get_int();
2417c478bd9Sstevel@tonic-gate 	packet_check_eom();
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	kex_kexinit_finish(kex);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * This is for GSS keyex, where actual KEX offer can change at rekey
2487c478bd9Sstevel@tonic-gate  * time due to credential expiration/renewal...
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static
2517c478bd9Sstevel@tonic-gate void
kex_do_hook(Kex * kex)2527c478bd9Sstevel@tonic-gate kex_do_hook(Kex *kex)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	char    **prop;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (kex->kex_hook == NULL)
2577c478bd9Sstevel@tonic-gate 		return;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* Unmarshall my proposal, let the hook modify it, remarshall it */
2607c478bd9Sstevel@tonic-gate 	prop = kex_buf2prop(&kex->my, NULL);
2617c478bd9Sstevel@tonic-gate 	buffer_clear(&kex->my);
2627c478bd9Sstevel@tonic-gate 	(kex->kex_hook)(kex, prop);
2637c478bd9Sstevel@tonic-gate 	kex_prop2buf(&kex->my, prop);
2647c478bd9Sstevel@tonic-gate 	kex_prop_free(prop);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
267cd7d5fafSJan Pechanec /* Initiate the key exchange by sending the SSH2_MSG_KEXINIT message. */
268cd7d5fafSJan Pechanec void
kex_start(Kex * kex)269cd7d5fafSJan Pechanec kex_start(Kex *kex)
270cd7d5fafSJan Pechanec {
271cd7d5fafSJan Pechanec 	kex_send_kexinit(kex);
272cd7d5fafSJan Pechanec 	kex_reset_dispatch();
273cd7d5fafSJan Pechanec }
274cd7d5fafSJan Pechanec 
275cd7d5fafSJan Pechanec /*
276cd7d5fafSJan Pechanec  * Allocate a key exchange structure and populate it with a proposal we are
277cd7d5fafSJan Pechanec  * going to use. This function does not start the actual key exchange.
278cd7d5fafSJan Pechanec  */
2797c478bd9Sstevel@tonic-gate Kex *
kex_setup(const char * host,char * proposal[PROPOSAL_MAX],Kex_hook_func hook)2807c478bd9Sstevel@tonic-gate kex_setup(const char *host, char *proposal[PROPOSAL_MAX], Kex_hook_func hook)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	Kex	*kex;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	kex = xmalloc(sizeof(*kex));
2857c478bd9Sstevel@tonic-gate 	memset(kex, 0, sizeof(*kex));
2867c478bd9Sstevel@tonic-gate 	buffer_init(&kex->peer);
2877c478bd9Sstevel@tonic-gate 	buffer_init(&kex->my);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	kex->kex_hook = hook; /* called by kex_send_kexinit() */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (host != NULL && *host != '\0')
2927c478bd9Sstevel@tonic-gate 		kex->serverhost = xstrdup(host);
2937c478bd9Sstevel@tonic-gate 	else
2947c478bd9Sstevel@tonic-gate 		kex->server = 1;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	kex_prop2buf(&kex->my, proposal);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return kex;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void
kex_kexinit_finish(Kex * kex)3027c478bd9Sstevel@tonic-gate kex_kexinit_finish(Kex *kex)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if (!(kex->flags & KEX_INIT_SENT))
3057c478bd9Sstevel@tonic-gate 		kex_send_kexinit(kex);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	kex_choose_conf(kex);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
3107c478bd9Sstevel@tonic-gate 	    kex->kex[kex->kex_type] != NULL)
3117c478bd9Sstevel@tonic-gate 		(kex->kex[kex->kex_type])(kex);
3127c478bd9Sstevel@tonic-gate 	else
3137c478bd9Sstevel@tonic-gate 		fatal("Unsupported key exchange %d", kex->kex_type);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static void
choose_lang(char ** lang,char * client,char * server)3177c478bd9Sstevel@tonic-gate choose_lang(char **lang, char *client, char *server)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	if (datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)
3207c478bd9Sstevel@tonic-gate 		*lang = match_list(client, server, NULL);
3217c478bd9Sstevel@tonic-gate 	else
3227c478bd9Sstevel@tonic-gate 		*lang = g11n_srvr_locale_negotiate(client, NULL);
3237c478bd9Sstevel@tonic-gate }
324e63a6e29SJan Pechanec 
325e63a6e29SJan Pechanec /*
326e63a6e29SJan Pechanec  * Make the message clear enough so that if this happens the user can figure out
327e63a6e29SJan Pechanec  * the workaround of changing the Ciphers option.
328e63a6e29SJan Pechanec  */
329e63a6e29SJan Pechanec #define	CLIENT_ERR_MSG							       \
330e63a6e29SJan Pechanec   "Client and server could not agree on a common cipher:\n"		       \
331e63a6e29SJan Pechanec   "  client: %s\n"							       \
332e63a6e29SJan Pechanec   "  server: %s\n"							       \
333e63a6e29SJan Pechanec   "\n"									       \
334e63a6e29SJan Pechanec   "The client cipher list can be controlled using the \"Ciphers\" option, \n"  \
335e63a6e29SJan Pechanec   "see ssh_config(4) for more information. The \"-o Ciphers=<cipher-list>\"\n" \
336e63a6e29SJan Pechanec   "option may be used to temporarily override the ciphers the client\n"	       \
337e63a6e29SJan Pechanec   "offers."
338e63a6e29SJan Pechanec 
339e63a6e29SJan Pechanec /*
340e63a6e29SJan Pechanec  * The server side message goes to syslogd and we do not want to send multiline
341e63a6e29SJan Pechanec  * messages there. What's more, the server side notification may be shorter
342e63a6e29SJan Pechanec  * since we expect that an administrator will deal with that, not the user.
343e63a6e29SJan Pechanec  */
344e63a6e29SJan Pechanec #define	SERVER_ERR_MSG							       \
345e63a6e29SJan Pechanec   "Client and server could not agree on a common cipher: client \"%s\", "      \
346e63a6e29SJan Pechanec   "server \"%s\". The server cipher list can be controlled using the "	       \
347e63a6e29SJan Pechanec   "\"Ciphers\" option, see sshd_config(4) for more information."
348e63a6e29SJan Pechanec 
3497c478bd9Sstevel@tonic-gate static void
choose_enc(int is_server,Enc * enc,char * client,char * server)350e63a6e29SJan Pechanec choose_enc(int is_server, Enc *enc, char *client, char *server)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
353e63a6e29SJan Pechanec 
354e63a6e29SJan Pechanec 	if (name == NULL) {
355e63a6e29SJan Pechanec 		if (is_server == 1)
356e63a6e29SJan Pechanec 			fatal(SERVER_ERR_MSG, client, server);
357e63a6e29SJan Pechanec 		else
358e63a6e29SJan Pechanec 			fatal(CLIENT_ERR_MSG, client, server);
359e63a6e29SJan Pechanec 	}
360e63a6e29SJan Pechanec 
3617c478bd9Sstevel@tonic-gate 	if ((enc->cipher = cipher_by_name(name)) == NULL)
3627c478bd9Sstevel@tonic-gate 		fatal("matching cipher is not supported: %s", name);
363e63a6e29SJan Pechanec 
3647c478bd9Sstevel@tonic-gate 	enc->name = name;
3657c478bd9Sstevel@tonic-gate 	enc->enabled = 0;
3667c478bd9Sstevel@tonic-gate 	enc->iv = NULL;
3677c478bd9Sstevel@tonic-gate 	enc->key = NULL;
3687c478bd9Sstevel@tonic-gate 	enc->key_len = cipher_keylen(enc->cipher);
3697c478bd9Sstevel@tonic-gate 	enc->block_size = cipher_blocksize(enc->cipher);
3707c478bd9Sstevel@tonic-gate }
371e63a6e29SJan Pechanec 
3727c478bd9Sstevel@tonic-gate static void
choose_mac(Mac * mac,char * client,char * server)3737c478bd9Sstevel@tonic-gate choose_mac(Mac *mac, char *client, char *server)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
3767c478bd9Sstevel@tonic-gate 	if (name == NULL)
3778caf082fSJan Pechanec 		fatal("no matching mac found: client %s server %s",
3788caf082fSJan Pechanec 		    client, server);
3798caf082fSJan Pechanec 	if (mac_setup(mac, name) < 0)
3807c478bd9Sstevel@tonic-gate 		fatal("unsupported mac %s", name);
3817c478bd9Sstevel@tonic-gate 	/* truncate the key */
3827c478bd9Sstevel@tonic-gate 	if (datafellows & SSH_BUG_HMAC)
3837c478bd9Sstevel@tonic-gate 		mac->key_len = 16;
3847c478bd9Sstevel@tonic-gate 	mac->name = name;
3857c478bd9Sstevel@tonic-gate 	mac->key = NULL;
3867c478bd9Sstevel@tonic-gate 	mac->enabled = 0;
3877c478bd9Sstevel@tonic-gate }
3888caf082fSJan Pechanec 
3897c478bd9Sstevel@tonic-gate static void
choose_comp(Comp * comp,char * client,char * server)3907c478bd9Sstevel@tonic-gate choose_comp(Comp *comp, char *client, char *server)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	char *name = match_list(client, server, NULL);
3937c478bd9Sstevel@tonic-gate 	if (name == NULL)
3947c478bd9Sstevel@tonic-gate 		fatal("no matching comp found: client %s server %s", client, server);
3957c478bd9Sstevel@tonic-gate 	if (strcmp(name, "zlib") == 0) {
3967c478bd9Sstevel@tonic-gate 		comp->type = 1;
3977c478bd9Sstevel@tonic-gate 	} else if (strcmp(name, "none") == 0) {
3987c478bd9Sstevel@tonic-gate 		comp->type = 0;
3997c478bd9Sstevel@tonic-gate 	} else {
4007c478bd9Sstevel@tonic-gate 		fatal("unsupported comp %s", name);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	comp->name = name;
4037c478bd9Sstevel@tonic-gate }
4048caf082fSJan Pechanec 
4057c478bd9Sstevel@tonic-gate static void
choose_kex(Kex * k,char * client,char * server)4067c478bd9Sstevel@tonic-gate choose_kex(Kex *k, char *client, char *server)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	k->name = match_list(client, server, NULL);
4097c478bd9Sstevel@tonic-gate 	if (k->name == NULL)
410cd7d5fafSJan Pechanec 		fatal("no common kex alg: client '%s', server '%s'", client,
411cd7d5fafSJan Pechanec 		    server);
4127c478bd9Sstevel@tonic-gate 	/* XXX Finish 3.6/7 merge of kex stuff -- choose_kex() done */
4137c478bd9Sstevel@tonic-gate 	if (strcmp(k->name, KEX_DH1) == 0) {
4147c478bd9Sstevel@tonic-gate 		k->kex_type = KEX_DH_GRP1_SHA1;
4157c478bd9Sstevel@tonic-gate 	} else if (strcmp(k->name, KEX_DHGEX) == 0) {
4167c478bd9Sstevel@tonic-gate 		k->kex_type = KEX_DH_GEX_SHA1;
4177c478bd9Sstevel@tonic-gate #ifdef GSSAPI
4187c478bd9Sstevel@tonic-gate 	} else if (strncmp(k->name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) == 0) {
4197c478bd9Sstevel@tonic-gate 		k->kex_type = KEX_GSS_GRP1_SHA1;
4207c478bd9Sstevel@tonic-gate #endif
4217c478bd9Sstevel@tonic-gate 	} else
4227c478bd9Sstevel@tonic-gate 		fatal("bad kex alg %s", k->name);
4237c478bd9Sstevel@tonic-gate }
4248caf082fSJan Pechanec 
4257c478bd9Sstevel@tonic-gate static void
choose_hostkeyalg(Kex * k,char * client,char * server)4267c478bd9Sstevel@tonic-gate choose_hostkeyalg(Kex *k, char *client, char *server)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	char *hostkeyalg = match_list(client, server, NULL);
4297c478bd9Sstevel@tonic-gate 	if (hostkeyalg == NULL)
4307c478bd9Sstevel@tonic-gate 		fatal("no hostkey alg");
4317c478bd9Sstevel@tonic-gate 	k->hostkey_type = key_type_from_name(hostkeyalg);
4327c478bd9Sstevel@tonic-gate 	if (k->hostkey_type == KEY_UNSPEC)
4337c478bd9Sstevel@tonic-gate 		fatal("bad hostkey alg '%s'", hostkeyalg);
4347c478bd9Sstevel@tonic-gate 	xfree(hostkeyalg);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static int
proposals_match(char * my[PROPOSAL_MAX],char * peer[PROPOSAL_MAX])4387c478bd9Sstevel@tonic-gate proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	static int check[] = {
4417c478bd9Sstevel@tonic-gate 		PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
4427c478bd9Sstevel@tonic-gate 	};
4437c478bd9Sstevel@tonic-gate 	int *idx;
4447c478bd9Sstevel@tonic-gate 	char *p;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	for (idx = &check[0]; *idx != -1; idx++) {
4477c478bd9Sstevel@tonic-gate 		if ((p = strchr(my[*idx], ',')) != NULL)
4487c478bd9Sstevel@tonic-gate 			*p = '\0';
4497c478bd9Sstevel@tonic-gate 		if ((p = strchr(peer[*idx], ',')) != NULL)
4507c478bd9Sstevel@tonic-gate 			*p = '\0';
4517c478bd9Sstevel@tonic-gate 		if (strcmp(my[*idx], peer[*idx]) != 0) {
4527c478bd9Sstevel@tonic-gate 			debug2("proposal mismatch: my %s peer %s",
4537c478bd9Sstevel@tonic-gate 			    my[*idx], peer[*idx]);
4547c478bd9Sstevel@tonic-gate 			return (0);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	debug2("proposals match");
4587c478bd9Sstevel@tonic-gate 	return (1);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate static void
kex_choose_conf(Kex * kex)4627c478bd9Sstevel@tonic-gate kex_choose_conf(Kex *kex)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	Newkeys *newkeys;
4657c478bd9Sstevel@tonic-gate 	char **my, **peer;
4667c478bd9Sstevel@tonic-gate 	char **cprop, **sprop;
4677c478bd9Sstevel@tonic-gate 	char *p_langs_c2s, *p_langs_s2c; /* peer's langs */
4687c478bd9Sstevel@tonic-gate 	char *plangs = NULL;		 /* peer's langs*/
4697c478bd9Sstevel@tonic-gate 	char *mlangs = NULL;		 /* my langs */
4707c478bd9Sstevel@tonic-gate 	int nenc, nmac, ncomp;
4717c478bd9Sstevel@tonic-gate 	int mode;
4727c478bd9Sstevel@tonic-gate 	int ctos;				/* direction: if true client-to-server */
4737c478bd9Sstevel@tonic-gate 	int need;
4747c478bd9Sstevel@tonic-gate 	int first_kex_follows, type;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	my   = kex_buf2prop(&kex->my, NULL);
4777c478bd9Sstevel@tonic-gate 	peer = kex_buf2prop(&kex->peer, &first_kex_follows);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (kex->server) {
4807c478bd9Sstevel@tonic-gate 		cprop=peer;
4817c478bd9Sstevel@tonic-gate 		sprop=my;
4827c478bd9Sstevel@tonic-gate 	} else {
4837c478bd9Sstevel@tonic-gate 		cprop=my;
4847c478bd9Sstevel@tonic-gate 		sprop=peer;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* Algorithm Negotiation */
4887c478bd9Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
4897c478bd9Sstevel@tonic-gate 		newkeys = xmalloc(sizeof(*newkeys));
4907c478bd9Sstevel@tonic-gate 		memset(newkeys, 0, sizeof(*newkeys));
4917c478bd9Sstevel@tonic-gate 		kex->newkeys[mode] = newkeys;
4927c478bd9Sstevel@tonic-gate 		ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
4937c478bd9Sstevel@tonic-gate 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
4947c478bd9Sstevel@tonic-gate 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
4957c478bd9Sstevel@tonic-gate 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
496e63a6e29SJan Pechanec 		choose_enc(kex->server, &newkeys->enc,  cprop[nenc],  sprop[nenc]);
4977c478bd9Sstevel@tonic-gate 		choose_mac(&newkeys->mac,  cprop[nmac],  sprop[nmac]);
4987c478bd9Sstevel@tonic-gate 		choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
4997c478bd9Sstevel@tonic-gate 		debug("kex: %s %s %s %s",
5007c478bd9Sstevel@tonic-gate 		    ctos ? "client->server" : "server->client",
5017c478bd9Sstevel@tonic-gate 		    newkeys->enc.name,
5027c478bd9Sstevel@tonic-gate 		    newkeys->mac.name,
5037c478bd9Sstevel@tonic-gate 		    newkeys->comp.name);
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
5067c478bd9Sstevel@tonic-gate 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
5077c478bd9Sstevel@tonic-gate 	    sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
5087c478bd9Sstevel@tonic-gate 	need = 0;
5097c478bd9Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
5107c478bd9Sstevel@tonic-gate 		newkeys = kex->newkeys[mode];
5117c478bd9Sstevel@tonic-gate 		if (need < newkeys->enc.key_len)
5127c478bd9Sstevel@tonic-gate 			need = newkeys->enc.key_len;
5137c478bd9Sstevel@tonic-gate 		if (need < newkeys->enc.block_size)
5147c478bd9Sstevel@tonic-gate 			need = newkeys->enc.block_size;
5157c478bd9Sstevel@tonic-gate 		if (need < newkeys->mac.key_len)
5167c478bd9Sstevel@tonic-gate 			need = newkeys->mac.key_len;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	/* XXX need runden? */
5197c478bd9Sstevel@tonic-gate 	kex->we_need = need;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* ignore the next message if the proposals do not match */
5227c478bd9Sstevel@tonic-gate 	if (first_kex_follows && !proposals_match(my, peer) &&
5237c478bd9Sstevel@tonic-gate 	    !(datafellows & SSH_BUG_FIRSTKEX)) {
5247c478bd9Sstevel@tonic-gate 		type = packet_read();
5257c478bd9Sstevel@tonic-gate 		debug2("skipping next packet (type %u)", type);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/* Language/locale negotiation -- not worth doing on re-key */
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (!kex->initial_kex_done) {
5317c478bd9Sstevel@tonic-gate 		p_langs_c2s = peer[PROPOSAL_LANG_CTOS];
5327c478bd9Sstevel@tonic-gate 		p_langs_s2c = peer[PROPOSAL_LANG_STOC];
5337c478bd9Sstevel@tonic-gate 		debug("Peer sent proposed langtags, ctos: %s", p_langs_c2s);
5347c478bd9Sstevel@tonic-gate 		debug("Peer sent proposed langtags, stoc: %s", p_langs_s2c);
5357c478bd9Sstevel@tonic-gate 		plangs = NULL;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		/* We propose the same langs for each protocol direction */
5387c478bd9Sstevel@tonic-gate 		mlangs = my[PROPOSAL_LANG_STOC];
5397c478bd9Sstevel@tonic-gate 		debug("We proposed langtags, ctos: %s", my[PROPOSAL_LANG_CTOS]);
5407c478bd9Sstevel@tonic-gate 		debug("We proposed langtags, stoc: %s", mlangs);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		/*
5437c478bd9Sstevel@tonic-gate 		 * Why oh why did they bother with negotiating langs for
5447c478bd9Sstevel@tonic-gate 		 * each protocol direction?!
5457c478bd9Sstevel@tonic-gate 		 *
5467c478bd9Sstevel@tonic-gate 		 * The semantics of this are vaguely specified, but one can
5477c478bd9Sstevel@tonic-gate 		 * imagine using one language (locale) for the whole session and
5487c478bd9Sstevel@tonic-gate 		 * a different one for message localization (e.g., 'en_US.UTF-8'
5497c478bd9Sstevel@tonic-gate 		 * overall and 'fr' for messages).  Weird?  Maybe.  But lang
5507c478bd9Sstevel@tonic-gate 		 * tags don't include codeset info, like locales do...
5517c478bd9Sstevel@tonic-gate 		 *
5527c478bd9Sstevel@tonic-gate 		 * So, server-side we want:
5537c478bd9Sstevel@tonic-gate 		 *  - setlocale(LC_ALL, c2s_locale);
5547c478bd9Sstevel@tonic-gate 		 *  and
5557c478bd9Sstevel@tonic-gate 		 *  - setlocale(LC_MESSAGES, s2c_locale);
5567c478bd9Sstevel@tonic-gate 		 *
5577c478bd9Sstevel@tonic-gate 		 * Client-side we don't really care.  But we could do:
5587c478bd9Sstevel@tonic-gate 		 *
5597c478bd9Sstevel@tonic-gate 		 *  - when very verbose, tell the use what lang the server's
5607c478bd9Sstevel@tonic-gate 		 *    messages are in, if left out in the protocol
5617c478bd9Sstevel@tonic-gate 		 *  - when sending messages to the server, and if applicable, we
5627c478bd9Sstevel@tonic-gate 		 *    can localize them according to the language negotiated for
5637c478bd9Sstevel@tonic-gate 		 *    that direction.
5647c478bd9Sstevel@tonic-gate 		 *
5657c478bd9Sstevel@tonic-gate 		 * But for now we do nothing on the client side.
5667c478bd9Sstevel@tonic-gate 		 */
5677c478bd9Sstevel@tonic-gate 		if ((p_langs_c2s && *p_langs_c2s) && !(p_langs_s2c && *p_langs_s2c))
5687c478bd9Sstevel@tonic-gate 			plangs = p_langs_c2s;
5697c478bd9Sstevel@tonic-gate 		else if ((p_langs_s2c && *p_langs_s2c) && !(p_langs_c2s && *p_langs_c2s))
5707c478bd9Sstevel@tonic-gate 			plangs = p_langs_s2c;
5717c478bd9Sstevel@tonic-gate 		else
5727c478bd9Sstevel@tonic-gate 			plangs = p_langs_c2s;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		if (kex->server) {
5757c478bd9Sstevel@tonic-gate 			if (plangs && mlangs && *plangs && *mlangs) {
5767c478bd9Sstevel@tonic-gate 				char *locale;
5777c478bd9Sstevel@tonic-gate 
578*6f786aceSNobutomo Nakano 				g11n_test_langtag(plangs, 1);
579*6f786aceSNobutomo Nakano 
5807c478bd9Sstevel@tonic-gate 				choose_lang(&locale, plangs, mlangs);
5817c478bd9Sstevel@tonic-gate 				if (locale) {
5827c478bd9Sstevel@tonic-gate 					g11n_setlocale(LC_ALL, locale);
5837c478bd9Sstevel@tonic-gate 					debug("Negotiated main locale: %s", locale);
5847c478bd9Sstevel@tonic-gate 					packet_send_debug("Negotiated main locale: %s", locale);
5859a8058b5Sjp161948 					xfree(locale);
5867c478bd9Sstevel@tonic-gate 				}
5877c478bd9Sstevel@tonic-gate 				if (plangs != p_langs_s2c &&
5887c478bd9Sstevel@tonic-gate 				    p_langs_s2c && *p_langs_s2c) {
5897c478bd9Sstevel@tonic-gate 					choose_lang(&locale, p_langs_s2c, mlangs);
5907c478bd9Sstevel@tonic-gate 					if (locale) {
5917c478bd9Sstevel@tonic-gate 						g11n_setlocale(LC_MESSAGES, locale);
5927c478bd9Sstevel@tonic-gate 						debug("Negotiated messages locale: %s", locale);
5939a8058b5Sjp161948 						packet_send_debug("Negotiated "
5949a8058b5Sjp161948 						    "messages locale: %s", locale);
5959a8058b5Sjp161948 						xfree(locale);
5967c478bd9Sstevel@tonic-gate 					}
5977c478bd9Sstevel@tonic-gate 				}
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		else {
6017c478bd9Sstevel@tonic-gate 			if (plangs && mlangs && *plangs && *mlangs &&
6027c478bd9Sstevel@tonic-gate 			    !(datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)) {
6037c478bd9Sstevel@tonic-gate 				char *lang;
6047c478bd9Sstevel@tonic-gate 				lang = g11n_clnt_langtag_negotiate(mlangs, plangs);
6057c478bd9Sstevel@tonic-gate 				if (lang) {
6067c478bd9Sstevel@tonic-gate 					session_lang = lang;
6077c478bd9Sstevel@tonic-gate 					debug("Negotiated lang: %s", lang);
608*6f786aceSNobutomo Nakano 					g11n_test_langtag(lang, 0);
6097c478bd9Sstevel@tonic-gate 				}
6107c478bd9Sstevel@tonic-gate 			}
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	kex_prop_free(my);
6157c478bd9Sstevel@tonic-gate 	kex_prop_free(peer);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static u_char *
derive_key(Kex * kex,int id,int need,u_char * hash,BIGNUM * shared_secret)6197c478bd9Sstevel@tonic-gate derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	Buffer b;
6227c478bd9Sstevel@tonic-gate 	const EVP_MD *evp_md = EVP_sha1();
6237c478bd9Sstevel@tonic-gate 	EVP_MD_CTX md;
6247c478bd9Sstevel@tonic-gate 	char c = id;
6257c478bd9Sstevel@tonic-gate 	int have;
6267c478bd9Sstevel@tonic-gate 	int mdsz = EVP_MD_size(evp_md);
6277c478bd9Sstevel@tonic-gate 	u_char *digest = xmalloc(roundup(need, mdsz));
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	buffer_init(&b);
6307c478bd9Sstevel@tonic-gate 	buffer_put_bignum2(&b, shared_secret);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* K1 = HASH(K || H || "A" || session_id) */
6337c478bd9Sstevel@tonic-gate 	EVP_DigestInit(&md, evp_md);
6347c478bd9Sstevel@tonic-gate 	if (!(datafellows & SSH_BUG_DERIVEKEY))
6357c478bd9Sstevel@tonic-gate 		EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
6367c478bd9Sstevel@tonic-gate 	EVP_DigestUpdate(&md, hash, mdsz);
6377c478bd9Sstevel@tonic-gate 	EVP_DigestUpdate(&md, &c, 1);
6387c478bd9Sstevel@tonic-gate 	EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
6397c478bd9Sstevel@tonic-gate 	EVP_DigestFinal(&md, digest, NULL);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * expand key:
6437c478bd9Sstevel@tonic-gate 	 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
6447c478bd9Sstevel@tonic-gate 	 * Key = K1 || K2 || ... || Kn
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	for (have = mdsz; need > have; have += mdsz) {
6477c478bd9Sstevel@tonic-gate 		EVP_DigestInit(&md, evp_md);
6487c478bd9Sstevel@tonic-gate 		if (!(datafellows & SSH_BUG_DERIVEKEY))
6497c478bd9Sstevel@tonic-gate 			EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
6507c478bd9Sstevel@tonic-gate 		EVP_DigestUpdate(&md, hash, mdsz);
6517c478bd9Sstevel@tonic-gate 		EVP_DigestUpdate(&md, digest, have);
6527c478bd9Sstevel@tonic-gate 		EVP_DigestFinal(&md, digest + have, NULL);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	buffer_free(&b);
6557c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEX
6567c478bd9Sstevel@tonic-gate 	fprintf(stderr, "key '%c'== ", c);
6577c478bd9Sstevel@tonic-gate 	dump_digest("key", digest, need);
6587c478bd9Sstevel@tonic-gate #endif
6597c478bd9Sstevel@tonic-gate 	return digest;
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate Newkeys *current_keys[MODE_MAX];
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate #define NKEYS	6
6657c478bd9Sstevel@tonic-gate void
kex_derive_keys(Kex * kex,u_char * hash,BIGNUM * shared_secret)6667c478bd9Sstevel@tonic-gate kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	u_char *keys[NKEYS];
6697c478bd9Sstevel@tonic-gate 	int i, mode, ctos;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	for (i = 0; i < NKEYS; i++)
6727c478bd9Sstevel@tonic-gate 		keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	debug2("kex_derive_keys");
6757c478bd9Sstevel@tonic-gate 	for (mode = 0; mode < MODE_MAX; mode++) {
6767c478bd9Sstevel@tonic-gate 		current_keys[mode] = kex->newkeys[mode];
6777c478bd9Sstevel@tonic-gate 		kex->newkeys[mode] = NULL;
6787c478bd9Sstevel@tonic-gate 		ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
6797c478bd9Sstevel@tonic-gate 		current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
6807c478bd9Sstevel@tonic-gate 		current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
6817c478bd9Sstevel@tonic-gate 		current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate Newkeys *
kex_get_newkeys(int mode)6867c478bd9Sstevel@tonic-gate kex_get_newkeys(int mode)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	Newkeys *ret;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	ret = current_keys[mode];
6917c478bd9Sstevel@tonic-gate 	current_keys[mode] = NULL;
6927c478bd9Sstevel@tonic-gate 	return ret;
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
6967c478bd9Sstevel@tonic-gate void
dump_digest(char * msg,u_char * digest,int len)6977c478bd9Sstevel@tonic-gate dump_digest(char *msg, u_char *digest, int len)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	int i;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s\n", msg);
7027c478bd9Sstevel@tonic-gate 	for (i = 0; i< len; i++) {
7037c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%02x", digest[i]);
7047c478bd9Sstevel@tonic-gate 		if (i%32 == 31)
7057c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\n");
7067c478bd9Sstevel@tonic-gate 		else if (i%8 == 7)
7077c478bd9Sstevel@tonic-gate 			fprintf(stderr, " ");
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate #endif
712