2000-04-16 01:52:47 +00:00
|
|
|
/*
|
2001-07-04 03:32:30 +00:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-04-16 01:52:47 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2002-01-22 12:24:13 +00:00
|
|
|
RCSID("$OpenBSD: kex.c,v 1.42 2002/01/11 13:39:36 markus Exp $");
|
2000-04-16 01:52:47 +00:00
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "ssh2.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufaux.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "cipher.h"
|
2000-04-16 01:52:47 +00:00
|
|
|
#include "kex.h"
|
2000-11-13 11:57:25 +00:00
|
|
|
#include "key.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
2001-02-15 03:01:59 +00:00
|
|
|
#include "mac.h"
|
2001-03-11 01:49:19 +00:00
|
|
|
#include "match.h"
|
2001-04-04 01:56:17 +00:00
|
|
|
#include "dispatch.h"
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2000-05-30 03:44:51 +00:00
|
|
|
#define KEX_COOKIE_LEN 16
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
/* prototype */
|
|
|
|
static void kex_kexinit_finish(Kex *);
|
|
|
|
static void kex_choose_conf(Kex *);
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
/* put algorithm proposal into buffer */
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
|
|
|
u_int32_t rand = 0;
|
|
|
|
int i;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
buffer_clear(b);
|
2000-05-30 03:44:51 +00:00
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
2000-04-16 01:52:47 +00:00
|
|
|
if (i % 4 == 0)
|
|
|
|
rand = arc4random();
|
2001-04-04 01:56:17 +00:00
|
|
|
buffer_put_char(b, rand & 0xff);
|
2000-04-16 01:52:47 +00:00
|
|
|
rand >>= 8;
|
|
|
|
}
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
2001-04-04 01:56:17 +00:00
|
|
|
buffer_put_cstring(b, proposal[i]);
|
|
|
|
buffer_put_char(b, 0); /* first_kex_packet_follows */
|
|
|
|
buffer_put_int(b, 0); /* uint32 reserved */
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
/* parse buffer and return algorithm proposal */
|
2001-06-25 05:01:22 +00:00
|
|
|
static char **
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_buf2prop(Buffer *raw)
|
2000-05-30 03:44:51 +00:00
|
|
|
{
|
2001-04-04 01:56:17 +00:00
|
|
|
Buffer b;
|
2000-05-30 03:44:51 +00:00
|
|
|
int i;
|
2001-04-04 01:56:17 +00:00
|
|
|
char **proposal;
|
2000-05-30 03:44:51 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
proposal = xmalloc(PROPOSAL_MAX * sizeof(char *));
|
|
|
|
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
|
2000-05-30 03:44:51 +00:00
|
|
|
/* skip cookie */
|
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
2001-04-04 01:56:17 +00:00
|
|
|
buffer_get_char(&b);
|
2000-05-30 03:44:51 +00:00
|
|
|
/* extract kex init proposal strings */
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
2001-04-04 01:56:17 +00:00
|
|
|
proposal[i] = buffer_get_string(&b,NULL);
|
|
|
|
debug2("kex_parse_kexinit: %s", proposal[i]);
|
2000-05-30 03:44:51 +00:00
|
|
|
}
|
2001-04-04 01:56:17 +00:00
|
|
|
/* first kex follows / reserved */
|
|
|
|
i = buffer_get_char(&b);
|
|
|
|
debug2("kex_parse_kexinit: first_kex_follows %d ", i);
|
|
|
|
i = buffer_get_int(&b);
|
|
|
|
debug2("kex_parse_kexinit: reserved %d ", i);
|
|
|
|
buffer_free(&b);
|
|
|
|
return proposal;
|
2000-05-30 03:44:51 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_prop_free(char **proposal)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
|
|
|
int i;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
|
|
|
xfree(proposal[i]);
|
|
|
|
xfree(proposal);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2002-01-22 12:17:30 +00:00
|
|
|
kex_protocol_error(int type, u_int32_t seq, void *ctxt)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2002-01-22 12:17:30 +00:00
|
|
|
error("Hm, kex protocol error: type %d seq %u", type, seq);
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2001-04-04 17:57:54 +00:00
|
|
|
kex_clear_dispatch(void)
|
|
|
|
{
|
2002-01-22 12:24:13 +00:00
|
|
|
dispatch_range(SSH2_MSG_TRANSPORT_MIN,
|
|
|
|
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
2001-04-04 17:57:54 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
void
|
2001-04-04 17:52:53 +00:00
|
|
|
kex_finish(Kex *kex)
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
2001-04-04 17:57:54 +00:00
|
|
|
kex_clear_dispatch();
|
2001-04-04 17:52:53 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
packet_start(SSH2_MSG_NEWKEYS);
|
|
|
|
packet_send();
|
|
|
|
/* packet_write_wait(); */
|
|
|
|
debug("SSH2_MSG_NEWKEYS sent");
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-04-05 23:26:32 +00:00
|
|
|
debug("waiting for SSH2_MSG_NEWKEYS");
|
2002-01-22 12:16:32 +00:00
|
|
|
packet_read_expect(SSH2_MSG_NEWKEYS);
|
2001-04-04 01:56:17 +00:00
|
|
|
debug("SSH2_MSG_NEWKEYS received");
|
2001-04-05 02:05:21 +00:00
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->done = 1;
|
2001-04-04 01:56:17 +00:00
|
|
|
buffer_clear(&kex->peer);
|
2001-04-04 02:00:54 +00:00
|
|
|
/* buffer_clear(&kex->my); */
|
2001-04-04 01:56:17 +00:00
|
|
|
kex->flags &= ~KEX_INIT_SENT;
|
2001-04-05 02:05:21 +00:00
|
|
|
xfree(kex->name);
|
|
|
|
kex->name = NULL;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
void
|
|
|
|
kex_send_kexinit(Kex *kex)
|
2000-10-14 05:23:11 +00:00
|
|
|
{
|
2001-04-04 17:57:54 +00:00
|
|
|
if (kex == NULL) {
|
|
|
|
error("kex_send_kexinit: no kex, cannot rekey");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-04 17:52:53 +00:00
|
|
|
if (kex->flags & KEX_INIT_SENT) {
|
|
|
|
debug("KEX_INIT_SENT");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->done = 0;
|
2001-04-04 01:56:17 +00:00
|
|
|
packet_start(SSH2_MSG_KEXINIT);
|
|
|
|
packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
|
|
|
|
packet_send();
|
|
|
|
debug("SSH2_MSG_KEXINIT sent");
|
|
|
|
kex->flags |= KEX_INIT_SENT;
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
void
|
2002-01-22 12:17:30 +00:00
|
|
|
kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int dlen;
|
2001-04-04 23:50:21 +00:00
|
|
|
int i;
|
2001-04-04 01:56:17 +00:00
|
|
|
Kex *kex = (Kex *)ctxt;
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
debug("SSH2_MSG_KEXINIT received");
|
2001-04-04 17:57:54 +00:00
|
|
|
if (kex == NULL)
|
|
|
|
fatal("kex_input_kexinit: no kex, cannot rekey");
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
ptr = packet_get_raw(&dlen);
|
|
|
|
buffer_append(&kex->peer, ptr, dlen);
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2001-04-04 23:50:21 +00:00
|
|
|
/* discard packet */
|
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
|
|
|
packet_get_char();
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
|
|
|
xfree(packet_get_string(NULL));
|
|
|
|
packet_get_char();
|
|
|
|
packet_get_int();
|
2002-01-22 12:11:40 +00:00
|
|
|
packet_check_eom();
|
2001-04-04 23:50:21 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_kexinit_finish(kex);
|
2000-10-14 05:23:11 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
Kex *
|
2001-04-04 17:52:53 +00:00
|
|
|
kex_setup(char *proposal[PROPOSAL_MAX])
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-04-04 01:56:17 +00:00
|
|
|
Kex *kex;
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
kex = xmalloc(sizeof(*kex));
|
|
|
|
memset(kex, 0, sizeof(*kex));
|
|
|
|
buffer_init(&kex->peer);
|
|
|
|
buffer_init(&kex->my);
|
|
|
|
kex_prop2buf(&kex->my, proposal);
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->done = 0;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
kex_send_kexinit(kex); /* we start */
|
2001-04-04 17:57:54 +00:00
|
|
|
kex_clear_dispatch();
|
2001-04-04 01:56:17 +00:00
|
|
|
dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
2001-04-04 17:57:54 +00:00
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
return kex;
|
|
|
|
}
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_kexinit_finish(Kex *kex)
|
|
|
|
{
|
|
|
|
if (!(kex->flags & KEX_INIT_SENT))
|
|
|
|
kex_send_kexinit(kex);
|
|
|
|
|
|
|
|
kex_choose_conf(kex);
|
|
|
|
|
2001-12-06 18:00:18 +00:00
|
|
|
switch (kex->kex_type) {
|
2001-04-04 01:56:17 +00:00
|
|
|
case DH_GRP1_SHA1:
|
|
|
|
kexdh(kex);
|
|
|
|
break;
|
|
|
|
case DH_GEX_SHA1:
|
|
|
|
kexgex(kex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("Unsupported key exchange %d", kex->kex_type);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-04-16 01:52:47 +00:00
|
|
|
choose_enc(Enc *enc, char *client, char *server)
|
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
|
|
|
fatal("no matching cipher found: client %s server %s", client, server);
|
2000-10-14 05:23:11 +00:00
|
|
|
enc->cipher = cipher_by_name(name);
|
|
|
|
if (enc->cipher == NULL)
|
|
|
|
fatal("matching cipher is not supported: %s", name);
|
2000-04-16 01:52:47 +00:00
|
|
|
enc->name = name;
|
|
|
|
enc->enabled = 0;
|
|
|
|
enc->iv = NULL;
|
|
|
|
enc->key = NULL;
|
|
|
|
}
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-04-16 01:52:47 +00:00
|
|
|
choose_mac(Mac *mac, char *client, char *server)
|
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
|
|
|
fatal("no matching mac found: client %s server %s", client, server);
|
2001-02-15 03:01:59 +00:00
|
|
|
if (mac_init(mac, name) < 0)
|
2000-04-16 01:52:47 +00:00
|
|
|
fatal("unsupported mac %s", name);
|
2001-02-15 03:01:59 +00:00
|
|
|
/* truncate the key */
|
|
|
|
if (datafellows & SSH_BUG_HMAC)
|
|
|
|
mac->key_len = 16;
|
2000-04-16 01:52:47 +00:00
|
|
|
mac->name = name;
|
|
|
|
mac->key = NULL;
|
|
|
|
mac->enabled = 0;
|
|
|
|
}
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-04-16 01:52:47 +00:00
|
|
|
choose_comp(Comp *comp, char *client, char *server)
|
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
|
|
|
fatal("no matching comp found: client %s server %s", client, server);
|
|
|
|
if (strcmp(name, "zlib") == 0) {
|
|
|
|
comp->type = 1;
|
|
|
|
} else if (strcmp(name, "none") == 0) {
|
|
|
|
comp->type = 0;
|
|
|
|
} else {
|
|
|
|
fatal("unsupported comp %s", name);
|
|
|
|
}
|
|
|
|
comp->name = name;
|
|
|
|
}
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-04-16 01:52:47 +00:00
|
|
|
choose_kex(Kex *k, char *client, char *server)
|
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
k->name = match_list(client, server, NULL);
|
2000-04-16 01:52:47 +00:00
|
|
|
if (k->name == NULL)
|
|
|
|
fatal("no kex alg");
|
2000-10-14 05:23:11 +00:00
|
|
|
if (strcmp(k->name, KEX_DH1) == 0) {
|
|
|
|
k->kex_type = DH_GRP1_SHA1;
|
|
|
|
} else if (strcmp(k->name, KEX_DHGEX) == 0) {
|
|
|
|
k->kex_type = DH_GEX_SHA1;
|
|
|
|
} else
|
2000-04-16 01:52:47 +00:00
|
|
|
fatal("bad kex alg %s", k->name);
|
|
|
|
}
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-04-16 01:52:47 +00:00
|
|
|
choose_hostkeyalg(Kex *k, char *client, char *server)
|
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *hostkeyalg = match_list(client, server, NULL);
|
2000-11-13 11:57:25 +00:00
|
|
|
if (hostkeyalg == NULL)
|
2000-04-16 01:52:47 +00:00
|
|
|
fatal("no hostkey alg");
|
2000-11-13 11:57:25 +00:00
|
|
|
k->hostkey_type = key_type_from_name(hostkeyalg);
|
|
|
|
if (k->hostkey_type == KEY_UNSPEC)
|
|
|
|
fatal("bad hostkey alg '%s'", hostkeyalg);
|
2001-01-09 00:35:42 +00:00
|
|
|
xfree(hostkeyalg);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2001-04-04 02:00:54 +00:00
|
|
|
kex_choose_conf(Kex *kex)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-04-04 02:00:54 +00:00
|
|
|
Newkeys *newkeys;
|
2001-04-04 01:56:17 +00:00
|
|
|
char **my, **peer;
|
|
|
|
char **cprop, **sprop;
|
2001-04-04 02:00:54 +00:00
|
|
|
int nenc, nmac, ncomp;
|
2000-04-16 01:52:47 +00:00
|
|
|
int mode;
|
|
|
|
int ctos; /* direction: if true client-to-server */
|
|
|
|
int need;
|
|
|
|
|
2001-04-04 02:00:54 +00:00
|
|
|
my = kex_buf2prop(&kex->my);
|
|
|
|
peer = kex_buf2prop(&kex->peer);
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2001-04-04 02:00:54 +00:00
|
|
|
if (kex->server) {
|
2001-04-04 01:56:17 +00:00
|
|
|
cprop=peer;
|
|
|
|
sprop=my;
|
|
|
|
} else {
|
|
|
|
cprop=my;
|
|
|
|
sprop=peer;
|
|
|
|
}
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
/* Algorithm Negotiation */
|
2000-04-16 01:52:47 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-04 02:00:54 +00:00
|
|
|
newkeys = xmalloc(sizeof(*newkeys));
|
|
|
|
memset(newkeys, 0, sizeof(*newkeys));
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->newkeys[mode] = newkeys;
|
2001-04-04 02:00:54 +00:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
|
2000-04-16 01:52:47 +00:00
|
|
|
nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
|
|
|
|
nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
|
|
|
|
ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
|
2001-04-04 02:00:54 +00:00
|
|
|
choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]);
|
|
|
|
choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]);
|
|
|
|
choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
|
2000-04-16 01:52:47 +00:00
|
|
|
debug("kex: %s %s %s %s",
|
|
|
|
ctos ? "client->server" : "server->client",
|
2001-04-04 02:00:54 +00:00
|
|
|
newkeys->enc.name,
|
|
|
|
newkeys->mac.name,
|
|
|
|
newkeys->comp.name);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2001-04-04 02:00:54 +00:00
|
|
|
choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
|
|
|
|
choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
2000-04-16 01:52:47 +00:00
|
|
|
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
|
|
|
|
need = 0;
|
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-04 23:46:07 +00:00
|
|
|
newkeys = kex->newkeys[mode];
|
2001-04-04 02:00:54 +00:00
|
|
|
if (need < newkeys->enc.cipher->key_len)
|
|
|
|
need = newkeys->enc.cipher->key_len;
|
|
|
|
if (need < newkeys->enc.cipher->block_size)
|
|
|
|
need = newkeys->enc.cipher->block_size;
|
|
|
|
if (need < newkeys->mac.key_len)
|
|
|
|
need = newkeys->mac.key_len;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2000-05-30 03:44:51 +00:00
|
|
|
/* XXX need runden? */
|
2001-04-04 02:00:54 +00:00
|
|
|
kex->we_need = need;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
kex_prop_free(my);
|
|
|
|
kex_prop_free(peer);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static u_char *
|
2001-04-04 02:00:54 +00:00
|
|
|
derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret)
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
EVP_MD *evp_md = EVP_sha1();
|
|
|
|
EVP_MD_CTX md;
|
|
|
|
char c = id;
|
|
|
|
int have;
|
|
|
|
int mdsz = evp_md->md_size;
|
2001-04-04 23:46:07 +00:00
|
|
|
u_char *digest = xmalloc(roundup(need, mdsz));
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_put_bignum2(&b, shared_secret);
|
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
/* K1 = HASH(K || H || "A" || session_id) */
|
2001-04-04 01:56:17 +00:00
|
|
|
EVP_DigestInit(&md, evp_md);
|
2001-04-30 23:06:57 +00:00
|
|
|
if (!(datafellows & SSH_BUG_DERIVEKEY))
|
|
|
|
EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
|
2001-04-04 23:46:07 +00:00
|
|
|
EVP_DigestUpdate(&md, hash, mdsz);
|
|
|
|
EVP_DigestUpdate(&md, &c, 1);
|
2001-04-04 02:00:54 +00:00
|
|
|
EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
|
2001-04-04 01:56:17 +00:00
|
|
|
EVP_DigestFinal(&md, digest, NULL);
|
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
/*
|
|
|
|
* expand key:
|
|
|
|
* Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
|
|
|
|
* Key = K1 || K2 || ... || Kn
|
|
|
|
*/
|
2001-04-04 01:56:17 +00:00
|
|
|
for (have = mdsz; need > have; have += mdsz) {
|
|
|
|
EVP_DigestInit(&md, evp_md);
|
2001-04-30 23:06:57 +00:00
|
|
|
if (!(datafellows & SSH_BUG_DERIVEKEY))
|
|
|
|
EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
|
2001-04-04 01:56:17 +00:00
|
|
|
EVP_DigestUpdate(&md, hash, mdsz);
|
|
|
|
EVP_DigestUpdate(&md, digest, have);
|
|
|
|
EVP_DigestFinal(&md, digest + have, NULL);
|
|
|
|
}
|
|
|
|
buffer_free(&b);
|
|
|
|
#ifdef DEBUG_KEX
|
|
|
|
fprintf(stderr, "key '%c'== ", c);
|
|
|
|
dump_digest("key", digest, need);
|
|
|
|
#endif
|
|
|
|
return digest;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
Newkeys *current_keys[MODE_MAX];
|
2001-04-04 02:00:54 +00:00
|
|
|
|
2001-03-11 01:49:19 +00:00
|
|
|
#define NKEYS 6
|
2001-04-04 01:56:17 +00:00
|
|
|
void
|
2001-04-04 02:00:54 +00:00
|
|
|
kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char *keys[NKEYS];
|
2001-04-04 02:00:54 +00:00
|
|
|
int i, mode, ctos;
|
2000-04-16 01:52:47 +00:00
|
|
|
|
|
|
|
for (i = 0; i < NKEYS; i++)
|
2001-04-04 02:00:54 +00:00
|
|
|
keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret);
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2001-04-04 02:00:54 +00:00
|
|
|
debug("kex_derive_keys");
|
2000-04-16 01:52:47 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-04 23:46:07 +00:00
|
|
|
current_keys[mode] = kex->newkeys[mode];
|
|
|
|
kex->newkeys[mode] = NULL;
|
2001-04-04 02:00:54 +00:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN);
|
2001-04-04 23:46:07 +00:00
|
|
|
current_keys[mode]->enc.iv = keys[ctos ? 0 : 1];
|
|
|
|
current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
|
|
|
|
current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2001-04-04 02:00:54 +00:00
|
|
|
Newkeys *
|
|
|
|
kex_get_newkeys(int mode)
|
|
|
|
{
|
2001-04-04 23:46:07 +00:00
|
|
|
Newkeys *ret;
|
|
|
|
|
|
|
|
ret = current_keys[mode];
|
|
|
|
current_keys[mode] = NULL;
|
|
|
|
return ret;
|
2001-04-04 02:00:54 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
|
|
|
|
void
|
|
|
|
dump_digest(char *msg, u_char *digest, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2001-12-06 18:00:18 +00:00
|
|
|
for (i = 0; i< len; i++) {
|
2001-04-04 01:56:17 +00:00
|
|
|
fprintf(stderr, "%02x", digest[i]);
|
|
|
|
if (i%32 == 31)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
else if (i%8 == 7)
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|