djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexgexc.c,v 1.35 2019/11/25 00:51:37 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 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 40 | #include "openbsd-compat/openssl-compat.h" |
| 41 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 42 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 44 | #include "digest.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 45 | #include "kex.h" |
| 46 | #include "log.h" |
| 47 | #include "packet.h" |
| 48 | #include "dh.h" |
| 49 | #include "ssh2.h" |
| 50 | #include "compat.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 51 | #include "dispatch.h" |
| 52 | #include "ssherr.h" |
| 53 | #include "sshbuf.h" |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 54 | #include "misc.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 55 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 56 | static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *); |
| 57 | static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 58 | |
| 59 | int |
| 60 | kexgex_client(struct ssh *ssh) |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 61 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 62 | struct kex *kex = ssh->kex; |
| 63 | int r; |
| 64 | u_int nbits; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 65 | |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 66 | nbits = dh_estimate(kex->dh_need * 8); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 67 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 68 | kex->min = DH_GRP_MIN; |
| 69 | kex->max = DH_GRP_MAX; |
| 70 | kex->nbits = nbits; |
dtucker@openbsd.org | b282fec | 2015-05-26 23:23:40 +0000 | [diff] [blame] | 71 | if (datafellows & SSH_BUG_DHGEX_LARGE) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 72 | kex->nbits = MINIMUM(kex->nbits, 4096); |
djm@openbsd.org | 318be28 | 2015-04-13 02:04:08 +0000 | [diff] [blame] | 73 | /* New GEX request */ |
| 74 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 || |
| 75 | (r = sshpkt_put_u32(ssh, kex->min)) != 0 || |
| 76 | (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 || |
| 77 | (r = sshpkt_put_u32(ssh, kex->max)) != 0 || |
| 78 | (r = sshpkt_send(ssh)) != 0) |
| 79 | goto out; |
| 80 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", |
| 81 | kex->min, kex->nbits, kex->max); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 82 | #ifdef DEBUG_KEXDH |
| 83 | fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 84 | kex->min, kex->nbits, kex->max); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 85 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 86 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, |
| 87 | &input_kex_dh_gex_group); |
| 88 | r = 0; |
| 89 | out: |
| 90 | return r; |
| 91 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 92 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 93 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 94 | input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 95 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 96 | struct kex *kex = ssh->kex; |
| 97 | BIGNUM *p = NULL, *g = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 98 | const BIGNUM *pub_key; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 99 | int r, bits; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 100 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 101 | debug("got SSH2_MSG_KEX_DH_GEX_GROUP"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 102 | |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 103 | if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 || |
| 104 | (r = sshpkt_get_bignum2(ssh, &g)) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 105 | (r = sshpkt_get_end(ssh)) != 0) |
| 106 | goto out; |
| 107 | if ((bits = BN_num_bits(p)) < 0 || |
| 108 | (u_int)bits < kex->min || (u_int)bits > kex->max) { |
| 109 | r = SSH_ERR_DH_GEX_OUT_OF_RANGE; |
| 110 | goto out; |
| 111 | } |
| 112 | if ((kex->dh = dh_new_group(g, p)) == NULL) { |
| 113 | r = SSH_ERR_ALLOC_FAIL; |
| 114 | goto out; |
| 115 | } |
| 116 | p = g = NULL; /* belong to kex->dh now */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 117 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 118 | /* generate and send 'e', client DH public key */ |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 119 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) |
| 120 | goto out; |
| 121 | DH_get0_key(kex->dh, &pub_key, NULL); |
| 122 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 || |
| 123 | (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 124 | (r = sshpkt_send(ssh)) != 0) |
| 125 | goto out; |
| 126 | debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 127 | #ifdef DEBUG_KEXDH |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 128 | DHparams_print_fp(stderr, kex->dh); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 129 | fprintf(stderr, "pub= "); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 130 | BN_print_fp(stderr, pub_key); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 131 | fprintf(stderr, "\n"); |
| 132 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 133 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL); |
| 134 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply); |
| 135 | r = 0; |
| 136 | out: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 137 | BN_clear_free(p); |
| 138 | BN_clear_free(g); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 139 | return r; |
| 140 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 141 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 142 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 143 | input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 144 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 145 | struct kex *kex = ssh->kex; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 146 | BIGNUM *dh_server_pub = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 147 | const BIGNUM *pub_key, *dh_p, *dh_g; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 148 | struct sshbuf *shared_secret = NULL; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 149 | struct sshbuf *tmp = NULL, *server_host_key_blob = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 150 | struct sshkey *server_host_key = NULL; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 151 | u_char *signature = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 152 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 153 | size_t slen, hashlen; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 154 | int r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 155 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 156 | debug("got SSH2_MSG_KEX_DH_GEX_REPLY"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 157 | /* key, cert */ |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 158 | if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 159 | goto out; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 160 | /* sshkey_fromb() consumes its buffer, so make a copy */ |
| 161 | if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) { |
| 162 | r = SSH_ERR_ALLOC_FAIL; |
| 163 | goto out; |
| 164 | } |
| 165 | if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 || |
| 166 | (r = kex_verify_host_key(ssh, server_host_key)) != 0) |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 167 | goto out; |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 168 | /* DH parameter f, server public DH key, signed H */ |
| 169 | if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 170 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
| 171 | (r = sshpkt_get_end(ssh)) != 0) |
| 172 | goto out; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 173 | if ((shared_secret = sshbuf_new()) == NULL) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 174 | r = SSH_ERR_ALLOC_FAIL; |
| 175 | goto out; |
| 176 | } |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 177 | if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 178 | goto out; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 179 | if (ssh->compat & SSH_OLD_DHGEX) |
| 180 | kex->min = kex->max = -1; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 181 | |
| 182 | /* calc and verify H */ |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 183 | DH_get0_key(kex->dh, &pub_key, NULL); |
| 184 | DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 185 | hashlen = sizeof(hash); |
| 186 | if ((r = kexgex_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 187 | kex->hash_alg, |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 188 | kex->client_version, |
| 189 | kex->server_version, |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 190 | kex->my, |
| 191 | kex->peer, |
| 192 | server_host_key_blob, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 193 | kex->min, kex->nbits, kex->max, |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 194 | dh_p, dh_g, |
| 195 | pub_key, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 196 | dh_server_pub, |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 197 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 198 | hash, &hashlen)) != 0) |
| 199 | goto out; |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 200 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 201 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 202 | hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 203 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 204 | |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 205 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 206 | r = kex_send_newkeys(ssh); |
| 207 | out: |
| 208 | explicit_bzero(hash, sizeof(hash)); |
| 209 | DH_free(kex->dh); |
| 210 | kex->dh = NULL; |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 211 | BN_clear_free(dh_server_pub); |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 212 | sshbuf_free(shared_secret); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 213 | sshkey_free(server_host_key); |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 214 | sshbuf_free(tmp); |
| 215 | sshbuf_free(server_host_key_blob); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 216 | free(signature); |
| 217 | return r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 218 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 219 | #endif /* WITH_OPENSSL */ |