]> andersk Git - openssh.git/blob - kexgex.c
Import of Niels Provos' 20020312 ssh-complete.diff
[openssh.git] / kexgex.c
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"
27 RCSID("$OpenBSD: kexgex.c,v 1.20 2002/02/28 15:46:33 markus Exp $");
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 #include "monitor.h"
42 #include "monitor_wrap.h"
43
44 /* Imports */
45 extern int use_privsep;
46 extern int mm_recvfd;
47
48 static u_char *
49 kexgex_hash(
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)
59 {
60         Buffer b;
61         static u_char digest[EVP_MAX_MD_SIZE];
62         const EVP_MD *evp_md = EVP_sha1();
63         EVP_MD_CTX md;
64
65         buffer_init(&b);
66         buffer_put_cstring(&b, client_version_string);
67         buffer_put_cstring(&b, server_version_string);
68
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);
76
77         buffer_put_string(&b, serverhostkeyblob, sbloblen);
78         if (min == -1 || max == -1)
79                 buffer_put_int(&b, wantbits);
80         else {
81                 buffer_put_int(&b, min);
82                 buffer_put_int(&b, wantbits);
83                 buffer_put_int(&b, max);
84         }
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);
90
91 #ifdef DEBUG_KEXDH
92         buffer_dump(&b);
93 #endif
94         EVP_DigestInit(&md, evp_md);
95         EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
96         EVP_DigestFinal(&md, digest, NULL);
97
98         buffer_free(&b);
99
100 #ifdef DEBUG_KEXDH
101         dump_digest("hash", digest, EVP_MD_size(evp_md));
102 #endif
103         return digest;
104 }
105
106 /* client */
107
108 static void
109 kexgex_client(Kex *kex)
110 {
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;
116         int min, max, nbits;
117         DH *dh;
118
119         nbits = dh_estimate(kex->we_need * 8);
120
121         if (datafellows & SSH_OLD_DHGEX) {
122                 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD sent");
123
124                 /* Old GEX request */
125                 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
126                 packet_put_int(nbits);
127                 min = DH_GRP_MIN;
128                 max = DH_GRP_MAX;
129         } else {
130                 debug("SSH2_MSG_KEX_DH_GEX_REQUEST sent");
131
132                 /* New GEX request */
133                 min = DH_GRP_MIN;
134                 max = DH_GRP_MAX;
135                 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
136                 packet_put_int(min);
137                 packet_put_int(nbits);
138                 packet_put_int(max);
139         }
140 #ifdef DEBUG_KEXDH
141         fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
142             min, nbits, max);
143 #endif
144         packet_send();
145
146         debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
147         packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
148
149         if ((p = BN_new()) == NULL)
150                 fatal("BN_new");
151         packet_get_bignum2(p);
152         if ((g = BN_new()) == NULL)
153                 fatal("BN_new");
154         packet_get_bignum2(g);
155         packet_check_eom();
156
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);
160
161         dh = dh_new_group(g, p);
162         dh_gen_key(dh, kex->we_need * 8);
163
164 #ifdef DEBUG_KEXDH
165         DHparams_print_fp(stderr, dh);
166         fprintf(stderr, "pub= ");
167         BN_print_fp(stderr, dh->pub_key);
168         fprintf(stderr, "\n");
169 #endif
170
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);
175         packet_send();
176
177         debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
178         packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
179
180         /* key, cert */
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");
191
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);
196
197 #ifdef DEBUG_KEXDH
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));
202 #endif
203
204         /* signed H */
205         signature = packet_get_string(&slen);
206         packet_check_eom();
207
208         if (!dh_pub_is_valid(dh, dh_server_pub))
209                 packet_disconnect("bad server public DH value");
210
211         klen = DH_size(dh);
212         kbuf = xmalloc(klen);
213         kout = DH_compute_key(kbuf, dh_server_pub, dh);
214 #ifdef DEBUG_KEXDH
215         dump_digest("shared secret", kbuf, kout);
216 #endif
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);
221         xfree(kbuf);
222
223         if (datafellows & SSH_OLD_DHGEX)
224                 min = max = -1;
225
226         /* calc and verify H */
227         hash = kexgex_hash(
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,
233             min, nbits, max,
234             dh->p, dh->g,
235             dh->pub_key,
236             dh_server_pub,
237             shared_secret
238         );
239         /* have keys, free DH */
240         DH_free(dh);
241         xfree(server_host_key_blob);
242         BN_clear_free(dh_server_pub);
243
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);
247         xfree(signature);
248
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);
254         }
255         kex_derive_keys(kex, hash, shared_secret);
256         BN_clear_free(shared_secret);
257
258         kex_finish(kex);
259 }
260
261 /* server */
262
263 static void
264 kexgex_server(Kex *kex)
265 {
266         BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
267         Key *server_host_key;
268         DH *dh = dh;
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;
272
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);
278
279         type = packet_read();
280         switch (type) {
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);
288                 break;
289         case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
290                 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
291                 nbits = packet_get_int();
292                 min = DH_GRP_MIN;
293                 max = DH_GRP_MAX;
294                 /* unused for old GEX */
295                 break;
296         default:
297                 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
298         }
299         packet_check_eom();
300
301         if (max < min || nbits < min || max < nbits)
302                 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
303                     min, nbits, max);
304
305         /* Contact privileged parent */
306         if (use_privsep)
307                 dh = mm_choose_dh(mm_recvfd, min, nbits, max);
308         else
309                 dh = choose_dh(min, nbits, max);
310         if (dh == NULL)
311                 packet_disconnect("Protocol error: no matching DH grp found");
312
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);
317         packet_send();
318
319         /* flush */
320         packet_write_wait();
321
322         /* Compute our exchange value in parallel with the client */
323         dh_gen_key(dh, kex->we_need * 8);
324
325         debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
326         packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
327
328         /* key, cert */
329         if ((dh_client_pub = BN_new()) == NULL)
330                 fatal("dh_client_pub == NULL");
331         packet_get_bignum2(dh_client_pub);
332         packet_check_eom();
333
334 #ifdef DEBUG_KEXDH
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));
339 #endif
340
341 #ifdef DEBUG_KEXDH
342         DHparams_print_fp(stderr, dh);
343         fprintf(stderr, "pub= ");
344         BN_print_fp(stderr, dh->pub_key);
345         fprintf(stderr, "\n");
346 #endif
347         if (!dh_pub_is_valid(dh, dh_client_pub))
348                 packet_disconnect("bad client public DH value");
349
350         klen = DH_size(dh);
351         kbuf = xmalloc(klen);
352         kout = DH_compute_key(kbuf, dh_client_pub, dh);
353 #ifdef DEBUG_KEXDH
354         dump_digest("shared secret", kbuf, kout);
355 #endif
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);
360         xfree(kbuf);
361
362         key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
363
364         if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
365                 min = max = -1;
366
367         /* calc H */                    /* XXX depends on 'kex' */
368         hash = kexgex_hash(
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,
374             min, nbits, max,
375             dh->p, dh->g,
376             dh_client_pub,
377             dh->pub_key,
378             shared_secret
379         );
380         BN_clear_free(dh_client_pub);
381
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);
388         }
389
390         /* sign H */
391         /* XXX hashlen depends on KEX */
392         if (use_privsep)
393                 mm_key_sign(mm_recvfd, kex->host_key_index(server_host_key),
394                     &signature, &slen, hash, 20);
395         else
396                 key_sign(server_host_key, &signature, &slen, hash, 20);
397
398         /* destroy_sensitive_data(); */
399
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);
406         packet_send();
407
408         xfree(signature);
409         xfree(server_host_key_blob);
410         /* have keys, free DH */
411         DH_free(dh);
412
413         kex_derive_keys(kex, hash, shared_secret);
414         BN_clear_free(shared_secret);
415
416         kex_finish(kex);
417 }
418
419 void
420 kexgex(Kex *kex)
421 {
422         if (kex->server)
423                 kexgex_server(kex);
424         else
425                 kexgex_client(kex);
426 }
This page took 1.531464 seconds and 5 git commands to generate.