blob: 9c281d28853c7ae9ebe5c95883031dba789515be [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: kexgexs.c,v 1.24 2015/01/26 06:10:03 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
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
Adam Langleyd0592972015-03-30 14:49:51 -070029#ifdef WITH_OPENSSL
30
31#include <sys/param.h> /* MIN MAX */
Greg Hartmanbd77cf72015-02-25 13:21:06 -080032
33#include <stdarg.h>
34#include <stdio.h>
35#include <string.h>
36#include <signal.h>
37
38#include <openssl/dh.h>
39
Adam Langleyd0592972015-03-30 14:49:51 -070040#include "sshkey.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080041#include "cipher.h"
Adam Langleyd0592972015-03-30 14:49:51 -070042#include "digest.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080043#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"
Adam Langleyd0592972015-03-30 14:49:51 -070053#include "dispatch.h"
54#include "ssherr.h"
55#include "sshbuf.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080056
Adam Langleyd0592972015-03-30 14:49:51 -070057static 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)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080062{
Adam Langleyd0592972015-03-30 14:49:51 -070063 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}
Greg Hartmanbd77cf72015-02-25 13:21:06 -080070
Adam Langleyd0592972015-03-30 14:49:51 -070071static 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080078
Greg Hartmanbd77cf72015-02-25 13:21:06 -080079 switch (type) {
80 case SSH2_MSG_KEX_DH_GEX_REQUEST:
81 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
Adam Langleyd0592972015-03-30 14:49:51 -070082 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080090 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");
Adam Langleyd0592972015-03-30 14:49:51 -070097 if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
98 (r = sshpkt_get_end(ssh)) != 0)
99 goto out;
100 kex->nbits = nbits;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800101 /* unused for old GEX */
Adam Langleyd0592972015-03-30 14:49:51 -0700102 kex->min = min = DH_GRP_MIN;
103 kex->max = max = DH_GRP_MAX;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800104 break;
105 default:
Adam Langleyd0592972015-03-30 14:49:51 -0700106 r = SSH_ERR_INVALID_ARGUMENT;
107 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800108 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800109
Adam Langleyd0592972015-03-30 14:49:51 -0700110 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 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800115
116 /* Contact privileged parent */
Adam Langleyd0592972015-03-30 14:49:51 -0700117 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 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800123 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
Adam Langleyd0592972015-03-30 14:49:51 -0700124 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800129
130 /* Compute our exchange value in parallel with the client */
Adam Langleyd0592972015-03-30 14:49:51 -0700131 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800137
138 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
Adam Langleyd0592972015-03-30 14:49:51 -0700139 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 }
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 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800171
172 /* key, cert */
Adam Langleyd0592972015-03-30 14:49:51 -0700173 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800180
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
Adam Langleyd0592972015-03-30 14:49:51 -0700189 DHparams_print_fp(stderr, kex->dh);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800190 fprintf(stderr, "pub= ");
Adam Langleyd0592972015-03-30 14:49:51 -0700191 BN_print_fp(stderr, kex->dh->pub_key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800192 fprintf(stderr, "\n");
193#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700194 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 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800199
Adam Langleyd0592972015-03-30 14:49:51 -0700200 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 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800211#ifdef DEBUG_KEXDH
212 dump_digest("shared secret", kbuf, kout);
213#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700214 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
215 &sbloblen)) != 0)
216 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800217 /* calc H */
Adam Langleyd0592972015-03-30 14:49:51 -0700218 hashlen = sizeof(hash);
219 if ((r = kexgex_hash(
220 kex->hash_alg,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800221 kex->client_version_string,
222 kex->server_version_string,
Adam Langleyd0592972015-03-30 14:49:51 -0700223 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
224 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800225 server_host_key_blob, sbloblen,
Adam Langleyd0592972015-03-30 14:49:51 -0700226 kex->min, kex->nbits, kex->max,
227 kex->dh->p, kex->dh->g,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800228 dh_client_pub,
Adam Langleyd0592972015-03-30 14:49:51 -0700229 kex->dh->pub_key,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800230 shared_secret,
Adam Langleyd0592972015-03-30 14:49:51 -0700231 hash, &hashlen)) != 0)
232 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800233
234 /* save session id := H */
235 if (kex->session_id == NULL) {
236 kex->session_id_len = hashlen;
Adam Langleyd0592972015-03-30 14:49:51 -0700237 kex->session_id = malloc(kex->session_id_len);
238 if (kex->session_id == NULL) {
239 r = SSH_ERR_ALLOC_FAIL;
240 goto out;
241 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800242 memcpy(kex->session_id, hash, kex->session_id_len);
243 }
244
245 /* sign H */
Adam Langleyd0592972015-03-30 14:49:51 -0700246 if ((r = kex->sign(server_host_private, server_host_public,
247 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
248 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800249
250 /* destroy_sensitive_data(); */
251
252 /* send server hostkey, DH pubkey 'f' and singed H */
Adam Langleyd0592972015-03-30 14:49:51 -0700253 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800259
Adam Langleyd0592972015-03-30 14:49:51 -0700260 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800276}
Adam Langleyd0592972015-03-30 14:49:51 -0700277#endif /* WITH_OPENSSL */