Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1 | /* $OpenBSD: kex.c,v 1.99 2014/04/29 18:01:49 markus 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 | |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 28 | #include <sys/param.h> |
| 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 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 40 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include "ssh2.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "packet.h" |
| 44 | #include "compat.h" |
| 45 | #include "cipher.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 46 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 47 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "log.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 49 | #include "mac.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 50 | #include "match.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 51 | #include "dispatch.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 52 | #include "monitor.h" |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 53 | #include "roaming.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 54 | #include "digest.h" |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 55 | |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 56 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 57 | # if defined(HAVE_EVP_SHA256) |
Damien Miller | af87af1 | 2006-03-15 13:02:28 +1100 | [diff] [blame] | 58 | # define evp_ssh_sha256 EVP_sha256 |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 59 | # else |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 60 | extern const EVP_MD *evp_ssh_sha256(void); |
Damien Miller | b309203 | 2006-03-16 18:22:18 +1100 | [diff] [blame] | 61 | # endif |
Tim Rice | 425a688 | 2006-03-15 20:17:05 -0800 | [diff] [blame] | 62 | #endif |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 63 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 64 | /* prototype */ |
| 65 | static void kex_kexinit_finish(Kex *); |
| 66 | static void kex_choose_conf(Kex *); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 67 | |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 68 | struct kexalg { |
| 69 | char *name; |
| 70 | int type; |
| 71 | int ec_nid; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 72 | int hash_alg; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 73 | }; |
| 74 | static const struct kexalg kexalgs[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 75 | #ifdef WITH_OPENSSL |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 76 | { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 }, |
| 77 | { KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 }, |
| 78 | { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 }, |
Darren Tucker | a75d247 | 2013-05-10 18:11:55 +1000 | [diff] [blame] | 79 | #ifdef HAVE_EVP_SHA256 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 80 | { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 }, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 81 | #endif /* HAVE_EVP_SHA256 */ |
Darren Tucker | a75d247 | 2013-05-10 18:11:55 +1000 | [diff] [blame] | 82 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 83 | { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, |
| 84 | NID_X9_62_prime256v1, SSH_DIGEST_SHA256 }, |
| 85 | { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, |
| 86 | SSH_DIGEST_SHA384 }, |
Darren Tucker | 37bcef5 | 2013-11-09 18:39:25 +1100 | [diff] [blame] | 87 | # ifdef OPENSSL_HAS_NISTP521 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 88 | { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, |
| 89 | SSH_DIGEST_SHA512 }, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 90 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 91 | #endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 92 | #endif /* WITH_OPENSSL */ |
Darren Tucker | 1c8ce34 | 2013-11-08 19:50:32 +1100 | [diff] [blame] | 93 | #ifdef HAVE_EVP_SHA256 |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 94 | { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 }, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 95 | #endif /* HAVE_EVP_SHA256 */ |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 96 | { NULL, -1, -1, -1}, |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | char * |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 100 | kex_alg_list(char sep) |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 101 | { |
| 102 | char *ret = NULL; |
| 103 | size_t nlen, rlen = 0; |
| 104 | const struct kexalg *k; |
| 105 | |
| 106 | for (k = kexalgs; k->name != NULL; k++) { |
| 107 | if (ret != NULL) |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 108 | ret[rlen++] = sep; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 109 | nlen = strlen(k->name); |
| 110 | ret = xrealloc(ret, 1, rlen + nlen + 2); |
| 111 | memcpy(ret + rlen, k->name, nlen + 1); |
| 112 | rlen += nlen; |
| 113 | } |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static const struct kexalg * |
| 118 | kex_alg_by_name(const char *name) |
| 119 | { |
| 120 | const struct kexalg *k; |
| 121 | |
| 122 | for (k = kexalgs; k->name != NULL; k++) { |
| 123 | if (strcmp(k->name, name) == 0) |
| 124 | return k; |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 129 | /* Validate KEX method name list */ |
| 130 | int |
| 131 | kex_names_valid(const char *names) |
| 132 | { |
| 133 | char *s, *cp, *p; |
| 134 | |
| 135 | if (names == NULL || strcmp(names, "") == 0) |
| 136 | return 0; |
| 137 | s = cp = xstrdup(names); |
| 138 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
| 139 | (p = strsep(&cp, ","))) { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 140 | if (kex_alg_by_name(p) == NULL) { |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 141 | error("Unsupported KEX algorithm \"%.100s\"", p); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 142 | free(s); |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | } |
| 146 | debug3("kex names ok: [%s]", names); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 147 | free(s); |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 148 | return 1; |
| 149 | } |
| 150 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 151 | /* put algorithm proposal into buffer */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 152 | static void |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 153 | kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX]) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 154 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 155 | u_int i; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 156 | |
| 157 | buffer_clear(b); |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 158 | /* |
| 159 | * add a dummy cookie, the cookie will be overwritten by |
| 160 | * kex_send_kexinit(), each time a kexinit is set |
| 161 | */ |
| 162 | for (i = 0; i < KEX_COOKIE_LEN; i++) |
| 163 | buffer_put_char(b, 0); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 164 | for (i = 0; i < PROPOSAL_MAX; i++) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 165 | buffer_put_cstring(b, proposal[i]); |
| 166 | buffer_put_char(b, 0); /* first_kex_packet_follows */ |
| 167 | buffer_put_int(b, 0); /* uint32 reserved */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 168 | } |
| 169 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 170 | /* parse buffer and return algorithm proposal */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 171 | static char ** |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 172 | kex_buf2prop(Buffer *raw, int *first_kex_follows) |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 173 | { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 174 | Buffer b; |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 175 | u_int i; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 176 | char **proposal; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 177 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 178 | proposal = xcalloc(PROPOSAL_MAX, sizeof(char *)); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 179 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 180 | buffer_init(&b); |
| 181 | buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 182 | /* skip cookie */ |
| 183 | for (i = 0; i < KEX_COOKIE_LEN; i++) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 184 | buffer_get_char(&b); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 185 | /* extract kex init proposal strings */ |
| 186 | for (i = 0; i < PROPOSAL_MAX; i++) { |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 187 | proposal[i] = buffer_get_cstring(&b,NULL); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 188 | debug2("kex_parse_kexinit: %s", proposal[i]); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 189 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 190 | /* first kex follows / reserved */ |
| 191 | i = buffer_get_char(&b); |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 192 | if (first_kex_follows != NULL) |
| 193 | *first_kex_follows = i; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 194 | debug2("kex_parse_kexinit: first_kex_follows %d ", i); |
| 195 | i = buffer_get_int(&b); |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 196 | debug2("kex_parse_kexinit: reserved %u ", i); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 197 | buffer_free(&b); |
| 198 | return proposal; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 199 | } |
| 200 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 201 | static void |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 202 | kex_prop_free(char **proposal) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 203 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 204 | u_int i; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 205 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 206 | for (i = 0; i < PROPOSAL_MAX; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 207 | free(proposal[i]); |
| 208 | free(proposal); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 209 | } |
| 210 | |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 211 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 212 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 213 | kex_protocol_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | { |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 215 | error("Hm, kex protocol error: type %d seq %u", type, seq); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 216 | } |
| 217 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 218 | static void |
Damien Miller | bc27d4e | 2002-02-13 13:54:06 +1100 | [diff] [blame] | 219 | kex_reset_dispatch(void) |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 220 | { |
Damien Miller | 7d05339 | 2002-01-22 23:24:13 +1100 | [diff] [blame] | 221 | dispatch_range(SSH2_MSG_TRANSPORT_MIN, |
| 222 | SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); |
Damien Miller | bc27d4e | 2002-02-13 13:54:06 +1100 | [diff] [blame] | 223 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 227 | kex_finish(Kex *kex) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 228 | { |
Damien Miller | bc27d4e | 2002-02-13 13:54:06 +1100 | [diff] [blame] | 229 | kex_reset_dispatch(); |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 230 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 231 | packet_start(SSH2_MSG_NEWKEYS); |
| 232 | packet_send(); |
| 233 | /* packet_write_wait(); */ |
| 234 | debug("SSH2_MSG_NEWKEYS sent"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 235 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 236 | debug("expecting SSH2_MSG_NEWKEYS"); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 237 | packet_read_expect(SSH2_MSG_NEWKEYS); |
Ben Lindstrom | 1451908 | 2002-02-26 17:58:29 +0000 | [diff] [blame] | 238 | packet_check_eom(); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 239 | debug("SSH2_MSG_NEWKEYS received"); |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 240 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 241 | kex->done = 1; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 242 | buffer_clear(&kex->peer); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 243 | /* buffer_clear(&kex->my); */ |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 244 | kex->flags &= ~KEX_INIT_SENT; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 245 | free(kex->name); |
Ben Lindstrom | 5ba23b3 | 2001-04-05 02:05:21 +0000 | [diff] [blame] | 246 | kex->name = NULL; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 247 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 248 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 249 | void |
| 250 | kex_send_kexinit(Kex *kex) |
| 251 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 252 | u_int32_t rnd = 0; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 253 | u_char *cookie; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 254 | u_int i; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 255 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 256 | if (kex == NULL) { |
| 257 | error("kex_send_kexinit: no kex, cannot rekey"); |
| 258 | return; |
| 259 | } |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 260 | if (kex->flags & KEX_INIT_SENT) { |
| 261 | debug("KEX_INIT_SENT"); |
| 262 | return; |
| 263 | } |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 264 | kex->done = 0; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 265 | |
| 266 | /* generate a random cookie */ |
| 267 | if (buffer_len(&kex->my) < KEX_COOKIE_LEN) |
| 268 | fatal("kex_send_kexinit: kex proposal too short"); |
| 269 | cookie = buffer_ptr(&kex->my); |
| 270 | for (i = 0; i < KEX_COOKIE_LEN; i++) { |
| 271 | if (i % 4 == 0) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 272 | rnd = arc4random(); |
| 273 | cookie[i] = rnd; |
| 274 | rnd >>= 8; |
Ben Lindstrom | 5997172 | 2002-03-27 17:42:57 +0000 | [diff] [blame] | 275 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 276 | packet_start(SSH2_MSG_KEXINIT); |
| 277 | packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my)); |
| 278 | packet_send(); |
| 279 | debug("SSH2_MSG_KEXINIT sent"); |
| 280 | kex->flags |= KEX_INIT_SENT; |
| 281 | } |
| 282 | |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 283 | /* ARGSUSED */ |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 284 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 285 | kex_input_kexinit(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 286 | { |
| 287 | char *ptr; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 288 | u_int i, dlen; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 289 | Kex *kex = (Kex *)ctxt; |
| 290 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 291 | debug("SSH2_MSG_KEXINIT received"); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 292 | if (kex == NULL) |
| 293 | fatal("kex_input_kexinit: no kex, cannot rekey"); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 294 | |
| 295 | ptr = packet_get_raw(&dlen); |
| 296 | buffer_append(&kex->peer, ptr, dlen); |
| 297 | |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 298 | /* discard packet */ |
| 299 | for (i = 0; i < KEX_COOKIE_LEN; i++) |
| 300 | packet_get_char(); |
| 301 | for (i = 0; i < PROPOSAL_MAX; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 302 | free(packet_get_string(NULL)); |
Darren Tucker | ae608bd | 2012-09-06 21:19:51 +1000 | [diff] [blame] | 303 | /* |
| 304 | * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported |
| 305 | * KEX method has the server move first, but a server might be using |
| 306 | * a custom method or one that we otherwise don't support. We should |
| 307 | * be prepared to remember first_kex_follows here so we can eat a |
| 308 | * packet later. |
| 309 | * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means |
| 310 | * for cases where the server *doesn't* go first. I guess we should |
| 311 | * ignore it when it is set for these cases, which is what we do now. |
| 312 | */ |
| 313 | (void) packet_get_char(); /* first_kex_follows */ |
| 314 | (void) packet_get_int(); /* reserved */ |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 315 | packet_check_eom(); |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 316 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 317 | kex_kexinit_finish(kex); |
| 318 | } |
| 319 | |
| 320 | Kex * |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 321 | kex_setup(char *proposal[PROPOSAL_MAX]) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 322 | { |
| 323 | Kex *kex; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 324 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 325 | kex = xcalloc(1, sizeof(*kex)); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 326 | buffer_init(&kex->peer); |
| 327 | buffer_init(&kex->my); |
| 328 | kex_prop2buf(&kex->my, proposal); |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 329 | kex->done = 0; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 330 | |
| 331 | kex_send_kexinit(kex); /* we start */ |
Damien Miller | bc27d4e | 2002-02-13 13:54:06 +1100 | [diff] [blame] | 332 | kex_reset_dispatch(); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 333 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 334 | return kex; |
| 335 | } |
| 336 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 337 | static void |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 338 | kex_kexinit_finish(Kex *kex) |
| 339 | { |
| 340 | if (!(kex->flags & KEX_INIT_SENT)) |
| 341 | kex_send_kexinit(kex); |
| 342 | |
| 343 | kex_choose_conf(kex); |
| 344 | |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 345 | if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX && |
| 346 | kex->kex[kex->kex_type] != NULL) { |
| 347 | (kex->kex[kex->kex_type])(kex); |
| 348 | } else { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 349 | fatal("Unsupported key exchange %d", kex->kex_type); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 350 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 351 | } |
| 352 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 353 | static void |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 354 | choose_enc(Enc *enc, char *client, char *server) |
| 355 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 356 | char *name = match_list(client, server, NULL); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 357 | if (name == NULL) |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 358 | fatal("no matching cipher found: client %s server %s", |
| 359 | client, server); |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 360 | if ((enc->cipher = cipher_by_name(name)) == NULL) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 361 | fatal("matching cipher is not supported: %s", name); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 362 | enc->name = name; |
| 363 | enc->enabled = 0; |
| 364 | enc->iv = NULL; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 365 | enc->iv_len = cipher_ivlen(enc->cipher); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 366 | enc->key = NULL; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 367 | enc->key_len = cipher_keylen(enc->cipher); |
| 368 | enc->block_size = cipher_blocksize(enc->cipher); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 369 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 370 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 371 | static void |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 372 | choose_mac(Mac *mac, char *client, char *server) |
| 373 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 374 | char *name = match_list(client, server, NULL); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 375 | if (name == NULL) |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 376 | fatal("no matching mac found: client %s server %s", |
| 377 | client, server); |
Darren Tucker | 5f3d5be | 2007-06-05 18:30:18 +1000 | [diff] [blame] | 378 | if (mac_setup(mac, name) < 0) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 379 | fatal("unsupported mac %s", name); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 380 | /* truncate the key */ |
| 381 | if (datafellows & SSH_BUG_HMAC) |
| 382 | mac->key_len = 16; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 383 | mac->name = name; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 384 | mac->key = NULL; |
| 385 | mac->enabled = 0; |
| 386 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 387 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 388 | static void |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 389 | choose_comp(Comp *comp, char *client, char *server) |
| 390 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 391 | char *name = match_list(client, server, NULL); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 392 | if (name == NULL) |
| 393 | fatal("no matching comp found: client %s server %s", client, server); |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 394 | if (strcmp(name, "zlib@openssh.com") == 0) { |
| 395 | comp->type = COMP_DELAYED; |
| 396 | } else if (strcmp(name, "zlib") == 0) { |
| 397 | comp->type = COMP_ZLIB; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 398 | } else if (strcmp(name, "none") == 0) { |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 399 | comp->type = COMP_NONE; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 400 | } else { |
| 401 | fatal("unsupported comp %s", name); |
| 402 | } |
| 403 | comp->name = name; |
| 404 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 405 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 406 | static void |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 407 | choose_kex(Kex *k, char *client, char *server) |
| 408 | { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 409 | const struct kexalg *kexalg; |
| 410 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 411 | k->name = match_list(client, server, NULL); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 412 | if (k->name == NULL) |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 413 | fatal("Unable to negotiate a key exchange method"); |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 414 | if ((kexalg = kex_alg_by_name(k->name)) == NULL) |
| 415 | fatal("unsupported kex alg %s", k->name); |
| 416 | k->kex_type = kexalg->type; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 417 | k->hash_alg = kexalg->hash_alg; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 418 | k->ec_nid = kexalg->ec_nid; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 419 | } |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 420 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 421 | static void |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 422 | choose_hostkeyalg(Kex *k, char *client, char *server) |
| 423 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 424 | char *hostkeyalg = match_list(client, server, NULL); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 425 | if (hostkeyalg == NULL) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 426 | fatal("no hostkey alg"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 427 | k->hostkey_type = key_type_from_name(hostkeyalg); |
| 428 | if (k->hostkey_type == KEY_UNSPEC) |
| 429 | fatal("bad hostkey alg '%s'", hostkeyalg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 430 | free(hostkeyalg); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 431 | } |
| 432 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 433 | static int |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 434 | proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) |
| 435 | { |
| 436 | static int check[] = { |
| 437 | PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 |
| 438 | }; |
| 439 | int *idx; |
| 440 | char *p; |
| 441 | |
| 442 | for (idx = &check[0]; *idx != -1; idx++) { |
| 443 | if ((p = strchr(my[*idx], ',')) != NULL) |
| 444 | *p = '\0'; |
| 445 | if ((p = strchr(peer[*idx], ',')) != NULL) |
| 446 | *p = '\0'; |
| 447 | if (strcmp(my[*idx], peer[*idx]) != 0) { |
| 448 | debug2("proposal mismatch: my %s peer %s", |
| 449 | my[*idx], peer[*idx]); |
| 450 | return (0); |
| 451 | } |
| 452 | } |
| 453 | debug2("proposals match"); |
| 454 | return (1); |
| 455 | } |
| 456 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 457 | static void |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 458 | kex_choose_conf(Kex *kex) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 459 | { |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 460 | Newkeys *newkeys; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 461 | char **my, **peer; |
| 462 | char **cprop, **sprop; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 463 | int nenc, nmac, ncomp; |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 464 | u_int mode, ctos, need, dh_need, authlen; |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 465 | int first_kex_follows, type; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 466 | |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 467 | my = kex_buf2prop(&kex->my, NULL); |
| 468 | peer = kex_buf2prop(&kex->peer, &first_kex_follows); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 469 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 470 | if (kex->server) { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 471 | cprop=peer; |
| 472 | sprop=my; |
| 473 | } else { |
| 474 | cprop=my; |
| 475 | sprop=peer; |
| 476 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 477 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 478 | /* Check whether server offers roaming */ |
| 479 | if (!kex->server) { |
| 480 | char *roaming; |
| 481 | roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL); |
| 482 | if (roaming) { |
| 483 | kex->roaming = 1; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 484 | free(roaming); |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 488 | /* Algorithm Negotiation */ |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 489 | for (mode = 0; mode < MODE_MAX; mode++) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 490 | newkeys = xcalloc(1, sizeof(*newkeys)); |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 491 | kex->newkeys[mode] = newkeys; |
Darren Tucker | 0d0d195 | 2007-06-05 18:23:28 +1000 | [diff] [blame] | 492 | ctos = (!kex->server && mode == MODE_OUT) || |
| 493 | (kex->server && mode == MODE_IN); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 494 | nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; |
| 495 | nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; |
| 496 | ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 497 | choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]); |
| 498 | /* ignore mac for authenticated encryption */ |
| 499 | authlen = cipher_authlen(newkeys->enc.cipher); |
| 500 | if (authlen == 0) |
| 501 | choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]); |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 502 | choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 503 | debug("kex: %s %s %s %s", |
| 504 | ctos ? "client->server" : "server->client", |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 505 | newkeys->enc.name, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 506 | authlen == 0 ? newkeys->mac.name : "<implicit>", |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 507 | newkeys->comp.name); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 508 | } |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 509 | choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]); |
| 510 | choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 511 | sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]); |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 512 | need = dh_need = 0; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 513 | for (mode = 0; mode < MODE_MAX; mode++) { |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 514 | newkeys = kex->newkeys[mode]; |
Damien Miller | a92ac74 | 2014-01-26 09:38:03 +1100 | [diff] [blame] | 515 | need = MAX(need, newkeys->enc.key_len); |
| 516 | need = MAX(need, newkeys->enc.block_size); |
| 517 | need = MAX(need, newkeys->enc.iv_len); |
| 518 | need = MAX(need, newkeys->mac.key_len); |
| 519 | dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher)); |
| 520 | dh_need = MAX(dh_need, newkeys->enc.block_size); |
| 521 | dh_need = MAX(dh_need, newkeys->enc.iv_len); |
| 522 | dh_need = MAX(dh_need, newkeys->mac.key_len); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 523 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 524 | /* XXX need runden? */ |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 525 | kex->we_need = need; |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 526 | kex->dh_need = dh_need; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 527 | |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 528 | /* ignore the next message if the proposals do not match */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 529 | if (first_kex_follows && !proposals_match(my, peer) && |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 530 | !(datafellows & SSH_BUG_FIRSTKEX)) { |
Damien Miller | babb47a | 2003-02-24 11:53:32 +1100 | [diff] [blame] | 531 | type = packet_read(); |
| 532 | debug2("skipping next packet (type %u)", type); |
| 533 | } |
| 534 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 535 | kex_prop_free(my); |
| 536 | kex_prop_free(peer); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 539 | static u_char * |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 540 | derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen, |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 541 | const u_char *shared_secret, u_int slen) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 542 | { |
| 543 | Buffer b; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 544 | struct ssh_digest_ctx *hashctx; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 545 | char c = id; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 546 | u_int have; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 547 | size_t mdsz; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 548 | u_char *digest; |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 549 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 550 | if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0) |
| 551 | fatal("bad kex md size %zu", mdsz); |
Damien Miller | c91e556 | 2006-03-26 13:58:55 +1100 | [diff] [blame] | 552 | digest = xmalloc(roundup(need, mdsz)); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 553 | |
| 554 | buffer_init(&b); |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 555 | buffer_append(&b, shared_secret, slen); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 556 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 557 | /* K1 = HASH(K || H || "A" || session_id) */ |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 558 | if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL) |
| 559 | fatal("%s: ssh_digest_start failed", __func__); |
| 560 | if (ssh_digest_update_buffer(hashctx, &b) != 0 || |
| 561 | ssh_digest_update(hashctx, hash, hashlen) != 0 || |
| 562 | ssh_digest_update(hashctx, &c, 1) != 0 || |
| 563 | ssh_digest_update(hashctx, kex->session_id, |
| 564 | kex->session_id_len) != 0) |
| 565 | fatal("%s: ssh_digest_update failed", __func__); |
| 566 | if (ssh_digest_final(hashctx, digest, mdsz) != 0) |
| 567 | fatal("%s: ssh_digest_final failed", __func__); |
| 568 | ssh_digest_free(hashctx); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 569 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 570 | /* |
| 571 | * expand key: |
| 572 | * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) |
| 573 | * Key = K1 || K2 || ... || Kn |
| 574 | */ |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 575 | for (have = mdsz; need > have; have += mdsz) { |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 576 | if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL) |
| 577 | fatal("%s: ssh_digest_start failed", __func__); |
| 578 | if (ssh_digest_update_buffer(hashctx, &b) != 0 || |
| 579 | ssh_digest_update(hashctx, hash, hashlen) != 0 || |
| 580 | ssh_digest_update(hashctx, digest, have) != 0) |
| 581 | fatal("%s: ssh_digest_update failed", __func__); |
| 582 | if (ssh_digest_final(hashctx, digest + have, mdsz) != 0) |
| 583 | fatal("%s: ssh_digest_final failed", __func__); |
| 584 | ssh_digest_free(hashctx); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 585 | } |
| 586 | buffer_free(&b); |
| 587 | #ifdef DEBUG_KEX |
| 588 | fprintf(stderr, "key '%c'== ", c); |
| 589 | dump_digest("key", digest, need); |
| 590 | #endif |
| 591 | return digest; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 592 | } |
| 593 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 594 | Newkeys *current_keys[MODE_MAX]; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 595 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 596 | #define NKEYS 6 |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 597 | void |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 598 | kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, |
| 599 | const u_char *shared_secret, u_int slen) |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 600 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 601 | u_char *keys[NKEYS]; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 602 | u_int i, mode, ctos; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 603 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 604 | for (i = 0; i < NKEYS; i++) { |
| 605 | keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen, |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 606 | shared_secret, slen); |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 607 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 608 | |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 609 | debug2("kex_derive_keys"); |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 610 | for (mode = 0; mode < MODE_MAX; mode++) { |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 611 | current_keys[mode] = kex->newkeys[mode]; |
| 612 | kex->newkeys[mode] = NULL; |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 613 | ctos = (!kex->server && mode == MODE_OUT) || |
| 614 | (kex->server && mode == MODE_IN); |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 615 | current_keys[mode]->enc.iv = keys[ctos ? 0 : 1]; |
| 616 | current_keys[mode]->enc.key = keys[ctos ? 2 : 3]; |
| 617 | current_keys[mode]->mac.key = keys[ctos ? 4 : 5]; |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 618 | } |
Damien Miller | a664e87 | 2000-04-16 11:52:47 +1000 | [diff] [blame] | 619 | } |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 620 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 621 | #ifdef WITH_OPENSSL |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 622 | void |
| 623 | kex_derive_keys_bn(Kex *kex, u_char *hash, u_int hashlen, const BIGNUM *secret) |
| 624 | { |
| 625 | Buffer shared_secret; |
| 626 | |
| 627 | buffer_init(&shared_secret); |
| 628 | buffer_put_bignum2(&shared_secret, secret); |
| 629 | kex_derive_keys(kex, hash, hashlen, |
| 630 | buffer_ptr(&shared_secret), buffer_len(&shared_secret)); |
| 631 | buffer_free(&shared_secret); |
| 632 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 633 | #endif |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 634 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 635 | Newkeys * |
| 636 | kex_get_newkeys(int mode) |
| 637 | { |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 638 | Newkeys *ret; |
| 639 | |
| 640 | ret = current_keys[mode]; |
| 641 | current_keys[mode] = NULL; |
| 642 | return ret; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 645 | #ifdef WITH_SSH1 |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 646 | void |
| 647 | derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus, |
| 648 | u_int8_t cookie[8], u_int8_t id[16]) |
| 649 | { |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 650 | u_int8_t nbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH]; |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 651 | int len; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 652 | struct ssh_digest_ctx *hashctx; |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 653 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 654 | if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) |
| 655 | fatal("%s: ssh_digest_start", __func__); |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 656 | |
| 657 | len = BN_num_bytes(host_modulus); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 658 | if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 659 | fatal("%s: bad host modulus (len %d)", __func__, len); |
| 660 | BN_bn2bin(host_modulus, nbuf); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 661 | if (ssh_digest_update(hashctx, nbuf, len) != 0) |
| 662 | fatal("%s: ssh_digest_update failed", __func__); |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 663 | |
| 664 | len = BN_num_bytes(server_modulus); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 665 | if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 666 | fatal("%s: bad server modulus (len %d)", __func__, len); |
| 667 | BN_bn2bin(server_modulus, nbuf); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 668 | if (ssh_digest_update(hashctx, nbuf, len) != 0 || |
| 669 | ssh_digest_update(hashctx, cookie, 8) != 0) |
| 670 | fatal("%s: ssh_digest_update failed", __func__); |
| 671 | if (ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) |
| 672 | fatal("%s: ssh_digest_final failed", __func__); |
| 673 | memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5)); |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 674 | |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 675 | explicit_bzero(nbuf, sizeof(nbuf)); |
| 676 | explicit_bzero(obuf, sizeof(obuf)); |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 677 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 678 | #endif |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 679 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 680 | #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 681 | void |
| 682 | dump_digest(char *msg, u_char *digest, int len) |
| 683 | { |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 684 | int i; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 685 | |
| 686 | fprintf(stderr, "%s\n", msg); |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 687 | for (i = 0; i < len; i++) { |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 688 | fprintf(stderr, "%02x", digest[i]); |
| 689 | if (i%32 == 31) |
| 690 | fprintf(stderr, "\n"); |
| 691 | else if (i%8 == 7) |
| 692 | fprintf(stderr, " "); |
| 693 | } |
| 694 | fprintf(stderr, "\n"); |
| 695 | } |
| 696 | #endif |