Annotation of src/usr.bin/ssh/kex.c, Revision 1.177
1.177 ! guenther 1: /* $OpenBSD: kex.c,v 1.176 2023/03/06 12:14:48 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.167 djm 449: int
1.133 markus 450: kex_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
1.1 markus 451: {
1.112 djm 452: int r;
453:
454: error("kex protocol error: type %d seq %u", type, seq);
455: if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
456: (r = sshpkt_put_u32(ssh, seq)) != 0 ||
457: (r = sshpkt_send(ssh)) != 0)
458: return r;
1.101 markus 459: return 0;
1.26 markus 460: }
1.1 markus 461:
1.35 itojun 462: static void
1.102 markus 463: kex_reset_dispatch(struct ssh *ssh)
1.29 markus 464: {
1.102 markus 465: ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42 markus 466: SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.29 markus 467: }
468:
1.113 markus 469: static int
470: kex_send_ext_info(struct ssh *ssh)
471: {
472: int r;
1.121 djm 473: char *algs;
1.113 markus 474:
1.151 djm 475: debug("Sending SSH2_MSG_EXT_INFO");
1.130 djm 476: if ((algs = sshkey_alg_list(0, 1, 1, ',')) == NULL)
1.121 djm 477: return SSH_ERR_ALLOC_FAIL;
1.137 djm 478: /* XXX filter algs list by allowed pubkey/hostbased types */
1.113 markus 479: if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
1.170 djm 480: (r = sshpkt_put_u32(ssh, 2)) != 0 ||
1.113 markus 481: (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
1.121 djm 482: (r = sshpkt_put_cstring(ssh, algs)) != 0 ||
1.170 djm 483: (r = sshpkt_put_cstring(ssh,
484: "publickey-hostbound@openssh.com")) != 0 ||
485: (r = sshpkt_put_cstring(ssh, "0")) != 0 ||
1.152 djm 486: (r = sshpkt_send(ssh)) != 0) {
1.160 djm 487: error_fr(r, "compose");
1.121 djm 488: goto out;
1.152 djm 489: }
1.121 djm 490: /* success */
491: r = 0;
492: out:
493: free(algs);
1.123 djm 494: return r;
1.113 markus 495: }
496:
1.102 markus 497: int
498: kex_send_newkeys(struct ssh *ssh)
1.26 markus 499: {
1.102 markus 500: int r;
1.28 markus 501:
1.102 markus 502: kex_reset_dispatch(ssh);
503: if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
504: (r = sshpkt_send(ssh)) != 0)
505: return r;
1.26 markus 506: debug("SSH2_MSG_NEWKEYS sent");
1.102 markus 507: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.151 djm 508: if (ssh->kex->ext_info_c && (ssh->kex->flags & KEX_INITIAL) != 0)
1.113 markus 509: if ((r = kex_send_ext_info(ssh)) != 0)
510: return r;
1.151 djm 511: debug("expecting SSH2_MSG_NEWKEYS");
1.102 markus 512: return 0;
513: }
514:
1.113 markus 515: int
1.133 markus 516: kex_input_ext_info(int type, u_int32_t seq, struct ssh *ssh)
1.113 markus 517: {
518: struct kex *kex = ssh->kex;
519: u_int32_t i, ninfo;
1.137 djm 520: char *name;
1.134 djm 521: u_char *val;
522: size_t vlen;
1.113 markus 523: int r;
524:
525: debug("SSH2_MSG_EXT_INFO received");
526: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
527: if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
528: return r;
529: for (i = 0; i < ninfo; i++) {
530: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
531: return r;
1.134 djm 532: if ((r = sshpkt_get_string(ssh, &val, &vlen)) != 0) {
1.113 markus 533: free(name);
534: return r;
535: }
536: if (strcmp(name, "server-sig-algs") == 0) {
1.134 djm 537: /* Ensure no \0 lurking in value */
538: if (memchr(val, '\0', vlen) != NULL) {
1.160 djm 539: error_f("nul byte in %s", name);
1.134 djm 540: return SSH_ERR_INVALID_FORMAT;
541: }
1.160 djm 542: debug_f("%s=<%s>", name, val);
1.137 djm 543: kex->server_sig_algs = val;
544: val = NULL;
1.170 djm 545: } else if (strcmp(name,
546: "publickey-hostbound@openssh.com") == 0) {
547: /* XXX refactor */
548: /* Ensure no \0 lurking in value */
549: if (memchr(val, '\0', vlen) != NULL) {
550: error_f("nul byte in %s", name);
551: return SSH_ERR_INVALID_FORMAT;
552: }
553: debug_f("%s=<%s>", name, val);
554: if (strcmp(val, "0") == 0)
555: kex->flags |= KEX_HAS_PUBKEY_HOSTBOUND;
556: else {
557: debug_f("unsupported version of %s extension",
558: name);
559: }
1.134 djm 560: } else
1.160 djm 561: debug_f("%s (unrecognised)", name);
1.113 markus 562: free(name);
563: free(val);
564: }
565: return sshpkt_get_end(ssh);
566: }
567:
1.102 markus 568: static int
1.133 markus 569: kex_input_newkeys(int type, u_int32_t seq, struct ssh *ssh)
1.102 markus 570: {
571: struct kex *kex = ssh->kex;
572: int r;
1.19 stevesk 573:
1.26 markus 574: debug("SSH2_MSG_NEWKEYS received");
1.102 markus 575: ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
1.131 markus 576: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.102 markus 577: if ((r = sshpkt_get_end(ssh)) != 0)
1.122 markus 578: return r;
579: if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0)
1.102 markus 580: return r;
1.30 markus 581: kex->done = 1;
1.142 djm 582: kex->flags &= ~KEX_INITIAL;
1.102 markus 583: sshbuf_reset(kex->peer);
584: /* sshbuf_reset(kex->my); */
1.26 markus 585: kex->flags &= ~KEX_INIT_SENT;
1.91 djm 586: free(kex->name);
1.32 markus 587: kex->name = NULL;
1.102 markus 588: return 0;
1.26 markus 589: }
1.1 markus 590:
1.102 markus 591: int
592: kex_send_kexinit(struct ssh *ssh)
1.26 markus 593: {
1.49 markus 594: u_char *cookie;
1.102 markus 595: struct kex *kex = ssh->kex;
596: int r;
1.49 markus 597:
1.152 djm 598: if (kex == NULL) {
1.161 djm 599: error_f("no kex");
1.102 markus 600: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 601: }
1.102 markus 602: if (kex->flags & KEX_INIT_SENT)
603: return 0;
1.30 markus 604: kex->done = 0;
1.49 markus 605:
606: /* generate a random cookie */
1.152 djm 607: if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) {
1.160 djm 608: error_f("bad kex length: %zu < %d",
1.152 djm 609: sshbuf_len(kex->my), KEX_COOKIE_LEN);
1.102 markus 610: return SSH_ERR_INVALID_FORMAT;
1.152 djm 611: }
612: if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) {
1.160 djm 613: error_f("buffer error");
1.102 markus 614: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 615: }
1.102 markus 616: arc4random_buf(cookie, KEX_COOKIE_LEN);
617:
618: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
619: (r = sshpkt_putb(ssh, kex->my)) != 0 ||
1.152 djm 620: (r = sshpkt_send(ssh)) != 0) {
1.160 djm 621: error_fr(r, "compose reply");
1.102 markus 622: return r;
1.152 djm 623: }
1.26 markus 624: debug("SSH2_MSG_KEXINIT sent");
625: kex->flags |= KEX_INIT_SENT;
1.102 markus 626: return 0;
1.1 markus 627: }
628:
1.101 markus 629: int
1.133 markus 630: kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
1.11 provos 631: {
1.102 markus 632: struct kex *kex = ssh->kex;
633: const u_char *ptr;
1.100 markus 634: u_int i;
635: size_t dlen;
1.102 markus 636: int r;
1.11 provos 637:
1.26 markus 638: debug("SSH2_MSG_KEXINIT received");
1.152 djm 639: if (kex == NULL) {
1.161 djm 640: error_f("no kex");
1.152 djm 641: return SSH_ERR_INTERNAL_ERROR;
642: }
1.127 markus 643: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
1.102 markus 644: ptr = sshpkt_ptr(ssh, &dlen);
645: if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
646: return r;
1.31 markus 647:
648: /* discard packet */
1.152 djm 649: for (i = 0; i < KEX_COOKIE_LEN; i++) {
650: if ((r = sshpkt_get_u8(ssh, NULL)) != 0) {
1.160 djm 651: error_fr(r, "discard cookie");
1.102 markus 652: return r;
1.152 djm 653: }
654: }
655: for (i = 0; i < PROPOSAL_MAX; i++) {
656: if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1.160 djm 657: error_fr(r, "discard proposal");
1.102 markus 658: return r;
1.152 djm 659: }
660: }
1.87 djm 661: /*
662: * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
663: * KEX method has the server move first, but a server might be using
664: * a custom method or one that we otherwise don't support. We should
665: * be prepared to remember first_kex_follows here so we can eat a
666: * packet later.
667: * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
668: * for cases where the server *doesn't* go first. I guess we should
669: * ignore it when it is set for these cases, which is what we do now.
670: */
1.102 markus 671: if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
672: (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
673: (r = sshpkt_get_end(ssh)) != 0)
674: return r;
675:
676: if (!(kex->flags & KEX_INIT_SENT))
677: if ((r = kex_send_kexinit(ssh)) != 0)
678: return r;
679: if ((r = kex_choose_conf(ssh)) != 0)
680: return r;
1.19 stevesk 681:
1.102 markus 682: if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
683: return (kex->kex[kex->kex_type])(ssh);
684:
1.160 djm 685: error_f("unknown kex type %u", kex->kex_type);
1.102 markus 686: return SSH_ERR_INTERNAL_ERROR;
687: }
688:
1.143 djm 689: struct kex *
690: kex_new(void)
1.102 markus 691: {
692: struct kex *kex;
693:
1.143 djm 694: if ((kex = calloc(1, sizeof(*kex))) == NULL ||
695: (kex->peer = sshbuf_new()) == NULL ||
696: (kex->my = sshbuf_new()) == NULL ||
697: (kex->client_version = sshbuf_new()) == NULL ||
1.165 djm 698: (kex->server_version = sshbuf_new()) == NULL ||
699: (kex->session_id = sshbuf_new()) == NULL) {
1.143 djm 700: kex_free(kex);
701: return NULL;
1.102 markus 702: }
1.143 djm 703: return kex;
1.26 markus 704: }
1.11 provos 705:
1.100 markus 706: void
707: kex_free_newkeys(struct newkeys *newkeys)
708: {
709: if (newkeys == NULL)
710: return;
711: if (newkeys->enc.key) {
712: explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
713: free(newkeys->enc.key);
714: newkeys->enc.key = NULL;
715: }
716: if (newkeys->enc.iv) {
1.111 djm 717: explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
1.100 markus 718: free(newkeys->enc.iv);
719: newkeys->enc.iv = NULL;
720: }
721: free(newkeys->enc.name);
722: explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
723: free(newkeys->comp.name);
724: explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
725: mac_clear(&newkeys->mac);
726: if (newkeys->mac.key) {
727: explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
728: free(newkeys->mac.key);
729: newkeys->mac.key = NULL;
730: }
731: free(newkeys->mac.name);
732: explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
1.157 jsg 733: freezero(newkeys, sizeof(*newkeys));
1.100 markus 734: }
735:
1.102 markus 736: void
737: kex_free(struct kex *kex)
1.26 markus 738: {
1.102 markus 739: u_int mode;
1.11 provos 740:
1.143 djm 741: if (kex == NULL)
742: return;
743:
1.102 markus 744: #ifdef WITH_OPENSSL
1.136 jsing 745: DH_free(kex->dh);
746: EC_KEY_free(kex->ec_client_key);
1.102 markus 747: #endif
748: for (mode = 0; mode < MODE_MAX; mode++) {
749: kex_free_newkeys(kex->newkeys[mode]);
750: kex->newkeys[mode] = NULL;
1.100 markus 751: }
1.102 markus 752: sshbuf_free(kex->peer);
753: sshbuf_free(kex->my);
1.143 djm 754: sshbuf_free(kex->client_version);
755: sshbuf_free(kex->server_version);
1.149 djm 756: sshbuf_free(kex->client_pub);
1.165 djm 757: sshbuf_free(kex->session_id);
1.169 djm 758: sshbuf_free(kex->initial_sig);
759: sshkey_free(kex->initial_hostkey);
1.107 djm 760: free(kex->failed_choice);
1.113 markus 761: free(kex->hostkey_alg);
762: free(kex->name);
1.102 markus 763: free(kex);
1.11 provos 764: }
765:
1.102 markus 766: int
1.143 djm 767: kex_ready(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
768: {
769: int r;
770:
771: if ((r = kex_prop2buf(ssh->kex->my, proposal)) != 0)
772: return r;
773: ssh->kex->flags = KEX_INITIAL;
774: kex_reset_dispatch(ssh);
775: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
776: return 0;
777: }
778:
779: int
1.102 markus 780: kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
1.1 markus 781: {
1.102 markus 782: int r;
1.1 markus 783:
1.143 djm 784: if ((r = kex_ready(ssh, proposal)) != 0)
1.102 markus 785: return r;
786: if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
787: kex_free(ssh->kex);
788: ssh->kex = NULL;
789: return r;
1.1 markus 790: }
1.102 markus 791: return 0;
1.117 djm 792: }
793:
794: /*
795: * Request key re-exchange, returns 0 on success or a ssherr.h error
796: * code otherwise. Must not be called if KEX is incomplete or in-progress.
797: */
798: int
799: kex_start_rekex(struct ssh *ssh)
800: {
801: if (ssh->kex == NULL) {
1.160 djm 802: error_f("no kex");
1.117 djm 803: return SSH_ERR_INTERNAL_ERROR;
804: }
805: if (ssh->kex->done == 0) {
1.160 djm 806: error_f("requested twice");
1.117 djm 807: return SSH_ERR_INTERNAL_ERROR;
808: }
809: ssh->kex->done = 0;
810: return kex_send_kexinit(ssh);
1.1 markus 811: }
812:
1.102 markus 813: static int
814: choose_enc(struct sshenc *enc, char *client, char *server)
1.1 markus 815: {
1.23 markus 816: char *name = match_list(client, server, NULL);
1.102 markus 817:
1.1 markus 818: if (name == NULL)
1.102 markus 819: return SSH_ERR_NO_CIPHER_ALG_MATCH;
1.129 dtucker 820: if ((enc->cipher = cipher_by_name(name)) == NULL) {
1.160 djm 821: error_f("unsupported cipher %s", name);
1.129 dtucker 822: free(name);
1.102 markus 823: return SSH_ERR_INTERNAL_ERROR;
1.129 dtucker 824: }
1.1 markus 825: enc->name = name;
826: enc->enabled = 0;
827: enc->iv = NULL;
1.88 markus 828: enc->iv_len = cipher_ivlen(enc->cipher);
1.1 markus 829: enc->key = NULL;
1.45 markus 830: enc->key_len = cipher_keylen(enc->cipher);
831: enc->block_size = cipher_blocksize(enc->cipher);
1.102 markus 832: return 0;
1.1 markus 833: }
1.69 deraadt 834:
1.102 markus 835: static int
836: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1 markus 837: {
1.23 markus 838: char *name = match_list(client, server, NULL);
1.102 markus 839:
1.1 markus 840: if (name == NULL)
1.102 markus 841: return SSH_ERR_NO_MAC_ALG_MATCH;
1.129 dtucker 842: if (mac_setup(mac, name) < 0) {
1.160 djm 843: error_f("unsupported MAC %s", name);
1.129 dtucker 844: free(name);
1.102 markus 845: return SSH_ERR_INTERNAL_ERROR;
1.129 dtucker 846: }
1.1 markus 847: mac->name = name;
848: mac->key = NULL;
849: mac->enabled = 0;
1.102 markus 850: return 0;
1.1 markus 851: }
1.69 deraadt 852:
1.102 markus 853: static int
854: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1 markus 855: {
1.23 markus 856: char *name = match_list(client, server, NULL);
1.102 markus 857:
1.1 markus 858: if (name == NULL)
1.102 markus 859: return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.156 dtucker 860: #ifdef WITH_ZLIB
1.64 markus 861: if (strcmp(name, "zlib@openssh.com") == 0) {
1.141 sf 862: comp->type = COMP_DELAYED;
863: } else if (strcmp(name, "zlib") == 0) {
1.140 sf 864: comp->type = COMP_ZLIB;
1.156 dtucker 865: } else
866: #endif /* WITH_ZLIB */
867: if (strcmp(name, "none") == 0) {
1.64 markus 868: comp->type = COMP_NONE;
1.1 markus 869: } else {
1.160 djm 870: error_f("unsupported compression scheme %s", name);
1.129 dtucker 871: free(name);
1.102 markus 872: return SSH_ERR_INTERNAL_ERROR;
1.1 markus 873: }
874: comp->name = name;
1.102 markus 875: return 0;
1.1 markus 876: }
1.69 deraadt 877:
1.102 markus 878: static int
879: choose_kex(struct kex *k, char *client, char *server)
1.1 markus 880: {
1.89 djm 881: const struct kexalg *kexalg;
882:
1.23 markus 883: k->name = match_list(client, server, NULL);
1.102 markus 884:
1.110 djm 885: debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1 markus 886: if (k->name == NULL)
1.102 markus 887: return SSH_ERR_NO_KEX_ALG_MATCH;
1.152 djm 888: if ((kexalg = kex_alg_by_name(k->name)) == NULL) {
1.160 djm 889: error_f("unsupported KEX method %s", k->name);
1.102 markus 890: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 891: }
1.89 djm 892: k->kex_type = kexalg->type;
1.94 djm 893: k->hash_alg = kexalg->hash_alg;
1.89 djm 894: k->ec_nid = kexalg->ec_nid;
1.102 markus 895: return 0;
1.1 markus 896: }
1.65 djm 897:
1.102 markus 898: static int
899: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1 markus 900: {
1.162 djm 901: free(k->hostkey_alg);
1.113 markus 902: k->hostkey_alg = match_list(client, server, NULL);
1.102 markus 903:
1.110 djm 904: debug("kex: host key algorithm: %s",
1.113 markus 905: k->hostkey_alg ? k->hostkey_alg : "(no match)");
906: if (k->hostkey_alg == NULL)
1.102 markus 907: return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113 markus 908: k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.152 djm 909: if (k->hostkey_type == KEY_UNSPEC) {
1.160 djm 910: error_f("unsupported hostkey algorithm %s", k->hostkey_alg);
1.102 markus 911: return SSH_ERR_INTERNAL_ERROR;
1.152 djm 912: }
1.113 markus 913: k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102 markus 914: return 0;
1.1 markus 915: }
916:
1.56 djm 917: static int
1.53 markus 918: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
919: {
920: static int check[] = {
921: PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
922: };
923: int *idx;
924: char *p;
925:
926: for (idx = &check[0]; *idx != -1; idx++) {
927: if ((p = strchr(my[*idx], ',')) != NULL)
928: *p = '\0';
929: if ((p = strchr(peer[*idx], ',')) != NULL)
930: *p = '\0';
931: if (strcmp(my[*idx], peer[*idx]) != 0) {
932: debug2("proposal mismatch: my %s peer %s",
933: my[*idx], peer[*idx]);
934: return (0);
935: }
936: }
937: debug2("proposals match");
938: return (1);
939: }
940:
1.171 djm 941: /* returns non-zero if proposal contains any algorithm from algs */
942: static int
943: has_any_alg(const char *proposal, const char *algs)
944: {
945: char *cp;
946:
947: if ((cp = match_list(proposal, algs, NULL)) == NULL)
948: return 0;
949: free(cp);
950: return 1;
951: }
952:
1.102 markus 953: static int
954: kex_choose_conf(struct ssh *ssh)
1.1 markus 955: {
1.102 markus 956: struct kex *kex = ssh->kex;
957: struct newkeys *newkeys;
958: char **my = NULL, **peer = NULL;
1.26 markus 959: char **cprop, **sprop;
1.27 markus 960: int nenc, nmac, ncomp;
1.96 dtucker 961: u_int mode, ctos, need, dh_need, authlen;
1.102 markus 962: int r, first_kex_follows;
1.1 markus 963:
1.110 djm 964: debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
965: if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
966: goto out;
967: debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
968: if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102 markus 969: goto out;
1.26 markus 970:
1.27 markus 971: if (kex->server) {
1.26 markus 972: cprop=peer;
973: sprop=my;
974: } else {
975: cprop=my;
976: sprop=peer;
1.113 markus 977: }
978:
979: /* Check whether client supports ext_info_c */
1.142 djm 980: if (kex->server && (kex->flags & KEX_INITIAL)) {
1.113 markus 981: char *ext;
982:
983: ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
1.119 markus 984: kex->ext_info_c = (ext != NULL);
985: free(ext);
1.171 djm 986: }
987:
988: /* Check whether client supports rsa-sha2 algorithms */
989: if (kex->server && (kex->flags & KEX_INITIAL)) {
990: if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
991: "rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com"))
992: kex->flags |= KEX_RSA_SHA2_256_SUPPORTED;
993: if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
994: "rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com"))
995: kex->flags |= KEX_RSA_SHA2_512_SUPPORTED;
1.26 markus 996: }
1.1 markus 997:
1.30 markus 998: /* Algorithm Negotiation */
1.110 djm 999: if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
1000: sprop[PROPOSAL_KEX_ALGS])) != 0) {
1001: kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
1002: peer[PROPOSAL_KEX_ALGS] = NULL;
1003: goto out;
1004: }
1005: if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
1006: sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
1007: kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
1008: peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
1009: goto out;
1010: }
1.1 markus 1011: for (mode = 0; mode < MODE_MAX; mode++) {
1.102 markus 1012: if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
1013: r = SSH_ERR_ALLOC_FAIL;
1014: goto out;
1015: }
1.30 markus 1016: kex->newkeys[mode] = newkeys;
1.78 djm 1017: ctos = (!kex->server && mode == MODE_OUT) ||
1018: (kex->server && mode == MODE_IN);
1.1 markus 1019: nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
1020: nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
1021: ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102 markus 1022: if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107 djm 1023: sprop[nenc])) != 0) {
1024: kex->failed_choice = peer[nenc];
1025: peer[nenc] = NULL;
1.102 markus 1026: goto out;
1.107 djm 1027: }
1.102 markus 1028: authlen = cipher_authlen(newkeys->enc.cipher);
1.88 markus 1029: /* ignore mac for authenticated encryption */
1.102 markus 1030: if (authlen == 0 &&
1031: (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107 djm 1032: sprop[nmac])) != 0) {
1033: kex->failed_choice = peer[nmac];
1034: peer[nmac] = NULL;
1.102 markus 1035: goto out;
1.107 djm 1036: }
1.102 markus 1037: if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107 djm 1038: sprop[ncomp])) != 0) {
1039: kex->failed_choice = peer[ncomp];
1040: peer[ncomp] = NULL;
1.102 markus 1041: goto out;
1.107 djm 1042: }
1.110 djm 1043: debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1 markus 1044: ctos ? "client->server" : "server->client",
1.27 markus 1045: newkeys->enc.name,
1.88 markus 1046: authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27 markus 1047: newkeys->comp.name);
1.107 djm 1048: }
1.96 dtucker 1049: need = dh_need = 0;
1.1 markus 1050: for (mode = 0; mode < MODE_MAX; mode++) {
1.30 markus 1051: newkeys = kex->newkeys[mode];
1.120 deraadt 1052: need = MAXIMUM(need, newkeys->enc.key_len);
1053: need = MAXIMUM(need, newkeys->enc.block_size);
1054: need = MAXIMUM(need, newkeys->enc.iv_len);
1055: need = MAXIMUM(need, newkeys->mac.key_len);
1056: dh_need = MAXIMUM(dh_need, cipher_seclen(newkeys->enc.cipher));
1057: dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
1058: dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
1059: dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
1.1 markus 1060: }
1.7 markus 1061: /* XXX need runden? */
1.27 markus 1062: kex->we_need = need;
1.96 dtucker 1063: kex->dh_need = dh_need;
1.53 markus 1064:
1065: /* ignore the next message if the proposals do not match */
1.135 djm 1066: if (first_kex_follows && !proposals_match(my, peer))
1.102 markus 1067: ssh->dispatch_skip_packets = 1;
1068: r = 0;
1069: out:
1.26 markus 1070: kex_prop_free(my);
1071: kex_prop_free(peer);
1.102 markus 1072: return r;
1.26 markus 1073: }
1074:
1.102 markus 1075: static int
1076: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
1077: const struct sshbuf *shared_secret, u_char **keyp)
1.26 markus 1078: {
1.102 markus 1079: struct kex *kex = ssh->kex;
1080: struct ssh_digest_ctx *hashctx = NULL;
1.26 markus 1081: char c = id;
1.61 djm 1082: u_int have;
1.94 djm 1083: size_t mdsz;
1.61 djm 1084: u_char *digest;
1.102 markus 1085: int r;
1.62 djm 1086:
1.94 djm 1087: if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102 markus 1088: return SSH_ERR_INVALID_ARGUMENT;
1.120 deraadt 1089: if ((digest = calloc(1, ROUNDUP(need, mdsz))) == NULL) {
1.102 markus 1090: r = SSH_ERR_ALLOC_FAIL;
1091: goto out;
1092: }
1.26 markus 1093:
1.30 markus 1094: /* K1 = HASH(K || H || "A" || session_id) */
1.102 markus 1095: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
1096: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 1097: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1098: ssh_digest_update(hashctx, &c, 1) != 0 ||
1.165 djm 1099: ssh_digest_update_buffer(hashctx, kex->session_id) != 0 ||
1.102 markus 1100: ssh_digest_final(hashctx, digest, mdsz) != 0) {
1101: r = SSH_ERR_LIBCRYPTO_ERROR;
1.160 djm 1102: error_f("KEX hash failed");
1.102 markus 1103: goto out;
1104: }
1.94 djm 1105: ssh_digest_free(hashctx);
1.102 markus 1106: hashctx = NULL;
1.26 markus 1107:
1.30 markus 1108: /*
1109: * expand key:
1110: * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
1111: * Key = K1 || K2 || ... || Kn
1112: */
1.26 markus 1113: for (have = mdsz; need > have; have += mdsz) {
1.102 markus 1114: if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
1115: ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94 djm 1116: ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102 markus 1117: ssh_digest_update(hashctx, digest, have) != 0 ||
1118: ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
1.160 djm 1119: error_f("KDF failed");
1.102 markus 1120: r = SSH_ERR_LIBCRYPTO_ERROR;
1121: goto out;
1122: }
1.94 djm 1123: ssh_digest_free(hashctx);
1.102 markus 1124: hashctx = NULL;
1.26 markus 1125: }
1126: #ifdef DEBUG_KEX
1127: fprintf(stderr, "key '%c'== ", c);
1128: dump_digest("key", digest, need);
1129: #endif
1.102 markus 1130: *keyp = digest;
1131: digest = NULL;
1132: r = 0;
1133: out:
1.114 mmcc 1134: free(digest);
1.102 markus 1135: ssh_digest_free(hashctx);
1136: return r;
1.1 markus 1137: }
1138:
1.23 markus 1139: #define NKEYS 6
1.102 markus 1140: int
1141: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
1142: const struct sshbuf *shared_secret)
1.1 markus 1143: {
1.102 markus 1144: struct kex *kex = ssh->kex;
1.15 markus 1145: u_char *keys[NKEYS];
1.102 markus 1146: u_int i, j, mode, ctos;
1147: int r;
1.1 markus 1148:
1.144 djm 1149: /* save initial hash as session id */
1.165 djm 1150: if ((kex->flags & KEX_INITIAL) != 0) {
1.166 djm 1151: if (sshbuf_len(kex->session_id) != 0) {
1152: error_f("already have session ID at kex");
1153: return SSH_ERR_INTERNAL_ERROR;
1154: }
1.165 djm 1155: if ((r = sshbuf_put(kex->session_id, hash, hashlen)) != 0)
1156: return r;
1157: } else if (sshbuf_len(kex->session_id) == 0) {
1158: error_f("no session ID in rekex");
1.166 djm 1159: return SSH_ERR_INTERNAL_ERROR;
1.144 djm 1160: }
1.65 djm 1161: for (i = 0; i < NKEYS; i++) {
1.102 markus 1162: if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
1163: shared_secret, &keys[i])) != 0) {
1164: for (j = 0; j < i; j++)
1165: free(keys[j]);
1166: return r;
1167: }
1.65 djm 1168: }
1.1 markus 1169: for (mode = 0; mode < MODE_MAX; mode++) {
1.69 deraadt 1170: ctos = (!kex->server && mode == MODE_OUT) ||
1171: (kex->server && mode == MODE_IN);
1.100 markus 1172: kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1];
1173: kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
1174: kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1 markus 1175: }
1.102 markus 1176: return 0;
1.95 djm 1177: }
1.57 djm 1178:
1.145 djm 1179: int
1.150 djm 1180: kex_load_hostkey(struct ssh *ssh, struct sshkey **prvp, struct sshkey **pubp)
1.145 djm 1181: {
1182: struct kex *kex = ssh->kex;
1183:
1184: *pubp = NULL;
1185: *prvp = NULL;
1186: if (kex->load_host_public_key == NULL ||
1.152 djm 1187: kex->load_host_private_key == NULL) {
1.160 djm 1188: error_f("missing hostkey loader");
1.145 djm 1189: return SSH_ERR_INVALID_ARGUMENT;
1.152 djm 1190: }
1.145 djm 1191: *pubp = kex->load_host_public_key(kex->hostkey_type,
1192: kex->hostkey_nid, ssh);
1193: *prvp = kex->load_host_private_key(kex->hostkey_type,
1194: kex->hostkey_nid, ssh);
1195: if (*pubp == NULL)
1196: return SSH_ERR_NO_HOSTKEY_LOADED;
1.146 djm 1197: return 0;
1198: }
1199:
1200: int
1201: kex_verify_host_key(struct ssh *ssh, struct sshkey *server_host_key)
1202: {
1203: struct kex *kex = ssh->kex;
1204:
1.152 djm 1205: if (kex->verify_host_key == NULL) {
1.160 djm 1206: error_f("missing hostkey verifier");
1.146 djm 1207: return SSH_ERR_INVALID_ARGUMENT;
1.152 djm 1208: }
1.146 djm 1209: if (server_host_key->type != kex->hostkey_type ||
1210: (kex->hostkey_type == KEY_ECDSA &&
1211: server_host_key->ecdsa_nid != kex->hostkey_nid))
1212: return SSH_ERR_KEY_TYPE_MISMATCH;
1213: if (kex->verify_host_key(server_host_key, ssh) == -1)
1214: return SSH_ERR_SIGNATURE_INVALID;
1.145 djm 1215: return 0;
1216: }
1.26 markus 1217:
1.84 djm 1218: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26 markus 1219: void
1.147 djm 1220: dump_digest(const char *msg, const u_char *digest, int len)
1.26 markus 1221: {
1222: fprintf(stderr, "%s\n", msg);
1.102 markus 1223: sshbuf_dump_data(digest, len, stderr);
1.26 markus 1224: }
1225: #endif
1.143 djm 1226:
1227: /*
1228: * Send a plaintext error message to the peer, suffixed by \r\n.
1229: * Only used during banner exchange, and there only for the server.
1230: */
1231: static void
1232: send_error(struct ssh *ssh, char *msg)
1233: {
1234: char *crnl = "\r\n";
1235:
1236: if (!ssh->kex->server)
1237: return;
1238:
1239: if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1240: msg, strlen(msg)) != strlen(msg) ||
1241: atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1242: crnl, strlen(crnl)) != strlen(crnl))
1.160 djm 1243: error_f("write: %.100s", strerror(errno));
1.143 djm 1244: }
1245:
1246: /*
1247: * Sends our identification string and waits for the peer's. Will block for
1248: * up to timeout_ms (or indefinitely if timeout_ms <= 0).
1249: * Returns on 0 success or a ssherr.h code on failure.
1250: */
1251: int
1252: kex_exchange_identification(struct ssh *ssh, int timeout_ms,
1253: const char *version_addendum)
1254: {
1.158 djm 1255: int remote_major, remote_minor, mismatch, oerrno = 0;
1.173 dtucker 1256: size_t len, n;
1.143 djm 1257: int r, expect_nl;
1258: u_char c;
1259: struct sshbuf *our_version = ssh->kex->server ?
1260: ssh->kex->server_version : ssh->kex->client_version;
1261: struct sshbuf *peer_version = ssh->kex->server ?
1262: ssh->kex->client_version : ssh->kex->server_version;
1263: char *our_version_string = NULL, *peer_version_string = NULL;
1264: char *cp, *remote_version = NULL;
1265:
1266: /* Prepare and send our banner */
1267: sshbuf_reset(our_version);
1268: if (version_addendum != NULL && *version_addendum == '\0')
1269: version_addendum = NULL;
1270: if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
1.168 djm 1271: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
1.143 djm 1272: version_addendum == NULL ? "" : " ",
1273: version_addendum == NULL ? "" : version_addendum)) != 0) {
1.158 djm 1274: oerrno = errno;
1.160 djm 1275: error_fr(r, "sshbuf_putf");
1.143 djm 1276: goto out;
1277: }
1278:
1279: if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
1280: sshbuf_mutable_ptr(our_version),
1281: sshbuf_len(our_version)) != sshbuf_len(our_version)) {
1.158 djm 1282: oerrno = errno;
1.160 djm 1283: debug_f("write: %.100s", strerror(errno));
1.143 djm 1284: r = SSH_ERR_SYSTEM_ERROR;
1285: goto out;
1286: }
1287: if ((r = sshbuf_consume_end(our_version, 2)) != 0) { /* trim \r\n */
1.158 djm 1288: oerrno = errno;
1.160 djm 1289: error_fr(r, "sshbuf_consume_end");
1.143 djm 1290: goto out;
1291: }
1292: our_version_string = sshbuf_dup_string(our_version);
1293: if (our_version_string == NULL) {
1.160 djm 1294: error_f("sshbuf_dup_string failed");
1.143 djm 1295: r = SSH_ERR_ALLOC_FAIL;
1296: goto out;
1297: }
1298: debug("Local version string %.100s", our_version_string);
1299:
1300: /* Read other side's version identification. */
1301: for (n = 0; ; n++) {
1302: if (n >= SSH_MAX_PRE_BANNER_LINES) {
1303: send_error(ssh, "No SSH identification string "
1304: "received.");
1.160 djm 1305: error_f("No SSH version received in first %u lines "
1306: "from server", SSH_MAX_PRE_BANNER_LINES);
1.143 djm 1307: r = SSH_ERR_INVALID_FORMAT;
1308: goto out;
1309: }
1310: sshbuf_reset(peer_version);
1311: expect_nl = 0;
1.173 dtucker 1312: for (;;) {
1.143 djm 1313: if (timeout_ms > 0) {
1314: r = waitrfd(ssh_packet_get_connection_in(ssh),
1315: &timeout_ms);
1316: if (r == -1 && errno == ETIMEDOUT) {
1317: send_error(ssh, "Timed out waiting "
1318: "for SSH identification string.");
1319: error("Connection timed out during "
1320: "banner exchange");
1321: r = SSH_ERR_CONN_TIMEOUT;
1322: goto out;
1323: } else if (r == -1) {
1.158 djm 1324: oerrno = errno;
1.160 djm 1325: error_f("%s", strerror(errno));
1.143 djm 1326: r = SSH_ERR_SYSTEM_ERROR;
1327: goto out;
1328: }
1329: }
1330:
1331: len = atomicio(read, ssh_packet_get_connection_in(ssh),
1332: &c, 1);
1333: if (len != 1 && errno == EPIPE) {
1.160 djm 1334: error_f("Connection closed by remote host");
1.143 djm 1335: r = SSH_ERR_CONN_CLOSED;
1336: goto out;
1337: } else if (len != 1) {
1.158 djm 1338: oerrno = errno;
1.160 djm 1339: error_f("read: %.100s", strerror(errno));
1.143 djm 1340: r = SSH_ERR_SYSTEM_ERROR;
1341: goto out;
1342: }
1343: if (c == '\r') {
1344: expect_nl = 1;
1345: continue;
1346: }
1347: if (c == '\n')
1348: break;
1349: if (c == '\0' || expect_nl) {
1.160 djm 1350: error_f("banner line contains invalid "
1351: "characters");
1.143 djm 1352: goto invalid;
1353: }
1354: if ((r = sshbuf_put_u8(peer_version, c)) != 0) {
1.158 djm 1355: oerrno = errno;
1.160 djm 1356: error_fr(r, "sshbuf_put");
1.143 djm 1357: goto out;
1358: }
1359: if (sshbuf_len(peer_version) > SSH_MAX_BANNER_LEN) {
1.160 djm 1360: error_f("banner line too long");
1.143 djm 1361: goto invalid;
1362: }
1363: }
1364: /* Is this an actual protocol banner? */
1365: if (sshbuf_len(peer_version) > 4 &&
1366: memcmp(sshbuf_ptr(peer_version), "SSH-", 4) == 0)
1367: break;
1368: /* If not, then just log the line and continue */
1369: if ((cp = sshbuf_dup_string(peer_version)) == NULL) {
1.160 djm 1370: error_f("sshbuf_dup_string failed");
1.143 djm 1371: r = SSH_ERR_ALLOC_FAIL;
1372: goto out;
1373: }
1374: /* Do not accept lines before the SSH ident from a client */
1375: if (ssh->kex->server) {
1.160 djm 1376: error_f("client sent invalid protocol identifier "
1377: "\"%.256s\"", cp);
1.143 djm 1378: free(cp);
1379: goto invalid;
1380: }
1.160 djm 1381: debug_f("banner line %zu: %s", n, cp);
1.143 djm 1382: free(cp);
1383: }
1384: peer_version_string = sshbuf_dup_string(peer_version);
1385: if (peer_version_string == NULL)
1.175 dtucker 1386: fatal_f("sshbuf_dup_string failed");
1.143 djm 1387: /* XXX must be same size for sscanf */
1388: if ((remote_version = calloc(1, sshbuf_len(peer_version))) == NULL) {
1.160 djm 1389: error_f("calloc failed");
1.143 djm 1390: r = SSH_ERR_ALLOC_FAIL;
1391: goto out;
1392: }
1393:
1394: /*
1395: * Check that the versions match. In future this might accept
1396: * several versions and set appropriate flags to handle them.
1397: */
1398: if (sscanf(peer_version_string, "SSH-%d.%d-%[^\n]\n",
1399: &remote_major, &remote_minor, remote_version) != 3) {
1400: error("Bad remote protocol version identification: '%.100s'",
1401: peer_version_string);
1402: invalid:
1403: send_error(ssh, "Invalid SSH identification string.");
1404: r = SSH_ERR_INVALID_FORMAT;
1405: goto out;
1406: }
1407: debug("Remote protocol version %d.%d, remote software version %.100s",
1408: remote_major, remote_minor, remote_version);
1.164 djm 1409: compat_banner(ssh, remote_version);
1.143 djm 1410:
1411: mismatch = 0;
1412: switch (remote_major) {
1413: case 2:
1414: break;
1415: case 1:
1416: if (remote_minor != 99)
1417: mismatch = 1;
1418: break;
1419: default:
1420: mismatch = 1;
1421: break;
1422: }
1423: if (mismatch) {
1424: error("Protocol major versions differ: %d vs. %d",
1425: PROTOCOL_MAJOR_2, remote_major);
1426: send_error(ssh, "Protocol major versions differ.");
1427: r = SSH_ERR_NO_PROTOCOL_VERSION;
1428: goto out;
1429: }
1430:
1431: if (ssh->kex->server && (ssh->compat & SSH_BUG_PROBE) != 0) {
1432: logit("probed from %s port %d with %s. Don't panic.",
1433: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1434: peer_version_string);
1435: r = SSH_ERR_CONN_CLOSED; /* XXX */
1436: goto out;
1437: }
1438: if (ssh->kex->server && (ssh->compat & SSH_BUG_SCANNER) != 0) {
1439: logit("scanned from %s port %d with %s. Don't panic.",
1440: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1441: peer_version_string);
1442: r = SSH_ERR_CONN_CLOSED; /* XXX */
1443: goto out;
1444: }
1445: /* success */
1446: r = 0;
1447: out:
1448: free(our_version_string);
1449: free(peer_version_string);
1450: free(remote_version);
1.158 djm 1451: if (r == SSH_ERR_SYSTEM_ERROR)
1452: errno = oerrno;
1.143 djm 1453: return r;
1454: }
1455: