2015-01-30 00:22:25 +00:00
|
|
|
/* $OpenBSD: kex.c,v 1.105 2015/01/30 00:22:25 djm Exp $ */
|
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"
|
|
|
|
|
2015-01-20 23:14:00 +00:00
|
|
|
#include <sys/param.h> /* MAX roundup */
|
2006-08-05 01:02:17 +00:00
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <signal.h>
|
2006-09-01 05:38:36 +00:00
|
|
|
#include <stdarg.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-05-15 04:24:09 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <openssl/crypto.h>
|
2014-05-15 04:24:09 +00:00
|
|
|
#endif
|
2006-08-05 02:39:39 +00:00
|
|
|
|
|
|
|
#include "ssh2.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "packet.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "cipher.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshkey.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "kex.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"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "misc.h"
|
2001-04-04 01:56:17 +00:00
|
|
|
#include "dispatch.h"
|
2002-03-22 02:30:41 +00:00
|
|
|
#include "monitor.h"
|
2010-01-08 05:50:41 +00:00
|
|
|
#include "roaming.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "sshbuf.h"
|
2014-01-09 23:58:53 +00:00
|
|
|
#include "digest.h"
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2006-03-16 07:22:18 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
|
|
|
|
# if defined(HAVE_EVP_SHA256)
|
2006-03-15 02:02:28 +00:00
|
|
|
# define evp_ssh_sha256 EVP_sha256
|
2006-03-16 07:22:18 +00:00
|
|
|
# else
|
2006-03-15 01:08:28 +00:00
|
|
|
extern const EVP_MD *evp_ssh_sha256(void);
|
2006-03-16 07:22:18 +00:00
|
|
|
# endif
|
2006-03-16 04:17:05 +00:00
|
|
|
#endif
|
2006-03-15 01:08:28 +00:00
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
/* prototype */
|
2015-01-19 20:16:15 +00:00
|
|
|
static int kex_choose_conf(struct ssh *);
|
|
|
|
static int kex_input_newkeys(int, u_int32_t, void *);
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2013-04-23 09:24:32 +00:00
|
|
|
struct kexalg {
|
|
|
|
char *name;
|
2015-01-19 20:16:15 +00:00
|
|
|
u_int type;
|
2013-04-23 09:24:32 +00:00
|
|
|
int ec_nid;
|
2014-01-09 23:58:53 +00:00
|
|
|
int hash_alg;
|
2013-04-23 09:24:32 +00:00
|
|
|
};
|
|
|
|
static const struct kexalg kexalgs[] = {
|
2014-05-15 04:24:09 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
2014-01-09 23:58:53 +00:00
|
|
|
{ KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
|
|
|
|
{ KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
|
|
|
|
{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
|
2013-05-10 08:11:55 +00:00
|
|
|
#ifdef HAVE_EVP_SHA256
|
2014-01-09 23:58:53 +00:00
|
|
|
{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
|
2014-05-15 04:24:09 +00:00
|
|
|
#endif /* HAVE_EVP_SHA256 */
|
2013-05-10 08:11:55 +00:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2014-01-09 23:58:53 +00:00
|
|
|
{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
|
|
|
|
NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
|
|
|
|
{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
|
|
|
|
SSH_DIGEST_SHA384 },
|
2013-11-09 07:39:25 +00:00
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
2014-01-09 23:58:53 +00:00
|
|
|
{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
|
|
|
|
SSH_DIGEST_SHA512 },
|
2014-05-15 04:24:09 +00:00
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
|
|
|
#endif /* WITH_OPENSSL */
|
2015-01-14 15:21:31 +00:00
|
|
|
#if defined(HAVE_EVP_SHA256) || !defined(WITH_OPENSSL)
|
2014-01-09 23:58:53 +00:00
|
|
|
{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
|
2015-01-14 15:21:31 +00:00
|
|
|
#endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
|
2014-01-09 23:58:53 +00:00
|
|
|
{ NULL, -1, -1, -1},
|
2013-04-23 09:24:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
2013-11-08 01:16:49 +00:00
|
|
|
kex_alg_list(char sep)
|
2013-04-23 09:24:32 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
char *ret = NULL, *tmp;
|
2013-04-23 09:24:32 +00:00
|
|
|
size_t nlen, rlen = 0;
|
|
|
|
const struct kexalg *k;
|
|
|
|
|
|
|
|
for (k = kexalgs; k->name != NULL; k++) {
|
|
|
|
if (ret != NULL)
|
2013-11-08 01:16:49 +00:00
|
|
|
ret[rlen++] = sep;
|
2013-04-23 09:24:32 +00:00
|
|
|
nlen = strlen(k->name);
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = tmp;
|
2013-04-23 09:24:32 +00:00
|
|
|
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 12:11:14 +00: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;
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((s = cp = strdup(names)) == NULL)
|
|
|
|
return 0;
|
2010-09-24 12:11:14 +00:00
|
|
|
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
|
|
|
(p = strsep(&cp, ","))) {
|
2013-04-23 09:24:32 +00:00
|
|
|
if (kex_alg_by_name(p) == NULL) {
|
2010-09-24 12:11:14 +00:00
|
|
|
error("Unsupported KEX algorithm \"%.100s\"", p);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(s);
|
2010-09-24 12:11:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug3("kex names ok: [%s]", names);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(s);
|
2010-09-24 12:11:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
/* put algorithm proposal into buffer */
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2005-06-17 02:59:34 +00:00
|
|
|
u_int i;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
sshbuf_reset(b);
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2002-03-27 17:42:57 +00:00
|
|
|
/*
|
|
|
|
* add a dummy cookie, the cookie will be overwritten by
|
|
|
|
* kex_send_kexinit(), each time a kexinit is set
|
|
|
|
*/
|
2015-01-19 20:16:15 +00:00
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
|
|
|
if ((r = sshbuf_put_u8(b, 0)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
|
|
|
if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */
|
|
|
|
(r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */
|
|
|
|
return r;
|
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-04-04 01:56:17 +00:00
|
|
|
/* parse buffer and return algorithm proposal */
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
|
2000-05-30 03:44:51 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
u_char v;
|
2007-06-05 08:23:28 +00:00
|
|
|
u_int i;
|
2015-01-19 20:16:15 +00:00
|
|
|
char **proposal = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*propp = NULL;
|
|
|
|
if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((b = sshbuf_fromb(raw)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
|
|
|
|
goto out;
|
2000-05-30 03:44:51 +00:00
|
|
|
/* extract kex init proposal strings */
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
|
|
|
|
goto out;
|
2001-04-04 01:56:17 +00:00
|
|
|
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 */
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshbuf_get_u8(b, &v)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(b, &i)) != 0)
|
|
|
|
goto out;
|
2003-02-24 00:53:32 +00:00
|
|
|
if (first_kex_follows != NULL)
|
|
|
|
*first_kex_follows = i;
|
2015-01-19 20:16:15 +00:00
|
|
|
debug2("kex_parse_kexinit: first_kex_follows %d ", v);
|
2007-06-05 08:23:28 +00:00
|
|
|
debug2("kex_parse_kexinit: reserved %u ", i);
|
2015-01-19 20:16:15 +00:00
|
|
|
r = 0;
|
|
|
|
*propp = proposal;
|
|
|
|
out:
|
|
|
|
if (r != 0 && proposal != NULL)
|
|
|
|
kex_prop_free(proposal);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
2000-05-30 03:44:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
void
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_prop_free(char **proposal)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2005-06-17 02:59:34 +00:00
|
|
|
u_int i;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
2013-06-01 21:31:17 +00:00
|
|
|
free(proposal[i]);
|
|
|
|
free(proposal);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2007-06-05 08:23:28 +00:00
|
|
|
/* ARGSUSED */
|
2015-01-19 20:07:45 +00:00
|
|
|
static int
|
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);
|
2015-01-19 20:07:45 +00:00
|
|
|
return 0;
|
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
|
2015-01-19 20:16:15 +00:00
|
|
|
kex_reset_dispatch(struct ssh *ssh)
|
2001-04-04 17:57:54 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
|
2002-01-22 12:24:13 +00:00
|
|
|
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
2015-01-19 20:16:15 +00:00
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
2001-04-04 17:57:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_send_newkeys(struct ssh *ssh)
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
2001-04-04 17:52:53 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
kex_reset_dispatch(ssh);
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
2001-04-04 01:56:17 +00:00
|
|
|
debug("SSH2_MSG_NEWKEYS sent");
|
2002-12-23 02:04:22 +00:00
|
|
|
debug("expecting SSH2_MSG_NEWKEYS");
|
2015-01-19 20:16:15 +00:00
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
|
|
|
|
{
|
|
|
|
struct ssh *ssh = ctxt;
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
int r;
|
2001-04-05 02:05:21 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
debug("SSH2_MSG_NEWKEYS received");
|
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
|
|
|
|
if ((r = sshpkt_get_end(ssh)) != 0)
|
|
|
|
return r;
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->done = 1;
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_reset(kex->peer);
|
|
|
|
/* sshbuf_reset(kex->my); */
|
2001-04-04 01:56:17 +00:00
|
|
|
kex->flags &= ~KEX_INIT_SENT;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(kex->name);
|
2001-04-05 02:05:21 +00:00
|
|
|
kex->name = NULL;
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_send_kexinit(struct ssh *ssh)
|
2000-10-14 05:23:11 +00:00
|
|
|
{
|
2002-03-27 17:42:57 +00:00
|
|
|
u_char *cookie;
|
2015-01-19 20:16:15 +00:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
int r;
|
2002-03-27 17:42:57 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
if (kex == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
if (kex->flags & KEX_INIT_SENT)
|
|
|
|
return 0;
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->done = 0;
|
2002-03-27 17:42:57 +00:00
|
|
|
|
|
|
|
/* generate a random cookie */
|
2015-01-19 20:16:15 +00:00
|
|
|
if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
arc4random_buf(cookie, KEX_COOKIE_LEN);
|
|
|
|
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
|
|
|
|
(r = sshpkt_putb(ssh, kex->my)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
2001-04-04 01:56:17 +00:00
|
|
|
debug("SSH2_MSG_KEXINIT sent");
|
|
|
|
kex->flags |= KEX_INIT_SENT;
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2007-06-05 08:23:28 +00:00
|
|
|
/* ARGSUSED */
|
2015-01-19 20:07:45 +00:00
|
|
|
int
|
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
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct ssh *ssh = ctxt;
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
const u_char *ptr;
|
2015-01-19 19:52:16 +00:00
|
|
|
u_int i;
|
|
|
|
size_t dlen;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
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)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
ptr = sshpkt_ptr(ssh, &dlen);
|
|
|
|
if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
|
|
|
|
return r;
|
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++)
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0)
|
|
|
|
return r;
|
2001-04-04 23:50:21 +00:00
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0)
|
|
|
|
return r;
|
2012-09-06 11:19:51 +00: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.
|
|
|
|
*/
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
|
|
|
|
(r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
|
|
|
|
(r = sshpkt_get_end(ssh)) != 0)
|
|
|
|
return r;
|
2001-04-04 23:50:21 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
if (!(kex->flags & KEX_INIT_SENT))
|
|
|
|
if ((r = kex_send_kexinit(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((r = kex_choose_conf(ssh)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
|
|
|
|
return (kex->kex[kex->kex_type])(ssh);
|
|
|
|
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp)
|
|
|
|
{
|
|
|
|
struct kex *kex;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*kexp = NULL;
|
|
|
|
if ((kex = calloc(1, sizeof(*kex))) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((kex->peer = sshbuf_new()) == NULL ||
|
|
|
|
(kex->my = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = kex_prop2buf(kex->my, proposal)) != 0)
|
|
|
|
goto out;
|
|
|
|
kex->done = 0;
|
|
|
|
kex_reset_dispatch(ssh);
|
|
|
|
r = 0;
|
|
|
|
*kexp = kex;
|
|
|
|
out:
|
|
|
|
if (r != 0)
|
|
|
|
kex_free(kex);
|
|
|
|
return r;
|
2000-10-14 05:23:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 19:52:16 +00:00
|
|
|
void
|
|
|
|
kex_free_newkeys(struct newkeys *newkeys)
|
|
|
|
{
|
|
|
|
if (newkeys == NULL)
|
|
|
|
return;
|
|
|
|
if (newkeys->enc.key) {
|
|
|
|
explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
|
|
|
|
free(newkeys->enc.key);
|
|
|
|
newkeys->enc.key = NULL;
|
|
|
|
}
|
|
|
|
if (newkeys->enc.iv) {
|
|
|
|
explicit_bzero(newkeys->enc.iv, newkeys->enc.block_size);
|
|
|
|
free(newkeys->enc.iv);
|
|
|
|
newkeys->enc.iv = NULL;
|
|
|
|
}
|
|
|
|
free(newkeys->enc.name);
|
|
|
|
explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
|
|
|
|
free(newkeys->comp.name);
|
|
|
|
explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
|
|
|
|
mac_clear(&newkeys->mac);
|
|
|
|
if (newkeys->mac.key) {
|
|
|
|
explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
|
|
|
|
free(newkeys->mac.key);
|
|
|
|
newkeys->mac.key = NULL;
|
|
|
|
}
|
|
|
|
free(newkeys->mac.name);
|
|
|
|
explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
|
|
|
|
explicit_bzero(newkeys, sizeof(*newkeys));
|
|
|
|
free(newkeys);
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
void
|
|
|
|
kex_free(struct kex *kex)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
u_int mode;
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
if (kex->dh)
|
|
|
|
DH_free(kex->dh);
|
|
|
|
if (kex->ec_client_key)
|
|
|
|
EC_KEY_free(kex->ec_client_key);
|
|
|
|
#endif
|
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
|
|
|
kex_free_newkeys(kex->newkeys[mode]);
|
|
|
|
kex->newkeys[mode] = NULL;
|
2015-01-19 19:52:16 +00:00
|
|
|
}
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_free(kex->peer);
|
|
|
|
sshbuf_free(kex->my);
|
|
|
|
free(kex->session_id);
|
|
|
|
free(kex->client_version_string);
|
|
|
|
free(kex->server_version_string);
|
|
|
|
free(kex);
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
|
|
|
|
kex_free(ssh->kex);
|
|
|
|
ssh->kex = NULL;
|
|
|
|
return r;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
choose_enc(struct sshenc *enc, char *client, char *server)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_NO_CIPHER_ALG_MATCH;
|
2002-02-19 04:21:23 +00:00
|
|
|
if ((enc->cipher = cipher_by_name(name)) == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2000-04-16 01:52:47 +00:00
|
|
|
enc->name = name;
|
|
|
|
enc->enabled = 0;
|
|
|
|
enc->iv = NULL;
|
2013-01-09 05:12:19 +00:00
|
|
|
enc->iv_len = cipher_ivlen(enc->cipher);
|
2000-04-16 01:52:47 +00:00
|
|
|
enc->key = NULL;
|
2002-02-19 04:21:23 +00:00
|
|
|
enc->key_len = cipher_keylen(enc->cipher);
|
|
|
|
enc->block_size = cipher_blocksize(enc->cipher);
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2006-03-26 03:10:14 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_NO_MAC_ALG_MATCH;
|
2007-06-05 08:30:18 +00:00
|
|
|
if (mac_setup(mac, name) < 0)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2001-02-15 03:01:59 +00:00
|
|
|
/* truncate the key */
|
2015-01-19 20:16:15 +00:00
|
|
|
if (ssh->compat & SSH_BUG_HMAC)
|
2001-02-15 03:01:59 +00:00
|
|
|
mac->key_len = 16;
|
2000-04-16 01:52:47 +00:00
|
|
|
mac->name = name;
|
|
|
|
mac->key = NULL;
|
|
|
|
mac->enabled = 0;
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2006-03-26 03:10:14 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
choose_comp(struct sshcomp *comp, char *client, char *server)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
2000-04-16 01:52:47 +00:00
|
|
|
if (name == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_NO_COMPRESS_ALG_MATCH;
|
2005-07-26 11:54:56 +00: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 01:52:47 +00:00
|
|
|
} else if (strcmp(name, "none") == 0) {
|
2005-07-26 11:54:56 +00:00
|
|
|
comp->type = COMP_NONE;
|
2000-04-16 01:52:47 +00:00
|
|
|
} else {
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
comp->name = name;
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2006-03-26 03:10:14 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
choose_kex(struct kex *k, char *client, char *server)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2013-04-23 09:24:32 +00:00
|
|
|
const struct kexalg *kexalg;
|
|
|
|
|
2001-03-11 01:49:19 +00:00
|
|
|
k->name = match_list(client, server, NULL);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
2000-04-16 01:52:47 +00:00
|
|
|
if (k->name == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_NO_KEX_ALG_MATCH;
|
2013-04-23 09:24:32 +00:00
|
|
|
if ((kexalg = kex_alg_by_name(k->name)) == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2013-04-23 09:24:32 +00:00
|
|
|
k->kex_type = kexalg->type;
|
2014-01-09 23:58:53 +00:00
|
|
|
k->hash_alg = kexalg->hash_alg;
|
2013-04-23 09:24:32 +00:00
|
|
|
k->ec_nid = kexalg->ec_nid;
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2005-11-05 04:19:35 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
choose_hostkeyalg(struct kex *k, char *client, char *server)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2001-03-11 01:49:19 +00:00
|
|
|
char *hostkeyalg = match_list(client, server, NULL);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
if (hostkeyalg == NULL)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
|
|
|
|
k->hostkey_type = sshkey_type_from_name(hostkeyalg);
|
2000-11-13 11:57:25 +00:00
|
|
|
if (k->hostkey_type == KEY_UNSPEC)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2015-01-26 06:10:03 +00:00
|
|
|
k->hostkey_nid = sshkey_ecdsa_nid_from_name(hostkeyalg);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(hostkeyalg);
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2003-11-21 12:48:55 +00:00
|
|
|
static int
|
2003-02-24 00:53:32 +00: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);
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
kex_choose_conf(struct ssh *ssh)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
struct newkeys *newkeys;
|
|
|
|
char **my = NULL, **peer = NULL;
|
2001-04-04 01:56:17 +00:00
|
|
|
char **cprop, **sprop;
|
2001-04-04 02:00:54 +00:00
|
|
|
int nenc, nmac, ncomp;
|
2014-01-25 22:37:25 +00:00
|
|
|
u_int mode, ctos, need, dh_need, authlen;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r, first_kex_follows;
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0 ||
|
|
|
|
(r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
|
|
|
|
goto out;
|
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
|
|
|
|
2010-01-08 05:50:41 +00:00
|
|
|
/* Check whether server offers roaming */
|
|
|
|
if (!kex->server) {
|
2015-01-19 20:16:15 +00:00
|
|
|
char *roaming = match_list(KEX_RESUME,
|
|
|
|
peer[PROPOSAL_KEX_ALGS], NULL);
|
|
|
|
|
2010-01-08 05:50:41 +00:00
|
|
|
if (roaming) {
|
|
|
|
kex->roaming = 1;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(roaming);
|
2010-01-08 05:50:41 +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++) {
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-04-04 23:46:07 +00:00
|
|
|
kex->newkeys[mode] = newkeys;
|
2007-06-05 08:23:28 +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;
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = choose_enc(&newkeys->enc, cprop[nenc],
|
|
|
|
sprop[nenc])) != 0)
|
|
|
|
goto out;
|
2013-01-09 05:12:19 +00:00
|
|
|
authlen = cipher_authlen(newkeys->enc.cipher);
|
2015-01-19 20:16:15 +00:00
|
|
|
/* ignore mac for authenticated encryption */
|
|
|
|
if (authlen == 0 &&
|
|
|
|
(r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
|
|
|
|
sprop[nmac])) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
|
|
|
|
sprop[ncomp])) != 0)
|
|
|
|
goto out;
|
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,
|
2013-01-09 05:12:19 +00:00
|
|
|
authlen == 0 ? newkeys->mac.name : "<implicit>",
|
2001-04-04 02:00:54 +00:00
|
|
|
newkeys->comp.name);
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
|
|
|
|
sprop[PROPOSAL_KEX_ALGS])) != 0 ||
|
|
|
|
(r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
|
|
|
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0)
|
|
|
|
goto out;
|
2014-01-25 22:37:25 +00:00
|
|
|
need = dh_need = 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-04 23:46:07 +00:00
|
|
|
newkeys = kex->newkeys[mode];
|
2014-01-25 22:38:03 +00:00
|
|
|
need = MAX(need, newkeys->enc.key_len);
|
|
|
|
need = MAX(need, newkeys->enc.block_size);
|
|
|
|
need = MAX(need, newkeys->enc.iv_len);
|
|
|
|
need = MAX(need, newkeys->mac.key_len);
|
|
|
|
dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher));
|
|
|
|
dh_need = MAX(dh_need, newkeys->enc.block_size);
|
|
|
|
dh_need = MAX(dh_need, newkeys->enc.iv_len);
|
|
|
|
dh_need = MAX(dh_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;
|
2014-01-25 22:37:25 +00:00
|
|
|
kex->dh_need = dh_need;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2003-02-24 00:53:32 +00:00
|
|
|
/* ignore the next message if the proposals do not match */
|
2003-11-21 12:48:55 +00:00
|
|
|
if (first_kex_follows && !proposals_match(my, peer) &&
|
2015-01-19 20:16:15 +00:00
|
|
|
!(ssh->compat & SSH_BUG_FIRSTKEX))
|
|
|
|
ssh->dispatch_skip_packets = 1;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2001-04-04 01:56:17 +00:00
|
|
|
kex_prop_free(my);
|
|
|
|
kex_prop_free(peer);
|
2015-01-19 20:16:15 +00:00
|
|
|
return r;
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
static int
|
|
|
|
derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
|
|
|
|
const struct sshbuf *shared_secret, u_char **keyp)
|
2001-04-04 01:56:17 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
struct ssh_digest_ctx *hashctx = NULL;
|
2001-04-04 01:56:17 +00:00
|
|
|
char c = id;
|
2005-06-17 02:59:34 +00:00
|
|
|
u_int have;
|
2014-01-09 23:58:53 +00:00
|
|
|
size_t mdsz;
|
2005-06-17 02:59:34 +00:00
|
|
|
u_char *digest;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
2005-07-17 07:02:09 +00:00
|
|
|
|
2014-01-09 23:58:53 +00:00
|
|
|
if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((digest = calloc(1, roundup(need, mdsz))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2001-04-04 23:46:07 +00:00
|
|
|
/* K1 = HASH(K || H || "A" || session_id) */
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
|
|
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
2014-01-09 23:58:53 +00:00
|
|
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
|
|
|
ssh_digest_update(hashctx, &c, 1) != 0 ||
|
|
|
|
ssh_digest_update(hashctx, kex->session_id,
|
2015-01-19 20:16:15 +00:00
|
|
|
kex->session_id_len) != 0 ||
|
|
|
|
ssh_digest_final(hashctx, digest, mdsz) != 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-09 23:58:53 +00:00
|
|
|
ssh_digest_free(hashctx);
|
2015-01-19 20:16:15 +00:00
|
|
|
hashctx = NULL;
|
2001-04-04 01:56:17 +00:00
|
|
|
|
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) {
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
|
|
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
2014-01-09 23:58:53 +00:00
|
|
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
2015-01-19 20:16:15 +00:00
|
|
|
ssh_digest_update(hashctx, digest, have) != 0 ||
|
|
|
|
ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-09 23:58:53 +00:00
|
|
|
ssh_digest_free(hashctx);
|
2015-01-19 20:16:15 +00:00
|
|
|
hashctx = NULL;
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_KEX
|
|
|
|
fprintf(stderr, "key '%c'== ", c);
|
|
|
|
dump_digest("key", digest, need);
|
|
|
|
#endif
|
2015-01-19 20:16:15 +00:00
|
|
|
*keyp = digest;
|
|
|
|
digest = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
if (digest)
|
|
|
|
free(digest);
|
|
|
|
ssh_digest_free(hashctx);
|
|
|
|
return r;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
|
|
|
|
2001-03-11 01:49:19 +00:00
|
|
|
#define NKEYS 6
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
|
|
|
|
const struct sshbuf *shared_secret)
|
2000-04-16 01:52:47 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct kex *kex = ssh->kex;
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char *keys[NKEYS];
|
2015-01-19 20:16:15 +00:00
|
|
|
u_int i, j, mode, ctos;
|
|
|
|
int r;
|
2000-04-16 01:52:47 +00:00
|
|
|
|
2005-11-05 04:19:35 +00:00
|
|
|
for (i = 0; i < NKEYS; i++) {
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
|
|
|
|
shared_secret, &keys[i])) != 0) {
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
free(keys[j]);
|
|
|
|
return r;
|
|
|
|
}
|
2005-11-05 04:19:35 +00:00
|
|
|
}
|
2000-04-16 01:52:47 +00:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2006-03-26 03:10:14 +00:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
|
|
|
(kex->server && mode == MODE_IN);
|
2015-01-19 19:52:16 +00:00
|
|
|
kex->newkeys[mode]->enc.iv = keys[ctos ? 0 : 1];
|
|
|
|
kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
|
|
|
|
kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2015-01-19 20:16:15 +00:00
|
|
|
return 0;
|
2000-04-16 01:52:47 +00:00
|
|
|
}
|
2001-04-04 01:56:17 +00:00
|
|
|
|
2014-05-15 04:24:09 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
|
|
|
kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
|
|
|
|
const BIGNUM *secret)
|
2014-01-12 08:21:22 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct sshbuf *shared_secret;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((shared_secret = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
|
|
|
|
r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
|
|
|
|
sshbuf_free(shared_secret);
|
|
|
|
return r;
|
2014-01-12 08:21:22 +00:00
|
|
|
}
|
2014-05-15 04:24:09 +00:00
|
|
|
#endif
|
2014-01-12 08:21:22 +00:00
|
|
|
|
2014-05-15 04:24:09 +00:00
|
|
|
#ifdef WITH_SSH1
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
2004-05-13 06:30:44 +00:00
|
|
|
derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
|
|
|
|
u_int8_t cookie[8], u_int8_t id[16])
|
|
|
|
{
|
2015-01-30 00:22:25 +00:00
|
|
|
u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
|
2015-01-19 20:16:15 +00:00
|
|
|
struct ssh_digest_ctx *hashctx = NULL;
|
2015-01-30 00:22:25 +00:00
|
|
|
size_t hlen, slen;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
2004-05-13 06:30:44 +00:00
|
|
|
|
2015-01-30 00:22:25 +00:00
|
|
|
hlen = BN_num_bytes(host_modulus);
|
|
|
|
slen = BN_num_bytes(server_modulus);
|
|
|
|
if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) ||
|
|
|
|
slen < (512 / 8) || (u_int)slen > sizeof(sbuf))
|
2015-01-19 20:16:15 +00:00
|
|
|
return SSH_ERR_KEY_BITS_MISMATCH;
|
2015-01-30 00:22:25 +00:00
|
|
|
if (BN_bn2bin(host_modulus, hbuf) <= 0 ||
|
|
|
|
BN_bn2bin(server_modulus, sbuf) <= 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ssh_digest_update(hashctx, hbuf, hlen) != 0 ||
|
|
|
|
ssh_digest_update(hashctx, sbuf, slen) != 0 ||
|
2015-01-19 20:16:15 +00:00
|
|
|
ssh_digest_update(hashctx, cookie, 8) != 0 ||
|
|
|
|
ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-09 23:58:53 +00:00
|
|
|
memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
|
2015-01-19 20:16:15 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
ssh_digest_free(hashctx);
|
2015-01-30 00:22:25 +00:00
|
|
|
explicit_bzero(hbuf, sizeof(hbuf));
|
|
|
|
explicit_bzero(sbuf, sizeof(sbuf));
|
2014-02-04 00:20:14 +00:00
|
|
|
explicit_bzero(obuf, sizeof(obuf));
|
2015-01-19 20:16:15 +00:00
|
|
|
return r;
|
2004-05-13 06:30:44 +00:00
|
|
|
}
|
2014-05-15 04:24:09 +00:00
|
|
|
#endif
|
2004-05-13 06:30:44 +00:00
|
|
|
|
2010-08-31 12:41:14 +00:00
|
|
|
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
|
2001-04-04 01:56:17 +00:00
|
|
|
void
|
|
|
|
dump_digest(char *msg, u_char *digest, int len)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_dump_data(digest, len, stderr);
|
2001-04-04 01:56:17 +00:00
|
|
|
}
|
|
|
|
#endif
|