Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/wp_dh_kmgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,16 @@ static int wp_dh_encode_spki_size(const wp_Dh *dh, size_t* keyLen)
int ret;
word32 len;

/* If we have a generated public key that is not set in the inner key,
* set it now */
if (mp_bitsused(&dh->key.pub) == 0 && dh->pub != NULL && dh->pubSz > 0) {
ret = wc_DhImportKeyPair((DhKey*)&dh->key, NULL, 0,
dh->pub, (word32)dh->pubSz);
if (ret != 0) {
ok = 0;
}
}

ret = wc_DhPubKeyToDer((DhKey*)&dh->key, NULL, &len);
if (ret != LENGTH_ONLY_E) {
ok = 0;
Expand Down Expand Up @@ -2374,6 +2384,16 @@ static int wp_dh_encode_pki_size(const wp_Dh *dh, size_t* keyLen)
int ret;
word32 len;

/* If we have a generated private key that is not set in the inner key,
* set it now */
if (mp_bitsused(&dh->key.priv) == 0 && dh->priv != NULL && dh->privSz > 0) {
ret = wc_DhImportKeyPair((DhKey*)&dh->key, dh->priv, (word32)dh->privSz,
dh->pub, (word32)dh->pubSz);
if (ret != 0) {
ok = 0;
}
}

ret = wc_DhPrivKeyToDer((DhKey*)&dh->key, NULL, &len);
if (ret != LENGTH_ONLY_E) {
ok = 0;
Expand Down Expand Up @@ -2639,6 +2659,7 @@ static int wp_dh_encode(wp_DhEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
(void)pwCbArg;
#endif

BIO_free(out);
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
Expand Down
10 changes: 10 additions & 0 deletions src/wp_wolfprov.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,16 @@ static const OSSL_ALGORITHM wolfprov_decoder[] = {
{ WP_NAMES_DH, WP_DECODER_PROPERTIES(type-specific),
wp_dh_type_specific_decoder_functions,
"" },
/* Add the same decoders for name "DHX" */
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(SubjectPublicKeyInfo),
wp_dh_spki_decoder_functions,
"" },
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(PrivateKeyInfo),
wp_dh_pki_decoder_functions,
"" },
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(type-specific),
wp_dh_type_specific_decoder_functions,
"" },
#endif

#ifdef WP_HAVE_ECC
Expand Down
170 changes: 170 additions & 0 deletions test/test_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "unit.h"
#include <openssl/core_names.h>
#include <openssl/decoder.h>

#ifdef WP_HAVE_DH

Expand Down Expand Up @@ -96,6 +97,76 @@ static const unsigned char dh_g[] =
0x02,
};

/* Fixed param from krb5 DH generation, named "o2048" */
static const uint8_t dh_2048[] = {
0x30, 0x82, 0x02, 0x0C, 0x02, 0x82, 0x01, 0x01,
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2,
0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C,
0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC,
0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B,
0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04,
0xDD, 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43,
0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14,
0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2,
0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E,
0xC6, 0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED,
0x6B, 0x0B, 0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CF, 0x3B, 0x96, 0x0C, 0x07, 0x43, 0x01,
0xCD, 0x93, 0xC1, 0xD1, 0x76, 0x03, 0xD1, 0x47,
0xDA, 0xE2, 0xAE, 0xF8, 0x37, 0xA6, 0x29, 0x64,
0xEF, 0x15, 0xE5, 0xFB, 0x4A, 0xAC, 0x0B, 0x8C,
0x1C, 0xCA, 0xA4, 0xBE, 0x75, 0x4A, 0xB5, 0x72,
0x8A, 0xE9, 0x13, 0x0C, 0x4C, 0x7D, 0x02, 0x88,
0x0A, 0xB9, 0x47, 0x2D, 0x45, 0x56, 0x55, 0x34,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

static int test_dh_pkey_keygen(EVP_PKEY *params)
{
int err;
Expand Down Expand Up @@ -472,5 +543,104 @@ int test_dh_get_params(void *data)
return err;
}

static int test_dh_krb5_keygen_ex(OSSL_LIB_CTX *libCtx)
{
int err = 0;
EVP_PKEY *params = NULL;
EVP_PKEY *key = NULL;
EVP_PKEY_CTX *ctx = NULL;
OSSL_DECODER_CTX *dctx = NULL;
const unsigned char *inptr = dh_2048;
size_t inlen = sizeof(dh_2048);
unsigned char *spki = NULL;
size_t spki_len = 0;
unsigned char *der = NULL;
size_t der_len;

PRINT_MSG("Testing DH key generation with krb5 parameters");

/* Create decoder context for DH parameters */
dctx = OSSL_DECODER_CTX_new_for_pkey(&params, "DER", "type-specific", "DHX",
EVP_PKEY_KEY_PARAMETERS, libCtx, NULL);
err = dctx == NULL;
if (err == 0) {
/* Decode the parameters */
err = OSSL_DECODER_from_data(dctx, &inptr, &inlen) != 1;
}

if (err == 0) {
/* Create key generation context */
ctx = EVP_PKEY_CTX_new_from_pkey(libCtx, params, NULL);
err = ctx == NULL;
}
if (err == 0) {
err = EVP_PKEY_keygen_init(ctx) != 1;
}
if (err == 0) {
/* Generate key pair */
err = EVP_PKEY_keygen(ctx, &key) != 1;
}

if (err == 0) {
/* Get the size of the encoded public key */
err = i2d_PUBKEY(key, NULL) <= 0;
}
if (err == 0) {
der_len = i2d_PUBKEY(key, NULL);
der = OPENSSL_malloc(der_len);
err = der == NULL;
}
if (err == 0) {
unsigned char *p = der;
err = i2d_PUBKEY(key, &p) <= 0;
}
if (err == 0) {
spki = der;
der = NULL;
spki_len = der_len;
}
/* We were previously producing an empty subject public key info which was
* too short. We are still producing a dhKeyAgreement SPKI instead of a
* PKCS3 key, which should be fine for now but means we can't directly
* compare outputs with openssl. For now lets just make sure the SPKI
* encoding length is reasonable, about 260 for an empty SPKI */
if (err == 0 && spki_len < 300) {
PRINT_MSG("SPKI is too short");
err = 1;
}

if (der) {
OPENSSL_free(spki);
}
if (spki) {
OPENSSL_free(spki);
}
EVP_PKEY_free(key);
EVP_PKEY_free(params);
EVP_PKEY_CTX_free(ctx);
OSSL_DECODER_CTX_free(dctx);

return err;
}

/**
* Test DH key generation using the krb5 test parameters.
*
* @param [in] data Unused.
* @return 1 on success.
* @return 0 on failure.
*/
int test_dh_krb5_keygen(void *data)
{
int err = 0;

(void)data;

err = test_dh_krb5_keygen_ex(osslLibCtx);
if (err == 0) {
err = test_dh_krb5_keygen_ex(wpLibCtx);
}
return err;
}

#endif /* WP_HAVE_DH */
1 change: 1 addition & 0 deletions test/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ TEST_CASE test_case[] = {
TEST_DECL(test_dh_pgen_pkey, NULL),
TEST_DECL(test_dh_pkey, NULL),
TEST_DECL(test_dh_decode, NULL),
TEST_DECL(test_dh_krb5_keygen, NULL),
#ifndef WOLFPROV_QUICKTEST
TEST_DECL(test_dh_get_params, NULL),
#endif
Expand Down
1 change: 1 addition & 0 deletions test/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ int test_dh_pgen_pkey(void *data);
int test_dh_pkey(void *data);
int test_dh_decode(void *data);
int test_dh_get_params(void *data);
int test_dh_krb5_keygen(void *data);
#endif /* WP_HAVE_DH */

#ifdef WP_HAVE_ECC
Expand Down
Loading