2013-04-23 17:25:29 +08:00
|
|
|
/* $OpenBSD: kex.c,v 1.90 2013/04/19 12:07:08 djm Exp $ */
|
2000-04-16 09:52:47 +08:00
|
|
|
/*
|
2001-07-04 11:32:30 +08:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-04-16 09:52:47 +08: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"
|
|
|
|
|
2006-08-05 09:02:17 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <signal.h>
|
2006-09-01 13:38:36 +08:00
|
|
|
#include <stdarg.h>
|
2006-08-05 09:37:59 +08:00
|
|
|
#include <stdio.h>
|
2006-08-05 09:34:19 +08:00
|
|
|
#include <stdlib.h>
|
2006-07-24 12:13:33 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 10:39:39 +08:00
|
|
|
#include "ssh2.h"
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "cipher.h"
|
2000-11-13 19:57:25 +08:00
|
|
|
#include "key.h"
|
2006-08-05 10:39:39 +08:00
|
|
|
#include "kex.h"
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "log.h"
|
2001-02-15 11:01:59 +08:00
|
|
|
#include "mac.h"
|
2001-03-11 09:49:19 +08:00
|
|
|
#include "match.h"
|
2001-04-04 09:56:17 +08:00
|
|
|
#include "dispatch.h"
|
2002-03-22 10:30:41 +08:00
|
|
|
#include "monitor.h"
|
2010-01-08 13:50:41 +08:00
|
|
|
#include "roaming.h"
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2006-03-16 15:22:18 +08:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
|
|
|
|
# if defined(HAVE_EVP_SHA256)
|
2006-03-15 10:02:28 +08:00
|
|
|
# define evp_ssh_sha256 EVP_sha256
|
2006-03-16 15:22:18 +08:00
|
|
|
# else
|
2006-03-15 09:08:28 +08:00
|
|
|
extern const EVP_MD *evp_ssh_sha256(void);
|
2006-03-16 15:22:18 +08:00
|
|
|
# endif
|
2006-03-16 12:17:05 +08:00
|
|
|
#endif
|
2006-03-15 09:08:28 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
/* prototype */
|
|
|
|
static void kex_kexinit_finish(Kex *);
|
|
|
|
static void kex_choose_conf(Kex *);
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2013-04-23 17:24:32 +08:00
|
|
|
struct kexalg {
|
|
|
|
char *name;
|
|
|
|
int type;
|
|
|
|
int ec_nid;
|
|
|
|
const EVP_MD *(*mdfunc)(void);
|
|
|
|
};
|
|
|
|
static const struct kexalg kexalgs[] = {
|
|
|
|
{ KEX_DH1, KEX_DH_GRP1_SHA1, 0, EVP_sha1 },
|
|
|
|
{ KEX_DH14, KEX_DH_GRP14_SHA1, 0, EVP_sha1 },
|
|
|
|
{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, EVP_sha1 },
|
2013-05-10 16:11:55 +08:00
|
|
|
#ifdef HAVE_EVP_SHA256
|
2013-04-23 17:24:32 +08:00
|
|
|
{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, EVP_sha256 },
|
2013-05-10 16:11:55 +08:00
|
|
|
#endif
|
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2013-04-23 17:24:32 +08:00
|
|
|
{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, NID_X9_62_prime256v1, EVP_sha256 },
|
|
|
|
{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, EVP_sha384 },
|
|
|
|
{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, EVP_sha512 },
|
|
|
|
#endif
|
|
|
|
{ NULL, -1, -1, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
|
|
|
kex_alg_list(void)
|
|
|
|
{
|
|
|
|
char *ret = NULL;
|
|
|
|
size_t nlen, rlen = 0;
|
|
|
|
const struct kexalg *k;
|
|
|
|
|
|
|
|
for (k = kexalgs; k->name != NULL; k++) {
|
|
|
|
if (ret != NULL)
|
|
|
|
ret[rlen++] = '\n';
|
|
|
|
nlen = strlen(k->name);
|
|
|
|
ret = xrealloc(ret, 1, rlen + nlen + 2);
|
|
|
|
memcpy(ret + rlen, k->name, nlen + 1);
|
|
|
|
rlen += nlen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct kexalg *
|
|
|
|
kex_alg_by_name(const char *name)
|
|
|
|
{
|
|
|
|
const struct kexalg *k;
|
|
|
|
|
|
|
|
for (k = kexalgs; k->name != NULL; k++) {
|
|
|
|
if (strcmp(k->name, name) == 0)
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-24 20:11:14 +08:00
|
|
|
/* Validate KEX method name list */
|
|
|
|
int
|
|
|
|
kex_names_valid(const char *names)
|
|
|
|
{
|
|
|
|
char *s, *cp, *p;
|
|
|
|
|
|
|
|
if (names == NULL || strcmp(names, "") == 0)
|
|
|
|
return 0;
|
|
|
|
s = cp = xstrdup(names);
|
|
|
|
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
|
|
|
(p = strsep(&cp, ","))) {
|
2013-04-23 17:24:32 +08:00
|
|
|
if (kex_alg_by_name(p) == NULL) {
|
2010-09-24 20:11:14 +08:00
|
|
|
error("Unsupported KEX algorithm \"%.100s\"", p);
|
|
|
|
xfree(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug3("kex names ok: [%s]", names);
|
|
|
|
xfree(s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
/* put algorithm proposal into buffer */
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
buffer_clear(b);
|
2002-03-28 01:42:57 +08:00
|
|
|
/*
|
|
|
|
* add a dummy cookie, the cookie will be overwritten by
|
|
|
|
* kex_send_kexinit(), each time a kexinit is set
|
|
|
|
*/
|
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
|
|
|
buffer_put_char(b, 0);
|
2000-04-16 09:52:47 +08:00
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
2001-04-04 09:56:17 +08: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 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
/* parse buffer and return algorithm proposal */
|
2001-06-25 13:01:22 +08:00
|
|
|
static char **
|
2003-02-24 08:53:32 +08:00
|
|
|
kex_buf2prop(Buffer *raw, int *first_kex_follows)
|
2000-05-30 11:44:51 +08:00
|
|
|
{
|
2001-04-04 09:56:17 +08:00
|
|
|
Buffer b;
|
2007-06-05 16:23:28 +08:00
|
|
|
u_int i;
|
2001-04-04 09:56:17 +08:00
|
|
|
char **proposal;
|
2000-05-30 11:44:51 +08:00
|
|
|
|
2006-03-26 11:19:21 +08:00
|
|
|
proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
|
2000-05-30 11:44:51 +08:00
|
|
|
/* skip cookie */
|
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
2001-04-04 09:56:17 +08:00
|
|
|
buffer_get_char(&b);
|
2000-05-30 11:44:51 +08:00
|
|
|
/* extract kex init proposal strings */
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
2010-08-31 20:36:39 +08:00
|
|
|
proposal[i] = buffer_get_cstring(&b,NULL);
|
2001-04-04 09:56:17 +08:00
|
|
|
debug2("kex_parse_kexinit: %s", proposal[i]);
|
2000-05-30 11:44:51 +08:00
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
/* first kex follows / reserved */
|
|
|
|
i = buffer_get_char(&b);
|
2003-02-24 08:53:32 +08:00
|
|
|
if (first_kex_follows != NULL)
|
|
|
|
*first_kex_follows = i;
|
2001-04-04 09:56:17 +08:00
|
|
|
debug2("kex_parse_kexinit: first_kex_follows %d ", i);
|
|
|
|
i = buffer_get_int(&b);
|
2007-06-05 16:23:28 +08:00
|
|
|
debug2("kex_parse_kexinit: reserved %u ", i);
|
2001-04-04 09:56:17 +08:00
|
|
|
buffer_free(&b);
|
|
|
|
return proposal;
|
2000-05-30 11:44:51 +08:00
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_prop_free(char **proposal)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
|
|
|
xfree(proposal[i]);
|
|
|
|
xfree(proposal);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2007-06-05 16:23:28 +08:00
|
|
|
/* ARGSUSED */
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2002-01-22 20:17:30 +08:00
|
|
|
kex_protocol_error(int type, u_int32_t seq, void *ctxt)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2002-01-22 20:17:30 +08:00
|
|
|
error("Hm, kex protocol error: type %d seq %u", type, seq);
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2002-02-13 10:54:06 +08:00
|
|
|
kex_reset_dispatch(void)
|
2001-04-05 01:57:54 +08:00
|
|
|
{
|
2002-01-22 20:24:13 +08:00
|
|
|
dispatch_range(SSH2_MSG_TRANSPORT_MIN,
|
|
|
|
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
2002-02-13 10:54:06 +08:00
|
|
|
dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
2001-04-05 01:57:54 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
void
|
2001-04-05 01:52:53 +08:00
|
|
|
kex_finish(Kex *kex)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
2002-02-13 10:54:06 +08:00
|
|
|
kex_reset_dispatch();
|
2001-04-05 01:52:53 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
packet_start(SSH2_MSG_NEWKEYS);
|
|
|
|
packet_send();
|
|
|
|
/* packet_write_wait(); */
|
|
|
|
debug("SSH2_MSG_NEWKEYS sent");
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2002-12-23 10:04:22 +08:00
|
|
|
debug("expecting SSH2_MSG_NEWKEYS");
|
2002-01-22 20:16:32 +08:00
|
|
|
packet_read_expect(SSH2_MSG_NEWKEYS);
|
2002-02-27 01:58:29 +08:00
|
|
|
packet_check_eom();
|
2001-04-04 09:56:17 +08:00
|
|
|
debug("SSH2_MSG_NEWKEYS received");
|
2001-04-05 10:05:21 +08:00
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->done = 1;
|
2001-04-04 09:56:17 +08:00
|
|
|
buffer_clear(&kex->peer);
|
2001-04-04 10:00:54 +08:00
|
|
|
/* buffer_clear(&kex->my); */
|
2001-04-04 09:56:17 +08:00
|
|
|
kex->flags &= ~KEX_INIT_SENT;
|
2001-04-05 10:05:21 +08:00
|
|
|
xfree(kex->name);
|
|
|
|
kex->name = NULL;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
void
|
|
|
|
kex_send_kexinit(Kex *kex)
|
2000-10-14 13:23:11 +08:00
|
|
|
{
|
2004-06-22 10:56:01 +08:00
|
|
|
u_int32_t rnd = 0;
|
2002-03-28 01:42:57 +08:00
|
|
|
u_char *cookie;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i;
|
2002-03-28 01:42:57 +08:00
|
|
|
|
2001-04-05 01:57:54 +08:00
|
|
|
if (kex == NULL) {
|
|
|
|
error("kex_send_kexinit: no kex, cannot rekey");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-05 01:52:53 +08:00
|
|
|
if (kex->flags & KEX_INIT_SENT) {
|
|
|
|
debug("KEX_INIT_SENT");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->done = 0;
|
2002-03-28 01:42:57 +08:00
|
|
|
|
|
|
|
/* generate a random cookie */
|
|
|
|
if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
|
|
|
|
fatal("kex_send_kexinit: kex proposal too short");
|
|
|
|
cookie = buffer_ptr(&kex->my);
|
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
|
|
|
if (i % 4 == 0)
|
2004-06-22 10:56:01 +08:00
|
|
|
rnd = arc4random();
|
|
|
|
cookie[i] = rnd;
|
|
|
|
rnd >>= 8;
|
2002-03-28 01:42:57 +08:00
|
|
|
}
|
2001-04-04 09:56:17 +08: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 20:42:17 +08:00
|
|
|
|
2007-06-05 16:23:28 +08:00
|
|
|
/* ARGSUSED */
|
2001-04-04 09:56:17 +08:00
|
|
|
void
|
2002-01-22 20:17:30 +08:00
|
|
|
kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
|
|
|
char *ptr;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i, dlen;
|
2001-04-04 09:56:17 +08:00
|
|
|
Kex *kex = (Kex *)ctxt;
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
debug("SSH2_MSG_KEXINIT received");
|
2001-04-05 01:57:54 +08:00
|
|
|
if (kex == NULL)
|
|
|
|
fatal("kex_input_kexinit: no kex, cannot rekey");
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
ptr = packet_get_raw(&dlen);
|
|
|
|
buffer_append(&kex->peer, ptr, dlen);
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2001-04-05 07:50:21 +08: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));
|
2012-09-06 19:19:51 +08:00
|
|
|
/*
|
|
|
|
* XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
|
|
|
|
* KEX method has the server move first, but a server might be using
|
|
|
|
* a custom method or one that we otherwise don't support. We should
|
|
|
|
* be prepared to remember first_kex_follows here so we can eat a
|
|
|
|
* packet later.
|
|
|
|
* XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
|
|
|
|
* for cases where the server *doesn't* go first. I guess we should
|
|
|
|
* ignore it when it is set for these cases, which is what we do now.
|
|
|
|
*/
|
|
|
|
(void) packet_get_char(); /* first_kex_follows */
|
|
|
|
(void) packet_get_int(); /* reserved */
|
2002-01-22 20:11:40 +08:00
|
|
|
packet_check_eom();
|
2001-04-05 07:50:21 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_kexinit_finish(kex);
|
2000-10-14 13:23:11 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
Kex *
|
2001-04-05 01:52:53 +08:00
|
|
|
kex_setup(char *proposal[PROPOSAL_MAX])
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2001-04-04 09:56:17 +08:00
|
|
|
Kex *kex;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2006-03-26 11:19:21 +08:00
|
|
|
kex = xcalloc(1, sizeof(*kex));
|
2001-04-04 09:56:17 +08:00
|
|
|
buffer_init(&kex->peer);
|
|
|
|
buffer_init(&kex->my);
|
|
|
|
kex_prop2buf(&kex->my, proposal);
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->done = 0;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
kex_send_kexinit(kex); /* we start */
|
2002-02-13 10:54:06 +08:00
|
|
|
kex_reset_dispatch();
|
2001-04-05 01:57:54 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
return kex;
|
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_kexinit_finish(Kex *kex)
|
|
|
|
{
|
|
|
|
if (!(kex->flags & KEX_INIT_SENT))
|
|
|
|
kex_send_kexinit(kex);
|
|
|
|
|
|
|
|
kex_choose_conf(kex);
|
|
|
|
|
2003-02-24 09:03:03 +08:00
|
|
|
if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
|
|
|
|
kex->kex[kex->kex_type] != NULL) {
|
|
|
|
(kex->kex[kex->kex_type])(kex);
|
|
|
|
} else {
|
2001-04-04 09:56:17 +08:00
|
|
|
fatal("Unsupported key exchange %d", kex->kex_type);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2000-04-16 09:52:47 +08:00
|
|
|
choose_enc(Enc *enc, char *client, char *server)
|
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
2007-06-05 16:23:28 +08:00
|
|
|
fatal("no matching cipher found: client %s server %s",
|
|
|
|
client, server);
|
2002-02-19 12:21:23 +08:00
|
|
|
if ((enc->cipher = cipher_by_name(name)) == NULL)
|
2000-10-14 13:23:11 +08:00
|
|
|
fatal("matching cipher is not supported: %s", name);
|
2000-04-16 09:52:47 +08:00
|
|
|
enc->name = name;
|
|
|
|
enc->enabled = 0;
|
|
|
|
enc->iv = NULL;
|
2013-01-09 13:12:19 +08:00
|
|
|
enc->iv_len = cipher_ivlen(enc->cipher);
|
2000-04-16 09:52:47 +08:00
|
|
|
enc->key = NULL;
|
2002-02-19 12:21:23 +08:00
|
|
|
enc->key_len = cipher_keylen(enc->cipher);
|
|
|
|
enc->block_size = cipher_blocksize(enc->cipher);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2000-04-16 09:52:47 +08:00
|
|
|
choose_mac(Mac *mac, char *client, char *server)
|
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
2007-06-05 16:23:28 +08:00
|
|
|
fatal("no matching mac found: client %s server %s",
|
|
|
|
client, server);
|
2007-06-05 16:30:18 +08:00
|
|
|
if (mac_setup(mac, name) < 0)
|
2000-04-16 09:52:47 +08:00
|
|
|
fatal("unsupported mac %s", name);
|
2001-02-15 11:01:59 +08:00
|
|
|
/* truncate the key */
|
|
|
|
if (datafellows & SSH_BUG_HMAC)
|
|
|
|
mac->key_len = 16;
|
2000-04-16 09:52:47 +08:00
|
|
|
mac->name = name;
|
|
|
|
mac->key = NULL;
|
|
|
|
mac->enabled = 0;
|
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2000-04-16 09:52:47 +08:00
|
|
|
choose_comp(Comp *comp, char *client, char *server)
|
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
|
|
|
fatal("no matching comp found: client %s server %s", client, server);
|
2005-07-26 19:54:56 +08:00
|
|
|
if (strcmp(name, "zlib@openssh.com") == 0) {
|
|
|
|
comp->type = COMP_DELAYED;
|
|
|
|
} else if (strcmp(name, "zlib") == 0) {
|
|
|
|
comp->type = COMP_ZLIB;
|
2000-04-16 09:52:47 +08:00
|
|
|
} else if (strcmp(name, "none") == 0) {
|
2005-07-26 19:54:56 +08:00
|
|
|
comp->type = COMP_NONE;
|
2000-04-16 09:52:47 +08:00
|
|
|
} else {
|
|
|
|
fatal("unsupported comp %s", name);
|
|
|
|
}
|
|
|
|
comp->name = name;
|
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2000-04-16 09:52:47 +08:00
|
|
|
choose_kex(Kex *k, char *client, char *server)
|
|
|
|
{
|
2013-04-23 17:24:32 +08:00
|
|
|
const struct kexalg *kexalg;
|
|
|
|
|
2001-03-11 09:49:19 +08:00
|
|
|
k->name = match_list(client, server, NULL);
|
2000-04-16 09:52:47 +08:00
|
|
|
if (k->name == NULL)
|
2007-06-05 16:23:28 +08:00
|
|
|
fatal("Unable to negotiate a key exchange method");
|
2013-04-23 17:24:32 +08:00
|
|
|
if ((kexalg = kex_alg_by_name(k->name)) == NULL)
|
|
|
|
fatal("unsupported kex alg %s", k->name);
|
|
|
|
k->kex_type = kexalg->type;
|
|
|
|
k->evp_md = kexalg->mdfunc();
|
|
|
|
k->ec_nid = kexalg->ec_nid;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2005-11-05 12:19:35 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2000-04-16 09:52:47 +08:00
|
|
|
choose_hostkeyalg(Kex *k, char *client, char *server)
|
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *hostkeyalg = match_list(client, server, NULL);
|
2000-11-13 19:57:25 +08:00
|
|
|
if (hostkeyalg == NULL)
|
2000-04-16 09:52:47 +08:00
|
|
|
fatal("no hostkey alg");
|
2000-11-13 19:57:25 +08:00
|
|
|
k->hostkey_type = key_type_from_name(hostkeyalg);
|
|
|
|
if (k->hostkey_type == KEY_UNSPEC)
|
|
|
|
fatal("bad hostkey alg '%s'", hostkeyalg);
|
2001-01-09 08:35:42 +08:00
|
|
|
xfree(hostkeyalg);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2003-11-21 20:48:55 +08:00
|
|
|
static int
|
2003-02-24 08:53:32 +08:00
|
|
|
proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
|
|
|
|
{
|
|
|
|
static int check[] = {
|
|
|
|
PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
|
|
|
|
};
|
|
|
|
int *idx;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (idx = &check[0]; *idx != -1; idx++) {
|
|
|
|
if ((p = strchr(my[*idx], ',')) != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
if ((p = strchr(peer[*idx], ',')) != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
if (strcmp(my[*idx], peer[*idx]) != 0) {
|
|
|
|
debug2("proposal mismatch: my %s peer %s",
|
|
|
|
my[*idx], peer[*idx]);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug2("proposals match");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static void
|
2001-04-04 10:00:54 +08:00
|
|
|
kex_choose_conf(Kex *kex)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2001-04-04 10:00:54 +08:00
|
|
|
Newkeys *newkeys;
|
2001-04-04 09:56:17 +08:00
|
|
|
char **my, **peer;
|
|
|
|
char **cprop, **sprop;
|
2001-04-04 10:00:54 +08:00
|
|
|
int nenc, nmac, ncomp;
|
2013-01-09 13:12:19 +08:00
|
|
|
u_int mode, ctos, need, authlen;
|
2003-02-24 08:53:32 +08:00
|
|
|
int first_kex_follows, type;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2003-02-24 08:53:32 +08:00
|
|
|
my = kex_buf2prop(&kex->my, NULL);
|
|
|
|
peer = kex_buf2prop(&kex->peer, &first_kex_follows);
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2001-04-04 10:00:54 +08:00
|
|
|
if (kex->server) {
|
2001-04-04 09:56:17 +08:00
|
|
|
cprop=peer;
|
|
|
|
sprop=my;
|
|
|
|
} else {
|
|
|
|
cprop=my;
|
|
|
|
sprop=peer;
|
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2010-01-08 13:50:41 +08:00
|
|
|
/* Check whether server offers roaming */
|
|
|
|
if (!kex->server) {
|
|
|
|
char *roaming;
|
|
|
|
roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
|
|
|
|
if (roaming) {
|
|
|
|
kex->roaming = 1;
|
|
|
|
xfree(roaming);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
/* Algorithm Negotiation */
|
2000-04-16 09:52:47 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2006-03-26 11:19:21 +08:00
|
|
|
newkeys = xcalloc(1, sizeof(*newkeys));
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->newkeys[mode] = newkeys;
|
2007-06-05 16:23:28 +08:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
|
|
|
(kex->server && mode == MODE_IN);
|
2000-04-16 09:52:47 +08: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;
|
2013-01-09 13:12:19 +08:00
|
|
|
choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
|
|
|
|
/* ignore mac for authenticated encryption */
|
|
|
|
authlen = cipher_authlen(newkeys->enc.cipher);
|
|
|
|
if (authlen == 0)
|
|
|
|
choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
|
2001-04-04 10:00:54 +08:00
|
|
|
choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
|
2000-04-16 09:52:47 +08:00
|
|
|
debug("kex: %s %s %s %s",
|
|
|
|
ctos ? "client->server" : "server->client",
|
2001-04-04 10:00:54 +08:00
|
|
|
newkeys->enc.name,
|
2013-01-09 13:12:19 +08:00
|
|
|
authlen == 0 ? newkeys->mac.name : "<implicit>",
|
2001-04-04 10:00:54 +08:00
|
|
|
newkeys->comp.name);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2001-04-04 10:00:54 +08:00
|
|
|
choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
|
|
|
|
choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
2000-04-16 09:52:47 +08:00
|
|
|
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
|
|
|
|
need = 0;
|
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-05 07:46:07 +08:00
|
|
|
newkeys = kex->newkeys[mode];
|
2002-02-19 12:21:23 +08:00
|
|
|
if (need < newkeys->enc.key_len)
|
|
|
|
need = newkeys->enc.key_len;
|
|
|
|
if (need < newkeys->enc.block_size)
|
|
|
|
need = newkeys->enc.block_size;
|
2013-01-09 13:12:19 +08:00
|
|
|
if (need < newkeys->enc.iv_len)
|
|
|
|
need = newkeys->enc.iv_len;
|
2001-04-04 10:00:54 +08:00
|
|
|
if (need < newkeys->mac.key_len)
|
|
|
|
need = newkeys->mac.key_len;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2000-05-30 11:44:51 +08:00
|
|
|
/* XXX need runden? */
|
2001-04-04 10:00:54 +08:00
|
|
|
kex->we_need = need;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2003-02-24 08:53:32 +08:00
|
|
|
/* ignore the next message if the proposals do not match */
|
2003-11-21 20:48:55 +08:00
|
|
|
if (first_kex_follows && !proposals_match(my, peer) &&
|
2005-07-17 15:22:45 +08:00
|
|
|
!(datafellows & SSH_BUG_FIRSTKEX)) {
|
2003-02-24 08:53:32 +08:00
|
|
|
type = packet_read();
|
|
|
|
debug2("skipping next packet (type %u)", type);
|
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_prop_free(my);
|
|
|
|
kex_prop_free(peer);
|
|
|
|
}
|
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
static u_char *
|
2005-11-05 12:19:35 +08:00
|
|
|
derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
|
|
|
|
BIGNUM *shared_secret)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
EVP_MD_CTX md;
|
|
|
|
char c = id;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int have;
|
2005-11-05 12:19:35 +08:00
|
|
|
int mdsz;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_char *digest;
|
2005-07-17 15:02:09 +08:00
|
|
|
|
2005-11-05 12:19:35 +08:00
|
|
|
if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0)
|
|
|
|
fatal("bad kex md size %d", mdsz);
|
2006-03-26 10:58:55 +08:00
|
|
|
digest = xmalloc(roundup(need, mdsz));
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_put_bignum2(&b, shared_secret);
|
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
/* K1 = HASH(K || H || "A" || session_id) */
|
2005-11-05 12:19:35 +08:00
|
|
|
EVP_DigestInit(&md, kex->evp_md);
|
2001-05-01 07:06:57 +08:00
|
|
|
if (!(datafellows & SSH_BUG_DERIVEKEY))
|
|
|
|
EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
|
2005-11-05 12:19:35 +08:00
|
|
|
EVP_DigestUpdate(&md, hash, hashlen);
|
2001-04-05 07:46:07 +08:00
|
|
|
EVP_DigestUpdate(&md, &c, 1);
|
2001-04-04 10:00:54 +08:00
|
|
|
EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
|
2001-04-04 09:56:17 +08:00
|
|
|
EVP_DigestFinal(&md, digest, NULL);
|
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
/*
|
|
|
|
* expand key:
|
|
|
|
* Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
|
|
|
|
* Key = K1 || K2 || ... || Kn
|
|
|
|
*/
|
2001-04-04 09:56:17 +08:00
|
|
|
for (have = mdsz; need > have; have += mdsz) {
|
2005-11-05 12:19:35 +08:00
|
|
|
EVP_DigestInit(&md, kex->evp_md);
|
2001-05-01 07:06:57 +08:00
|
|
|
if (!(datafellows & SSH_BUG_DERIVEKEY))
|
|
|
|
EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
|
2005-11-05 12:19:35 +08:00
|
|
|
EVP_DigestUpdate(&md, hash, hashlen);
|
2001-04-04 09:56:17 +08:00
|
|
|
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 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
Newkeys *current_keys[MODE_MAX];
|
2001-04-04 10:00:54 +08:00
|
|
|
|
2001-03-11 09:49:19 +08:00
|
|
|
#define NKEYS 6
|
2001-04-04 09:56:17 +08:00
|
|
|
void
|
2005-11-05 12:19:35 +08:00
|
|
|
kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *keys[NKEYS];
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i, mode, ctos;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2005-11-05 12:19:35 +08:00
|
|
|
for (i = 0; i < NKEYS; i++) {
|
|
|
|
keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
|
|
|
|
shared_secret);
|
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2002-12-23 10:04:22 +08:00
|
|
|
debug2("kex_derive_keys");
|
2000-04-16 09:52:47 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-05 07:46:07 +08:00
|
|
|
current_keys[mode] = kex->newkeys[mode];
|
|
|
|
kex->newkeys[mode] = NULL;
|
2006-03-26 11:10:14 +08:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
|
|
|
(kex->server && mode == MODE_IN);
|
2001-04-05 07:46:07 +08: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 09:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2001-04-04 10:00:54 +08:00
|
|
|
Newkeys *
|
|
|
|
kex_get_newkeys(int mode)
|
|
|
|
{
|
2001-04-05 07:46:07 +08:00
|
|
|
Newkeys *ret;
|
|
|
|
|
|
|
|
ret = current_keys[mode];
|
|
|
|
current_keys[mode] = NULL;
|
|
|
|
return ret;
|
2001-04-04 10:00:54 +08:00
|
|
|
}
|
|
|
|
|
2004-05-13 14:30:44 +08:00
|
|
|
void
|
|
|
|
derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
|
|
|
|
u_int8_t cookie[8], u_int8_t id[16])
|
|
|
|
{
|
|
|
|
const EVP_MD *evp_md = EVP_md5();
|
|
|
|
EVP_MD_CTX md;
|
|
|
|
u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
EVP_DigestInit(&md, evp_md);
|
|
|
|
|
|
|
|
len = BN_num_bytes(host_modulus);
|
2005-06-17 10:59:34 +08:00
|
|
|
if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
|
2004-05-13 14:30:44 +08:00
|
|
|
fatal("%s: bad host modulus (len %d)", __func__, len);
|
|
|
|
BN_bn2bin(host_modulus, nbuf);
|
|
|
|
EVP_DigestUpdate(&md, nbuf, len);
|
|
|
|
|
|
|
|
len = BN_num_bytes(server_modulus);
|
2005-06-17 10:59:34 +08:00
|
|
|
if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
|
2004-05-13 14:30:44 +08:00
|
|
|
fatal("%s: bad server modulus (len %d)", __func__, len);
|
|
|
|
BN_bn2bin(server_modulus, nbuf);
|
|
|
|
EVP_DigestUpdate(&md, nbuf, len);
|
|
|
|
|
|
|
|
EVP_DigestUpdate(&md, cookie, 8);
|
|
|
|
|
2004-05-13 14:31:48 +08:00
|
|
|
EVP_DigestFinal(&md, obuf, NULL);
|
2004-05-13 14:30:44 +08:00
|
|
|
memcpy(id, obuf, 16);
|
|
|
|
|
|
|
|
memset(nbuf, 0, sizeof(nbuf));
|
|
|
|
memset(obuf, 0, sizeof(obuf));
|
|
|
|
memset(&md, 0, sizeof(md));
|
|
|
|
}
|
|
|
|
|
2010-08-31 20:41:14 +08:00
|
|
|
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
|
2001-04-04 09:56:17 +08:00
|
|
|
void
|
|
|
|
dump_digest(char *msg, u_char *digest, int len)
|
|
|
|
{
|
2010-08-31 20:41:14 +08:00
|
|
|
int i;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2007-02-19 19:10:25 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
2001-04-04 09:56:17 +08:00
|
|
|
fprintf(stderr, "%02x", digest[i]);
|
|
|
|
if (i%32 == 31)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
else if (i%8 == 7)
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|