djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: kex.c,v 1.109 2015/07/30 00:01:34 djm 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 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 28 | #include <sys/param.h> /* MAX roundup */ |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 29 | |
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> |
| 35 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 36 | #ifdef WITH_OPENSSL |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <openssl/crypto.h> |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 38 | #endif |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include "ssh2.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 41 | #include "packet.h" |
| 42 | #include "compat.h" |
| 43 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 44 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 46 | #include "log.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 47 | #include "mac.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 48 | #include "match.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 49 | #include "misc.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 50 | #include "dispatch.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 51 | #include "monitor.h" |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 52 | #include "roaming.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 53 | |
| 54 | #include "ssherr.h" |
| 55 | #include "sshbuf.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 56 | #include "digest.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 57 | |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 58 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 59 | # if defined(HAVE_EVP_SHA256) |
Damien Miller | af87af1 | 2006-03-15 13:02:28 +1100 | [diff] [blame] | 60 | # define evp_ssh_sha256 EVP_sha256 |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 61 | # else |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 62 | extern const EVP_MD *evp_ssh_sha256(void); |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 63 | # endif |
Tim Rice | 425a688 | 2006-03-15 20:17:05 -0800 | [diff] [blame] | 64 | #endif |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 65 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 66 | /* prototype */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 67 | static int kex_choose_conf(struct ssh *); |
| 68 | static int kex_input_newkeys(int, u_int32_t, void *); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 69 | |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 70 | struct kexalg { |
| 71 | char *name; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 72 | u_int type; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 73 | int ec_nid; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 74 | int hash_alg; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 75 | }; |
| 76 | static const struct kexalg kexalgs[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 77 | #ifdef WITH_OPENSSL |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 78 | { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 }, |
| 79 | { KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 }, |
| 80 | { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 }, |
Darren Tucker | a75d247 | 2013-05-10 18:11:55 +1000 | [diff] [blame] | 81 | #ifdef HAVE_EVP_SHA256 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 82 | { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 }, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 83 | #endif /* HAVE_EVP_SHA256 */ |
Darren Tucker | a75d247 | 2013-05-10 18:11:55 +1000 | [diff] [blame] | 84 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 85 | { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, |
| 86 | NID_X9_62_prime256v1, SSH_DIGEST_SHA256 }, |
| 87 | { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, |
| 88 | SSH_DIGEST_SHA384 }, |
Darren Tucker | 37bcef5 | 2013-11-09 18:39:25 +1100 | [diff] [blame] | 89 | # ifdef OPENSSL_HAS_NISTP521 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 90 | { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, |
| 91 | SSH_DIGEST_SHA512 }, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 92 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 93 | #endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 94 | #endif /* WITH_OPENSSL */ |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 95 | #if defined(HAVE_EVP_SHA256) || !defined(WITH_OPENSSL) |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 96 | { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 }, |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 97 | #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */ |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 98 | { NULL, -1, -1, -1}, |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | char * |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 102 | kex_alg_list(char sep) |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 103 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 104 | char *ret = NULL, *tmp; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 105 | size_t nlen, rlen = 0; |
| 106 | const struct kexalg *k; |
| 107 | |
| 108 | for (k = kexalgs; k->name != NULL; k++) { |
| 109 | if (ret != NULL) |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 110 | ret[rlen++] = sep; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 111 | nlen = strlen(k->name); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 112 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 113 | free(ret); |
| 114 | return NULL; |
| 115 | } |
| 116 | ret = tmp; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 117 | memcpy(ret + rlen, k->name, nlen + 1); |
| 118 | rlen += nlen; |
| 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static const struct kexalg * |
| 124 | kex_alg_by_name(const char *name) |
| 125 | { |
| 126 | const struct kexalg *k; |
| 127 | |
| 128 | for (k = kexalgs; k->name != NULL; k++) { |
| 129 | if (strcmp(k->name, name) == 0) |
| 130 | return k; |
| 131 | } |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 135 | /* Validate KEX method name list */ |
| 136 | int |
| 137 | kex_names_valid(const char *names) |
| 138 | { |
| 139 | char *s, *cp, *p; |
| 140 | |
| 141 | if (names == NULL || strcmp(names, "") == 0) |
| 142 | return 0; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 143 | if ((s = cp = strdup(names)) == NULL) |
| 144 | return 0; |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 145 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
| 146 | (p = strsep(&cp, ","))) { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 147 | if (kex_alg_by_name(p) == NULL) { |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 148 | error("Unsupported KEX algorithm \"%.100s\"", p); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 149 | free(s); |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | debug3("kex names ok: [%s]", names); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 154 | free(s); |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 155 | return 1; |
| 156 | } |
| 157 | |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame^] | 158 | /* |
| 159 | * Concatenate algorithm names, avoiding duplicates in the process. |
| 160 | * Caller must free returned string. |
| 161 | */ |
| 162 | char * |
| 163 | kex_names_cat(const char *a, const char *b) |
| 164 | { |
| 165 | char *ret = NULL, *tmp = NULL, *cp, *p; |
| 166 | size_t len; |
| 167 | |
| 168 | if (a == NULL || *a == '\0') |
| 169 | return NULL; |
| 170 | if (b == NULL || *b == '\0') |
| 171 | return strdup(a); |
| 172 | if (strlen(b) > 1024*1024) |
| 173 | return NULL; |
| 174 | len = strlen(a) + strlen(b) + 2; |
| 175 | if ((tmp = cp = strdup(b)) == NULL || |
| 176 | (ret = calloc(1, len)) == NULL) { |
| 177 | free(tmp); |
| 178 | return NULL; |
| 179 | } |
| 180 | strlcpy(ret, a, len); |
| 181 | for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) { |
| 182 | if (match_list(ret, p, NULL) != NULL) |
| 183 | continue; /* Algorithm already present */ |
| 184 | if (strlcat(ret, ",", len) >= len || |
| 185 | strlcat(ret, p, len) >= len) { |
| 186 | free(tmp); |
| 187 | free(ret); |
| 188 | return NULL; /* Shouldn't happen */ |
| 189 | } |
| 190 | } |
| 191 | free(tmp); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Assemble a list of algorithms from a default list and a string from a |
| 197 | * configuration file. The user-provided string may begin with '+' to |
| 198 | * indicate that it should be appended to the default. |
| 199 | */ |
| 200 | int |
| 201 | kex_assemble_names(const char *def, char **list) |
| 202 | { |
| 203 | char *ret; |
| 204 | |
| 205 | if (list == NULL || *list == NULL || **list == '\0') { |
| 206 | *list = strdup(def); |
| 207 | return 0; |
| 208 | } |
| 209 | if (**list != '+') { |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | if ((ret = kex_names_cat(def, *list + 1)) == NULL) |
| 214 | return SSH_ERR_ALLOC_FAIL; |
| 215 | free(*list); |
| 216 | *list = ret; |
| 217 | return 0; |
| 218 | } |
| 219 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 220 | /* put algorithm proposal into buffer */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 221 | int |
| 222 | kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX]) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 223 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 224 | u_int i; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 225 | int r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 226 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 227 | sshbuf_reset(b); |
| 228 | |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 229 | /* |
| 230 | * add a dummy cookie, the cookie will be overwritten by |
| 231 | * kex_send_kexinit(), each time a kexinit is set |
| 232 | */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 233 | for (i = 0; i < KEX_COOKIE_LEN; i++) { |
| 234 | if ((r = sshbuf_put_u8(b, 0)) != 0) |
| 235 | return r; |
| 236 | } |
| 237 | for (i = 0; i < PROPOSAL_MAX; i++) { |
| 238 | if ((r = sshbuf_put_cstring(b, proposal[i])) != 0) |
| 239 | return r; |
| 240 | } |
| 241 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */ |
| 242 | (r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */ |
| 243 | return r; |
| 244 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 245 | } |
| 246 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 247 | /* parse buffer and return algorithm proposal */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 248 | int |
| 249 | kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp) |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 250 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 251 | struct sshbuf *b = NULL; |
| 252 | u_char v; |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 253 | u_int i; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 254 | char **proposal = NULL; |
| 255 | int r; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 256 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 257 | *propp = NULL; |
| 258 | if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL) |
| 259 | return SSH_ERR_ALLOC_FAIL; |
| 260 | if ((b = sshbuf_fromb(raw)) == NULL) { |
| 261 | r = SSH_ERR_ALLOC_FAIL; |
| 262 | goto out; |
| 263 | } |
| 264 | if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */ |
| 265 | goto out; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 266 | /* extract kex init proposal strings */ |
| 267 | for (i = 0; i < PROPOSAL_MAX; i++) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 268 | if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0) |
| 269 | goto out; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 270 | debug2("kex_parse_kexinit: %s", proposal[i]); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 271 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 272 | /* first kex follows / reserved */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 273 | if ((r = sshbuf_get_u8(b, &v)) != 0 || |
| 274 | (r = sshbuf_get_u32(b, &i)) != 0) |
| 275 | goto out; |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 276 | if (first_kex_follows != NULL) |
| 277 | *first_kex_follows = i; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 278 | debug2("kex_parse_kexinit: first_kex_follows %d ", v); |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 279 | debug2("kex_parse_kexinit: reserved %u ", i); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 280 | r = 0; |
| 281 | *propp = proposal; |
| 282 | out: |
| 283 | if (r != 0 && proposal != NULL) |
| 284 | kex_prop_free(proposal); |
| 285 | sshbuf_free(b); |
| 286 | return r; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 287 | } |
| 288 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 289 | void |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 290 | kex_prop_free(char **proposal) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 291 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 292 | u_int i; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 293 | |
djm@openbsd.org | 44a8e7c | 2015-04-17 13:25:52 +0000 | [diff] [blame] | 294 | if (proposal == NULL) |
| 295 | return; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 296 | for (i = 0; i < PROPOSAL_MAX; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 297 | free(proposal[i]); |
| 298 | free(proposal); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 299 | } |
| 300 | |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 301 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 302 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 303 | kex_protocol_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 304 | { |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 305 | error("Hm, kex protocol error: type %d seq %u", type, seq); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 306 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 307 | } |
| 308 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 309 | static void |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 310 | kex_reset_dispatch(struct ssh *ssh) |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 311 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 312 | ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN, |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 313 | SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 314 | ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 315 | } |
| 316 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 317 | int |
| 318 | kex_send_newkeys(struct ssh *ssh) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 319 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 320 | int r; |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 321 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 322 | kex_reset_dispatch(ssh); |
| 323 | if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 || |
| 324 | (r = sshpkt_send(ssh)) != 0) |
| 325 | return r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 326 | debug("SSH2_MSG_NEWKEYS sent"); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 327 | debug("expecting SSH2_MSG_NEWKEYS"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 328 | ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys); |
| 329 | return 0; |
| 330 | } |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 331 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 332 | static int |
| 333 | kex_input_newkeys(int type, u_int32_t seq, void *ctxt) |
| 334 | { |
| 335 | struct ssh *ssh = ctxt; |
| 336 | struct kex *kex = ssh->kex; |
| 337 | int r; |
| 338 | |
| 339 | debug("SSH2_MSG_NEWKEYS received"); |
| 340 | ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); |
| 341 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 342 | return r; |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 343 | kex->done = 1; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 344 | sshbuf_reset(kex->peer); |
| 345 | /* sshbuf_reset(kex->my); */ |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 346 | kex->flags &= ~KEX_INIT_SENT; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 347 | free(kex->name); |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 348 | kex->name = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 349 | return 0; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 350 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 351 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 352 | int |
| 353 | kex_send_kexinit(struct ssh *ssh) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 354 | { |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 355 | u_char *cookie; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 356 | struct kex *kex = ssh->kex; |
| 357 | int r; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 358 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 359 | if (kex == NULL) |
| 360 | return SSH_ERR_INTERNAL_ERROR; |
| 361 | if (kex->flags & KEX_INIT_SENT) |
| 362 | return 0; |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 363 | kex->done = 0; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 364 | |
| 365 | /* generate a random cookie */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 366 | if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) |
| 367 | return SSH_ERR_INVALID_FORMAT; |
| 368 | if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) |
| 369 | return SSH_ERR_INTERNAL_ERROR; |
| 370 | arc4random_buf(cookie, KEX_COOKIE_LEN); |
| 371 | |
| 372 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 || |
| 373 | (r = sshpkt_putb(ssh, kex->my)) != 0 || |
| 374 | (r = sshpkt_send(ssh)) != 0) |
| 375 | return r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 376 | debug("SSH2_MSG_KEXINIT sent"); |
| 377 | kex->flags |= KEX_INIT_SENT; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 378 | return 0; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 381 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 382 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 383 | kex_input_kexinit(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 384 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 385 | struct ssh *ssh = ctxt; |
| 386 | struct kex *kex = ssh->kex; |
| 387 | const u_char *ptr; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 388 | u_int i; |
| 389 | size_t dlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 390 | int r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 391 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 392 | debug("SSH2_MSG_KEXINIT received"); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 393 | if (kex == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 394 | return SSH_ERR_INVALID_ARGUMENT; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 395 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 396 | ptr = sshpkt_ptr(ssh, &dlen); |
| 397 | if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0) |
| 398 | return r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 399 | |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 400 | /* discard packet */ |
| 401 | for (i = 0; i < KEX_COOKIE_LEN; i++) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 402 | if ((r = sshpkt_get_u8(ssh, NULL)) != 0) |
| 403 | return r; |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 404 | for (i = 0; i < PROPOSAL_MAX; i++) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 405 | if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0) |
| 406 | return r; |
Darren Tucker | ae608bd | 2012-09-06 21:19:51 +1000 | [diff] [blame] | 407 | /* |
| 408 | * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported |
| 409 | * KEX method has the server move first, but a server might be using |
| 410 | * a custom method or one that we otherwise don't support. We should |
| 411 | * be prepared to remember first_kex_follows here so we can eat a |
| 412 | * packet later. |
| 413 | * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means |
| 414 | * for cases where the server *doesn't* go first. I guess we should |
| 415 | * ignore it when it is set for these cases, which is what we do now. |
| 416 | */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 417 | if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */ |
| 418 | (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */ |
| 419 | (r = sshpkt_get_end(ssh)) != 0) |
| 420 | return r; |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 421 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 422 | if (!(kex->flags & KEX_INIT_SENT)) |
| 423 | if ((r = kex_send_kexinit(ssh)) != 0) |
| 424 | return r; |
| 425 | if ((r = kex_choose_conf(ssh)) != 0) |
| 426 | return r; |
| 427 | |
| 428 | if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL) |
| 429 | return (kex->kex[kex->kex_type])(ssh); |
| 430 | |
| 431 | return SSH_ERR_INTERNAL_ERROR; |
| 432 | } |
| 433 | |
| 434 | int |
| 435 | kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp) |
| 436 | { |
| 437 | struct kex *kex; |
| 438 | int r; |
| 439 | |
| 440 | *kexp = NULL; |
| 441 | if ((kex = calloc(1, sizeof(*kex))) == NULL) |
| 442 | return SSH_ERR_ALLOC_FAIL; |
| 443 | if ((kex->peer = sshbuf_new()) == NULL || |
| 444 | (kex->my = sshbuf_new()) == NULL) { |
| 445 | r = SSH_ERR_ALLOC_FAIL; |
| 446 | goto out; |
| 447 | } |
| 448 | if ((r = kex_prop2buf(kex->my, proposal)) != 0) |
| 449 | goto out; |
| 450 | kex->done = 0; |
| 451 | kex_reset_dispatch(ssh); |
| 452 | r = 0; |
| 453 | *kexp = kex; |
| 454 | out: |
| 455 | if (r != 0) |
| 456 | kex_free(kex); |
| 457 | return r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 458 | } |
| 459 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 460 | void |
| 461 | kex_free_newkeys(struct newkeys *newkeys) |
| 462 | { |
| 463 | if (newkeys == NULL) |
| 464 | return; |
| 465 | if (newkeys->enc.key) { |
| 466 | explicit_bzero(newkeys->enc.key, newkeys->enc.key_len); |
| 467 | free(newkeys->enc.key); |
| 468 | newkeys->enc.key = NULL; |
| 469 | } |
| 470 | if (newkeys->enc.iv) { |
| 471 | explicit_bzero(newkeys->enc.iv, newkeys->enc.block_size); |
| 472 | free(newkeys->enc.iv); |
| 473 | newkeys->enc.iv = NULL; |
| 474 | } |
| 475 | free(newkeys->enc.name); |
| 476 | explicit_bzero(&newkeys->enc, sizeof(newkeys->enc)); |
| 477 | free(newkeys->comp.name); |
| 478 | explicit_bzero(&newkeys->comp, sizeof(newkeys->comp)); |
| 479 | mac_clear(&newkeys->mac); |
| 480 | if (newkeys->mac.key) { |
| 481 | explicit_bzero(newkeys->mac.key, newkeys->mac.key_len); |
| 482 | free(newkeys->mac.key); |
| 483 | newkeys->mac.key = NULL; |
| 484 | } |
| 485 | free(newkeys->mac.name); |
| 486 | explicit_bzero(&newkeys->mac, sizeof(newkeys->mac)); |
| 487 | explicit_bzero(newkeys, sizeof(*newkeys)); |
| 488 | free(newkeys); |
| 489 | } |
| 490 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 491 | void |
| 492 | kex_free(struct kex *kex) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 493 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 494 | u_int mode; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 495 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 496 | #ifdef WITH_OPENSSL |
| 497 | if (kex->dh) |
| 498 | DH_free(kex->dh); |
Damien Miller | 4df590c | 2015-03-11 10:02:39 +1100 | [diff] [blame] | 499 | #ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 500 | if (kex->ec_client_key) |
| 501 | EC_KEY_free(kex->ec_client_key); |
Damien Miller | 4df590c | 2015-03-11 10:02:39 +1100 | [diff] [blame] | 502 | #endif /* OPENSSL_HAS_ECC */ |
| 503 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 504 | for (mode = 0; mode < MODE_MAX; mode++) { |
| 505 | kex_free_newkeys(kex->newkeys[mode]); |
| 506 | kex->newkeys[mode] = NULL; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 507 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 508 | sshbuf_free(kex->peer); |
| 509 | sshbuf_free(kex->my); |
| 510 | free(kex->session_id); |
| 511 | free(kex->client_version_string); |
| 512 | free(kex->server_version_string); |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 513 | free(kex->failed_choice); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 514 | free(kex); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 515 | } |
| 516 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 517 | int |
| 518 | kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX]) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 519 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 520 | int r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 521 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 522 | if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0) |
| 523 | return r; |
| 524 | if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */ |
| 525 | kex_free(ssh->kex); |
| 526 | ssh->kex = NULL; |
| 527 | return r; |
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 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 530 | } |
| 531 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 532 | static int |
| 533 | choose_enc(struct sshenc *enc, char *client, char *server) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 534 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 535 | char *name = match_list(client, server, NULL); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 536 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 537 | if (name == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 538 | return SSH_ERR_NO_CIPHER_ALG_MATCH; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 539 | if ((enc->cipher = cipher_by_name(name)) == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 540 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 541 | enc->name = name; |
| 542 | enc->enabled = 0; |
| 543 | enc->iv = NULL; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 544 | enc->iv_len = cipher_ivlen(enc->cipher); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 545 | enc->key = NULL; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 546 | enc->key_len = cipher_keylen(enc->cipher); |
| 547 | enc->block_size = cipher_blocksize(enc->cipher); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 548 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 549 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 550 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 551 | static int |
| 552 | choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 553 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 554 | char *name = match_list(client, server, NULL); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 555 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 556 | if (name == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 557 | return SSH_ERR_NO_MAC_ALG_MATCH; |
Darren Tucker | 5f3d5be | 2007-06-05 18:30:18 +1000 | [diff] [blame] | 558 | if (mac_setup(mac, name) < 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 559 | return SSH_ERR_INTERNAL_ERROR; |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 560 | /* truncate the key */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 561 | if (ssh->compat & SSH_BUG_HMAC) |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 562 | mac->key_len = 16; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 563 | mac->name = name; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 564 | mac->key = NULL; |
| 565 | mac->enabled = 0; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 566 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 567 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 568 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 569 | static int |
| 570 | choose_comp(struct sshcomp *comp, char *client, char *server) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 571 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 572 | char *name = match_list(client, server, NULL); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 573 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 574 | if (name == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 575 | return SSH_ERR_NO_COMPRESS_ALG_MATCH; |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 576 | if (strcmp(name, "zlib@openssh.com") == 0) { |
| 577 | comp->type = COMP_DELAYED; |
| 578 | } else if (strcmp(name, "zlib") == 0) { |
| 579 | comp->type = COMP_ZLIB; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 580 | } else if (strcmp(name, "none") == 0) { |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 581 | comp->type = COMP_NONE; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 582 | } else { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 583 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 584 | } |
| 585 | comp->name = name; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 586 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 587 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 588 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 589 | static int |
| 590 | choose_kex(struct kex *k, char *client, char *server) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 591 | { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 592 | const struct kexalg *kexalg; |
| 593 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 594 | k->name = match_list(client, server, NULL); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 595 | |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 596 | if (k->name == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 597 | return SSH_ERR_NO_KEX_ALG_MATCH; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 598 | if ((kexalg = kex_alg_by_name(k->name)) == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 599 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 600 | k->kex_type = kexalg->type; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 601 | k->hash_alg = kexalg->hash_alg; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 602 | k->ec_nid = kexalg->ec_nid; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 603 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 604 | } |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 605 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 606 | static int |
| 607 | choose_hostkeyalg(struct kex *k, char *client, char *server) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 608 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 609 | char *hostkeyalg = match_list(client, server, NULL); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 610 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 611 | if (hostkeyalg == NULL) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 612 | return SSH_ERR_NO_HOSTKEY_ALG_MATCH; |
| 613 | k->hostkey_type = sshkey_type_from_name(hostkeyalg); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 614 | if (k->hostkey_type == KEY_UNSPEC) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 615 | return SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 616 | k->hostkey_nid = sshkey_ecdsa_nid_from_name(hostkeyalg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 617 | free(hostkeyalg); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 618 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 619 | } |
| 620 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 621 | static int |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 622 | proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) |
| 623 | { |
| 624 | static int check[] = { |
| 625 | PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 |
| 626 | }; |
| 627 | int *idx; |
| 628 | char *p; |
| 629 | |
| 630 | for (idx = &check[0]; *idx != -1; idx++) { |
| 631 | if ((p = strchr(my[*idx], ',')) != NULL) |
| 632 | *p = '\0'; |
| 633 | if ((p = strchr(peer[*idx], ',')) != NULL) |
| 634 | *p = '\0'; |
| 635 | if (strcmp(my[*idx], peer[*idx]) != 0) { |
| 636 | debug2("proposal mismatch: my %s peer %s", |
| 637 | my[*idx], peer[*idx]); |
| 638 | return (0); |
| 639 | } |
| 640 | } |
| 641 | debug2("proposals match"); |
| 642 | return (1); |
| 643 | } |
| 644 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 645 | static int |
| 646 | kex_choose_conf(struct ssh *ssh) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 647 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 648 | struct kex *kex = ssh->kex; |
| 649 | struct newkeys *newkeys; |
| 650 | char **my = NULL, **peer = NULL; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 651 | char **cprop, **sprop; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 652 | int nenc, nmac, ncomp; |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 653 | u_int mode, ctos, need, dh_need, authlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 654 | int r, first_kex_follows; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 655 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 656 | if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0 || |
| 657 | (r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0) |
| 658 | goto out; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 659 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 660 | if (kex->server) { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 661 | cprop=peer; |
| 662 | sprop=my; |
| 663 | } else { |
| 664 | cprop=my; |
| 665 | sprop=peer; |
| 666 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 667 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 668 | /* Check whether server offers roaming */ |
| 669 | if (!kex->server) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 670 | char *roaming = match_list(KEX_RESUME, |
| 671 | peer[PROPOSAL_KEX_ALGS], NULL); |
| 672 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 673 | if (roaming) { |
| 674 | kex->roaming = 1; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 675 | free(roaming); |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 679 | /* Algorithm Negotiation */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 680 | for (mode = 0; mode < MODE_MAX; mode++) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 681 | if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) { |
| 682 | r = SSH_ERR_ALLOC_FAIL; |
| 683 | goto out; |
| 684 | } |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 685 | kex->newkeys[mode] = newkeys; |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 686 | ctos = (!kex->server && mode == MODE_OUT) || |
| 687 | (kex->server && mode == MODE_IN); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 688 | nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; |
| 689 | nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; |
| 690 | ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 691 | if ((r = choose_enc(&newkeys->enc, cprop[nenc], |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 692 | sprop[nenc])) != 0) { |
| 693 | kex->failed_choice = peer[nenc]; |
| 694 | peer[nenc] = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 695 | goto out; |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 696 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 697 | authlen = cipher_authlen(newkeys->enc.cipher); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 698 | /* ignore mac for authenticated encryption */ |
| 699 | if (authlen == 0 && |
| 700 | (r = choose_mac(ssh, &newkeys->mac, cprop[nmac], |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 701 | sprop[nmac])) != 0) { |
| 702 | kex->failed_choice = peer[nmac]; |
| 703 | peer[nmac] = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 704 | goto out; |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 705 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 706 | if ((r = choose_comp(&newkeys->comp, cprop[ncomp], |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 707 | sprop[ncomp])) != 0) { |
| 708 | kex->failed_choice = peer[ncomp]; |
| 709 | peer[ncomp] = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 710 | goto out; |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 711 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 712 | debug("kex: %s %s %s %s", |
| 713 | ctos ? "client->server" : "server->client", |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 714 | newkeys->enc.name, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 715 | authlen == 0 ? newkeys->mac.name : "<implicit>", |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 716 | newkeys->comp.name); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 717 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 718 | if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 719 | sprop[PROPOSAL_KEX_ALGS])) != 0) { |
| 720 | kex->failed_choice = peer[PROPOSAL_KEX_ALGS]; |
| 721 | peer[PROPOSAL_KEX_ALGS] = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 722 | goto out; |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 723 | } |
| 724 | if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], |
| 725 | sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) { |
djm@openbsd.org | 5cefe76 | 2015-07-29 08:34:54 +0000 | [diff] [blame] | 726 | kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS]; |
| 727 | peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL; |
djm@openbsd.org | f319912 | 2015-07-29 04:43:06 +0000 | [diff] [blame] | 728 | goto out; |
| 729 | } |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 730 | need = dh_need = 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 731 | for (mode = 0; mode < MODE_MAX; mode++) { |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 732 | newkeys = kex->newkeys[mode]; |
Damien Miller | a92ac74 | 2014-01-26 09:38:03 +1100 | [diff] [blame] | 733 | need = MAX(need, newkeys->enc.key_len); |
| 734 | need = MAX(need, newkeys->enc.block_size); |
| 735 | need = MAX(need, newkeys->enc.iv_len); |
| 736 | need = MAX(need, newkeys->mac.key_len); |
| 737 | dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher)); |
| 738 | dh_need = MAX(dh_need, newkeys->enc.block_size); |
| 739 | dh_need = MAX(dh_need, newkeys->enc.iv_len); |
| 740 | dh_need = MAX(dh_need, newkeys->mac.key_len); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 741 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 742 | /* XXX need runden? */ |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 743 | kex->we_need = need; |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 744 | kex->dh_need = dh_need; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 745 | |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 746 | /* ignore the next message if the proposals do not match */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 747 | if (first_kex_follows && !proposals_match(my, peer) && |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 748 | !(ssh->compat & SSH_BUG_FIRSTKEX)) |
| 749 | ssh->dispatch_skip_packets = 1; |
| 750 | r = 0; |
| 751 | out: |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 752 | kex_prop_free(my); |
| 753 | kex_prop_free(peer); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 754 | return r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 755 | } |
| 756 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 757 | static int |
| 758 | derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen, |
| 759 | const struct sshbuf *shared_secret, u_char **keyp) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 760 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 761 | struct kex *kex = ssh->kex; |
| 762 | struct ssh_digest_ctx *hashctx = NULL; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 763 | char c = id; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 764 | u_int have; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 765 | size_t mdsz; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 766 | u_char *digest; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 767 | int r; |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 768 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 769 | if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 770 | return SSH_ERR_INVALID_ARGUMENT; |
| 771 | if ((digest = calloc(1, roundup(need, mdsz))) == NULL) { |
| 772 | r = SSH_ERR_ALLOC_FAIL; |
| 773 | goto out; |
| 774 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 775 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 776 | /* K1 = HASH(K || H || "A" || session_id) */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 777 | if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL || |
| 778 | ssh_digest_update_buffer(hashctx, shared_secret) != 0 || |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 779 | ssh_digest_update(hashctx, hash, hashlen) != 0 || |
| 780 | ssh_digest_update(hashctx, &c, 1) != 0 || |
| 781 | ssh_digest_update(hashctx, kex->session_id, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 782 | kex->session_id_len) != 0 || |
| 783 | ssh_digest_final(hashctx, digest, mdsz) != 0) { |
| 784 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 785 | goto out; |
| 786 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 787 | ssh_digest_free(hashctx); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 788 | hashctx = NULL; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 789 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 790 | /* |
| 791 | * expand key: |
| 792 | * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) |
| 793 | * Key = K1 || K2 || ... || Kn |
| 794 | */ |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 795 | for (have = mdsz; need > have; have += mdsz) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 796 | if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL || |
| 797 | ssh_digest_update_buffer(hashctx, shared_secret) != 0 || |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 798 | ssh_digest_update(hashctx, hash, hashlen) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 799 | ssh_digest_update(hashctx, digest, have) != 0 || |
| 800 | ssh_digest_final(hashctx, digest + have, mdsz) != 0) { |
| 801 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 802 | goto out; |
| 803 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 804 | ssh_digest_free(hashctx); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 805 | hashctx = NULL; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 806 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 807 | #ifdef DEBUG_KEX |
| 808 | fprintf(stderr, "key '%c'== ", c); |
| 809 | dump_digest("key", digest, need); |
| 810 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 811 | *keyp = digest; |
| 812 | digest = NULL; |
| 813 | r = 0; |
| 814 | out: |
| 815 | if (digest) |
| 816 | free(digest); |
| 817 | ssh_digest_free(hashctx); |
| 818 | return r; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 819 | } |
| 820 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 821 | #define NKEYS 6 |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 822 | int |
| 823 | kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen, |
| 824 | const struct sshbuf *shared_secret) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 825 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 826 | struct kex *kex = ssh->kex; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 827 | u_char *keys[NKEYS]; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 828 | u_int i, j, mode, ctos; |
| 829 | int r; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 830 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 831 | for (i = 0; i < NKEYS; i++) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 832 | if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen, |
| 833 | shared_secret, &keys[i])) != 0) { |
| 834 | for (j = 0; j < i; j++) |
| 835 | free(keys[j]); |
| 836 | return r; |
| 837 | } |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 838 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 839 | for (mode = 0; mode < MODE_MAX; mode++) { |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 840 | ctos = (!kex->server && mode == MODE_OUT) || |
| 841 | (kex->server && mode == MODE_IN); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 842 | kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1]; |
| 843 | kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3]; |
| 844 | kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5]; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 845 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 846 | return 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 847 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 848 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 849 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 850 | int |
| 851 | kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen, |
| 852 | const BIGNUM *secret) |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 853 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 854 | struct sshbuf *shared_secret; |
| 855 | int r; |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 856 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 857 | if ((shared_secret = sshbuf_new()) == NULL) |
| 858 | return SSH_ERR_ALLOC_FAIL; |
| 859 | if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0) |
| 860 | r = kex_derive_keys(ssh, hash, hashlen, shared_secret); |
| 861 | sshbuf_free(shared_secret); |
| 862 | return r; |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 863 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 864 | #endif |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 865 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 866 | #ifdef WITH_SSH1 |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 867 | int |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 868 | derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus, |
| 869 | u_int8_t cookie[8], u_int8_t id[16]) |
| 870 | { |
djm@openbsd.org | 25f5f78 | 2015-01-30 00:22:25 +0000 | [diff] [blame] | 871 | u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH]; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 872 | struct ssh_digest_ctx *hashctx = NULL; |
djm@openbsd.org | 25f5f78 | 2015-01-30 00:22:25 +0000 | [diff] [blame] | 873 | size_t hlen, slen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 874 | int r; |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 875 | |
djm@openbsd.org | 25f5f78 | 2015-01-30 00:22:25 +0000 | [diff] [blame] | 876 | hlen = BN_num_bytes(host_modulus); |
| 877 | slen = BN_num_bytes(server_modulus); |
| 878 | if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) || |
| 879 | slen < (512 / 8) || (u_int)slen > sizeof(sbuf)) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 880 | return SSH_ERR_KEY_BITS_MISMATCH; |
djm@openbsd.org | 25f5f78 | 2015-01-30 00:22:25 +0000 | [diff] [blame] | 881 | if (BN_bn2bin(host_modulus, hbuf) <= 0 || |
| 882 | BN_bn2bin(server_modulus, sbuf) <= 0) { |
| 883 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 884 | goto out; |
| 885 | } |
| 886 | if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) { |
| 887 | r = SSH_ERR_ALLOC_FAIL; |
| 888 | goto out; |
| 889 | } |
| 890 | if (ssh_digest_update(hashctx, hbuf, hlen) != 0 || |
| 891 | ssh_digest_update(hashctx, sbuf, slen) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 892 | ssh_digest_update(hashctx, cookie, 8) != 0 || |
| 893 | ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) { |
| 894 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 895 | goto out; |
| 896 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 897 | memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 898 | r = 0; |
| 899 | out: |
| 900 | ssh_digest_free(hashctx); |
djm@openbsd.org | 25f5f78 | 2015-01-30 00:22:25 +0000 | [diff] [blame] | 901 | explicit_bzero(hbuf, sizeof(hbuf)); |
| 902 | explicit_bzero(sbuf, sizeof(sbuf)); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 903 | explicit_bzero(obuf, sizeof(obuf)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 904 | return r; |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 905 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 906 | #endif |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 907 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 908 | #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 909 | void |
| 910 | dump_digest(char *msg, u_char *digest, int len) |
| 911 | { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 912 | fprintf(stderr, "%s\n", msg); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 913 | sshbuf_dump_data(digest, len, stderr); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 914 | } |
| 915 | #endif |