blob: 955bc837cba830a37356768a6da4df29ca74b915 [file] [log] [blame]
jsing@openbsd.org7cd31632018-02-07 02:06:50 +00001/* $OpenBSD: kexgexc.c,v 1.27 2018/02/07 02:06:51 jsing Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
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"
Damien Miller8e7fb332003-02-24 12:03:03 +110028
Damien Miller72ef7c12015-01-15 02:21:31 +110029#ifdef WITH_OPENSSL
30
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/types.h>
32
Damien Miller4499f4c2010-11-20 15:15:49 +110033#include <openssl/dh.h>
34
Damien Millerded319c2006-09-01 15:38:36 +100035#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100036#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000040#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000042#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110043#include "kex.h"
44#include "log.h"
45#include "packet.h"
46#include "dh.h"
47#include "ssh2.h"
48#include "compat.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000049#include "dispatch.h"
50#include "ssherr.h"
51#include "sshbuf.h"
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000052#include "misc.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110053
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000054static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
55static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000056
57int
58kexgex_client(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110059{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000060 struct kex *kex = ssh->kex;
61 int r;
62 u_int nbits;
Damien Miller8e7fb332003-02-24 12:03:03 +110063
Damien Miller76eea4a2014-01-26 09:37:25 +110064 nbits = dh_estimate(kex->dh_need * 8);
Damien Miller8e7fb332003-02-24 12:03:03 +110065
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000066 kex->min = DH_GRP_MIN;
67 kex->max = DH_GRP_MAX;
68 kex->nbits = nbits;
dtucker@openbsd.orgb282fec2015-05-26 23:23:40 +000069 if (datafellows & SSH_BUG_DHGEX_LARGE)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000070 kex->nbits = MINIMUM(kex->nbits, 4096);
djm@openbsd.org318be282015-04-13 02:04:08 +000071 /* New GEX request */
72 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
73 (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
74 (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
75 (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
76 (r = sshpkt_send(ssh)) != 0)
77 goto out;
78 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
79 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110080#ifdef DEBUG_KEXDH
81 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000082 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110083#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000084 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
85 &input_kex_dh_gex_group);
86 r = 0;
87 out:
88 return r;
89}
Damien Miller8e7fb332003-02-24 12:03:03 +110090
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000091static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000092input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000093{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000094 struct kex *kex = ssh->kex;
95 BIGNUM *p = NULL, *g = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +000096 const BIGNUM *pub_key;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000097 int r, bits;
Damien Miller8e7fb332003-02-24 12:03:03 +110098
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000099 debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
Damien Miller8e7fb332003-02-24 12:03:03 +1100100
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000101 if ((p = BN_new()) == NULL ||
102 (g = BN_new()) == NULL) {
103 r = SSH_ERR_ALLOC_FAIL;
104 goto out;
105 }
106 if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
107 (r = sshpkt_get_bignum2(ssh, g)) != 0 ||
108 (r = sshpkt_get_end(ssh)) != 0)
109 goto out;
110 if ((bits = BN_num_bits(p)) < 0 ||
111 (u_int)bits < kex->min || (u_int)bits > kex->max) {
112 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
113 goto out;
114 }
115 if ((kex->dh = dh_new_group(g, p)) == NULL) {
116 r = SSH_ERR_ALLOC_FAIL;
117 goto out;
118 }
119 p = g = NULL; /* belong to kex->dh now */
Damien Miller8e7fb332003-02-24 12:03:03 +1100120
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000121 /* generate and send 'e', client DH public key */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000122 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
123 goto out;
124 DH_get0_key(kex->dh, &pub_key, NULL);
125 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
126 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000127 (r = sshpkt_send(ssh)) != 0)
128 goto out;
129 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
Damien Miller8e7fb332003-02-24 12:03:03 +1100130#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000131 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100132 fprintf(stderr, "pub= ");
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000133 BN_print_fp(stderr, pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100134 fprintf(stderr, "\n");
135#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000136 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
137 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
138 r = 0;
139out:
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000140 BN_clear_free(p);
141 BN_clear_free(g);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000142 return r;
143}
Damien Miller8e7fb332003-02-24 12:03:03 +1100144
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000145static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000146input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000147{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000148 struct kex *kex = ssh->kex;
149 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000150 const BIGNUM *pub_key, *dh_p, *dh_g;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000151 struct sshkey *server_host_key = NULL;
152 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
153 u_char hash[SSH_DIGEST_MAX_LENGTH];
154 size_t klen = 0, slen, sbloblen, hashlen;
155 int kout, r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100156
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000157 debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
158 if (kex->verify_host_key == NULL) {
159 r = SSH_ERR_INVALID_ARGUMENT;
160 goto out;
161 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100162 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000163 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
164 &sbloblen)) != 0 ||
165 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
166 &server_host_key)) != 0)
167 goto out;
djm@openbsd.org5104db72015-01-26 06:10:03 +0000168 if (server_host_key->type != kex->hostkey_type ||
169 (kex->hostkey_type == KEY_ECDSA &&
170 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
171 r = SSH_ERR_KEY_TYPE_MISMATCH;
172 goto out;
173 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000174 if (kex->verify_host_key(server_host_key, ssh) == -1) {
175 r = SSH_ERR_SIGNATURE_INVALID;
176 goto out;
177 }
Damien Millerad6b14d2006-06-13 13:00:41 +1000178 /* DH parameter f, server public DH key */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000179 if ((dh_server_pub = BN_new()) == NULL) {
180 r = SSH_ERR_ALLOC_FAIL;
181 goto out;
182 }
183 /* signed H */
184 if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
185 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
186 (r = sshpkt_get_end(ssh)) != 0)
187 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100188#ifdef DEBUG_KEXDH
189 fprintf(stderr, "dh_server_pub= ");
190 BN_print_fp(stderr, dh_server_pub);
191 fprintf(stderr, "\n");
192 debug("bits %d", BN_num_bits(dh_server_pub));
193#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000194 if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
195 sshpkt_disconnect(ssh, "bad server public DH value");
196 r = SSH_ERR_MESSAGE_INCOMPLETE;
197 goto out;
198 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100199
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000200 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_server_pub, kex->dh)) < 0 ||
207 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
208 r = SSH_ERR_LIBCRYPTO_ERROR;
209 goto out;
210 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100211#ifdef DEBUG_KEXDH
212 dump_digest("shared secret", kbuf, kout);
213#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000214 if (ssh->compat & SSH_OLD_DHGEX)
215 kex->min = kex->max = -1;
Damien Miller8e7fb332003-02-24 12:03:03 +1100216
217 /* calc and verify H */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000218 DH_get0_key(kex->dh, &pub_key, NULL);
219 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000220 hashlen = sizeof(hash);
221 if ((r = kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100222 kex->hash_alg,
Damien Miller8e7fb332003-02-24 12:03:03 +1100223 kex->client_version_string,
224 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000225 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
226 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Miller8e7fb332003-02-24 12:03:03 +1100227 server_host_key_blob, sbloblen,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000228 kex->min, kex->nbits, kex->max,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000229 dh_p, dh_g,
230 pub_key,
Damien Miller8e7fb332003-02-24 12:03:03 +1100231 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100232 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000233 hash, &hashlen)) != 0)
234 goto out;
Damien Miller19bb3a52005-11-05 15:19:35 +1100235
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000236 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000237 hashlen, kex->hostkey_alg, ssh->compat)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000238 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100239
240 /* save session id */
241 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100242 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000243 kex->session_id = malloc(kex->session_id_len);
244 if (kex->session_id == NULL) {
245 r = SSH_ERR_ALLOC_FAIL;
246 goto out;
247 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100248 memcpy(kex->session_id, hash, kex->session_id_len);
249 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100250
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000251 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
252 r = kex_send_newkeys(ssh);
253 out:
254 explicit_bzero(hash, sizeof(hash));
255 DH_free(kex->dh);
256 kex->dh = NULL;
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000257 BN_clear_free(dh_server_pub);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000258 if (kbuf) {
259 explicit_bzero(kbuf, klen);
260 free(kbuf);
261 }
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000262 BN_clear_free(shared_secret);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000263 sshkey_free(server_host_key);
264 free(server_host_key_blob);
265 free(signature);
266 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100267}
Damien Miller72ef7c12015-01-15 02:21:31 +1100268#endif /* WITH_OPENSSL */