1 /* $OpenBSD: kexgexc.c,v 1.38 2021/12/19 22:08:06 djm Exp $ */ 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 <sys/types.h> 28 29 #include <openssl/dh.h> 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <signal.h> 34 35 #include "sshkey.h" 36 #include "cipher.h" 37 #include "digest.h" 38 #include "kex.h" 39 #include "log.h" 40 #include "packet.h" 41 #include "dh.h" 42 #include "ssh2.h" 43 #include "compat.h" 44 #include "dispatch.h" 45 #include "ssherr.h" 46 #include "sshbuf.h" 47 #include "misc.h" 48 49 static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *); 50 static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *); 51 52 int 53 kexgex_client(struct ssh *ssh) 54 { 55 struct kex *kex = ssh->kex; 56 int r; 57 u_int nbits; 58 59 nbits = dh_estimate(kex->dh_need * 8); 60 61 kex->min = DH_GRP_MIN; 62 kex->max = DH_GRP_MAX; 63 kex->nbits = nbits; 64 if (ssh->compat & SSH_BUG_DHGEX_LARGE) 65 kex->nbits = MINIMUM(kex->nbits, 4096); 66 /* New GEX request */ 67 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 || 68 (r = sshpkt_put_u32(ssh, kex->min)) != 0 || 69 (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 || 70 (r = sshpkt_put_u32(ssh, kex->max)) != 0 || 71 (r = sshpkt_send(ssh)) != 0) 72 goto out; 73 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", 74 kex->min, kex->nbits, kex->max); 75 #ifdef DEBUG_KEXDH 76 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", 77 kex->min, kex->nbits, kex->max); 78 #endif 79 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP"); 80 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, 81 &input_kex_dh_gex_group); 82 r = 0; 83 out: 84 return r; 85 } 86 87 static int 88 input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh) 89 { 90 struct kex *kex = ssh->kex; 91 BIGNUM *p = NULL, *g = NULL; 92 const BIGNUM *pub_key; 93 int r, bits; 94 95 debug("SSH2_MSG_KEX_DH_GEX_GROUP received"); 96 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, &kex_protocol_error); 97 98 if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 || 99 (r = sshpkt_get_bignum2(ssh, &g)) != 0 || 100 (r = sshpkt_get_end(ssh)) != 0) 101 goto out; 102 if ((bits = BN_num_bits(p)) < 0 || 103 (u_int)bits < kex->min || (u_int)bits > kex->max) { 104 r = SSH_ERR_DH_GEX_OUT_OF_RANGE; 105 goto out; 106 } 107 if ((kex->dh = dh_new_group(g, p)) == NULL) { 108 r = SSH_ERR_ALLOC_FAIL; 109 goto out; 110 } 111 p = g = NULL; /* belong to kex->dh now */ 112 113 /* generate and send 'e', client DH public key */ 114 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) 115 goto out; 116 DH_get0_key(kex->dh, &pub_key, NULL); 117 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 || 118 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || 119 (r = sshpkt_send(ssh)) != 0) 120 goto out; 121 debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); 122 #ifdef DEBUG_KEXDH 123 DHparams_print_fp(stderr, kex->dh); 124 fprintf(stderr, "pub= "); 125 BN_print_fp(stderr, pub_key); 126 fprintf(stderr, "\n"); 127 #endif 128 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY"); 129 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply); 130 r = 0; 131 out: 132 BN_clear_free(p); 133 BN_clear_free(g); 134 return r; 135 } 136 137 static int 138 input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh) 139 { 140 struct kex *kex = ssh->kex; 141 BIGNUM *dh_server_pub = NULL; 142 const BIGNUM *pub_key, *dh_p, *dh_g; 143 struct sshbuf *shared_secret = NULL; 144 struct sshbuf *tmp = NULL, *server_host_key_blob = NULL; 145 struct sshkey *server_host_key = NULL; 146 u_char *signature = NULL; 147 u_char hash[SSH_DIGEST_MAX_LENGTH]; 148 size_t slen, hashlen; 149 int r; 150 151 debug("SSH2_MSG_KEX_DH_GEX_REPLY received"); 152 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &kex_protocol_error); 153 154 /* key, cert */ 155 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0) 156 goto out; 157 /* sshkey_fromb() consumes its buffer, so make a copy */ 158 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) { 159 r = SSH_ERR_ALLOC_FAIL; 160 goto out; 161 } 162 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 || 163 (r = kex_verify_host_key(ssh, server_host_key)) != 0) 164 goto out; 165 /* DH parameter f, server public DH key, signed H */ 166 if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 || 167 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || 168 (r = sshpkt_get_end(ssh)) != 0) 169 goto out; 170 if ((shared_secret = sshbuf_new()) == NULL) { 171 r = SSH_ERR_ALLOC_FAIL; 172 goto out; 173 } 174 if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0) 175 goto out; 176 if (ssh->compat & SSH_OLD_DHGEX) 177 kex->min = kex->max = -1; 178 179 /* calc and verify H */ 180 DH_get0_key(kex->dh, &pub_key, NULL); 181 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); 182 hashlen = sizeof(hash); 183 if ((r = kexgex_hash( 184 kex->hash_alg, 185 kex->client_version, 186 kex->server_version, 187 kex->my, 188 kex->peer, 189 server_host_key_blob, 190 kex->min, kex->nbits, kex->max, 191 dh_p, dh_g, 192 pub_key, 193 dh_server_pub, 194 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), 195 hash, &hashlen)) != 0) 196 goto out; 197 198 if ((r = sshkey_verify(server_host_key, signature, slen, hash, 199 hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0) 200 goto out; 201 202 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 203 (r = kex_send_newkeys(ssh)) != 0) 204 goto out; 205 206 /* save initial signature and hostkey */ 207 if ((kex->flags & KEX_INITIAL) != 0) { 208 if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) { 209 r = SSH_ERR_INTERNAL_ERROR; 210 goto out; 211 } 212 if ((kex->initial_sig = sshbuf_new()) == NULL) { 213 r = SSH_ERR_ALLOC_FAIL; 214 goto out; 215 } 216 if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0) 217 goto out; 218 kex->initial_hostkey = server_host_key; 219 server_host_key = NULL; 220 } 221 /* success */ 222 out: 223 explicit_bzero(hash, sizeof(hash)); 224 DH_free(kex->dh); 225 kex->dh = NULL; 226 BN_clear_free(dh_server_pub); 227 sshbuf_free(shared_secret); 228 sshkey_free(server_host_key); 229 sshbuf_free(tmp); 230 sshbuf_free(server_host_key_blob); 231 free(signature); 232 return r; 233 } 234