djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexgexc.c,v 1.20 2015/01/26 06:10:03 djm Exp $ */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Niels Provos. All rights reserved. |
| 4 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 28 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 29 | #ifdef WITH_OPENSSL |
| 30 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 33 | #include <openssl/dh.h> |
| 34 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 35 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 36 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 39 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 40 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 42 | #include "digest.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 43 | #include "kex.h" |
| 44 | #include "log.h" |
| 45 | #include "packet.h" |
| 46 | #include "dh.h" |
| 47 | #include "ssh2.h" |
| 48 | #include "compat.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 49 | #include "dispatch.h" |
| 50 | #include "ssherr.h" |
| 51 | #include "sshbuf.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 52 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 53 | static int input_kex_dh_gex_group(int, u_int32_t, void *); |
| 54 | static int input_kex_dh_gex_reply(int, u_int32_t, void *); |
| 55 | |
| 56 | int |
| 57 | kexgex_client(struct ssh *ssh) |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 58 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 59 | struct kex *kex = ssh->kex; |
| 60 | int r; |
| 61 | u_int nbits; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 62 | |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 63 | nbits = dh_estimate(kex->dh_need * 8); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 64 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 65 | kex->min = DH_GRP_MIN; |
| 66 | kex->max = DH_GRP_MAX; |
| 67 | kex->nbits = nbits; |
| 68 | if (ssh->compat & SSH_OLD_DHGEX) { |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 69 | /* Old GEX request */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 70 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)) |
| 71 | != 0 || |
| 72 | (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 || |
| 73 | (r = sshpkt_send(ssh)) != 0) |
| 74 | goto out; |
| 75 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", kex->nbits); |
Darren Tucker | 564f19e | 2003-12-09 19:18:07 +1100 | [diff] [blame] | 76 | } else { |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 77 | /* New GEX request */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 78 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 || |
| 79 | (r = sshpkt_put_u32(ssh, kex->min)) != 0 || |
| 80 | (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 || |
| 81 | (r = sshpkt_put_u32(ssh, kex->max)) != 0 || |
| 82 | (r = sshpkt_send(ssh)) != 0) |
| 83 | goto out; |
Darren Tucker | 564f19e | 2003-12-09 19:18:07 +1100 | [diff] [blame] | 84 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 85 | kex->min, kex->nbits, kex->max); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 86 | } |
| 87 | #ifdef DEBUG_KEXDH |
| 88 | fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 89 | kex->min, kex->nbits, kex->max); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 90 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 91 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, |
| 92 | &input_kex_dh_gex_group); |
| 93 | r = 0; |
| 94 | out: |
| 95 | return r; |
| 96 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 97 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 98 | static int |
| 99 | input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt) |
| 100 | { |
| 101 | struct ssh *ssh = ctxt; |
| 102 | struct kex *kex = ssh->kex; |
| 103 | BIGNUM *p = NULL, *g = NULL; |
| 104 | int r, bits; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 105 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 106 | debug("got SSH2_MSG_KEX_DH_GEX_GROUP"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 107 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 108 | if ((p = BN_new()) == NULL || |
| 109 | (g = BN_new()) == NULL) { |
| 110 | r = SSH_ERR_ALLOC_FAIL; |
| 111 | goto out; |
| 112 | } |
| 113 | if ((r = sshpkt_get_bignum2(ssh, p)) != 0 || |
| 114 | (r = sshpkt_get_bignum2(ssh, g)) != 0 || |
| 115 | (r = sshpkt_get_end(ssh)) != 0) |
| 116 | goto out; |
| 117 | if ((bits = BN_num_bits(p)) < 0 || |
| 118 | (u_int)bits < kex->min || (u_int)bits > kex->max) { |
| 119 | r = SSH_ERR_DH_GEX_OUT_OF_RANGE; |
| 120 | goto out; |
| 121 | } |
| 122 | if ((kex->dh = dh_new_group(g, p)) == NULL) { |
| 123 | r = SSH_ERR_ALLOC_FAIL; |
| 124 | goto out; |
| 125 | } |
| 126 | p = g = NULL; /* belong to kex->dh now */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 127 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 128 | /* generate and send 'e', client DH public key */ |
| 129 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 || |
| 130 | (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 || |
| 131 | (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || |
| 132 | (r = sshpkt_send(ssh)) != 0) |
| 133 | goto out; |
| 134 | debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 135 | #ifdef DEBUG_KEXDH |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 136 | DHparams_print_fp(stderr, kex->dh); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 137 | fprintf(stderr, "pub= "); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 138 | BN_print_fp(stderr, kex->dh->pub_key); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 139 | fprintf(stderr, "\n"); |
| 140 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 141 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL); |
| 142 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply); |
| 143 | r = 0; |
| 144 | out: |
| 145 | if (p) |
| 146 | BN_clear_free(p); |
| 147 | if (g) |
| 148 | BN_clear_free(g); |
| 149 | return r; |
| 150 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 151 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 152 | static int |
| 153 | input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt) |
| 154 | { |
| 155 | struct ssh *ssh = ctxt; |
| 156 | struct kex *kex = ssh->kex; |
| 157 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; |
| 158 | struct sshkey *server_host_key = NULL; |
| 159 | u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; |
| 160 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 161 | size_t klen = 0, slen, sbloblen, hashlen; |
| 162 | int kout, r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 163 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 164 | debug("got SSH2_MSG_KEX_DH_GEX_REPLY"); |
| 165 | if (kex->verify_host_key == NULL) { |
| 166 | r = SSH_ERR_INVALID_ARGUMENT; |
| 167 | goto out; |
| 168 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 169 | /* key, cert */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 170 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
| 171 | &sbloblen)) != 0 || |
| 172 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
| 173 | &server_host_key)) != 0) |
| 174 | goto out; |
| 175 | if (server_host_key->type != kex->hostkey_type) { |
| 176 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 177 | goto out; |
| 178 | } |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 179 | if (server_host_key->type != kex->hostkey_type || |
| 180 | (kex->hostkey_type == KEY_ECDSA && |
| 181 | server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
| 182 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 183 | goto out; |
| 184 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 185 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
| 186 | r = SSH_ERR_SIGNATURE_INVALID; |
| 187 | goto out; |
| 188 | } |
Damien Miller | ad6b14d | 2006-06-13 13:00:41 +1000 | [diff] [blame] | 189 | /* DH parameter f, server public DH key */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 190 | if ((dh_server_pub = BN_new()) == NULL) { |
| 191 | r = SSH_ERR_ALLOC_FAIL; |
| 192 | goto out; |
| 193 | } |
| 194 | /* signed H */ |
| 195 | if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 || |
| 196 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
| 197 | (r = sshpkt_get_end(ssh)) != 0) |
| 198 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 199 | #ifdef DEBUG_KEXDH |
| 200 | fprintf(stderr, "dh_server_pub= "); |
| 201 | BN_print_fp(stderr, dh_server_pub); |
| 202 | fprintf(stderr, "\n"); |
| 203 | debug("bits %d", BN_num_bits(dh_server_pub)); |
| 204 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 205 | if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { |
| 206 | sshpkt_disconnect(ssh, "bad server public DH value"); |
| 207 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
| 208 | goto out; |
| 209 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 210 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 211 | klen = DH_size(kex->dh); |
| 212 | if ((kbuf = malloc(klen)) == NULL || |
| 213 | (shared_secret = BN_new()) == NULL) { |
| 214 | r = SSH_ERR_ALLOC_FAIL; |
| 215 | goto out; |
| 216 | } |
| 217 | if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 || |
| 218 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) { |
| 219 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 220 | goto out; |
| 221 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 222 | #ifdef DEBUG_KEXDH |
| 223 | dump_digest("shared secret", kbuf, kout); |
| 224 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 225 | if (ssh->compat & SSH_OLD_DHGEX) |
| 226 | kex->min = kex->max = -1; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 227 | |
| 228 | /* calc and verify H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 229 | hashlen = sizeof(hash); |
| 230 | if ((r = kexgex_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 231 | kex->hash_alg, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 232 | kex->client_version_string, |
| 233 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 234 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
| 235 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 236 | server_host_key_blob, sbloblen, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 237 | kex->min, kex->nbits, kex->max, |
| 238 | kex->dh->p, kex->dh->g, |
| 239 | kex->dh->pub_key, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 240 | dh_server_pub, |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 241 | shared_secret, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 242 | hash, &hashlen)) != 0) |
| 243 | goto out; |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 244 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 245 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, |
| 246 | hashlen, ssh->compat)) != 0) |
| 247 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 248 | |
| 249 | /* save session id */ |
| 250 | if (kex->session_id == NULL) { |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 251 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 252 | kex->session_id = malloc(kex->session_id_len); |
| 253 | if (kex->session_id == NULL) { |
| 254 | r = SSH_ERR_ALLOC_FAIL; |
| 255 | goto out; |
| 256 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 257 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 258 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 259 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 260 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
| 261 | r = kex_send_newkeys(ssh); |
| 262 | out: |
| 263 | explicit_bzero(hash, sizeof(hash)); |
| 264 | DH_free(kex->dh); |
| 265 | kex->dh = NULL; |
| 266 | if (dh_server_pub) |
| 267 | BN_clear_free(dh_server_pub); |
| 268 | if (kbuf) { |
| 269 | explicit_bzero(kbuf, klen); |
| 270 | free(kbuf); |
| 271 | } |
| 272 | if (shared_secret) |
| 273 | BN_clear_free(shared_secret); |
| 274 | sshkey_free(server_host_key); |
| 275 | free(server_host_key_blob); |
| 276 | free(signature); |
| 277 | return r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 278 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 279 | #endif /* WITH_OPENSSL */ |