2019-01-21 10:40:11 +00:00
|
|
|
/* $OpenBSD: kexecdh.c,v 1.10 2019/01/21 10:40:11 djm Exp $ */
|
2010-08-31 12:41:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
2019-01-21 10:29:56 +00:00
|
|
|
* Copyright (c) 2019 Markus Friedl. All rights reserved.
|
2010-08-31 12:41:14 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2010-08-31 12:50:48 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2015-01-14 15:21:31 +00:00
|
|
|
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
|
2010-09-10 01:39:26 +00:00
|
|
|
|
2010-08-31 12:41:14 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-01-21 10:29:56 +00:00
|
|
|
#include <stdio.h>
|
2010-08-31 12:41:14 +00:00
|
|
|
#include <string.h>
|
2019-01-21 10:29:56 +00:00
|
|
|
#include <signal.h>
|
2010-08-31 12:41:14 +00:00
|
|
|
|
|
|
|
#include <openssl/ecdh.h>
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshkey.h"
|
2010-08-31 12:41:14 +00:00
|
|
|
#include "kex.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshbuf.h"
|
2014-01-09 23:58:53 +00:00
|
|
|
#include "digest.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "ssherr.h"
|
2010-08-31 12:41:14 +00:00
|
|
|
|
2019-01-21 10:29:56 +00:00
|
|
|
static int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key,
|
2019-01-21 10:29:56 +00:00
|
|
|
const EC_GROUP *, struct sshbuf **);
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
2019-01-21 10:29:56 +00:00
|
|
|
kex_ecdh_keypair(struct kex *kex)
|
|
|
|
{
|
|
|
|
EC_KEY *client_key = NULL;
|
|
|
|
const EC_GROUP *group;
|
|
|
|
const EC_POINT *public_key;
|
|
|
|
struct sshbuf *buf = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EC_KEY_generate_key(client_key) != 1) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
group = EC_KEY_get0_group(client_key);
|
|
|
|
public_key = EC_KEY_get0_public_key(client_key);
|
|
|
|
|
|
|
|
if ((buf = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(buf, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
fputs("client private key:\n", stderr);
|
|
|
|
sshkey_dump_ec_key(client_key);
|
|
|
|
#endif
|
|
|
|
kex->ec_client_key = client_key;
|
|
|
|
kex->ec_group = group;
|
|
|
|
client_key = NULL; /* owned by the kex */
|
2019-01-21 10:40:11 +00:00
|
|
|
kex->client_pub = buf;
|
2019-01-21 10:29:56 +00:00
|
|
|
buf = NULL;
|
|
|
|
out:
|
|
|
|
EC_KEY_free(client_key);
|
|
|
|
sshbuf_free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
|
2019-01-21 10:29:56 +00:00
|
|
|
struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
const EC_GROUP *group;
|
|
|
|
const EC_POINT *pub_key;
|
|
|
|
EC_KEY *server_key = NULL;
|
|
|
|
struct sshbuf *server_blob = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*server_blobp = NULL;
|
|
|
|
*shared_secretp = NULL;
|
|
|
|
|
|
|
|
if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EC_KEY_generate_key(server_key) != 1) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
group = EC_KEY_get0_group(server_key);
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
fputs("server private key:\n", stderr);
|
|
|
|
sshkey_dump_ec_key(server_key);
|
|
|
|
#endif
|
|
|
|
pub_key = EC_KEY_get0_public_key(server_key);
|
|
|
|
if ((server_blob = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(server_blob, NULL)) != 0)
|
|
|
|
goto out;
|
2019-01-21 10:35:09 +00:00
|
|
|
if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group,
|
2019-01-21 10:29:56 +00:00
|
|
|
shared_secretp)) != 0)
|
|
|
|
goto out;
|
|
|
|
*server_blobp = server_blob;
|
|
|
|
server_blob = NULL;
|
|
|
|
out:
|
|
|
|
EC_KEY_free(server_key);
|
|
|
|
sshbuf_free(server_blob);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
|
2019-01-21 10:29:56 +00:00
|
|
|
EC_KEY *key, const EC_GROUP *group, struct sshbuf **shared_secretp)
|
2010-08-31 12:41:14 +00:00
|
|
|
{
|
2019-01-21 10:29:56 +00:00
|
|
|
struct sshbuf *buf = NULL;
|
|
|
|
BIGNUM *shared_secret = NULL;
|
|
|
|
EC_POINT *dh_pub = NULL;
|
|
|
|
u_char *kbuf = NULL;
|
|
|
|
size_t klen = 0;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
2010-08-31 12:41:14 +00:00
|
|
|
|
2019-01-21 10:29:56 +00:00
|
|
|
*shared_secretp = NULL;
|
|
|
|
|
|
|
|
if ((buf = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-01-21 10:35:09 +00:00
|
|
|
if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
|
2019-01-21 10:29:56 +00:00
|
|
|
goto out;
|
|
|
|
if ((dh_pub = EC_POINT_new(group)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) {
|
|
|
|
goto out;
|
2015-01-19 20:16:15 +00:00
|
|
|
}
|
2019-01-21 10:29:56 +00:00
|
|
|
sshbuf_reset(buf);
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
fputs("public key:\n", stderr);
|
|
|
|
sshkey_dump_ec_point(group, dh_pub);
|
2010-08-31 12:41:14 +00:00
|
|
|
#endif
|
2019-01-21 10:29:56 +00:00
|
|
|
if (sshkey_ec_validate_public(group, dh_pub) != 0) {
|
|
|
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
klen = (EC_GROUP_get_degree(group) + 7) / 8;
|
|
|
|
if ((kbuf = malloc(klen)) == NULL ||
|
|
|
|
(shared_secret = BN_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen ||
|
|
|
|
BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
2015-01-19 20:16:15 +00:00
|
|
|
}
|
2019-01-21 10:29:56 +00:00
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
dump_digest("shared secret", kbuf, klen);
|
2010-08-31 12:41:14 +00:00
|
|
|
#endif
|
2019-01-21 10:29:56 +00:00
|
|
|
if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)
|
|
|
|
goto out;
|
|
|
|
*shared_secretp = buf;
|
|
|
|
buf = NULL;
|
|
|
|
out:
|
|
|
|
EC_POINT_clear_free(dh_pub);
|
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
freezero(kbuf, klen);
|
|
|
|
sshbuf_free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
|
2019-01-21 10:29:56 +00:00
|
|
|
struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2019-01-21 10:35:09 +00:00
|
|
|
r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key,
|
2019-01-21 10:29:56 +00:00
|
|
|
kex->ec_group, shared_secretp);
|
|
|
|
EC_KEY_free(kex->ec_client_key);
|
|
|
|
kex->ec_client_key = NULL;
|
|
|
|
return r;
|
2010-08-31 12:41:14 +00:00
|
|
|
}
|
2019-05-17 00:54:51 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-10-02 04:30:55 +00:00
|
|
|
#include "ssherr.h"
|
|
|
|
|
2019-10-02 00:54:28 +00:00
|
|
|
struct kex;
|
|
|
|
struct sshbuf;
|
|
|
|
struct sshkey;
|
2019-05-17 00:54:51 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
kex_ecdh_keypair(struct kex *kex)
|
|
|
|
{
|
|
|
|
return SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
|
|
|
|
struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
return SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
|
|
|
|
struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
return SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
}
|
2015-01-14 15:21:31 +00:00
|
|
|
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|