blob: 1a412ce0fcab2c98104d184d046b7b3c852a7880 [file] [log] [blame]
Damien Millera664e872000-04-16 11:52:47 +10001/*
Ben Lindstrom44697232001-07-04 03:32:30 +00002 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millera664e872000-04-16 11:52:47 +10003 *
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.
Damien Millera664e872000-04-16 11:52:47 +100012 *
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"
Ben Lindstrom44697232001-07-04 03:32:30 +000026RCSID("$OpenBSD: kex.c,v 1.36 2001/06/25 08:25:37 markus Exp $");
Damien Millera664e872000-04-16 11:52:47 +100027
28#include <openssl/crypto.h>
Damien Millera664e872000-04-16 11:52:47 +100029
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#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"
Damien Millera664e872000-04-16 11:52:47 +100037#include "kex.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110038#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000040#include "mac.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000041#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000042#include "dispatch.h"
Damien Millera664e872000-04-16 11:52:47 +100043
Damien Millerb1715dc2000-05-30 13:44:51 +100044#define KEX_COOKIE_LEN 16
45
Ben Lindstrombba81212001-06-25 05:01:22 +000046/* prototype */
47static void kex_kexinit_finish(Kex *);
48static void kex_choose_conf(Kex *);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000049
50/* put algorithm proposal into buffer */
Ben Lindstrombba81212001-06-25 05:01:22 +000051static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000052kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
Damien Millera664e872000-04-16 11:52:47 +100053{
Damien Millera664e872000-04-16 11:52:47 +100054 u_int32_t rand = 0;
55 int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000056
57 buffer_clear(b);
Damien Millerb1715dc2000-05-30 13:44:51 +100058 for (i = 0; i < KEX_COOKIE_LEN; i++) {
Damien Millera664e872000-04-16 11:52:47 +100059 if (i % 4 == 0)
60 rand = arc4random();
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000061 buffer_put_char(b, rand & 0xff);
Damien Millera664e872000-04-16 11:52:47 +100062 rand >>= 8;
63 }
Damien Millera664e872000-04-16 11:52:47 +100064 for (i = 0; i < PROPOSAL_MAX; i++)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000065 buffer_put_cstring(b, proposal[i]);
66 buffer_put_char(b, 0); /* first_kex_packet_follows */
67 buffer_put_int(b, 0); /* uint32 reserved */
Damien Millera664e872000-04-16 11:52:47 +100068}
69
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000070/* parse buffer and return algorithm proposal */
Ben Lindstrombba81212001-06-25 05:01:22 +000071static char **
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000072kex_buf2prop(Buffer *raw)
Damien Millerb1715dc2000-05-30 13:44:51 +100073{
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000074 Buffer b;
Damien Millerb1715dc2000-05-30 13:44:51 +100075 int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000076 char **proposal;
Damien Millerb1715dc2000-05-30 13:44:51 +100077
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000078 proposal = xmalloc(PROPOSAL_MAX * sizeof(char *));
Damien Millerb1715dc2000-05-30 13:44:51 +100079
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000080 buffer_init(&b);
81 buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
Damien Millerb1715dc2000-05-30 13:44:51 +100082 /* skip cookie */
83 for (i = 0; i < KEX_COOKIE_LEN; i++)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000084 buffer_get_char(&b);
Damien Millerb1715dc2000-05-30 13:44:51 +100085 /* extract kex init proposal strings */
86 for (i = 0; i < PROPOSAL_MAX; i++) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000087 proposal[i] = buffer_get_string(&b,NULL);
88 debug2("kex_parse_kexinit: %s", proposal[i]);
Damien Millerb1715dc2000-05-30 13:44:51 +100089 }
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000090 /* first kex follows / reserved */
91 i = buffer_get_char(&b);
92 debug2("kex_parse_kexinit: first_kex_follows %d ", i);
93 i = buffer_get_int(&b);
94 debug2("kex_parse_kexinit: reserved %d ", i);
95 buffer_free(&b);
96 return proposal;
Damien Millerb1715dc2000-05-30 13:44:51 +100097}
98
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100kex_prop_free(char **proposal)
Damien Millera664e872000-04-16 11:52:47 +1000101{
102 int i;
Damien Millera664e872000-04-16 11:52:47 +1000103
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000104 for (i = 0; i < PROPOSAL_MAX; i++)
105 xfree(proposal[i]);
106 xfree(proposal);
Damien Millera664e872000-04-16 11:52:47 +1000107}
108
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000110kex_protocol_error(int type, int plen, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100111{
Ben Lindstroma3700052001-04-05 23:26:32 +0000112 error("Hm, kex protocol error: type %d plen %d", type, plen);
Damien Miller874d77b2000-10-14 16:23:11 +1100113}
114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static void
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000116kex_clear_dispatch(void)
117{
118 int i;
119
120 /* Numbers 30-49 are used for kex packets */
121 for (i = 30; i <= 49; i++)
122 dispatch_set(i, &kex_protocol_error);
123}
124
125void
Ben Lindstrom238abf62001-04-04 17:52:53 +0000126kex_finish(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000127{
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000128 int plen;
129
130 kex_clear_dispatch();
Ben Lindstrom238abf62001-04-04 17:52:53 +0000131
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000132 packet_start(SSH2_MSG_NEWKEYS);
133 packet_send();
134 /* packet_write_wait(); */
135 debug("SSH2_MSG_NEWKEYS sent");
Damien Millera664e872000-04-16 11:52:47 +1000136
Ben Lindstroma3700052001-04-05 23:26:32 +0000137 debug("waiting for SSH2_MSG_NEWKEYS");
138 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000139 debug("SSH2_MSG_NEWKEYS received");
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000140
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000141 kex->done = 1;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000142 buffer_clear(&kex->peer);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000143 /* buffer_clear(&kex->my); */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000144 kex->flags &= ~KEX_INIT_SENT;
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000145 xfree(kex->name);
146 kex->name = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000147}
Damien Millera664e872000-04-16 11:52:47 +1000148
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000149void
150kex_send_kexinit(Kex *kex)
151{
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000152 if (kex == NULL) {
153 error("kex_send_kexinit: no kex, cannot rekey");
154 return;
155 }
Ben Lindstrom238abf62001-04-04 17:52:53 +0000156 if (kex->flags & KEX_INIT_SENT) {
157 debug("KEX_INIT_SENT");
158 return;
159 }
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000160 kex->done = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000161 packet_start(SSH2_MSG_KEXINIT);
162 packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
163 packet_send();
164 debug("SSH2_MSG_KEXINIT sent");
165 kex->flags |= KEX_INIT_SENT;
166}
167
168void
169kex_input_kexinit(int type, int plen, void *ctxt)
170{
171 char *ptr;
172 int dlen;
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000173 int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000174 Kex *kex = (Kex *)ctxt;
175
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000176 debug("SSH2_MSG_KEXINIT received");
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000177 if (kex == NULL)
178 fatal("kex_input_kexinit: no kex, cannot rekey");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000179
180 ptr = packet_get_raw(&dlen);
181 buffer_append(&kex->peer, ptr, dlen);
182
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000183 /* discard packet */
184 for (i = 0; i < KEX_COOKIE_LEN; i++)
185 packet_get_char();
186 for (i = 0; i < PROPOSAL_MAX; i++)
187 xfree(packet_get_string(NULL));
188 packet_get_char();
189 packet_get_int();
190 packet_done();
191
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000192 kex_kexinit_finish(kex);
193}
194
195Kex *
Ben Lindstrom238abf62001-04-04 17:52:53 +0000196kex_setup(char *proposal[PROPOSAL_MAX])
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000197{
198 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000199
200 kex = xmalloc(sizeof(*kex));
201 memset(kex, 0, sizeof(*kex));
202 buffer_init(&kex->peer);
203 buffer_init(&kex->my);
204 kex_prop2buf(&kex->my, proposal);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000205 kex->done = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000206
207 kex_send_kexinit(kex); /* we start */
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000208 kex_clear_dispatch();
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000209 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000210
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000211 return kex;
212}
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000215kex_kexinit_finish(Kex *kex)
216{
217 if (!(kex->flags & KEX_INIT_SENT))
218 kex_send_kexinit(kex);
219
220 kex_choose_conf(kex);
221
222 switch(kex->kex_type) {
223 case DH_GRP1_SHA1:
224 kexdh(kex);
225 break;
226 case DH_GEX_SHA1:
227 kexgex(kex);
228 break;
229 default:
230 fatal("Unsupported key exchange %d", kex->kex_type);
Damien Millera664e872000-04-16 11:52:47 +1000231 }
Damien Millera664e872000-04-16 11:52:47 +1000232}
233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Damien Millera664e872000-04-16 11:52:47 +1000235choose_enc(Enc *enc, char *client, char *server)
236{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000237 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000238 if (name == NULL)
239 fatal("no matching cipher found: client %s server %s", client, server);
Damien Miller874d77b2000-10-14 16:23:11 +1100240 enc->cipher = cipher_by_name(name);
241 if (enc->cipher == NULL)
242 fatal("matching cipher is not supported: %s", name);
Damien Millera664e872000-04-16 11:52:47 +1000243 enc->name = name;
244 enc->enabled = 0;
245 enc->iv = NULL;
246 enc->key = NULL;
247}
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static void
Damien Millera664e872000-04-16 11:52:47 +1000249choose_mac(Mac *mac, char *client, char *server)
250{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000251 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000252 if (name == NULL)
253 fatal("no matching mac found: client %s server %s", client, server);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000254 if (mac_init(mac, name) < 0)
Damien Millera664e872000-04-16 11:52:47 +1000255 fatal("unsupported mac %s", name);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000256 /* truncate the key */
257 if (datafellows & SSH_BUG_HMAC)
258 mac->key_len = 16;
Damien Millera664e872000-04-16 11:52:47 +1000259 mac->name = name;
Damien Millera664e872000-04-16 11:52:47 +1000260 mac->key = NULL;
261 mac->enabled = 0;
262}
Ben Lindstrombba81212001-06-25 05:01:22 +0000263static void
Damien Millera664e872000-04-16 11:52:47 +1000264choose_comp(Comp *comp, char *client, char *server)
265{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000266 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000267 if (name == NULL)
268 fatal("no matching comp found: client %s server %s", client, server);
269 if (strcmp(name, "zlib") == 0) {
270 comp->type = 1;
271 } else if (strcmp(name, "none") == 0) {
272 comp->type = 0;
273 } else {
274 fatal("unsupported comp %s", name);
275 }
276 comp->name = name;
277}
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static void
Damien Millera664e872000-04-16 11:52:47 +1000279choose_kex(Kex *k, char *client, char *server)
280{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000281 k->name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000282 if (k->name == NULL)
283 fatal("no kex alg");
Damien Miller874d77b2000-10-14 16:23:11 +1100284 if (strcmp(k->name, KEX_DH1) == 0) {
285 k->kex_type = DH_GRP1_SHA1;
286 } else if (strcmp(k->name, KEX_DHGEX) == 0) {
287 k->kex_type = DH_GEX_SHA1;
288 } else
Damien Millera664e872000-04-16 11:52:47 +1000289 fatal("bad kex alg %s", k->name);
290}
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static void
Damien Millera664e872000-04-16 11:52:47 +1000292choose_hostkeyalg(Kex *k, char *client, char *server)
293{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000294 char *hostkeyalg = match_list(client, server, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100295 if (hostkeyalg == NULL)
Damien Millera664e872000-04-16 11:52:47 +1000296 fatal("no hostkey alg");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100297 k->hostkey_type = key_type_from_name(hostkeyalg);
298 if (k->hostkey_type == KEY_UNSPEC)
299 fatal("bad hostkey alg '%s'", hostkeyalg);
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000300 xfree(hostkeyalg);
Damien Millera664e872000-04-16 11:52:47 +1000301}
302
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000304kex_choose_conf(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000305{
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000306 Newkeys *newkeys;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000307 char **my, **peer;
308 char **cprop, **sprop;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000309 int nenc, nmac, ncomp;
Damien Millera664e872000-04-16 11:52:47 +1000310 int mode;
311 int ctos; /* direction: if true client-to-server */
312 int need;
Damien Millera664e872000-04-16 11:52:47 +1000313
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000314 my = kex_buf2prop(&kex->my);
315 peer = kex_buf2prop(&kex->peer);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000316
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000317 if (kex->server) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000318 cprop=peer;
319 sprop=my;
320 } else {
321 cprop=my;
322 sprop=peer;
323 }
Damien Millera664e872000-04-16 11:52:47 +1000324
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000325 /* Algorithm Negotiation */
Damien Millera664e872000-04-16 11:52:47 +1000326 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000327 newkeys = xmalloc(sizeof(*newkeys));
328 memset(newkeys, 0, sizeof(*newkeys));
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000329 kex->newkeys[mode] = newkeys;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000330 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
Damien Millera664e872000-04-16 11:52:47 +1000331 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
332 nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
333 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000334 choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]);
335 choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]);
336 choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
Damien Millera664e872000-04-16 11:52:47 +1000337 debug("kex: %s %s %s %s",
338 ctos ? "client->server" : "server->client",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000339 newkeys->enc.name,
340 newkeys->mac.name,
341 newkeys->comp.name);
Damien Millera664e872000-04-16 11:52:47 +1000342 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000343 choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
344 choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
Damien Millera664e872000-04-16 11:52:47 +1000345 sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
Damien Millera664e872000-04-16 11:52:47 +1000346 need = 0;
347 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000348 newkeys = kex->newkeys[mode];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000349 if (need < newkeys->enc.cipher->key_len)
350 need = newkeys->enc.cipher->key_len;
351 if (need < newkeys->enc.cipher->block_size)
352 need = newkeys->enc.cipher->block_size;
353 if (need < newkeys->mac.key_len)
354 need = newkeys->mac.key_len;
Damien Millera664e872000-04-16 11:52:47 +1000355 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000356 /* XXX need runden? */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000357 kex->we_need = need;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000358
359 kex_prop_free(my);
360 kex_prop_free(peer);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000361}
362
Ben Lindstrombba81212001-06-25 05:01:22 +0000363static u_char *
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000364derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000365{
366 Buffer b;
367 EVP_MD *evp_md = EVP_sha1();
368 EVP_MD_CTX md;
369 char c = id;
370 int have;
371 int mdsz = evp_md->md_size;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000372 u_char *digest = xmalloc(roundup(need, mdsz));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000373
374 buffer_init(&b);
375 buffer_put_bignum2(&b, shared_secret);
376
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000377 /* K1 = HASH(K || H || "A" || session_id) */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000378 EVP_DigestInit(&md, evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000379 if (!(datafellows & SSH_BUG_DERIVEKEY))
380 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000381 EVP_DigestUpdate(&md, hash, mdsz);
382 EVP_DigestUpdate(&md, &c, 1);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000383 EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000384 EVP_DigestFinal(&md, digest, NULL);
385
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000386 /*
387 * expand key:
388 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
389 * Key = K1 || K2 || ... || Kn
390 */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000391 for (have = mdsz; need > have; have += mdsz) {
392 EVP_DigestInit(&md, evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000393 if (!(datafellows & SSH_BUG_DERIVEKEY))
394 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000395 EVP_DigestUpdate(&md, hash, mdsz);
396 EVP_DigestUpdate(&md, digest, have);
397 EVP_DigestFinal(&md, digest + have, NULL);
398 }
399 buffer_free(&b);
400#ifdef DEBUG_KEX
401 fprintf(stderr, "key '%c'== ", c);
402 dump_digest("key", digest, need);
403#endif
404 return digest;
Damien Millera664e872000-04-16 11:52:47 +1000405}
406
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000407Newkeys *current_keys[MODE_MAX];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000408
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000409#define NKEYS 6
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000410void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000411kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret)
Damien Millera664e872000-04-16 11:52:47 +1000412{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000413 u_char *keys[NKEYS];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000414 int i, mode, ctos;
Damien Millera664e872000-04-16 11:52:47 +1000415
416 for (i = 0; i < NKEYS; i++)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000417 keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret);
Damien Millera664e872000-04-16 11:52:47 +1000418
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000419 debug("kex_derive_keys");
Damien Millera664e872000-04-16 11:52:47 +1000420 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000421 current_keys[mode] = kex->newkeys[mode];
422 kex->newkeys[mode] = NULL;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000423 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000424 current_keys[mode]->enc.iv = keys[ctos ? 0 : 1];
425 current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
426 current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
Damien Millera664e872000-04-16 11:52:47 +1000427 }
Damien Millera664e872000-04-16 11:52:47 +1000428}
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000429
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000430Newkeys *
431kex_get_newkeys(int mode)
432{
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000433 Newkeys *ret;
434
435 ret = current_keys[mode];
436 current_keys[mode] = NULL;
437 return ret;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000438}
439
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000440#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
441void
442dump_digest(char *msg, u_char *digest, int len)
443{
444 int i;
445
446 fprintf(stderr, "%s\n", msg);
447 for (i = 0; i< len; i++){
448 fprintf(stderr, "%02x", digest[i]);
449 if (i%32 == 31)
450 fprintf(stderr, "\n");
451 else if (i%8 == 7)
452 fprintf(stderr, " ");
453 }
454 fprintf(stderr, "\n");
455}
456#endif