1 /* $OpenBSD: kexgexs.c,v 1.44 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 "includes.h" 28 29 #ifdef WITH_OPENSSL 30 31 32 #include <stdarg.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <signal.h> 36 37 #include <openssl/dh.h> 38 39 #include "openbsd-compat/openssl-compat.h" 40 41 #include "sshkey.h" 42 #include "cipher.h" 43 #include "digest.h" 44 #include "kex.h" 45 #include "log.h" 46 #include "packet.h" 47 #include "dh.h" 48 #include "ssh2.h" 49 #include "compat.h" 50 #ifdef GSSAPI 51 #include "ssh-gss.h" 52 #endif 53 #include "monitor_wrap.h" 54 #include "dispatch.h" 55 #include "ssherr.h" 56 #include "sshbuf.h" 57 #include "misc.h" 58 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 *); 61 62 int 63 kexgex_server(struct ssh *ssh) 64 { 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, struct ssh *ssh) 73 { 74 struct kex *kex = ssh->kex; 75 int r; 76 u_int min = 0, max = 0, nbits = 0; 77 const BIGNUM *dh_p, *dh_g; 78 79 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 80 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error); 81 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 = MAXIMUM(DH_GRP_MIN, min); 91 max = MINIMUM(DH_GRP_MAX, max); 92 nbits = MAXIMUM(DH_GRP_MIN, nbits); 93 nbits = MINIMUM(DH_GRP_MAX, nbits); 94 95 if (kex->max < kex->min || kex->nbits < kex->min || 96 kex->max < kex->nbits || kex->max < DH_GRP_MIN) { 97 r = SSH_ERR_DH_GEX_OUT_OF_RANGE; 98 goto out; 99 } 100 101 /* Contact privileged parent */ 102 kex->dh = PRIVSEP(choose_dh(min, nbits, max)); 103 if (kex->dh == NULL) { 104 sshpkt_disconnect(ssh, "no matching DH grp found"); 105 r = SSH_ERR_ALLOC_FAIL; 106 goto out; 107 } 108 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 109 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); 110 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 || 111 (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 || 112 (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 || 113 (r = sshpkt_send(ssh)) != 0) 114 goto out; 115 116 /* Compute our exchange value in parallel with the client */ 117 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) 118 goto out; 119 120 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 121 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init); 122 r = 0; 123 out: 124 return r; 125 } 126 127 static int 128 input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh) 129 { 130 struct kex *kex = ssh->kex; 131 BIGNUM *dh_client_pub = NULL; 132 const BIGNUM *pub_key, *dh_p, *dh_g; 133 struct sshbuf *shared_secret = NULL; 134 struct sshbuf *server_host_key_blob = NULL; 135 struct sshkey *server_host_public, *server_host_private; 136 u_char *signature = NULL; 137 u_char hash[SSH_DIGEST_MAX_LENGTH]; 138 size_t slen, hashlen; 139 int r; 140 141 debug("SSH2_MSG_KEX_DH_GEX_INIT received"); 142 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error); 143 144 if ((r = kex_load_hostkey(ssh, &server_host_private, 145 &server_host_public)) != 0) 146 goto out; 147 148 /* key, cert */ 149 if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 || 150 (r = sshpkt_get_end(ssh)) != 0) 151 goto out; 152 if ((shared_secret = sshbuf_new()) == NULL) { 153 r = SSH_ERR_ALLOC_FAIL; 154 goto out; 155 } 156 if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0) 157 goto out; 158 if ((server_host_key_blob = sshbuf_new()) == NULL) { 159 r = SSH_ERR_ALLOC_FAIL; 160 goto out; 161 } 162 if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0) 163 goto out; 164 165 /* calc H */ 166 DH_get0_key(kex->dh, &pub_key, NULL); 167 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); 168 hashlen = sizeof(hash); 169 if ((r = kexgex_hash( 170 kex->hash_alg, 171 kex->client_version, 172 kex->server_version, 173 kex->peer, 174 kex->my, 175 server_host_key_blob, 176 kex->min, kex->nbits, kex->max, 177 dh_p, dh_g, 178 dh_client_pub, 179 pub_key, 180 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), 181 hash, &hashlen)) != 0) 182 goto out; 183 184 /* sign H */ 185 if ((r = kex->sign(ssh, server_host_private, server_host_public, 186 &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0) 187 goto out; 188 189 /* send server hostkey, DH pubkey 'f' and signed H */ 190 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 || 191 (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 || 192 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */ 193 (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 194 (r = sshpkt_send(ssh)) != 0) 195 goto out; 196 197 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 198 (r = kex_send_newkeys(ssh)) != 0) 199 goto out; 200 201 /* retain copy of hostkey used at initial KEX */ 202 if (kex->initial_hostkey == NULL && 203 (r = sshkey_from_private(server_host_public, 204 &kex->initial_hostkey)) != 0) 205 goto out; 206 /* success */ 207 out: 208 explicit_bzero(hash, sizeof(hash)); 209 DH_free(kex->dh); 210 kex->dh = NULL; 211 BN_clear_free(dh_client_pub); 212 sshbuf_free(shared_secret); 213 sshbuf_free(server_host_key_blob); 214 free(signature); 215 return r; 216 } 217 #endif /* WITH_OPENSSL */ 218