blob: 600d91acc2dd97b86738b407f047efac66f522f9 [file] [log] [blame]
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +00001/* $OpenBSD: kexgexc.c,v 1.32 2019/01/21 10:03:37 djm 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
Damien Miller48f54b92018-09-13 12:13:50 +100040#include "openbsd-compat/openssl-compat.h"
41
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000042#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000044#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110045#include "kex.h"
46#include "log.h"
47#include "packet.h"
48#include "dh.h"
49#include "ssh2.h"
50#include "compat.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000051#include "dispatch.h"
52#include "ssherr.h"
53#include "sshbuf.h"
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000054#include "misc.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110055
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000056static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
57static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000058
59int
60kexgex_client(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110061{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000062 struct kex *kex = ssh->kex;
63 int r;
64 u_int nbits;
Damien Miller8e7fb332003-02-24 12:03:03 +110065
Damien Miller76eea4a2014-01-26 09:37:25 +110066 nbits = dh_estimate(kex->dh_need * 8);
Damien Miller8e7fb332003-02-24 12:03:03 +110067
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000068 kex->min = DH_GRP_MIN;
69 kex->max = DH_GRP_MAX;
70 kex->nbits = nbits;
dtucker@openbsd.orgb282fec2015-05-26 23:23:40 +000071 if (datafellows & SSH_BUG_DHGEX_LARGE)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000072 kex->nbits = MINIMUM(kex->nbits, 4096);
djm@openbsd.org318be282015-04-13 02:04:08 +000073 /* New GEX request */
74 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
75 (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
76 (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
77 (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
78 (r = sshpkt_send(ssh)) != 0)
79 goto out;
80 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
81 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110082#ifdef DEBUG_KEXDH
83 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000084 kex->min, kex->nbits, kex->max);
Damien Miller8e7fb332003-02-24 12:03:03 +110085#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000086 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
87 &input_kex_dh_gex_group);
88 r = 0;
89 out:
90 return r;
91}
Damien Miller8e7fb332003-02-24 12:03:03 +110092
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000093static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000094input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000095{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000096 struct kex *kex = ssh->kex;
97 BIGNUM *p = NULL, *g = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +000098 const BIGNUM *pub_key;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000099 int r, bits;
Damien Miller8e7fb332003-02-24 12:03:03 +1100100
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000101 debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
Damien Miller8e7fb332003-02-24 12:03:03 +1100102
djm@openbsd.org7be85722019-01-21 09:54:11 +0000103 if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
104 (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000105 (r = sshpkt_get_end(ssh)) != 0)
106 goto out;
107 if ((bits = BN_num_bits(p)) < 0 ||
108 (u_int)bits < kex->min || (u_int)bits > kex->max) {
109 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
110 goto out;
111 }
112 if ((kex->dh = dh_new_group(g, p)) == NULL) {
113 r = SSH_ERR_ALLOC_FAIL;
114 goto out;
115 }
116 p = g = NULL; /* belong to kex->dh now */
Damien Miller8e7fb332003-02-24 12:03:03 +1100117
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000118 /* generate and send 'e', client DH public key */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000119 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
120 goto out;
121 DH_get0_key(kex->dh, &pub_key, NULL);
122 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
123 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000124 (r = sshpkt_send(ssh)) != 0)
125 goto out;
126 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
Damien Miller8e7fb332003-02-24 12:03:03 +1100127#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000128 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100129 fprintf(stderr, "pub= ");
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000130 BN_print_fp(stderr, pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100131 fprintf(stderr, "\n");
132#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000133 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
134 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
135 r = 0;
136out:
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000137 BN_clear_free(p);
138 BN_clear_free(g);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000139 return r;
140}
Damien Miller8e7fb332003-02-24 12:03:03 +1100141
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000142static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000143input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000144{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000145 struct kex *kex = ssh->kex;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000146 BIGNUM *dh_server_pub = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000147 const BIGNUM *pub_key, *dh_p, *dh_g;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000148 struct sshbuf *shared_secret = NULL;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000149 struct sshkey *server_host_key = NULL;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000150 u_char *signature = NULL, *server_host_key_blob = NULL;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000151 u_char hash[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000152 size_t slen, sbloblen, hashlen;
153 int r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100154
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000155 debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
156 if (kex->verify_host_key == NULL) {
157 r = SSH_ERR_INVALID_ARGUMENT;
158 goto out;
159 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100160 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000161 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
162 &sbloblen)) != 0 ||
163 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
164 &server_host_key)) != 0)
165 goto out;
djm@openbsd.org5104db72015-01-26 06:10:03 +0000166 if (server_host_key->type != kex->hostkey_type ||
167 (kex->hostkey_type == KEY_ECDSA &&
168 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
169 r = SSH_ERR_KEY_TYPE_MISMATCH;
170 goto out;
171 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000172 if (kex->verify_host_key(server_host_key, ssh) == -1) {
173 r = SSH_ERR_SIGNATURE_INVALID;
174 goto out;
175 }
djm@openbsd.org7be85722019-01-21 09:54:11 +0000176 /* DH parameter f, server public DH key, signed H */
177 if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000178 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
179 (r = sshpkt_get_end(ssh)) != 0)
180 goto out;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000181 if ((shared_secret = sshbuf_new()) == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000182 r = SSH_ERR_ALLOC_FAIL;
183 goto out;
184 }
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000185 if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000186 goto out;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000187 if (ssh->compat & SSH_OLD_DHGEX)
188 kex->min = kex->max = -1;
Damien Miller8e7fb332003-02-24 12:03:03 +1100189
190 /* calc and verify H */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000191 DH_get0_key(kex->dh, &pub_key, NULL);
192 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000193 hashlen = sizeof(hash);
194 if ((r = kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100195 kex->hash_alg,
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000196 kex->client_version,
197 kex->server_version,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000198 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
199 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Miller8e7fb332003-02-24 12:03:03 +1100200 server_host_key_blob, sbloblen,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000201 kex->min, kex->nbits, kex->max,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000202 dh_p, dh_g,
203 pub_key,
Damien Miller8e7fb332003-02-24 12:03:03 +1100204 dh_server_pub,
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000205 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000206 hash, &hashlen)) != 0)
207 goto out;
Damien Miller19bb3a52005-11-05 15:19:35 +1100208
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000209 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000210 hashlen, kex->hostkey_alg, ssh->compat)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000211 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100212
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000213 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000214 r = kex_send_newkeys(ssh);
215 out:
216 explicit_bzero(hash, sizeof(hash));
217 DH_free(kex->dh);
218 kex->dh = NULL;
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000219 BN_clear_free(dh_server_pub);
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000220 sshbuf_free(shared_secret);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000221 sshkey_free(server_host_key);
222 free(server_host_key_blob);
223 free(signature);
224 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100225}
Damien Miller72ef7c12015-01-15 02:21:31 +1100226#endif /* WITH_OPENSSL */