blob: d456820631416e23c1e0f5e67c5ba34365bdcaee [file] [log] [blame]
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +00001/* $OpenBSD: kexgexs.c,v 1.22 2015/01/20 07:55:33 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 Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
32
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100036#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037
Damien Miller4499f4c2010-11-20 15:15:49 +110038#include <openssl/dh.h>
39
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"
Damien Millerd7834352006-08-05 12:39:39 +100049#ifdef GSSAPI
50#include "ssh-gss.h"
51#endif
Damien Miller8e7fb332003-02-24 12:03:03 +110052#include "monitor_wrap.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000053#include "dispatch.h"
54#include "ssherr.h"
55#include "sshbuf.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110056
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000057static int input_kex_dh_gex_request(int, u_int32_t, void *);
58static int input_kex_dh_gex_init(int, u_int32_t, void *);
59
60int
61kexgex_server(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110062{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000063 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}
Damien Miller8e7fb332003-02-24 12:03:03 +110070
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000071static int
72input_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;
Damien Miller8e7fb332003-02-24 12:03:03 +110078
Damien Miller8e7fb332003-02-24 12:03:03 +110079 switch (type) {
80 case SSH2_MSG_KEX_DH_GEX_REQUEST:
81 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000082 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;
Damien Miller8e7fb332003-02-24 12:03:03 +110090 min = MAX(DH_GRP_MIN, min);
91 max = MIN(DH_GRP_MAX, max);
Damien Millerccf7e222009-01-28 16:23:06 +110092 nbits = MAX(DH_GRP_MIN, nbits);
93 nbits = MIN(DH_GRP_MAX, nbits);
Damien Miller8e7fb332003-02-24 12:03:03 +110094 break;
95 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
96 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000097 if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
98 (r = sshpkt_get_end(ssh)) != 0)
99 goto out;
100 kex->nbits = nbits;
Damien Miller8e7fb332003-02-24 12:03:03 +1100101 /* unused for old GEX */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000102 kex->min = min = DH_GRP_MIN;
103 kex->max = max = DH_GRP_MAX;
Damien Miller8e7fb332003-02-24 12:03:03 +1100104 break;
105 default:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000106 r = SSH_ERR_INVALID_ARGUMENT;
107 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100108 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100109
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000110 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 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100115
116 /* Contact privileged parent */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000117 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 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100123 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000124 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;
Damien Miller8e7fb332003-02-24 12:03:03 +1100129
130 /* Compute our exchange value in parallel with the client */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000131 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;
Damien Miller8e7fb332003-02-24 12:03:03 +1100137
138 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000139 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
145static int
146input_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 }
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +0000163 server_host_public = kex->load_host_public_key(kex->hostkey_type, ssh);
164 server_host_private = kex->load_host_private_key(kex->hostkey_type, ssh);
165 if (server_host_public == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000166 r = SSH_ERR_NO_HOSTKEY_LOADED;
167 goto out;
168 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100169
170 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000171 if ((dh_client_pub = BN_new()) == NULL) {
172 r = SSH_ERR_ALLOC_FAIL;
173 goto out;
174 }
175 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
176 (r = sshpkt_get_end(ssh)) != 0)
177 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100178
179#ifdef DEBUG_KEXDH
180 fprintf(stderr, "dh_client_pub= ");
181 BN_print_fp(stderr, dh_client_pub);
182 fprintf(stderr, "\n");
183 debug("bits %d", BN_num_bits(dh_client_pub));
184#endif
185
186#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000187 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100188 fprintf(stderr, "pub= ");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000189 BN_print_fp(stderr, kex->dh->pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100190 fprintf(stderr, "\n");
191#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000192 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
193 sshpkt_disconnect(ssh, "bad client public DH value");
194 r = SSH_ERR_MESSAGE_INCOMPLETE;
195 goto out;
196 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100197
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000198 klen = DH_size(kex->dh);
199 if ((kbuf = malloc(klen)) == NULL ||
200 (shared_secret = BN_new()) == NULL) {
201 r = SSH_ERR_ALLOC_FAIL;
202 goto out;
203 }
204 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
205 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
206 r = SSH_ERR_LIBCRYPTO_ERROR;
207 goto out;
208 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100209#ifdef DEBUG_KEXDH
210 dump_digest("shared secret", kbuf, kout);
211#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000212 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
213 &sbloblen)) != 0)
214 goto out;
Damien Miller19bb3a52005-11-05 15:19:35 +1100215 /* calc H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000216 hashlen = sizeof(hash);
217 if ((r = kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100218 kex->hash_alg,
Damien Miller8e7fb332003-02-24 12:03:03 +1100219 kex->client_version_string,
220 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000221 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
222 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Damien Miller8e7fb332003-02-24 12:03:03 +1100223 server_host_key_blob, sbloblen,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000224 kex->min, kex->nbits, kex->max,
225 kex->dh->p, kex->dh->g,
Damien Miller8e7fb332003-02-24 12:03:03 +1100226 dh_client_pub,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000227 kex->dh->pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100228 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000229 hash, &hashlen)) != 0)
230 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100231
232 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100233 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100234 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000235 kex->session_id = malloc(kex->session_id_len);
236 if (kex->session_id == NULL) {
237 r = SSH_ERR_ALLOC_FAIL;
238 goto out;
239 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100240 memcpy(kex->session_id, hash, kex->session_id_len);
241 }
242
243 /* sign H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000244 if ((r = kex->sign(server_host_private, server_host_public,
245 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
246 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100247
248 /* destroy_sensitive_data(); */
249
250 /* send server hostkey, DH pubkey 'f' and singed H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000251 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
252 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
253 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
254 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
255 (r = sshpkt_send(ssh)) != 0)
256 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100257
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000258 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
259 r = kex_send_newkeys(ssh);
260 out:
261 DH_free(kex->dh);
262 kex->dh = NULL;
263 if (dh_client_pub)
264 BN_clear_free(dh_client_pub);
265 if (kbuf) {
266 explicit_bzero(kbuf, klen);
267 free(kbuf);
268 }
269 if (shared_secret)
270 BN_clear_free(shared_secret);
Darren Tuckera627d422013-06-02 07:31:17 +1000271 free(server_host_key_blob);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000272 free(signature);
273 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100274}
Damien Miller72ef7c12015-01-15 02:21:31 +1100275#endif /* WITH_OPENSSL */