blob: 02c9780ad1d1728ac900aa68ef796169c771bd5c [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"
Damien Millerbc27d4e2002-02-13 13:54:06 +110026RCSID("$OpenBSD: kex.c,v 1.44 2002/02/11 16:10:15 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
Damien Miller630d6f42002-01-22 23:17:30 +1100110kex_protocol_error(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100111{
Damien Miller630d6f42002-01-22 23:17:30 +1100112 error("Hm, kex protocol error: type %d seq %u", type, seq);
Damien Miller874d77b2000-10-14 16:23:11 +1100113}
114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static void
Damien Millerbc27d4e2002-02-13 13:54:06 +1100116kex_reset_dispatch(void)
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000117{
Damien Miller7d053392002-01-22 23:24:13 +1100118 dispatch_range(SSH2_MSG_TRANSPORT_MIN,
119 SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
Damien Millerbc27d4e2002-02-13 13:54:06 +1100120 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000121}
122
123void
Ben Lindstrom238abf62001-04-04 17:52:53 +0000124kex_finish(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000125{
Damien Millerbc27d4e2002-02-13 13:54:06 +1100126 kex_reset_dispatch();
Ben Lindstrom238abf62001-04-04 17:52:53 +0000127
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000128 packet_start(SSH2_MSG_NEWKEYS);
129 packet_send();
130 /* packet_write_wait(); */
131 debug("SSH2_MSG_NEWKEYS sent");
Damien Millera664e872000-04-16 11:52:47 +1000132
Ben Lindstroma3700052001-04-05 23:26:32 +0000133 debug("waiting for SSH2_MSG_NEWKEYS");
Damien Millerdff50992002-01-22 23:16:32 +1100134 packet_read_expect(SSH2_MSG_NEWKEYS);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000135 debug("SSH2_MSG_NEWKEYS received");
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000136
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000137 kex->done = 1;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000138 buffer_clear(&kex->peer);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000139 /* buffer_clear(&kex->my); */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000140 kex->flags &= ~KEX_INIT_SENT;
Ben Lindstrom5ba23b32001-04-05 02:05:21 +0000141 xfree(kex->name);
142 kex->name = NULL;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000143}
Damien Millera664e872000-04-16 11:52:47 +1000144
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000145void
146kex_send_kexinit(Kex *kex)
147{
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000148 if (kex == NULL) {
149 error("kex_send_kexinit: no kex, cannot rekey");
150 return;
151 }
Ben Lindstrom238abf62001-04-04 17:52:53 +0000152 if (kex->flags & KEX_INIT_SENT) {
153 debug("KEX_INIT_SENT");
154 return;
155 }
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000156 kex->done = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000157 packet_start(SSH2_MSG_KEXINIT);
158 packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
159 packet_send();
160 debug("SSH2_MSG_KEXINIT sent");
161 kex->flags |= KEX_INIT_SENT;
162}
163
164void
Damien Miller630d6f42002-01-22 23:17:30 +1100165kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000166{
167 char *ptr;
168 int dlen;
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000169 int i;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000170 Kex *kex = (Kex *)ctxt;
171
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000172 debug("SSH2_MSG_KEXINIT received");
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000173 if (kex == NULL)
174 fatal("kex_input_kexinit: no kex, cannot rekey");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000175
176 ptr = packet_get_raw(&dlen);
177 buffer_append(&kex->peer, ptr, dlen);
178
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000179 /* discard packet */
180 for (i = 0; i < KEX_COOKIE_LEN; i++)
181 packet_get_char();
182 for (i = 0; i < PROPOSAL_MAX; i++)
183 xfree(packet_get_string(NULL));
184 packet_get_char();
185 packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100186 packet_check_eom();
Ben Lindstrom8e312f32001-04-04 23:50:21 +0000187
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000188 kex_kexinit_finish(kex);
189}
190
191Kex *
Ben Lindstrom238abf62001-04-04 17:52:53 +0000192kex_setup(char *proposal[PROPOSAL_MAX])
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000193{
194 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000195
196 kex = xmalloc(sizeof(*kex));
197 memset(kex, 0, sizeof(*kex));
198 buffer_init(&kex->peer);
199 buffer_init(&kex->my);
200 kex_prop2buf(&kex->my, proposal);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000201 kex->done = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000202
203 kex_send_kexinit(kex); /* we start */
Damien Millerbc27d4e2002-02-13 13:54:06 +1100204 kex_reset_dispatch();
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000205
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000206 return kex;
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000210kex_kexinit_finish(Kex *kex)
211{
212 if (!(kex->flags & KEX_INIT_SENT))
213 kex_send_kexinit(kex);
214
215 kex_choose_conf(kex);
216
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000217 switch (kex->kex_type) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000218 case DH_GRP1_SHA1:
219 kexdh(kex);
220 break;
221 case DH_GEX_SHA1:
222 kexgex(kex);
223 break;
224 default:
225 fatal("Unsupported key exchange %d", kex->kex_type);
Damien Millera664e872000-04-16 11:52:47 +1000226 }
Damien Millera664e872000-04-16 11:52:47 +1000227}
228
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static void
Damien Millera664e872000-04-16 11:52:47 +1000230choose_enc(Enc *enc, char *client, char *server)
231{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000232 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000233 if (name == NULL)
234 fatal("no matching cipher found: client %s server %s", client, server);
Damien Miller874d77b2000-10-14 16:23:11 +1100235 enc->cipher = cipher_by_name(name);
236 if (enc->cipher == NULL)
237 fatal("matching cipher is not supported: %s", name);
Damien Millera664e872000-04-16 11:52:47 +1000238 enc->name = name;
239 enc->enabled = 0;
240 enc->iv = NULL;
241 enc->key = NULL;
242}
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Millera664e872000-04-16 11:52:47 +1000244choose_mac(Mac *mac, char *client, char *server)
245{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000246 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000247 if (name == NULL)
248 fatal("no matching mac found: client %s server %s", client, server);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000249 if (mac_init(mac, name) < 0)
Damien Millera664e872000-04-16 11:52:47 +1000250 fatal("unsupported mac %s", name);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000251 /* truncate the key */
252 if (datafellows & SSH_BUG_HMAC)
253 mac->key_len = 16;
Damien Millera664e872000-04-16 11:52:47 +1000254 mac->name = name;
Damien Millera664e872000-04-16 11:52:47 +1000255 mac->key = NULL;
256 mac->enabled = 0;
257}
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static void
Damien Millera664e872000-04-16 11:52:47 +1000259choose_comp(Comp *comp, char *client, char *server)
260{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000261 char *name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000262 if (name == NULL)
263 fatal("no matching comp found: client %s server %s", client, server);
264 if (strcmp(name, "zlib") == 0) {
265 comp->type = 1;
266 } else if (strcmp(name, "none") == 0) {
267 comp->type = 0;
268 } else {
269 fatal("unsupported comp %s", name);
270 }
271 comp->name = name;
272}
Ben Lindstrombba81212001-06-25 05:01:22 +0000273static void
Damien Millera664e872000-04-16 11:52:47 +1000274choose_kex(Kex *k, char *client, char *server)
275{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000276 k->name = match_list(client, server, NULL);
Damien Millera664e872000-04-16 11:52:47 +1000277 if (k->name == NULL)
278 fatal("no kex alg");
Damien Miller874d77b2000-10-14 16:23:11 +1100279 if (strcmp(k->name, KEX_DH1) == 0) {
280 k->kex_type = DH_GRP1_SHA1;
281 } else if (strcmp(k->name, KEX_DHGEX) == 0) {
282 k->kex_type = DH_GEX_SHA1;
283 } else
Damien Millera664e872000-04-16 11:52:47 +1000284 fatal("bad kex alg %s", k->name);
285}
Ben Lindstrombba81212001-06-25 05:01:22 +0000286static void
Damien Millera664e872000-04-16 11:52:47 +1000287choose_hostkeyalg(Kex *k, char *client, char *server)
288{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000289 char *hostkeyalg = match_list(client, server, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100290 if (hostkeyalg == NULL)
Damien Millera664e872000-04-16 11:52:47 +1000291 fatal("no hostkey alg");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100292 k->hostkey_type = key_type_from_name(hostkeyalg);
293 if (k->hostkey_type == KEY_UNSPEC)
294 fatal("bad hostkey alg '%s'", hostkeyalg);
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000295 xfree(hostkeyalg);
Damien Millera664e872000-04-16 11:52:47 +1000296}
297
Ben Lindstrombba81212001-06-25 05:01:22 +0000298static void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000299kex_choose_conf(Kex *kex)
Damien Millera664e872000-04-16 11:52:47 +1000300{
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000301 Newkeys *newkeys;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000302 char **my, **peer;
303 char **cprop, **sprop;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000304 int nenc, nmac, ncomp;
Damien Millera664e872000-04-16 11:52:47 +1000305 int mode;
306 int ctos; /* direction: if true client-to-server */
307 int need;
Damien Millera664e872000-04-16 11:52:47 +1000308
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000309 my = kex_buf2prop(&kex->my);
310 peer = kex_buf2prop(&kex->peer);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000311
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000312 if (kex->server) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000313 cprop=peer;
314 sprop=my;
315 } else {
316 cprop=my;
317 sprop=peer;
318 }
Damien Millera664e872000-04-16 11:52:47 +1000319
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000320 /* Algorithm Negotiation */
Damien Millera664e872000-04-16 11:52:47 +1000321 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000322 newkeys = xmalloc(sizeof(*newkeys));
323 memset(newkeys, 0, sizeof(*newkeys));
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000324 kex->newkeys[mode] = newkeys;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000325 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
Damien Millera664e872000-04-16 11:52:47 +1000326 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
327 nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
328 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000329 choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]);
330 choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]);
331 choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
Damien Millera664e872000-04-16 11:52:47 +1000332 debug("kex: %s %s %s %s",
333 ctos ? "client->server" : "server->client",
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000334 newkeys->enc.name,
335 newkeys->mac.name,
336 newkeys->comp.name);
Damien Millera664e872000-04-16 11:52:47 +1000337 }
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000338 choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
339 choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
Damien Millera664e872000-04-16 11:52:47 +1000340 sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
Damien Millera664e872000-04-16 11:52:47 +1000341 need = 0;
342 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000343 newkeys = kex->newkeys[mode];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000344 if (need < newkeys->enc.cipher->key_len)
345 need = newkeys->enc.cipher->key_len;
346 if (need < newkeys->enc.cipher->block_size)
347 need = newkeys->enc.cipher->block_size;
348 if (need < newkeys->mac.key_len)
349 need = newkeys->mac.key_len;
Damien Millera664e872000-04-16 11:52:47 +1000350 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000351 /* XXX need runden? */
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000352 kex->we_need = need;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000353
354 kex_prop_free(my);
355 kex_prop_free(peer);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000356}
357
Ben Lindstrombba81212001-06-25 05:01:22 +0000358static u_char *
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000359derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000360{
361 Buffer b;
362 EVP_MD *evp_md = EVP_sha1();
363 EVP_MD_CTX md;
364 char c = id;
365 int have;
Damien Miller3672e4b2002-02-05 11:54:07 +1100366 int mdsz = EVP_MD_size(evp_md);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000367 u_char *digest = xmalloc(roundup(need, mdsz));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000368
369 buffer_init(&b);
370 buffer_put_bignum2(&b, shared_secret);
371
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000372 /* K1 = HASH(K || H || "A" || session_id) */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000373 EVP_DigestInit(&md, evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000374 if (!(datafellows & SSH_BUG_DERIVEKEY))
375 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000376 EVP_DigestUpdate(&md, hash, mdsz);
377 EVP_DigestUpdate(&md, &c, 1);
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000378 EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000379 EVP_DigestFinal(&md, digest, NULL);
380
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000381 /*
382 * expand key:
383 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
384 * Key = K1 || K2 || ... || Kn
385 */
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000386 for (have = mdsz; need > have; have += mdsz) {
387 EVP_DigestInit(&md, evp_md);
Ben Lindstrom8c963922001-04-30 23:06:57 +0000388 if (!(datafellows & SSH_BUG_DERIVEKEY))
389 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000390 EVP_DigestUpdate(&md, hash, mdsz);
391 EVP_DigestUpdate(&md, digest, have);
392 EVP_DigestFinal(&md, digest + have, NULL);
393 }
394 buffer_free(&b);
395#ifdef DEBUG_KEX
396 fprintf(stderr, "key '%c'== ", c);
397 dump_digest("key", digest, need);
398#endif
399 return digest;
Damien Millera664e872000-04-16 11:52:47 +1000400}
401
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000402Newkeys *current_keys[MODE_MAX];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000403
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000404#define NKEYS 6
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000405void
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000406kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret)
Damien Millera664e872000-04-16 11:52:47 +1000407{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000408 u_char *keys[NKEYS];
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000409 int i, mode, ctos;
Damien Millera664e872000-04-16 11:52:47 +1000410
411 for (i = 0; i < NKEYS; i++)
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000412 keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret);
Damien Millera664e872000-04-16 11:52:47 +1000413
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000414 debug("kex_derive_keys");
Damien Millera664e872000-04-16 11:52:47 +1000415 for (mode = 0; mode < MODE_MAX; mode++) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000416 current_keys[mode] = kex->newkeys[mode];
417 kex->newkeys[mode] = NULL;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000418 ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000419 current_keys[mode]->enc.iv = keys[ctos ? 0 : 1];
420 current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
421 current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
Damien Millera664e872000-04-16 11:52:47 +1000422 }
Damien Millera664e872000-04-16 11:52:47 +1000423}
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000424
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000425Newkeys *
426kex_get_newkeys(int mode)
427{
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000428 Newkeys *ret;
429
430 ret = current_keys[mode];
431 current_keys[mode] = NULL;
432 return ret;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000433}
434
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000435#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
436void
437dump_digest(char *msg, u_char *digest, int len)
438{
439 int i;
440
441 fprintf(stderr, "%s\n", msg);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000442 for (i = 0; i< len; i++) {
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000443 fprintf(stderr, "%02x", digest[i]);
444 if (i%32 == 31)
445 fprintf(stderr, "\n");
446 else if (i%8 == 7)
447 fprintf(stderr, " ");
448 }
449 fprintf(stderr, "\n");
450}
451#endif