1 /* $OpenBSD: kexgexs.c,v 1.24 2015/01/26 06:10:03 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 "includes.h" 28 29 #ifdef WITH_OPENSSL 30 31 #include <sys/param.h> /* MIN MAX */ 32 33 #include <stdarg.h> 34 #include <stdio.h> 35 #include <string.h> 36 #include <signal.h> 37 38 #include <openssl/dh.h> 39 40 #include "sshkey.h" 41 #include "cipher.h" 42 #include "digest.h" 43 #include "kex.h" 44 #include "log.h" 45 #include "packet.h" 46 #include "dh.h" 47 #include "ssh2.h" 48 #include "compat.h" 49 #ifdef GSSAPI 50 #include "ssh-gss.h" 51 #endif 52 #include "monitor_wrap.h" 53 #include "dispatch.h" 54 #include "ssherr.h" 55 #include "sshbuf.h" 56 57 static int input_kex_dh_gex_request(int, u_int32_t, void *); 58 static int input_kex_dh_gex_init(int, u_int32_t, void *); 59 60 int 61 kexgex_server(struct ssh *ssh) 62 { 63 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD, 64 &input_kex_dh_gex_request); 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 } 70 71 static int 72 input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt) 73 { 74 struct ssh *ssh = ctxt; 75 struct kex *kex = ssh->kex; 76 int r; 77 u_int min = 0, max = 0, nbits = 0; 78 79 switch (type) { 80 case SSH2_MSG_KEX_DH_GEX_REQUEST: 81 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 82 if ((r = sshpkt_get_u32(ssh, &min)) != 0 || 83 (r = sshpkt_get_u32(ssh, &nbits)) != 0 || 84 (r = sshpkt_get_u32(ssh, &max)) != 0 || 85 (r = sshpkt_get_end(ssh)) != 0) 86 goto out; 87 kex->nbits = nbits; 88 kex->min = min; 89 kex->max = max; 90 min = MAX(DH_GRP_MIN, min); 91 max = MIN(DH_GRP_MAX, max); 92 nbits = MAX(DH_GRP_MIN, nbits); 93 nbits = MIN(DH_GRP_MAX, nbits); 94 break; 95 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: 96 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); 97 if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 || 98 (r = sshpkt_get_end(ssh)) != 0) 99 goto out; 100 kex->nbits = nbits; 101 /* unused for old GEX */ 102 kex->min = min = DH_GRP_MIN; 103 kex->max = max = DH_GRP_MAX; 104 break; 105 default: 106 r = SSH_ERR_INVALID_ARGUMENT; 107 goto out; 108 } 109 110 if (kex->max < kex->min || kex->nbits < kex->min || 111 kex->max < kex->nbits) { 112 r = SSH_ERR_DH_GEX_OUT_OF_RANGE; 113 goto out; 114 } 115 116 /* Contact privileged parent */ 117 kex->dh = PRIVSEP(choose_dh(min, nbits, max)); 118 if (kex->dh == NULL) { 119 sshpkt_disconnect(ssh, "no matching DH grp found"); 120 r = SSH_ERR_ALLOC_FAIL; 121 goto out; 122 } 123 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 124 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 || 125 (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 || 126 (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 || 127 (r = sshpkt_send(ssh)) != 0) 128 goto out; 129 130 /* Compute our exchange value in parallel with the client */ 131 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) 132 goto out; 133 134 /* old KEX does not use min/max in kexgex_hash() */ 135 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) 136 kex->min = kex->max = -1; 137 138 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 139 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init); 140 r = 0; 141 out: 142 return r; 143 } 144 145 static int 146 input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt) 147 { 148 struct ssh *ssh = ctxt; 149 struct kex *kex = ssh->kex; 150 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 151 struct sshkey *server_host_public, *server_host_private; 152 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; 153 u_char hash[SSH_DIGEST_MAX_LENGTH]; 154 size_t sbloblen, slen; 155 size_t klen = 0, hashlen; 156 int kout, r; 157 158 if (kex->load_host_public_key == NULL || 159 kex->load_host_private_key == NULL) { 160 r = SSH_ERR_INVALID_ARGUMENT; 161 goto out; 162 } 163 server_host_public = kex->load_host_public_key(kex->hostkey_type, 164 kex->hostkey_nid, ssh); 165 server_host_private = kex->load_host_private_key(kex->hostkey_type, 166 kex->hostkey_nid, ssh); 167 if (server_host_public == NULL) { 168 r = SSH_ERR_NO_HOSTKEY_LOADED; 169 goto out; 170 } 171 172 /* key, cert */ 173 if ((dh_client_pub = BN_new()) == NULL) { 174 r = SSH_ERR_ALLOC_FAIL; 175 goto out; 176 } 177 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 || 178 (r = sshpkt_get_end(ssh)) != 0) 179 goto out; 180 181 #ifdef DEBUG_KEXDH 182 fprintf(stderr, "dh_client_pub= "); 183 BN_print_fp(stderr, dh_client_pub); 184 fprintf(stderr, "\n"); 185 debug("bits %d", BN_num_bits(dh_client_pub)); 186 #endif 187 188 #ifdef DEBUG_KEXDH 189 DHparams_print_fp(stderr, kex->dh); 190 fprintf(stderr, "pub= "); 191 BN_print_fp(stderr, kex->dh->pub_key); 192 fprintf(stderr, "\n"); 193 #endif 194 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) { 195 sshpkt_disconnect(ssh, "bad client public DH value"); 196 r = SSH_ERR_MESSAGE_INCOMPLETE; 197 goto out; 198 } 199 200 klen = DH_size(kex->dh); 201 if ((kbuf = malloc(klen)) == NULL || 202 (shared_secret = BN_new()) == NULL) { 203 r = SSH_ERR_ALLOC_FAIL; 204 goto out; 205 } 206 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 || 207 BN_bin2bn(kbuf, kout, shared_secret) == NULL) { 208 r = SSH_ERR_LIBCRYPTO_ERROR; 209 goto out; 210 } 211 #ifdef DEBUG_KEXDH 212 dump_digest("shared secret", kbuf, kout); 213 #endif 214 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, 215 &sbloblen)) != 0) 216 goto out; 217 /* calc H */ 218 hashlen = sizeof(hash); 219 if ((r = kexgex_hash( 220 kex->hash_alg, 221 kex->client_version_string, 222 kex->server_version_string, 223 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), 224 sshbuf_ptr(kex->my), sshbuf_len(kex->my), 225 server_host_key_blob, sbloblen, 226 kex->min, kex->nbits, kex->max, 227 kex->dh->p, kex->dh->g, 228 dh_client_pub, 229 kex->dh->pub_key, 230 shared_secret, 231 hash, &hashlen)) != 0) 232 goto out; 233 234 /* save session id := H */ 235 if (kex->session_id == NULL) { 236 kex->session_id_len = hashlen; 237 kex->session_id = malloc(kex->session_id_len); 238 if (kex->session_id == NULL) { 239 r = SSH_ERR_ALLOC_FAIL; 240 goto out; 241 } 242 memcpy(kex->session_id, hash, kex->session_id_len); 243 } 244 245 /* sign H */ 246 if ((r = kex->sign(server_host_private, server_host_public, 247 &signature, &slen, hash, hashlen, ssh->compat)) < 0) 248 goto out; 249 250 /* destroy_sensitive_data(); */ 251 252 /* send server hostkey, DH pubkey 'f' and singed H */ 253 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 || 254 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || 255 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */ 256 (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 257 (r = sshpkt_send(ssh)) != 0) 258 goto out; 259 260 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) 261 r = kex_send_newkeys(ssh); 262 out: 263 DH_free(kex->dh); 264 kex->dh = NULL; 265 if (dh_client_pub) 266 BN_clear_free(dh_client_pub); 267 if (kbuf) { 268 explicit_bzero(kbuf, klen); 269 free(kbuf); 270 } 271 if (shared_secret) 272 BN_clear_free(shared_secret); 273 free(server_host_key_blob); 274 free(signature); 275 return r; 276 } 277 #endif /* WITH_OPENSSL */ 278