Annotation of src/usr.bin/ssh/kex.c, Revision 1.129
1.129 ! dtucker 1: /* $OpenBSD: kex.c,v 1.128 2017/02/03 23:01:19 djm Exp $ */
1.1 markus 2: /*
1.36 markus 3: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.1 markus 4: *
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.
13: *
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:
1.18 markus 26:
1.76 deraadt 27: #include <signal.h>
1.75 stevesk 28: #include <stdio.h>
1.74 stevesk 29: #include <stdlib.h>
1.72 stevesk 30: #include <string.h>
1.1 markus 31:
1.99 markus 32: #ifdef WITH_OPENSSL
1.76 deraadt 33: #include <openssl/crypto.h>
1.99 markus 34: #endif
1.76 deraadt 35:
1.1 markus 36: #include "ssh2.h"
1.7 markus 37: #include "packet.h"
1.1 markus 38: #include "compat.h"
1.18 markus 39: #include "cipher.h"
1.102 markus 40: #include "sshkey.h"
1.1 markus 41: #include "kex.h"
1.18 markus 42: #include "log.h"
1.21 markus 43: #include "mac.h"
1.23 markus 44: #include "match.h"
1.102 markus 45: #include "misc.h"
1.26 markus 46: #include "dispatch.h"
1.48 provos 47: #include "monitor.h"
1.102 markus 48:
49: #include "ssherr.h"
50: #include "sshbuf.h"
1.94 djm 51: #include "digest.h"
1.48 provos 52:
1.35 itojun 53: /* prototype */
1.102 markus 54: static int kex_choose_conf(struct ssh *);
55: static int kex_input_newkeys(int, u_int32_t, void *);
1.86 djm 56:
1.110 djm 57: static const char *proposal_names[PROPOSAL_MAX] = {
58: "KEX algorithms",
59: "host key algorithms",
60: "ciphers ctos",
61: "ciphers stoc",
62: "MACs ctos",
63: "MACs stoc",
64: "compression ctos",
65: "compression stoc",
66: "languages ctos",
67: "languages stoc",
68: };
69:
1.89 djm 70: struct kexalg {
71: char *name;
1.102 markus 72: u_int type;
1.89 djm 73: int ec_nid;
1.94 djm 74: int hash_alg;
1.89 djm 75: };
76: static const struct kexalg kexalgs[] = {
1.99 markus 77: #ifdef WITH_OPENSSL
1.94 djm 78: { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1.118 djm 79: { KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
80: { KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
81: { KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
82: { KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
1.94 djm 83: { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
84: { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
85: { 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 },
89: { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
90: SSH_DIGEST_SHA512 },
1.99 markus 91: #endif
1.94 djm 92: { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.124 djm 93: { KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.94 djm 94: { NULL, -1, -1, -1},
1.89 djm 95: };
96:
97: char *
1.93 dtucker 98: kex_alg_list(char sep)
1.89 djm 99: {
1.102 markus 100: char *ret = NULL, *tmp;
1.89 djm 101: size_t nlen, rlen = 0;
102: const struct kexalg *k;
103:
104: for (k = kexalgs; k->name != NULL; k++) {
105: if (ret != NULL)
1.93 dtucker 106: ret[rlen++] = sep;
1.89 djm 107: nlen = strlen(k->name);
1.102 markus 108: if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
109: free(ret);
110: return NULL;
111: }
112: ret = tmp;
1.89 djm 113: memcpy(ret + rlen, k->name, nlen + 1);
114: rlen += nlen;
115: }
116: return ret;
117: }
118:
119: static const struct kexalg *
120: kex_alg_by_name(const char *name)
121: {
122: const struct kexalg *k;
123:
124: for (k = kexalgs; k->name != NULL; k++) {
125: if (strcmp(k->name, name) == 0)
126: return k;
127: }
128: return NULL;
129: }
130:
1.86 djm 131: /* Validate KEX method name list */
132: int
133: kex_names_valid(const char *names)
134: {
135: char *s, *cp, *p;
136:
137: if (names == NULL || strcmp(names, "") == 0)
138: return 0;
1.102 markus 139: if ((s = cp = strdup(names)) == NULL)
140: return 0;
1.86 djm 141: for ((p = strsep(&cp, ",")); p && *p != '\0';
142: (p = strsep(&cp, ","))) {
1.89 djm 143: if (kex_alg_by_name(p) == NULL) {
1.86 djm 144: error("Unsupported KEX algorithm \"%.100s\"", p);
1.91 djm 145: free(s);
1.86 djm 146: return 0;
147: }
148: }
149: debug3("kex names ok: [%s]", names);
1.91 djm 150: free(s);
1.86 djm 151: return 1;
1.109 djm 152: }
153:
154: /*
155: * Concatenate algorithm names, avoiding duplicates in the process.
156: * Caller must free returned string.
157: */
158: char *
159: kex_names_cat(const char *a, const char *b)
160: {
1.129 ! dtucker 161: char *ret = NULL, *tmp = NULL, *cp, *p, *m;
1.109 djm 162: size_t len;
163:
164: if (a == NULL || *a == '\0')
165: return NULL;
166: if (b == NULL || *b == '\0')
167: return strdup(a);
168: if (strlen(b) > 1024*1024)
169: return NULL;
170: len = strlen(a) + strlen(b) + 2;
171: if ((tmp = cp = strdup(b)) == NULL ||
172: (ret = calloc(1, len)) == NULL) {
173: free(tmp);
174: return NULL;
175: }
176: strlcpy(ret, a, len);
177: for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
1.129 ! dtucker 178: if ((m = match_list(ret, p, NULL)) != NULL) {
! 179: free(m);
1.109 djm 180: continue; /* Algorithm already present */
1.129 ! dtucker 181: }
1.109 djm 182: if (strlcat(ret, ",", len) >= len ||
183: strlcat(ret, p, len) >= len) {
184: free(tmp);
185: free(ret);
186: return NULL; /* Shouldn't happen */
187: }
188: }
189: free(tmp);
190: return ret;
191: }
192:
193: /*
194: * Assemble a list of algorithms from a default list and a string from a
195: * configuration file. The user-provided string may begin with '+' to
1.128 djm 196: * indicate that it should be appended to the default or '-' that the
197: * specified names should be removed.
1.109 djm 198: */
199: int
200: kex_assemble_names(const char *def, char **list)
201: {
202: char *ret;
203:
204: if (list == NULL || *list == NULL || **list == '\0') {
205: *list = strdup(def);
206: return 0;
207: }
1.128 djm 208: if (**list == '+') {
209: if ((ret = kex_names_cat(def, *list + 1)) == NULL)
210: return SSH_ERR_ALLOC_FAIL;
211: free(*list);
212: *list = ret;
213: } else if (**list == '-') {
214: if ((ret = match_filter_list(def, *list + 1)) == NULL)
215: return SSH_ERR_ALLOC_FAIL;
216: free(*list);
217: *list = ret;
1.109 djm 218: }
219:
220: return 0;
1.86 djm 221: }
1.26 markus 222:
223: /* put algorithm proposal into buffer */
1.102 markus 224: int
225: kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1.1 markus 226: {
1.61 djm 227: u_int i;
1.102 markus 228: int r;
229:
230: sshbuf_reset(b);
1.26 markus 231:
1.49 markus 232: /*
233: * add a dummy cookie, the cookie will be overwritten by
234: * kex_send_kexinit(), each time a kexinit is set
235: */
1.102 markus 236: for (i = 0; i < KEX_COOKIE_LEN; i++) {
237: if ((r = sshbuf_put_u8(b, 0)) != 0)
238: return r;
239: }
240: for (i = 0; i < PROPOSAL_MAX; i++) {
241: if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
242: return r;
243: }
244: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */
245: (r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */
246: return r;
247: return 0;
1.1 markus 248: }
249:
1.26 markus 250: /* parse buffer and return algorithm proposal */
1.102 markus 251: int
252: kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
1.7 markus 253: {
1.102 markus 254: struct sshbuf *b = NULL;
255: u_char v;
1.78 djm 256: u_int i;
1.102 markus 257: char **proposal = NULL;
258: int r;
1.7 markus 259:
1.102 markus 260: *propp = NULL;
261: if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
262: return SSH_ERR_ALLOC_FAIL;
263: if ((b = sshbuf_fromb(raw)) == NULL) {
264: r = SSH_ERR_ALLOC_FAIL;
265: goto out;
266: }
267: if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
268: goto out;
1.7 markus 269: /* extract kex init proposal strings */
270: for (i = 0; i < PROPOSAL_MAX; i++) {
1.102 markus 271: if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
272: goto out;
1.110 djm 273: debug2("%s: %s", proposal_names[i], proposal[i]);
1.7 markus 274: }
1.26 markus 275: /* first kex follows / reserved */
1.115 djm 276: if ((r = sshbuf_get_u8(b, &v)) != 0 || /* first_kex_follows */
277: (r = sshbuf_get_u32(b, &i)) != 0) /* reserved */
1.102 markus 278: goto out;
1.53 markus 279: if (first_kex_follows != NULL)
1.115 djm 280: *first_kex_follows = v;
1.110 djm 281: debug2("first_kex_follows %d ", v);
282: debug2("reserved %u ", i);
1.102 markus 283: r = 0;
284: *propp = proposal;
285: out:
286: if (r != 0 && proposal != NULL)
287: kex_prop_free(proposal);
288: sshbuf_free(b);
289: return r;
1.1 markus 290: }
291:
1.102 markus 292: void
1.26 markus 293: kex_prop_free(char **proposal)
1.1 markus 294: {
1.61 djm 295: u_int i;
1.26 markus 296:
1.106 djm 297: if (proposal == NULL)
298: return;
1.26 markus 299: for (i = 0; i < PROPOSAL_MAX; i++)
1.91 djm 300: free(proposal[i]);
301: free(proposal);
1.1 markus 302: }
303:
1.78 djm 304: /* ARGSUSED */
1.101 markus 305: static int
1.41 markus 306: kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1.1 markus 307: {
1.112 djm 308: struct ssh *ssh = active_state; /* XXX */
309: int r;
310:
311: error("kex protocol error: type %d seq %u", type, seq);
312: if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
313: (r = sshpkt_put_u32(ssh, seq)) != 0 ||
314: (r = sshpkt_send(ssh)) != 0)
315: return r;
1.101 markus 316: return 0;
1.26 markus 317: }
1.1 markus 318:
1.35 itojun 319: static void
1.102 markus 320: kex_reset_dispatch(struct ssh *ssh)
1.29 markus 321: {
1.102 markus 322: ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42 markus 323: SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.102 markus 324: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.29 markus 325: }
326:
1.113 markus 327: static int
328: kex_send_ext_info(struct ssh *ssh)
329: {
330: int r;
1.121 djm 331: char *algs;
1.113 markus 332:
1.121 djm 333: if ((algs = sshkey_alg_list(0, 1, ',')) == NULL)
334: return SSH_ERR_ALLOC_FAIL;
1.113 markus 335: if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
336: (r = sshpkt_put_u32(ssh, 1)) != 0 ||
337: (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
1.121 djm 338: (r = sshpkt_put_cstring(ssh, algs)) != 0 ||
1.113 markus 339: (r = sshpkt_send(ssh)) != 0)
1.121 djm 340: goto out;
341: /* success */
342: r = 0;
343: out:
344: free(algs);
1.123 djm 345: return r;
1.113 markus 346: }
347:
1.102 markus 348: int
349: kex_send_newkeys(struct ssh *ssh)
1.26 markus 350: {
1.102 markus 351: int r;
1.28 markus 352:
1.102 markus 353: kex_reset_dispatch(ssh);
354: if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
355: (r = sshpkt_send(ssh)) != 0)
356: return r;
1.26 markus 357: debug("SSH2_MSG_NEWKEYS sent");
1.102 markus 358: debug("expecting SSH2_MSG_NEWKEYS");
359: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.113 markus 360: if (ssh->kex->ext_info_c)
361: if ((r = kex_send_ext_info(ssh)) != 0)
362: return r;
1.102 markus 363: return 0;
364: }
365:
1.113 markus 366: int
367: kex_input_ext_info(int type, u_int32_t seq, void *ctxt)
368: {
369: struct ssh *ssh = ctxt;
370: struct kex *kex = ssh->kex;
371: u_int32_t i, ninfo;
372: char *name, *val, *found;
373: int r;
374:
375: debug("SSH2_MSG_EXT_INFO received");
376: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
377: if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
378: return r;
379: for (i = 0; i < ninfo; i++) {
380: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
381: return r;
382: if ((r = sshpkt_get_cstring(ssh, &val, NULL)) != 0) {
383: free(name);
384: return r;
385: }
386: debug("%s: %s=<%s>", __func__, name, val);
387: if (strcmp(name, "server-sig-algs") == 0) {
388: found = match_list("rsa-sha2-256", val, NULL);
389: if (found) {
390: kex->rsa_sha2 = 256;
391: free(found);
392: }
393: found = match_list("rsa-sha2-512", val, NULL);
394: if (found) {
395: kex->rsa_sha2 = 512;
396: free(found);
397: }
398: }
399: free(name);
400: free(val);
401: }
402: return sshpkt_get_end(ssh);
403: }
404:
1.102 markus 405: static int
406: kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
407: {
408: struct ssh *ssh = ctxt;
409: struct kex *kex = ssh->kex;
410: int r;
1.19 stevesk 411:
1.26 markus 412: debug("SSH2_MSG_NEWKEYS received");
1.102 markus 413: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
414: if ((r = sshpkt_get_end(ssh)) != 0)
1.122 markus 415: return r;
416: if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0)
1.102 markus 417: return r;
1.30 markus 418: kex->done = 1;
1.102 markus 419: sshbuf_reset(kex->peer);
420: /* sshbuf_reset(kex->my); */
1.26 markus 421: kex->flags &= ~KEX_INIT_SENT;
1.91 djm 422: free(kex->name);
1.32 markus 423: kex->name = NULL;
1.102 markus 424: return 0;
1.26 markus 425: }
1.1 markus 426:
1.102 markus 427: int
428: kex_send_kexinit(struct ssh *ssh)
1.26 markus 429: {
1.49 markus 430: u_char *cookie;
1.102 markus 431: struct kex *kex = ssh->kex;
432: int r;
1.49 markus 433:
1.102 markus 434: if (kex == NULL)
435: return SSH_ERR_INTERNAL_ERROR;
436: if (kex->flags & KEX_INIT_SENT)
437: return 0;
1.30 markus 438: kex->done = 0;
1.49 markus 439:
440: /* generate a random cookie */
1.102 markus 441: if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
442: return SSH_ERR_INVALID_FORMAT;
443: if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
444: return SSH_ERR_INTERNAL_ERROR;
445: arc4random_buf(cookie, KEX_COOKIE_LEN);
446:
447: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
448: (r = sshpkt_putb(ssh, kex->my)) != 0 ||
449: (r = sshpkt_send(ssh)) != 0)
450: return r;
1.26 markus 451: debug("SSH2_MSG_KEXINIT sent");
452: kex->flags |= KEX_INIT_SENT;
1.102 markus 453: return 0;
1.1 markus 454: }
455:
1.78 djm 456: /* ARGSUSED */
1.101 markus 457: int
1.41 markus 458: kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
1.11 provos 459: {
1.102 markus 460: struct ssh *ssh = ctxt;
461: struct kex *kex = ssh->kex;
462: const u_char *ptr;
1.100 markus 463: u_int i;
464: size_t dlen;
1.102 markus 465: int r;
1.11 provos 466:
1.26 markus 467: debug("SSH2_MSG_KEXINIT received");
1.29 markus 468: if (kex == NULL)
1.102 markus 469: return SSH_ERR_INVALID_ARGUMENT;
1.11 provos 470:
1.127 markus 471: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
1.102 markus 472: ptr = sshpkt_ptr(ssh, &dlen);
473: if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
474: return r;
1.31 markus 475:
476: /* discard packet */
477: for (i = 0; i < KEX_COOKIE_LEN; i++)
1.102 markus 478: if ((r = sshpkt_get_u8(ssh, NULL)) != 0)
479: return r;
1.31 markus 480: for (i = 0; i < PROPOSAL_MAX; i++)
1.102 markus 481: if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0)
482: return r;
1.87 djm 483: /*
484: * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
485: * KEX method has the server move first, but a server might be using
486: * a custom method or one that we otherwise don't support. We should
487: * be prepared to remember first_kex_follows here so we can eat a
488: * packet later.
489: * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
490: * for cases where the server *doesn't* go first. I guess we should
491: * ignore it when it is set for these cases, which is what we do now.
492: */
1.102 markus 493: if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
494: (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
495: (r = sshpkt_get_end(ssh)) != 0)
496: return r;
497:
498: if (!(kex->flags & KEX_INIT_SENT))
499: if ((r = kex_send_kexinit(ssh)) != 0)
500: return r;
501: if ((r = kex_choose_conf(ssh)) != 0)
502: return r;
1.19 stevesk 503:
1.102 markus 504: if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
505: return (kex->kex[kex->kex_type])(ssh);
506:
507: return SSH_ERR_INTERNAL_ERROR;
508: }
509:
510: int
511: kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp)
512: {
513: struct kex *kex;
514: int r;
515:
516: *kexp = NULL;
517: if ((kex = calloc(1, sizeof(*kex))) == NULL)
518: return SSH_ERR_ALLOC_FAIL;
519: if ((kex->peer = sshbuf_new()) == NULL ||
520: (kex->my = sshbuf_new()) == NULL) {
521: r = SSH_ERR_ALLOC_FAIL;
522: goto out;
523: }
524: if ((r = kex_prop2buf(kex->my, proposal)) != 0)
525: goto out;
526: kex->done = 0;
527: kex_reset_dispatch(ssh);
528: r = 0;
529: *kexp = kex;
530: out:
531: if (r != 0)
532: kex_free(kex);
533: return r;
1.26 markus 534: }
1.11 provos 535:
1.100 markus 536: void
537: kex_free_newkeys(struct newkeys *newkeys)
538: {
539: if (newkeys == NULL)
540: return;
541: if (newkeys->enc.key) {
542: explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
543: free(newkeys->enc.key);
544: newkeys->enc.key = NULL;
545: }
546: if (newkeys->enc.iv) {
1.111 djm 547: explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
1.100 markus 548: free(newkeys->enc.iv);
549: newkeys->enc.iv = NULL;
550: }
551: free(newkeys->enc.name);
552: explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
553: free(newkeys->comp.name);
554: explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
555: mac_clear(&newkeys->mac);
556: if (newkeys->mac.key) {
557: explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
558: free(newkeys->mac.key);
559: newkeys->mac.key = NULL;
560: }
561: free(newkeys->mac.name);
562: explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
563: explicit_bzero(newkeys, sizeof(*newkeys));
564: free(newkeys);
565: }
566:
1.102 markus 567: void
568: kex_free(struct kex *kex)
1.26 markus 569: {
1.102 markus 570: u_int mode;
1.11 provos 571:
1.102 markus 572: #ifdef WITH_OPENSSL
573: if (kex->dh)
574: DH_free(kex->dh);
575: if (kex->ec_client_key)
576: EC_KEY_free(kex->ec_client_key);
577: #endif
578: for (mode = 0; mode < MODE_MAX; mode++) {
579: kex_free_newkeys(kex->newkeys[mode]);
580: kex->newkeys[mode] = NULL;
1.100 markus 581: }
1.102 markus 582: sshbuf_free(kex->peer);
583: sshbuf_free(kex->my);
584: free(kex->session_id);
585: free(kex->client_version_string);
586: free(kex->server_version_string);
1.107 djm 587: free(kex->failed_choice);
1.113 markus 588: free(kex->hostkey_alg);
589: free(kex->name);
1.102 markus 590: free(kex);
1.11 provos 591: }
592:
1.102 markus 593: int
594: kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
1.1 markus 595: {
1.102 markus 596: int r;
1.1 markus 597:
1.102 markus 598: if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0)
599: return r;
600: if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
601: kex_free(ssh->kex);
602: ssh->kex = NULL;
603: return r;
1.1 markus 604: }
1.102 markus 605: return 0;
1.117 djm 606: }
607:
608: /*
609: * Request key re-exchange, returns 0 on success or a ssherr.h error
610: * code otherwise. Must not be called if KEX is incomplete or in-progress.
611: */
612: int
613: kex_start_rekex(struct ssh *ssh)
614: {
615: if (ssh->kex == NULL) {
616: error("%s: no kex", __func__);
617: return SSH_ERR_INTERNAL_ERROR;
618: }
619: if (ssh->kex->done == 0) {
620: error("%s: requested twice", __func__);
621: return SSH_ERR_INTERNAL_ERROR;
622: }
623: ssh->kex->done = 0;
624: return kex_send_kexinit(ssh);
1.1 markus 625: }
626:
1.102 markus 627: static int
628: choose_enc(struct sshenc *enc, char *client, char *server)
1.1 markus 629: {
1.23 markus 630: char *name = match_list(client, server, NULL);
1.102 markus 631:
1.1 markus 632: if (name == NULL)
1.102 markus 633: return SSH_ERR_NO_CIPHER_ALG_MATCH;
1.129 ! dtucker 634: if ((enc->cipher = cipher_by_name(name)) == NULL) {
! 635: free(name);
1.102 markus 636: return SSH_ERR_INTERNAL_ERROR;
1.129 ! dtucker 637: }
1.1 markus 638: enc->name = name;
639: enc->enabled = 0;
640: enc->iv = NULL;
1.88 markus 641: enc->iv_len = cipher_ivlen(enc->cipher);
1.1 markus 642: enc->key = NULL;
1.45 markus 643: enc->key_len = cipher_keylen(enc->cipher);
644: enc->block_size = cipher_blocksize(enc->cipher);
1.102 markus 645: return 0;
1.1 markus 646: }
1.69 deraadt 647:
1.102 markus 648: static int
649: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1 markus 650: {
1.23 markus 651: char *name = match_list(client, server, NULL);
1.102 markus 652:
1.1 markus 653: if (name == NULL)
1.102 markus 654: return SSH_ERR_NO_MAC_ALG_MATCH;
1.129 ! dtucker 655: if (mac_setup(mac, name) < 0) {
! 656: free(name);
1.102 markus 657: return SSH_ERR_INTERNAL_ERROR;
1.129 ! dtucker 658: }
1.21 markus 659: /* truncate the key */
1.102 markus 660: if (ssh->compat & SSH_BUG_HMAC)
1.21 markus 661: mac->key_len = 16;
1.1 markus 662: mac->name = name;
663: mac->key = NULL;
664: mac->enabled = 0;
1.102 markus 665: return 0;
1.1 markus 666: }
1.69 deraadt 667:
1.102 markus 668: static int
669: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1 markus 670: {
1.23 markus 671: char *name = match_list(client, server, NULL);
1.102 markus 672:
1.1 markus 673: if (name == NULL)
1.102 markus 674: return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.64 markus 675: if (strcmp(name, "zlib@openssh.com") == 0) {
676: comp->type = COMP_DELAYED;
1.126 djm 677: } else if (strcmp(name, "zlib") == 0) {
678: comp->type = COMP_ZLIB;
1.1 markus 679: } else if (strcmp(name, "none") == 0) {
1.64 markus 680: comp->type = COMP_NONE;
1.1 markus 681: } else {
1.129 ! dtucker 682: free(name);
1.102 markus 683: return SSH_ERR_INTERNAL_ERROR;
1.1 markus 684: }
685: comp->name = name;
1.102 markus 686: return 0;
1.1 markus 687: }
1.69 deraadt 688:
1.102 markus 689: static int
690: choose_kex(struct kex *k, char *client, char *server)
1.1 markus 691: {
1.89 djm 692: const struct kexalg *kexalg;
693:
1.23 markus 694: k->name = match_list(client, server, NULL);
1.102 markus 695:
1.110 djm 696: debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1 markus 697: if (k->name == NULL)
1.102 markus 698: return SSH_ERR_NO_KEX_ALG_MATCH;
1.89 djm 699: if ((kexalg = kex_alg_by_name(k->name)) == NULL)
1.102 markus 700: return SSH_ERR_INTERNAL_ERROR;
1.89 djm 701: k->kex_type = kexalg->type;
1.94 djm 702: k->hash_alg = kexalg->hash_alg;
1.89 djm 703: k->ec_nid = kexalg->ec_nid;
1.102 markus 704: return 0;
1.1 markus 705: }
1.65 djm 706:
1.102 markus 707: static int
708: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1 markus 709: {
1.113 markus 710: k->hostkey_alg = match_list(client, server, NULL);
1.102 markus 711:
1.110 djm 712: debug("kex: host key algorithm: %s",
1.113 markus 713: k->hostkey_alg ? k->hostkey_alg : "(no match)");
714: if (k->hostkey_alg == NULL)
1.102 markus 715: return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113 markus 716: k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.13 markus 717: if (k->hostkey_type == KEY_UNSPEC)
1.102 markus 718: return SSH_ERR_INTERNAL_ERROR;
1.113 markus 719: k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102 markus 720: return 0;
1.1 markus 721: }
722:
1.56 djm 723: static int
1.53 markus 724: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
725: {
726: static int check[] = {
727: PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
728: };
729: int *idx;
730: char *p;
731:
732: for (idx = &check[0]; *idx != -1; idx++) {
733: if ((p = strchr(my[*idx], ',')) != NULL)
734: *p = '\0';
735: if ((p = strchr(peer[*idx], ',')) != NULL)
736: *p = '\0';
737: if (strcmp(my[*idx], peer[*idx]) != 0) {
738: debug2("proposal mismatch: my %s peer %s",
739: my[*idx], peer[*idx]);
740: return (0);
741: }
742: }
743: debug2("proposals match");
744: return (1);
745: }
746:
1.102 markus 747: static int
748: kex_choose_conf(struct ssh *ssh)
1.1 markus 749: {
1.102 markus 750: struct kex *kex = ssh->kex;
751: struct newkeys *newkeys;
752: char **my = NULL, **peer = NULL;
1.26 markus 753: char **cprop, **sprop;
1.27 markus 754: int nenc, nmac, ncomp;
1.96 dtucker 755: u_int mode, ctos, need, dh_need, authlen;
1.102 markus 756: int r, first_kex_follows;
1.1 markus 757:
1.110 djm 758: debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
759: if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
760: goto out;
761: debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
762: if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102 markus 763: goto out;
1.26 markus 764:
1.27 markus 765: if (kex->server) {
1.26 markus 766: cprop=peer;
767: sprop=my;
768: } else {
769: cprop=my;
770: sprop=peer;
1.113 markus 771: }
772:
773: /* Check whether client supports ext_info_c */
774: if (kex->server) {
775: char *ext;
776:
777: ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
1.119 markus 778: kex->ext_info_c = (ext != NULL);
779: free(ext);
1.26 markus 780: }
1.1 markus 781:
1.30 markus 782: /* Algorithm Negotiation */
1.110 djm 783: if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
784: sprop[PROPOSAL_KEX_ALGS])) != 0) {
785: kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
786: peer[PROPOSAL_KEX_ALGS] = NULL;
787: goto out;
788: }
789: if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
790: sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
791: kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
792: peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
793: goto out;
794: }
1.1 markus 795: for (mode = 0; mode < MODE_MAX; mode++) {
1.102 markus 796: if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
797: r = SSH_ERR_ALLOC_FAIL;
798: goto out;
799: }
1.30 markus 800: kex->newkeys[mode] = newkeys;
1.78 djm 801: ctos = (!kex->server && mode == MODE_OUT) ||
802: (kex->server && mode == MODE_IN);
1.1 markus 803: nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
804: nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
805: ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102 markus 806: if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107 djm 807: sprop[nenc])) != 0) {
808: kex->failed_choice = peer[nenc];
809: peer[nenc] = NULL;
1.102 markus 810: goto out;
1.107 djm 811: }
1.102 markus 812: authlen = cipher_authlen(newkeys->enc.cipher);
1.88 markus 813: /* ignore mac for authenticated encryption */
1.102 markus 814: if (authlen == 0 &&
815: (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107 djm 816: sprop[nmac])) != 0) {
817: kex->failed_choice = peer[nmac];
818: peer[nmac] = NULL;
1.102 markus 819: goto out;
1.107 djm 820: }
1.102 markus 821: if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107 djm 822: sprop[ncomp])) != 0) {
823: kex->failed_choice = peer[ncomp];
824: peer[ncomp] = NULL;
1.102 markus 825: goto out;
1.107 djm 826: }
1.110 djm 827: debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1 markus 828: ctos ? "client->server" : "server->client",
1.27 markus 829: newkeys->enc.name,
1.88 markus 830: authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27 markus 831: newkeys->comp.name);
1.107 djm 832: }
1.96 dtucker 833: need = dh_need = 0;
1.1 markus 834: for (mode = 0; mode < MODE_MAX; mode++) {
1.30 markus 835: newkeys = kex->newkeys[mode];
1.120 deraadt 836: need = MAXIMUM(need, newkeys->enc.key_len);
837: need = MAXIMUM(need, newkeys->enc.block_size);
838: need = MAXIMUM(need, newkeys->enc.iv_len);
839: need = MAXIMUM(need, newkeys->mac.key_len);
840: dh_need = MAXIMUM(dh_need, cipher_seclen(newkeys->enc.cipher));
841: dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
842: dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
843: dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
1.1 markus 844: }
1.7 markus 845: /* XXX need runden? */
1.27 markus 846: kex->we_need = need;
1.96 dtucker 847: kex->dh_need = dh_need;
1.53 markus 848:
849: /* ignore the next message if the proposals do not match */
1.56 djm 850: if (first_kex_follows && !proposals_match(my, peer) &&
1.102 markus 851: !(ssh->compat & SSH_BUG_FIRSTKEX))
852: ssh->dispatch_skip_packets = 1;
853: r = 0;
854: out:
1.26 markus 855: kex_prop_free(my);
856: kex_prop_free(peer);
1.102 markus 857: return r;
1.26 markus 858: }
859:
1.102 markus 860: static int
861: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
862: const struct sshbuf *shared_secret, u_char **keyp)
1.26 markus 863: {
1.102 markus 864: struct kex *kex = ssh->kex;
865: struct ssh_digest_ctx *hashctx = NULL;
1.26 markus 866: char c = id;
1.61 djm 867: u_int have;
1.94 djm 868: size_t mdsz;
1.61 djm 869: u_char *digest;
1.102 markus 870: int r;
1.62 djm 871:
1.94 djm 872: if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102 markus 873: return SSH_ERR_INVALID_ARGUMENT;
1.120 deraadt 874: if ((digest = calloc(1, ROUNDUP(need, mdsz))) == NULL) {
1.102 markus 875: r = SSH_ERR_ALLOC_FAIL;
876: goto out;
877: }
1.26 markus 878:
1.30 markus 879: /* K1 = HASH(K || H || "A" || session_id) */
1.102 markus 880: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
881: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 882: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
883: ssh_digest_update(hashctx, &c, 1) != 0 ||
884: ssh_digest_update(hashctx, kex->session_id,
1.102 markus 885: kex->session_id_len) != 0 ||
886: ssh_digest_final(hashctx, digest, mdsz) != 0) {
887: r = SSH_ERR_LIBCRYPTO_ERROR;
888: goto out;
889: }
1.94 djm 890: ssh_digest_free(hashctx);
1.102 markus 891: hashctx = NULL;
1.26 markus 892:
1.30 markus 893: /*
894: * expand key:
895: * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
896: * Key = K1 || K2 || ... || Kn
897: */
1.26 markus 898: for (have = mdsz; need > have; have += mdsz) {
1.102 markus 899: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
900: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 901: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102 markus 902: ssh_digest_update(hashctx, digest, have) != 0 ||
903: ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
904: r = SSH_ERR_LIBCRYPTO_ERROR;
905: goto out;
906: }
1.94 djm 907: ssh_digest_free(hashctx);
1.102 markus 908: hashctx = NULL;
1.26 markus 909: }
910: #ifdef DEBUG_KEX
911: fprintf(stderr, "key '%c'== ", c);
912: dump_digest("key", digest, need);
913: #endif
1.102 markus 914: *keyp = digest;
915: digest = NULL;
916: r = 0;
917: out:
1.114 mmcc 918: free(digest);
1.102 markus 919: ssh_digest_free(hashctx);
920: return r;
1.1 markus 921: }
922:
1.23 markus 923: #define NKEYS 6
1.102 markus 924: int
925: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
926: const struct sshbuf *shared_secret)
1.1 markus 927: {
1.102 markus 928: struct kex *kex = ssh->kex;
1.15 markus 929: u_char *keys[NKEYS];
1.102 markus 930: u_int i, j, mode, ctos;
931: int r;
1.1 markus 932:
1.65 djm 933: for (i = 0; i < NKEYS; i++) {
1.102 markus 934: if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
935: shared_secret, &keys[i])) != 0) {
936: for (j = 0; j < i; j++)
937: free(keys[j]);
938: return r;
939: }
1.65 djm 940: }
1.1 markus 941: for (mode = 0; mode < MODE_MAX; mode++) {
1.69 deraadt 942: ctos = (!kex->server && mode == MODE_OUT) ||
943: (kex->server && mode == MODE_IN);
1.100 markus 944: kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1];
945: kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
946: kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1 markus 947: }
1.102 markus 948: return 0;
1.95 djm 949: }
950:
1.99 markus 951: #ifdef WITH_OPENSSL
1.102 markus 952: int
953: kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
954: const BIGNUM *secret)
1.95 djm 955: {
1.102 markus 956: struct sshbuf *shared_secret;
957: int r;
1.95 djm 958:
1.102 markus 959: if ((shared_secret = sshbuf_new()) == NULL)
960: return SSH_ERR_ALLOC_FAIL;
961: if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
962: r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
963: sshbuf_free(shared_secret);
964: return r;
1.27 markus 965: }
1.99 markus 966: #endif
1.57 djm 967:
1.99 markus 968: #ifdef WITH_SSH1
1.102 markus 969: int
1.57 djm 970: derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
971: u_int8_t cookie[8], u_int8_t id[16])
972: {
1.105 djm 973: u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
1.102 markus 974: struct ssh_digest_ctx *hashctx = NULL;
1.105 djm 975: size_t hlen, slen;
1.102 markus 976: int r;
1.57 djm 977:
1.105 djm 978: hlen = BN_num_bytes(host_modulus);
979: slen = BN_num_bytes(server_modulus);
980: if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) ||
981: slen < (512 / 8) || (u_int)slen > sizeof(sbuf))
1.102 markus 982: return SSH_ERR_KEY_BITS_MISMATCH;
1.105 djm 983: if (BN_bn2bin(host_modulus, hbuf) <= 0 ||
984: BN_bn2bin(server_modulus, sbuf) <= 0) {
985: r = SSH_ERR_LIBCRYPTO_ERROR;
986: goto out;
987: }
988: if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) {
989: r = SSH_ERR_ALLOC_FAIL;
990: goto out;
991: }
992: if (ssh_digest_update(hashctx, hbuf, hlen) != 0 ||
993: ssh_digest_update(hashctx, sbuf, slen) != 0 ||
1.102 markus 994: ssh_digest_update(hashctx, cookie, 8) != 0 ||
995: ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
996: r = SSH_ERR_LIBCRYPTO_ERROR;
997: goto out;
998: }
1.94 djm 999: memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
1.102 markus 1000: r = 0;
1001: out:
1002: ssh_digest_free(hashctx);
1.105 djm 1003: explicit_bzero(hbuf, sizeof(hbuf));
1004: explicit_bzero(sbuf, sizeof(sbuf));
1.98 djm 1005: explicit_bzero(obuf, sizeof(obuf));
1.102 markus 1006: return r;
1.1 markus 1007: }
1.99 markus 1008: #endif
1.26 markus 1009:
1.84 djm 1010: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26 markus 1011: void
1012: dump_digest(char *msg, u_char *digest, int len)
1013: {
1014: fprintf(stderr, "%s\n", msg);
1.102 markus 1015: sshbuf_dump_data(digest, len, stderr);
1.26 markus 1016: }
1017: #endif