Skip to content

Commit

Permalink
Make DH/ECDH KEX buildable on RHEL8
Browse files Browse the repository at this point in the history
  • Loading branch information
beldmit committed Oct 10, 2023
1 parent be272a3 commit 4cb1449
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 61 deletions.
29 changes: 29 additions & 0 deletions dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
#include <openssl/bn.h>
#include <openssl/dh.h>
#include <openssl/evp.h>
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
#include <openssl/core_names.h>
#include <openssl/param_build.h>
#endif

#include "dh.h"
#include "pathnames.h"
Expand Down Expand Up @@ -286,6 +288,7 @@ dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
int
dh_gen_key(DH *dh, int need)
{
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
const BIGNUM *dh_p, *dh_g;
BIGNUM *pub_key = NULL, *priv_key = NULL;
EVP_PKEY *pkey = NULL;
Expand Down Expand Up @@ -381,6 +384,32 @@ dh_gen_key(DH *dh, int need)
BN_clear_free(pub_key);
BN_clear_free(priv_key);
return r;
#else
int pbits;
const BIGNUM *dh_p, *pub_key;

DH_get0_pqg(dh, &dh_p, NULL, NULL);

if (need < 0 || dh_p == NULL ||
(pbits = BN_num_bits(dh_p)) <= 0 ||
need > INT_MAX / 2 || 2 * need > pbits)
return SSH_ERR_INVALID_ARGUMENT;
if (need < 256)
need = 256;
/*
* Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
* so double requested need here.
*/
if (!DH_set_length(dh, MINIMUM(need * 2, pbits - 1)))
return SSH_ERR_LIBCRYPTO_ERROR;

if (DH_generate_key(dh) == 0)
return SSH_ERR_LIBCRYPTO_ERROR;
DH_get0_key(dh, &pub_key, NULL);
if (!dh_pub_is_valid(dh, pub_key))
return SSH_ERR_INVALID_FORMAT;
return 0;
#endif
}

DH *
Expand Down
44 changes: 0 additions & 44 deletions kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1494,47 +1494,3 @@ kex_exchange_identification(struct ssh *ssh, int timeout_ms,
return r;
}

#ifdef WITH_OPENSSL
/*
* Creates an EVP_PKEY from the given parameters and keys.
* The private key can be omitted.
*/
int
kex_create_evp_dh(EVP_PKEY **pkey, const BIGNUM *p, const BIGNUM *q,
const BIGNUM *g, const BIGNUM *pub, const BIGNUM *priv)
{
OSSL_PARAM_BLD *param_bld = NULL;
EVP_PKEY_CTX *ctx = NULL;
int r = 0;

/* create EVP_PKEY-DH key */
if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)) == NULL ||
(param_bld = OSSL_PARAM_BLD_new()) == NULL) {
error_f("EVP_PKEY_CTX or PARAM_BLD init failed");
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_P, p) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_Q, q) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_G, g) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld,
OSSL_PKEY_PARAM_PUB_KEY, pub) != 1) {
error_f("Failed pushing params to OSSL_PARAM_BLD");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if (priv != NULL &&
OSSL_PARAM_BLD_push_BN(param_bld,
OSSL_PKEY_PARAM_PRIV_KEY, priv) != 1) {
error_f("Failed pushing private key to OSSL_PARAM_BLD");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL)
r = SSH_ERR_LIBCRYPTO_ERROR;
out:
OSSL_PARAM_BLD_free(param_bld);
EVP_PKEY_CTX_free(ctx);
return r;
}
#endif /* WITH_OPENSSL */
6 changes: 3 additions & 3 deletions kex.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
# include <openssl/dh.h>
# include <openssl/ecdsa.h>
# include <openssl/evp.h>
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
# include <openssl/core_names.h>
# include <openssl/param_build.h>
#endif
#else /* WITH_OPENSSL */
# define DH void
# define BIGNUM void
Expand Down Expand Up @@ -165,7 +167,7 @@ struct kex {
/* kex specific state */
DH *dh; /* DH */
u_int min, max, nbits; /* GEX */
EVP_PKEY *pkey;
EVP_PKEY *pkey; /* ECDH */
u_char c25519_client_key[CURVE25519_SIZE]; /* 25519 + KEM */
u_char c25519_client_pubkey[CURVE25519_SIZE]; /* 25519 */
u_char sntrup761_client_key[crypto_kem_sntrup761_SECRETKEYBYTES]; /* KEM */
Expand Down Expand Up @@ -249,8 +251,6 @@ int kexc25519_shared_key_ext(const u_char key[CURVE25519_SIZE],
const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int)
__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
int kex_create_evp_dh(EVP_PKEY **, const BIGNUM *, const BIGNUM *,
const BIGNUM *, const BIGNUM *, const BIGNUM *);

#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
void dump_digest(const char *, const u_char *, int);
Expand Down
49 changes: 48 additions & 1 deletion kexdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
#include <openssl/dh.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
#include <openssl/core_names.h>
#include <openssl/param_build.h>
#endif

#include "sshkey.h"
#include "kex.h"
Expand Down Expand Up @@ -117,7 +119,7 @@ kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
goto out;
}

if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL) {
if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) {
error_f("Could not init EVP_PKEY_CTX for dh");
r = SSH_ERR_ALLOC_FAIL;
goto out;
Expand Down Expand Up @@ -242,4 +244,49 @@ kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
sshbuf_free(buf);
return r;
}
/*
* Creates an EVP_PKEY from the given parameters and keys.
* The private key can be omitted.
*/
int
kex_create_evp_dh(EVP_PKEY **pkey, const BIGNUM *p, const BIGNUM *q,
const BIGNUM *g, const BIGNUM *pub, const BIGNUM *priv)
{
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PARAM_BLD *param_bld = NULL;
EVP_PKEY_CTX *ctx = NULL;
int r = 0;

/* create EVP_PKEY-DH key */
if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)) == NULL ||
(param_bld = OSSL_PARAM_BLD_new()) == NULL) {
error_f("EVP_PKEY_CTX or PARAM_BLD init failed");
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_P, p) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_Q, q) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_G, g) != 1 ||
OSSL_PARAM_BLD_push_BN(param_bld,
OSSL_PKEY_PARAM_PUB_KEY, pub) != 1) {
error_f("Failed pushing params to OSSL_PARAM_BLD");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if (priv != NULL &&
OSSL_PARAM_BLD_push_BN(param_bld,
OSSL_PKEY_PARAM_PRIV_KEY, priv) != 1) {
error_f("Failed pushing private key to OSSL_PARAM_BLD");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL)
r = SSH_ERR_LIBCRYPTO_ERROR;
out:
OSSL_PARAM_BLD_free(param_bld);
EVP_PKEY_CTX_free(ctx);
return r;
#else
#endif
}
#endif /* WITH_OPENSSL */
70 changes: 57 additions & 13 deletions kexecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#include <signal.h>

#include <openssl/evp.h>
#include <openssl/err.h>
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
#include <openssl/core_names.h>
#include <openssl/param_build.h>
#include <openssl/err.h>
#endif

#include "sshkey.h"
#include "kex.h"
Expand All @@ -55,6 +57,7 @@ generate_ec_keys(int ec_nid)
{
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PARAM_BLD *param_bld = NULL;
OSSL_PARAM *params = NULL;
const char *group_name;
Expand All @@ -75,10 +78,21 @@ generate_ec_keys(int ec_nid)
error_f("Could not generate ec keys");
goto out;
}
#else
if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
EVP_PKEY_keygen_init(ctx) != 1 ||
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, ec_nid) <= 0 ||
EVP_PKEY_keygen(ctx, &pkey) != 1) {
error_f("Could not generate ec keys");
goto out;
}
#endif
out:
EVP_PKEY_CTX_free(ctx);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PARAM_BLD_free(param_bld);
OSSL_PARAM_free(params);
#endif
return pkey;
}

Expand Down Expand Up @@ -160,12 +174,16 @@ kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
struct sshbuf *buf = NULL;
BIGNUM *shared_secret = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *dh_pkey = NULL;
EVP_PKEY *peer_key = NULL;
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PARAM_BLD *param_bld = NULL;
OSSL_PARAM *params = NULL;
const char *group_name;
#else
EC_KEY *ec = NULL;
#endif
u_char *kbuf = NULL, *pub = NULL;
size_t klen = 0, publen;
const char *group_name;
int r;

*shared_secretp = NULL;
Expand All @@ -177,45 +195,67 @@ kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
goto out;

#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
if ((r = sshbuf_get_ec(buf, &pub, &publen)) != 0)
goto out;
sshbuf_reset(buf);
if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL ||
(param_bld = OSSL_PARAM_BLD_new()) == NULL) {
if ((group_name = OSSL_EC_curve_nid2name(kex->ec_nid)) == NULL) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((group_name = OSSL_EC_curve_nid2name(kex->ec_nid)) == NULL) {
r = SSH_ERR_LIBCRYPTO_ERROR;
if ((param_bld = OSSL_PARAM_BLD_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (OSSL_PARAM_BLD_push_octet_string(param_bld,
OSSL_PKEY_PARAM_PUB_KEY, pub, publen) != 1 ||
OSSL_PARAM_BLD_push_utf8_string(param_bld,
OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0) != 1 ||
(params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
error_f("Failed to set params for dh_pkey");
error_f("Failed to set params for peer_key");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if (EVP_PKEY_fromdata_init(ctx) != 1 ||
EVP_PKEY_fromdata(ctx, &dh_pkey,
EVP_PKEY_fromdata(ctx, &peer_key,
EVP_PKEY_PUBLIC_KEY, params) != 1 ||
EVP_PKEY_public_check(ctx) != 1) {
error_f("Peer public key import failed");
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
#else
if ((ec = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshbuf_get_eckey(ec_blob, ec)) != 0)
goto out;

if ((peer_key = EVP_PKEY_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}

if (EVP_PKEY_set1_EC_KEY(peer_key, ec) != 1) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
#endif

#ifdef DEBUG_KEXECDH
fputs("public key:\n", stderr);
EVP_PKEY_print_public_fp(stderr, dh_pkey, 0, NULL);
EVP_PKEY_print_public_fp(stderr, peer_key, 0, NULL);
#endif
EVP_PKEY_CTX_free(ctx);
ctx = NULL;
if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL ||
if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL ||
EVP_PKEY_derive_init(ctx) != 1 ||
EVP_PKEY_derive_set_peer(ctx, dh_pkey) != 1 ||
EVP_PKEY_derive_set_peer(ctx, peer_key) != 1 ||
EVP_PKEY_derive(ctx, NULL, &klen) != 1) {
error_f("Failed to get derive information");
r = SSH_ERR_LIBCRYPTO_ERROR;
Expand Down Expand Up @@ -243,9 +283,13 @@ kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
buf = NULL;
out:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(dh_pkey);
EVP_PKEY_free(peer_key);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PARAM_BLD_free(param_bld);
OSSL_PARAM_free(params);
#else
EC_KEY_free(ec);
#endif
BN_clear_free(shared_secret);
freezero(kbuf, klen);
freezero(pub, publen);
Expand Down

0 comments on commit 4cb1449

Please sign in to comment.