2005-07-05 05:21:44 +04:00
|
|
|
/*
|
2009-03-30 00:19:18 +04:00
|
|
|
* kex.c - key exchange
|
|
|
|
*
|
|
|
|
* This file is part of the SSH Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 by Aris Adamantiadis
|
|
|
|
*
|
|
|
|
* The SSH Library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SSH Library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with the SSH Library; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
* MA 02111-1307, USA.
|
|
|
|
*/
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2009-09-25 12:01:56 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2005-10-26 03:11:41 +04:00
|
|
|
#include <stdio.h>
|
2018-10-18 15:46:32 +03:00
|
|
|
#include <stdbool.h>
|
2009-03-29 00:43:53 +03:00
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
#include "libssh/priv.h"
|
2009-09-24 01:51:04 +04:00
|
|
|
#include "libssh/buffer.h"
|
2009-09-26 03:15:48 +04:00
|
|
|
#include "libssh/dh.h"
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
2018-11-06 23:44:46 +03:00
|
|
|
#include "libssh/dh-gex.h"
|
2019-02-02 18:49:05 +03:00
|
|
|
#endif /* WITH_GEX */
|
2009-12-12 02:23:53 +03:00
|
|
|
#include "libssh/kex.h"
|
2011-09-10 13:03:46 +04:00
|
|
|
#include "libssh/session.h"
|
|
|
|
#include "libssh/ssh2.h"
|
2010-05-12 21:24:22 +04:00
|
|
|
#include "libssh/string.h"
|
2013-09-22 01:34:50 +04:00
|
|
|
#include "libssh/curve25519.h"
|
2014-02-05 01:28:30 +04:00
|
|
|
#include "libssh/knownhosts.h"
|
2018-07-03 09:19:14 +03:00
|
|
|
#include "libssh/misc.h"
|
2019-01-08 16:27:39 +03:00
|
|
|
#include "libssh/pki.h"
|
2018-10-28 00:54:56 +03:00
|
|
|
#include "libssh/bignum.h"
|
2019-05-15 18:48:41 +03:00
|
|
|
#include "libssh/token.h"
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2018-12-16 15:32:42 +03:00
|
|
|
#ifdef WITH_BLOWFISH_CIPHER
|
|
|
|
# if defined(HAVE_OPENSSL_BLOWFISH_H) || defined(HAVE_LIBGCRYPT) || defined(HAVE_LIBMBEDCRYPTO)
|
|
|
|
# define BLOWFISH "blowfish-cbc,"
|
|
|
|
# else
|
|
|
|
# define BLOWFISH ""
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define BLOWFISH ""
|
|
|
|
#endif
|
|
|
|
|
2005-10-05 02:11:19 +04:00
|
|
|
#ifdef HAVE_LIBGCRYPT
|
2018-10-01 15:32:05 +03:00
|
|
|
# define AES "aes256-gcm@openssh.com,aes128-gcm@openssh.com," \
|
2020-07-15 22:09:40 +03:00
|
|
|
"aes256-ctr,aes192-ctr,aes128-ctr,"
|
|
|
|
# define AES_CBC "aes256-cbc,aes192-cbc,aes128-cbc,"
|
2015-01-24 20:19:57 +03:00
|
|
|
# define DES "3des-cbc"
|
2018-06-28 18:52:58 +03:00
|
|
|
# define DES_SUPPORTED "3des-cbc"
|
2015-06-24 17:21:58 +03:00
|
|
|
|
2018-12-16 15:32:42 +03:00
|
|
|
#elif defined(HAVE_LIBMBEDCRYPTO)
|
2018-10-04 16:21:36 +03:00
|
|
|
# ifdef MBEDTLS_GCM_C
|
|
|
|
# define GCM "aes256-gcm@openssh.com,aes128-gcm@openssh.com,"
|
|
|
|
# else
|
|
|
|
# define GCM ""
|
|
|
|
# endif /* MBEDTLS_GCM_C */
|
2020-07-15 22:09:40 +03:00
|
|
|
# define AES GCM "aes256-ctr,aes192-ctr,aes128-ctr,"
|
|
|
|
# define AES_CBC "aes256-cbc,aes192-cbc,aes128-cbc,"
|
2017-12-28 13:10:43 +03:00
|
|
|
# define DES "3des-cbc"
|
2018-06-28 18:52:58 +03:00
|
|
|
# define DES_SUPPORTED "3des-cbc"
|
2017-12-28 13:10:43 +03:00
|
|
|
|
2012-09-25 17:29:38 +04:00
|
|
|
#elif defined(HAVE_LIBCRYPTO)
|
|
|
|
# ifdef HAVE_OPENSSL_AES_H
|
2020-12-22 21:54:14 +03:00
|
|
|
# define GCM "aes256-gcm@openssh.com,aes128-gcm@openssh.com,"
|
2020-12-18 15:53:06 +03:00
|
|
|
# define AES GCM "aes256-ctr,aes192-ctr,aes128-ctr,"
|
|
|
|
# define AES_CBC "aes256-cbc,aes192-cbc,aes128-cbc,"
|
2015-06-24 17:21:58 +03:00
|
|
|
# else /* HAVE_OPENSSL_AES_H */
|
2012-09-25 17:29:38 +04:00
|
|
|
# define AES ""
|
2020-07-15 22:09:40 +03:00
|
|
|
# define AES_CBC ""
|
2015-06-24 17:21:58 +03:00
|
|
|
# endif /* HAVE_OPENSSL_AES_H */
|
|
|
|
|
2015-01-24 20:19:57 +03:00
|
|
|
# define DES "3des-cbc"
|
2018-06-28 18:52:58 +03:00
|
|
|
# define DES_SUPPORTED "3des-cbc"
|
2015-06-24 17:21:58 +03:00
|
|
|
#endif /* HAVE_LIBCRYPTO */
|
2005-10-05 02:11:19 +04:00
|
|
|
|
2011-09-24 00:54:33 +04:00
|
|
|
#ifdef WITH_ZLIB
|
2010-10-04 18:19:20 +04:00
|
|
|
#define ZLIB "none,zlib,zlib@openssh.com"
|
2005-07-05 05:21:44 +04:00
|
|
|
#else
|
|
|
|
#define ZLIB "none"
|
|
|
|
#endif
|
2005-10-05 02:11:19 +04:00
|
|
|
|
2013-09-22 01:34:50 +04:00
|
|
|
#ifdef HAVE_CURVE25519
|
2018-06-25 14:01:57 +03:00
|
|
|
#define CURVE25519 "curve25519-sha256,curve25519-sha256@libssh.org,"
|
2013-09-22 01:34:50 +04:00
|
|
|
#else
|
|
|
|
#define CURVE25519 ""
|
|
|
|
#endif
|
|
|
|
|
2011-06-13 15:46:34 +04:00
|
|
|
#ifdef HAVE_ECDH
|
2017-08-24 19:14:38 +03:00
|
|
|
#define ECDH "ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,"
|
2019-05-29 14:47:41 +03:00
|
|
|
#define EC_HOSTKEYS "ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,"
|
|
|
|
#define EC_PUBLIC_KEY_ALGORITHMS "ecdsa-sha2-nistp521-cert-v01@openssh.com," \
|
|
|
|
"ecdsa-sha2-nistp384-cert-v01@openssh.com," \
|
|
|
|
"ecdsa-sha2-nistp256-cert-v01@openssh.com,"
|
2011-06-13 15:46:34 +04:00
|
|
|
#else
|
2019-05-29 14:47:41 +03:00
|
|
|
#define EC_HOSTKEYS ""
|
|
|
|
#define EC_PUBLIC_KEY_ALGORITHMS ""
|
|
|
|
#define ECDH ""
|
|
|
|
#endif
|
|
|
|
|
2017-12-28 13:10:43 +03:00
|
|
|
#ifdef HAVE_DSA
|
2019-05-29 14:47:41 +03:00
|
|
|
#define DSA_HOSTKEYS ",ssh-dss"
|
|
|
|
#define DSA_PUBLIC_KEY_ALGORITHMS ",ssh-dss-cert-v01@openssh.com"
|
2017-12-28 13:10:43 +03:00
|
|
|
#else
|
2019-05-29 14:47:41 +03:00
|
|
|
#define DSA_HOSTKEYS ""
|
|
|
|
#define DSA_PUBLIC_KEY_ALGORITHMS ""
|
2011-06-13 15:46:34 +04:00
|
|
|
#endif
|
|
|
|
|
2020-04-28 12:04:59 +03:00
|
|
|
#ifdef WITH_INSECURE_NONE
|
|
|
|
#define NONE ",none"
|
|
|
|
#else
|
|
|
|
#define NONE
|
|
|
|
#endif
|
|
|
|
|
2019-05-29 14:47:41 +03:00
|
|
|
#define HOSTKEYS "ssh-ed25519," \
|
|
|
|
EC_HOSTKEYS \
|
|
|
|
"rsa-sha2-512," \
|
|
|
|
"rsa-sha2-256," \
|
|
|
|
"ssh-rsa" \
|
|
|
|
DSA_HOSTKEYS
|
2020-04-14 13:26:50 +03:00
|
|
|
#define DEFAULT_HOSTKEYS "ssh-ed25519," \
|
|
|
|
EC_HOSTKEYS \
|
|
|
|
"rsa-sha2-512," \
|
|
|
|
"rsa-sha2-256"
|
|
|
|
|
2019-05-29 14:47:41 +03:00
|
|
|
#define PUBLIC_KEY_ALGORITHMS "ssh-ed25519-cert-v01@openssh.com," \
|
|
|
|
EC_PUBLIC_KEY_ALGORITHMS \
|
|
|
|
"rsa-sha2-512-cert-v01@openssh.com," \
|
|
|
|
"rsa-sha2-256-cert-v01@openssh.com," \
|
|
|
|
"ssh-rsa-cert-v01@openssh.com" \
|
|
|
|
DSA_PUBLIC_KEY_ALGORITHMS "," \
|
|
|
|
HOSTKEYS
|
2020-04-14 13:26:50 +03:00
|
|
|
#define DEFAULT_PUBLIC_KEY_ALGORITHMS "ssh-ed25519-cert-v01@openssh.com," \
|
|
|
|
EC_PUBLIC_KEY_ALGORITHMS \
|
|
|
|
"rsa-sha2-512-cert-v01@openssh.com," \
|
|
|
|
"rsa-sha2-256-cert-v01@openssh.com," \
|
|
|
|
DEFAULT_HOSTKEYS
|
2019-05-29 14:47:41 +03:00
|
|
|
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
2019-02-02 17:00:26 +03:00
|
|
|
#define GEX_SHA256 "diffie-hellman-group-exchange-sha256,"
|
|
|
|
#define GEX_SHA1 "diffie-hellman-group-exchange-sha1,"
|
2019-02-02 18:49:05 +03:00
|
|
|
#else
|
|
|
|
#define GEX_SHA256
|
|
|
|
#define GEX_SHA1
|
|
|
|
#endif /* WITH_GEX */
|
2019-02-02 17:00:26 +03:00
|
|
|
|
2018-02-28 19:24:53 +03:00
|
|
|
#define CHACHA20 "chacha20-poly1305@openssh.com,"
|
|
|
|
|
2020-08-26 13:02:29 +03:00
|
|
|
#define DEFAULT_KEY_EXCHANGE \
|
2018-11-06 23:44:46 +03:00
|
|
|
CURVE25519 \
|
|
|
|
ECDH \
|
|
|
|
"diffie-hellman-group18-sha512,diffie-hellman-group16-sha512," \
|
2019-02-02 17:00:26 +03:00
|
|
|
GEX_SHA256 \
|
2020-08-26 13:02:29 +03:00
|
|
|
"diffie-hellman-group14-sha256" \
|
|
|
|
|
2019-02-02 17:00:26 +03:00
|
|
|
#define KEY_EXCHANGE_SUPPORTED \
|
|
|
|
GEX_SHA1 \
|
2020-08-26 13:02:29 +03:00
|
|
|
DEFAULT_KEY_EXCHANGE \
|
|
|
|
",diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"
|
2019-05-22 19:33:14 +03:00
|
|
|
|
2018-08-06 12:47:32 +03:00
|
|
|
/* RFC 8308 */
|
|
|
|
#define KEX_EXTENSION_CLIENT "ext-info-c"
|
|
|
|
|
2019-05-22 19:33:14 +03:00
|
|
|
/* Allowed algorithms in FIPS mode */
|
|
|
|
#define FIPS_ALLOWED_CIPHERS "aes256-gcm@openssh.com,"\
|
|
|
|
"aes256-ctr,"\
|
|
|
|
"aes256-cbc,"\
|
|
|
|
"aes128-gcm@openssh.com,"\
|
|
|
|
"aes128-ctr,"\
|
|
|
|
"aes128-cbc"
|
|
|
|
|
2019-05-29 14:47:41 +03:00
|
|
|
#define FIPS_ALLOWED_HOSTKEYS EC_HOSTKEYS \
|
|
|
|
"rsa-sha2-512," \
|
|
|
|
"rsa-sha2-256"
|
|
|
|
|
|
|
|
#define FIPS_ALLOWED_PUBLIC_KEY_ALGORITHMS EC_PUBLIC_KEY_ALGORITHMS \
|
|
|
|
"rsa-sha2-512-cert-v01@openssh.com," \
|
|
|
|
"rsa-sha2-256-cert-v01@openssh.com," \
|
|
|
|
FIPS_ALLOWED_HOSTKEYS
|
2019-05-22 19:33:14 +03:00
|
|
|
|
|
|
|
#define FIPS_ALLOWED_KEX "ecdh-sha2-nistp256,"\
|
|
|
|
"ecdh-sha2-nistp384,"\
|
|
|
|
"ecdh-sha2-nistp521,"\
|
|
|
|
"diffie-hellman-group-exchange-sha256,"\
|
2020-04-03 13:49:19 +03:00
|
|
|
"diffie-hellman-group14-sha256,"\
|
2019-05-22 19:33:14 +03:00
|
|
|
"diffie-hellman-group16-sha512,"\
|
|
|
|
"diffie-hellman-group18-sha512"
|
|
|
|
|
|
|
|
#define FIPS_ALLOWED_MACS "hmac-sha2-256-etm@openssh.com,"\
|
|
|
|
"hmac-sha1-etm@openssh.com,"\
|
|
|
|
"hmac-sha2-512-etm@openssh.com,"\
|
|
|
|
"hmac-sha2-256,"\
|
|
|
|
"hmac-sha1,"\
|
|
|
|
"hmac-sha2-512"
|
|
|
|
|
|
|
|
/* NOTE: This is a fixed API and the index is defined by ssh_kex_types_e */
|
|
|
|
static const char *fips_methods[] = {
|
|
|
|
FIPS_ALLOWED_KEX,
|
|
|
|
FIPS_ALLOWED_PUBLIC_KEY_ALGORITHMS,
|
|
|
|
FIPS_ALLOWED_CIPHERS,
|
|
|
|
FIPS_ALLOWED_CIPHERS,
|
|
|
|
FIPS_ALLOWED_MACS,
|
|
|
|
FIPS_ALLOWED_MACS,
|
|
|
|
ZLIB,
|
|
|
|
ZLIB,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-10-07 14:01:34 +04:00
|
|
|
/* NOTE: This is a fixed API and the index is defined by ssh_kex_types_e */
|
2011-11-10 17:46:22 +04:00
|
|
|
static const char *default_methods[] = {
|
2020-08-26 13:02:29 +03:00
|
|
|
DEFAULT_KEY_EXCHANGE,
|
2020-04-14 13:26:50 +03:00
|
|
|
DEFAULT_PUBLIC_KEY_ALGORITHMS,
|
2020-08-26 13:02:29 +03:00
|
|
|
CHACHA20 AES,
|
|
|
|
CHACHA20 AES,
|
|
|
|
"hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha2-512",
|
|
|
|
"hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha2-512",
|
2020-04-14 13:26:50 +03:00
|
|
|
"none",
|
|
|
|
"none",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL
|
2009-04-05 13:46:47 +04:00
|
|
|
};
|
|
|
|
|
2012-10-07 14:01:34 +04:00
|
|
|
/* NOTE: This is a fixed API and the index is defined by ssh_kex_types_e */
|
2012-10-12 19:43:32 +04:00
|
|
|
static const char *supported_methods[] = {
|
2019-02-02 17:00:26 +03:00
|
|
|
KEY_EXCHANGE_SUPPORTED,
|
2018-07-02 11:50:28 +03:00
|
|
|
PUBLIC_KEY_ALGORITHMS,
|
2020-07-15 22:09:40 +03:00
|
|
|
CHACHA20 AES AES_CBC BLOWFISH DES_SUPPORTED NONE,
|
|
|
|
CHACHA20 AES AES_CBC BLOWFISH DES_SUPPORTED NONE,
|
2020-04-28 12:04:59 +03:00
|
|
|
"hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1" NONE,
|
|
|
|
"hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1" NONE,
|
2009-04-05 13:46:47 +04:00
|
|
|
ZLIB,
|
2015-09-16 09:34:28 +03:00
|
|
|
ZLIB,
|
2009-04-05 13:46:47 +04:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
/* descriptions of the key exchange packet */
|
2012-10-12 19:43:32 +04:00
|
|
|
static const char *ssh_kex_descriptions[] = {
|
2009-04-05 13:46:47 +04:00
|
|
|
"kex algos",
|
|
|
|
"server host key algo",
|
|
|
|
"encryption client->server",
|
|
|
|
"encryption server->client",
|
|
|
|
"mac algo client->server",
|
|
|
|
"mac algo server->client",
|
|
|
|
"compression algo client->server",
|
|
|
|
"compression algo server->client",
|
|
|
|
"languages client->server",
|
|
|
|
"languages server->client",
|
|
|
|
NULL
|
|
|
|
};
|
2005-07-05 05:21:44 +04:00
|
|
|
|
2018-08-07 12:32:36 +03:00
|
|
|
const char *ssh_kex_get_default_methods(uint32_t algo)
|
|
|
|
{
|
2019-11-01 16:59:27 +03:00
|
|
|
if (algo >= SSH_KEX_METHODS) {
|
2018-08-07 12:32:36 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_methods[algo];
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:48:06 +03:00
|
|
|
const char *ssh_kex_get_supported_method(uint32_t algo)
|
|
|
|
{
|
2019-11-01 16:59:27 +03:00
|
|
|
if (algo >= SSH_KEX_METHODS) {
|
2019-05-29 14:48:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-12 19:43:32 +04:00
|
|
|
|
2019-05-29 14:48:06 +03:00
|
|
|
return supported_methods[algo];
|
2012-10-12 19:43:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *ssh_kex_get_description(uint32_t algo) {
|
2019-11-01 16:59:27 +03:00
|
|
|
if (algo >= SSH_KEX_METHODS) {
|
2012-10-12 19:43:32 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh_kex_descriptions[algo];
|
|
|
|
}
|
|
|
|
|
2019-05-22 19:33:14 +03:00
|
|
|
const char *ssh_kex_get_fips_methods(uint32_t algo) {
|
2019-11-01 16:59:27 +03:00
|
|
|
if (algo >= SSH_KEX_METHODS) {
|
2019-05-22 19:33:14 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fips_methods[algo];
|
|
|
|
}
|
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
/**
|
|
|
|
* @internal
|
2015-05-05 02:22:35 +03:00
|
|
|
* @brief returns whether the first client key exchange algorithm or
|
|
|
|
* hostkey type matches its server counterpart
|
|
|
|
* @returns whether the first client key exchange algorithm or hostkey type
|
|
|
|
* matches its server counterpart
|
2014-04-15 12:48:24 +04:00
|
|
|
*/
|
2015-05-05 02:22:35 +03:00
|
|
|
static int cmp_first_kex_algo(const char *client_str,
|
|
|
|
const char *server_str) {
|
2019-05-15 19:57:14 +03:00
|
|
|
size_t client_kex_len;
|
|
|
|
size_t server_kex_len;
|
2014-04-22 12:11:03 +04:00
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
char *colon;
|
2014-04-22 12:11:03 +04:00
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
int is_wrong = 1;
|
2014-04-15 12:48:24 +04:00
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
colon = strchr(client_str, ',');
|
|
|
|
if (colon == NULL) {
|
|
|
|
client_kex_len = strlen(client_str);
|
|
|
|
} else {
|
|
|
|
client_kex_len = colon - client_str;
|
2014-04-15 12:48:24 +04:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
colon = strchr(server_str, ',');
|
|
|
|
if (colon == NULL) {
|
|
|
|
server_kex_len = strlen(server_str);
|
|
|
|
} else {
|
|
|
|
server_kex_len = colon - server_str;
|
2014-04-15 12:48:24 +04:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
if (client_kex_len != server_kex_len) {
|
|
|
|
return is_wrong;
|
2014-04-15 12:48:24 +04:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:57:14 +03:00
|
|
|
is_wrong = (strncmp(client_str, server_str, client_kex_len) != 0);
|
2014-04-15 12:48:24 +04:00
|
|
|
|
|
|
|
return is_wrong;
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:34:38 +03:00
|
|
|
SSH_PACKET_CALLBACK(ssh_packet_kexinit)
|
|
|
|
{
|
2018-07-03 17:16:25 +03:00
|
|
|
int i, ok;
|
2018-11-07 12:34:38 +03:00
|
|
|
int server_kex = session->server;
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_string str = NULL;
|
2019-11-01 16:59:27 +03:00
|
|
|
char *strings[SSH_KEX_METHODS] = {0};
|
2019-02-05 02:21:21 +03:00
|
|
|
char *rsa_sig_ext = NULL;
|
2014-04-15 12:48:24 +04:00
|
|
|
int rc = SSH_ERROR;
|
2019-10-31 15:58:35 +03:00
|
|
|
size_t len;
|
2014-04-15 12:48:24 +04:00
|
|
|
|
|
|
|
uint8_t first_kex_packet_follows = 0;
|
|
|
|
uint32_t kexinit_reserved = 0;
|
|
|
|
|
|
|
|
(void)type;
|
|
|
|
(void)user;
|
|
|
|
|
2018-11-07 12:34:38 +03:00
|
|
|
if (session->session_state == SSH_SESSION_STATE_AUTHENTICATED) {
|
2018-11-15 15:43:18 +03:00
|
|
|
SSH_LOG(SSH_LOG_INFO, "Initiating key re-exchange");
|
2018-11-07 12:34:38 +03:00
|
|
|
} else if (session->session_state != SSH_SESSION_STATE_INITIAL_KEX) {
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_set_error(session,SSH_FATAL,"SSH_KEXINIT received in wrong state");
|
2010-08-09 00:52:09 +04:00
|
|
|
goto error;
|
2014-04-15 12:48:24 +04:00
|
|
|
}
|
2014-04-10 02:24:04 +04:00
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
if (server_kex) {
|
2019-10-31 15:58:35 +03:00
|
|
|
len = ssh_buffer_get_data(packet,session->next_crypto->client_kex.cookie, 16);
|
|
|
|
if (len != 16) {
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_set_error(session, SSH_FATAL, "ssh_packet_kexinit: no cookie in packet");
|
|
|
|
goto error;
|
|
|
|
}
|
2014-04-10 02:24:04 +04:00
|
|
|
|
2019-12-11 13:28:33 +03:00
|
|
|
ok = ssh_hashbufin_add_cookie(session, session->next_crypto->client_kex.cookie);
|
|
|
|
if (ok < 0) {
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_set_error(session, SSH_FATAL, "ssh_packet_kexinit: adding cookie failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-31 15:58:35 +03:00
|
|
|
len = ssh_buffer_get_data(packet,session->next_crypto->server_kex.cookie, 16);
|
|
|
|
if (len != 16) {
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_set_error(session, SSH_FATAL, "ssh_packet_kexinit: no cookie in packet");
|
|
|
|
goto error;
|
|
|
|
}
|
2009-04-07 23:27:50 +04:00
|
|
|
|
2019-12-11 13:28:33 +03:00
|
|
|
ok = ssh_hashbufin_add_cookie(session, session->next_crypto->server_kex.cookie);
|
|
|
|
if (ok < 0) {
|
2014-04-15 12:48:24 +04:00
|
|
|
ssh_set_error(session, SSH_FATAL, "ssh_packet_kexinit: adding cookie failed");
|
|
|
|
goto error;
|
|
|
|
}
|
2009-04-07 22:46:29 +04:00
|
|
|
}
|
2009-04-07 23:27:50 +04:00
|
|
|
|
2019-11-01 16:59:27 +03:00
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
2015-09-17 10:43:33 +03:00
|
|
|
str = ssh_buffer_get_ssh_string(packet);
|
2014-04-15 12:48:24 +04:00
|
|
|
if (str == NULL) {
|
2014-10-19 10:23:26 +04:00
|
|
|
goto error;
|
2014-04-15 12:48:24 +04:00
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
rc = ssh_buffer_add_ssh_string(session->in_hashbuf, str);
|
2014-04-15 12:48:24 +04:00
|
|
|
if (rc < 0) {
|
|
|
|
ssh_set_error(session, SSH_FATAL, "Error adding string in hash buffer");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
strings[i] = ssh_string_to_char(str);
|
|
|
|
if (strings[i] == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(str);
|
2014-04-15 12:48:24 +04:00
|
|
|
str = NULL;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2014-04-10 02:24:04 +04:00
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
/* copy the server kex info into an array of strings */
|
|
|
|
if (server_kex) {
|
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
|
|
|
session->next_crypto->client_kex.methods[i] = strings[i];
|
|
|
|
}
|
|
|
|
} else { /* client */
|
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
|
|
|
session->next_crypto->server_kex.methods[i] = strings[i];
|
|
|
|
}
|
2009-04-07 23:27:50 +04:00
|
|
|
}
|
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
/*
|
|
|
|
* Handle the two final fields for the KEXINIT message (RFC 4253 7.1):
|
|
|
|
*
|
|
|
|
* boolean first_kex_packet_follows
|
|
|
|
* uint32 0 (reserved for future extension)
|
|
|
|
*
|
|
|
|
* Notably if clients set 'first_kex_packet_follows', it is expected
|
|
|
|
* that its value is included when computing the session ID (see
|
|
|
|
* 'make_sessionid').
|
|
|
|
*/
|
|
|
|
if (server_kex) {
|
2015-09-17 10:43:33 +03:00
|
|
|
rc = ssh_buffer_get_u8(packet, &first_kex_packet_follows);
|
2014-04-15 12:48:24 +04:00
|
|
|
if (rc != 1) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
rc = ssh_buffer_add_u8(session->in_hashbuf, first_kex_packet_follows);
|
2014-04-15 12:48:24 +04:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
rc = ssh_buffer_add_u32(session->in_hashbuf, kexinit_reserved);
|
2014-04-15 12:48:24 +04:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:16:25 +03:00
|
|
|
/*
|
|
|
|
* If client sent a ext-info-c message in the kex list, it supports
|
|
|
|
* RFC 8308 extension negotiation.
|
|
|
|
*/
|
|
|
|
ok = ssh_match_group(session->next_crypto->client_kex.methods[SSH_KEX],
|
|
|
|
KEX_EXTENSION_CLIENT);
|
|
|
|
if (ok) {
|
2019-01-07 20:49:58 +03:00
|
|
|
const char *hostkeys = NULL;
|
|
|
|
|
|
|
|
/* The client supports extension negotiation */
|
|
|
|
session->extensions |= SSH_EXT_NEGOTIATION;
|
2018-07-03 17:16:25 +03:00
|
|
|
/*
|
2019-01-07 20:49:58 +03:00
|
|
|
* RFC 8332 Section 3.1: Use for Server Authentication
|
|
|
|
* Check what algorithms were provided in the SSH_HOSTKEYS list
|
|
|
|
* by the client and enable the respective extensions to provide
|
|
|
|
* correct signature in the next packet if RSA is negotiated
|
2018-07-03 17:16:25 +03:00
|
|
|
*/
|
2019-01-07 20:49:58 +03:00
|
|
|
hostkeys = session->next_crypto->client_kex.methods[SSH_HOSTKEYS];
|
|
|
|
ok = ssh_match_group(hostkeys, "rsa-sha2-512");
|
|
|
|
if (ok) {
|
2019-06-12 19:00:34 +03:00
|
|
|
/* Check if rsa-sha2-512 is allowed by config */
|
|
|
|
if (session->opts.wanted_methods[SSH_HOSTKEYS] != NULL) {
|
|
|
|
char *is_allowed =
|
|
|
|
ssh_find_matching(session->opts.wanted_methods[SSH_HOSTKEYS],
|
|
|
|
"rsa-sha2-512");
|
|
|
|
if (is_allowed != NULL) {
|
|
|
|
session->extensions |= SSH_EXT_SIG_RSA_SHA512;
|
|
|
|
}
|
|
|
|
SAFE_FREE(is_allowed);
|
|
|
|
}
|
2019-01-07 20:49:58 +03:00
|
|
|
}
|
|
|
|
ok = ssh_match_group(hostkeys, "rsa-sha2-256");
|
|
|
|
if (ok) {
|
2019-06-12 19:00:34 +03:00
|
|
|
/* Check if rsa-sha2-256 is allowed by config */
|
|
|
|
if (session->opts.wanted_methods[SSH_HOSTKEYS] != NULL) {
|
|
|
|
char *is_allowed =
|
|
|
|
ssh_find_matching(session->opts.wanted_methods[SSH_HOSTKEYS],
|
|
|
|
"rsa-sha2-256");
|
|
|
|
if (is_allowed != NULL) {
|
|
|
|
session->extensions |= SSH_EXT_SIG_RSA_SHA256;
|
|
|
|
}
|
|
|
|
SAFE_FREE(is_allowed);
|
|
|
|
}
|
2019-01-07 20:49:58 +03:00
|
|
|
}
|
2019-02-05 02:21:21 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the client preference is honored for the case
|
|
|
|
* both signature types are enabled.
|
|
|
|
*/
|
|
|
|
if ((session->extensions & SSH_EXT_SIG_RSA_SHA256) &&
|
|
|
|
(session->extensions & SSH_EXT_SIG_RSA_SHA512)) {
|
|
|
|
session->extensions &= ~(SSH_EXT_SIG_RSA_SHA256 | SSH_EXT_SIG_RSA_SHA512);
|
|
|
|
rsa_sig_ext = ssh_find_matching("rsa-sha2-512,rsa-sha2-256",
|
|
|
|
session->next_crypto->client_kex.methods[SSH_HOSTKEYS]);
|
|
|
|
if (rsa_sig_ext == NULL) {
|
|
|
|
goto error; /* should never happen */
|
|
|
|
} else if (strcmp(rsa_sig_ext, "rsa-sha2-512") == 0) {
|
|
|
|
session->extensions |= SSH_EXT_SIG_RSA_SHA512;
|
|
|
|
} else if (strcmp(rsa_sig_ext, "rsa-sha2-256") == 0) {
|
|
|
|
session->extensions |= SSH_EXT_SIG_RSA_SHA256;
|
|
|
|
} else {
|
|
|
|
SAFE_FREE(rsa_sig_ext);
|
|
|
|
goto error; /* should never happen */
|
|
|
|
}
|
|
|
|
SAFE_FREE(rsa_sig_ext);
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:16:25 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG, "The client supports extension "
|
2019-01-07 20:49:58 +03:00
|
|
|
"negotiation. Enabled signature algorithms: %s%s",
|
|
|
|
session->extensions & SSH_EXT_SIG_RSA_SHA256 ? "SHA256" : "",
|
|
|
|
session->extensions & SSH_EXT_SIG_RSA_SHA512 ? " SHA512" : "");
|
2018-07-03 17:16:25 +03:00
|
|
|
}
|
|
|
|
|
2014-04-22 12:11:03 +04:00
|
|
|
/*
|
|
|
|
* Remember whether 'first_kex_packet_follows' was set and the client
|
|
|
|
* guess was wrong: in this case the next SSH_MSG_KEXDH_INIT message
|
|
|
|
* must be ignored.
|
|
|
|
*/
|
|
|
|
if (first_kex_packet_follows) {
|
|
|
|
session->first_kex_follows_guess_wrong =
|
2015-05-05 02:22:35 +03:00
|
|
|
cmp_first_kex_algo(session->next_crypto->client_kex.methods[SSH_KEX],
|
|
|
|
session->next_crypto->server_kex.methods[SSH_KEX]) ||
|
|
|
|
cmp_first_kex_algo(session->next_crypto->client_kex.methods[SSH_HOSTKEYS],
|
|
|
|
session->next_crypto->server_kex.methods[SSH_HOSTKEYS]);
|
2014-04-22 12:11:03 +04:00
|
|
|
}
|
2014-04-10 02:24:04 +04:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:18 +03:00
|
|
|
/* Note, that his overwrites authenticated state in case of rekeying */
|
2014-04-15 12:48:24 +04:00
|
|
|
session->session_state = SSH_SESSION_STATE_KEXINIT_RECEIVED;
|
|
|
|
session->dh_handshake_state = DH_STATE_INIT;
|
|
|
|
session->ssh_connection_callback(session);
|
|
|
|
return SSH_PACKET_USED;
|
|
|
|
|
2009-04-07 23:27:50 +04:00
|
|
|
error:
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(str);
|
2014-04-15 12:48:24 +04:00
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
2014-10-19 10:23:26 +04:00
|
|
|
if (server_kex) {
|
|
|
|
session->next_crypto->client_kex.methods[i] = NULL;
|
|
|
|
} else { /* client */
|
|
|
|
session->next_crypto->server_kex.methods[i] = NULL;
|
|
|
|
}
|
2014-04-15 12:48:24 +04:00
|
|
|
SAFE_FREE(strings[i]);
|
|
|
|
}
|
2009-04-07 23:27:50 +04:00
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
session->session_state = SSH_SESSION_STATE_ERROR;
|
2013-07-14 15:31:24 +04:00
|
|
|
|
2014-04-15 12:48:24 +04:00
|
|
|
return SSH_PACKET_USED;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
void ssh_list_kex(struct ssh_kex_struct *kex) {
|
2009-03-12 18:45:38 +03:00
|
|
|
int i = 0;
|
|
|
|
|
2005-07-05 05:21:44 +04:00
|
|
|
#ifdef DEBUG_CRYPTO
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("session cookie", kex->cookie, 16);
|
2005-07-05 05:21:44 +04:00
|
|
|
#endif
|
2012-10-08 22:34:19 +04:00
|
|
|
|
2012-02-05 01:08:08 +04:00
|
|
|
for(i = 0; i < SSH_KEX_METHODS; i++) {
|
2012-10-08 22:34:19 +04:00
|
|
|
if (kex->methods[i] == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-14 14:29:45 +04:00
|
|
|
SSH_LOG(SSH_LOG_FUNCTIONS, "%s: %s",
|
2012-10-12 19:43:32 +04:00
|
|
|
ssh_kex_descriptions[i], kex->methods[i]);
|
2009-03-12 18:45:38 +03:00
|
|
|
}
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:28:30 +04:00
|
|
|
/**
|
|
|
|
* @internal
|
2019-07-01 20:39:07 +03:00
|
|
|
*
|
2014-02-05 01:28:30 +04:00
|
|
|
* @brief selects the hostkey mechanisms to be chosen for the key exchange,
|
2019-07-01 20:39:07 +03:00
|
|
|
* as some hostkey mechanisms may be present in known_hosts files.
|
|
|
|
*
|
2014-02-05 01:28:30 +04:00
|
|
|
* @returns a cstring containing a comma-separated list of hostkey methods.
|
|
|
|
* NULL if no method matches
|
|
|
|
*/
|
2018-10-18 17:18:22 +03:00
|
|
|
char *ssh_client_select_hostkeys(ssh_session session)
|
2018-07-03 09:19:14 +03:00
|
|
|
{
|
2019-07-01 20:39:07 +03:00
|
|
|
const char *wanted = NULL;
|
|
|
|
char *wanted_without_certs = NULL;
|
|
|
|
char *known_hosts_algorithms = NULL;
|
|
|
|
char *known_hosts_ordered = NULL;
|
2018-10-18 15:46:32 +03:00
|
|
|
char *new_hostkeys = NULL;
|
2019-07-01 20:39:07 +03:00
|
|
|
char *fips_hostkeys = NULL;
|
|
|
|
|
|
|
|
wanted = session->opts.wanted_methods[SSH_HOSTKEYS];
|
|
|
|
if (wanted == NULL) {
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
wanted = ssh_kex_get_fips_methods(SSH_HOSTKEYS);
|
|
|
|
} else {
|
|
|
|
wanted = ssh_kex_get_default_methods(SSH_HOSTKEYS);
|
|
|
|
}
|
2018-07-03 09:19:14 +03:00
|
|
|
}
|
2014-02-05 01:28:30 +04:00
|
|
|
|
2019-07-01 20:39:07 +03:00
|
|
|
/* This removes the certificate types, unsupported for now */
|
|
|
|
wanted_without_certs = ssh_find_all_matching(HOSTKEYS, wanted);
|
|
|
|
if (wanted_without_certs == NULL) {
|
|
|
|
SSH_LOG(SSH_LOG_WARNING,
|
|
|
|
"List of allowed host key algorithms is empty or contains only "
|
|
|
|
"unsupported algorithms");
|
2018-07-03 09:19:14 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:39:07 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG,
|
|
|
|
"Order of wanted host keys: \"%s\"",
|
|
|
|
wanted_without_certs);
|
|
|
|
|
|
|
|
known_hosts_algorithms = ssh_known_hosts_get_algorithms_names(session);
|
|
|
|
if (known_hosts_algorithms == NULL) {
|
|
|
|
SSH_LOG(SSH_LOG_DEBUG,
|
|
|
|
"No key found in known_hosts; "
|
|
|
|
"changing host key method to \"%s\"",
|
|
|
|
wanted_without_certs);
|
|
|
|
|
|
|
|
return wanted_without_certs;
|
2018-07-03 09:19:14 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 20:39:07 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG,
|
|
|
|
"Algorithms found in known_hosts files: \"%s\"",
|
|
|
|
known_hosts_algorithms);
|
|
|
|
|
|
|
|
/* Filter and order the keys from known_hosts according to wanted list */
|
|
|
|
known_hosts_ordered = ssh_find_all_matching(known_hosts_algorithms,
|
|
|
|
wanted_without_certs);
|
|
|
|
SAFE_FREE(known_hosts_algorithms);
|
|
|
|
if (known_hosts_ordered == NULL) {
|
2018-07-03 09:19:14 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG,
|
2019-07-01 20:39:07 +03:00
|
|
|
"No key found in known_hosts is allowed; "
|
|
|
|
"changing host key method to \"%s\"",
|
|
|
|
wanted_without_certs);
|
|
|
|
|
|
|
|
return wanted_without_certs;
|
2018-07-03 09:19:14 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 20:39:07 +03:00
|
|
|
/* Append the other supported keys after the preferred ones
|
|
|
|
* This function tolerates NULL pointers in parameters */
|
|
|
|
new_hostkeys = ssh_append_without_duplicates(known_hosts_ordered,
|
|
|
|
wanted_without_certs);
|
|
|
|
SAFE_FREE(known_hosts_ordered);
|
|
|
|
SAFE_FREE(wanted_without_certs);
|
2018-10-18 15:46:32 +03:00
|
|
|
if (new_hostkeys == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-01 20:39:07 +03:00
|
|
|
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
/* Filter out algorithms not allowed in FIPS mode */
|
|
|
|
fips_hostkeys = ssh_keep_fips_algos(SSH_HOSTKEYS, new_hostkeys);
|
|
|
|
SAFE_FREE(new_hostkeys);
|
|
|
|
if (fips_hostkeys == NULL) {
|
|
|
|
SSH_LOG(SSH_LOG_WARNING,
|
|
|
|
"None of the wanted host keys or keys in known_hosts files "
|
|
|
|
"is allowed in FIPS mode.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
new_hostkeys = fips_hostkeys;
|
|
|
|
}
|
2018-10-18 15:46:32 +03:00
|
|
|
|
2018-07-03 09:19:14 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG,
|
|
|
|
"Changing host key method to \"%s\"",
|
2018-10-18 15:46:32 +03:00
|
|
|
new_hostkeys);
|
2018-07-03 09:19:14 +03:00
|
|
|
|
2018-10-18 15:46:32 +03:00
|
|
|
return new_hostkeys;
|
2014-02-05 01:28:30 +04:00
|
|
|
}
|
2018-07-03 09:19:14 +03:00
|
|
|
|
2011-09-17 01:36:20 +04:00
|
|
|
/**
|
|
|
|
* @brief sets the key exchange parameters to be sent to the server,
|
|
|
|
* in function of the options and available methods.
|
|
|
|
*/
|
2018-08-06 12:47:32 +03:00
|
|
|
int ssh_set_client_kex(ssh_session session)
|
|
|
|
{
|
2019-07-01 20:39:07 +03:00
|
|
|
struct ssh_kex_struct *client = &session->next_crypto->client_kex;
|
2009-04-05 13:46:47 +04:00
|
|
|
const char *wanted;
|
2018-08-06 12:47:32 +03:00
|
|
|
char *kex = NULL;
|
|
|
|
char *kex_tmp = NULL;
|
2018-07-05 11:47:49 +03:00
|
|
|
int ok;
|
2014-02-05 01:28:30 +04:00
|
|
|
int i;
|
2018-08-06 12:47:32 +03:00
|
|
|
size_t kex_len, len;
|
2012-02-05 01:08:08 +04:00
|
|
|
|
2018-07-05 11:47:49 +03:00
|
|
|
ok = ssh_get_random(client->cookie, 16, 0);
|
|
|
|
if (!ok) {
|
|
|
|
ssh_set_error(session, SSH_FATAL, "PRNG error");
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
2012-02-05 01:08:08 +04:00
|
|
|
|
2019-11-01 16:59:27 +03:00
|
|
|
memset(client->methods, 0, SSH_KEX_METHODS * sizeof(char **));
|
2014-02-01 20:34:16 +04:00
|
|
|
|
2019-07-01 20:39:07 +03:00
|
|
|
/* Set the list of allowed algorithms in order of preference, if it hadn't
|
|
|
|
* been set yet. */
|
2019-11-01 16:59:27 +03:00
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
2019-07-01 20:39:07 +03:00
|
|
|
if (i == SSH_HOSTKEYS) {
|
|
|
|
/* Set the hostkeys in the following order:
|
|
|
|
* - First: keys present in known_hosts files ordered by preference
|
|
|
|
* - Next: other wanted algorithms ordered by preference */
|
|
|
|
client->methods[i] = ssh_client_select_hostkeys(session);
|
|
|
|
if (client->methods[i] == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-05 14:50:49 +04:00
|
|
|
wanted = session->opts.wanted_methods[i];
|
2019-05-22 19:33:14 +03:00
|
|
|
if (wanted == NULL) {
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
wanted = fips_methods[i];
|
|
|
|
} else {
|
|
|
|
wanted = default_methods[i];
|
|
|
|
}
|
|
|
|
}
|
2012-02-05 01:08:08 +04:00
|
|
|
client->methods[i] = strdup(wanted);
|
2018-06-27 14:48:04 +03:00
|
|
|
if (client->methods[i] == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
2011-09-17 01:36:20 +04:00
|
|
|
}
|
2012-02-05 01:08:08 +04:00
|
|
|
|
2018-11-15 12:22:19 +03:00
|
|
|
/* For rekeying, skip the extension negotiation */
|
2018-12-11 22:03:30 +03:00
|
|
|
if (session->flags & SSH_SESSION_FLAG_AUTHENTICATED) {
|
2018-11-15 12:22:19 +03:00
|
|
|
return SSH_OK;
|
|
|
|
}
|
|
|
|
|
2018-08-06 12:47:32 +03:00
|
|
|
/* Here we append ext-info-c to the list of kex algorithms */
|
|
|
|
kex = client->methods[SSH_KEX];
|
|
|
|
len = strlen(kex);
|
|
|
|
if (len + strlen(KEX_EXTENSION_CLIENT) + 2 < len) {
|
|
|
|
/* Overflow */
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
kex_len = len + strlen(KEX_EXTENSION_CLIENT) + 2; /* comma, NULL */
|
|
|
|
kex_tmp = realloc(kex, kex_len);
|
|
|
|
if (kex_tmp == NULL) {
|
|
|
|
free(kex);
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
snprintf(kex_tmp + len, kex_len - len, ",%s", KEX_EXTENSION_CLIENT);
|
|
|
|
client->methods[SSH_KEX] = kex_tmp;
|
|
|
|
|
2011-09-17 01:36:20 +04:00
|
|
|
return SSH_OK;
|
|
|
|
}
|
|
|
|
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
static const char *ssh_find_aead_hmac(const char *cipher)
|
|
|
|
{
|
|
|
|
if (cipher == NULL) {
|
|
|
|
return NULL;
|
|
|
|
} else if (strcmp(cipher, "chacha20-poly1305@openssh.com") == 0) {
|
|
|
|
return "aead-poly1305";
|
|
|
|
} else if (strcmp(cipher, "aes256-gcm@openssh.com") == 0) {
|
|
|
|
return "aead-gcm";
|
|
|
|
} else if (strcmp(cipher, "aes128-gcm@openssh.com") == 0) {
|
|
|
|
return "aead-gcm";
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-17 01:36:20 +04:00
|
|
|
/** @brief Select the different methods on basis of client's and
|
|
|
|
* server's kex messages, and watches out if a match is possible.
|
|
|
|
*/
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
int ssh_kex_select_methods (ssh_session session)
|
|
|
|
{
|
2011-09-17 14:10:34 +04:00
|
|
|
struct ssh_kex_struct *server = &session->next_crypto->server_kex;
|
|
|
|
struct ssh_kex_struct *client = &session->next_crypto->client_kex;
|
2018-08-06 12:47:32 +03:00
|
|
|
char *ext_start = NULL;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
const char *aead_hmac = NULL;
|
2011-09-17 01:36:20 +04:00
|
|
|
int i;
|
|
|
|
|
2018-08-06 12:47:32 +03:00
|
|
|
/* Here we should drop the ext-info-c from the list so we avoid matching.
|
|
|
|
* it. We added it to the end, so we can just truncate the string here */
|
|
|
|
ext_start = strstr(client->methods[SSH_KEX], ","KEX_EXTENSION_CLIENT);
|
|
|
|
if (ext_start != NULL) {
|
|
|
|
ext_start[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-11-01 16:59:27 +03:00
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
2011-09-17 02:17:45 +04:00
|
|
|
session->next_crypto->kex_methods[i]=ssh_find_matching(server->methods[i],client->methods[i]);
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
|
|
|
|
if (i == SSH_MAC_C_S || i == SSH_MAC_S_C) {
|
|
|
|
aead_hmac = ssh_find_aead_hmac(session->next_crypto->kex_methods[i-2]);
|
|
|
|
if (aead_hmac) {
|
|
|
|
free(session->next_crypto->kex_methods[i]);
|
|
|
|
session->next_crypto->kex_methods[i] = strdup(aead_hmac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (session->next_crypto->kex_methods[i] == NULL && i < SSH_LANG_C_S){
|
2011-09-17 01:36:20 +04:00
|
|
|
ssh_set_error(session,SSH_FATAL,"kex error : no match for method %s: server [%s], client [%s]",
|
2012-10-12 19:43:32 +04:00
|
|
|
ssh_kex_descriptions[i],server->methods[i],client->methods[i]);
|
2013-07-14 15:31:24 +04:00
|
|
|
return SSH_ERROR;
|
2011-09-17 02:17:45 +04:00
|
|
|
} else if ((i >= SSH_LANG_C_S) && (session->next_crypto->kex_methods[i] == NULL)) {
|
2009-04-02 13:35:41 +04:00
|
|
|
/* we can safely do that for languages */
|
2011-09-17 02:17:45 +04:00
|
|
|
session->next_crypto->kex_methods[i] = strdup("");
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
}
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group1-sha1") == 0){
|
2011-06-13 15:46:34 +04:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GROUP1_SHA1;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group14-sha1") == 0){
|
2012-09-04 18:16:15 +04:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GROUP14_SHA1;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group14-sha256") == 0){
|
2020-04-03 13:49:19 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GROUP14_SHA256;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group16-sha512") == 0){
|
2018-09-10 15:56:07 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GROUP16_SHA512;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group18-sha512") == 0){
|
2018-09-20 11:39:13 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GROUP18_SHA512;
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group-exchange-sha1") == 0){
|
2018-11-06 23:44:46 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GEX_SHA1;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group-exchange-sha256") == 0){
|
2018-11-06 23:44:46 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_DH_GEX_SHA256;
|
2019-02-02 18:49:05 +03:00
|
|
|
#endif /* WITH_GEX */
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "ecdh-sha2-nistp256") == 0){
|
2011-06-13 15:46:34 +04:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_ECDH_SHA2_NISTP256;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "ecdh-sha2-nistp384") == 0){
|
2017-08-24 19:14:38 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_ECDH_SHA2_NISTP384;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "ecdh-sha2-nistp521") == 0){
|
2017-08-24 19:14:38 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_ECDH_SHA2_NISTP521;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "curve25519-sha256@libssh.org") == 0){
|
2013-09-22 01:34:50 +04:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG;
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
} else if (strcmp(session->next_crypto->kex_methods[SSH_KEX], "curve25519-sha256") == 0){
|
2018-06-25 14:01:57 +03:00
|
|
|
session->next_crypto->kex_type=SSH_KEX_CURVE25519_SHA256;
|
2011-06-13 15:46:34 +04:00
|
|
|
}
|
2015-12-21 14:06:11 +03:00
|
|
|
SSH_LOG(SSH_LOG_INFO, "Negotiated %s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
|
|
|
|
session->next_crypto->kex_methods[SSH_KEX],
|
|
|
|
session->next_crypto->kex_methods[SSH_HOSTKEYS],
|
|
|
|
session->next_crypto->kex_methods[SSH_CRYPT_C_S],
|
|
|
|
session->next_crypto->kex_methods[SSH_CRYPT_S_C],
|
|
|
|
session->next_crypto->kex_methods[SSH_MAC_C_S],
|
|
|
|
session->next_crypto->kex_methods[SSH_MAC_S_C],
|
|
|
|
session->next_crypto->kex_methods[SSH_COMP_C_S],
|
|
|
|
session->next_crypto->kex_methods[SSH_COMP_S_C],
|
|
|
|
session->next_crypto->kex_methods[SSH_LANG_C_S],
|
|
|
|
session->next_crypto->kex_methods[SSH_LANG_S_C]
|
|
|
|
);
|
2013-07-14 15:31:24 +04:00
|
|
|
return SSH_OK;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2011-09-17 01:36:20 +04:00
|
|
|
|
2009-04-07 22:46:29 +04:00
|
|
|
/* this function only sends the predefined set of kex methods */
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
int ssh_send_kex(ssh_session session, int server_kex)
|
|
|
|
{
|
2011-09-17 14:10:34 +04:00
|
|
|
struct ssh_kex_struct *kex = (server_kex ? &session->next_crypto->server_kex :
|
2011-09-17 02:17:45 +04:00
|
|
|
&session->next_crypto->client_kex);
|
2009-07-24 23:45:41 +04:00
|
|
|
ssh_string str = NULL;
|
2009-04-07 23:37:45 +04:00
|
|
|
int i;
|
2014-04-10 19:46:54 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_buffer_pack(session->out_buffer,
|
|
|
|
"bP",
|
|
|
|
SSH2_MSG_KEXINIT,
|
|
|
|
16,
|
|
|
|
kex->cookie); /* cookie */
|
|
|
|
if (rc != SSH_OK)
|
2009-04-07 23:37:45 +04:00
|
|
|
goto error;
|
2015-09-17 11:26:05 +03:00
|
|
|
if (ssh_hashbufout_add_cookie(session) < 0) {
|
2009-04-07 23:37:45 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-07-14 14:29:45 +04:00
|
|
|
ssh_list_kex(kex);
|
2009-04-07 23:37:45 +04:00
|
|
|
|
2019-11-01 16:59:27 +03:00
|
|
|
for (i = 0; i < SSH_KEX_METHODS; i++) {
|
2010-05-14 02:51:08 +04:00
|
|
|
str = ssh_string_from_char(kex->methods[i]);
|
2009-04-07 23:37:45 +04:00
|
|
|
if (str == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_ssh_string(session->out_hashbuf, str) < 0) {
|
2009-04-07 23:37:45 +04:00
|
|
|
goto error;
|
2009-04-07 22:46:29 +04:00
|
|
|
}
|
2015-09-17 10:43:33 +03:00
|
|
|
if (ssh_buffer_add_ssh_string(session->out_buffer, str) < 0) {
|
2009-04-07 23:37:45 +04:00
|
|
|
goto error;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(str);
|
2013-06-13 12:51:12 +04:00
|
|
|
str = NULL;
|
2009-04-07 23:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-04-10 19:46:54 +04:00
|
|
|
rc = ssh_buffer_pack(session->out_buffer,
|
|
|
|
"bd",
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
if (rc != SSH_OK) {
|
2009-04-07 23:37:45 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-09-17 15:11:08 +03:00
|
|
|
if (ssh_packet_send(session) == SSH_ERROR) {
|
2009-04-07 23:37:45 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:18 +03:00
|
|
|
SSH_LOG(SSH_LOG_PACKET, "SSH_MSG_KEXINIT sent");
|
2009-04-07 23:37:45 +04:00
|
|
|
return 0;
|
|
|
|
error:
|
2014-01-19 23:43:29 +04:00
|
|
|
ssh_buffer_reinit(session->out_buffer);
|
|
|
|
ssh_buffer_reinit(session->out_hashbuf);
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(str);
|
2009-04-07 23:37:45 +04:00
|
|
|
|
|
|
|
return -1;
|
2005-07-05 05:21:44 +04:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:18 +03:00
|
|
|
/*
|
|
|
|
* Key re-exchange (rekey) is triggered by this function.
|
|
|
|
* It can not be called again after the rekey is initialized!
|
|
|
|
*/
|
|
|
|
int ssh_send_rekex(ssh_session session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (session->dh_handshake_state != DH_STATE_FINISHED) {
|
|
|
|
/* Rekey/Key exchange is already in progress */
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Attempting rekey in bad state");
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->current_crypto == NULL) {
|
|
|
|
/* No current crypto used -- can not exchange it */
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "No crypto to rekey");
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->client) {
|
|
|
|
rc = ssh_set_client_kex(session);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Failed to set client kex");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef WITH_SERVER
|
|
|
|
rc = server_set_kex(session);
|
|
|
|
if (rc == SSH_ERROR) {
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Failed to set server kex");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Invalid session state.");
|
|
|
|
return SSH_ERROR;
|
|
|
|
#endif /* WITH_SERVER */
|
|
|
|
}
|
|
|
|
|
|
|
|
session->dh_handshake_state = DH_STATE_INIT;
|
|
|
|
rc = ssh_send_kex(session, session->server);
|
|
|
|
if (rc < 0) {
|
|
|
|
SSH_LOG(SSH_LOG_PACKET, "Failed to send kex");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the handshake state */
|
|
|
|
session->dh_handshake_state = DH_STATE_INIT_SENT;
|
|
|
|
return SSH_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:28:39 +03:00
|
|
|
/* returns a copy of the provided list if everything is supported,
|
|
|
|
* otherwise a new list of the supported algorithms */
|
|
|
|
char *ssh_keep_known_algos(enum ssh_kex_types_e algo, const char *list)
|
|
|
|
{
|
|
|
|
if (algo > SSH_LANG_S_C) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh_find_all_matching(supported_methods[algo], list);
|
|
|
|
}
|
2018-10-28 00:54:56 +03:00
|
|
|
|
2019-05-22 19:33:14 +03:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @brief Return a new allocated string containing only the FIPS allowed
|
|
|
|
* algorithms from the list.
|
|
|
|
*
|
|
|
|
* @param[in] algo The type of the methods to filter
|
|
|
|
* @param[in] list The list to be filtered
|
|
|
|
*
|
|
|
|
* @return A new allocated list containing only the FIPS allowed algorithms from
|
|
|
|
* the list; NULL in case of error.
|
|
|
|
*/
|
|
|
|
char *ssh_keep_fips_algos(enum ssh_kex_types_e algo, const char *list)
|
|
|
|
{
|
|
|
|
if (algo > SSH_LANG_S_C) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh_find_all_matching(fips_methods[algo], list);
|
|
|
|
}
|
|
|
|
|
2018-10-28 00:54:56 +03:00
|
|
|
int ssh_make_sessionid(ssh_session session)
|
|
|
|
{
|
|
|
|
ssh_string num = NULL;
|
|
|
|
ssh_buffer server_hash = NULL;
|
|
|
|
ssh_buffer client_hash = NULL;
|
|
|
|
ssh_buffer buf = NULL;
|
|
|
|
ssh_string server_pubkey_blob = NULL;
|
2019-03-13 21:20:52 +03:00
|
|
|
const_bignum client_pubkey, server_pubkey;
|
|
|
|
#ifdef WITH_GEX
|
|
|
|
const_bignum modulus, generator;
|
|
|
|
#endif
|
2018-10-28 00:54:56 +03:00
|
|
|
int rc = SSH_ERROR;
|
|
|
|
|
|
|
|
buf = ssh_buffer_new();
|
|
|
|
if (buf == NULL) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"ss",
|
|
|
|
session->clientbanner,
|
|
|
|
session->serverbanner);
|
|
|
|
if (rc == SSH_ERROR) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->client) {
|
|
|
|
server_hash = session->in_hashbuf;
|
|
|
|
client_hash = session->out_hashbuf;
|
|
|
|
} else {
|
|
|
|
server_hash = session->out_hashbuf;
|
|
|
|
client_hash = session->in_hashbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle the two final fields for the KEXINIT message (RFC 4253 7.1):
|
|
|
|
*
|
|
|
|
* boolean first_kex_packet_follows
|
|
|
|
* uint32 0 (reserved for future extension)
|
|
|
|
*/
|
|
|
|
rc = ssh_buffer_add_u8(server_hash, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_buffer_add_u32(server_hash, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These fields are handled for the server case in ssh_packet_kexinit. */
|
|
|
|
if (session->client) {
|
|
|
|
rc = ssh_buffer_add_u8(client_hash, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_buffer_add_u32(client_hash, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_dh_get_next_server_publickey_blob(session, &server_pubkey_blob);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"dPdPS",
|
|
|
|
ssh_buffer_get_len(client_hash),
|
|
|
|
ssh_buffer_get_len(client_hash),
|
|
|
|
ssh_buffer_get(client_hash),
|
|
|
|
ssh_buffer_get_len(server_hash),
|
|
|
|
ssh_buffer_get_len(server_hash),
|
|
|
|
ssh_buffer_get(server_hash),
|
|
|
|
server_pubkey_blob);
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(server_pubkey_blob);
|
Fix handshake bug with AEAD ciphers and no HMAC overlap
There's currently a bug in libssh that a handshake doesn't complete if
there is no overlap between HMAC methods, but when an AEAD cipher is
used.
In case of an AEAD cipher such as chacha20-poly1305 or aes256-gcm, the
HMAC algorithm that is being picked is not relevant. But the problem
here is that the HMAC still needs to have an overlap in the handshake,
even if it is not used afterwards.
This was found with a very strict server side configuration with libssh
where only AEAD ciphers and EtM HMAC modes are accepted. The client
tested against was dropbear.
Dropbear does have support for chacha20-poly1305 and AES GCM modes, but
no support for EtM HMAC modes. This meant that the libssh server in this
case rejected the dropbear client, even though it is perfectly able to
serve it since dropbear supports AEAD algorithms.
The fix implemented here updates the HMAC phase of the handshake to
handle this case. If it detects an AEAD cipher is used, it uses the HMAC
abbreviations for the method instead. This is the same name that is used
in other places as well. It matches the client to server and server to
client values, but it does depend on the order of things in the
ssh_kex_types_e enum, which I'm assuming here is ok since it's explicit.
I've looked at how to add a test for this, but I couldn't really find a
suitable place for it. I would love some tips if this is easily
possible, or if it's easier for someone else to contribute, that's of
course welcome too.
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
2020-09-29 16:14:09 +03:00
|
|
|
if (rc != SSH_OK){
|
2018-10-28 00:54:56 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(session->next_crypto->kex_type) {
|
|
|
|
case SSH_KEX_DH_GROUP1_SHA1:
|
|
|
|
case SSH_KEX_DH_GROUP14_SHA1:
|
2020-04-03 13:49:19 +03:00
|
|
|
case SSH_KEX_DH_GROUP14_SHA256:
|
2018-10-28 00:54:56 +03:00
|
|
|
case SSH_KEX_DH_GROUP16_SHA512:
|
|
|
|
case SSH_KEX_DH_GROUP18_SHA512:
|
2019-03-13 21:20:52 +03:00
|
|
|
rc = ssh_dh_keypair_get_keys(session->next_crypto->dh_ctx,
|
|
|
|
DH_CLIENT_KEYPAIR, NULL, &client_pubkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_dh_keypair_get_keys(session->next_crypto->dh_ctx,
|
|
|
|
DH_SERVER_KEYPAIR, NULL, &server_pubkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-10-28 00:54:56 +03:00
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"BB",
|
2019-03-13 21:20:52 +03:00
|
|
|
client_pubkey,
|
|
|
|
server_pubkey);
|
2018-10-28 00:54:56 +03:00
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
2018-11-06 23:44:46 +03:00
|
|
|
case SSH_KEX_DH_GEX_SHA1:
|
|
|
|
case SSH_KEX_DH_GEX_SHA256:
|
2019-03-13 21:20:52 +03:00
|
|
|
rc = ssh_dh_keypair_get_keys(session->next_crypto->dh_ctx,
|
|
|
|
DH_CLIENT_KEYPAIR, NULL, &client_pubkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_dh_keypair_get_keys(session->next_crypto->dh_ctx,
|
|
|
|
DH_SERVER_KEYPAIR, NULL, &server_pubkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_dh_get_parameters(session->next_crypto->dh_ctx,
|
|
|
|
&modulus, &generator);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-11-06 23:44:46 +03:00
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"dddBBBB",
|
2018-11-07 18:15:50 +03:00
|
|
|
session->next_crypto->dh_pmin,
|
|
|
|
session->next_crypto->dh_pn,
|
|
|
|
session->next_crypto->dh_pmax,
|
2019-03-13 21:20:52 +03:00
|
|
|
modulus,
|
|
|
|
generator,
|
|
|
|
client_pubkey,
|
|
|
|
server_pubkey);
|
2018-11-06 23:44:46 +03:00
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-02 18:49:05 +03:00
|
|
|
#endif /* WITH_GEX */
|
2018-10-28 00:54:56 +03:00
|
|
|
#ifdef HAVE_ECDH
|
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP256:
|
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP384:
|
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP521:
|
|
|
|
if (session->next_crypto->ecdh_client_pubkey == NULL ||
|
|
|
|
session->next_crypto->ecdh_server_pubkey == NULL) {
|
|
|
|
SSH_LOG(SSH_LOG_WARNING, "ECDH parameted missing");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"SS",
|
|
|
|
session->next_crypto->ecdh_client_pubkey,
|
|
|
|
session->next_crypto->ecdh_server_pubkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CURVE25519
|
|
|
|
case SSH_KEX_CURVE25519_SHA256:
|
|
|
|
case SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG:
|
|
|
|
rc = ssh_buffer_pack(buf,
|
|
|
|
"dPdP",
|
|
|
|
CURVE25519_PUBKEY_SIZE,
|
|
|
|
(size_t)CURVE25519_PUBKEY_SIZE, session->next_crypto->curve25519_client_pubkey,
|
|
|
|
CURVE25519_PUBKEY_SIZE,
|
|
|
|
(size_t)CURVE25519_PUBKEY_SIZE, session->next_crypto->curve25519_server_pubkey);
|
|
|
|
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2019-03-13 01:24:36 +03:00
|
|
|
rc = ssh_buffer_pack(buf, "B", session->next_crypto->shared_secret);
|
2018-10-28 00:54:56 +03:00
|
|
|
if (rc != SSH_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CRYPTO
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("hash buffer", ssh_buffer_get(buf), ssh_buffer_get_len(buf));
|
2018-10-28 00:54:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (session->next_crypto->kex_type) {
|
|
|
|
case SSH_KEX_DH_GROUP1_SHA1:
|
|
|
|
case SSH_KEX_DH_GROUP14_SHA1:
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
2018-11-06 23:44:46 +03:00
|
|
|
case SSH_KEX_DH_GEX_SHA1:
|
2019-02-02 18:49:05 +03:00
|
|
|
#endif /* WITH_GEX */
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->digest_len = SHA_DIGEST_LENGTH;
|
2018-10-02 01:00:01 +03:00
|
|
|
session->next_crypto->digest_type = SSH_KDF_SHA1;
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->secret_hash = malloc(session->next_crypto->digest_len);
|
|
|
|
if (session->next_crypto->secret_hash == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
sha1(ssh_buffer_get(buf), ssh_buffer_get_len(buf),
|
|
|
|
session->next_crypto->secret_hash);
|
|
|
|
break;
|
2020-04-03 13:49:19 +03:00
|
|
|
case SSH_KEX_DH_GROUP14_SHA256:
|
2018-10-28 00:54:56 +03:00
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP256:
|
|
|
|
case SSH_KEX_CURVE25519_SHA256:
|
|
|
|
case SSH_KEX_CURVE25519_SHA256_LIBSSH_ORG:
|
2019-02-02 18:49:05 +03:00
|
|
|
#ifdef WITH_GEX
|
2018-11-06 23:44:46 +03:00
|
|
|
case SSH_KEX_DH_GEX_SHA256:
|
2019-02-02 18:49:05 +03:00
|
|
|
#endif /* WITH_GEX */
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->digest_len = SHA256_DIGEST_LENGTH;
|
2018-10-02 01:00:01 +03:00
|
|
|
session->next_crypto->digest_type = SSH_KDF_SHA256;
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->secret_hash = malloc(session->next_crypto->digest_len);
|
|
|
|
if (session->next_crypto->secret_hash == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
sha256(ssh_buffer_get(buf), ssh_buffer_get_len(buf),
|
|
|
|
session->next_crypto->secret_hash);
|
|
|
|
break;
|
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP384:
|
|
|
|
session->next_crypto->digest_len = SHA384_DIGEST_LENGTH;
|
2018-10-02 01:00:01 +03:00
|
|
|
session->next_crypto->digest_type = SSH_KDF_SHA384;
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->secret_hash = malloc(session->next_crypto->digest_len);
|
|
|
|
if (session->next_crypto->secret_hash == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
sha384(ssh_buffer_get(buf), ssh_buffer_get_len(buf),
|
|
|
|
session->next_crypto->secret_hash);
|
|
|
|
break;
|
|
|
|
case SSH_KEX_DH_GROUP16_SHA512:
|
|
|
|
case SSH_KEX_DH_GROUP18_SHA512:
|
|
|
|
case SSH_KEX_ECDH_SHA2_NISTP521:
|
|
|
|
session->next_crypto->digest_len = SHA512_DIGEST_LENGTH;
|
2018-10-02 01:00:01 +03:00
|
|
|
session->next_crypto->digest_type = SSH_KDF_SHA512;
|
2018-10-28 00:54:56 +03:00
|
|
|
session->next_crypto->secret_hash = malloc(session->next_crypto->digest_len);
|
|
|
|
if (session->next_crypto->secret_hash == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
sha512(ssh_buffer_get(buf),
|
|
|
|
ssh_buffer_get_len(buf),
|
|
|
|
session->next_crypto->secret_hash);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* During the first kex, secret hash and session ID are equal. However, after
|
|
|
|
* a key re-exchange, a new secret hash is calculated. This hash will not replace
|
|
|
|
* but complement existing session id.
|
|
|
|
*/
|
|
|
|
if (!session->next_crypto->session_id) {
|
|
|
|
session->next_crypto->session_id = malloc(session->next_crypto->digest_len);
|
|
|
|
if (session->next_crypto->session_id == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(session->next_crypto->session_id, session->next_crypto->secret_hash,
|
|
|
|
session->next_crypto->digest_len);
|
2021-06-23 14:16:33 +03:00
|
|
|
/* Initial length is the same as secret hash */
|
|
|
|
session->next_crypto->session_id_len = session->next_crypto->digest_len;
|
2018-10-28 00:54:56 +03:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_CRYPTO
|
2021-06-25 21:06:18 +03:00
|
|
|
SSH_LOG(SSH_LOG_DEBUG, "Session hash: \n");
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("secret hash", session->next_crypto->secret_hash, session->next_crypto->digest_len);
|
2021-06-23 14:16:33 +03:00
|
|
|
ssh_log_hexdump("session id", session->next_crypto->session_id, session->next_crypto->session_id_len);
|
2018-10-28 00:54:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rc = SSH_OK;
|
|
|
|
error:
|
2019-10-28 15:52:32 +03:00
|
|
|
SSH_BUFFER_FREE(buf);
|
|
|
|
SSH_BUFFER_FREE(client_hash);
|
|
|
|
SSH_BUFFER_FREE(server_hash);
|
2018-10-28 00:54:56 +03:00
|
|
|
|
|
|
|
session->in_hashbuf = NULL;
|
|
|
|
session->out_hashbuf = NULL;
|
|
|
|
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(num);
|
2018-10-28 00:54:56 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssh_hashbufout_add_cookie(ssh_session session)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
session->out_hashbuf = ssh_buffer_new();
|
|
|
|
if (session->out_hashbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_allocate_size(session->out_hashbuf,
|
|
|
|
sizeof(uint8_t) + 16);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_buffer_reinit(session->out_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssh_buffer_add_u8(session->out_hashbuf, 20) < 0) {
|
|
|
|
ssh_buffer_reinit(session->out_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->server) {
|
|
|
|
if (ssh_buffer_add_data(session->out_hashbuf,
|
|
|
|
session->next_crypto->server_kex.cookie, 16) < 0) {
|
|
|
|
ssh_buffer_reinit(session->out_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ssh_buffer_add_data(session->out_hashbuf,
|
|
|
|
session->next_crypto->client_kex.cookie, 16) < 0) {
|
|
|
|
ssh_buffer_reinit(session->out_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssh_hashbufin_add_cookie(ssh_session session, unsigned char *cookie)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
session->in_hashbuf = ssh_buffer_new();
|
|
|
|
if (session->in_hashbuf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_buffer_allocate_size(session->in_hashbuf,
|
|
|
|
sizeof(uint8_t) + 20 + 16);
|
|
|
|
if (rc < 0) {
|
|
|
|
ssh_buffer_reinit(session->in_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssh_buffer_add_u8(session->in_hashbuf, 20) < 0) {
|
|
|
|
ssh_buffer_reinit(session->in_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ssh_buffer_add_data(session->in_hashbuf,cookie, 16) < 0) {
|
|
|
|
ssh_buffer_reinit(session->in_hashbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssh_generate_session_keys(ssh_session session)
|
|
|
|
{
|
|
|
|
ssh_string k_string = NULL;
|
|
|
|
struct ssh_crypto_struct *crypto = session->next_crypto;
|
2018-10-02 01:00:01 +03:00
|
|
|
unsigned char *key = NULL;
|
2018-10-01 19:56:32 +03:00
|
|
|
unsigned char *IV_cli_to_srv = NULL;
|
|
|
|
unsigned char *IV_srv_to_cli = NULL;
|
|
|
|
unsigned char *enckey_cli_to_srv = NULL;
|
|
|
|
unsigned char *enckey_srv_to_cli = NULL;
|
|
|
|
unsigned char *intkey_cli_to_srv = NULL;
|
|
|
|
unsigned char *intkey_srv_to_cli = NULL;
|
2018-10-02 01:00:01 +03:00
|
|
|
size_t key_len = 0;
|
2018-10-01 19:56:32 +03:00
|
|
|
size_t IV_len = 0;
|
|
|
|
size_t enckey_cli_to_srv_len = 0;
|
|
|
|
size_t enckey_srv_to_cli_len = 0;
|
|
|
|
size_t intkey_cli_to_srv_len = 0;
|
|
|
|
size_t intkey_srv_to_cli_len = 0;
|
2018-10-28 00:54:56 +03:00
|
|
|
int rc = -1;
|
|
|
|
|
2019-03-13 01:24:36 +03:00
|
|
|
k_string = ssh_make_bignum_string(crypto->shared_secret);
|
2018-10-28 00:54:56 +03:00
|
|
|
if (k_string == NULL) {
|
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
2018-10-02 01:00:01 +03:00
|
|
|
/* See RFC4251 Section 5 for the definition of mpint which is the
|
|
|
|
* encoding we need to use for key in the SSH KDF */
|
|
|
|
key = (unsigned char *)k_string;
|
|
|
|
key_len = ssh_string_len(k_string) + 4;
|
2018-10-28 00:54:56 +03:00
|
|
|
|
2018-10-01 19:56:32 +03:00
|
|
|
IV_len = crypto->digest_len;
|
|
|
|
if (session->client) {
|
|
|
|
enckey_cli_to_srv_len = crypto->out_cipher->keysize / 8;
|
|
|
|
enckey_srv_to_cli_len = crypto->in_cipher->keysize / 8;
|
|
|
|
intkey_cli_to_srv_len = hmac_digest_len(crypto->out_hmac);
|
|
|
|
intkey_srv_to_cli_len = hmac_digest_len(crypto->in_hmac);
|
|
|
|
} else {
|
|
|
|
enckey_cli_to_srv_len = crypto->in_cipher->keysize / 8;
|
|
|
|
enckey_srv_to_cli_len = crypto->out_cipher->keysize / 8;
|
|
|
|
intkey_cli_to_srv_len = hmac_digest_len(crypto->in_hmac);
|
|
|
|
intkey_srv_to_cli_len = hmac_digest_len(crypto->out_hmac);
|
|
|
|
}
|
|
|
|
|
|
|
|
IV_cli_to_srv = malloc(IV_len);
|
|
|
|
IV_srv_to_cli = malloc(IV_len);
|
|
|
|
enckey_cli_to_srv = malloc(enckey_cli_to_srv_len);
|
|
|
|
enckey_srv_to_cli = malloc(enckey_srv_to_cli_len);
|
|
|
|
intkey_cli_to_srv = malloc(intkey_cli_to_srv_len);
|
|
|
|
intkey_srv_to_cli = malloc(intkey_srv_to_cli_len);
|
|
|
|
if (IV_cli_to_srv == NULL || IV_srv_to_cli == NULL ||
|
|
|
|
enckey_cli_to_srv == NULL || enckey_srv_to_cli == NULL ||
|
|
|
|
intkey_cli_to_srv == NULL || intkey_srv_to_cli == NULL) {
|
2018-10-28 00:54:56 +03:00
|
|
|
ssh_set_error_oom(session);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IV */
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'A', IV_cli_to_srv, IV_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
2018-10-28 00:54:56 +03:00
|
|
|
}
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'B', IV_srv_to_cli, IV_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Encryption Key */
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'C', enckey_cli_to_srv,
|
|
|
|
enckey_cli_to_srv_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'D', enckey_srv_to_cli,
|
|
|
|
enckey_srv_to_cli_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Integrity Key */
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'E', intkey_cli_to_srv,
|
|
|
|
intkey_cli_to_srv_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-10-02 01:00:01 +03:00
|
|
|
rc = ssh_kdf(crypto, key, key_len, 'F', intkey_srv_to_cli,
|
|
|
|
intkey_srv_to_cli_len);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
goto error;
|
2018-10-28 00:54:56 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 19:56:32 +03:00
|
|
|
if (session->client) {
|
|
|
|
crypto->encryptIV = IV_cli_to_srv;
|
|
|
|
crypto->decryptIV = IV_srv_to_cli;
|
|
|
|
crypto->encryptkey = enckey_cli_to_srv;
|
|
|
|
crypto->decryptkey = enckey_srv_to_cli;
|
|
|
|
crypto->encryptMAC = intkey_cli_to_srv;
|
|
|
|
crypto->decryptMAC = intkey_srv_to_cli;
|
2018-10-28 00:54:56 +03:00
|
|
|
} else {
|
2018-10-01 19:56:32 +03:00
|
|
|
crypto->encryptIV = IV_srv_to_cli;
|
|
|
|
crypto->decryptIV = IV_cli_to_srv;
|
|
|
|
crypto->encryptkey = enckey_srv_to_cli;
|
|
|
|
crypto->decryptkey = enckey_cli_to_srv;
|
|
|
|
crypto->encryptMAC = intkey_srv_to_cli;
|
|
|
|
crypto->decryptMAC = intkey_cli_to_srv;
|
2018-10-28 00:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CRYPTO
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("Client to Server IV", IV_cli_to_srv, IV_len);
|
|
|
|
ssh_log_hexdump("Server to Client IV", IV_srv_to_cli, IV_len);
|
|
|
|
ssh_log_hexdump("Client to Server Encryption Key", enckey_cli_to_srv,
|
2018-10-01 19:56:32 +03:00
|
|
|
enckey_cli_to_srv_len);
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("Server to Client Encryption Key", enckey_srv_to_cli,
|
2018-10-01 19:56:32 +03:00
|
|
|
enckey_srv_to_cli_len);
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("Client to Server Integrity Key", intkey_cli_to_srv,
|
2018-10-01 19:56:32 +03:00
|
|
|
intkey_cli_to_srv_len);
|
2019-07-29 18:40:35 +03:00
|
|
|
ssh_log_hexdump("Server to Client Integrity Key", intkey_srv_to_cli,
|
2018-10-01 19:56:32 +03:00
|
|
|
intkey_srv_to_cli_len);
|
2018-10-28 00:54:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
error:
|
2018-11-06 18:12:20 +03:00
|
|
|
ssh_string_burn(k_string);
|
2019-10-28 15:52:05 +03:00
|
|
|
SSH_STRING_FREE(k_string);
|
2018-10-01 19:56:32 +03:00
|
|
|
if (rc != 0) {
|
|
|
|
free(IV_cli_to_srv);
|
|
|
|
free(IV_srv_to_cli);
|
|
|
|
free(enckey_cli_to_srv);
|
|
|
|
free(enckey_srv_to_cli);
|
|
|
|
free(intkey_cli_to_srv);
|
|
|
|
free(intkey_srv_to_cli);
|
|
|
|
}
|
2018-10-28 00:54:56 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|