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