blob: b38bae0f03338647a08d8419f82f14c3e2468922 [file] [log] [blame]
Damien Miller690d9892013-11-08 12:16:49 +11001/* $OpenBSD: kex.c,v 1.93 2013/11/07 11:58:27 dtucker 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
Damien Miller8dbffe72006-08-05 11:02:17 +100028#include <sys/param.h>
29
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 Millerd7834352006-08-05 12:39:39 +100036#include <openssl/crypto.h>
37
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "ssh2.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "packet.h"
42#include "compat.h"
43#include "cipher.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110044#include "key.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"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000049#include "dispatch.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include "monitor.h"
Darren Tucker36331b52010-01-08 16:50:41 +110051#include "roaming.h"
Damien Millera664e872000-04-16 11:52:47 +100052
Damien Millerb3092032006-03-16 18:22:18 +110053#if OPENSSL_VERSION_NUMBER >= 0x00907000L
54# if defined(HAVE_EVP_SHA256)
Damien Milleraf87af12006-03-15 13:02:28 +110055# define evp_ssh_sha256 EVP_sha256
Damien Millerb3092032006-03-16 18:22:18 +110056# else
Damien Millera63128d2006-03-15 12:08:28 +110057extern const EVP_MD *evp_ssh_sha256(void);
Damien Millerb3092032006-03-16 18:22:18 +110058# endif
Tim Rice425a6882006-03-15 20:17:05 -080059#endif
Damien Millera63128d2006-03-15 12:08:28 +110060
Ben Lindstrombba81212001-06-25 05:01:22 +000061/* prototype */
62static void kex_kexinit_finish(Kex *);
63static void kex_choose_conf(Kex *);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000064
Damien Millerea111192013-04-23 19:24:32 +100065struct kexalg {
66 char *name;
67 int type;
68 int ec_nid;
69 const EVP_MD *(*mdfunc)(void);
70};
71static const struct kexalg kexalgs[] = {
72 { KEX_DH1, KEX_DH_GRP1_SHA1, 0, EVP_sha1 },
73 { KEX_DH14, KEX_DH_GRP14_SHA1, 0, EVP_sha1 },
74 { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, EVP_sha1 },
Darren Tuckera75d2472013-05-10 18:11:55 +100075#ifdef HAVE_EVP_SHA256
Damien Millerea111192013-04-23 19:24:32 +100076 { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, EVP_sha256 },
Darren Tuckera75d2472013-05-10 18:11:55 +100077#endif
78#ifdef OPENSSL_HAS_ECC
Damien Millerea111192013-04-23 19:24:32 +100079 { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, NID_X9_62_prime256v1, EVP_sha256 },
80 { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, EVP_sha384 },
Darren Tucker37bcef52013-11-09 18:39:25 +110081# ifdef OPENSSL_HAS_NISTP521
Damien Millerea111192013-04-23 19:24:32 +100082 { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, EVP_sha512 },
Darren Tucker37bcef52013-11-09 18:39:25 +110083# endif
Damien Millerea111192013-04-23 19:24:32 +100084#endif
Darren Tucker1c8ce342013-11-08 19:50:32 +110085#ifdef HAVE_EVP_SHA256
Damien Miller1e124262013-11-04 08:26:52 +110086 { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, EVP_sha256 },
Darren Tucker1c8ce342013-11-08 19:50:32 +110087#endif
Damien Millerea111192013-04-23 19:24:32 +100088 { NULL, -1, -1, NULL},
89};
90
91char *
Damien Miller690d9892013-11-08 12:16:49 +110092kex_alg_list(char sep)
Damien Millerea111192013-04-23 19:24:32 +100093{
94 char *ret = NULL;
95 size_t nlen, rlen = 0;
96 const struct kexalg *k;
97
98 for (k = kexalgs; k->name != NULL; k++) {
99 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100100 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000101 nlen = strlen(k->name);
102 ret = xrealloc(ret, 1, rlen + nlen + 2);
103 memcpy(ret + rlen, k->name, nlen + 1);
104 rlen += nlen;
105 }
106 return ret;
107}
108
109static const struct kexalg *
110kex_alg_by_name(const char *name)
111{
112 const struct kexalg *k;
113
114 for (k = kexalgs; k->name != NULL; k++) {
115 if (strcmp(k->name, name) == 0)
116 return k;
117 }
118 return NULL;
119}
120
Damien Millerd5f62bf2010-09-24 22:11:14 +1000121/* Validate KEX method name list */
122int
123kex_names_valid(const char *names)
124{
125 char *s, *cp, *p;
126
127 if (names == NULL || strcmp(names, "") == 0)
128 return 0;
129 s = cp = xstrdup(names);
130 for ((p = strsep(&cp, ",")); p && *p != '\0';
131 (p = strsep(&cp, ","))) {
Damien Millerea111192013-04-23 19:24:32 +1000132 if (kex_alg_by_name(p) == NULL) {
Damien Millerd5f62bf2010-09-24 22:11:14 +1000133 error("Unsupported KEX algorithm \"%.100s\"", p);
Darren Tuckera627d422013-06-02 07:31:17 +1000134 free(s);
Damien Millerd5f62bf2010-09-24 22:11:14 +1000135 return 0;
136 }
137 }
138 debug3("kex names ok: [%s]", names);
Darren Tuckera627d422013-06-02 07:31:17 +1000139 free(s);
Damien Millerd5f62bf2010-09-24 22:11:14 +1000140 return 1;
141}
142
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000143/* put algorithm proposal into buffer */
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000145kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
Damien Millera664e872000-04-16 11:52:47 +1000146{
Damien Millereccb9de2005-06-17 12:59:34 +1000147 u_int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000148
149 buffer_clear(b);
Ben Lindstrom59971722002-03-27 17:42:57 +0000150 /*
151 * add a dummy cookie, the cookie will be overwritten by
152 * kex_send_kexinit(), each time a kexinit is set
153 */
154 for (i = 0; i < KEX_COOKIE_LEN; i++)
155 buffer_put_char(b, 0);
Damien Millera664e872000-04-16 11:52:47 +1000156 for (i = 0; i < PROPOSAL_MAX; i++)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000157 buffer_put_cstring(b, proposal[i]);
158 buffer_put_char(b, 0); /* first_kex_packet_follows */
159 buffer_put_int(b, 0); /* uint32 reserved */
Damien Millera664e872000-04-16 11:52:47 +1000160}
161
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000162/* parse buffer and return algorithm proposal */
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static char **
Damien Millerbabb47a2003-02-24 11:53:32 +1100164kex_buf2prop(Buffer *raw, int *first_kex_follows)
Damien Millerb1715dc2000-05-30 13:44:51 +1000165{
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000166 Buffer b;
Darren Tucker0d0d1952007-06-05 18:23:28 +1000167 u_int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000168 char **proposal;
Damien Millerb1715dc2000-05-30 13:44:51 +1000169
Damien Miller07d86be2006-03-26 14:19:21 +1100170 proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
Damien Millerb1715dc2000-05-30 13:44:51 +1000171
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000172 buffer_init(&b);
173 buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
Damien Millerb1715dc2000-05-30 13:44:51 +1000174 /* skip cookie */
175 for (i = 0; i < KEX_COOKIE_LEN; i++)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000176 buffer_get_char(&b);
Damien Millerb1715dc2000-05-30 13:44:51 +1000177 /* extract kex init proposal strings */
178 for (i = 0; i < PROPOSAL_MAX; i++) {
Damien Millerda108ec2010-08-31 22:36:39 +1000179 proposal[i] = buffer_get_cstring(&b,NULL);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000180 debug2("kex_parse_kexinit: %s", proposal[i]);
Damien Millerb1715dc2000-05-30 13:44:51 +1000181 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000182 /* first kex follows / reserved */
183 i = buffer_get_char(&b);
Damien Millerbabb47a2003-02-24 11:53:32 +1100184 if (first_kex_follows != NULL)
185 *first_kex_follows = i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000186 debug2("kex_parse_kexinit: first_kex_follows %d ", i);
187 i = buffer_get_int(&b);
Darren Tucker0d0d1952007-06-05 18:23:28 +1000188 debug2("kex_parse_kexinit: reserved %u ", i);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000189 buffer_free(&b);
190 return proposal;
Damien Millerb1715dc2000-05-30 13:44:51 +1000191}
192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000194kex_prop_free(char **proposal)
Damien Millera664e872000-04-16 11:52:47 +1000195{
Damien Millereccb9de2005-06-17 12:59:34 +1000196 u_int i;
Damien Millera664e872000-04-16 11:52:47 +1000197
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000198 for (i = 0; i < PROPOSAL_MAX; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000199 free(proposal[i]);
200 free(proposal);
Damien Millera664e872000-04-16 11:52:47 +1000201}
202
Darren Tucker0d0d1952007-06-05 18:23:28 +1000203/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static void
Damien Miller630d6f42002-01-22 23:17:30 +1100205kex_protocol_error(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100206{
Damien Miller630d6f42002-01-22 23:17:30 +1100207 error("Hm, kex protocol error: type %d seq %u", type, seq);
Damien Miller874d77b2000-10-14 16:23:11 +1100208}
209
Ben Lindstrombba81212001-06-25 05:01:22 +0000210static void
Damien Millerbc27d4e2002-02-13 13:54:06 +1100211kex_reset_dispatch(void)
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000212{
Damien Miller7d053392002-01-22 23:24:13 +1100213 dispatch_range(SSH2_MSG_TRANSPORT_MIN,
214 SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
Damien Millerbc27d4e2002-02-13 13:54:06 +1100215 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000216}
217
218void
Ben Lindstrom238abf62001-04-04 17:52:53 +0000219kex_finish(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000220{
Damien Millerbc27d4e2002-02-13 13:54:06 +1100221 kex_reset_dispatch();
Ben Lindstrom238abf62001-04-04 17:52:53 +0000222
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000223 packet_start(SSH2_MSG_NEWKEYS);
224 packet_send();
225 /* packet_write_wait(); */
226 debug("SSH2_MSG_NEWKEYS sent");
Damien Millera664e872000-04-16 11:52:47 +1000227
Ben Lindstrom064496f2002-12-23 02:04:22 +0000228 debug("expecting SSH2_MSG_NEWKEYS");
Damien Millerdff50992002-01-22 23:16:32 +1100229 packet_read_expect(SSH2_MSG_NEWKEYS);
Ben Lindstrom14519082002-02-26 17:58:29 +0000230 packet_check_eom();
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000231 debug("SSH2_MSG_NEWKEYS received");
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000232
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000233 kex->done = 1;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000234 buffer_clear(&kex->peer);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000235 /* buffer_clear(&kex->my); */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000236 kex->flags &= ~KEX_INIT_SENT;
Darren Tuckera627d422013-06-02 07:31:17 +1000237 free(kex->name);
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000238 kex->name = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000239}
Damien Millera664e872000-04-16 11:52:47 +1000240
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000241void
242kex_send_kexinit(Kex *kex)
243{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000244 u_int32_t rnd = 0;
Ben Lindstrom59971722002-03-27 17:42:57 +0000245 u_char *cookie;
Damien Millereccb9de2005-06-17 12:59:34 +1000246 u_int i;
Ben Lindstrom59971722002-03-27 17:42:57 +0000247
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000248 if (kex == NULL) {
249 error("kex_send_kexinit: no kex, cannot rekey");
250 return;
251 }
Ben Lindstrom238abf62001-04-04 17:52:53 +0000252 if (kex->flags & KEX_INIT_SENT) {
253 debug("KEX_INIT_SENT");
254 return;
255 }
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000256 kex->done = 0;
Ben Lindstrom59971722002-03-27 17:42:57 +0000257
258 /* generate a random cookie */
259 if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
260 fatal("kex_send_kexinit: kex proposal too short");
261 cookie = buffer_ptr(&kex->my);
262 for (i = 0; i < KEX_COOKIE_LEN; i++) {
263 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000264 rnd = arc4random();
265 cookie[i] = rnd;
266 rnd >>= 8;
Ben Lindstrom59971722002-03-27 17:42:57 +0000267 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000268 packet_start(SSH2_MSG_KEXINIT);
269 packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
270 packet_send();
271 debug("SSH2_MSG_KEXINIT sent");
272 kex->flags |= KEX_INIT_SENT;
273}
274
Darren Tucker0d0d1952007-06-05 18:23:28 +1000275/* ARGSUSED */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000276void
Damien Miller630d6f42002-01-22 23:17:30 +1100277kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000278{
279 char *ptr;
Damien Millereccb9de2005-06-17 12:59:34 +1000280 u_int i, dlen;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000281 Kex *kex = (Kex *)ctxt;
282
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000283 debug("SSH2_MSG_KEXINIT received");
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000284 if (kex == NULL)
285 fatal("kex_input_kexinit: no kex, cannot rekey");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000286
287 ptr = packet_get_raw(&dlen);
288 buffer_append(&kex->peer, ptr, dlen);
289
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000290 /* discard packet */
291 for (i = 0; i < KEX_COOKIE_LEN; i++)
292 packet_get_char();
293 for (i = 0; i < PROPOSAL_MAX; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000294 free(packet_get_string(NULL));
Darren Tuckerae608bd2012-09-06 21:19:51 +1000295 /*
296 * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
297 * KEX method has the server move first, but a server might be using
298 * a custom method or one that we otherwise don't support. We should
299 * be prepared to remember first_kex_follows here so we can eat a
300 * packet later.
301 * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
302 * for cases where the server *doesn't* go first. I guess we should
303 * ignore it when it is set for these cases, which is what we do now.
304 */
305 (void) packet_get_char(); /* first_kex_follows */
306 (void) packet_get_int(); /* reserved */
Damien Miller48b03fc2002-01-22 23:11:40 +1100307 packet_check_eom();
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000308
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000309 kex_kexinit_finish(kex);
310}
311
312Kex *
Ben Lindstrom238abf62001-04-04 17:52:53 +0000313kex_setup(char *proposal[PROPOSAL_MAX])
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000314{
315 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000316
Damien Miller07d86be2006-03-26 14:19:21 +1100317 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000318 buffer_init(&kex->peer);
319 buffer_init(&kex->my);
320 kex_prop2buf(&kex->my, proposal);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000321 kex->done = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000322
323 kex_send_kexinit(kex); /* we start */
Damien Millerbc27d4e2002-02-13 13:54:06 +1100324 kex_reset_dispatch();
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000325
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000326 return kex;
327}
328
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000330kex_kexinit_finish(Kex *kex)
331{
332 if (!(kex->flags & KEX_INIT_SENT))
333 kex_send_kexinit(kex);
334
335 kex_choose_conf(kex);
336
Damien Miller8e7fb332003-02-24 12:03:03 +1100337 if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
338 kex->kex[kex->kex_type] != NULL) {
339 (kex->kex[kex->kex_type])(kex);
340 } else {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000341 fatal("Unsupported key exchange %d", kex->kex_type);
Damien Millera664e872000-04-16 11:52:47 +1000342 }
Damien Millera664e872000-04-16 11:52:47 +1000343}
344
Ben Lindstrombba81212001-06-25 05:01:22 +0000345static void
Damien Millera664e872000-04-16 11:52:47 +1000346choose_enc(Enc *enc, char *client, char *server)
347{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000348 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000349 if (name == NULL)
Darren Tucker0d0d1952007-06-05 18:23:28 +1000350 fatal("no matching cipher found: client %s server %s",
351 client, server);
Damien Miller963f6b22002-02-19 15:21:23 +1100352 if ((enc->cipher = cipher_by_name(name)) == NULL)
Damien Miller874d77b2000-10-14 16:23:11 +1100353 fatal("matching cipher is not supported: %s", name);
Damien Millera664e872000-04-16 11:52:47 +1000354 enc->name = name;
355 enc->enabled = 0;
356 enc->iv = NULL;
Damien Miller1d75abf2013-01-09 16:12:19 +1100357 enc->iv_len = cipher_ivlen(enc->cipher);
Damien Millera664e872000-04-16 11:52:47 +1000358 enc->key = NULL;
Damien Miller963f6b22002-02-19 15:21:23 +1100359 enc->key_len = cipher_keylen(enc->cipher);
360 enc->block_size = cipher_blocksize(enc->cipher);
Damien Millera664e872000-04-16 11:52:47 +1000361}
Damien Miller4f7becb2006-03-26 14:10:14 +1100362
Ben Lindstrombba81212001-06-25 05:01:22 +0000363static void
Damien Millera664e872000-04-16 11:52:47 +1000364choose_mac(Mac *mac, char *client, char *server)
365{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000366 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000367 if (name == NULL)
Darren Tucker0d0d1952007-06-05 18:23:28 +1000368 fatal("no matching mac found: client %s server %s",
369 client, server);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000370 if (mac_setup(mac, name) < 0)
Damien Millera664e872000-04-16 11:52:47 +1000371 fatal("unsupported mac %s", name);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000372 /* truncate the key */
373 if (datafellows & SSH_BUG_HMAC)
374 mac->key_len = 16;
Damien Millera664e872000-04-16 11:52:47 +1000375 mac->name = name;
Damien Millera664e872000-04-16 11:52:47 +1000376 mac->key = NULL;
377 mac->enabled = 0;
378}
Damien Miller4f7becb2006-03-26 14:10:14 +1100379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static void
Damien Millera664e872000-04-16 11:52:47 +1000381choose_comp(Comp *comp, char *client, char *server)
382{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000383 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000384 if (name == NULL)
385 fatal("no matching comp found: client %s server %s", client, server);
Damien Miller9786e6e2005-07-26 21:54:56 +1000386 if (strcmp(name, "zlib@openssh.com") == 0) {
387 comp->type = COMP_DELAYED;
388 } else if (strcmp(name, "zlib") == 0) {
389 comp->type = COMP_ZLIB;
Damien Millera664e872000-04-16 11:52:47 +1000390 } else if (strcmp(name, "none") == 0) {
Damien Miller9786e6e2005-07-26 21:54:56 +1000391 comp->type = COMP_NONE;
Damien Millera664e872000-04-16 11:52:47 +1000392 } else {
393 fatal("unsupported comp %s", name);
394 }
395 comp->name = name;
396}
Damien Miller4f7becb2006-03-26 14:10:14 +1100397
Ben Lindstrombba81212001-06-25 05:01:22 +0000398static void
Damien Millera664e872000-04-16 11:52:47 +1000399choose_kex(Kex *k, char *client, char *server)
400{
Damien Millerea111192013-04-23 19:24:32 +1000401 const struct kexalg *kexalg;
402
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000403 k->name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000404 if (k->name == NULL)
Darren Tucker0d0d1952007-06-05 18:23:28 +1000405 fatal("Unable to negotiate a key exchange method");
Damien Millerea111192013-04-23 19:24:32 +1000406 if ((kexalg = kex_alg_by_name(k->name)) == NULL)
407 fatal("unsupported kex alg %s", k->name);
408 k->kex_type = kexalg->type;
409 k->evp_md = kexalg->mdfunc();
410 k->ec_nid = kexalg->ec_nid;
Damien Millera664e872000-04-16 11:52:47 +1000411}
Damien Miller19bb3a52005-11-05 15:19:35 +1100412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Damien Millera664e872000-04-16 11:52:47 +1000414choose_hostkeyalg(Kex *k, char *client, char *server)
415{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000416 char *hostkeyalg = match_list(client, server, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100417 if (hostkeyalg == NULL)
Damien Millera664e872000-04-16 11:52:47 +1000418 fatal("no hostkey alg");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100419 k->hostkey_type = key_type_from_name(hostkeyalg);
420 if (k->hostkey_type == KEY_UNSPEC)
421 fatal("bad hostkey alg '%s'", hostkeyalg);
Darren Tuckera627d422013-06-02 07:31:17 +1000422 free(hostkeyalg);
Damien Millera664e872000-04-16 11:52:47 +1000423}
424
Damien Millera8e06ce2003-11-21 23:48:55 +1100425static int
Damien Millerbabb47a2003-02-24 11:53:32 +1100426proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
427{
428 static int check[] = {
429 PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
430 };
431 int *idx;
432 char *p;
433
434 for (idx = &check[0]; *idx != -1; idx++) {
435 if ((p = strchr(my[*idx], ',')) != NULL)
436 *p = '\0';
437 if ((p = strchr(peer[*idx], ',')) != NULL)
438 *p = '\0';
439 if (strcmp(my[*idx], peer[*idx]) != 0) {
440 debug2("proposal mismatch: my %s peer %s",
441 my[*idx], peer[*idx]);
442 return (0);
443 }
444 }
445 debug2("proposals match");
446 return (1);
447}
448
Ben Lindstrombba81212001-06-25 05:01:22 +0000449static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000450kex_choose_conf(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000451{
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000452 Newkeys *newkeys;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000453 char **my, **peer;
454 char **cprop, **sprop;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000455 int nenc, nmac, ncomp;
Damien Miller1d75abf2013-01-09 16:12:19 +1100456 u_int mode, ctos, need, authlen;
Damien Millerbabb47a2003-02-24 11:53:32 +1100457 int first_kex_follows, type;
Damien Millera664e872000-04-16 11:52:47 +1000458
Damien Millerbabb47a2003-02-24 11:53:32 +1100459 my = kex_buf2prop(&kex->my, NULL);
460 peer = kex_buf2prop(&kex->peer, &first_kex_follows);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000461
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000462 if (kex->server) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000463 cprop=peer;
464 sprop=my;
465 } else {
466 cprop=my;
467 sprop=peer;
468 }
Damien Millera664e872000-04-16 11:52:47 +1000469
Darren Tucker36331b52010-01-08 16:50:41 +1100470 /* Check whether server offers roaming */
471 if (!kex->server) {
472 char *roaming;
473 roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
474 if (roaming) {
475 kex->roaming = 1;
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(roaming);
Darren Tucker36331b52010-01-08 16:50:41 +1100477 }
478 }
479
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000480 /* Algorithm Negotiation */
Damien Millera664e872000-04-16 11:52:47 +1000481 for (mode = 0; mode < MODE_MAX; mode++) {
Damien Miller07d86be2006-03-26 14:19:21 +1100482 newkeys = xcalloc(1, sizeof(*newkeys));
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000483 kex->newkeys[mode] = newkeys;
Darren Tucker0d0d1952007-06-05 18:23:28 +1000484 ctos = (!kex->server && mode == MODE_OUT) ||
485 (kex->server && mode == MODE_IN);
Damien Millera664e872000-04-16 11:52:47 +1000486 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
487 nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
488 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
Damien Miller1d75abf2013-01-09 16:12:19 +1100489 choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
490 /* ignore mac for authenticated encryption */
491 authlen = cipher_authlen(newkeys->enc.cipher);
492 if (authlen == 0)
493 choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000494 choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
Damien Millera664e872000-04-16 11:52:47 +1000495 debug("kex: %s %s %s %s",
496 ctos ? "client->server" : "server->client",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000497 newkeys->enc.name,
Damien Miller1d75abf2013-01-09 16:12:19 +1100498 authlen == 0 ? newkeys->mac.name : "<implicit>",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000499 newkeys->comp.name);
Damien Millera664e872000-04-16 11:52:47 +1000500 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000501 choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
502 choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
Damien Millera664e872000-04-16 11:52:47 +1000503 sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
Damien Millera664e872000-04-16 11:52:47 +1000504 need = 0;
505 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000506 newkeys = kex->newkeys[mode];
Damien Miller963f6b22002-02-19 15:21:23 +1100507 if (need < newkeys->enc.key_len)
508 need = newkeys->enc.key_len;
509 if (need < newkeys->enc.block_size)
510 need = newkeys->enc.block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100511 if (need < newkeys->enc.iv_len)
512 need = newkeys->enc.iv_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000513 if (need < newkeys->mac.key_len)
514 need = newkeys->mac.key_len;
Damien Millera664e872000-04-16 11:52:47 +1000515 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000516 /* XXX need runden? */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000517 kex->we_need = need;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000518
Damien Millerbabb47a2003-02-24 11:53:32 +1100519 /* ignore the next message if the proposals do not match */
Damien Millera8e06ce2003-11-21 23:48:55 +1100520 if (first_kex_follows && !proposals_match(my, peer) &&
Damien Miller0dc1bef2005-07-17 17:22:45 +1000521 !(datafellows & SSH_BUG_FIRSTKEX)) {
Damien Millerbabb47a2003-02-24 11:53:32 +1100522 type = packet_read();
523 debug2("skipping next packet (type %u)", type);
524 }
525
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000526 kex_prop_free(my);
527 kex_prop_free(peer);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000528}
529
Ben Lindstrombba81212001-06-25 05:01:22 +0000530static u_char *
Damien Miller19bb3a52005-11-05 15:19:35 +1100531derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
532 BIGNUM *shared_secret)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000533{
534 Buffer b;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000535 EVP_MD_CTX md;
536 char c = id;
Damien Millereccb9de2005-06-17 12:59:34 +1000537 u_int have;
Damien Miller19bb3a52005-11-05 15:19:35 +1100538 int mdsz;
Damien Millereccb9de2005-06-17 12:59:34 +1000539 u_char *digest;
Damien Miller46d38de2005-07-17 17:02:09 +1000540
Damien Miller19bb3a52005-11-05 15:19:35 +1100541 if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0)
542 fatal("bad kex md size %d", mdsz);
Damien Millerc91e5562006-03-26 13:58:55 +1100543 digest = xmalloc(roundup(need, mdsz));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000544
545 buffer_init(&b);
546 buffer_put_bignum2(&b, shared_secret);
547
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000548 /* K1 = HASH(K || H || "A" || session_id) */
Damien Miller19bb3a52005-11-05 15:19:35 +1100549 EVP_DigestInit(&md, kex->evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000550 if (!(datafellows & SSH_BUG_DERIVEKEY))
551 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Damien Miller19bb3a52005-11-05 15:19:35 +1100552 EVP_DigestUpdate(&md, hash, hashlen);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000553 EVP_DigestUpdate(&md, &c, 1);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000554 EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000555 EVP_DigestFinal(&md, digest, NULL);
556
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000557 /*
558 * expand key:
559 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
560 * Key = K1 || K2 || ... || Kn
561 */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000562 for (have = mdsz; need > have; have += mdsz) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100563 EVP_DigestInit(&md, kex->evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000564 if (!(datafellows & SSH_BUG_DERIVEKEY))
565 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Damien Miller19bb3a52005-11-05 15:19:35 +1100566 EVP_DigestUpdate(&md, hash, hashlen);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000567 EVP_DigestUpdate(&md, digest, have);
568 EVP_DigestFinal(&md, digest + have, NULL);
569 }
570 buffer_free(&b);
571#ifdef DEBUG_KEX
572 fprintf(stderr, "key '%c'== ", c);
573 dump_digest("key", digest, need);
574#endif
575 return digest;
Damien Millera664e872000-04-16 11:52:47 +1000576}
577
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000578Newkeys *current_keys[MODE_MAX];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000579
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000580#define NKEYS 6
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000581void
Damien Miller19bb3a52005-11-05 15:19:35 +1100582kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret)
Damien Millera664e872000-04-16 11:52:47 +1000583{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000584 u_char *keys[NKEYS];
Damien Millereccb9de2005-06-17 12:59:34 +1000585 u_int i, mode, ctos;
Damien Millera664e872000-04-16 11:52:47 +1000586
Damien Miller19bb3a52005-11-05 15:19:35 +1100587 for (i = 0; i < NKEYS; i++) {
588 keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
589 shared_secret);
590 }
Damien Millera664e872000-04-16 11:52:47 +1000591
Ben Lindstrom064496f2002-12-23 02:04:22 +0000592 debug2("kex_derive_keys");
Damien Millera664e872000-04-16 11:52:47 +1000593 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000594 current_keys[mode] = kex->newkeys[mode];
595 kex->newkeys[mode] = NULL;
Damien Miller4f7becb2006-03-26 14:10:14 +1100596 ctos = (!kex->server && mode == MODE_OUT) ||
597 (kex->server && mode == MODE_IN);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000598 current_keys[mode]->enc.iv = keys[ctos ? 0 : 1];
599 current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
600 current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
Damien Millera664e872000-04-16 11:52:47 +1000601 }
Damien Millera664e872000-04-16 11:52:47 +1000602}
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000603
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000604Newkeys *
605kex_get_newkeys(int mode)
606{
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000607 Newkeys *ret;
608
609 ret = current_keys[mode];
610 current_keys[mode] = NULL;
611 return ret;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000612}
613
Darren Tuckere14e0052004-05-13 16:30:44 +1000614void
615derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
616 u_int8_t cookie[8], u_int8_t id[16])
617{
618 const EVP_MD *evp_md = EVP_md5();
619 EVP_MD_CTX md;
620 u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE];
621 int len;
622
623 EVP_DigestInit(&md, evp_md);
624
625 len = BN_num_bytes(host_modulus);
Damien Millereccb9de2005-06-17 12:59:34 +1000626 if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
Darren Tuckere14e0052004-05-13 16:30:44 +1000627 fatal("%s: bad host modulus (len %d)", __func__, len);
628 BN_bn2bin(host_modulus, nbuf);
629 EVP_DigestUpdate(&md, nbuf, len);
630
631 len = BN_num_bytes(server_modulus);
Damien Millereccb9de2005-06-17 12:59:34 +1000632 if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
Darren Tuckere14e0052004-05-13 16:30:44 +1000633 fatal("%s: bad server modulus (len %d)", __func__, len);
634 BN_bn2bin(server_modulus, nbuf);
635 EVP_DigestUpdate(&md, nbuf, len);
636
637 EVP_DigestUpdate(&md, cookie, 8);
638
Darren Tuckerb42714e2004-05-13 16:31:48 +1000639 EVP_DigestFinal(&md, obuf, NULL);
Darren Tuckere14e0052004-05-13 16:30:44 +1000640 memcpy(id, obuf, 16);
641
642 memset(nbuf, 0, sizeof(nbuf));
643 memset(obuf, 0, sizeof(obuf));
644 memset(&md, 0, sizeof(md));
645}
646
Damien Millereb8b60e2010-08-31 22:41:14 +1000647#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000648void
649dump_digest(char *msg, u_char *digest, int len)
650{
Damien Millereb8b60e2010-08-31 22:41:14 +1000651 int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000652
653 fprintf(stderr, "%s\n", msg);
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100654 for (i = 0; i < len; i++) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000655 fprintf(stderr, "%02x", digest[i]);
656 if (i%32 == 31)
657 fprintf(stderr, "\n");
658 else if (i%8 == 7)
659 fprintf(stderr, " ");
660 }
661 fprintf(stderr, "\n");
662}
663#endif