1 /* $NetBSD: kexgexs.c,v 1.16 2018/04/06 18:59:00 christos Exp $ */ 2 /* $OpenBSD: kexgexs.c,v 1.32 2018/02/07 02:06:51 jsing Exp $ */ 3 /* 4 * Copyright (c) 2000 Niels Provos. All rights reserved. 5 * Copyright (c) 2001 Markus Friedl. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "includes.h" 29 __RCSID("$NetBSD: kexgexs.c,v 1.16 2018/04/06 18:59:00 christos Exp $"); 30 31 #include <sys/param.h> /* MIN MAX */ 32 #include <stdio.h> 33 #include <string.h> 34 #include <signal.h> 35 36 #include <openssl/dh.h> 37 38 #include "sshkey.h" 39 #include "cipher.h" 40 #include "digest.h" 41 #include "kex.h" 42 #include "log.h" 43 #include "packet.h" 44 #include "dh.h" 45 #include "ssh2.h" 46 #include "compat.h" 47 #ifdef GSSAPI 48 #include "ssh-gss.h" 49 #endif 50 #include "monitor_wrap.h" 51 #include "dispatch.h" 52 #include "ssherr.h" 53 #include "sshbuf.h" 54 #include "misc.h" 55 56 static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *); 57 static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *); 58 59 int 60 kexgex_server(struct ssh *ssh) 61 { 62 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, 63 &input_kex_dh_gex_request); 64 debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST"); 65 return 0; 66 } 67 68 static int 69 input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh) 70 { 71 struct kex *kex = ssh->kex; 72 int r; 73 u_int min = 0, max = 0, nbits = 0; 74 75 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 76 if ((r = sshpkt_get_u32(ssh, &min)) != 0 || 77 (r = sshpkt_get_u32(ssh, &nbits)) != 0 || 78 (r = sshpkt_get_u32(ssh, &max)) != 0 || 79 (r = sshpkt_get_end(ssh)) != 0) 80 goto out; 81 kex->nbits = nbits; 82 kex->min = min; 83 kex->max = max; 84 min = MAXIMUM(DH_GRP_MIN, min); 85 max = MINIMUM(DH_GRP_MAX, max); 86 nbits = MAXIMUM(DH_GRP_MIN, nbits); 87 nbits = MINIMUM(DH_GRP_MAX, nbits); 88 89 if (kex->max < kex->min || kex->nbits < kex->min || 90 kex->max < kex->nbits || kex->max < DH_GRP_MIN) { 91 r = SSH_ERR_DH_GEX_OUT_OF_RANGE; 92 goto out; 93 } 94 95 /* Contact privileged parent */ 96 kex->dh = PRIVSEP(choose_dh(min, nbits, max)); 97 if (kex->dh == NULL) { 98 sshpkt_disconnect(ssh, "no matching DH grp found"); 99 r = SSH_ERR_ALLOC_FAIL; 100 goto out; 101 } 102 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 103 { 104 const BIGNUM *p, *g; 105 DH_get0_pqg(kex->dh, &p, NULL, &g); 106 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 || 107 (r = sshpkt_put_bignum2(ssh, p)) != 0 || 108 (r = sshpkt_put_bignum2(ssh, g)) != 0 || 109 (r = sshpkt_send(ssh)) != 0) { 110 goto out; 111 } 112 } 113 114 /* Compute our exchange value in parallel with the client */ 115 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) 116 goto out; 117 118 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 119 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init); 120 r = 0; 121 out: 122 if (r != 0) { 123 DH_free(kex->dh); 124 kex->dh = NULL; 125 } 126 return r; 127 } 128 129 static int 130 input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh) 131 { 132 struct kex *kex = ssh->kex; 133 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 134 struct sshkey *server_host_public, *server_host_private; 135 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; 136 u_char hash[SSH_DIGEST_MAX_LENGTH]; 137 size_t sbloblen, slen; 138 size_t klen = 0, hashlen; 139 int kout, r; 140 141 if (kex->load_host_public_key == NULL || 142 kex->load_host_private_key == NULL) { 143 r = SSH_ERR_INVALID_ARGUMENT; 144 goto out; 145 } 146 server_host_public = kex->load_host_public_key(kex->hostkey_type, 147 kex->hostkey_nid, ssh); 148 server_host_private = kex->load_host_private_key(kex->hostkey_type, 149 kex->hostkey_nid, ssh); 150 if (server_host_public == NULL) { 151 r = SSH_ERR_NO_HOSTKEY_LOADED; 152 goto out; 153 } 154 155 /* key, cert */ 156 if ((dh_client_pub = BN_new()) == NULL) { 157 r = SSH_ERR_ALLOC_FAIL; 158 goto out; 159 } 160 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 || 161 (r = sshpkt_get_end(ssh)) != 0) 162 goto out; 163 164 #ifdef DEBUG_KEXDH 165 fprintf(stderr, "dh_client_pub= "); 166 BN_print_fp(stderr, dh_client_pub); 167 fprintf(stderr, "\n"); 168 debug("bits %d", BN_num_bits(dh_client_pub)); 169 #endif 170 171 #ifdef DEBUG_KEXDH 172 DHparams_print_fp(stderr, kex->dh); 173 fprintf(stderr, "pub= "); 174 BN_print_fp(stderr, kex->dh->pub_key); 175 fprintf(stderr, "\n"); 176 #endif 177 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) { 178 sshpkt_disconnect(ssh, "bad client public DH value"); 179 r = SSH_ERR_MESSAGE_INCOMPLETE; 180 goto out; 181 } 182 183 klen = DH_size(kex->dh); 184 if ((kbuf = malloc(klen)) == NULL || 185 (shared_secret = BN_new()) == NULL) { 186 r = SSH_ERR_ALLOC_FAIL; 187 goto out; 188 } 189 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 || 190 BN_bin2bn(kbuf, kout, shared_secret) == NULL) { 191 r = SSH_ERR_LIBCRYPTO_ERROR; 192 goto out; 193 } 194 #ifdef DEBUG_KEXDH 195 dump_digest("shared secret", kbuf, kout); 196 #endif 197 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, 198 &sbloblen)) != 0) 199 goto out; 200 /* calc H */ 201 hashlen = sizeof(hash); 202 { 203 const BIGNUM *p, *g, *pub_key; 204 DH_get0_pqg(kex->dh, &p, NULL, &g); 205 DH_get0_key(kex->dh, &pub_key, NULL); 206 if ((r = kexgex_hash( 207 kex->hash_alg, 208 kex->client_version_string, 209 kex->server_version_string, 210 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), 211 sshbuf_ptr(kex->my), sshbuf_len(kex->my), 212 server_host_key_blob, sbloblen, 213 kex->min, kex->nbits, kex->max, 214 p, g, 215 dh_client_pub, 216 pub_key, 217 shared_secret, 218 hash, &hashlen)) != 0) { 219 goto out; 220 } 221 } 222 223 /* save session id := H */ 224 if (kex->session_id == NULL) { 225 kex->session_id_len = hashlen; 226 kex->session_id = malloc(kex->session_id_len); 227 if (kex->session_id == NULL) { 228 r = SSH_ERR_ALLOC_FAIL; 229 goto out; 230 } 231 memcpy(kex->session_id, hash, kex->session_id_len); 232 } 233 234 /* sign H */ 235 if ((r = kex->sign(server_host_private, server_host_public, &signature, 236 &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0) 237 goto out; 238 239 /* destroy_sensitive_data(); */ 240 241 /* send server hostkey, DH pubkey 'f' and singed H */ 242 { 243 const BIGNUM *pub_key; 244 DH_get0_key(kex->dh, &pub_key, NULL); 245 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 || 246 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || 247 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */ 248 (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 249 (r = sshpkt_send(ssh)) != 0) { 250 goto out; 251 } 252 } 253 254 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) 255 r = kex_send_newkeys(ssh); 256 out: 257 DH_free(kex->dh); 258 kex->dh = NULL; 259 BN_clear_free(dh_client_pub); 260 if (kbuf) { 261 explicit_bzero(kbuf, klen); 262 free(kbuf); 263 } 264 BN_clear_free(shared_secret); 265 free(server_host_key_blob); 266 free(signature); 267 return r; 268 } 269