2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: kexdh.c,v 1.17 2002/02/28 15:46:33 markus Exp $");
28 #include <openssl/crypto.h>
29 #include <openssl/bn.h>
41 #include "monitor_wrap.h"
44 extern int use_privsep;
49 char *client_version_string,
50 char *server_version_string,
51 char *ckexinit, int ckexinitlen,
52 char *skexinit, int skexinitlen,
53 u_char *serverhostkeyblob, int sbloblen,
54 BIGNUM *client_dh_pub,
55 BIGNUM *server_dh_pub,
56 BIGNUM *shared_secret)
59 static u_char digest[EVP_MAX_MD_SIZE];
60 const EVP_MD *evp_md = EVP_sha1();
64 buffer_put_cstring(&b, client_version_string);
65 buffer_put_cstring(&b, server_version_string);
67 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
68 buffer_put_int(&b, ckexinitlen+1);
69 buffer_put_char(&b, SSH2_MSG_KEXINIT);
70 buffer_append(&b, ckexinit, ckexinitlen);
71 buffer_put_int(&b, skexinitlen+1);
72 buffer_put_char(&b, SSH2_MSG_KEXINIT);
73 buffer_append(&b, skexinit, skexinitlen);
75 buffer_put_string(&b, serverhostkeyblob, sbloblen);
76 buffer_put_bignum2(&b, client_dh_pub);
77 buffer_put_bignum2(&b, server_dh_pub);
78 buffer_put_bignum2(&b, shared_secret);
83 EVP_DigestInit(&md, evp_md);
84 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
85 EVP_DigestFinal(&md, digest, NULL);
90 dump_digest("hash", digest, EVP_MD_size(evp_md));
98 kexdh_client(Kex *kex)
100 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
102 Key *server_host_key;
103 u_char *server_host_key_blob = NULL, *signature = NULL;
105 u_int klen, kout, slen, sbloblen;
107 /* generate and send 'e', client DH public key */
108 dh = dh_new_group1();
109 dh_gen_key(dh, kex->we_need * 8);
110 packet_start(SSH2_MSG_KEXDH_INIT);
111 packet_put_bignum2(dh->pub_key);
114 debug("sending SSH2_MSG_KEXDH_INIT");
116 DHparams_print_fp(stderr, dh);
117 fprintf(stderr, "pub= ");
118 BN_print_fp(stderr, dh->pub_key);
119 fprintf(stderr, "\n");
122 debug("expecting SSH2_MSG_KEXDH_REPLY");
123 packet_read_expect(SSH2_MSG_KEXDH_REPLY);
126 server_host_key_blob = packet_get_string(&sbloblen);
127 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
128 if (server_host_key == NULL)
129 fatal("cannot decode server_host_key_blob");
130 if (server_host_key->type != kex->hostkey_type)
131 fatal("type mismatch for decoded server_host_key_blob");
132 if (kex->verify_host_key == NULL)
133 fatal("cannot verify server_host_key");
134 if (kex->verify_host_key(server_host_key) == -1)
135 fatal("server_host_key verification failed");
137 /* DH paramter f, server public DH key */
138 if ((dh_server_pub = BN_new()) == NULL)
139 fatal("dh_server_pub == NULL");
140 packet_get_bignum2(dh_server_pub);
143 fprintf(stderr, "dh_server_pub= ");
144 BN_print_fp(stderr, dh_server_pub);
145 fprintf(stderr, "\n");
146 debug("bits %d", BN_num_bits(dh_server_pub));
150 signature = packet_get_string(&slen);
153 if (!dh_pub_is_valid(dh, dh_server_pub))
154 packet_disconnect("bad server public DH value");
157 kbuf = xmalloc(klen);
158 kout = DH_compute_key(kbuf, dh_server_pub, dh);
160 dump_digest("shared secret", kbuf, kout);
162 if ((shared_secret = BN_new()) == NULL)
163 fatal("kexdh_client: BN_new failed");
164 BN_bin2bn(kbuf, kout, shared_secret);
165 memset(kbuf, 0, klen);
168 /* calc and verify H */
170 kex->client_version_string,
171 kex->server_version_string,
172 buffer_ptr(&kex->my), buffer_len(&kex->my),
173 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
174 server_host_key_blob, sbloblen,
179 xfree(server_host_key_blob);
180 BN_clear_free(dh_server_pub);
183 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
184 fatal("key_verify failed for server_host_key");
185 key_free(server_host_key);
188 /* save session id */
189 if (kex->session_id == NULL) {
190 kex->session_id_len = 20;
191 kex->session_id = xmalloc(kex->session_id_len);
192 memcpy(kex->session_id, hash, kex->session_id_len);
195 kex_derive_keys(kex, hash, shared_secret);
196 BN_clear_free(shared_secret);
203 kexdh_server(Kex *kex)
205 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
207 Key *server_host_key;
208 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
209 u_int sbloblen, klen, kout;
212 /* generate server DH public key */
213 dh = dh_new_group1();
214 dh_gen_key(dh, kex->we_need * 8);
216 debug("expecting SSH2_MSG_KEXDH_INIT");
217 packet_read_expect(SSH2_MSG_KEXDH_INIT);
219 if (kex->load_host_key == NULL)
220 fatal("Cannot load hostkey");
221 server_host_key = kex->load_host_key(kex->hostkey_type);
222 if (server_host_key == NULL)
223 fatal("Unsupported hostkey type %d", kex->hostkey_type);
226 if ((dh_client_pub = BN_new()) == NULL)
227 fatal("dh_client_pub == NULL");
228 packet_get_bignum2(dh_client_pub);
232 fprintf(stderr, "dh_client_pub= ");
233 BN_print_fp(stderr, dh_client_pub);
234 fprintf(stderr, "\n");
235 debug("bits %d", BN_num_bits(dh_client_pub));
239 DHparams_print_fp(stderr, dh);
240 fprintf(stderr, "pub= ");
241 BN_print_fp(stderr, dh->pub_key);
242 fprintf(stderr, "\n");
244 if (!dh_pub_is_valid(dh, dh_client_pub))
245 packet_disconnect("bad client public DH value");
248 kbuf = xmalloc(klen);
249 kout = DH_compute_key(kbuf, dh_client_pub, dh);
251 dump_digest("shared secret", kbuf, kout);
253 if ((shared_secret = BN_new()) == NULL)
254 fatal("kexdh_server: BN_new failed");
255 BN_bin2bn(kbuf, kout, shared_secret);
256 memset(kbuf, 0, klen);
259 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
263 kex->client_version_string,
264 kex->server_version_string,
265 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
266 buffer_ptr(&kex->my), buffer_len(&kex->my),
267 server_host_key_blob, sbloblen,
272 BN_clear_free(dh_client_pub);
274 /* save session id := H */
275 /* XXX hashlen depends on KEX */
276 if (kex->session_id == NULL) {
277 kex->session_id_len = 20;
278 kex->session_id = xmalloc(kex->session_id_len);
279 memcpy(kex->session_id, hash, kex->session_id_len);
283 /* XXX hashlen depends on KEX */
285 mm_key_sign(mm_recvfd,
286 kex->host_key_index(server_host_key),
287 &signature, &slen, hash, 20);
289 key_sign(server_host_key, &signature, &slen, hash, 20);
291 /* destroy_sensitive_data(); */
293 /* send server hostkey, DH pubkey 'f' and singed H */
294 packet_start(SSH2_MSG_KEXDH_REPLY);
295 packet_put_string(server_host_key_blob, sbloblen);
296 packet_put_bignum2(dh->pub_key); /* f */
297 packet_put_string(signature, slen);
301 xfree(server_host_key_blob);
302 /* have keys, free DH */
305 kex_derive_keys(kex, hash, shared_secret);
306 BN_clear_free(shared_secret);