Annotation of src/usr.bin/ssh/kex.c, Revision 1.171
1.171 ! djm 1: /* $OpenBSD: kex.c,v 1.170 2021/12/19 22:13:12 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.143 djm 27: #include <sys/types.h>
28: #include <errno.h>
1.76 deraadt 29: #include <signal.h>
1.75 stevesk 30: #include <stdio.h>
1.74 stevesk 31: #include <stdlib.h>
1.72 stevesk 32: #include <string.h>
1.143 djm 33: #include <unistd.h>
34: #include <poll.h>
1.1 markus 35:
1.99 markus 36: #ifdef WITH_OPENSSL
1.76 deraadt 37: #include <openssl/crypto.h>
1.99 markus 38: #endif
1.76 deraadt 39:
1.143 djm 40: #include "ssh.h"
1.1 markus 41: #include "ssh2.h"
1.143 djm 42: #include "atomicio.h"
43: #include "version.h"
1.7 markus 44: #include "packet.h"
1.1 markus 45: #include "compat.h"
1.18 markus 46: #include "cipher.h"
1.102 markus 47: #include "sshkey.h"
1.1 markus 48: #include "kex.h"
1.18 markus 49: #include "log.h"
1.21 markus 50: #include "mac.h"
1.23 markus 51: #include "match.h"
1.102 markus 52: #include "misc.h"
1.26 markus 53: #include "dispatch.h"
1.48 provos 54: #include "monitor.h"
1.102 markus 55:
56: #include "ssherr.h"
57: #include "sshbuf.h"
1.94 djm 58: #include "digest.h"
1.48 provos 59:
1.35 itojun 60: /* prototype */
1.102 markus 61: static int kex_choose_conf(struct ssh *);
1.133 markus 62: static int kex_input_newkeys(int, u_int32_t, struct ssh *);
1.86 djm 63:
1.110 djm 64: static const char *proposal_names[PROPOSAL_MAX] = {
65: "KEX algorithms",
66: "host key algorithms",
67: "ciphers ctos",
68: "ciphers stoc",
69: "MACs ctos",
70: "MACs stoc",
71: "compression ctos",
72: "compression stoc",
73: "languages ctos",
74: "languages stoc",
75: };
76:
1.89 djm 77: struct kexalg {
78: char *name;
1.102 markus 79: u_int type;
1.89 djm 80: int ec_nid;
1.94 djm 81: int hash_alg;
1.89 djm 82: };
83: static const struct kexalg kexalgs[] = {
1.99 markus 84: #ifdef WITH_OPENSSL
1.94 djm 85: { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1.118 djm 86: { KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
87: { KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
88: { KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
89: { KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
1.94 djm 90: { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
91: { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
92: { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
93: NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
94: { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
95: SSH_DIGEST_SHA384 },
96: { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
97: SSH_DIGEST_SHA512 },
1.99 markus 98: #endif
1.94 djm 99: { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.124 djm 100: { KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.163 djm 101: { KEX_SNTRUP761X25519_SHA512, KEX_KEM_SNTRUP761X25519_SHA512, 0,
1.147 djm 102: SSH_DIGEST_SHA512 },
1.155 dtucker 103: { NULL, 0, -1, -1},
1.89 djm 104: };
105:
106: char *
1.93 dtucker 107: kex_alg_list(char sep)
1.89 djm 108: {
1.102 markus 109: char *ret = NULL, *tmp;
1.89 djm 110: size_t nlen, rlen = 0;
111: const struct kexalg *k;
112:
113: for (k = kexalgs; k->name != NULL; k++) {
114: if (ret != NULL)
1.93 dtucker 115: ret[rlen++] = sep;
1.89 djm 116: nlen = strlen(k->name);
1.102 markus 117: if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
118: free(ret);
119: return NULL;
120: }
121: ret = tmp;
1.89 djm 122: memcpy(ret + rlen, k->name, nlen + 1);
123: rlen += nlen;
124: }
125: return ret;
126: }
127:
128: static const struct kexalg *
129: kex_alg_by_name(const char *name)
130: {
131: const struct kexalg *k;
132:
133: for (k = kexalgs; k->name != NULL; k++) {
134: if (strcmp(k->name, name) == 0)
135: return k;
136: }
137: return NULL;
138: }
139:
1.86 djm 140: /* Validate KEX method name list */
141: int
142: kex_names_valid(const char *names)
143: {
144: char *s, *cp, *p;
145:
146: if (names == NULL || strcmp(names, "") == 0)
147: return 0;
1.102 markus 148: if ((s = cp = strdup(names)) == NULL)
149: return 0;
1.86 djm 150: for ((p = strsep(&cp, ",")); p && *p != '\0';
151: (p = strsep(&cp, ","))) {
1.89 djm 152: if (kex_alg_by_name(p) == NULL) {
1.86 djm 153: error("Unsupported KEX algorithm \"%.100s\"", p);
1.91 djm 154: free(s);
1.86 djm 155: return 0;
156: }
157: }
158: debug3("kex names ok: [%s]", names);
1.91 djm 159: free(s);
1.86 djm 160: return 1;
1.109 djm 161: }
162:
163: /*
164: * Concatenate algorithm names, avoiding duplicates in the process.
165: * Caller must free returned string.
166: */
167: char *
168: kex_names_cat(const char *a, const char *b)
169: {
1.129 dtucker 170: char *ret = NULL, *tmp = NULL, *cp, *p, *m;
1.109 djm 171: size_t len;
172:
173: if (a == NULL || *a == '\0')
1.138 djm 174: return strdup(b);
1.109 djm 175: if (b == NULL || *b == '\0')
176: return strdup(a);
177: if (strlen(b) > 1024*1024)
178: return NULL;
179: len = strlen(a) + strlen(b) + 2;
180: if ((tmp = cp = strdup(b)) == NULL ||
181: (ret = calloc(1, len)) == NULL) {
182: free(tmp);
183: return NULL;
184: }
185: strlcpy(ret, a, len);
186: for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
1.129 dtucker 187: if ((m = match_list(ret, p, NULL)) != NULL) {
188: free(m);
1.109 djm 189: continue; /* Algorithm already present */
1.129 dtucker 190: }
1.109 djm 191: if (strlcat(ret, ",", len) >= len ||
192: strlcat(ret, p, len) >= len) {
193: free(tmp);
194: free(ret);
195: return NULL; /* Shouldn't happen */
196: }
197: }
198: free(tmp);
199: return ret;
200: }
201:
202: /*
203: * Assemble a list of algorithms from a default list and a string from a
204: * configuration file. The user-provided string may begin with '+' to
1.154 naddy 205: * indicate that it should be appended to the default, '-' that the
206: * specified names should be removed, or '^' that they should be placed
207: * at the head.
1.109 djm 208: */
209: int
1.138 djm 210: kex_assemble_names(char **listp, const char *def, const char *all)
1.109 djm 211: {
1.138 djm 212: char *cp, *tmp, *patterns;
213: char *list = NULL, *ret = NULL, *matching = NULL, *opatterns = NULL;
214: int r = SSH_ERR_INTERNAL_ERROR;
215:
1.153 djm 216: if (listp == NULL || def == NULL || all == NULL)
217: return SSH_ERR_INVALID_ARGUMENT;
218:
219: if (*listp == NULL || **listp == '\0') {
1.138 djm 220: if ((*listp = strdup(def)) == NULL)
221: return SSH_ERR_ALLOC_FAIL;
222: return 0;
223: }
1.109 djm 224:
1.138 djm 225: list = *listp;
226: *listp = NULL;
227: if (*list == '+') {
228: /* Append names to default list */
229: if ((tmp = kex_names_cat(def, list + 1)) == NULL) {
230: r = SSH_ERR_ALLOC_FAIL;
231: goto fail;
232: }
233: free(list);
234: list = tmp;
235: } else if (*list == '-') {
236: /* Remove names from default list */
1.159 djm 237: if ((*listp = match_filter_denylist(def, list + 1)) == NULL) {
1.138 djm 238: r = SSH_ERR_ALLOC_FAIL;
239: goto fail;
240: }
241: free(list);
242: /* filtering has already been done */
1.109 djm 243: return 0;
1.154 naddy 244: } else if (*list == '^') {
245: /* Place names at head of default list */
246: if ((tmp = kex_names_cat(list + 1, def)) == NULL) {
247: r = SSH_ERR_ALLOC_FAIL;
248: goto fail;
249: }
250: free(list);
251: list = tmp;
1.138 djm 252: } else {
253: /* Explicit list, overrides default - just use "list" as is */
254: }
255:
256: /*
257: * The supplied names may be a pattern-list. For the -list case,
258: * the patterns are applied above. For the +list and explicit list
259: * cases we need to do it now.
260: */
261: ret = NULL;
262: if ((patterns = opatterns = strdup(list)) == NULL) {
263: r = SSH_ERR_ALLOC_FAIL;
264: goto fail;
265: }
266: /* Apply positive (i.e. non-negated) patterns from the list */
267: while ((cp = strsep(&patterns, ",")) != NULL) {
268: if (*cp == '!') {
269: /* negated matches are not supported here */
270: r = SSH_ERR_INVALID_ARGUMENT;
271: goto fail;
272: }
273: free(matching);
1.159 djm 274: if ((matching = match_filter_allowlist(all, cp)) == NULL) {
1.138 djm 275: r = SSH_ERR_ALLOC_FAIL;
276: goto fail;
277: }
278: if ((tmp = kex_names_cat(ret, matching)) == NULL) {
279: r = SSH_ERR_ALLOC_FAIL;
280: goto fail;
281: }
282: free(ret);
283: ret = tmp;
1.109 djm 284: }
1.138 djm 285: if (ret == NULL || *ret == '\0') {
286: /* An empty name-list is an error */
287: /* XXX better error code? */
288: r = SSH_ERR_INVALID_ARGUMENT;
289: goto fail;
1.109 djm 290: }
291:
1.138 djm 292: /* success */
293: *listp = ret;
294: ret = NULL;
295: r = 0;
296:
297: fail:
298: free(matching);
299: free(opatterns);
300: free(list);
301: free(ret);
302: return r;
1.86 djm 303: }
1.26 markus 304:
305: /* put algorithm proposal into buffer */
1.102 markus 306: int
307: kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1.1 markus 308: {
1.61 djm 309: u_int i;
1.102 markus 310: int r;
311:
312: sshbuf_reset(b);
1.26 markus 313:
1.49 markus 314: /*
315: * add a dummy cookie, the cookie will be overwritten by
316: * kex_send_kexinit(), each time a kexinit is set
317: */
1.102 markus 318: for (i = 0; i < KEX_COOKIE_LEN; i++) {
319: if ((r = sshbuf_put_u8(b, 0)) != 0)
320: return r;
321: }
322: for (i = 0; i < PROPOSAL_MAX; i++) {
323: if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
324: return r;
325: }
326: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */
327: (r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */
328: return r;
329: return 0;
1.1 markus 330: }
331:
1.26 markus 332: /* parse buffer and return algorithm proposal */
1.102 markus 333: int
334: kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
1.7 markus 335: {
1.102 markus 336: struct sshbuf *b = NULL;
337: u_char v;
1.78 djm 338: u_int i;
1.102 markus 339: char **proposal = NULL;
340: int r;
1.7 markus 341:
1.102 markus 342: *propp = NULL;
343: if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
344: return SSH_ERR_ALLOC_FAIL;
345: if ((b = sshbuf_fromb(raw)) == NULL) {
346: r = SSH_ERR_ALLOC_FAIL;
347: goto out;
348: }
1.152 djm 349: if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) { /* skip cookie */
1.160 djm 350: error_fr(r, "consume cookie");
1.102 markus 351: goto out;
1.152 djm 352: }
1.7 markus 353: /* extract kex init proposal strings */
354: for (i = 0; i < PROPOSAL_MAX; i++) {
1.152 djm 355: if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0) {
1.160 djm 356: error_fr(r, "parse proposal %u", i);
1.102 markus 357: goto out;
1.152 djm 358: }
1.110 djm 359: debug2("%s: %s", proposal_names[i], proposal[i]);
1.7 markus 360: }
1.26 markus 361: /* first kex follows / reserved */
1.115 djm 362: if ((r = sshbuf_get_u8(b, &v)) != 0 || /* first_kex_follows */
1.152 djm 363: (r = sshbuf_get_u32(b, &i)) != 0) { /* reserved */
1.160 djm 364: error_fr(r, "parse");
1.102 markus 365: goto out;
1.152 djm 366: }
1.53 markus 367: if (first_kex_follows != NULL)
1.115 djm 368: *first_kex_follows = v;
1.110 djm 369: debug2("first_kex_follows %d ", v);
370: debug2("reserved %u ", i);
1.102 markus 371: r = 0;
372: *propp = proposal;
373: out:
374: if (r != 0 && proposal != NULL)
375: kex_prop_free(proposal);
376: sshbuf_free(b);
377: return r;
1.1 markus 378: }
379:
1.102 markus 380: void
1.26 markus 381: kex_prop_free(char **proposal)
1.1 markus 382: {
1.61 djm 383: u_int i;
1.26 markus 384:
1.106 djm 385: if (proposal == NULL)
386: return;
1.26 markus 387: for (i = 0; i < PROPOSAL_MAX; i++)
1.91 djm 388: free(proposal[i]);
389: free(proposal);
1.1 markus 390: }
391:
1.78 djm 392: /* ARGSUSED */
1.167 djm 393: int
1.133 markus 394: kex_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
1.1 markus 395: {
1.112 djm 396: int r;
397:
398: error("kex protocol error: type %d seq %u", type, seq);
399: if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
400: (r = sshpkt_put_u32(ssh, seq)) != 0 ||
401: (r = sshpkt_send(ssh)) != 0)
402: return r;
1.101 markus 403: return 0;
1.26 markus 404: }
1.1 markus 405:
1.35 itojun 406: static void
1.102 markus 407: kex_reset_dispatch(struct ssh *ssh)
1.29 markus 408: {
1.102 markus 409: ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42 markus 410: SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.29 markus 411: }
412:
1.113 markus 413: static int
414: kex_send_ext_info(struct ssh *ssh)
415: {
416: int r;
1.121 djm 417: char *algs;
1.113 markus 418:
1.151 djm 419: debug("Sending SSH2_MSG_EXT_INFO");
1.130 djm 420: if ((algs = sshkey_alg_list(0, 1, 1, ',')) == NULL)
1.121 djm 421: return SSH_ERR_ALLOC_FAIL;
1.137 djm 422: /* XXX filter algs list by allowed pubkey/hostbased types */
1.113 markus 423: if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
1.170 djm 424: (r = sshpkt_put_u32(ssh, 2)) != 0 ||
1.113 markus 425: (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
1.121 djm 426: (r = sshpkt_put_cstring(ssh, algs)) != 0 ||
1.170 djm 427: (r = sshpkt_put_cstring(ssh,
428: "publickey-hostbound@openssh.com")) != 0 ||
429: (r = sshpkt_put_cstring(ssh, "0")) != 0 ||
1.152 djm 430: (r = sshpkt_send(ssh)) != 0) {
1.160 djm 431: error_fr(r, "compose");
1.121 djm 432: goto out;
1.152 djm 433: }
1.121 djm 434: /* success */
435: r = 0;
436: out:
437: free(algs);
1.123 djm 438: return r;
1.113 markus 439: }
440:
1.102 markus 441: int
442: kex_send_newkeys(struct ssh *ssh)
1.26 markus 443: {
1.102 markus 444: int r;
1.28 markus 445:
1.102 markus 446: kex_reset_dispatch(ssh);
447: if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
448: (r = sshpkt_send(ssh)) != 0)
449: return r;
1.26 markus 450: debug("SSH2_MSG_NEWKEYS sent");
1.102 markus 451: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.151 djm 452: if (ssh->kex->ext_info_c && (ssh->kex->flags & KEX_INITIAL) != 0)
1.113 markus 453: if ((r = kex_send_ext_info(ssh)) != 0)
454: return r;
1.151 djm 455: debug("expecting SSH2_MSG_NEWKEYS");
1.102 markus 456: return 0;
457: }
458:
1.113 markus 459: int
1.133 markus 460: kex_input_ext_info(int type, u_int32_t seq, struct ssh *ssh)
1.113 markus 461: {
462: struct kex *kex = ssh->kex;
463: u_int32_t i, ninfo;
1.137 djm 464: char *name;
1.134 djm 465: u_char *val;
466: size_t vlen;
1.113 markus 467: int r;
468:
469: debug("SSH2_MSG_EXT_INFO received");
470: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
471: if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
472: return r;
473: for (i = 0; i < ninfo; i++) {
474: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
475: return r;
1.134 djm 476: if ((r = sshpkt_get_string(ssh, &val, &vlen)) != 0) {
1.113 markus 477: free(name);
478: return r;
479: }
480: if (strcmp(name, "server-sig-algs") == 0) {
1.134 djm 481: /* Ensure no \0 lurking in value */
482: if (memchr(val, '\0', vlen) != NULL) {
1.160 djm 483: error_f("nul byte in %s", name);
1.134 djm 484: return SSH_ERR_INVALID_FORMAT;
485: }
1.160 djm 486: debug_f("%s=<%s>", name, val);
1.137 djm 487: kex->server_sig_algs = val;
488: val = NULL;
1.170 djm 489: } else if (strcmp(name,
490: "publickey-hostbound@openssh.com") == 0) {
491: /* XXX refactor */
492: /* Ensure no \0 lurking in value */
493: if (memchr(val, '\0', vlen) != NULL) {
494: error_f("nul byte in %s", name);
495: return SSH_ERR_INVALID_FORMAT;
496: }
497: debug_f("%s=<%s>", name, val);
498: if (strcmp(val, "0") == 0)
499: kex->flags |= KEX_HAS_PUBKEY_HOSTBOUND;
500: else {
501: debug_f("unsupported version of %s extension",
502: name);
503: }
1.134 djm 504: } else
1.160 djm 505: debug_f("%s (unrecognised)", name);
1.113 markus 506: free(name);
507: free(val);
508: }
509: return sshpkt_get_end(ssh);
510: }
511:
1.102 markus 512: static int
1.133 markus 513: kex_input_newkeys(int type, u_int32_t seq, struct ssh *ssh)
1.102 markus 514: {
515: struct kex *kex = ssh->kex;
516: int r;
1.19 stevesk 517:
1.26 markus 518: debug("SSH2_MSG_NEWKEYS received");
1.102 markus 519: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
1.131 markus 520: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.102 markus 521: if ((r = sshpkt_get_end(ssh)) != 0)
1.122 markus 522: return r;
523: if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0)
1.102 markus 524: return r;
1.30 markus 525: kex->done = 1;
1.142 djm 526: kex->flags &= ~KEX_INITIAL;
1.102 markus 527: sshbuf_reset(kex->peer);
528: /* sshbuf_reset(kex->my); */
1.26 markus 529: kex->flags &= ~KEX_INIT_SENT;
1.91 djm 530: free(kex->name);
1.32 markus 531: kex->name = NULL;
1.102 markus 532: return 0;
1.26 markus 533: }
1.1 markus 534:
1.102 markus 535: int
536: kex_send_kexinit(struct ssh *ssh)
1.26 markus 537: {
1.49 markus 538: u_char *cookie;
1.102 markus 539: struct kex *kex = ssh->kex;
540: int r;
1.49 markus 541:
1.152 djm 542: if (kex == NULL) {
1.161 djm 543: error_f("no kex");
1.102 markus 544: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 545: }
1.102 markus 546: if (kex->flags & KEX_INIT_SENT)
547: return 0;
1.30 markus 548: kex->done = 0;
1.49 markus 549:
550: /* generate a random cookie */
1.152 djm 551: if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) {
1.160 djm 552: error_f("bad kex length: %zu < %d",
1.152 djm 553: sshbuf_len(kex->my), KEX_COOKIE_LEN);
1.102 markus 554: return SSH_ERR_INVALID_FORMAT;
1.152 djm 555: }
556: if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) {
1.160 djm 557: error_f("buffer error");
1.102 markus 558: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 559: }
1.102 markus 560: arc4random_buf(cookie, KEX_COOKIE_LEN);
561:
562: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
563: (r = sshpkt_putb(ssh, kex->my)) != 0 ||
1.152 djm 564: (r = sshpkt_send(ssh)) != 0) {
1.160 djm 565: error_fr(r, "compose reply");
1.102 markus 566: return r;
1.152 djm 567: }
1.26 markus 568: debug("SSH2_MSG_KEXINIT sent");
569: kex->flags |= KEX_INIT_SENT;
1.102 markus 570: return 0;
1.1 markus 571: }
572:
1.78 djm 573: /* ARGSUSED */
1.101 markus 574: int
1.133 markus 575: kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
1.11 provos 576: {
1.102 markus 577: struct kex *kex = ssh->kex;
578: const u_char *ptr;
1.100 markus 579: u_int i;
580: size_t dlen;
1.102 markus 581: int r;
1.11 provos 582:
1.26 markus 583: debug("SSH2_MSG_KEXINIT received");
1.152 djm 584: if (kex == NULL) {
1.161 djm 585: error_f("no kex");
1.152 djm 586: return SSH_ERR_INTERNAL_ERROR;
587: }
1.127 markus 588: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
1.102 markus 589: ptr = sshpkt_ptr(ssh, &dlen);
590: if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
591: return r;
1.31 markus 592:
593: /* discard packet */
1.152 djm 594: for (i = 0; i < KEX_COOKIE_LEN; i++) {
595: if ((r = sshpkt_get_u8(ssh, NULL)) != 0) {
1.160 djm 596: error_fr(r, "discard cookie");
1.102 markus 597: return r;
1.152 djm 598: }
599: }
600: for (i = 0; i < PROPOSAL_MAX; i++) {
601: if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1.160 djm 602: error_fr(r, "discard proposal");
1.102 markus 603: return r;
1.152 djm 604: }
605: }
1.87 djm 606: /*
607: * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
608: * KEX method has the server move first, but a server might be using
609: * a custom method or one that we otherwise don't support. We should
610: * be prepared to remember first_kex_follows here so we can eat a
611: * packet later.
612: * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
613: * for cases where the server *doesn't* go first. I guess we should
614: * ignore it when it is set for these cases, which is what we do now.
615: */
1.102 markus 616: if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
617: (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
618: (r = sshpkt_get_end(ssh)) != 0)
619: return r;
620:
621: if (!(kex->flags & KEX_INIT_SENT))
622: if ((r = kex_send_kexinit(ssh)) != 0)
623: return r;
624: if ((r = kex_choose_conf(ssh)) != 0)
625: return r;
1.19 stevesk 626:
1.102 markus 627: if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
628: return (kex->kex[kex->kex_type])(ssh);
629:
1.160 djm 630: error_f("unknown kex type %u", kex->kex_type);
1.102 markus 631: return SSH_ERR_INTERNAL_ERROR;
632: }
633:
1.143 djm 634: struct kex *
635: kex_new(void)
1.102 markus 636: {
637: struct kex *kex;
638:
1.143 djm 639: if ((kex = calloc(1, sizeof(*kex))) == NULL ||
640: (kex->peer = sshbuf_new()) == NULL ||
641: (kex->my = sshbuf_new()) == NULL ||
642: (kex->client_version = sshbuf_new()) == NULL ||
1.165 djm 643: (kex->server_version = sshbuf_new()) == NULL ||
644: (kex->session_id = sshbuf_new()) == NULL) {
1.143 djm 645: kex_free(kex);
646: return NULL;
1.102 markus 647: }
1.143 djm 648: return kex;
1.26 markus 649: }
1.11 provos 650:
1.100 markus 651: void
652: kex_free_newkeys(struct newkeys *newkeys)
653: {
654: if (newkeys == NULL)
655: return;
656: if (newkeys->enc.key) {
657: explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
658: free(newkeys->enc.key);
659: newkeys->enc.key = NULL;
660: }
661: if (newkeys->enc.iv) {
1.111 djm 662: explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
1.100 markus 663: free(newkeys->enc.iv);
664: newkeys->enc.iv = NULL;
665: }
666: free(newkeys->enc.name);
667: explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
668: free(newkeys->comp.name);
669: explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
670: mac_clear(&newkeys->mac);
671: if (newkeys->mac.key) {
672: explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
673: free(newkeys->mac.key);
674: newkeys->mac.key = NULL;
675: }
676: free(newkeys->mac.name);
677: explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
1.157 jsg 678: freezero(newkeys, sizeof(*newkeys));
1.100 markus 679: }
680:
1.102 markus 681: void
682: kex_free(struct kex *kex)
1.26 markus 683: {
1.102 markus 684: u_int mode;
1.11 provos 685:
1.143 djm 686: if (kex == NULL)
687: return;
688:
1.102 markus 689: #ifdef WITH_OPENSSL
1.136 jsing 690: DH_free(kex->dh);
691: EC_KEY_free(kex->ec_client_key);
1.102 markus 692: #endif
693: for (mode = 0; mode < MODE_MAX; mode++) {
694: kex_free_newkeys(kex->newkeys[mode]);
695: kex->newkeys[mode] = NULL;
1.100 markus 696: }
1.102 markus 697: sshbuf_free(kex->peer);
698: sshbuf_free(kex->my);
1.143 djm 699: sshbuf_free(kex->client_version);
700: sshbuf_free(kex->server_version);
1.149 djm 701: sshbuf_free(kex->client_pub);
1.165 djm 702: sshbuf_free(kex->session_id);
1.169 djm 703: sshbuf_free(kex->initial_sig);
704: sshkey_free(kex->initial_hostkey);
1.107 djm 705: free(kex->failed_choice);
1.113 markus 706: free(kex->hostkey_alg);
707: free(kex->name);
1.102 markus 708: free(kex);
1.11 provos 709: }
710:
1.102 markus 711: int
1.143 djm 712: kex_ready(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
713: {
714: int r;
715:
716: if ((r = kex_prop2buf(ssh->kex->my, proposal)) != 0)
717: return r;
718: ssh->kex->flags = KEX_INITIAL;
719: kex_reset_dispatch(ssh);
720: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
721: return 0;
722: }
723:
724: int
1.102 markus 725: kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
1.1 markus 726: {
1.102 markus 727: int r;
1.1 markus 728:
1.143 djm 729: if ((r = kex_ready(ssh, proposal)) != 0)
1.102 markus 730: return r;
731: if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
732: kex_free(ssh->kex);
733: ssh->kex = NULL;
734: return r;
1.1 markus 735: }
1.102 markus 736: return 0;
1.117 djm 737: }
738:
739: /*
740: * Request key re-exchange, returns 0 on success or a ssherr.h error
741: * code otherwise. Must not be called if KEX is incomplete or in-progress.
742: */
743: int
744: kex_start_rekex(struct ssh *ssh)
745: {
746: if (ssh->kex == NULL) {
1.160 djm 747: error_f("no kex");
1.117 djm 748: return SSH_ERR_INTERNAL_ERROR;
749: }
750: if (ssh->kex->done == 0) {
1.160 djm 751: error_f("requested twice");
1.117 djm 752: return SSH_ERR_INTERNAL_ERROR;
753: }
754: ssh->kex->done = 0;
755: return kex_send_kexinit(ssh);
1.1 markus 756: }
757:
1.102 markus 758: static int
759: choose_enc(struct sshenc *enc, char *client, char *server)
1.1 markus 760: {
1.23 markus 761: char *name = match_list(client, server, NULL);
1.102 markus 762:
1.1 markus 763: if (name == NULL)
1.102 markus 764: return SSH_ERR_NO_CIPHER_ALG_MATCH;
1.129 dtucker 765: if ((enc->cipher = cipher_by_name(name)) == NULL) {
1.160 djm 766: error_f("unsupported cipher %s", name);
1.129 dtucker 767: free(name);
1.102 markus 768: return SSH_ERR_INTERNAL_ERROR;
1.129 dtucker 769: }
1.1 markus 770: enc->name = name;
771: enc->enabled = 0;
772: enc->iv = NULL;
1.88 markus 773: enc->iv_len = cipher_ivlen(enc->cipher);
1.1 markus 774: enc->key = NULL;
1.45 markus 775: enc->key_len = cipher_keylen(enc->cipher);
776: enc->block_size = cipher_blocksize(enc->cipher);
1.102 markus 777: return 0;
1.1 markus 778: }
1.69 deraadt 779:
1.102 markus 780: static int
781: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1 markus 782: {
1.23 markus 783: char *name = match_list(client, server, NULL);
1.102 markus 784:
1.1 markus 785: if (name == NULL)
1.102 markus 786: return SSH_ERR_NO_MAC_ALG_MATCH;
1.129 dtucker 787: if (mac_setup(mac, name) < 0) {
1.160 djm 788: error_f("unsupported MAC %s", name);
1.129 dtucker 789: free(name);
1.102 markus 790: return SSH_ERR_INTERNAL_ERROR;
1.129 dtucker 791: }
1.1 markus 792: mac->name = name;
793: mac->key = NULL;
794: mac->enabled = 0;
1.102 markus 795: return 0;
1.1 markus 796: }
1.69 deraadt 797:
1.102 markus 798: static int
799: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1 markus 800: {
1.23 markus 801: char *name = match_list(client, server, NULL);
1.102 markus 802:
1.1 markus 803: if (name == NULL)
1.102 markus 804: return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.156 dtucker 805: #ifdef WITH_ZLIB
1.64 markus 806: if (strcmp(name, "zlib@openssh.com") == 0) {
1.141 sf 807: comp->type = COMP_DELAYED;
808: } else if (strcmp(name, "zlib") == 0) {
1.140 sf 809: comp->type = COMP_ZLIB;
1.156 dtucker 810: } else
811: #endif /* WITH_ZLIB */
812: if (strcmp(name, "none") == 0) {
1.64 markus 813: comp->type = COMP_NONE;
1.1 markus 814: } else {
1.160 djm 815: error_f("unsupported compression scheme %s", name);
1.129 dtucker 816: free(name);
1.102 markus 817: return SSH_ERR_INTERNAL_ERROR;
1.1 markus 818: }
819: comp->name = name;
1.102 markus 820: return 0;
1.1 markus 821: }
1.69 deraadt 822:
1.102 markus 823: static int
824: choose_kex(struct kex *k, char *client, char *server)
1.1 markus 825: {
1.89 djm 826: const struct kexalg *kexalg;
827:
1.23 markus 828: k->name = match_list(client, server, NULL);
1.102 markus 829:
1.110 djm 830: debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1 markus 831: if (k->name == NULL)
1.102 markus 832: return SSH_ERR_NO_KEX_ALG_MATCH;
1.152 djm 833: if ((kexalg = kex_alg_by_name(k->name)) == NULL) {
1.160 djm 834: error_f("unsupported KEX method %s", k->name);
1.102 markus 835: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 836: }
1.89 djm 837: k->kex_type = kexalg->type;
1.94 djm 838: k->hash_alg = kexalg->hash_alg;
1.89 djm 839: k->ec_nid = kexalg->ec_nid;
1.102 markus 840: return 0;
1.1 markus 841: }
1.65 djm 842:
1.102 markus 843: static int
844: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1 markus 845: {
1.162 djm 846: free(k->hostkey_alg);
1.113 markus 847: k->hostkey_alg = match_list(client, server, NULL);
1.102 markus 848:
1.110 djm 849: debug("kex: host key algorithm: %s",
1.113 markus 850: k->hostkey_alg ? k->hostkey_alg : "(no match)");
851: if (k->hostkey_alg == NULL)
1.102 markus 852: return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113 markus 853: k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.152 djm 854: if (k->hostkey_type == KEY_UNSPEC) {
1.160 djm 855: error_f("unsupported hostkey algorithm %s", k->hostkey_alg);
1.102 markus 856: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 857: }
1.113 markus 858: k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102 markus 859: return 0;
1.1 markus 860: }
861:
1.56 djm 862: static int
1.53 markus 863: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
864: {
865: static int check[] = {
866: PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
867: };
868: int *idx;
869: char *p;
870:
871: for (idx = &check[0]; *idx != -1; idx++) {
872: if ((p = strchr(my[*idx], ',')) != NULL)
873: *p = '\0';
874: if ((p = strchr(peer[*idx], ',')) != NULL)
875: *p = '\0';
876: if (strcmp(my[*idx], peer[*idx]) != 0) {
877: debug2("proposal mismatch: my %s peer %s",
878: my[*idx], peer[*idx]);
879: return (0);
880: }
881: }
882: debug2("proposals match");
883: return (1);
884: }
885:
1.171 ! djm 886: /* returns non-zero if proposal contains any algorithm from algs */
! 887: static int
! 888: has_any_alg(const char *proposal, const char *algs)
! 889: {
! 890: char *cp;
! 891:
! 892: if ((cp = match_list(proposal, algs, NULL)) == NULL)
! 893: return 0;
! 894: free(cp);
! 895: return 1;
! 896: }
! 897:
1.102 markus 898: static int
899: kex_choose_conf(struct ssh *ssh)
1.1 markus 900: {
1.102 markus 901: struct kex *kex = ssh->kex;
902: struct newkeys *newkeys;
903: char **my = NULL, **peer = NULL;
1.26 markus 904: char **cprop, **sprop;
1.27 markus 905: int nenc, nmac, ncomp;
1.96 dtucker 906: u_int mode, ctos, need, dh_need, authlen;
1.102 markus 907: int r, first_kex_follows;
1.1 markus 908:
1.110 djm 909: debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
910: if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
911: goto out;
912: debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
913: if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102 markus 914: goto out;
1.26 markus 915:
1.27 markus 916: if (kex->server) {
1.26 markus 917: cprop=peer;
918: sprop=my;
919: } else {
920: cprop=my;
921: sprop=peer;
1.113 markus 922: }
923:
924: /* Check whether client supports ext_info_c */
1.142 djm 925: if (kex->server && (kex->flags & KEX_INITIAL)) {
1.113 markus 926: char *ext;
927:
928: ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
1.119 markus 929: kex->ext_info_c = (ext != NULL);
930: free(ext);
1.171 ! djm 931: }
! 932:
! 933: /* Check whether client supports rsa-sha2 algorithms */
! 934: if (kex->server && (kex->flags & KEX_INITIAL)) {
! 935: if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
! 936: "rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com"))
! 937: kex->flags |= KEX_RSA_SHA2_256_SUPPORTED;
! 938: if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
! 939: "rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com"))
! 940: kex->flags |= KEX_RSA_SHA2_512_SUPPORTED;
1.26 markus 941: }
1.1 markus 942:
1.30 markus 943: /* Algorithm Negotiation */
1.110 djm 944: if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
945: sprop[PROPOSAL_KEX_ALGS])) != 0) {
946: kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
947: peer[PROPOSAL_KEX_ALGS] = NULL;
948: goto out;
949: }
950: if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
951: sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
952: kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
953: peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
954: goto out;
955: }
1.1 markus 956: for (mode = 0; mode < MODE_MAX; mode++) {
1.102 markus 957: if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
958: r = SSH_ERR_ALLOC_FAIL;
959: goto out;
960: }
1.30 markus 961: kex->newkeys[mode] = newkeys;
1.78 djm 962: ctos = (!kex->server && mode == MODE_OUT) ||
963: (kex->server && mode == MODE_IN);
1.1 markus 964: nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
965: nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
966: ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102 markus 967: if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107 djm 968: sprop[nenc])) != 0) {
969: kex->failed_choice = peer[nenc];
970: peer[nenc] = NULL;
1.102 markus 971: goto out;
1.107 djm 972: }
1.102 markus 973: authlen = cipher_authlen(newkeys->enc.cipher);
1.88 markus 974: /* ignore mac for authenticated encryption */
1.102 markus 975: if (authlen == 0 &&
976: (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107 djm 977: sprop[nmac])) != 0) {
978: kex->failed_choice = peer[nmac];
979: peer[nmac] = NULL;
1.102 markus 980: goto out;
1.107 djm 981: }
1.102 markus 982: if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107 djm 983: sprop[ncomp])) != 0) {
984: kex->failed_choice = peer[ncomp];
985: peer[ncomp] = NULL;
1.102 markus 986: goto out;
1.107 djm 987: }
1.110 djm 988: debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1 markus 989: ctos ? "client->server" : "server->client",
1.27 markus 990: newkeys->enc.name,
1.88 markus 991: authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27 markus 992: newkeys->comp.name);
1.107 djm 993: }
1.96 dtucker 994: need = dh_need = 0;
1.1 markus 995: for (mode = 0; mode < MODE_MAX; mode++) {
1.30 markus 996: newkeys = kex->newkeys[mode];
1.120 deraadt 997: need = MAXIMUM(need, newkeys->enc.key_len);
998: need = MAXIMUM(need, newkeys->enc.block_size);
999: need = MAXIMUM(need, newkeys->enc.iv_len);
1000: need = MAXIMUM(need, newkeys->mac.key_len);
1001: dh_need = MAXIMUM(dh_need, cipher_seclen(newkeys->enc.cipher));
1002: dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
1003: dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
1004: dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
1.1 markus 1005: }
1.7 markus 1006: /* XXX need runden? */
1.27 markus 1007: kex->we_need = need;
1.96 dtucker 1008: kex->dh_need = dh_need;
1.53 markus 1009:
1010: /* ignore the next message if the proposals do not match */
1.135 djm 1011: if (first_kex_follows && !proposals_match(my, peer))
1.102 markus 1012: ssh->dispatch_skip_packets = 1;
1013: r = 0;
1014: out:
1.26 markus 1015: kex_prop_free(my);
1016: kex_prop_free(peer);
1.102 markus 1017: return r;
1.26 markus 1018: }
1019:
1.102 markus 1020: static int
1021: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
1022: const struct sshbuf *shared_secret, u_char **keyp)
1.26 markus 1023: {
1.102 markus 1024: struct kex *kex = ssh->kex;
1025: struct ssh_digest_ctx *hashctx = NULL;
1.26 markus 1026: char c = id;
1.61 djm 1027: u_int have;
1.94 djm 1028: size_t mdsz;
1.61 djm 1029: u_char *digest;
1.102 markus 1030: int r;
1.62 djm 1031:
1.94 djm 1032: if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102 markus 1033: return SSH_ERR_INVALID_ARGUMENT;
1.120 deraadt 1034: if ((digest = calloc(1, ROUNDUP(need, mdsz))) == NULL) {
1.102 markus 1035: r = SSH_ERR_ALLOC_FAIL;
1036: goto out;
1037: }
1.26 markus 1038:
1.30 markus 1039: /* K1 = HASH(K || H || "A" || session_id) */
1.102 markus 1040: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
1041: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 1042: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1043: ssh_digest_update(hashctx, &c, 1) != 0 ||
1.165 djm 1044: ssh_digest_update_buffer(hashctx, kex->session_id) != 0 ||
1.102 markus 1045: ssh_digest_final(hashctx, digest, mdsz) != 0) {
1046: r = SSH_ERR_LIBCRYPTO_ERROR;
1.160 djm 1047: error_f("KEX hash failed");
1.102 markus 1048: goto out;
1049: }
1.94 djm 1050: ssh_digest_free(hashctx);
1.102 markus 1051: hashctx = NULL;
1.26 markus 1052:
1.30 markus 1053: /*
1054: * expand key:
1055: * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
1056: * Key = K1 || K2 || ... || Kn
1057: */
1.26 markus 1058: for (have = mdsz; need > have; have += mdsz) {
1.102 markus 1059: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
1060: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 1061: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102 markus 1062: ssh_digest_update(hashctx, digest, have) != 0 ||
1063: ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
1.160 djm 1064: error_f("KDF failed");
1.102 markus 1065: r = SSH_ERR_LIBCRYPTO_ERROR;
1066: goto out;
1067: }
1.94 djm 1068: ssh_digest_free(hashctx);
1.102 markus 1069: hashctx = NULL;
1.26 markus 1070: }
1071: #ifdef DEBUG_KEX
1072: fprintf(stderr, "key '%c'== ", c);
1073: dump_digest("key", digest, need);
1074: #endif
1.102 markus 1075: *keyp = digest;
1076: digest = NULL;
1077: r = 0;
1078: out:
1.114 mmcc 1079: free(digest);
1.102 markus 1080: ssh_digest_free(hashctx);
1081: return r;
1.1 markus 1082: }
1083:
1.23 markus 1084: #define NKEYS 6
1.102 markus 1085: int
1086: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
1087: const struct sshbuf *shared_secret)
1.1 markus 1088: {
1.102 markus 1089: struct kex *kex = ssh->kex;
1.15 markus 1090: u_char *keys[NKEYS];
1.102 markus 1091: u_int i, j, mode, ctos;
1092: int r;
1.1 markus 1093:
1.144 djm 1094: /* save initial hash as session id */
1.165 djm 1095: if ((kex->flags & KEX_INITIAL) != 0) {
1.166 djm 1096: if (sshbuf_len(kex->session_id) != 0) {
1097: error_f("already have session ID at kex");
1098: return SSH_ERR_INTERNAL_ERROR;
1099: }
1.165 djm 1100: if ((r = sshbuf_put(kex->session_id, hash, hashlen)) != 0)
1101: return r;
1102: } else if (sshbuf_len(kex->session_id) == 0) {
1103: error_f("no session ID in rekex");
1.166 djm 1104: return SSH_ERR_INTERNAL_ERROR;
1.144 djm 1105: }
1.65 djm 1106: for (i = 0; i < NKEYS; i++) {
1.102 markus 1107: if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
1108: shared_secret, &keys[i])) != 0) {
1109: for (j = 0; j < i; j++)
1110: free(keys[j]);
1111: return r;
1112: }
1.65 djm 1113: }
1.1 markus 1114: for (mode = 0; mode < MODE_MAX; mode++) {
1.69 deraadt 1115: ctos = (!kex->server && mode == MODE_OUT) ||
1116: (kex->server && mode == MODE_IN);
1.100 markus 1117: kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1];
1118: kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
1119: kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1 markus 1120: }
1.102 markus 1121: return 0;
1.95 djm 1122: }
1.57 djm 1123:
1.145 djm 1124: int
1.150 djm 1125: kex_load_hostkey(struct ssh *ssh, struct sshkey **prvp, struct sshkey **pubp)
1.145 djm 1126: {
1127: struct kex *kex = ssh->kex;
1128:
1129: *pubp = NULL;
1130: *prvp = NULL;
1131: if (kex->load_host_public_key == NULL ||
1.152 djm 1132: kex->load_host_private_key == NULL) {
1.160 djm 1133: error_f("missing hostkey loader");
1.145 djm 1134: return SSH_ERR_INVALID_ARGUMENT;
1.152 djm 1135: }
1.145 djm 1136: *pubp = kex->load_host_public_key(kex->hostkey_type,
1137: kex->hostkey_nid, ssh);
1138: *prvp = kex->load_host_private_key(kex->hostkey_type,
1139: kex->hostkey_nid, ssh);
1140: if (*pubp == NULL)
1141: return SSH_ERR_NO_HOSTKEY_LOADED;
1.146 djm 1142: return 0;
1143: }
1144:
1145: int
1146: kex_verify_host_key(struct ssh *ssh, struct sshkey *server_host_key)
1147: {
1148: struct kex *kex = ssh->kex;
1149:
1.152 djm 1150: if (kex->verify_host_key == NULL) {
1.160 djm 1151: error_f("missing hostkey verifier");
1.146 djm 1152: return SSH_ERR_INVALID_ARGUMENT;
1.152 djm 1153: }
1.146 djm 1154: if (server_host_key->type != kex->hostkey_type ||
1155: (kex->hostkey_type == KEY_ECDSA &&
1156: server_host_key->ecdsa_nid != kex->hostkey_nid))
1157: return SSH_ERR_KEY_TYPE_MISMATCH;
1158: if (kex->verify_host_key(server_host_key, ssh) == -1)
1159: return SSH_ERR_SIGNATURE_INVALID;
1.145 djm 1160: return 0;
1161: }
1.26 markus 1162:
1.84 djm 1163: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26 markus 1164: void
1.147 djm 1165: dump_digest(const char *msg, const u_char *digest, int len)
1.26 markus 1166: {
1167: fprintf(stderr, "%s\n", msg);
1.102 markus 1168: sshbuf_dump_data(digest, len, stderr);
1.26 markus 1169: }
1170: #endif
1.143 djm 1171:
1172: /*
1173: * Send a plaintext error message to the peer, suffixed by \r\n.
1174: * Only used during banner exchange, and there only for the server.
1175: */
1176: static void
1177: send_error(struct ssh *ssh, char *msg)
1178: {
1179: char *crnl = "\r\n";
1180:
1181: if (!ssh->kex->server)
1182: return;
1183:
1184: if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1185: msg, strlen(msg)) != strlen(msg) ||
1186: atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1187: crnl, strlen(crnl)) != strlen(crnl))
1.160 djm 1188: error_f("write: %.100s", strerror(errno));
1.143 djm 1189: }
1190:
1191: /*
1192: * Sends our identification string and waits for the peer's. Will block for
1193: * up to timeout_ms (or indefinitely if timeout_ms <= 0).
1194: * Returns on 0 success or a ssherr.h code on failure.
1195: */
1196: int
1197: kex_exchange_identification(struct ssh *ssh, int timeout_ms,
1198: const char *version_addendum)
1199: {
1.158 djm 1200: int remote_major, remote_minor, mismatch, oerrno = 0;
1.143 djm 1201: size_t len, i, n;
1202: int r, expect_nl;
1203: u_char c;
1204: struct sshbuf *our_version = ssh->kex->server ?
1205: ssh->kex->server_version : ssh->kex->client_version;
1206: struct sshbuf *peer_version = ssh->kex->server ?
1207: ssh->kex->client_version : ssh->kex->server_version;
1208: char *our_version_string = NULL, *peer_version_string = NULL;
1209: char *cp, *remote_version = NULL;
1210:
1211: /* Prepare and send our banner */
1212: sshbuf_reset(our_version);
1213: if (version_addendum != NULL && *version_addendum == '\0')
1214: version_addendum = NULL;
1215: if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
1.168 djm 1216: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
1.143 djm 1217: version_addendum == NULL ? "" : " ",
1218: version_addendum == NULL ? "" : version_addendum)) != 0) {
1.158 djm 1219: oerrno = errno;
1.160 djm 1220: error_fr(r, "sshbuf_putf");
1.143 djm 1221: goto out;
1222: }
1223:
1224: if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1225: sshbuf_mutable_ptr(our_version),
1226: sshbuf_len(our_version)) != sshbuf_len(our_version)) {
1.158 djm 1227: oerrno = errno;
1.160 djm 1228: debug_f("write: %.100s", strerror(errno));
1.143 djm 1229: r = SSH_ERR_SYSTEM_ERROR;
1230: goto out;
1231: }
1232: if ((r = sshbuf_consume_end(our_version, 2)) != 0) { /* trim \r\n */
1.158 djm 1233: oerrno = errno;
1.160 djm 1234: error_fr(r, "sshbuf_consume_end");
1.143 djm 1235: goto out;
1236: }
1237: our_version_string = sshbuf_dup_string(our_version);
1238: if (our_version_string == NULL) {
1.160 djm 1239: error_f("sshbuf_dup_string failed");
1.143 djm 1240: r = SSH_ERR_ALLOC_FAIL;
1241: goto out;
1242: }
1243: debug("Local version string %.100s", our_version_string);
1244:
1245: /* Read other side's version identification. */
1246: for (n = 0; ; n++) {
1247: if (n >= SSH_MAX_PRE_BANNER_LINES) {
1248: send_error(ssh, "No SSH identification string "
1249: "received.");
1.160 djm 1250: error_f("No SSH version received in first %u lines "
1251: "from server", SSH_MAX_PRE_BANNER_LINES);
1.143 djm 1252: r = SSH_ERR_INVALID_FORMAT;
1253: goto out;
1254: }
1255: sshbuf_reset(peer_version);
1256: expect_nl = 0;
1257: for (i = 0; ; i++) {
1258: if (timeout_ms > 0) {
1259: r = waitrfd(ssh_packet_get_connection_in(ssh),
1260: &timeout_ms);
1261: if (r == -1 && errno == ETIMEDOUT) {
1262: send_error(ssh, "Timed out waiting "
1263: "for SSH identification string.");
1264: error("Connection timed out during "
1265: "banner exchange");
1266: r = SSH_ERR_CONN_TIMEOUT;
1267: goto out;
1268: } else if (r == -1) {
1.158 djm 1269: oerrno = errno;
1.160 djm 1270: error_f("%s", strerror(errno));
1.143 djm 1271: r = SSH_ERR_SYSTEM_ERROR;
1272: goto out;
1273: }
1274: }
1275:
1276: len = atomicio(read, ssh_packet_get_connection_in(ssh),
1277: &c, 1);
1278: if (len != 1 && errno == EPIPE) {
1.160 djm 1279: error_f("Connection closed by remote host");
1.143 djm 1280: r = SSH_ERR_CONN_CLOSED;
1281: goto out;
1282: } else if (len != 1) {
1.158 djm 1283: oerrno = errno;
1.160 djm 1284: error_f("read: %.100s", strerror(errno));
1.143 djm 1285: r = SSH_ERR_SYSTEM_ERROR;
1286: goto out;
1287: }
1288: if (c == '\r') {
1289: expect_nl = 1;
1290: continue;
1291: }
1292: if (c == '\n')
1293: break;
1294: if (c == '\0' || expect_nl) {
1.160 djm 1295: error_f("banner line contains invalid "
1296: "characters");
1.143 djm 1297: goto invalid;
1298: }
1299: if ((r = sshbuf_put_u8(peer_version, c)) != 0) {
1.158 djm 1300: oerrno = errno;
1.160 djm 1301: error_fr(r, "sshbuf_put");
1.143 djm 1302: goto out;
1303: }
1304: if (sshbuf_len(peer_version) > SSH_MAX_BANNER_LEN) {
1.160 djm 1305: error_f("banner line too long");
1.143 djm 1306: goto invalid;
1307: }
1308: }
1309: /* Is this an actual protocol banner? */
1310: if (sshbuf_len(peer_version) > 4 &&
1311: memcmp(sshbuf_ptr(peer_version), "SSH-", 4) == 0)
1312: break;
1313: /* If not, then just log the line and continue */
1314: if ((cp = sshbuf_dup_string(peer_version)) == NULL) {
1.160 djm 1315: error_f("sshbuf_dup_string failed");
1.143 djm 1316: r = SSH_ERR_ALLOC_FAIL;
1317: goto out;
1318: }
1319: /* Do not accept lines before the SSH ident from a client */
1320: if (ssh->kex->server) {
1.160 djm 1321: error_f("client sent invalid protocol identifier "
1322: "\"%.256s\"", cp);
1.143 djm 1323: free(cp);
1324: goto invalid;
1325: }
1.160 djm 1326: debug_f("banner line %zu: %s", n, cp);
1.143 djm 1327: free(cp);
1328: }
1329: peer_version_string = sshbuf_dup_string(peer_version);
1330: if (peer_version_string == NULL)
1.160 djm 1331: error_f("sshbuf_dup_string failed");
1.143 djm 1332: /* XXX must be same size for sscanf */
1333: if ((remote_version = calloc(1, sshbuf_len(peer_version))) == NULL) {
1.160 djm 1334: error_f("calloc failed");
1.143 djm 1335: r = SSH_ERR_ALLOC_FAIL;
1336: goto out;
1337: }
1338:
1339: /*
1340: * Check that the versions match. In future this might accept
1341: * several versions and set appropriate flags to handle them.
1342: */
1343: if (sscanf(peer_version_string, "SSH-%d.%d-%[^\n]\n",
1344: &remote_major, &remote_minor, remote_version) != 3) {
1345: error("Bad remote protocol version identification: '%.100s'",
1346: peer_version_string);
1347: invalid:
1348: send_error(ssh, "Invalid SSH identification string.");
1349: r = SSH_ERR_INVALID_FORMAT;
1350: goto out;
1351: }
1352: debug("Remote protocol version %d.%d, remote software version %.100s",
1353: remote_major, remote_minor, remote_version);
1.164 djm 1354: compat_banner(ssh, remote_version);
1.143 djm 1355:
1356: mismatch = 0;
1357: switch (remote_major) {
1358: case 2:
1359: break;
1360: case 1:
1361: if (remote_minor != 99)
1362: mismatch = 1;
1363: break;
1364: default:
1365: mismatch = 1;
1366: break;
1367: }
1368: if (mismatch) {
1369: error("Protocol major versions differ: %d vs. %d",
1370: PROTOCOL_MAJOR_2, remote_major);
1371: send_error(ssh, "Protocol major versions differ.");
1372: r = SSH_ERR_NO_PROTOCOL_VERSION;
1373: goto out;
1374: }
1375:
1376: if (ssh->kex->server && (ssh->compat & SSH_BUG_PROBE) != 0) {
1377: logit("probed from %s port %d with %s. Don't panic.",
1378: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1379: peer_version_string);
1380: r = SSH_ERR_CONN_CLOSED; /* XXX */
1381: goto out;
1382: }
1383: if (ssh->kex->server && (ssh->compat & SSH_BUG_SCANNER) != 0) {
1384: logit("scanned from %s port %d with %s. Don't panic.",
1385: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1386: peer_version_string);
1387: r = SSH_ERR_CONN_CLOSED; /* XXX */
1388: goto out;
1389: }
1390: if ((ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1391: logit("Remote version \"%.100s\" uses unsafe RSA signature "
1392: "scheme; disabling use of RSA keys", remote_version);
1393: }
1394: /* success */
1395: r = 0;
1396: out:
1397: free(our_version_string);
1398: free(peer_version_string);
1399: free(remote_version);
1.158 djm 1400: if (r == SSH_ERR_SYSTEM_ERROR)
1401: errno = oerrno;
1.143 djm 1402: return r;
1403: }
1404: