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