blob: 1bc6b7e15ac319654e36cc4ebac6e45e15fd9116 [file] [log] [blame]
Ben Lindstrom22b19b42001-04-04 17:39:19 +00001/*
2 * Copyright (c) 2000 Niels Provos. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Ben Lindstrombba81212001-06-25 05:01:22 +000027RCSID("$OpenBSD: kexgex.c,v 1.8 2001/06/23 15:12:19 itojun Exp $");
Ben Lindstrom22b19b42001-04-04 17:39:19 +000028
29#include <openssl/bn.h>
30
31#include "xmalloc.h"
32#include "buffer.h"
33#include "bufaux.h"
34#include "key.h"
35#include "kex.h"
36#include "log.h"
37#include "packet.h"
38#include "dh.h"
39#include "ssh2.h"
40#include "compat.h"
41
Ben Lindstrombba81212001-06-25 05:01:22 +000042static u_char *
Ben Lindstrom22b19b42001-04-04 17:39:19 +000043kexgex_hash(
44 char *client_version_string,
45 char *server_version_string,
46 char *ckexinit, int ckexinitlen,
47 char *skexinit, int skexinitlen,
48 char *serverhostkeyblob, int sbloblen,
49 int min, int wantbits, int max, BIGNUM *prime, BIGNUM *gen,
50 BIGNUM *client_dh_pub,
51 BIGNUM *server_dh_pub,
52 BIGNUM *shared_secret)
53{
54 Buffer b;
55 static u_char digest[EVP_MAX_MD_SIZE];
56 EVP_MD *evp_md = EVP_sha1();
57 EVP_MD_CTX md;
58
59 buffer_init(&b);
Ben Lindstrom664408d2001-06-09 01:42:01 +000060 buffer_put_cstring(&b, client_version_string);
61 buffer_put_cstring(&b, server_version_string);
Ben Lindstrom22b19b42001-04-04 17:39:19 +000062
63 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
64 buffer_put_int(&b, ckexinitlen+1);
65 buffer_put_char(&b, SSH2_MSG_KEXINIT);
66 buffer_append(&b, ckexinit, ckexinitlen);
67 buffer_put_int(&b, skexinitlen+1);
68 buffer_put_char(&b, SSH2_MSG_KEXINIT);
69 buffer_append(&b, skexinit, skexinitlen);
70
71 buffer_put_string(&b, serverhostkeyblob, sbloblen);
Ben Lindstroma3700052001-04-05 23:26:32 +000072 if (min == -1 || max == -1)
Ben Lindstrom22b19b42001-04-04 17:39:19 +000073 buffer_put_int(&b, wantbits);
74 else {
75 buffer_put_int(&b, min);
76 buffer_put_int(&b, wantbits);
77 buffer_put_int(&b, max);
78 }
79 buffer_put_bignum2(&b, prime);
80 buffer_put_bignum2(&b, gen);
81 buffer_put_bignum2(&b, client_dh_pub);
82 buffer_put_bignum2(&b, server_dh_pub);
83 buffer_put_bignum2(&b, shared_secret);
84
85#ifdef DEBUG_KEXDH
86 buffer_dump(&b);
87#endif
88 EVP_DigestInit(&md, evp_md);
89 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
90 EVP_DigestFinal(&md, digest, NULL);
91
92 buffer_free(&b);
93
94#ifdef DEBUG_KEXDH
95 dump_digest("hash", digest, evp_md->md_size);
96#endif
97 return digest;
98}
99
100/* client */
101
Ben Lindstrombba81212001-06-25 05:01:22 +0000102static void
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000103kexgex_client(Kex *kex)
104{
105 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
106 BIGNUM *p = NULL, *g = NULL;
107 Key *server_host_key;
108 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
109 u_int klen, kout, slen, sbloblen;
110 int dlen, plen, min, max, nbits;
111 DH *dh;
112
113 nbits = dh_estimate(kex->we_need * 8);
114
115 if (datafellows & SSH_OLD_DHGEX) {
116 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent");
117
118 /* Old GEX request */
119 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
120 packet_put_int(nbits);
121 min = DH_GRP_MIN;
122 max = DH_GRP_MAX;
123 } else {
124 debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent");
125
126 /* New GEX request */
127 min = DH_GRP_MIN;
128 max = DH_GRP_MAX;
129 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
130 packet_put_int(min);
131 packet_put_int(nbits);
132 packet_put_int(max);
133 }
134#ifdef DEBUG_KEXDH
135 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
136 min, nbits, max);
137#endif
138 packet_send();
139
140 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
141 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
142
143 if ((p = BN_new()) == NULL)
144 fatal("BN_new");
145 packet_get_bignum2(p, &dlen);
146 if ((g = BN_new()) == NULL)
147 fatal("BN_new");
148 packet_get_bignum2(g, &dlen);
149 packet_done();
150
151 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
152 fatal("DH_GEX group out of range: %d !< %d !< %d",
153 min, BN_num_bits(p), max);
154
155 dh = dh_new_group(g, p);
156 dh_gen_key(dh, kex->we_need * 8);
157
158#ifdef DEBUG_KEXDH
159 DHparams_print_fp(stderr, dh);
160 fprintf(stderr, "pub= ");
161 BN_print_fp(stderr, dh->pub_key);
162 fprintf(stderr, "\n");
163#endif
164
165 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
166 /* generate and send 'e', client DH public key */
167 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
168 packet_put_bignum2(dh->pub_key);
169 packet_send();
170
171 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
172 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
173
174 /* key, cert */
175 server_host_key_blob = packet_get_string(&sbloblen);
176 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
177 if (server_host_key == NULL)
178 fatal("cannot decode server_host_key_blob");
179
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000180 if (kex->verify_host_key == NULL)
181 fatal("cannot verify server_host_key");
182 if (kex->verify_host_key(server_host_key) == -1)
183 fatal("server_host_key verification failed");
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000184
185 /* DH paramter f, server public DH key */
186 dh_server_pub = BN_new();
187 if (dh_server_pub == NULL)
188 fatal("dh_server_pub == NULL");
189 packet_get_bignum2(dh_server_pub, &dlen);
190
191#ifdef DEBUG_KEXDH
192 fprintf(stderr, "dh_server_pub= ");
193 BN_print_fp(stderr, dh_server_pub);
194 fprintf(stderr, "\n");
195 debug("bits %d", BN_num_bits(dh_server_pub));
196#endif
197
198 /* signed H */
199 signature = packet_get_string(&slen);
200 packet_done();
201
202 if (!dh_pub_is_valid(dh, dh_server_pub))
203 packet_disconnect("bad server public DH value");
204
205 klen = DH_size(dh);
206 kbuf = xmalloc(klen);
207 kout = DH_compute_key(kbuf, dh_server_pub, dh);
208#ifdef DEBUG_KEXDH
Ben Lindstroma3700052001-04-05 23:26:32 +0000209 dump_digest("shared secret", kbuf, kout);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000210#endif
211 shared_secret = BN_new();
212 BN_bin2bn(kbuf, kout, shared_secret);
213 memset(kbuf, 0, klen);
214 xfree(kbuf);
215
216 if (datafellows & SSH_OLD_DHGEX)
217 min = max = -1;
218
219 /* calc and verify H */
220 hash = kexgex_hash(
221 kex->client_version_string,
222 kex->server_version_string,
223 buffer_ptr(&kex->my), buffer_len(&kex->my),
224 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
225 server_host_key_blob, sbloblen,
226 min, nbits, max,
227 dh->p, dh->g,
228 dh->pub_key,
229 dh_server_pub,
230 shared_secret
231 );
Ben Lindstrom238abf62001-04-04 17:52:53 +0000232 /* have keys, free DH */
233 DH_free(dh);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000234 xfree(server_host_key_blob);
235 BN_free(dh_server_pub);
236
237 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
238 fatal("key_verify failed for server_host_key");
239 key_free(server_host_key);
240 xfree(signature);
241
242 /* save session id */
243 if (kex->session_id == NULL) {
244 kex->session_id_len = 20;
245 kex->session_id = xmalloc(kex->session_id_len);
246 memcpy(kex->session_id, hash, kex->session_id_len);
247 }
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000248 kex_derive_keys(kex, hash, shared_secret);
249 BN_clear_free(shared_secret);
250
Ben Lindstrom238abf62001-04-04 17:52:53 +0000251 kex_finish(kex);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000252}
253
254/* server */
255
Ben Lindstrombba81212001-06-25 05:01:22 +0000256static void
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000257kexgex_server(Kex *kex)
258{
259 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
260 Key *server_host_key;
261 DH *dh = dh;
262 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
263 u_int sbloblen, klen, kout;
264 int min = -1, max = -1, nbits = -1, type, plen, dlen, slen;
265
266 if (kex->load_host_key == NULL)
267 fatal("Cannot load hostkey");
268 server_host_key = kex->load_host_key(kex->hostkey_type);
269 if (server_host_key == NULL)
270 fatal("Unsupported hostkey type %d", kex->hostkey_type);
271
272 type = packet_read(&plen);
273 switch(type){
274 case SSH2_MSG_KEX_DH_GEX_REQUEST:
275 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
276 min = packet_get_int();
277 nbits = packet_get_int();
278 max = packet_get_int();
279 min = MAX(DH_GRP_MIN, min);
280 max = MIN(DH_GRP_MAX, max);
281 break;
282 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
283 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
284 nbits = packet_get_int();
285 min = DH_GRP_MIN;
286 max = DH_GRP_MAX;
287 /* unused for old GEX */
288 break;
289 default:
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000290 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000291 }
292 packet_done();
293
294 if (max < min || nbits < min || max < nbits)
295 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
296 min, nbits, max);
297
298 dh = choose_dh(min, nbits, max);
299 if (dh == NULL)
300 packet_disconnect("Protocol error: no matching DH grp found");
301
302 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
303 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
304 packet_put_bignum2(dh->p);
305 packet_put_bignum2(dh->g);
306 packet_send();
307
308 /* flush */
309 packet_write_wait();
310
311 /* Compute our exchange value in parallel with the client */
312 dh_gen_key(dh, kex->we_need * 8);
313
314 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
315 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_INIT);
316
317 /* key, cert */
318 dh_client_pub = BN_new();
319 if (dh_client_pub == NULL)
320 fatal("dh_client_pub == NULL");
321 packet_get_bignum2(dh_client_pub, &dlen);
322
323#ifdef DEBUG_KEXDH
324 fprintf(stderr, "dh_client_pub= ");
325 BN_print_fp(stderr, dh_client_pub);
326 fprintf(stderr, "\n");
327 debug("bits %d", BN_num_bits(dh_client_pub));
328#endif
329
330#ifdef DEBUG_KEXDH
331 DHparams_print_fp(stderr, dh);
332 fprintf(stderr, "pub= ");
333 BN_print_fp(stderr, dh->pub_key);
334 fprintf(stderr, "\n");
335#endif
336 if (!dh_pub_is_valid(dh, dh_client_pub))
337 packet_disconnect("bad client public DH value");
338
339 klen = DH_size(dh);
340 kbuf = xmalloc(klen);
341 kout = DH_compute_key(kbuf, dh_client_pub, dh);
342#ifdef DEBUG_KEXDH
Ben Lindstroma3700052001-04-05 23:26:32 +0000343 dump_digest("shared secret", kbuf, kout);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000344#endif
345 shared_secret = BN_new();
346 BN_bin2bn(kbuf, kout, shared_secret);
347 memset(kbuf, 0, klen);
348 xfree(kbuf);
349
350 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
351
352 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
353 min = max = -1;
354
355 /* calc H */ /* XXX depends on 'kex' */
356 hash = kexgex_hash(
357 kex->client_version_string,
358 kex->server_version_string,
359 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
360 buffer_ptr(&kex->my), buffer_len(&kex->my),
361 (char *)server_host_key_blob, sbloblen,
362 min, nbits, max,
363 dh->p, dh->g,
364 dh_client_pub,
365 dh->pub_key,
366 shared_secret
367 );
368 BN_free(dh_client_pub);
369
370 /* save session id := H */
371 /* XXX hashlen depends on KEX */
372 if (kex->session_id == NULL) {
373 kex->session_id_len = 20;
374 kex->session_id = xmalloc(kex->session_id_len);
375 memcpy(kex->session_id, hash, kex->session_id_len);
376 }
377
378 /* sign H */
379 /* XXX hashlen depends on KEX */
380 key_sign(server_host_key, &signature, &slen, hash, 20);
381
382 /* destroy_sensitive_data(); */
383
384 /* send server hostkey, DH pubkey 'f' and singed H */
385 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
386 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
387 packet_put_string((char *)server_host_key_blob, sbloblen);
388 packet_put_bignum2(dh->pub_key); /* f */
389 packet_put_string((char *)signature, slen);
390 packet_send();
391 xfree(signature);
392 xfree(server_host_key_blob);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000393 /* have keys, free DH */
394 DH_free(dh);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000395
396 kex_derive_keys(kex, hash, shared_secret);
397 BN_clear_free(shared_secret);
398
Ben Lindstrom238abf62001-04-04 17:52:53 +0000399 kex_finish(kex);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000400}
401
402void
403kexgex(Kex *kex)
404{
405 if (kex->server)
406 kexgex_server(kex);
407 else
408 kexgex_client(kex);
409}