]>
Commit | Line | Data |
---|---|---|
30460aeb | 1 | /* $OpenBSD: kex.c,v 1.76 2006/08/03 03:34:42 deraadt Exp $ */ |
3c0ef626 | 2 | /* |
3 | * Copyright (c) 2000, 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" | |
30460aeb | 27 | |
28 | #include <sys/param.h> | |
29 | ||
30 | #include <signal.h> | |
31 | #include <stdarg.h> | |
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
3c0ef626 | 35 | |
36 | #include <openssl/crypto.h> | |
37 | ||
3c0ef626 | 38 | #include "xmalloc.h" |
30460aeb | 39 | #include "ssh2.h" |
3c0ef626 | 40 | #include "buffer.h" |
3c0ef626 | 41 | #include "packet.h" |
42 | #include "compat.h" | |
43 | #include "cipher.h" | |
3c0ef626 | 44 | #include "key.h" |
30460aeb | 45 | #include "kex.h" |
3c0ef626 | 46 | #include "log.h" |
47 | #include "mac.h" | |
48 | #include "match.h" | |
49 | #include "dispatch.h" | |
350391c5 | 50 | #include "monitor.h" |
3c0ef626 | 51 | |
5598e598 | 52 | #ifdef GSSAPI |
53 | #include "ssh-gss.h" | |
54 | #endif | |
55 | ||
3c0ef626 | 56 | #define KEX_COOKIE_LEN 16 |
57 | ||
30460aeb | 58 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
59 | # if defined(HAVE_EVP_SHA256) | |
60 | # define evp_ssh_sha256 EVP_sha256 | |
61 | # else | |
62 | extern const EVP_MD *evp_ssh_sha256(void); | |
63 | # endif | |
64 | #endif | |
65 | ||
3c0ef626 | 66 | /* prototype */ |
67 | static void kex_kexinit_finish(Kex *); | |
68 | static void kex_choose_conf(Kex *); | |
69 | ||
70 | /* put algorithm proposal into buffer */ | |
473db5ab | 71 | void |
3c0ef626 | 72 | kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX]) |
73 | { | |
2ce0bfe4 | 74 | u_int i; |
3c0ef626 | 75 | |
76 | buffer_clear(b); | |
350391c5 | 77 | /* |
78 | * add a dummy cookie, the cookie will be overwritten by | |
79 | * kex_send_kexinit(), each time a kexinit is set | |
80 | */ | |
81 | for (i = 0; i < KEX_COOKIE_LEN; i++) | |
82 | buffer_put_char(b, 0); | |
3c0ef626 | 83 | for (i = 0; i < PROPOSAL_MAX; i++) |
84 | buffer_put_cstring(b, proposal[i]); | |
85 | buffer_put_char(b, 0); /* first_kex_packet_follows */ | |
86 | buffer_put_int(b, 0); /* uint32 reserved */ | |
87 | } | |
88 | ||
89 | /* parse buffer and return algorithm proposal */ | |
90 | static char ** | |
8afc6a66 | 91 | kex_buf2prop(Buffer *raw, int *first_kex_follows) |
3c0ef626 | 92 | { |
93 | Buffer b; | |
94 | int i; | |
95 | char **proposal; | |
96 | ||
30460aeb | 97 | proposal = xcalloc(PROPOSAL_MAX, sizeof(char *)); |
3c0ef626 | 98 | |
99 | buffer_init(&b); | |
100 | buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); | |
101 | /* skip cookie */ | |
102 | for (i = 0; i < KEX_COOKIE_LEN; i++) | |
103 | buffer_get_char(&b); | |
104 | /* extract kex init proposal strings */ | |
105 | for (i = 0; i < PROPOSAL_MAX; i++) { | |
106 | proposal[i] = buffer_get_string(&b,NULL); | |
107 | debug2("kex_parse_kexinit: %s", proposal[i]); | |
108 | } | |
109 | /* first kex follows / reserved */ | |
110 | i = buffer_get_char(&b); | |
8afc6a66 | 111 | if (first_kex_follows != NULL) |
112 | *first_kex_follows = i; | |
3c0ef626 | 113 | debug2("kex_parse_kexinit: first_kex_follows %d ", i); |
114 | i = buffer_get_int(&b); | |
115 | debug2("kex_parse_kexinit: reserved %d ", i); | |
116 | buffer_free(&b); | |
117 | return proposal; | |
118 | } | |
119 | ||
120 | static void | |
121 | kex_prop_free(char **proposal) | |
122 | { | |
2ce0bfe4 | 123 | u_int i; |
3c0ef626 | 124 | |
125 | for (i = 0; i < PROPOSAL_MAX; i++) | |
126 | xfree(proposal[i]); | |
127 | xfree(proposal); | |
128 | } | |
129 | ||
130 | static void | |
e9702f7d | 131 | kex_protocol_error(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 132 | { |
e9702f7d | 133 | error("Hm, kex protocol error: type %d seq %u", type, seq); |
3c0ef626 | 134 | } |
135 | ||
136 | static void | |
e9702f7d | 137 | kex_reset_dispatch(void) |
3c0ef626 | 138 | { |
e9702f7d | 139 | dispatch_range(SSH2_MSG_TRANSPORT_MIN, |
140 | SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); | |
141 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); | |
3c0ef626 | 142 | } |
143 | ||
144 | void | |
145 | kex_finish(Kex *kex) | |
146 | { | |
e9702f7d | 147 | kex_reset_dispatch(); |
3c0ef626 | 148 | |
149 | packet_start(SSH2_MSG_NEWKEYS); | |
150 | packet_send(); | |
151 | /* packet_write_wait(); */ | |
152 | debug("SSH2_MSG_NEWKEYS sent"); | |
153 | ||
8afc6a66 | 154 | debug("expecting SSH2_MSG_NEWKEYS"); |
e9702f7d | 155 | packet_read_expect(SSH2_MSG_NEWKEYS); |
156 | packet_check_eom(); | |
3c0ef626 | 157 | debug("SSH2_MSG_NEWKEYS received"); |
158 | ||
159 | kex->done = 1; | |
160 | buffer_clear(&kex->peer); | |
161 | /* buffer_clear(&kex->my); */ | |
162 | kex->flags &= ~KEX_INIT_SENT; | |
163 | xfree(kex->name); | |
164 | kex->name = NULL; | |
165 | } | |
166 | ||
167 | void | |
168 | kex_send_kexinit(Kex *kex) | |
169 | { | |
7e82606e | 170 | u_int32_t rnd = 0; |
350391c5 | 171 | u_char *cookie; |
2ce0bfe4 | 172 | u_int i; |
350391c5 | 173 | |
3c0ef626 | 174 | if (kex == NULL) { |
175 | error("kex_send_kexinit: no kex, cannot rekey"); | |
176 | return; | |
177 | } | |
178 | if (kex->flags & KEX_INIT_SENT) { | |
179 | debug("KEX_INIT_SENT"); | |
180 | return; | |
181 | } | |
182 | kex->done = 0; | |
350391c5 | 183 | |
184 | /* generate a random cookie */ | |
185 | if (buffer_len(&kex->my) < KEX_COOKIE_LEN) | |
186 | fatal("kex_send_kexinit: kex proposal too short"); | |
187 | cookie = buffer_ptr(&kex->my); | |
188 | for (i = 0; i < KEX_COOKIE_LEN; i++) { | |
189 | if (i % 4 == 0) | |
7e82606e | 190 | rnd = arc4random(); |
191 | cookie[i] = rnd; | |
192 | rnd >>= 8; | |
350391c5 | 193 | } |
3c0ef626 | 194 | packet_start(SSH2_MSG_KEXINIT); |
195 | packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my)); | |
196 | packet_send(); | |
197 | debug("SSH2_MSG_KEXINIT sent"); | |
198 | kex->flags |= KEX_INIT_SENT; | |
199 | } | |
200 | ||
201 | void | |
e9702f7d | 202 | kex_input_kexinit(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 203 | { |
204 | char *ptr; | |
2ce0bfe4 | 205 | u_int i, dlen; |
3c0ef626 | 206 | Kex *kex = (Kex *)ctxt; |
207 | ||
208 | debug("SSH2_MSG_KEXINIT received"); | |
209 | if (kex == NULL) | |
210 | fatal("kex_input_kexinit: no kex, cannot rekey"); | |
211 | ||
212 | ptr = packet_get_raw(&dlen); | |
213 | buffer_append(&kex->peer, ptr, dlen); | |
214 | ||
215 | /* discard packet */ | |
216 | for (i = 0; i < KEX_COOKIE_LEN; i++) | |
217 | packet_get_char(); | |
218 | for (i = 0; i < PROPOSAL_MAX; i++) | |
219 | xfree(packet_get_string(NULL)); | |
276b07a3 | 220 | (void) packet_get_char(); |
221 | (void) packet_get_int(); | |
e9702f7d | 222 | packet_check_eom(); |
3c0ef626 | 223 | |
224 | kex_kexinit_finish(kex); | |
225 | } | |
226 | ||
227 | Kex * | |
228 | kex_setup(char *proposal[PROPOSAL_MAX]) | |
229 | { | |
230 | Kex *kex; | |
231 | ||
30460aeb | 232 | kex = xcalloc(1, sizeof(*kex)); |
3c0ef626 | 233 | buffer_init(&kex->peer); |
234 | buffer_init(&kex->my); | |
235 | kex_prop2buf(&kex->my, proposal); | |
236 | kex->done = 0; | |
237 | ||
238 | kex_send_kexinit(kex); /* we start */ | |
e9702f7d | 239 | kex_reset_dispatch(); |
3c0ef626 | 240 | |
241 | return kex; | |
242 | } | |
243 | ||
244 | static void | |
245 | kex_kexinit_finish(Kex *kex) | |
246 | { | |
247 | if (!(kex->flags & KEX_INIT_SENT)) | |
248 | kex_send_kexinit(kex); | |
249 | ||
250 | kex_choose_conf(kex); | |
251 | ||
8afc6a66 | 252 | if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX && |
253 | kex->kex[kex->kex_type] != NULL) { | |
254 | (kex->kex[kex->kex_type])(kex); | |
255 | } else { | |
3c0ef626 | 256 | fatal("Unsupported key exchange %d", kex->kex_type); |
257 | } | |
258 | } | |
259 | ||
260 | static void | |
261 | choose_enc(Enc *enc, char *client, char *server) | |
262 | { | |
263 | char *name = match_list(client, server, NULL); | |
264 | if (name == NULL) | |
265 | fatal("no matching cipher found: client %s server %s", client, server); | |
e9702f7d | 266 | if ((enc->cipher = cipher_by_name(name)) == NULL) |
3c0ef626 | 267 | fatal("matching cipher is not supported: %s", name); |
268 | enc->name = name; | |
269 | enc->enabled = 0; | |
270 | enc->iv = NULL; | |
271 | enc->key = NULL; | |
e9702f7d | 272 | enc->key_len = cipher_keylen(enc->cipher); |
273 | enc->block_size = cipher_blocksize(enc->cipher); | |
3c0ef626 | 274 | } |
30460aeb | 275 | |
3c0ef626 | 276 | static void |
277 | choose_mac(Mac *mac, char *client, char *server) | |
278 | { | |
279 | char *name = match_list(client, server, NULL); | |
280 | if (name == NULL) | |
281 | fatal("no matching mac found: client %s server %s", client, server); | |
282 | if (mac_init(mac, name) < 0) | |
283 | fatal("unsupported mac %s", name); | |
284 | /* truncate the key */ | |
285 | if (datafellows & SSH_BUG_HMAC) | |
286 | mac->key_len = 16; | |
287 | mac->name = name; | |
288 | mac->key = NULL; | |
289 | mac->enabled = 0; | |
290 | } | |
30460aeb | 291 | |
3c0ef626 | 292 | static void |
293 | choose_comp(Comp *comp, char *client, char *server) | |
294 | { | |
295 | char *name = match_list(client, server, NULL); | |
296 | if (name == NULL) | |
297 | fatal("no matching comp found: client %s server %s", client, server); | |
2ce0bfe4 | 298 | if (strcmp(name, "zlib@openssh.com") == 0) { |
299 | comp->type = COMP_DELAYED; | |
300 | } else if (strcmp(name, "zlib") == 0) { | |
301 | comp->type = COMP_ZLIB; | |
3c0ef626 | 302 | } else if (strcmp(name, "none") == 0) { |
2ce0bfe4 | 303 | comp->type = COMP_NONE; |
3c0ef626 | 304 | } else { |
305 | fatal("unsupported comp %s", name); | |
306 | } | |
307 | comp->name = name; | |
308 | } | |
30460aeb | 309 | |
3c0ef626 | 310 | static void |
311 | choose_kex(Kex *k, char *client, char *server) | |
312 | { | |
313 | k->name = match_list(client, server, NULL); | |
314 | if (k->name == NULL) | |
7cac2b65 | 315 | fatal("no kex alg"); |
3c0ef626 | 316 | if (strcmp(k->name, KEX_DH1) == 0) { |
8afc6a66 | 317 | k->kex_type = KEX_DH_GRP1_SHA1; |
08822d99 | 318 | k->evp_md = EVP_sha1(); |
7e82606e | 319 | } else if (strcmp(k->name, KEX_DH14) == 0) { |
320 | k->kex_type = KEX_DH_GRP14_SHA1; | |
08822d99 | 321 | k->evp_md = EVP_sha1(); |
322 | } else if (strcmp(k->name, KEX_DHGEX_SHA1) == 0) { | |
8afc6a66 | 323 | k->kex_type = KEX_DH_GEX_SHA1; |
08822d99 | 324 | k->evp_md = EVP_sha1(); |
f713db99 | 325 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
326 | } else if (strcmp(k->name, KEX_DHGEX_SHA256) == 0) { | |
327 | k->kex_type = KEX_DH_GEX_SHA256; | |
328 | k->evp_md = evp_ssh_sha256(); | |
329 | #endif | |
5598e598 | 330 | #ifdef GSSAPI |
fe4ad273 | 331 | } else if (strncmp(k->name, KEX_GSS_GEX_SHA1_ID, |
f713db99 | 332 | sizeof(KEX_GSS_GEX_SHA1_ID) - 1) == 0) { |
fe4ad273 | 333 | k->kex_type = KEX_GSS_GEX_SHA1; |
08822d99 | 334 | k->evp_md = EVP_sha1(); |
c06c9ae8 | 335 | } else if (strncmp(k->name, KEX_GSS_GRP1_SHA1_ID, |
f713db99 | 336 | sizeof(KEX_GSS_GRP1_SHA1_ID) - 1) == 0) { |
8afc6a66 | 337 | k->kex_type = KEX_GSS_GRP1_SHA1; |
08822d99 | 338 | k->evp_md = EVP_sha1(); |
f713db99 | 339 | } else if (strncmp(k->name, KEX_GSS_GRP14_SHA1_ID, |
340 | sizeof(KEX_GSS_GRP14_SHA1_ID) - 1) == 0) { | |
341 | k->kex_type = KEX_GSS_GRP14_SHA1; | |
342 | k->evp_md = EVP_sha1(); | |
5598e598 | 343 | #endif |
3c0ef626 | 344 | } else |
345 | fatal("bad kex alg %s", k->name); | |
346 | } | |
08822d99 | 347 | |
3c0ef626 | 348 | static void |
349 | choose_hostkeyalg(Kex *k, char *client, char *server) | |
350 | { | |
351 | char *hostkeyalg = match_list(client, server, NULL); | |
352 | if (hostkeyalg == NULL) | |
353 | fatal("no hostkey alg"); | |
354 | k->hostkey_type = key_type_from_name(hostkeyalg); | |
355 | if (k->hostkey_type == KEY_UNSPEC) | |
356 | fatal("bad hostkey alg '%s'", hostkeyalg); | |
357 | xfree(hostkeyalg); | |
358 | } | |
359 | ||
540d72c3 | 360 | static int |
8afc6a66 | 361 | proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) |
362 | { | |
363 | static int check[] = { | |
364 | PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 | |
365 | }; | |
366 | int *idx; | |
367 | char *p; | |
368 | ||
369 | for (idx = &check[0]; *idx != -1; idx++) { | |
370 | if ((p = strchr(my[*idx], ',')) != NULL) | |
371 | *p = '\0'; | |
372 | if ((p = strchr(peer[*idx], ',')) != NULL) | |
373 | *p = '\0'; | |
374 | if (strcmp(my[*idx], peer[*idx]) != 0) { | |
375 | debug2("proposal mismatch: my %s peer %s", | |
376 | my[*idx], peer[*idx]); | |
377 | return (0); | |
378 | } | |
379 | } | |
380 | debug2("proposals match"); | |
381 | return (1); | |
382 | } | |
383 | ||
3c0ef626 | 384 | static void |
385 | kex_choose_conf(Kex *kex) | |
386 | { | |
387 | Newkeys *newkeys; | |
388 | char **my, **peer; | |
389 | char **cprop, **sprop; | |
390 | int nenc, nmac, ncomp; | |
2ce0bfe4 | 391 | u_int mode, ctos, need; |
8afc6a66 | 392 | int first_kex_follows, type; |
3c0ef626 | 393 | |
8afc6a66 | 394 | my = kex_buf2prop(&kex->my, NULL); |
395 | peer = kex_buf2prop(&kex->peer, &first_kex_follows); | |
3c0ef626 | 396 | |
397 | if (kex->server) { | |
398 | cprop=peer; | |
399 | sprop=my; | |
400 | } else { | |
401 | cprop=my; | |
402 | sprop=peer; | |
403 | } | |
404 | ||
405 | /* Algorithm Negotiation */ | |
406 | for (mode = 0; mode < MODE_MAX; mode++) { | |
30460aeb | 407 | newkeys = xcalloc(1, sizeof(*newkeys)); |
3c0ef626 | 408 | kex->newkeys[mode] = newkeys; |
409 | ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); | |
410 | nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; | |
411 | nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; | |
412 | ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; | |
413 | choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]); | |
414 | choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]); | |
415 | choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]); | |
416 | debug("kex: %s %s %s %s", | |
417 | ctos ? "client->server" : "server->client", | |
418 | newkeys->enc.name, | |
419 | newkeys->mac.name, | |
420 | newkeys->comp.name); | |
421 | } | |
422 | choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]); | |
423 | choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], | |
424 | sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]); | |
425 | need = 0; | |
426 | for (mode = 0; mode < MODE_MAX; mode++) { | |
427 | newkeys = kex->newkeys[mode]; | |
e9702f7d | 428 | if (need < newkeys->enc.key_len) |
429 | need = newkeys->enc.key_len; | |
430 | if (need < newkeys->enc.block_size) | |
431 | need = newkeys->enc.block_size; | |
3c0ef626 | 432 | if (need < newkeys->mac.key_len) |
433 | need = newkeys->mac.key_len; | |
434 | } | |
435 | /* XXX need runden? */ | |
436 | kex->we_need = need; | |
437 | ||
8afc6a66 | 438 | /* ignore the next message if the proposals do not match */ |
540d72c3 | 439 | if (first_kex_follows && !proposals_match(my, peer) && |
2ce0bfe4 | 440 | !(datafellows & SSH_BUG_FIRSTKEX)) { |
8afc6a66 | 441 | type = packet_read(); |
442 | debug2("skipping next packet (type %u)", type); | |
443 | } | |
444 | ||
3c0ef626 | 445 | kex_prop_free(my); |
446 | kex_prop_free(peer); | |
447 | } | |
448 | ||
449 | static u_char * | |
08822d99 | 450 | derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen, |
451 | BIGNUM *shared_secret) | |
3c0ef626 | 452 | { |
453 | Buffer b; | |
3c0ef626 | 454 | EVP_MD_CTX md; |
455 | char c = id; | |
2ce0bfe4 | 456 | u_int have; |
08822d99 | 457 | int mdsz; |
2ce0bfe4 | 458 | u_char *digest; |
459 | ||
08822d99 | 460 | if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0) |
461 | fatal("bad kex md size %d", mdsz); | |
30460aeb | 462 | digest = xmalloc(roundup(need, mdsz)); |
3c0ef626 | 463 | |
464 | buffer_init(&b); | |
465 | buffer_put_bignum2(&b, shared_secret); | |
466 | ||
467 | /* K1 = HASH(K || H || "A" || session_id) */ | |
08822d99 | 468 | EVP_DigestInit(&md, kex->evp_md); |
3c0ef626 | 469 | if (!(datafellows & SSH_BUG_DERIVEKEY)) |
470 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
08822d99 | 471 | EVP_DigestUpdate(&md, hash, hashlen); |
3c0ef626 | 472 | EVP_DigestUpdate(&md, &c, 1); |
473 | EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len); | |
474 | EVP_DigestFinal(&md, digest, NULL); | |
475 | ||
476 | /* | |
477 | * expand key: | |
478 | * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) | |
479 | * Key = K1 || K2 || ... || Kn | |
480 | */ | |
481 | for (have = mdsz; need > have; have += mdsz) { | |
08822d99 | 482 | EVP_DigestInit(&md, kex->evp_md); |
3c0ef626 | 483 | if (!(datafellows & SSH_BUG_DERIVEKEY)) |
484 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
08822d99 | 485 | EVP_DigestUpdate(&md, hash, hashlen); |
3c0ef626 | 486 | EVP_DigestUpdate(&md, digest, have); |
487 | EVP_DigestFinal(&md, digest + have, NULL); | |
488 | } | |
489 | buffer_free(&b); | |
490 | #ifdef DEBUG_KEX | |
491 | fprintf(stderr, "key '%c'== ", c); | |
492 | dump_digest("key", digest, need); | |
493 | #endif | |
494 | return digest; | |
495 | } | |
496 | ||
497 | Newkeys *current_keys[MODE_MAX]; | |
498 | ||
499 | #define NKEYS 6 | |
500 | void | |
08822d99 | 501 | kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret) |
3c0ef626 | 502 | { |
503 | u_char *keys[NKEYS]; | |
2ce0bfe4 | 504 | u_int i, mode, ctos; |
3c0ef626 | 505 | |
08822d99 | 506 | for (i = 0; i < NKEYS; i++) { |
507 | keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen, | |
508 | shared_secret); | |
509 | } | |
3c0ef626 | 510 | |
8afc6a66 | 511 | debug2("kex_derive_keys"); |
3c0ef626 | 512 | for (mode = 0; mode < MODE_MAX; mode++) { |
513 | current_keys[mode] = kex->newkeys[mode]; | |
514 | kex->newkeys[mode] = NULL; | |
30460aeb | 515 | ctos = (!kex->server && mode == MODE_OUT) || |
516 | (kex->server && mode == MODE_IN); | |
3c0ef626 | 517 | current_keys[mode]->enc.iv = keys[ctos ? 0 : 1]; |
518 | current_keys[mode]->enc.key = keys[ctos ? 2 : 3]; | |
519 | current_keys[mode]->mac.key = keys[ctos ? 4 : 5]; | |
520 | } | |
521 | } | |
522 | ||
523 | Newkeys * | |
524 | kex_get_newkeys(int mode) | |
525 | { | |
526 | Newkeys *ret; | |
527 | ||
528 | ret = current_keys[mode]; | |
529 | current_keys[mode] = NULL; | |
530 | return ret; | |
531 | } | |
532 | ||
7e82606e | 533 | void |
534 | derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus, | |
535 | u_int8_t cookie[8], u_int8_t id[16]) | |
536 | { | |
537 | const EVP_MD *evp_md = EVP_md5(); | |
538 | EVP_MD_CTX md; | |
539 | u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE]; | |
540 | int len; | |
541 | ||
542 | EVP_DigestInit(&md, evp_md); | |
543 | ||
544 | len = BN_num_bytes(host_modulus); | |
2ce0bfe4 | 545 | if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) |
7e82606e | 546 | fatal("%s: bad host modulus (len %d)", __func__, len); |
547 | BN_bn2bin(host_modulus, nbuf); | |
548 | EVP_DigestUpdate(&md, nbuf, len); | |
549 | ||
550 | len = BN_num_bytes(server_modulus); | |
2ce0bfe4 | 551 | if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) |
7e82606e | 552 | fatal("%s: bad server modulus (len %d)", __func__, len); |
553 | BN_bn2bin(server_modulus, nbuf); | |
554 | EVP_DigestUpdate(&md, nbuf, len); | |
555 | ||
556 | EVP_DigestUpdate(&md, cookie, 8); | |
557 | ||
558 | EVP_DigestFinal(&md, obuf, NULL); | |
559 | memcpy(id, obuf, 16); | |
560 | ||
561 | memset(nbuf, 0, sizeof(nbuf)); | |
562 | memset(obuf, 0, sizeof(obuf)); | |
563 | memset(&md, 0, sizeof(md)); | |
564 | } | |
565 | ||
3c0ef626 | 566 | #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) |
567 | void | |
568 | dump_digest(char *msg, u_char *digest, int len) | |
569 | { | |
2ce0bfe4 | 570 | u_int i; |
3c0ef626 | 571 | |
572 | fprintf(stderr, "%s\n", msg); | |
e9702f7d | 573 | for (i = 0; i< len; i++) { |
3c0ef626 | 574 | fprintf(stderr, "%02x", digest[i]); |
575 | if (i%32 == 31) | |
576 | fprintf(stderr, "\n"); | |
577 | else if (i%8 == 7) | |
578 | fprintf(stderr, " "); | |
579 | } | |
580 | fprintf(stderr, "\n"); | |
581 | } | |
582 | #endif |