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