2020-03-13 12:01:56 +08:00
|
|
|
/* $OpenBSD: kex.c,v 1.158 2020/03/13 04:01:56 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"
|
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.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>
|
2018-12-27 11:25:24 +08:00
|
|
|
#include <unistd.h>
|
2019-10-28 12:53:25 +08:00
|
|
|
#ifdef HAVE_POLL_H
|
2018-12-27 11:25:24 +08:00
|
|
|
#include <poll.h>
|
2019-10-28 12:53:25 +08:00
|
|
|
#endif
|
2006-07-24 12:13:33 +08:00
|
|
|
|
2014-05-15 12:24:09 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <openssl/crypto.h>
|
2016-07-15 17:14:48 +08:00
|
|
|
#include <openssl/dh.h>
|
2014-05-15 12:24:09 +08:00
|
|
|
#endif
|
2006-08-05 10:39:39 +08:00
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
#include "ssh.h"
|
2006-08-05 10:39:39 +08:00
|
|
|
#include "ssh2.h"
|
2018-12-27 11:25:24 +08:00
|
|
|
#include "atomicio.h"
|
|
|
|
#include "version.h"
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "packet.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "cipher.h"
|
2015-01-20 04:16:15 +08:00
|
|
|
#include "sshkey.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"
|
2015-01-20 04:16:15 +08:00
|
|
|
#include "misc.h"
|
2001-04-04 09:56:17 +08:00
|
|
|
#include "dispatch.h"
|
2002-03-22 10:30:41 +08:00
|
|
|
#include "monitor.h"
|
2015-01-20 04:16:15 +08:00
|
|
|
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "sshbuf.h"
|
2014-01-10 07:58:53 +08:00
|
|
|
#include "digest.h"
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2001-06-25 13:01:22 +08:00
|
|
|
/* prototype */
|
2015-01-20 04:16:15 +08:00
|
|
|
static int kex_choose_conf(struct ssh *);
|
2017-05-30 22:23:52 +08:00
|
|
|
static int kex_input_newkeys(int, u_int32_t, struct ssh *);
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2015-08-22 07:57:48 +08:00
|
|
|
static const char *proposal_names[PROPOSAL_MAX] = {
|
|
|
|
"KEX algorithms",
|
|
|
|
"host key algorithms",
|
|
|
|
"ciphers ctos",
|
|
|
|
"ciphers stoc",
|
|
|
|
"MACs ctos",
|
|
|
|
"MACs stoc",
|
|
|
|
"compression ctos",
|
|
|
|
"compression stoc",
|
|
|
|
"languages ctos",
|
|
|
|
"languages stoc",
|
|
|
|
};
|
|
|
|
|
2013-04-23 17:24:32 +08:00
|
|
|
struct kexalg {
|
|
|
|
char *name;
|
2015-01-20 04:16:15 +08:00
|
|
|
u_int type;
|
2013-04-23 17:24:32 +08:00
|
|
|
int ec_nid;
|
2014-01-10 07:58:53 +08:00
|
|
|
int hash_alg;
|
2013-04-23 17:24:32 +08:00
|
|
|
};
|
|
|
|
static const struct kexalg kexalgs[] = {
|
2014-05-15 12:24:09 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2014-01-10 07:58:53 +08:00
|
|
|
{ KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
|
2016-05-02 18:26:04 +08:00
|
|
|
{ KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
|
|
|
|
{ KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
|
|
|
|
{ KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
|
|
|
|
{ KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
|
2014-01-10 07:58:53 +08:00
|
|
|
{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
|
2013-05-10 16:11:55 +08:00
|
|
|
#ifdef HAVE_EVP_SHA256
|
2014-01-10 07:58:53 +08:00
|
|
|
{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
|
2014-05-15 12:24:09 +08:00
|
|
|
#endif /* HAVE_EVP_SHA256 */
|
2013-05-10 16:11:55 +08:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2014-01-10 07:58:53 +08: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 15:39:25 +08:00
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
2014-01-10 07:58:53 +08:00
|
|
|
{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
|
|
|
|
SSH_DIGEST_SHA512 },
|
2014-05-15 12:24:09 +08:00
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
|
|
|
#endif /* WITH_OPENSSL */
|
2015-01-14 23:21:31 +08:00
|
|
|
#if defined(HAVE_EVP_SHA256) || !defined(WITH_OPENSSL)
|
2014-01-10 07:58:53 +08:00
|
|
|
{ KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
|
2016-09-23 01:52:53 +08:00
|
|
|
{ KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
|
2019-01-21 18:20:12 +08:00
|
|
|
{ KEX_SNTRUP4591761X25519_SHA512, KEX_KEM_SNTRUP4591761X25519_SHA512, 0,
|
|
|
|
SSH_DIGEST_SHA512 },
|
2015-01-14 23:21:31 +08:00
|
|
|
#endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
|
2019-10-09 06:40:39 +08:00
|
|
|
{ NULL, 0, -1, -1},
|
2013-04-23 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
2013-11-08 09:16:49 +08:00
|
|
|
kex_alg_list(char sep)
|
2013-04-23 17:24:32 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
char *ret = NULL, *tmp;
|
2013-04-23 17:24:32 +08:00
|
|
|
size_t nlen, rlen = 0;
|
|
|
|
const struct kexalg *k;
|
|
|
|
|
|
|
|
for (k = kexalgs; k->name != NULL; k++) {
|
|
|
|
if (ret != NULL)
|
2013-11-08 09:16:49 +08:00
|
|
|
ret[rlen++] = sep;
|
2013-04-23 17:24:32 +08:00
|
|
|
nlen = strlen(k->name);
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = tmp;
|
2013-04-23 17:24:32 +08: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 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;
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((s = cp = strdup(names)) == NULL)
|
|
|
|
return 0;
|
2010-09-24 20:11:14 +08:00
|
|
|
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);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(s);
|
2010-09-24 20:11:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug3("kex names ok: [%s]", names);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(s);
|
2010-09-24 20:11:14 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:01:34 +08:00
|
|
|
/*
|
|
|
|
* Concatenate algorithm names, avoiding duplicates in the process.
|
|
|
|
* Caller must free returned string.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
kex_names_cat(const char *a, const char *b)
|
|
|
|
{
|
2017-03-10 11:45:40 +08:00
|
|
|
char *ret = NULL, *tmp = NULL, *cp, *p, *m;
|
2015-07-30 08:01:34 +08:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (a == NULL || *a == '\0')
|
2018-07-04 21:49:31 +08:00
|
|
|
return strdup(b);
|
2015-07-30 08:01:34 +08:00
|
|
|
if (b == NULL || *b == '\0')
|
|
|
|
return strdup(a);
|
|
|
|
if (strlen(b) > 1024*1024)
|
|
|
|
return NULL;
|
|
|
|
len = strlen(a) + strlen(b) + 2;
|
|
|
|
if ((tmp = cp = strdup(b)) == NULL ||
|
|
|
|
(ret = calloc(1, len)) == NULL) {
|
|
|
|
free(tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strlcpy(ret, a, len);
|
|
|
|
for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
|
2017-03-10 11:45:40 +08:00
|
|
|
if ((m = match_list(ret, p, NULL)) != NULL) {
|
|
|
|
free(m);
|
2015-07-30 08:01:34 +08:00
|
|
|
continue; /* Algorithm already present */
|
2017-03-10 11:45:40 +08:00
|
|
|
}
|
2015-07-30 08:01:34 +08:00
|
|
|
if (strlcat(ret, ",", len) >= len ||
|
|
|
|
strlcat(ret, p, len) >= len) {
|
|
|
|
free(tmp);
|
|
|
|
free(ret);
|
|
|
|
return NULL; /* Shouldn't happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assemble a list of algorithms from a default list and a string from a
|
|
|
|
* configuration file. The user-provided string may begin with '+' to
|
2019-09-06 22:45:34 +08:00
|
|
|
* indicate that it should be appended to the default, '-' that the
|
|
|
|
* specified names should be removed, or '^' that they should be placed
|
|
|
|
* at the head.
|
2015-07-30 08:01:34 +08:00
|
|
|
*/
|
|
|
|
int
|
2018-07-04 21:49:31 +08:00
|
|
|
kex_assemble_names(char **listp, const char *def, const char *all)
|
2015-07-30 08:01:34 +08:00
|
|
|
{
|
2018-07-04 21:49:31 +08:00
|
|
|
char *cp, *tmp, *patterns;
|
|
|
|
char *list = NULL, *ret = NULL, *matching = NULL, *opatterns = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2015-07-30 08:01:34 +08:00
|
|
|
|
2019-09-06 09:58:50 +08:00
|
|
|
if (listp == NULL || def == NULL || all == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
if (*listp == NULL || **listp == '\0') {
|
2018-07-04 21:49:31 +08:00
|
|
|
if ((*listp = strdup(def)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2015-07-30 08:01:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-04 21:49:31 +08:00
|
|
|
|
|
|
|
list = *listp;
|
|
|
|
*listp = NULL;
|
|
|
|
if (*list == '+') {
|
|
|
|
/* Append names to default list */
|
|
|
|
if ((tmp = kex_names_cat(def, list + 1)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
list = tmp;
|
|
|
|
} else if (*list == '-') {
|
|
|
|
/* Remove names from default list */
|
|
|
|
if ((*listp = match_filter_blacklist(def, list + 1)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
/* filtering has already been done */
|
|
|
|
return 0;
|
2019-09-06 22:45:34 +08:00
|
|
|
} else if (*list == '^') {
|
|
|
|
/* Place names at head of default list */
|
|
|
|
if ((tmp = kex_names_cat(list + 1, def)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
list = tmp;
|
2018-07-04 21:49:31 +08:00
|
|
|
} else {
|
|
|
|
/* Explicit list, overrides default - just use "list" as is */
|
2015-07-30 08:01:34 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:49:31 +08:00
|
|
|
/*
|
|
|
|
* The supplied names may be a pattern-list. For the -list case,
|
|
|
|
* the patterns are applied above. For the +list and explicit list
|
|
|
|
* cases we need to do it now.
|
|
|
|
*/
|
|
|
|
ret = NULL;
|
|
|
|
if ((patterns = opatterns = strdup(list)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* Apply positive (i.e. non-negated) patterns from the list */
|
|
|
|
while ((cp = strsep(&patterns, ",")) != NULL) {
|
|
|
|
if (*cp == '!') {
|
|
|
|
/* negated matches are not supported here */
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(matching);
|
|
|
|
if ((matching = match_filter_whitelist(all, cp)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((tmp = kex_names_cat(ret, matching)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(ret);
|
|
|
|
ret = tmp;
|
|
|
|
}
|
|
|
|
if (ret == NULL || *ret == '\0') {
|
|
|
|
/* An empty name-list is an error */
|
|
|
|
/* XXX better error code? */
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
*listp = ret;
|
|
|
|
ret = NULL;
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(matching);
|
|
|
|
free(opatterns);
|
|
|
|
free(list);
|
|
|
|
free(ret);
|
|
|
|
return r;
|
2015-07-30 08:01:34 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
/* put algorithm proposal into buffer */
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int i;
|
2015-01-20 04:16:15 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
sshbuf_reset(b);
|
2001-04-04 09:56:17 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2015-01-20 04:16:15 +08: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 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
/* parse buffer and return algorithm proposal */
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
|
2000-05-30 11:44:51 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
u_char v;
|
2007-06-05 16:23:28 +08:00
|
|
|
u_int i;
|
2015-01-20 04:16:15 +08: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;
|
|
|
|
}
|
2019-09-05 17:35:19 +08:00
|
|
|
if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) { /* skip cookie */
|
|
|
|
error("%s: consume cookie: %s", __func__, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2000-05-30 11:44:51 +08:00
|
|
|
/* extract kex init proposal strings */
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
2019-09-05 17:35:19 +08:00
|
|
|
if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0) {
|
|
|
|
error("%s: parse proposal %u: %s", __func__,
|
|
|
|
i, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2015-08-22 07:57:48 +08:00
|
|
|
debug2("%s: %s", proposal_names[i], proposal[i]);
|
2000-05-30 11:44:51 +08:00
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
/* first kex follows / reserved */
|
2015-12-14 06:42:23 +08:00
|
|
|
if ((r = sshbuf_get_u8(b, &v)) != 0 || /* first_kex_follows */
|
2019-09-05 17:35:19 +08:00
|
|
|
(r = sshbuf_get_u32(b, &i)) != 0) { /* reserved */
|
|
|
|
error("%s: parse: %s", __func__, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2003-02-24 08:53:32 +08:00
|
|
|
if (first_kex_follows != NULL)
|
2015-12-14 06:42:23 +08:00
|
|
|
*first_kex_follows = v;
|
2015-08-22 07:57:48 +08:00
|
|
|
debug2("first_kex_follows %d ", v);
|
|
|
|
debug2("reserved %u ", i);
|
2015-01-20 04:16:15 +08:00
|
|
|
r = 0;
|
|
|
|
*propp = proposal;
|
|
|
|
out:
|
|
|
|
if (r != 0 && proposal != NULL)
|
|
|
|
kex_prop_free(proposal);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
2000-05-30 11:44:51 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
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
|
|
|
|
2015-04-17 21:25:52 +08:00
|
|
|
if (proposal == NULL)
|
|
|
|
return;
|
2001-04-04 09:56:17 +08:00
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
2013-06-02 05:31:17 +08:00
|
|
|
free(proposal[i]);
|
|
|
|
free(proposal);
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2007-06-05 16:23:28 +08:00
|
|
|
/* ARGSUSED */
|
2015-01-20 04:07:45 +08:00
|
|
|
static int
|
2017-05-30 22:23:52 +08:00
|
|
|
kex_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2015-11-13 12:39:35 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
error("kex protocol error: type %d seq %u", type, seq);
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
|
|
|
|
(r = sshpkt_put_u32(ssh, seq)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
2015-01-20 04:07:45 +08:00
|
|
|
return 0;
|
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
|
2015-01-20 04:16:15 +08:00
|
|
|
kex_reset_dispatch(struct ssh *ssh)
|
2001-04-05 01:57:54 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
|
2002-01-22 20:24:13 +08:00
|
|
|
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
2001-04-05 01:57:54 +08:00
|
|
|
}
|
|
|
|
|
2015-12-05 00:41:28 +08:00
|
|
|
static int
|
|
|
|
kex_send_ext_info(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
int r;
|
2016-09-13 07:31:27 +08:00
|
|
|
char *algs;
|
2015-12-05 00:41:28 +08:00
|
|
|
|
2019-09-05 17:25:13 +08:00
|
|
|
debug("Sending SSH2_MSG_EXT_INFO");
|
2017-03-10 12:07:20 +08:00
|
|
|
if ((algs = sshkey_alg_list(0, 1, 1, ',')) == NULL)
|
2016-09-13 07:31:27 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-07-03 19:39:54 +08:00
|
|
|
/* XXX filter algs list by allowed pubkey/hostbased types */
|
2015-12-05 00:41:28 +08:00
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
|
|
|
|
(r = sshpkt_put_u32(ssh, 1)) != 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
|
2016-09-13 07:31:27 +08:00
|
|
|
(r = sshpkt_put_cstring(ssh, algs)) != 0 ||
|
2019-09-05 17:35:19 +08:00
|
|
|
(r = sshpkt_send(ssh)) != 0) {
|
|
|
|
error("%s: compose: %s", __func__, ssh_err(r));
|
2016-09-13 07:31:27 +08:00
|
|
|
goto out;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2016-09-13 07:31:27 +08:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(algs);
|
2016-09-22 03:53:12 +08:00
|
|
|
return r;
|
2015-12-05 00:41:28 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_send_newkeys(struct ssh *ssh)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
int r;
|
2001-04-05 01:52:53 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
kex_reset_dispatch(ssh);
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
return r;
|
2001-04-04 09:56:17 +08:00
|
|
|
debug("SSH2_MSG_NEWKEYS sent");
|
2015-01-20 04:16:15 +08:00
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
|
2019-09-05 17:25:13 +08:00
|
|
|
if (ssh->kex->ext_info_c && (ssh->kex->flags & KEX_INITIAL) != 0)
|
2015-12-05 00:41:28 +08:00
|
|
|
if ((r = kex_send_ext_info(ssh)) != 0)
|
|
|
|
return r;
|
2019-09-05 17:25:13 +08:00
|
|
|
debug("expecting SSH2_MSG_NEWKEYS");
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:41:28 +08:00
|
|
|
int
|
2017-05-30 22:23:52 +08:00
|
|
|
kex_input_ext_info(int type, u_int32_t seq, struct ssh *ssh)
|
2015-12-05 00:41:28 +08:00
|
|
|
{
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
u_int32_t i, ninfo;
|
2018-07-03 19:39:54 +08:00
|
|
|
char *name;
|
2017-06-13 20:13:59 +08:00
|
|
|
u_char *val;
|
|
|
|
size_t vlen;
|
2015-12-05 00:41:28 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
debug("SSH2_MSG_EXT_INFO received");
|
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
|
|
|
|
if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
|
|
|
|
return r;
|
|
|
|
for (i = 0; i < ninfo; i++) {
|
|
|
|
if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
|
|
|
|
return r;
|
2017-06-13 20:13:59 +08:00
|
|
|
if ((r = sshpkt_get_string(ssh, &val, &vlen)) != 0) {
|
2015-12-05 00:41:28 +08:00
|
|
|
free(name);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "server-sig-algs") == 0) {
|
2017-06-13 20:13:59 +08:00
|
|
|
/* Ensure no \0 lurking in value */
|
|
|
|
if (memchr(val, '\0', vlen) != NULL) {
|
|
|
|
error("%s: nul byte in %s", __func__, name);
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
debug("%s: %s=<%s>", __func__, name, val);
|
2018-07-03 19:39:54 +08:00
|
|
|
kex->server_sig_algs = val;
|
|
|
|
val = NULL;
|
2017-06-13 20:13:59 +08:00
|
|
|
} else
|
|
|
|
debug("%s: %s (unrecognised)", __func__, name);
|
2015-12-05 00:41:28 +08:00
|
|
|
free(name);
|
|
|
|
free(val);
|
|
|
|
}
|
|
|
|
return sshpkt_get_end(ssh);
|
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
2017-05-30 22:23:52 +08:00
|
|
|
kex_input_newkeys(int type, u_int32_t seq, struct ssh *ssh)
|
2015-01-20 04:16:15 +08:00
|
|
|
{
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
int r;
|
2001-04-05 10:05:21 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
debug("SSH2_MSG_NEWKEYS received");
|
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
|
2017-03-15 15:07:39 +08:00
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((r = sshpkt_get_end(ssh)) != 0)
|
|
|
|
return r;
|
2016-09-20 03:02:19 +08:00
|
|
|
if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0)
|
|
|
|
return r;
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->done = 1;
|
2018-12-07 11:39:40 +08:00
|
|
|
kex->flags &= ~KEX_INITIAL;
|
2015-01-20 04:16:15 +08:00
|
|
|
sshbuf_reset(kex->peer);
|
|
|
|
/* sshbuf_reset(kex->my); */
|
2001-04-04 09:56:17 +08:00
|
|
|
kex->flags &= ~KEX_INIT_SENT;
|
2013-06-02 05:31:17 +08:00
|
|
|
free(kex->name);
|
2001-04-05 10:05:21 +08:00
|
|
|
kex->name = NULL;
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_send_kexinit(struct ssh *ssh)
|
2000-10-14 13:23:11 +08:00
|
|
|
{
|
2002-03-28 01:42:57 +08:00
|
|
|
u_char *cookie;
|
2015-01-20 04:16:15 +08:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
int r;
|
2002-03-28 01:42:57 +08:00
|
|
|
|
2019-09-05 17:35:19 +08:00
|
|
|
if (kex == NULL) {
|
|
|
|
error("%s: no hex", __func__);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
if (kex->flags & KEX_INIT_SENT)
|
|
|
|
return 0;
|
2001-04-05 07:46:07 +08:00
|
|
|
kex->done = 0;
|
2002-03-28 01:42:57 +08:00
|
|
|
|
|
|
|
/* generate a random cookie */
|
2019-09-05 17:35:19 +08:00
|
|
|
if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) {
|
|
|
|
error("%s: bad kex length: %zu < %d", __func__,
|
|
|
|
sshbuf_len(kex->my), KEX_COOKIE_LEN);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
|
|
|
if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) {
|
|
|
|
error("%s: buffer error", __func__);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
arc4random_buf(cookie, KEX_COOKIE_LEN);
|
|
|
|
|
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
|
|
|
|
(r = sshpkt_putb(ssh, kex->my)) != 0 ||
|
2019-09-05 17:35:19 +08:00
|
|
|
(r = sshpkt_send(ssh)) != 0) {
|
|
|
|
error("%s: compose reply: %s", __func__, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
return r;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
debug("SSH2_MSG_KEXINIT sent");
|
|
|
|
kex->flags |= KEX_INIT_SENT;
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
2001-02-05 20:42:17 +08:00
|
|
|
|
2007-06-05 16:23:28 +08:00
|
|
|
/* ARGSUSED */
|
2015-01-20 04:07:45 +08:00
|
|
|
int
|
2017-05-30 22:23:52 +08:00
|
|
|
kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
const u_char *ptr;
|
2015-01-20 03:52:16 +08:00
|
|
|
u_int i;
|
|
|
|
size_t dlen;
|
2015-01-20 04:16:15 +08:00
|
|
|
int r;
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2001-04-04 09:56:17 +08:00
|
|
|
debug("SSH2_MSG_KEXINIT received");
|
2019-09-05 17:35:19 +08:00
|
|
|
if (kex == NULL) {
|
|
|
|
error("%s: no hex", __func__);
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
2016-10-11 03:28:48 +08:00
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
|
2015-01-20 04:16:15 +08:00
|
|
|
ptr = sshpkt_ptr(ssh, &dlen);
|
|
|
|
if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
|
|
|
|
return r;
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2001-04-05 07:50:21 +08:00
|
|
|
/* discard packet */
|
2019-09-05 17:35:19 +08:00
|
|
|
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
|
|
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0) {
|
|
|
|
error("%s: discard cookie: %s", __func__, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
return r;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
|
|
|
if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
|
|
|
|
error("%s: discard proposal: %s", __func__, ssh_err(r));
|
2015-01-20 04:16:15 +08:00
|
|
|
return r;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2015-01-20 04:16:15 +08: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-05 07:50:21 +08:00
|
|
|
|
2015-01-20 04:16:15 +08: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);
|
|
|
|
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: unknown kex type %u", __func__, kex->kex_type);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
struct kex *
|
|
|
|
kex_new(void)
|
2015-01-20 04:16:15 +08:00
|
|
|
{
|
|
|
|
struct kex *kex;
|
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
if ((kex = calloc(1, sizeof(*kex))) == NULL ||
|
|
|
|
(kex->peer = sshbuf_new()) == NULL ||
|
|
|
|
(kex->my = sshbuf_new()) == NULL ||
|
|
|
|
(kex->client_version = sshbuf_new()) == NULL ||
|
|
|
|
(kex->server_version = sshbuf_new()) == NULL) {
|
2015-01-20 04:16:15 +08:00
|
|
|
kex_free(kex);
|
2018-12-27 11:25:24 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return kex;
|
2000-10-14 13:23:11 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 03:52:16 +08: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) {
|
2015-10-13 08:21:27 +08:00
|
|
|
explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
|
2015-01-20 03:52:16 +08:00
|
|
|
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));
|
2020-02-26 21:40:09 +08:00
|
|
|
freezero(newkeys, sizeof(*newkeys));
|
2015-01-20 03:52:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
void
|
|
|
|
kex_free(struct kex *kex)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
u_int mode;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
if (kex == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2018-02-07 10:06:50 +08:00
|
|
|
DH_free(kex->dh);
|
2015-03-11 07:02:39 +08:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2018-02-07 10:06:50 +08:00
|
|
|
EC_KEY_free(kex->ec_client_key);
|
2015-03-11 07:02:39 +08:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
|
|
|
#endif /* WITH_OPENSSL */
|
2015-01-20 04:16:15 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
|
|
|
kex_free_newkeys(kex->newkeys[mode]);
|
|
|
|
kex->newkeys[mode] = NULL;
|
2015-01-20 03:52:16 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
sshbuf_free(kex->peer);
|
|
|
|
sshbuf_free(kex->my);
|
2018-12-27 11:25:24 +08:00
|
|
|
sshbuf_free(kex->client_version);
|
|
|
|
sshbuf_free(kex->server_version);
|
2019-01-21 18:40:11 +08:00
|
|
|
sshbuf_free(kex->client_pub);
|
2015-01-20 04:16:15 +08:00
|
|
|
free(kex->session_id);
|
2015-07-29 12:43:06 +08:00
|
|
|
free(kex->failed_choice);
|
2015-12-05 00:41:28 +08:00
|
|
|
free(kex->hostkey_alg);
|
|
|
|
free(kex->name);
|
2015-01-20 04:16:15 +08:00
|
|
|
free(kex);
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
int
|
|
|
|
kex_ready(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = kex_prop2buf(ssh->kex->my, proposal)) != 0)
|
|
|
|
return r;
|
|
|
|
ssh->kex->flags = KEX_INITIAL;
|
|
|
|
kex_reset_dispatch(ssh);
|
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
int r;
|
|
|
|
|
2018-12-27 11:25:24 +08:00
|
|
|
if ((r = kex_ready(ssh, proposal)) != 0)
|
2015-01-20 04:16:15 +08:00
|
|
|
return r;
|
|
|
|
if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
|
|
|
|
kex_free(ssh->kex);
|
|
|
|
ssh->kex = NULL;
|
|
|
|
return r;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:57:07 +08:00
|
|
|
/*
|
|
|
|
* Request key re-exchange, returns 0 on success or a ssherr.h error
|
|
|
|
* code otherwise. Must not be called if KEX is incomplete or in-progress.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
kex_start_rekex(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
if (ssh->kex == NULL) {
|
|
|
|
error("%s: no kex", __func__);
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
if (ssh->kex->done == 0) {
|
|
|
|
error("%s: requested twice", __func__);
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
ssh->kex->done = 0;
|
|
|
|
return kex_send_kexinit(ssh);
|
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
choose_enc(struct sshenc *enc, char *client, char *server)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-20 04:16:15 +08:00
|
|
|
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_NO_CIPHER_ALG_MATCH;
|
2017-03-10 11:45:40 +08:00
|
|
|
if ((enc->cipher = cipher_by_name(name)) == NULL) {
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: unsupported cipher %s", __func__, name);
|
2017-03-10 11:45:40 +08:00
|
|
|
free(name);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2017-03-10 11:45:40 +08:00
|
|
|
}
|
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);
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-20 04:16:15 +08:00
|
|
|
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_NO_MAC_ALG_MATCH;
|
2017-03-10 11:45:40 +08:00
|
|
|
if (mac_setup(mac, name) < 0) {
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: unsupported MAC %s", __func__, name);
|
2017-03-10 11:45:40 +08:00
|
|
|
free(name);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2017-03-10 11:45:40 +08:00
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
mac->name = name;
|
|
|
|
mac->key = NULL;
|
|
|
|
mac->enabled = 0;
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
choose_comp(struct sshcomp *comp, char *client, char *server)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2001-03-11 09:49:19 +08:00
|
|
|
char *name = match_list(client, server, NULL);
|
2015-01-20 04:16:15 +08:00
|
|
|
|
2000-04-16 09:52:47 +08:00
|
|
|
if (name == NULL)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_NO_COMPRESS_ALG_MATCH;
|
2020-01-23 18:24:29 +08:00
|
|
|
#ifdef WITH_ZLIB
|
2005-07-26 19:54:56 +08:00
|
|
|
if (strcmp(name, "zlib@openssh.com") == 0) {
|
2018-07-09 21:37:10 +08:00
|
|
|
comp->type = COMP_DELAYED;
|
|
|
|
} else if (strcmp(name, "zlib") == 0) {
|
2018-07-06 17:06:14 +08:00
|
|
|
comp->type = COMP_ZLIB;
|
2020-01-23 18:24:29 +08:00
|
|
|
} else
|
|
|
|
#endif /* WITH_ZLIB */
|
|
|
|
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 {
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: unsupported compression scheme %s", __func__, name);
|
2017-03-10 11:45:40 +08:00
|
|
|
free(name);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
comp->name = name;
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2006-03-26 11:10:14 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
choose_kex(struct kex *k, char *client, char *server)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
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);
|
2015-01-20 04:16:15 +08:00
|
|
|
|
2015-08-22 07:57:48 +08:00
|
|
|
debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
|
2000-04-16 09:52:47 +08:00
|
|
|
if (k->name == NULL)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_NO_KEX_ALG_MATCH;
|
2019-09-05 17:35:19 +08:00
|
|
|
if ((kexalg = kex_alg_by_name(k->name)) == NULL) {
|
|
|
|
error("%s: unsupported KEX method %s", __func__, k->name);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2013-04-23 17:24:32 +08:00
|
|
|
k->kex_type = kexalg->type;
|
2014-01-10 07:58:53 +08:00
|
|
|
k->hash_alg = kexalg->hash_alg;
|
2013-04-23 17:24:32 +08:00
|
|
|
k->ec_nid = kexalg->ec_nid;
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2005-11-05 12:19:35 +08:00
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
choose_hostkeyalg(struct kex *k, char *client, char *server)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2015-12-05 00:41:28 +08:00
|
|
|
k->hostkey_alg = match_list(client, server, NULL);
|
2015-01-20 04:16:15 +08:00
|
|
|
|
2015-08-22 07:57:48 +08:00
|
|
|
debug("kex: host key algorithm: %s",
|
2015-12-05 00:41:28 +08:00
|
|
|
k->hostkey_alg ? k->hostkey_alg : "(no match)");
|
|
|
|
if (k->hostkey_alg == NULL)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
|
2015-12-05 00:41:28 +08:00
|
|
|
k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
|
2019-09-05 17:35:19 +08:00
|
|
|
if (k->hostkey_type == KEY_UNSPEC) {
|
|
|
|
error("%s: unsupported hostkey algorithm %s", __func__,
|
|
|
|
k->hostkey_alg);
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2015-12-05 00:41:28 +08:00
|
|
|
k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08:00
|
|
|
static int
|
|
|
|
kex_choose_conf(struct ssh *ssh)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
struct newkeys *newkeys;
|
|
|
|
char **my = NULL, **peer = NULL;
|
2001-04-04 09:56:17 +08:00
|
|
|
char **cprop, **sprop;
|
2001-04-04 10:00:54 +08:00
|
|
|
int nenc, nmac, ncomp;
|
2014-01-26 06:37:25 +08:00
|
|
|
u_int mode, ctos, need, dh_need, authlen;
|
2015-01-20 04:16:15 +08:00
|
|
|
int r, first_kex_follows;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2015-08-22 07:57:48 +08:00
|
|
|
debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
|
|
|
|
if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
|
|
|
|
goto out;
|
|
|
|
debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
|
|
|
|
if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
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
|
|
|
|
2015-12-05 00:41:28 +08:00
|
|
|
/* Check whether client supports ext_info_c */
|
2018-12-07 11:39:40 +08:00
|
|
|
if (kex->server && (kex->flags & KEX_INITIAL)) {
|
2015-12-05 00:41:28 +08:00
|
|
|
char *ext;
|
|
|
|
|
|
|
|
ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
|
2016-09-06 17:14:05 +08:00
|
|
|
kex->ext_info_c = (ext != NULL);
|
|
|
|
free(ext);
|
2015-12-05 00:41:28 +08:00
|
|
|
}
|
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
/* Algorithm Negotiation */
|
2015-08-22 07:57:48 +08:00
|
|
|
if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
|
|
|
|
sprop[PROPOSAL_KEX_ALGS])) != 0) {
|
|
|
|
kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
|
|
|
|
peer[PROPOSAL_KEX_ALGS] = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
|
|
|
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
|
|
|
|
kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
|
|
|
|
peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
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;
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((r = choose_enc(&newkeys->enc, cprop[nenc],
|
2015-07-29 12:43:06 +08:00
|
|
|
sprop[nenc])) != 0) {
|
|
|
|
kex->failed_choice = peer[nenc];
|
|
|
|
peer[nenc] = NULL;
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2015-07-29 12:43:06 +08:00
|
|
|
}
|
2013-01-09 13:12:19 +08:00
|
|
|
authlen = cipher_authlen(newkeys->enc.cipher);
|
2015-01-20 04:16:15 +08:00
|
|
|
/* ignore mac for authenticated encryption */
|
|
|
|
if (authlen == 0 &&
|
|
|
|
(r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
|
2015-07-29 12:43:06 +08:00
|
|
|
sprop[nmac])) != 0) {
|
|
|
|
kex->failed_choice = peer[nmac];
|
|
|
|
peer[nmac] = NULL;
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2015-07-29 12:43:06 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
|
2015-07-29 12:43:06 +08:00
|
|
|
sprop[ncomp])) != 0) {
|
|
|
|
kex->failed_choice = peer[ncomp];
|
|
|
|
peer[ncomp] = NULL;
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
2015-07-29 12:43:06 +08:00
|
|
|
}
|
2015-08-22 07:57:48 +08:00
|
|
|
debug("kex: %s cipher: %s MAC: %s compression: %s",
|
2000-04-16 09:52:47 +08:00
|
|
|
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
|
|
|
}
|
2014-01-26 06:37:25 +08:00
|
|
|
need = dh_need = 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2001-04-05 07:46:07 +08:00
|
|
|
newkeys = kex->newkeys[mode];
|
2016-09-12 09:22:38 +08:00
|
|
|
need = MAXIMUM(need, newkeys->enc.key_len);
|
|
|
|
need = MAXIMUM(need, newkeys->enc.block_size);
|
|
|
|
need = MAXIMUM(need, newkeys->enc.iv_len);
|
|
|
|
need = MAXIMUM(need, newkeys->mac.key_len);
|
|
|
|
dh_need = MAXIMUM(dh_need, cipher_seclen(newkeys->enc.cipher));
|
|
|
|
dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
|
|
|
|
dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
|
|
|
|
dh_need = MAXIMUM(dh_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;
|
2014-01-26 06:37:25 +08:00
|
|
|
kex->dh_need = dh_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 */
|
2018-01-23 13:27:21 +08:00
|
|
|
if (first_kex_follows && !proposals_match(my, peer))
|
2015-01-20 04:16:15 +08:00
|
|
|
ssh->dispatch_skip_packets = 1;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2001-04-04 09:56:17 +08:00
|
|
|
kex_prop_free(my);
|
|
|
|
kex_prop_free(peer);
|
2015-01-20 04:16:15 +08:00
|
|
|
return r;
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:15 +08: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 09:56:17 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
struct ssh_digest_ctx *hashctx = NULL;
|
2001-04-04 09:56:17 +08:00
|
|
|
char c = id;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_int have;
|
2014-01-10 07:58:53 +08:00
|
|
|
size_t mdsz;
|
2005-06-17 10:59:34 +08:00
|
|
|
u_char *digest;
|
2015-01-20 04:16:15 +08:00
|
|
|
int r;
|
2005-07-17 15:02:09 +08:00
|
|
|
|
2014-01-10 07:58:53 +08:00
|
|
|
if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
|
2015-01-20 04:16:15 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2016-09-12 09:22:38 +08:00
|
|
|
if ((digest = calloc(1, ROUNDUP(need, mdsz))) == NULL) {
|
2015-01-20 04:16:15 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2001-04-05 07:46:07 +08:00
|
|
|
/* K1 = HASH(K || H || "A" || session_id) */
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
|
|
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
2014-01-10 07:58:53 +08:00
|
|
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
|
|
|
ssh_digest_update(hashctx, &c, 1) != 0 ||
|
|
|
|
ssh_digest_update(hashctx, kex->session_id,
|
2015-01-20 04:16:15 +08:00
|
|
|
kex->session_id_len) != 0 ||
|
|
|
|
ssh_digest_final(hashctx, digest, mdsz) != 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: KEX hash failed", __func__);
|
2015-01-20 04:16:15 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2014-01-10 07:58:53 +08:00
|
|
|
ssh_digest_free(hashctx);
|
2015-01-20 04:16:15 +08:00
|
|
|
hashctx = NULL;
|
2001-04-04 09:56:17 +08:00
|
|
|
|
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) {
|
2015-01-20 04:16:15 +08:00
|
|
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
|
|
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
2014-01-10 07:58:53 +08:00
|
|
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
2015-01-20 04:16:15 +08:00
|
|
|
ssh_digest_update(hashctx, digest, have) != 0 ||
|
|
|
|
ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
|
2019-09-05 17:35:19 +08:00
|
|
|
error("%s: KDF failed", __func__);
|
2015-01-20 04:16:15 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-10 07:58:53 +08:00
|
|
|
ssh_digest_free(hashctx);
|
2015-01-20 04:16:15 +08:00
|
|
|
hashctx = NULL;
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_KEX
|
|
|
|
fprintf(stderr, "key '%c'== ", c);
|
|
|
|
dump_digest("key", digest, need);
|
|
|
|
#endif
|
2015-01-20 04:16:15 +08:00
|
|
|
*keyp = digest;
|
|
|
|
digest = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2015-12-11 01:08:40 +08:00
|
|
|
free(digest);
|
2015-01-20 04:16:15 +08:00
|
|
|
ssh_digest_free(hashctx);
|
|
|
|
return r;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2001-03-11 09:49:19 +08:00
|
|
|
#define NKEYS 6
|
2015-01-20 04:16:15 +08:00
|
|
|
int
|
|
|
|
kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
|
|
|
|
const struct sshbuf *shared_secret)
|
2000-04-16 09:52:47 +08:00
|
|
|
{
|
2015-01-20 04:16:15 +08:00
|
|
|
struct kex *kex = ssh->kex;
|
2000-12-22 09:43:59 +08:00
|
|
|
u_char *keys[NKEYS];
|
2015-01-20 04:16:15 +08:00
|
|
|
u_int i, j, mode, ctos;
|
|
|
|
int r;
|
2000-04-16 09:52:47 +08:00
|
|
|
|
2019-01-21 17:55:52 +08:00
|
|
|
/* save initial hash as session id */
|
|
|
|
if (kex->session_id == NULL) {
|
|
|
|
kex->session_id_len = hashlen;
|
|
|
|
kex->session_id = malloc(kex->session_id_len);
|
|
|
|
if (kex->session_id == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
2005-11-05 12:19:35 +08:00
|
|
|
for (i = 0; i < NKEYS; i++) {
|
2015-01-20 04:16:15 +08: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 12:19:35 +08:00
|
|
|
}
|
2000-04-16 09:52:47 +08:00
|
|
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
2006-03-26 11:10:14 +08:00
|
|
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
|
|
|
(kex->server && mode == MODE_IN);
|
2015-01-20 03:52:16 +08: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 09:52:47 +08:00
|
|
|
}
|
2015-01-20 04:16:15 +08:00
|
|
|
return 0;
|
2000-04-16 09:52:47 +08:00
|
|
|
}
|
2001-04-04 09:56:17 +08:00
|
|
|
|
2019-01-21 18:05:09 +08:00
|
|
|
int
|
2019-01-21 20:08:13 +08:00
|
|
|
kex_load_hostkey(struct ssh *ssh, struct sshkey **prvp, struct sshkey **pubp)
|
2019-01-21 18:05:09 +08:00
|
|
|
{
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
|
|
|
|
*pubp = NULL;
|
|
|
|
*prvp = NULL;
|
|
|
|
if (kex->load_host_public_key == NULL ||
|
2019-09-05 17:35:19 +08:00
|
|
|
kex->load_host_private_key == NULL) {
|
|
|
|
error("%s: missing hostkey loader", __func__);
|
2019-01-21 18:05:09 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2019-01-21 18:05:09 +08:00
|
|
|
*pubp = kex->load_host_public_key(kex->hostkey_type,
|
|
|
|
kex->hostkey_nid, ssh);
|
|
|
|
*prvp = kex->load_host_private_key(kex->hostkey_type,
|
|
|
|
kex->hostkey_nid, ssh);
|
|
|
|
if (*pubp == NULL)
|
|
|
|
return SSH_ERR_NO_HOSTKEY_LOADED;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 14:30:44 +08:00
|
|
|
|
2019-01-21 18:07:22 +08:00
|
|
|
int
|
|
|
|
kex_verify_host_key(struct ssh *ssh, struct sshkey *server_host_key)
|
|
|
|
{
|
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
|
2019-09-05 17:35:19 +08:00
|
|
|
if (kex->verify_host_key == NULL) {
|
|
|
|
error("%s: missing hostkey verifier", __func__);
|
2019-01-21 18:07:22 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2019-09-05 17:35:19 +08:00
|
|
|
}
|
2019-01-21 18:07:22 +08:00
|
|
|
if (server_host_key->type != kex->hostkey_type ||
|
|
|
|
(kex->hostkey_type == KEY_ECDSA &&
|
|
|
|
server_host_key->ecdsa_nid != kex->hostkey_nid))
|
|
|
|
return SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
if (kex->verify_host_key(server_host_key, ssh) == -1)
|
|
|
|
return SSH_ERR_SIGNATURE_INVALID;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
2019-01-21 18:20:12 +08:00
|
|
|
dump_digest(const char *msg, const u_char *digest, int len)
|
2001-04-04 09:56:17 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2015-01-20 04:16:15 +08:00
|
|
|
sshbuf_dump_data(digest, len, stderr);
|
2001-04-04 09:56:17 +08:00
|
|
|
}
|
|
|
|
#endif
|
2018-12-27 11:25:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a plaintext error message to the peer, suffixed by \r\n.
|
|
|
|
* Only used during banner exchange, and there only for the server.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
send_error(struct ssh *ssh, char *msg)
|
|
|
|
{
|
|
|
|
char *crnl = "\r\n";
|
|
|
|
|
|
|
|
if (!ssh->kex->server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
|
|
|
|
msg, strlen(msg)) != strlen(msg) ||
|
|
|
|
atomicio(vwrite, ssh_packet_get_connection_out(ssh),
|
|
|
|
crnl, strlen(crnl)) != strlen(crnl))
|
|
|
|
error("%s: write: %.100s", __func__, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sends our identification string and waits for the peer's. Will block for
|
|
|
|
* up to timeout_ms (or indefinitely if timeout_ms <= 0).
|
|
|
|
* Returns on 0 success or a ssherr.h code on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
kex_exchange_identification(struct ssh *ssh, int timeout_ms,
|
|
|
|
const char *version_addendum)
|
|
|
|
{
|
2020-03-13 12:01:56 +08:00
|
|
|
int remote_major, remote_minor, mismatch, oerrno = 0;
|
2018-12-27 11:25:24 +08:00
|
|
|
size_t len, i, n;
|
|
|
|
int r, expect_nl;
|
|
|
|
u_char c;
|
|
|
|
struct sshbuf *our_version = ssh->kex->server ?
|
|
|
|
ssh->kex->server_version : ssh->kex->client_version;
|
|
|
|
struct sshbuf *peer_version = ssh->kex->server ?
|
|
|
|
ssh->kex->client_version : ssh->kex->server_version;
|
|
|
|
char *our_version_string = NULL, *peer_version_string = NULL;
|
|
|
|
char *cp, *remote_version = NULL;
|
|
|
|
|
|
|
|
/* Prepare and send our banner */
|
|
|
|
sshbuf_reset(our_version);
|
|
|
|
if (version_addendum != NULL && *version_addendum == '\0')
|
|
|
|
version_addendum = NULL;
|
|
|
|
if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
|
|
|
|
PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
|
|
|
|
version_addendum == NULL ? "" : " ",
|
|
|
|
version_addendum == NULL ? "" : version_addendum)) != 0) {
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
2018-12-27 11:25:24 +08:00
|
|
|
error("%s: sshbuf_putf: %s", __func__, ssh_err(r));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
|
|
|
|
sshbuf_mutable_ptr(our_version),
|
|
|
|
sshbuf_len(our_version)) != sshbuf_len(our_version)) {
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
|
|
|
debug("%s: write: %.100s", __func__, strerror(errno));
|
2018-12-27 11:25:24 +08:00
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_consume_end(our_version, 2)) != 0) { /* trim \r\n */
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
2018-12-27 11:25:24 +08:00
|
|
|
error("%s: sshbuf_consume_end: %s", __func__, ssh_err(r));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
our_version_string = sshbuf_dup_string(our_version);
|
|
|
|
if (our_version_string == NULL) {
|
|
|
|
error("%s: sshbuf_dup_string failed", __func__);
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
debug("Local version string %.100s", our_version_string);
|
|
|
|
|
|
|
|
/* Read other side's version identification. */
|
|
|
|
for (n = 0; ; n++) {
|
|
|
|
if (n >= SSH_MAX_PRE_BANNER_LINES) {
|
|
|
|
send_error(ssh, "No SSH identification string "
|
|
|
|
"received.");
|
|
|
|
error("%s: No SSH version received in first %u lines "
|
|
|
|
"from server", __func__, SSH_MAX_PRE_BANNER_LINES);
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sshbuf_reset(peer_version);
|
|
|
|
expect_nl = 0;
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (timeout_ms > 0) {
|
|
|
|
r = waitrfd(ssh_packet_get_connection_in(ssh),
|
|
|
|
&timeout_ms);
|
|
|
|
if (r == -1 && errno == ETIMEDOUT) {
|
|
|
|
send_error(ssh, "Timed out waiting "
|
|
|
|
"for SSH identification string.");
|
|
|
|
error("Connection timed out during "
|
|
|
|
"banner exchange");
|
|
|
|
r = SSH_ERR_CONN_TIMEOUT;
|
|
|
|
goto out;
|
|
|
|
} else if (r == -1) {
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
2018-12-27 11:25:24 +08:00
|
|
|
error("%s: %s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = atomicio(read, ssh_packet_get_connection_in(ssh),
|
|
|
|
&c, 1);
|
|
|
|
if (len != 1 && errno == EPIPE) {
|
|
|
|
error("%s: Connection closed by remote host",
|
|
|
|
__func__);
|
|
|
|
r = SSH_ERR_CONN_CLOSED;
|
|
|
|
goto out;
|
|
|
|
} else if (len != 1) {
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
2018-12-27 11:25:24 +08:00
|
|
|
error("%s: read: %.100s",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
expect_nl = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
if (c == '\0' || expect_nl) {
|
|
|
|
error("%s: banner line contains invalid "
|
|
|
|
"characters", __func__);
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_u8(peer_version, c)) != 0) {
|
2020-03-13 12:01:56 +08:00
|
|
|
oerrno = errno;
|
2018-12-27 11:25:24 +08:00
|
|
|
error("%s: sshbuf_put: %s",
|
|
|
|
__func__, ssh_err(r));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(peer_version) > SSH_MAX_BANNER_LEN) {
|
|
|
|
error("%s: banner line too long", __func__);
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Is this an actual protocol banner? */
|
|
|
|
if (sshbuf_len(peer_version) > 4 &&
|
|
|
|
memcmp(sshbuf_ptr(peer_version), "SSH-", 4) == 0)
|
|
|
|
break;
|
|
|
|
/* If not, then just log the line and continue */
|
|
|
|
if ((cp = sshbuf_dup_string(peer_version)) == NULL) {
|
|
|
|
error("%s: sshbuf_dup_string failed", __func__);
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Do not accept lines before the SSH ident from a client */
|
|
|
|
if (ssh->kex->server) {
|
|
|
|
error("%s: client sent invalid protocol identifier "
|
|
|
|
"\"%.256s\"", __func__, cp);
|
|
|
|
free(cp);
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
debug("%s: banner line %zu: %s", __func__, n, cp);
|
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
peer_version_string = sshbuf_dup_string(peer_version);
|
|
|
|
if (peer_version_string == NULL)
|
|
|
|
error("%s: sshbuf_dup_string failed", __func__);
|
|
|
|
/* XXX must be same size for sscanf */
|
|
|
|
if ((remote_version = calloc(1, sshbuf_len(peer_version))) == NULL) {
|
|
|
|
error("%s: calloc failed", __func__);
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the versions match. In future this might accept
|
|
|
|
* several versions and set appropriate flags to handle them.
|
|
|
|
*/
|
|
|
|
if (sscanf(peer_version_string, "SSH-%d.%d-%[^\n]\n",
|
|
|
|
&remote_major, &remote_minor, remote_version) != 3) {
|
|
|
|
error("Bad remote protocol version identification: '%.100s'",
|
|
|
|
peer_version_string);
|
|
|
|
invalid:
|
|
|
|
send_error(ssh, "Invalid SSH identification string.");
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
debug("Remote protocol version %d.%d, remote software version %.100s",
|
|
|
|
remote_major, remote_minor, remote_version);
|
|
|
|
ssh->compat = compat_datafellows(remote_version);
|
|
|
|
|
|
|
|
mismatch = 0;
|
|
|
|
switch (remote_major) {
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (remote_minor != 99)
|
|
|
|
mismatch = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mismatch = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mismatch) {
|
|
|
|
error("Protocol major versions differ: %d vs. %d",
|
|
|
|
PROTOCOL_MAJOR_2, remote_major);
|
|
|
|
send_error(ssh, "Protocol major versions differ.");
|
|
|
|
r = SSH_ERR_NO_PROTOCOL_VERSION;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssh->kex->server && (ssh->compat & SSH_BUG_PROBE) != 0) {
|
|
|
|
logit("probed from %s port %d with %s. Don't panic.",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
|
|
|
|
peer_version_string);
|
|
|
|
r = SSH_ERR_CONN_CLOSED; /* XXX */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ssh->kex->server && (ssh->compat & SSH_BUG_SCANNER) != 0) {
|
|
|
|
logit("scanned from %s port %d with %s. Don't panic.",
|
|
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
|
|
|
|
peer_version_string);
|
|
|
|
r = SSH_ERR_CONN_CLOSED; /* XXX */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
|
|
|
|
logit("Remote version \"%.100s\" uses unsafe RSA signature "
|
|
|
|
"scheme; disabling use of RSA keys", remote_version);
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(our_version_string);
|
|
|
|
free(peer_version_string);
|
|
|
|
free(remote_version);
|
2020-03-13 12:01:56 +08:00
|
|
|
if (r == SSH_ERR_SYSTEM_ERROR)
|
|
|
|
errno = oerrno;
|
2018-12-27 11:25:24 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|