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