djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexgexs.c,v 1.42 2019/01/23 00:30:41 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 | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 31 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 32 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 33 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 34 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 36 | |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 37 | #include <openssl/dh.h> |
| 38 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 39 | #include "openbsd-compat/openssl-compat.h" |
| 40 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 41 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 42 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 43 | #include "digest.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 44 | #include "kex.h" |
| 45 | #include "log.h" |
| 46 | #include "packet.h" |
| 47 | #include "dh.h" |
| 48 | #include "ssh2.h" |
| 49 | #include "compat.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 50 | #ifdef GSSAPI |
| 51 | #include "ssh-gss.h" |
| 52 | #endif |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 53 | #include "monitor_wrap.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 54 | #include "dispatch.h" |
| 55 | #include "ssherr.h" |
| 56 | #include "sshbuf.h" |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 57 | #include "misc.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 58 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 59 | static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *); |
| 60 | static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 61 | |
| 62 | int |
| 63 | kexgex_server(struct ssh *ssh) |
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 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, |
| 66 | &input_kex_dh_gex_request); |
| 67 | debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST"); |
| 68 | return 0; |
| 69 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 70 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 71 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 72 | input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 73 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 74 | struct kex *kex = ssh->kex; |
| 75 | int r; |
| 76 | u_int min = 0, max = 0, nbits = 0; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 77 | const BIGNUM *dh_p, *dh_g; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 78 | |
djm@openbsd.org | 318be28 | 2015-04-13 02:04:08 +0000 | [diff] [blame] | 79 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); |
| 80 | if ((r = sshpkt_get_u32(ssh, &min)) != 0 || |
| 81 | (r = sshpkt_get_u32(ssh, &nbits)) != 0 || |
| 82 | (r = sshpkt_get_u32(ssh, &max)) != 0 || |
| 83 | (r = sshpkt_get_end(ssh)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 84 | goto out; |
djm@openbsd.org | 318be28 | 2015-04-13 02:04:08 +0000 | [diff] [blame] | 85 | kex->nbits = nbits; |
| 86 | kex->min = min; |
| 87 | kex->max = max; |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 88 | min = MAXIMUM(DH_GRP_MIN, min); |
| 89 | max = MINIMUM(DH_GRP_MAX, max); |
| 90 | nbits = MAXIMUM(DH_GRP_MIN, nbits); |
| 91 | nbits = MINIMUM(DH_GRP_MAX, nbits); |
dtucker@openbsd.org | 68777fa | 2016-06-08 02:13:01 +0000 | [diff] [blame] | 92 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 93 | if (kex->max < kex->min || kex->nbits < kex->min || |
dtucker@openbsd.org | 68777fa | 2016-06-08 02:13:01 +0000 | [diff] [blame] | 94 | kex->max < kex->nbits || kex->max < DH_GRP_MIN) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 95 | r = SSH_ERR_DH_GEX_OUT_OF_RANGE; |
| 96 | goto out; |
| 97 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 98 | |
| 99 | /* Contact privileged parent */ |
dtucker@openbsd.org | 68777fa | 2016-06-08 02:13:01 +0000 | [diff] [blame] | 100 | kex->dh = PRIVSEP(choose_dh(min, nbits, max)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 101 | if (kex->dh == NULL) { |
dtucker@openbsd.org | 68777fa | 2016-06-08 02:13:01 +0000 | [diff] [blame] | 102 | sshpkt_disconnect(ssh, "no matching DH grp found"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 103 | r = SSH_ERR_ALLOC_FAIL; |
| 104 | goto out; |
| 105 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 106 | debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 107 | DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 108 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 109 | (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 || |
| 110 | (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 111 | (r = sshpkt_send(ssh)) != 0) |
| 112 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 113 | |
| 114 | /* Compute our exchange value in parallel with the client */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 115 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) |
| 116 | goto out; |
| 117 | |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 118 | debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 119 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init); |
| 120 | r = 0; |
| 121 | out: |
| 122 | return r; |
| 123 | } |
| 124 | |
| 125 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 126 | input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 127 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 128 | struct kex *kex = ssh->kex; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 129 | BIGNUM *dh_client_pub = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 130 | const BIGNUM *pub_key, *dh_p, *dh_g; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 131 | struct sshbuf *shared_secret = NULL; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 132 | struct sshbuf *server_host_key_blob = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 133 | struct sshkey *server_host_public, *server_host_private; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 134 | u_char *signature = NULL; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 135 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 136 | size_t slen, hashlen; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 137 | int r; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 138 | |
djm@openbsd.org | bb39baf | 2019-01-21 10:05:09 +0000 | [diff] [blame] | 139 | if ((r = kex_load_hostkey(ssh, &server_host_private, |
| 140 | &server_host_public)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 141 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 142 | |
| 143 | /* key, cert */ |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 144 | if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 145 | (r = sshpkt_get_end(ssh)) != 0) |
| 146 | goto out; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 147 | if ((shared_secret = sshbuf_new()) == NULL) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 148 | r = SSH_ERR_ALLOC_FAIL; |
| 149 | goto out; |
| 150 | } |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 151 | if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 152 | goto out; |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 153 | if ((server_host_key_blob = sshbuf_new()) == NULL) { |
| 154 | r = SSH_ERR_ALLOC_FAIL; |
| 155 | goto out; |
| 156 | } |
| 157 | if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 158 | goto out; |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 159 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 160 | /* calc H */ |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 161 | DH_get0_key(kex->dh, &pub_key, NULL); |
| 162 | DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 163 | hashlen = sizeof(hash); |
| 164 | if ((r = kexgex_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 165 | kex->hash_alg, |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 166 | kex->client_version, |
| 167 | kex->server_version, |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 168 | kex->peer, |
| 169 | kex->my, |
| 170 | server_host_key_blob, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 171 | kex->min, kex->nbits, kex->max, |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 172 | dh_p, dh_g, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 173 | dh_client_pub, |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 174 | pub_key, |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 175 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 176 | hash, &hashlen)) != 0) |
| 177 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 178 | |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 179 | /* sign H */ |
djm@openbsd.org | 04c091f | 2019-01-19 21:43:56 +0000 | [diff] [blame] | 180 | if ((r = kex->sign(ssh, server_host_private, server_host_public, |
| 181 | &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 182 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 183 | |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 184 | /* send server hostkey, DH pubkey 'f' and signed H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 185 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 || |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 186 | (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 187 | (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 188 | (r = sshpkt_put_string(ssh, signature, slen)) != 0 || |
| 189 | (r = sshpkt_send(ssh)) != 0) |
| 190 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 191 | |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 192 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 193 | r = kex_send_newkeys(ssh); |
| 194 | out: |
djm@openbsd.org | 2d1428b | 2018-10-04 00:04:41 +0000 | [diff] [blame] | 195 | explicit_bzero(hash, sizeof(hash)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 196 | DH_free(kex->dh); |
| 197 | kex->dh = NULL; |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 198 | BN_clear_free(dh_client_pub); |
djm@openbsd.org | dec5e9d | 2019-01-21 10:03:37 +0000 | [diff] [blame] | 199 | sshbuf_free(shared_secret); |
djm@openbsd.org | bb956ea | 2019-01-23 00:30:41 +0000 | [diff] [blame] | 200 | sshbuf_free(server_host_key_blob); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 201 | free(signature); |
| 202 | return r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 203 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 204 | #endif /* WITH_OPENSSL */ |