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