1 /* $OpenBSD: kexgexc.c,v 1.23 2016/09/12 01:22:38 deraadt 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, void *); 50 static int input_kex_dh_gex_reply(int, u_int32_t, void *); 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 (datafellows & 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 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, 80 &input_kex_dh_gex_group); 81 r = 0; 82 out: 83 return r; 84 } 85 86 static int 87 input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt) 88 { 89 struct ssh *ssh = ctxt; 90 struct kex *kex = ssh->kex; 91 BIGNUM *p = NULL, *g = NULL; 92 int r, bits; 93 94 debug("got SSH2_MSG_KEX_DH_GEX_GROUP"); 95 96 if ((p = BN_new()) == NULL || 97 (g = BN_new()) == NULL) { 98 r = SSH_ERR_ALLOC_FAIL; 99 goto out; 100 } 101 if ((r = sshpkt_get_bignum2(ssh, p)) != 0 || 102 (r = sshpkt_get_bignum2(ssh, g)) != 0 || 103 (r = sshpkt_get_end(ssh)) != 0) 104 goto out; 105 if ((bits = BN_num_bits(p)) < 0 || 106 (u_int)bits < kex->min || (u_int)bits > kex->max) { 107 r = SSH_ERR_DH_GEX_OUT_OF_RANGE; 108 goto out; 109 } 110 if ((kex->dh = dh_new_group(g, p)) == NULL) { 111 r = SSH_ERR_ALLOC_FAIL; 112 goto out; 113 } 114 p = g = NULL; /* belong to kex->dh now */ 115 116 /* generate and send 'e', client DH public key */ 117 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 || 118 (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 || 119 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || 120 (r = sshpkt_send(ssh)) != 0) 121 goto out; 122 debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); 123 #ifdef DEBUG_KEXDH 124 DHparams_print_fp(stderr, kex->dh); 125 fprintf(stderr, "pub= "); 126 BN_print_fp(stderr, kex->dh->pub_key); 127 fprintf(stderr, "\n"); 128 #endif 129 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL); 130 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply); 131 r = 0; 132 out: 133 if (p) 134 BN_clear_free(p); 135 if (g) 136 BN_clear_free(g); 137 return r; 138 } 139 140 static int 141 input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt) 142 { 143 struct ssh *ssh = ctxt; 144 struct kex *kex = ssh->kex; 145 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; 146 struct sshkey *server_host_key = NULL; 147 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; 148 u_char hash[SSH_DIGEST_MAX_LENGTH]; 149 size_t klen = 0, slen, sbloblen, hashlen; 150 int kout, r; 151 152 debug("got SSH2_MSG_KEX_DH_GEX_REPLY"); 153 if (kex->verify_host_key == NULL) { 154 r = SSH_ERR_INVALID_ARGUMENT; 155 goto out; 156 } 157 /* key, cert */ 158 if ((r = sshpkt_get_string(ssh, &server_host_key_blob, 159 &sbloblen)) != 0 || 160 (r = sshkey_from_blob(server_host_key_blob, sbloblen, 161 &server_host_key)) != 0) 162 goto out; 163 if (server_host_key->type != kex->hostkey_type) { 164 r = SSH_ERR_KEY_TYPE_MISMATCH; 165 goto out; 166 } 167 if (server_host_key->type != kex->hostkey_type || 168 (kex->hostkey_type == KEY_ECDSA && 169 server_host_key->ecdsa_nid != kex->hostkey_nid)) { 170 r = SSH_ERR_KEY_TYPE_MISMATCH; 171 goto out; 172 } 173 if (kex->verify_host_key(server_host_key, ssh) == -1) { 174 r = SSH_ERR_SIGNATURE_INVALID; 175 goto out; 176 } 177 /* DH parameter f, server public DH key */ 178 if ((dh_server_pub = BN_new()) == NULL) { 179 r = SSH_ERR_ALLOC_FAIL; 180 goto out; 181 } 182 /* signed H */ 183 if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 || 184 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || 185 (r = sshpkt_get_end(ssh)) != 0) 186 goto out; 187 #ifdef DEBUG_KEXDH 188 fprintf(stderr, "dh_server_pub= "); 189 BN_print_fp(stderr, dh_server_pub); 190 fprintf(stderr, "\n"); 191 debug("bits %d", BN_num_bits(dh_server_pub)); 192 #endif 193 if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { 194 sshpkt_disconnect(ssh, "bad server public DH value"); 195 r = SSH_ERR_MESSAGE_INCOMPLETE; 196 goto out; 197 } 198 199 klen = DH_size(kex->dh); 200 if ((kbuf = malloc(klen)) == NULL || 201 (shared_secret = BN_new()) == NULL) { 202 r = SSH_ERR_ALLOC_FAIL; 203 goto out; 204 } 205 if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 || 206 BN_bin2bn(kbuf, kout, shared_secret) == NULL) { 207 r = SSH_ERR_LIBCRYPTO_ERROR; 208 goto out; 209 } 210 #ifdef DEBUG_KEXDH 211 dump_digest("shared secret", kbuf, kout); 212 #endif 213 if (ssh->compat & SSH_OLD_DHGEX) 214 kex->min = kex->max = -1; 215 216 /* calc and verify H */ 217 hashlen = sizeof(hash); 218 if ((r = kexgex_hash( 219 kex->hash_alg, 220 kex->client_version_string, 221 kex->server_version_string, 222 sshbuf_ptr(kex->my), sshbuf_len(kex->my), 223 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), 224 server_host_key_blob, sbloblen, 225 kex->min, kex->nbits, kex->max, 226 kex->dh->p, kex->dh->g, 227 kex->dh->pub_key, 228 dh_server_pub, 229 shared_secret, 230 hash, &hashlen)) != 0) 231 goto out; 232 233 if ((r = sshkey_verify(server_host_key, signature, slen, hash, 234 hashlen, ssh->compat)) != 0) 235 goto out; 236 237 /* save session id */ 238 if (kex->session_id == NULL) { 239 kex->session_id_len = hashlen; 240 kex->session_id = malloc(kex->session_id_len); 241 if (kex->session_id == NULL) { 242 r = SSH_ERR_ALLOC_FAIL; 243 goto out; 244 } 245 memcpy(kex->session_id, hash, kex->session_id_len); 246 } 247 248 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) 249 r = kex_send_newkeys(ssh); 250 out: 251 explicit_bzero(hash, sizeof(hash)); 252 DH_free(kex->dh); 253 kex->dh = NULL; 254 if (dh_server_pub) 255 BN_clear_free(dh_server_pub); 256 if (kbuf) { 257 explicit_bzero(kbuf, klen); 258 free(kbuf); 259 } 260 if (shared_secret) 261 BN_clear_free(shared_secret); 262 sshkey_free(server_host_key); 263 free(server_host_key_blob); 264 free(signature); 265 return r; 266 } 267