blob: 60d13a8b9a06c846fc147ec96b1544d79056586c [file] [log] [blame]
Ben Lindstrom22b19b42001-04-04 17:39:19 +00001/*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Damien Miller3672e4b2002-02-05 11:54:07 +110026RCSID("$OpenBSD: kexdh.c,v 1.13 2002/01/25 22:07:40 markus Exp $");
Ben Lindstrom22b19b42001-04-04 17:39:19 +000027
28#include <openssl/crypto.h>
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
Ben Lindstrombba81212001-06-25 05:01:22 +000041static u_char *
Ben Lindstrom22b19b42001-04-04 17:39:19 +000042kex_dh_hash(
43 char *client_version_string,
44 char *server_version_string,
45 char *ckexinit, int ckexinitlen,
46 char *skexinit, int skexinitlen,
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +000047 u_char *serverhostkeyblob, int sbloblen,
Ben Lindstrom22b19b42001-04-04 17:39:19 +000048 BIGNUM *client_dh_pub,
49 BIGNUM *server_dh_pub,
50 BIGNUM *shared_secret)
51{
52 Buffer b;
53 static u_char digest[EVP_MAX_MD_SIZE];
54 EVP_MD *evp_md = EVP_sha1();
55 EVP_MD_CTX md;
56
57 buffer_init(&b);
Ben Lindstrom664408d2001-06-09 01:42:01 +000058 buffer_put_cstring(&b, client_version_string);
59 buffer_put_cstring(&b, server_version_string);
Ben Lindstrom22b19b42001-04-04 17:39:19 +000060
61 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
62 buffer_put_int(&b, ckexinitlen+1);
63 buffer_put_char(&b, SSH2_MSG_KEXINIT);
64 buffer_append(&b, ckexinit, ckexinitlen);
65 buffer_put_int(&b, skexinitlen+1);
66 buffer_put_char(&b, SSH2_MSG_KEXINIT);
67 buffer_append(&b, skexinit, skexinitlen);
68
69 buffer_put_string(&b, serverhostkeyblob, sbloblen);
70 buffer_put_bignum2(&b, client_dh_pub);
71 buffer_put_bignum2(&b, server_dh_pub);
72 buffer_put_bignum2(&b, shared_secret);
73
74#ifdef DEBUG_KEX
75 buffer_dump(&b);
76#endif
77 EVP_DigestInit(&md, evp_md);
78 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
79 EVP_DigestFinal(&md, digest, NULL);
80
81 buffer_free(&b);
82
83#ifdef DEBUG_KEX
Damien Miller3672e4b2002-02-05 11:54:07 +110084 dump_digest("hash", digest, EVP_MD_size(evp_md));
Ben Lindstrom22b19b42001-04-04 17:39:19 +000085#endif
86 return digest;
87}
88
89/* client */
90
Ben Lindstrombba81212001-06-25 05:01:22 +000091static void
Ben Lindstrom22b19b42001-04-04 17:39:19 +000092kexdh_client(Kex *kex)
93{
94 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
95 DH *dh;
96 Key *server_host_key;
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +000097 u_char *server_host_key_blob = NULL, *signature = NULL;
Ben Lindstrom22b19b42001-04-04 17:39:19 +000098 u_char *kbuf, *hash;
99 u_int klen, kout, slen, sbloblen;
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000100
101 /* generate and send 'e', client DH public key */
102 dh = dh_new_group1();
103 dh_gen_key(dh, kex->we_need * 8);
104 packet_start(SSH2_MSG_KEXDH_INIT);
105 packet_put_bignum2(dh->pub_key);
106 packet_send();
107
108 debug("sending SSH2_MSG_KEXDH_INIT");
109#ifdef DEBUG_KEXDH
110 DHparams_print_fp(stderr, dh);
111 fprintf(stderr, "pub= ");
112 BN_print_fp(stderr, dh->pub_key);
113 fprintf(stderr, "\n");
114#endif
115
116 debug("expecting SSH2_MSG_KEXDH_REPLY");
Damien Millerdff50992002-01-22 23:16:32 +1100117 packet_read_expect(SSH2_MSG_KEXDH_REPLY);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000118
119 /* key, cert */
120 server_host_key_blob = packet_get_string(&sbloblen);
121 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
122 if (server_host_key == NULL)
123 fatal("cannot decode server_host_key_blob");
124
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000125 if (kex->verify_host_key == NULL)
126 fatal("cannot verify server_host_key");
127 if (kex->verify_host_key(server_host_key) == -1)
128 fatal("server_host_key verification failed");
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000129
130 /* DH paramter f, server public DH key */
Damien Millerda755162002-01-22 23:09:22 +1100131 if ((dh_server_pub = BN_new()) == NULL)
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000132 fatal("dh_server_pub == NULL");
Damien Millerd432ccf2002-01-22 23:14:44 +1100133 packet_get_bignum2(dh_server_pub);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000134
135#ifdef DEBUG_KEXDH
136 fprintf(stderr, "dh_server_pub= ");
137 BN_print_fp(stderr, dh_server_pub);
138 fprintf(stderr, "\n");
139 debug("bits %d", BN_num_bits(dh_server_pub));
140#endif
141
142 /* signed H */
143 signature = packet_get_string(&slen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100144 packet_check_eom();
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000145
146 if (!dh_pub_is_valid(dh, dh_server_pub))
147 packet_disconnect("bad server public DH value");
148
149 klen = DH_size(dh);
150 kbuf = xmalloc(klen);
151 kout = DH_compute_key(kbuf, dh_server_pub, dh);
152#ifdef DEBUG_KEXDH
153 dump_digest("shared secret", kbuf, kout);
154#endif
Damien Millerda755162002-01-22 23:09:22 +1100155 if ((shared_secret = BN_new()) == NULL)
156 fatal("kexdh_client: BN_new failed");
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000157 BN_bin2bn(kbuf, kout, shared_secret);
158 memset(kbuf, 0, klen);
159 xfree(kbuf);
160
161 /* calc and verify H */
162 hash = kex_dh_hash(
163 kex->client_version_string,
164 kex->server_version_string,
165 buffer_ptr(&kex->my), buffer_len(&kex->my),
166 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
167 server_host_key_blob, sbloblen,
168 dh->pub_key,
169 dh_server_pub,
170 shared_secret
171 );
172 xfree(server_host_key_blob);
Damien Miller9ef95dd2002-01-22 23:10:33 +1100173 BN_clear_free(dh_server_pub);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000174 DH_free(dh);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000175
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000176 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000177 fatal("key_verify failed for server_host_key");
178 key_free(server_host_key);
179 xfree(signature);
180
181 /* save session id */
182 if (kex->session_id == NULL) {
183 kex->session_id_len = 20;
184 kex->session_id = xmalloc(kex->session_id_len);
185 memcpy(kex->session_id, hash, kex->session_id_len);
186 }
187
188 kex_derive_keys(kex, hash, shared_secret);
189 BN_clear_free(shared_secret);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000190 kex_finish(kex);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000191}
192
193/* server */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000196kexdh_server(Kex *kex)
197{
198 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
199 DH *dh;
200 Key *server_host_key;
201 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
202 u_int sbloblen, klen, kout;
Damien Millerdff50992002-01-22 23:16:32 +1100203 int slen;
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000204
205 /* generate server DH public key */
206 dh = dh_new_group1();
207 dh_gen_key(dh, kex->we_need * 8);
208
209 debug("expecting SSH2_MSG_KEXDH_INIT");
Damien Millerdff50992002-01-22 23:16:32 +1100210 packet_read_expect(SSH2_MSG_KEXDH_INIT);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000211
212 if (kex->load_host_key == NULL)
213 fatal("Cannot load hostkey");
214 server_host_key = kex->load_host_key(kex->hostkey_type);
215 if (server_host_key == NULL)
216 fatal("Unsupported hostkey type %d", kex->hostkey_type);
217
218 /* key, cert */
Damien Millerda755162002-01-22 23:09:22 +1100219 if ((dh_client_pub = BN_new()) == NULL)
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000220 fatal("dh_client_pub == NULL");
Damien Millerd432ccf2002-01-22 23:14:44 +1100221 packet_get_bignum2(dh_client_pub);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000222
223#ifdef DEBUG_KEXDH
224 fprintf(stderr, "dh_client_pub= ");
225 BN_print_fp(stderr, dh_client_pub);
226 fprintf(stderr, "\n");
227 debug("bits %d", BN_num_bits(dh_client_pub));
228#endif
229
230#ifdef DEBUG_KEXDH
231 DHparams_print_fp(stderr, dh);
232 fprintf(stderr, "pub= ");
233 BN_print_fp(stderr, dh->pub_key);
234 fprintf(stderr, "\n");
235#endif
236 if (!dh_pub_is_valid(dh, dh_client_pub))
237 packet_disconnect("bad client public DH value");
238
239 klen = DH_size(dh);
240 kbuf = xmalloc(klen);
241 kout = DH_compute_key(kbuf, dh_client_pub, dh);
242#ifdef DEBUG_KEXDH
243 dump_digest("shared secret", kbuf, kout);
244#endif
Damien Millerda755162002-01-22 23:09:22 +1100245 if ((shared_secret = BN_new()) == NULL)
246 fatal("kexdh_server: BN_new failed");
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000247 BN_bin2bn(kbuf, kout, shared_secret);
248 memset(kbuf, 0, klen);
249 xfree(kbuf);
250
251 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
252
253 /* calc H */
254 hash = kex_dh_hash(
255 kex->client_version_string,
256 kex->server_version_string,
257 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
258 buffer_ptr(&kex->my), buffer_len(&kex->my),
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000259 server_host_key_blob, sbloblen,
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000260 dh_client_pub,
261 dh->pub_key,
262 shared_secret
263 );
Damien Miller9ef95dd2002-01-22 23:10:33 +1100264 BN_clear_free(dh_client_pub);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000265
266 /* save session id := H */
267 /* XXX hashlen depends on KEX */
268 if (kex->session_id == NULL) {
269 kex->session_id_len = 20;
270 kex->session_id = xmalloc(kex->session_id_len);
271 memcpy(kex->session_id, hash, kex->session_id_len);
272 }
273
274 /* sign H */
275 /* XXX hashlen depends on KEX */
276 key_sign(server_host_key, &signature, &slen, hash, 20);
277
278 /* destroy_sensitive_data(); */
279
280 /* send server hostkey, DH pubkey 'f' and singed H */
281 packet_start(SSH2_MSG_KEXDH_REPLY);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000282 packet_put_string(server_host_key_blob, sbloblen);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000283 packet_put_bignum2(dh->pub_key); /* f */
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000284 packet_put_string(signature, slen);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000285 packet_send();
Ben Lindstrom238abf62001-04-04 17:52:53 +0000286
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000287 xfree(signature);
288 xfree(server_host_key_blob);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000289 /* have keys, free DH */
290 DH_free(dh);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000291
292 kex_derive_keys(kex, hash, shared_secret);
293 BN_clear_free(shared_secret);
Ben Lindstrom238abf62001-04-04 17:52:53 +0000294 kex_finish(kex);
Ben Lindstrom22b19b42001-04-04 17:39:19 +0000295}
296
297void
298kexdh(Kex *kex)
299{
300 if (kex->server)
301 kexdh_server(kex);
302 else
303 kexdh_client(kex);
304}