blob: dbc55ef7ee0878874860835874a41c51edeeb039 [file] [log] [blame]
djm@openbsd.org44a8e7c2015-04-17 13:25:52 +00001/* $OpenBSD: kex.c,v 1.106 2015/04/17 13:25:52 djm Exp $ */
Damien Millera664e872000-04-16 11:52:47 +10002/*
Ben Lindstrom44697232001-07-04 03:32:30 +00003 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millera664e872000-04-16 11:52:47 +10004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millera664e872000-04-16 11:52:47 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Millera664e872000-04-16 11:52:47 +100027
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000028#include <sys/param.h> /* MAX roundup */
Damien Miller8dbffe72006-08-05 11:02:17 +100029
Damien Millerd7834352006-08-05 12:39:39 +100030#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100031#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100033#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
35
Damien Miller1f0311c2014-05-15 14:24:09 +100036#ifdef WITH_OPENSSL
Damien Millerd7834352006-08-05 12:39:39 +100037#include <openssl/crypto.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100038#endif
Damien Millerd7834352006-08-05 12:39:39 +100039
Damien Millerd7834352006-08-05 12:39:39 +100040#include "ssh2.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "packet.h"
42#include "compat.h"
43#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000044#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "log.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000047#include "mac.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000048#include "match.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000049#include "misc.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000050#include "dispatch.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000051#include "monitor.h"
Darren Tucker36331b52010-01-08 16:50:41 +110052#include "roaming.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000053
54#include "ssherr.h"
55#include "sshbuf.h"
Damien Millerb3051d02014-01-10 10:58:53 +110056#include "digest.h"
Damien Millera664e872000-04-16 11:52:47 +100057
Damien Millerb3092032006-03-16 18:22:18 +110058#if OPENSSL_VERSION_NUMBER >= 0x00907000L
59# if defined(HAVE_EVP_SHA256)
Damien Milleraf87af12006-03-15 13:02:28 +110060# define evp_ssh_sha256 EVP_sha256
Damien Millerb3092032006-03-16 18:22:18 +110061# else
Damien Millera63128d2006-03-15 12:08:28 +110062extern const EVP_MD *evp_ssh_sha256(void);
Damien Millerb3092032006-03-16 18:22:18 +110063# endif
Tim Rice425a6882006-03-15 20:17:05 -080064#endif
Damien Millera63128d2006-03-15 12:08:28 +110065
Ben Lindstrombba81212001-06-25 05:01:22 +000066/* prototype */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000067static int kex_choose_conf(struct ssh *);
68static int kex_input_newkeys(int, u_int32_t, void *);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000069
Damien Millerea111192013-04-23 19:24:32 +100070struct kexalg {
71 char *name;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000072 u_int type;
Damien Millerea111192013-04-23 19:24:32 +100073 int ec_nid;
Damien Millerb3051d02014-01-10 10:58:53 +110074 int hash_alg;
Damien Millerea111192013-04-23 19:24:32 +100075};
76static const struct kexalg kexalgs[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100077#ifdef WITH_OPENSSL
Damien Millerb3051d02014-01-10 10:58:53 +110078 { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
79 { KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
80 { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
Darren Tuckera75d2472013-05-10 18:11:55 +100081#ifdef HAVE_EVP_SHA256
Damien Millerb3051d02014-01-10 10:58:53 +110082 { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
Damien Miller1f0311c2014-05-15 14:24:09 +100083#endif /* HAVE_EVP_SHA256 */
Darren Tuckera75d2472013-05-10 18:11:55 +100084#ifdef OPENSSL_HAS_ECC
Damien Millerb3051d02014-01-10 10:58:53 +110085 { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
86 NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
87 { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
88 SSH_DIGEST_SHA384 },
Darren Tucker37bcef52013-11-09 18:39:25 +110089# ifdef OPENSSL_HAS_NISTP521
Damien Millerb3051d02014-01-10 10:58:53 +110090 { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
91 SSH_DIGEST_SHA512 },
Damien Miller1f0311c2014-05-15 14:24:09 +100092# endif /* OPENSSL_HAS_NISTP521 */
93#endif /* OPENSSL_HAS_ECC */
Damien Miller1f0311c2014-05-15 14:24:09 +100094#endif /* WITH_OPENSSL */
Damien Miller72ef7c12015-01-15 02:21:31 +110095#if defined(HAVE_EVP_SHA256) || !defined(WITH_OPENSSL)
Damien Millerb3051d02014-01-10 10:58:53 +110096 { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
Damien Miller72ef7c12015-01-15 02:21:31 +110097#endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
Damien Millerb3051d02014-01-10 10:58:53 +110098 { NULL, -1, -1, -1},
Damien Millerea111192013-04-23 19:24:32 +100099};
100
101char *
Damien Miller690d9892013-11-08 12:16:49 +1100102kex_alg_list(char sep)
Damien Millerea111192013-04-23 19:24:32 +1000103{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000104 char *ret = NULL, *tmp;
Damien Millerea111192013-04-23 19:24:32 +1000105 size_t nlen, rlen = 0;
106 const struct kexalg *k;
107
108 for (k = kexalgs; k->name != NULL; k++) {
109 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100110 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000111 nlen = strlen(k->name);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000112 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
113 free(ret);
114 return NULL;
115 }
116 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000117 memcpy(ret + rlen, k->name, nlen + 1);
118 rlen += nlen;
119 }
120 return ret;
121}
122
123static const struct kexalg *
124kex_alg_by_name(const char *name)
125{
126 const struct kexalg *k;
127
128 for (k = kexalgs; k->name != NULL; k++) {
129 if (strcmp(k->name, name) == 0)
130 return k;
131 }
132 return NULL;
133}
134
Damien Millerd5f62bf2010-09-24 22:11:14 +1000135/* Validate KEX method name list */
136int
137kex_names_valid(const char *names)
138{
139 char *s, *cp, *p;
140
141 if (names == NULL || strcmp(names, "") == 0)
142 return 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000143 if ((s = cp = strdup(names)) == NULL)
144 return 0;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000145 for ((p = strsep(&cp, ",")); p && *p != '\0';
146 (p = strsep(&cp, ","))) {
Damien Millerea111192013-04-23 19:24:32 +1000147 if (kex_alg_by_name(p) == NULL) {
Damien Millerd5f62bf2010-09-24 22:11:14 +1000148 error("Unsupported KEX algorithm \"%.100s\"", p);
Darren Tuckera627d422013-06-02 07:31:17 +1000149 free(s);
Damien Millerd5f62bf2010-09-24 22:11:14 +1000150 return 0;
151 }
152 }
153 debug3("kex names ok: [%s]", names);
Darren Tuckera627d422013-06-02 07:31:17 +1000154 free(s);
Damien Millerd5f62bf2010-09-24 22:11:14 +1000155 return 1;
156}
157
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000158/* put algorithm proposal into buffer */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000159int
160kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
Damien Millera664e872000-04-16 11:52:47 +1000161{
Damien Millereccb9de2005-06-17 12:59:34 +1000162 u_int i;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000163 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000164
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000165 sshbuf_reset(b);
166
Ben Lindstrom59971722002-03-27 17:42:57 +0000167 /*
168 * add a dummy cookie, the cookie will be overwritten by
169 * kex_send_kexinit(), each time a kexinit is set
170 */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000171 for (i = 0; i < KEX_COOKIE_LEN; i++) {
172 if ((r = sshbuf_put_u8(b, 0)) != 0)
173 return r;
174 }
175 for (i = 0; i < PROPOSAL_MAX; i++) {
176 if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
177 return r;
178 }
179 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */
180 (r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */
181 return r;
182 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000183}
184
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000185/* parse buffer and return algorithm proposal */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000186int
187kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
Damien Millerb1715dc2000-05-30 13:44:51 +1000188{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000189 struct sshbuf *b = NULL;
190 u_char v;
Darren Tucker0d0d1952007-06-05 18:23:28 +1000191 u_int i;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000192 char **proposal = NULL;
193 int r;
Damien Millerb1715dc2000-05-30 13:44:51 +1000194
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000195 *propp = NULL;
196 if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
197 return SSH_ERR_ALLOC_FAIL;
198 if ((b = sshbuf_fromb(raw)) == NULL) {
199 r = SSH_ERR_ALLOC_FAIL;
200 goto out;
201 }
202 if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
203 goto out;
Damien Millerb1715dc2000-05-30 13:44:51 +1000204 /* extract kex init proposal strings */
205 for (i = 0; i < PROPOSAL_MAX; i++) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000206 if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
207 goto out;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000208 debug2("kex_parse_kexinit: %s", proposal[i]);
Damien Millerb1715dc2000-05-30 13:44:51 +1000209 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000210 /* first kex follows / reserved */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000211 if ((r = sshbuf_get_u8(b, &v)) != 0 ||
212 (r = sshbuf_get_u32(b, &i)) != 0)
213 goto out;
Damien Millerbabb47a2003-02-24 11:53:32 +1100214 if (first_kex_follows != NULL)
215 *first_kex_follows = i;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000216 debug2("kex_parse_kexinit: first_kex_follows %d ", v);
Darren Tucker0d0d1952007-06-05 18:23:28 +1000217 debug2("kex_parse_kexinit: reserved %u ", i);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000218 r = 0;
219 *propp = proposal;
220 out:
221 if (r != 0 && proposal != NULL)
222 kex_prop_free(proposal);
223 sshbuf_free(b);
224 return r;
Damien Millerb1715dc2000-05-30 13:44:51 +1000225}
226
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000227void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000228kex_prop_free(char **proposal)
Damien Millera664e872000-04-16 11:52:47 +1000229{
Damien Millereccb9de2005-06-17 12:59:34 +1000230 u_int i;
Damien Millera664e872000-04-16 11:52:47 +1000231
djm@openbsd.org44a8e7c2015-04-17 13:25:52 +0000232 if (proposal == NULL)
233 return;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000234 for (i = 0; i < PROPOSAL_MAX; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000235 free(proposal[i]);
236 free(proposal);
Damien Millera664e872000-04-16 11:52:47 +1000237}
238
Darren Tucker0d0d1952007-06-05 18:23:28 +1000239/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000240static int
Damien Miller630d6f42002-01-22 23:17:30 +1100241kex_protocol_error(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100242{
Damien Miller630d6f42002-01-22 23:17:30 +1100243 error("Hm, kex protocol error: type %d seq %u", type, seq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000244 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100245}
246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static void
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000248kex_reset_dispatch(struct ssh *ssh)
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000249{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000250 ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
Damien Miller7d053392002-01-22 23:24:13 +1100251 SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000252 ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000253}
254
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000255int
256kex_send_newkeys(struct ssh *ssh)
Damien Millera664e872000-04-16 11:52:47 +1000257{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000258 int r;
Ben Lindstrom238abf62001-04-04 17:52:53 +0000259
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000260 kex_reset_dispatch(ssh);
261 if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
262 (r = sshpkt_send(ssh)) != 0)
263 return r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000264 debug("SSH2_MSG_NEWKEYS sent");
Ben Lindstrom064496f2002-12-23 02:04:22 +0000265 debug("expecting SSH2_MSG_NEWKEYS");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000266 ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
267 return 0;
268}
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000269
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000270static int
271kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
272{
273 struct ssh *ssh = ctxt;
274 struct kex *kex = ssh->kex;
275 int r;
276
277 debug("SSH2_MSG_NEWKEYS received");
278 ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
279 if ((r = sshpkt_get_end(ssh)) != 0)
280 return r;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000281 kex->done = 1;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000282 sshbuf_reset(kex->peer);
283 /* sshbuf_reset(kex->my); */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000284 kex->flags &= ~KEX_INIT_SENT;
Darren Tuckera627d422013-06-02 07:31:17 +1000285 free(kex->name);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000286 kex->name = NULL;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000287 return 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000288}
Damien Millera664e872000-04-16 11:52:47 +1000289
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000290int
291kex_send_kexinit(struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000292{
Ben Lindstrom59971722002-03-27 17:42:57 +0000293 u_char *cookie;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000294 struct kex *kex = ssh->kex;
295 int r;
Ben Lindstrom59971722002-03-27 17:42:57 +0000296
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000297 if (kex == NULL)
298 return SSH_ERR_INTERNAL_ERROR;
299 if (kex->flags & KEX_INIT_SENT)
300 return 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000301 kex->done = 0;
Ben Lindstrom59971722002-03-27 17:42:57 +0000302
303 /* generate a random cookie */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000304 if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
305 return SSH_ERR_INVALID_FORMAT;
306 if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
307 return SSH_ERR_INTERNAL_ERROR;
308 arc4random_buf(cookie, KEX_COOKIE_LEN);
309
310 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
311 (r = sshpkt_putb(ssh, kex->my)) != 0 ||
312 (r = sshpkt_send(ssh)) != 0)
313 return r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000314 debug("SSH2_MSG_KEXINIT sent");
315 kex->flags |= KEX_INIT_SENT;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000316 return 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000317}
318
Darren Tucker0d0d1952007-06-05 18:23:28 +1000319/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000320int
Damien Miller630d6f42002-01-22 23:17:30 +1100321kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000322{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000323 struct ssh *ssh = ctxt;
324 struct kex *kex = ssh->kex;
325 const u_char *ptr;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000326 u_int i;
327 size_t dlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000328 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000329
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000330 debug("SSH2_MSG_KEXINIT received");
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000331 if (kex == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000332 return SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000333
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000334 ptr = sshpkt_ptr(ssh, &dlen);
335 if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
336 return r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000337
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000338 /* discard packet */
339 for (i = 0; i < KEX_COOKIE_LEN; i++)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000340 if ((r = sshpkt_get_u8(ssh, NULL)) != 0)
341 return r;
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000342 for (i = 0; i < PROPOSAL_MAX; i++)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000343 if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0)
344 return r;
Darren Tuckerae608bd2012-09-06 21:19:51 +1000345 /*
346 * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
347 * KEX method has the server move first, but a server might be using
348 * a custom method or one that we otherwise don't support. We should
349 * be prepared to remember first_kex_follows here so we can eat a
350 * packet later.
351 * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
352 * for cases where the server *doesn't* go first. I guess we should
353 * ignore it when it is set for these cases, which is what we do now.
354 */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000355 if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
356 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
357 (r = sshpkt_get_end(ssh)) != 0)
358 return r;
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000359
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000360 if (!(kex->flags & KEX_INIT_SENT))
361 if ((r = kex_send_kexinit(ssh)) != 0)
362 return r;
363 if ((r = kex_choose_conf(ssh)) != 0)
364 return r;
365
366 if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
367 return (kex->kex[kex->kex_type])(ssh);
368
369 return SSH_ERR_INTERNAL_ERROR;
370}
371
372int
373kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp)
374{
375 struct kex *kex;
376 int r;
377
378 *kexp = NULL;
379 if ((kex = calloc(1, sizeof(*kex))) == NULL)
380 return SSH_ERR_ALLOC_FAIL;
381 if ((kex->peer = sshbuf_new()) == NULL ||
382 (kex->my = sshbuf_new()) == NULL) {
383 r = SSH_ERR_ALLOC_FAIL;
384 goto out;
385 }
386 if ((r = kex_prop2buf(kex->my, proposal)) != 0)
387 goto out;
388 kex->done = 0;
389 kex_reset_dispatch(ssh);
390 r = 0;
391 *kexp = kex;
392 out:
393 if (r != 0)
394 kex_free(kex);
395 return r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000396}
397
markus@openbsd.org091c3022015-01-19 19:52:16 +0000398void
399kex_free_newkeys(struct newkeys *newkeys)
400{
401 if (newkeys == NULL)
402 return;
403 if (newkeys->enc.key) {
404 explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
405 free(newkeys->enc.key);
406 newkeys->enc.key = NULL;
407 }
408 if (newkeys->enc.iv) {
409 explicit_bzero(newkeys->enc.iv, newkeys->enc.block_size);
410 free(newkeys->enc.iv);
411 newkeys->enc.iv = NULL;
412 }
413 free(newkeys->enc.name);
414 explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
415 free(newkeys->comp.name);
416 explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
417 mac_clear(&newkeys->mac);
418 if (newkeys->mac.key) {
419 explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
420 free(newkeys->mac.key);
421 newkeys->mac.key = NULL;
422 }
423 free(newkeys->mac.name);
424 explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
425 explicit_bzero(newkeys, sizeof(*newkeys));
426 free(newkeys);
427}
428
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000429void
430kex_free(struct kex *kex)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000431{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000432 u_int mode;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000433
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000434#ifdef WITH_OPENSSL
435 if (kex->dh)
436 DH_free(kex->dh);
Damien Miller4df590c2015-03-11 10:02:39 +1100437#ifdef OPENSSL_HAS_ECC
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000438 if (kex->ec_client_key)
439 EC_KEY_free(kex->ec_client_key);
Damien Miller4df590c2015-03-11 10:02:39 +1100440#endif /* OPENSSL_HAS_ECC */
441#endif /* WITH_OPENSSL */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000442 for (mode = 0; mode < MODE_MAX; mode++) {
443 kex_free_newkeys(kex->newkeys[mode]);
444 kex->newkeys[mode] = NULL;
markus@openbsd.org091c3022015-01-19 19:52:16 +0000445 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000446 sshbuf_free(kex->peer);
447 sshbuf_free(kex->my);
448 free(kex->session_id);
449 free(kex->client_version_string);
450 free(kex->server_version_string);
451 free(kex);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000452}
453
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000454int
455kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000456{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000457 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000458
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000459 if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0)
460 return r;
461 if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
462 kex_free(ssh->kex);
463 ssh->kex = NULL;
464 return r;
Damien Millera664e872000-04-16 11:52:47 +1000465 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000466 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000467}
468
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000469static int
470choose_enc(struct sshenc *enc, char *client, char *server)
Damien Millera664e872000-04-16 11:52:47 +1000471{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000472 char *name = match_list(client, server, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000473
Damien Millera664e872000-04-16 11:52:47 +1000474 if (name == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000475 return SSH_ERR_NO_CIPHER_ALG_MATCH;
Damien Miller963f6b22002-02-19 15:21:23 +1100476 if ((enc->cipher = cipher_by_name(name)) == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000477 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000478 enc->name = name;
479 enc->enabled = 0;
480 enc->iv = NULL;
Damien Miller1d75abf2013-01-09 16:12:19 +1100481 enc->iv_len = cipher_ivlen(enc->cipher);
Damien Millera664e872000-04-16 11:52:47 +1000482 enc->key = NULL;
Damien Miller963f6b22002-02-19 15:21:23 +1100483 enc->key_len = cipher_keylen(enc->cipher);
484 enc->block_size = cipher_blocksize(enc->cipher);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000485 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000486}
Damien Miller4f7becb2006-03-26 14:10:14 +1100487
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000488static int
489choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
Damien Millera664e872000-04-16 11:52:47 +1000490{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000491 char *name = match_list(client, server, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000492
Damien Millera664e872000-04-16 11:52:47 +1000493 if (name == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000494 return SSH_ERR_NO_MAC_ALG_MATCH;
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000495 if (mac_setup(mac, name) < 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000496 return SSH_ERR_INTERNAL_ERROR;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000497 /* truncate the key */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000498 if (ssh->compat & SSH_BUG_HMAC)
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000499 mac->key_len = 16;
Damien Millera664e872000-04-16 11:52:47 +1000500 mac->name = name;
Damien Millera664e872000-04-16 11:52:47 +1000501 mac->key = NULL;
502 mac->enabled = 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000503 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000504}
Damien Miller4f7becb2006-03-26 14:10:14 +1100505
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000506static int
507choose_comp(struct sshcomp *comp, char *client, char *server)
Damien Millera664e872000-04-16 11:52:47 +1000508{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000509 char *name = match_list(client, server, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000510
Damien Millera664e872000-04-16 11:52:47 +1000511 if (name == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000512 return SSH_ERR_NO_COMPRESS_ALG_MATCH;
Damien Miller9786e6e2005-07-26 21:54:56 +1000513 if (strcmp(name, "zlib@openssh.com") == 0) {
514 comp->type = COMP_DELAYED;
515 } else if (strcmp(name, "zlib") == 0) {
516 comp->type = COMP_ZLIB;
Damien Millera664e872000-04-16 11:52:47 +1000517 } else if (strcmp(name, "none") == 0) {
Damien Miller9786e6e2005-07-26 21:54:56 +1000518 comp->type = COMP_NONE;
Damien Millera664e872000-04-16 11:52:47 +1000519 } else {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000520 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000521 }
522 comp->name = name;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000523 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000524}
Damien Miller4f7becb2006-03-26 14:10:14 +1100525
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000526static int
527choose_kex(struct kex *k, char *client, char *server)
Damien Millera664e872000-04-16 11:52:47 +1000528{
Damien Millerea111192013-04-23 19:24:32 +1000529 const struct kexalg *kexalg;
530
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000531 k->name = match_list(client, server, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000532
Damien Millera664e872000-04-16 11:52:47 +1000533 if (k->name == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000534 return SSH_ERR_NO_KEX_ALG_MATCH;
Damien Millerea111192013-04-23 19:24:32 +1000535 if ((kexalg = kex_alg_by_name(k->name)) == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000536 return SSH_ERR_INTERNAL_ERROR;
Damien Millerea111192013-04-23 19:24:32 +1000537 k->kex_type = kexalg->type;
Damien Millerb3051d02014-01-10 10:58:53 +1100538 k->hash_alg = kexalg->hash_alg;
Damien Millerea111192013-04-23 19:24:32 +1000539 k->ec_nid = kexalg->ec_nid;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000540 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000541}
Damien Miller19bb3a52005-11-05 15:19:35 +1100542
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000543static int
544choose_hostkeyalg(struct kex *k, char *client, char *server)
Damien Millera664e872000-04-16 11:52:47 +1000545{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000546 char *hostkeyalg = match_list(client, server, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000547
Damien Miller0bc1bd82000-11-13 22:57:25 +1100548 if (hostkeyalg == NULL)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000549 return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
550 k->hostkey_type = sshkey_type_from_name(hostkeyalg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100551 if (k->hostkey_type == KEY_UNSPEC)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000552 return SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org5104db72015-01-26 06:10:03 +0000553 k->hostkey_nid = sshkey_ecdsa_nid_from_name(hostkeyalg);
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(hostkeyalg);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000555 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000556}
557
Damien Millera8e06ce2003-11-21 23:48:55 +1100558static int
Damien Millerbabb47a2003-02-24 11:53:32 +1100559proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
560{
561 static int check[] = {
562 PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
563 };
564 int *idx;
565 char *p;
566
567 for (idx = &check[0]; *idx != -1; idx++) {
568 if ((p = strchr(my[*idx], ',')) != NULL)
569 *p = '\0';
570 if ((p = strchr(peer[*idx], ',')) != NULL)
571 *p = '\0';
572 if (strcmp(my[*idx], peer[*idx]) != 0) {
573 debug2("proposal mismatch: my %s peer %s",
574 my[*idx], peer[*idx]);
575 return (0);
576 }
577 }
578 debug2("proposals match");
579 return (1);
580}
581
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000582static int
583kex_choose_conf(struct ssh *ssh)
Damien Millera664e872000-04-16 11:52:47 +1000584{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000585 struct kex *kex = ssh->kex;
586 struct newkeys *newkeys;
587 char **my = NULL, **peer = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000588 char **cprop, **sprop;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000589 int nenc, nmac, ncomp;
Damien Miller76eea4a2014-01-26 09:37:25 +1100590 u_int mode, ctos, need, dh_need, authlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000591 int r, first_kex_follows;
Damien Millera664e872000-04-16 11:52:47 +1000592
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000593 if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0 ||
594 (r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
595 goto out;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000596
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000597 if (kex->server) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000598 cprop=peer;
599 sprop=my;
600 } else {
601 cprop=my;
602 sprop=peer;
603 }
Damien Millera664e872000-04-16 11:52:47 +1000604
Darren Tucker36331b52010-01-08 16:50:41 +1100605 /* Check whether server offers roaming */
606 if (!kex->server) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000607 char *roaming = match_list(KEX_RESUME,
608 peer[PROPOSAL_KEX_ALGS], NULL);
609
Darren Tucker36331b52010-01-08 16:50:41 +1100610 if (roaming) {
611 kex->roaming = 1;
Darren Tuckera627d422013-06-02 07:31:17 +1000612 free(roaming);
Darren Tucker36331b52010-01-08 16:50:41 +1100613 }
614 }
615
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000616 /* Algorithm Negotiation */
Damien Millera664e872000-04-16 11:52:47 +1000617 for (mode = 0; mode < MODE_MAX; mode++) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000618 if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
619 r = SSH_ERR_ALLOC_FAIL;
620 goto out;
621 }
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000622 kex->newkeys[mode] = newkeys;
Darren Tucker0d0d1952007-06-05 18:23:28 +1000623 ctos = (!kex->server && mode == MODE_OUT) ||
624 (kex->server && mode == MODE_IN);
Damien Millera664e872000-04-16 11:52:47 +1000625 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
626 nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
627 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000628 if ((r = choose_enc(&newkeys->enc, cprop[nenc],
629 sprop[nenc])) != 0)
630 goto out;
Damien Miller1d75abf2013-01-09 16:12:19 +1100631 authlen = cipher_authlen(newkeys->enc.cipher);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000632 /* ignore mac for authenticated encryption */
633 if (authlen == 0 &&
634 (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
635 sprop[nmac])) != 0)
636 goto out;
637 if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
638 sprop[ncomp])) != 0)
639 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000640 debug("kex: %s %s %s %s",
641 ctos ? "client->server" : "server->client",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000642 newkeys->enc.name,
Damien Miller1d75abf2013-01-09 16:12:19 +1100643 authlen == 0 ? newkeys->mac.name : "<implicit>",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000644 newkeys->comp.name);
Damien Millera664e872000-04-16 11:52:47 +1000645 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000646 if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
647 sprop[PROPOSAL_KEX_ALGS])) != 0 ||
648 (r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
649 sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0)
650 goto out;
Damien Miller76eea4a2014-01-26 09:37:25 +1100651 need = dh_need = 0;
Damien Millera664e872000-04-16 11:52:47 +1000652 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000653 newkeys = kex->newkeys[mode];
Damien Millera92ac742014-01-26 09:38:03 +1100654 need = MAX(need, newkeys->enc.key_len);
655 need = MAX(need, newkeys->enc.block_size);
656 need = MAX(need, newkeys->enc.iv_len);
657 need = MAX(need, newkeys->mac.key_len);
658 dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher));
659 dh_need = MAX(dh_need, newkeys->enc.block_size);
660 dh_need = MAX(dh_need, newkeys->enc.iv_len);
661 dh_need = MAX(dh_need, newkeys->mac.key_len);
Damien Millera664e872000-04-16 11:52:47 +1000662 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000663 /* XXX need runden? */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000664 kex->we_need = need;
Damien Miller76eea4a2014-01-26 09:37:25 +1100665 kex->dh_need = dh_need;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000666
Damien Millerbabb47a2003-02-24 11:53:32 +1100667 /* ignore the next message if the proposals do not match */
Damien Millera8e06ce2003-11-21 23:48:55 +1100668 if (first_kex_follows && !proposals_match(my, peer) &&
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000669 !(ssh->compat & SSH_BUG_FIRSTKEX))
670 ssh->dispatch_skip_packets = 1;
671 r = 0;
672 out:
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000673 kex_prop_free(my);
674 kex_prop_free(peer);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000675 return r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000676}
677
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000678static int
679derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
680 const struct sshbuf *shared_secret, u_char **keyp)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000681{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000682 struct kex *kex = ssh->kex;
683 struct ssh_digest_ctx *hashctx = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000684 char c = id;
Damien Millereccb9de2005-06-17 12:59:34 +1000685 u_int have;
Damien Millerb3051d02014-01-10 10:58:53 +1100686 size_t mdsz;
Damien Millereccb9de2005-06-17 12:59:34 +1000687 u_char *digest;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000688 int r;
Damien Miller46d38de2005-07-17 17:02:09 +1000689
Damien Millerb3051d02014-01-10 10:58:53 +1100690 if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000691 return SSH_ERR_INVALID_ARGUMENT;
692 if ((digest = calloc(1, roundup(need, mdsz))) == NULL) {
693 r = SSH_ERR_ALLOC_FAIL;
694 goto out;
695 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000696
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000697 /* K1 = HASH(K || H || "A" || session_id) */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000698 if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
699 ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
Damien Millerb3051d02014-01-10 10:58:53 +1100700 ssh_digest_update(hashctx, hash, hashlen) != 0 ||
701 ssh_digest_update(hashctx, &c, 1) != 0 ||
702 ssh_digest_update(hashctx, kex->session_id,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000703 kex->session_id_len) != 0 ||
704 ssh_digest_final(hashctx, digest, mdsz) != 0) {
705 r = SSH_ERR_LIBCRYPTO_ERROR;
706 goto out;
707 }
Damien Millerb3051d02014-01-10 10:58:53 +1100708 ssh_digest_free(hashctx);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000709 hashctx = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000710
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000711 /*
712 * expand key:
713 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
714 * Key = K1 || K2 || ... || Kn
715 */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000716 for (have = mdsz; need > have; have += mdsz) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000717 if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
718 ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
Damien Millerb3051d02014-01-10 10:58:53 +1100719 ssh_digest_update(hashctx, hash, hashlen) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000720 ssh_digest_update(hashctx, digest, have) != 0 ||
721 ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
722 r = SSH_ERR_LIBCRYPTO_ERROR;
723 goto out;
724 }
Damien Millerb3051d02014-01-10 10:58:53 +1100725 ssh_digest_free(hashctx);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000726 hashctx = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000727 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000728#ifdef DEBUG_KEX
729 fprintf(stderr, "key '%c'== ", c);
730 dump_digest("key", digest, need);
731#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000732 *keyp = digest;
733 digest = NULL;
734 r = 0;
735 out:
736 if (digest)
737 free(digest);
738 ssh_digest_free(hashctx);
739 return r;
Damien Millera664e872000-04-16 11:52:47 +1000740}
741
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000742#define NKEYS 6
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000743int
744kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
745 const struct sshbuf *shared_secret)
Damien Millera664e872000-04-16 11:52:47 +1000746{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000747 struct kex *kex = ssh->kex;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000748 u_char *keys[NKEYS];
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000749 u_int i, j, mode, ctos;
750 int r;
Damien Millera664e872000-04-16 11:52:47 +1000751
Damien Miller19bb3a52005-11-05 15:19:35 +1100752 for (i = 0; i < NKEYS; i++) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000753 if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
754 shared_secret, &keys[i])) != 0) {
755 for (j = 0; j < i; j++)
756 free(keys[j]);
757 return r;
758 }
Damien Miller19bb3a52005-11-05 15:19:35 +1100759 }
Damien Millera664e872000-04-16 11:52:47 +1000760 for (mode = 0; mode < MODE_MAX; mode++) {
Damien Miller4f7becb2006-03-26 14:10:14 +1100761 ctos = (!kex->server && mode == MODE_OUT) ||
762 (kex->server && mode == MODE_IN);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000763 kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1];
764 kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
765 kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
Damien Millera664e872000-04-16 11:52:47 +1000766 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000767 return 0;
Damien Millera664e872000-04-16 11:52:47 +1000768}
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000769
Damien Miller1f0311c2014-05-15 14:24:09 +1000770#ifdef WITH_OPENSSL
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000771int
772kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
773 const BIGNUM *secret)
Damien Miller91b580e2014-01-12 19:21:22 +1100774{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000775 struct sshbuf *shared_secret;
776 int r;
Damien Miller91b580e2014-01-12 19:21:22 +1100777
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000778 if ((shared_secret = sshbuf_new()) == NULL)
779 return SSH_ERR_ALLOC_FAIL;
780 if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
781 r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
782 sshbuf_free(shared_secret);
783 return r;
Damien Miller91b580e2014-01-12 19:21:22 +1100784}
Damien Miller1f0311c2014-05-15 14:24:09 +1000785#endif
Damien Miller91b580e2014-01-12 19:21:22 +1100786
Damien Miller1f0311c2014-05-15 14:24:09 +1000787#ifdef WITH_SSH1
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000788int
Darren Tuckere14e0052004-05-13 16:30:44 +1000789derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
790 u_int8_t cookie[8], u_int8_t id[16])
791{
djm@openbsd.org25f5f782015-01-30 00:22:25 +0000792 u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000793 struct ssh_digest_ctx *hashctx = NULL;
djm@openbsd.org25f5f782015-01-30 00:22:25 +0000794 size_t hlen, slen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000795 int r;
Darren Tuckere14e0052004-05-13 16:30:44 +1000796
djm@openbsd.org25f5f782015-01-30 00:22:25 +0000797 hlen = BN_num_bytes(host_modulus);
798 slen = BN_num_bytes(server_modulus);
799 if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) ||
800 slen < (512 / 8) || (u_int)slen > sizeof(sbuf))
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000801 return SSH_ERR_KEY_BITS_MISMATCH;
djm@openbsd.org25f5f782015-01-30 00:22:25 +0000802 if (BN_bn2bin(host_modulus, hbuf) <= 0 ||
803 BN_bn2bin(server_modulus, sbuf) <= 0) {
804 r = SSH_ERR_LIBCRYPTO_ERROR;
805 goto out;
806 }
807 if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) {
808 r = SSH_ERR_ALLOC_FAIL;
809 goto out;
810 }
811 if (ssh_digest_update(hashctx, hbuf, hlen) != 0 ||
812 ssh_digest_update(hashctx, sbuf, slen) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000813 ssh_digest_update(hashctx, cookie, 8) != 0 ||
814 ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
815 r = SSH_ERR_LIBCRYPTO_ERROR;
816 goto out;
817 }
Damien Millerb3051d02014-01-10 10:58:53 +1100818 memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000819 r = 0;
820 out:
821 ssh_digest_free(hashctx);
djm@openbsd.org25f5f782015-01-30 00:22:25 +0000822 explicit_bzero(hbuf, sizeof(hbuf));
823 explicit_bzero(sbuf, sizeof(sbuf));
Damien Millera5103f42014-02-04 11:20:14 +1100824 explicit_bzero(obuf, sizeof(obuf));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000825 return r;
Darren Tuckere14e0052004-05-13 16:30:44 +1000826}
Damien Miller1f0311c2014-05-15 14:24:09 +1000827#endif
Darren Tuckere14e0052004-05-13 16:30:44 +1000828
Damien Millereb8b60e2010-08-31 22:41:14 +1000829#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000830void
831dump_digest(char *msg, u_char *digest, int len)
832{
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000833 fprintf(stderr, "%s\n", msg);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000834 sshbuf_dump_data(digest, len, stderr);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000835}
836#endif