]> andersk Git - openssh.git/blame - kexgex.c
- markus@cvs.openbsd.org 2002/02/23 17:59:02
[openssh.git] / kexgex.c
CommitLineData
0bc35151 1/*
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"
b967d870 27RCSID("$OpenBSD: kexgex.c,v 1.18 2002/02/23 17:59:02 markus Exp $");
0bc35151 28
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
396c147e 42static u_char *
0bc35151 43kexgex_hash(
44 char *client_version_string,
45 char *server_version_string,
46 char *ckexinit, int ckexinitlen,
47 char *skexinit, int skexinitlen,
cf54363d 48 u_char *serverhostkeyblob, int sbloblen,
0bc35151 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);
449c5ba5 60 buffer_put_cstring(&b, client_version_string);
61 buffer_put_cstring(&b, server_version_string);
0bc35151 62
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);
cd332296 72 if (min == -1 || max == -1)
0bc35151 73 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
a209a158 95 dump_digest("hash", digest, EVP_MD_size(evp_md));
0bc35151 96#endif
97 return digest;
98}
99
100/* client */
101
396c147e 102static void
0bc35151 103kexgex_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;
54a5250f 110 int min, max, nbits;
0bc35151 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");
54a5250f 141 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
0bc35151 142
143 if ((p = BN_new()) == NULL)
144 fatal("BN_new");
20b279e6 145 packet_get_bignum2(p);
0bc35151 146 if ((g = BN_new()) == NULL)
147 fatal("BN_new");
20b279e6 148 packet_get_bignum2(g);
95500969 149 packet_check_eom();
0bc35151 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");
54a5250f 172 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
0bc35151 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");
215ced77 179 if (server_host_key->type != kex->hostkey_type)
180 fatal("type mismatch for decoded server_host_key_blob");
f49bc4f7 181 if (kex->verify_host_key == NULL)
182 fatal("cannot verify server_host_key");
183 if (kex->verify_host_key(server_host_key) == -1)
184 fatal("server_host_key verification failed");
0bc35151 185
186 /* DH paramter f, server public DH key */
b775c6f2 187 if ((dh_server_pub = BN_new()) == NULL)
0bc35151 188 fatal("dh_server_pub == NULL");
20b279e6 189 packet_get_bignum2(dh_server_pub);
0bc35151 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);
95500969 200 packet_check_eom();
0bc35151 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
cd332296 209 dump_digest("shared secret", kbuf, kout);
0bc35151 210#endif
b775c6f2 211 if ((shared_secret = BN_new()) == NULL)
212 fatal("kexgex_client: BN_new failed");
0bc35151 213 BN_bin2bn(kbuf, kout, shared_secret);
214 memset(kbuf, 0, klen);
215 xfree(kbuf);
216
217 if (datafellows & SSH_OLD_DHGEX)
218 min = max = -1;
219
220 /* calc and verify H */
221 hash = kexgex_hash(
222 kex->client_version_string,
223 kex->server_version_string,
224 buffer_ptr(&kex->my), buffer_len(&kex->my),
225 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
226 server_host_key_blob, sbloblen,
227 min, nbits, max,
228 dh->p, dh->g,
229 dh->pub_key,
230 dh_server_pub,
231 shared_secret
232 );
d8ee838b 233 /* have keys, free DH */
234 DH_free(dh);
0bc35151 235 xfree(server_host_key_blob);
108d362e 236 BN_clear_free(dh_server_pub);
0bc35151 237
cf54363d 238 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
0bc35151 239 fatal("key_verify failed for server_host_key");
240 key_free(server_host_key);
241 xfree(signature);
242
243 /* save session id */
244 if (kex->session_id == NULL) {
245 kex->session_id_len = 20;
246 kex->session_id = xmalloc(kex->session_id_len);
247 memcpy(kex->session_id, hash, kex->session_id_len);
248 }
0bc35151 249 kex_derive_keys(kex, hash, shared_secret);
250 BN_clear_free(shared_secret);
251
d8ee838b 252 kex_finish(kex);
0bc35151 253}
254
255/* server */
256
396c147e 257static void
0bc35151 258kexgex_server(Kex *kex)
259{
260 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
261 Key *server_host_key;
262 DH *dh = dh;
263 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
264 u_int sbloblen, klen, kout;
54a5250f 265 int min = -1, max = -1, nbits = -1, type, slen;
0bc35151 266
267 if (kex->load_host_key == NULL)
268 fatal("Cannot load hostkey");
269 server_host_key = kex->load_host_key(kex->hostkey_type);
270 if (server_host_key == NULL)
271 fatal("Unsupported hostkey type %d", kex->hostkey_type);
272
54a5250f 273 type = packet_read();
6aacefa7 274 switch (type) {
0bc35151 275 case SSH2_MSG_KEX_DH_GEX_REQUEST:
276 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
277 min = packet_get_int();
278 nbits = packet_get_int();
279 max = packet_get_int();
280 min = MAX(DH_GRP_MIN, min);
281 max = MIN(DH_GRP_MAX, max);
282 break;
283 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
284 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
285 nbits = packet_get_int();
286 min = DH_GRP_MIN;
287 max = DH_GRP_MAX;
288 /* unused for old GEX */
289 break;
290 default:
bbb4cc1b 291 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
0bc35151 292 }
95500969 293 packet_check_eom();
0bc35151 294
295 if (max < min || nbits < min || max < nbits)
296 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
297 min, nbits, max);
298
299 dh = choose_dh(min, nbits, max);
300 if (dh == NULL)
301 packet_disconnect("Protocol error: no matching DH grp found");
302
303 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
304 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
305 packet_put_bignum2(dh->p);
306 packet_put_bignum2(dh->g);
307 packet_send();
308
309 /* flush */
310 packet_write_wait();
311
312 /* Compute our exchange value in parallel with the client */
313 dh_gen_key(dh, kex->we_need * 8);
314
315 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
54a5250f 316 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
0bc35151 317
318 /* key, cert */
b775c6f2 319 if ((dh_client_pub = BN_new()) == NULL)
0bc35151 320 fatal("dh_client_pub == NULL");
20b279e6 321 packet_get_bignum2(dh_client_pub);
b967d870 322 packet_check_eom();
0bc35151 323
324#ifdef DEBUG_KEXDH
325 fprintf(stderr, "dh_client_pub= ");
326 BN_print_fp(stderr, dh_client_pub);
327 fprintf(stderr, "\n");
328 debug("bits %d", BN_num_bits(dh_client_pub));
329#endif
330
331#ifdef DEBUG_KEXDH
332 DHparams_print_fp(stderr, dh);
333 fprintf(stderr, "pub= ");
334 BN_print_fp(stderr, dh->pub_key);
335 fprintf(stderr, "\n");
336#endif
337 if (!dh_pub_is_valid(dh, dh_client_pub))
338 packet_disconnect("bad client public DH value");
339
340 klen = DH_size(dh);
341 kbuf = xmalloc(klen);
342 kout = DH_compute_key(kbuf, dh_client_pub, dh);
343#ifdef DEBUG_KEXDH
cd332296 344 dump_digest("shared secret", kbuf, kout);
0bc35151 345#endif
b775c6f2 346 if ((shared_secret = BN_new()) == NULL)
347 fatal("kexgex_server: BN_new failed");
0bc35151 348 BN_bin2bn(kbuf, kout, shared_secret);
349 memset(kbuf, 0, klen);
350 xfree(kbuf);
351
352 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
353
354 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
355 min = max = -1;
356
357 /* calc H */ /* XXX depends on 'kex' */
358 hash = kexgex_hash(
359 kex->client_version_string,
360 kex->server_version_string,
361 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
362 buffer_ptr(&kex->my), buffer_len(&kex->my),
cf54363d 363 server_host_key_blob, sbloblen,
0bc35151 364 min, nbits, max,
365 dh->p, dh->g,
366 dh_client_pub,
367 dh->pub_key,
368 shared_secret
369 );
108d362e 370 BN_clear_free(dh_client_pub);
0bc35151 371
372 /* save session id := H */
373 /* XXX hashlen depends on KEX */
374 if (kex->session_id == NULL) {
375 kex->session_id_len = 20;
376 kex->session_id = xmalloc(kex->session_id_len);
377 memcpy(kex->session_id, hash, kex->session_id_len);
378 }
379
380 /* sign H */
381 /* XXX hashlen depends on KEX */
382 key_sign(server_host_key, &signature, &slen, hash, 20);
383
384 /* destroy_sensitive_data(); */
385
386 /* send server hostkey, DH pubkey 'f' and singed H */
387 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
388 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
cf54363d 389 packet_put_string(server_host_key_blob, sbloblen);
0bc35151 390 packet_put_bignum2(dh->pub_key); /* f */
cf54363d 391 packet_put_string(signature, slen);
0bc35151 392 packet_send();
393 xfree(signature);
394 xfree(server_host_key_blob);
d8ee838b 395 /* have keys, free DH */
396 DH_free(dh);
0bc35151 397
398 kex_derive_keys(kex, hash, shared_secret);
399 BN_clear_free(shared_secret);
400
d8ee838b 401 kex_finish(kex);
0bc35151 402}
403
404void
405kexgex(Kex *kex)
406{
407 if (kex->server)
408 kexgex_server(kex);
409 else
410 kexgex_client(kex);
411}
This page took 0.138469 seconds and 5 git commands to generate.