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