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