2 * Copyright (c) 2000 Niels Provos. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 RCSID("$OpenBSD: kexgex.c,v 1.20 2002/02/28 15:46:33 markus Exp $");
29 #include <openssl/bn.h>
42 #include "monitor_wrap.h"
45 extern int use_privsep;
50 char *client_version_string,
51 char *server_version_string,
52 char *ckexinit, int ckexinitlen,
53 char *skexinit, int skexinitlen,
54 u_char *serverhostkeyblob, int sbloblen,
55 int min, int wantbits, int max, BIGNUM *prime, BIGNUM *gen,
56 BIGNUM *client_dh_pub,
57 BIGNUM *server_dh_pub,
58 BIGNUM *shared_secret)
61 static u_char digest[EVP_MAX_MD_SIZE];
62 const EVP_MD *evp_md = EVP_sha1();
66 buffer_put_cstring(&b, client_version_string);
67 buffer_put_cstring(&b, server_version_string);
69 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
70 buffer_put_int(&b, ckexinitlen+1);
71 buffer_put_char(&b, SSH2_MSG_KEXINIT);
72 buffer_append(&b, ckexinit, ckexinitlen);
73 buffer_put_int(&b, skexinitlen+1);
74 buffer_put_char(&b, SSH2_MSG_KEXINIT);
75 buffer_append(&b, skexinit, skexinitlen);
77 buffer_put_string(&b, serverhostkeyblob, sbloblen);
78 if (min == -1 || max == -1)
79 buffer_put_int(&b, wantbits);
81 buffer_put_int(&b, min);
82 buffer_put_int(&b, wantbits);
83 buffer_put_int(&b, max);
85 buffer_put_bignum2(&b, prime);
86 buffer_put_bignum2(&b, gen);
87 buffer_put_bignum2(&b, client_dh_pub);
88 buffer_put_bignum2(&b, server_dh_pub);
89 buffer_put_bignum2(&b, shared_secret);
94 EVP_DigestInit(&md, evp_md);
95 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
96 EVP_DigestFinal(&md, digest, NULL);
101 dump_digest("hash", digest, EVP_MD_size(evp_md));
109 kexgex_client(Kex *kex)
111 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
112 BIGNUM *p = NULL, *g = NULL;
113 Key *server_host_key;
114 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
115 u_int klen, kout, slen, sbloblen;
119 nbits = dh_estimate(kex->we_need * 8);
121 if (datafellows & SSH_OLD_DHGEX) {
122 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent");
124 /* Old GEX request */
125 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
126 packet_put_int(nbits);
130 debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent");
132 /* New GEX request */
135 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
137 packet_put_int(nbits);
141 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
146 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
147 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
149 if ((p = BN_new()) == NULL)
151 packet_get_bignum2(p);
152 if ((g = BN_new()) == NULL)
154 packet_get_bignum2(g);
157 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
158 fatal("DH_GEX group out of range: %d !< %d !< %d",
159 min, BN_num_bits(p), max);
161 dh = dh_new_group(g, p);
162 dh_gen_key(dh, kex->we_need * 8);
165 DHparams_print_fp(stderr, dh);
166 fprintf(stderr, "pub= ");
167 BN_print_fp(stderr, dh->pub_key);
168 fprintf(stderr, "\n");
171 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
172 /* generate and send 'e', client DH public key */
173 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
174 packet_put_bignum2(dh->pub_key);
177 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
178 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
181 server_host_key_blob = packet_get_string(&sbloblen);
182 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
183 if (server_host_key == NULL)
184 fatal("cannot decode server_host_key_blob");
185 if (server_host_key->type != kex->hostkey_type)
186 fatal("type mismatch for decoded server_host_key_blob");
187 if (kex->verify_host_key == NULL)
188 fatal("cannot verify server_host_key");
189 if (kex->verify_host_key(server_host_key) == -1)
190 fatal("server_host_key verification failed");
192 /* DH paramter f, server public DH key */
193 if ((dh_server_pub = BN_new()) == NULL)
194 fatal("dh_server_pub == NULL");
195 packet_get_bignum2(dh_server_pub);
198 fprintf(stderr, "dh_server_pub= ");
199 BN_print_fp(stderr, dh_server_pub);
200 fprintf(stderr, "\n");
201 debug("bits %d", BN_num_bits(dh_server_pub));
205 signature = packet_get_string(&slen);
208 if (!dh_pub_is_valid(dh, dh_server_pub))
209 packet_disconnect("bad server public DH value");
212 kbuf = xmalloc(klen);
213 kout = DH_compute_key(kbuf, dh_server_pub, dh);
215 dump_digest("shared secret", kbuf, kout);
217 if ((shared_secret = BN_new()) == NULL)
218 fatal("kexgex_client: BN_new failed");
219 BN_bin2bn(kbuf, kout, shared_secret);
220 memset(kbuf, 0, klen);
223 if (datafellows & SSH_OLD_DHGEX)
226 /* calc and verify H */
228 kex->client_version_string,
229 kex->server_version_string,
230 buffer_ptr(&kex->my), buffer_len(&kex->my),
231 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
232 server_host_key_blob, sbloblen,
239 /* have keys, free DH */
241 xfree(server_host_key_blob);
242 BN_clear_free(dh_server_pub);
244 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
245 fatal("key_verify failed for server_host_key");
246 key_free(server_host_key);
249 /* save session id */
250 if (kex->session_id == NULL) {
251 kex->session_id_len = 20;
252 kex->session_id = xmalloc(kex->session_id_len);
253 memcpy(kex->session_id, hash, kex->session_id_len);
255 kex_derive_keys(kex, hash, shared_secret);
256 BN_clear_free(shared_secret);
264 kexgex_server(Kex *kex)
266 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
267 Key *server_host_key;
269 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
270 u_int sbloblen, klen, kout, slen;
271 int min = -1, max = -1, nbits = -1, type;
273 if (kex->load_host_key == NULL)
274 fatal("Cannot load hostkey");
275 server_host_key = kex->load_host_key(kex->hostkey_type);
276 if (server_host_key == NULL)
277 fatal("Unsupported hostkey type %d", kex->hostkey_type);
279 type = packet_read();
281 case SSH2_MSG_KEX_DH_GEX_REQUEST:
282 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
283 min = packet_get_int();
284 nbits = packet_get_int();
285 max = packet_get_int();
286 min = MAX(DH_GRP_MIN, min);
287 max = MIN(DH_GRP_MAX, max);
289 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
290 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
291 nbits = packet_get_int();
294 /* unused for old GEX */
297 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
301 if (max < min || nbits < min || max < nbits)
302 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
305 /* Contact privileged parent */
307 dh = mm_choose_dh(mm_recvfd, min, nbits, max);
309 dh = choose_dh(min, nbits, max);
311 packet_disconnect("Protocol error: no matching DH grp found");
313 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
314 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
315 packet_put_bignum2(dh->p);
316 packet_put_bignum2(dh->g);
322 /* Compute our exchange value in parallel with the client */
323 dh_gen_key(dh, kex->we_need * 8);
325 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
326 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
329 if ((dh_client_pub = BN_new()) == NULL)
330 fatal("dh_client_pub == NULL");
331 packet_get_bignum2(dh_client_pub);
335 fprintf(stderr, "dh_client_pub= ");
336 BN_print_fp(stderr, dh_client_pub);
337 fprintf(stderr, "\n");
338 debug("bits %d", BN_num_bits(dh_client_pub));
342 DHparams_print_fp(stderr, dh);
343 fprintf(stderr, "pub= ");
344 BN_print_fp(stderr, dh->pub_key);
345 fprintf(stderr, "\n");
347 if (!dh_pub_is_valid(dh, dh_client_pub))
348 packet_disconnect("bad client public DH value");
351 kbuf = xmalloc(klen);
352 kout = DH_compute_key(kbuf, dh_client_pub, dh);
354 dump_digest("shared secret", kbuf, kout);
356 if ((shared_secret = BN_new()) == NULL)
357 fatal("kexgex_server: BN_new failed");
358 BN_bin2bn(kbuf, kout, shared_secret);
359 memset(kbuf, 0, klen);
362 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
364 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
367 /* calc H */ /* XXX depends on 'kex' */
369 kex->client_version_string,
370 kex->server_version_string,
371 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
372 buffer_ptr(&kex->my), buffer_len(&kex->my),
373 server_host_key_blob, sbloblen,
380 BN_clear_free(dh_client_pub);
382 /* save session id := H */
383 /* XXX hashlen depends on KEX */
384 if (kex->session_id == NULL) {
385 kex->session_id_len = 20;
386 kex->session_id = xmalloc(kex->session_id_len);
387 memcpy(kex->session_id, hash, kex->session_id_len);
391 /* XXX hashlen depends on KEX */
393 mm_key_sign(mm_recvfd, kex->host_key_index(server_host_key),
394 &signature, &slen, hash, 20);
396 key_sign(server_host_key, &signature, &slen, hash, 20);
398 /* destroy_sensitive_data(); */
400 /* send server hostkey, DH pubkey 'f' and singed H */
401 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
402 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
403 packet_put_string(server_host_key_blob, sbloblen);
404 packet_put_bignum2(dh->pub_key); /* f */
405 packet_put_string(signature, slen);
409 xfree(server_host_key_blob);
410 /* have keys, free DH */
413 kex_derive_keys(kex, hash, shared_secret);
414 BN_clear_free(shared_secret);