]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000, 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" | |
e9702f7d | 26 | RCSID("$OpenBSD: kex.c,v 1.47 2002/02/28 15:46:33 markus Exp $"); |
3c0ef626 | 27 | |
28 | #include <openssl/crypto.h> | |
29 | ||
30 | #include "ssh2.h" | |
31 | #include "xmalloc.h" | |
32 | #include "buffer.h" | |
33 | #include "bufaux.h" | |
34 | #include "packet.h" | |
35 | #include "compat.h" | |
36 | #include "cipher.h" | |
37 | #include "kex.h" | |
38 | #include "key.h" | |
39 | #include "log.h" | |
40 | #include "mac.h" | |
41 | #include "match.h" | |
42 | #include "dispatch.h" | |
43 | ||
5598e598 | 44 | #ifdef GSSAPI |
45 | #include "ssh-gss.h" | |
46 | #endif | |
47 | ||
3c0ef626 | 48 | #define KEX_COOKIE_LEN 16 |
49 | ||
50 | /* prototype */ | |
51 | static void kex_kexinit_finish(Kex *); | |
52 | static void kex_choose_conf(Kex *); | |
53 | ||
54 | /* put algorithm proposal into buffer */ | |
55 | static void | |
56 | kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX]) | |
57 | { | |
58 | u_int32_t rand = 0; | |
59 | int i; | |
60 | ||
61 | buffer_clear(b); | |
62 | for (i = 0; i < KEX_COOKIE_LEN; i++) { | |
63 | if (i % 4 == 0) | |
64 | rand = arc4random(); | |
65 | buffer_put_char(b, rand & 0xff); | |
66 | rand >>= 8; | |
67 | } | |
68 | for (i = 0; i < PROPOSAL_MAX; i++) | |
69 | buffer_put_cstring(b, proposal[i]); | |
70 | buffer_put_char(b, 0); /* first_kex_packet_follows */ | |
71 | buffer_put_int(b, 0); /* uint32 reserved */ | |
72 | } | |
73 | ||
74 | /* parse buffer and return algorithm proposal */ | |
75 | static char ** | |
76 | kex_buf2prop(Buffer *raw) | |
77 | { | |
78 | Buffer b; | |
79 | int i; | |
80 | char **proposal; | |
81 | ||
82 | proposal = xmalloc(PROPOSAL_MAX * sizeof(char *)); | |
83 | ||
84 | buffer_init(&b); | |
85 | buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); | |
86 | /* skip cookie */ | |
87 | for (i = 0; i < KEX_COOKIE_LEN; i++) | |
88 | buffer_get_char(&b); | |
89 | /* extract kex init proposal strings */ | |
90 | for (i = 0; i < PROPOSAL_MAX; i++) { | |
91 | proposal[i] = buffer_get_string(&b,NULL); | |
92 | debug2("kex_parse_kexinit: %s", proposal[i]); | |
93 | } | |
94 | /* first kex follows / reserved */ | |
95 | i = buffer_get_char(&b); | |
96 | debug2("kex_parse_kexinit: first_kex_follows %d ", i); | |
97 | i = buffer_get_int(&b); | |
98 | debug2("kex_parse_kexinit: reserved %d ", i); | |
99 | buffer_free(&b); | |
100 | return proposal; | |
101 | } | |
102 | ||
103 | static void | |
104 | kex_prop_free(char **proposal) | |
105 | { | |
106 | int i; | |
107 | ||
108 | for (i = 0; i < PROPOSAL_MAX; i++) | |
109 | xfree(proposal[i]); | |
110 | xfree(proposal); | |
111 | } | |
112 | ||
113 | static void | |
e9702f7d | 114 | kex_protocol_error(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 115 | { |
e9702f7d | 116 | error("Hm, kex protocol error: type %d seq %u", type, seq); |
3c0ef626 | 117 | } |
118 | ||
119 | static void | |
e9702f7d | 120 | kex_reset_dispatch(void) |
3c0ef626 | 121 | { |
e9702f7d | 122 | dispatch_range(SSH2_MSG_TRANSPORT_MIN, |
123 | SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); | |
124 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); | |
3c0ef626 | 125 | } |
126 | ||
127 | void | |
128 | kex_finish(Kex *kex) | |
129 | { | |
e9702f7d | 130 | kex_reset_dispatch(); |
3c0ef626 | 131 | |
132 | packet_start(SSH2_MSG_NEWKEYS); | |
133 | packet_send(); | |
134 | /* packet_write_wait(); */ | |
135 | debug("SSH2_MSG_NEWKEYS sent"); | |
136 | ||
137 | debug("waiting for SSH2_MSG_NEWKEYS"); | |
e9702f7d | 138 | packet_read_expect(SSH2_MSG_NEWKEYS); |
139 | packet_check_eom(); | |
3c0ef626 | 140 | debug("SSH2_MSG_NEWKEYS received"); |
141 | ||
142 | kex->done = 1; | |
143 | buffer_clear(&kex->peer); | |
144 | /* buffer_clear(&kex->my); */ | |
145 | kex->flags &= ~KEX_INIT_SENT; | |
146 | xfree(kex->name); | |
147 | kex->name = NULL; | |
148 | } | |
149 | ||
150 | void | |
151 | kex_send_kexinit(Kex *kex) | |
152 | { | |
153 | if (kex == NULL) { | |
154 | error("kex_send_kexinit: no kex, cannot rekey"); | |
155 | return; | |
156 | } | |
157 | if (kex->flags & KEX_INIT_SENT) { | |
158 | debug("KEX_INIT_SENT"); | |
159 | return; | |
160 | } | |
161 | kex->done = 0; | |
162 | packet_start(SSH2_MSG_KEXINIT); | |
163 | packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my)); | |
164 | packet_send(); | |
165 | debug("SSH2_MSG_KEXINIT sent"); | |
166 | kex->flags |= KEX_INIT_SENT; | |
167 | } | |
168 | ||
169 | void | |
e9702f7d | 170 | kex_input_kexinit(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 171 | { |
172 | char *ptr; | |
173 | int dlen; | |
174 | int i; | |
175 | Kex *kex = (Kex *)ctxt; | |
176 | ||
177 | debug("SSH2_MSG_KEXINIT received"); | |
178 | if (kex == NULL) | |
179 | fatal("kex_input_kexinit: no kex, cannot rekey"); | |
180 | ||
181 | ptr = packet_get_raw(&dlen); | |
182 | buffer_append(&kex->peer, ptr, dlen); | |
183 | ||
184 | /* discard packet */ | |
185 | for (i = 0; i < KEX_COOKIE_LEN; i++) | |
186 | packet_get_char(); | |
187 | for (i = 0; i < PROPOSAL_MAX; i++) | |
188 | xfree(packet_get_string(NULL)); | |
189 | packet_get_char(); | |
190 | packet_get_int(); | |
e9702f7d | 191 | packet_check_eom(); |
3c0ef626 | 192 | |
193 | kex_kexinit_finish(kex); | |
194 | } | |
195 | ||
196 | Kex * | |
197 | kex_setup(char *proposal[PROPOSAL_MAX]) | |
198 | { | |
199 | Kex *kex; | |
200 | ||
201 | kex = xmalloc(sizeof(*kex)); | |
202 | memset(kex, 0, sizeof(*kex)); | |
203 | buffer_init(&kex->peer); | |
204 | buffer_init(&kex->my); | |
205 | kex_prop2buf(&kex->my, proposal); | |
206 | kex->done = 0; | |
207 | ||
208 | kex_send_kexinit(kex); /* we start */ | |
e9702f7d | 209 | kex_reset_dispatch(); |
3c0ef626 | 210 | |
211 | return kex; | |
212 | } | |
213 | ||
214 | static void | |
215 | kex_kexinit_finish(Kex *kex) | |
216 | { | |
217 | if (!(kex->flags & KEX_INIT_SENT)) | |
218 | kex_send_kexinit(kex); | |
219 | ||
220 | kex_choose_conf(kex); | |
221 | ||
e9702f7d | 222 | switch (kex->kex_type) { |
3c0ef626 | 223 | case DH_GRP1_SHA1: |
224 | kexdh(kex); | |
225 | break; | |
226 | case DH_GEX_SHA1: | |
227 | kexgex(kex); | |
228 | break; | |
5598e598 | 229 | #ifdef GSSAPI |
230 | case GSS_GRP1_SHA1: | |
231 | kexgss(kex); | |
232 | break; | |
233 | #endif | |
3c0ef626 | 234 | default: |
235 | fatal("Unsupported key exchange %d", kex->kex_type); | |
236 | } | |
237 | } | |
238 | ||
239 | static void | |
240 | choose_enc(Enc *enc, char *client, char *server) | |
241 | { | |
242 | char *name = match_list(client, server, NULL); | |
243 | if (name == NULL) | |
244 | fatal("no matching cipher found: client %s server %s", client, server); | |
e9702f7d | 245 | if ((enc->cipher = cipher_by_name(name)) == NULL) |
3c0ef626 | 246 | fatal("matching cipher is not supported: %s", name); |
247 | enc->name = name; | |
248 | enc->enabled = 0; | |
249 | enc->iv = NULL; | |
250 | enc->key = NULL; | |
e9702f7d | 251 | enc->key_len = cipher_keylen(enc->cipher); |
252 | enc->block_size = cipher_blocksize(enc->cipher); | |
3c0ef626 | 253 | } |
254 | static void | |
255 | choose_mac(Mac *mac, char *client, char *server) | |
256 | { | |
257 | char *name = match_list(client, server, NULL); | |
258 | if (name == NULL) | |
259 | fatal("no matching mac found: client %s server %s", client, server); | |
260 | if (mac_init(mac, name) < 0) | |
261 | fatal("unsupported mac %s", name); | |
262 | /* truncate the key */ | |
263 | if (datafellows & SSH_BUG_HMAC) | |
264 | mac->key_len = 16; | |
265 | mac->name = name; | |
266 | mac->key = NULL; | |
267 | mac->enabled = 0; | |
268 | } | |
269 | static void | |
270 | choose_comp(Comp *comp, char *client, char *server) | |
271 | { | |
272 | char *name = match_list(client, server, NULL); | |
273 | if (name == NULL) | |
274 | fatal("no matching comp found: client %s server %s", client, server); | |
275 | if (strcmp(name, "zlib") == 0) { | |
276 | comp->type = 1; | |
277 | } else if (strcmp(name, "none") == 0) { | |
278 | comp->type = 0; | |
279 | } else { | |
280 | fatal("unsupported comp %s", name); | |
281 | } | |
282 | comp->name = name; | |
283 | } | |
284 | static void | |
285 | choose_kex(Kex *k, char *client, char *server) | |
286 | { | |
287 | k->name = match_list(client, server, NULL); | |
288 | if (k->name == NULL) | |
5598e598 | 289 | fatal("No key exchange algorithm"); |
3c0ef626 | 290 | if (strcmp(k->name, KEX_DH1) == 0) { |
291 | k->kex_type = DH_GRP1_SHA1; | |
292 | } else if (strcmp(k->name, KEX_DHGEX) == 0) { | |
293 | k->kex_type = DH_GEX_SHA1; | |
5598e598 | 294 | #ifdef GSSAPI |
295 | } else if (strncmp(k->name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) == 0) { | |
296 | k->kex_type = GSS_GRP1_SHA1; | |
297 | #endif | |
3c0ef626 | 298 | } else |
299 | fatal("bad kex alg %s", k->name); | |
300 | } | |
301 | static void | |
302 | choose_hostkeyalg(Kex *k, char *client, char *server) | |
303 | { | |
304 | char *hostkeyalg = match_list(client, server, NULL); | |
305 | if (hostkeyalg == NULL) | |
306 | fatal("no hostkey alg"); | |
307 | k->hostkey_type = key_type_from_name(hostkeyalg); | |
308 | if (k->hostkey_type == KEY_UNSPEC) | |
309 | fatal("bad hostkey alg '%s'", hostkeyalg); | |
310 | xfree(hostkeyalg); | |
311 | } | |
312 | ||
313 | static void | |
314 | kex_choose_conf(Kex *kex) | |
315 | { | |
316 | Newkeys *newkeys; | |
317 | char **my, **peer; | |
318 | char **cprop, **sprop; | |
319 | int nenc, nmac, ncomp; | |
320 | int mode; | |
321 | int ctos; /* direction: if true client-to-server */ | |
322 | int need; | |
323 | ||
324 | my = kex_buf2prop(&kex->my); | |
325 | peer = kex_buf2prop(&kex->peer); | |
326 | ||
327 | if (kex->server) { | |
328 | cprop=peer; | |
329 | sprop=my; | |
330 | } else { | |
331 | cprop=my; | |
332 | sprop=peer; | |
333 | } | |
334 | ||
335 | /* Algorithm Negotiation */ | |
336 | for (mode = 0; mode < MODE_MAX; mode++) { | |
337 | newkeys = xmalloc(sizeof(*newkeys)); | |
338 | memset(newkeys, 0, sizeof(*newkeys)); | |
339 | kex->newkeys[mode] = newkeys; | |
340 | ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); | |
341 | nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; | |
342 | nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; | |
343 | ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; | |
344 | choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]); | |
345 | choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]); | |
346 | choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]); | |
347 | debug("kex: %s %s %s %s", | |
348 | ctos ? "client->server" : "server->client", | |
349 | newkeys->enc.name, | |
350 | newkeys->mac.name, | |
351 | newkeys->comp.name); | |
352 | } | |
353 | choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]); | |
354 | choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], | |
355 | sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]); | |
356 | need = 0; | |
357 | for (mode = 0; mode < MODE_MAX; mode++) { | |
358 | newkeys = kex->newkeys[mode]; | |
e9702f7d | 359 | if (need < newkeys->enc.key_len) |
360 | need = newkeys->enc.key_len; | |
361 | if (need < newkeys->enc.block_size) | |
362 | need = newkeys->enc.block_size; | |
3c0ef626 | 363 | if (need < newkeys->mac.key_len) |
364 | need = newkeys->mac.key_len; | |
365 | } | |
366 | /* XXX need runden? */ | |
367 | kex->we_need = need; | |
368 | ||
369 | kex_prop_free(my); | |
370 | kex_prop_free(peer); | |
371 | } | |
372 | ||
373 | static u_char * | |
374 | derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret) | |
375 | { | |
376 | Buffer b; | |
e9702f7d | 377 | const EVP_MD *evp_md = EVP_sha1(); |
3c0ef626 | 378 | EVP_MD_CTX md; |
379 | char c = id; | |
380 | int have; | |
e9702f7d | 381 | int mdsz = EVP_MD_size(evp_md); |
3c0ef626 | 382 | u_char *digest = xmalloc(roundup(need, mdsz)); |
383 | ||
384 | buffer_init(&b); | |
385 | buffer_put_bignum2(&b, shared_secret); | |
386 | ||
387 | /* K1 = HASH(K || H || "A" || session_id) */ | |
388 | EVP_DigestInit(&md, evp_md); | |
389 | if (!(datafellows & SSH_BUG_DERIVEKEY)) | |
390 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
391 | EVP_DigestUpdate(&md, hash, mdsz); | |
392 | EVP_DigestUpdate(&md, &c, 1); | |
393 | EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len); | |
394 | EVP_DigestFinal(&md, digest, NULL); | |
395 | ||
396 | /* | |
397 | * expand key: | |
398 | * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) | |
399 | * Key = K1 || K2 || ... || Kn | |
400 | */ | |
401 | for (have = mdsz; need > have; have += mdsz) { | |
402 | EVP_DigestInit(&md, evp_md); | |
403 | if (!(datafellows & SSH_BUG_DERIVEKEY)) | |
404 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
405 | EVP_DigestUpdate(&md, hash, mdsz); | |
406 | EVP_DigestUpdate(&md, digest, have); | |
407 | EVP_DigestFinal(&md, digest + have, NULL); | |
408 | } | |
409 | buffer_free(&b); | |
410 | #ifdef DEBUG_KEX | |
411 | fprintf(stderr, "key '%c'== ", c); | |
412 | dump_digest("key", digest, need); | |
413 | #endif | |
414 | return digest; | |
415 | } | |
416 | ||
417 | Newkeys *current_keys[MODE_MAX]; | |
418 | ||
419 | #define NKEYS 6 | |
420 | void | |
421 | kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret) | |
422 | { | |
423 | u_char *keys[NKEYS]; | |
424 | int i, mode, ctos; | |
425 | ||
426 | for (i = 0; i < NKEYS; i++) | |
427 | keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret); | |
428 | ||
429 | debug("kex_derive_keys"); | |
430 | for (mode = 0; mode < MODE_MAX; mode++) { | |
431 | current_keys[mode] = kex->newkeys[mode]; | |
432 | kex->newkeys[mode] = NULL; | |
433 | ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); | |
434 | current_keys[mode]->enc.iv = keys[ctos ? 0 : 1]; | |
435 | current_keys[mode]->enc.key = keys[ctos ? 2 : 3]; | |
436 | current_keys[mode]->mac.key = keys[ctos ? 4 : 5]; | |
437 | } | |
438 | } | |
439 | ||
440 | Newkeys * | |
441 | kex_get_newkeys(int mode) | |
442 | { | |
443 | Newkeys *ret; | |
444 | ||
445 | ret = current_keys[mode]; | |
446 | current_keys[mode] = NULL; | |
447 | return ret; | |
448 | } | |
449 | ||
450 | #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) | |
451 | void | |
452 | dump_digest(char *msg, u_char *digest, int len) | |
453 | { | |
454 | int i; | |
455 | ||
456 | fprintf(stderr, "%s\n", msg); | |
e9702f7d | 457 | for (i = 0; i< len; i++) { |
3c0ef626 | 458 | fprintf(stderr, "%02x", digest[i]); |
459 | if (i%32 == 31) | |
460 | fprintf(stderr, "\n"); | |
461 | else if (i%8 == 7) | |
462 | fprintf(stderr, " "); | |
463 | } | |
464 | fprintf(stderr, "\n"); | |
465 | } | |
466 | #endif |