Skip to content

Commit 43cf24a

Browse files
authored
Merge pull request #187 from ColtonWilley/wp_dh_decode_fixes
Fix DH public key decoding, add new DH decoder registrations
2 parents 7e48411 + b74d66b commit 43cf24a

5 files changed

Lines changed: 203 additions & 0 deletions

File tree

src/wp_dh_kmgmt.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,16 @@ static int wp_dh_encode_spki_size(const wp_Dh *dh, size_t* keyLen)
23152315
int ret;
23162316
word32 len;
23172317

2318+
/* If we have a generated public key that is not set in the inner key,
2319+
* set it now */
2320+
if (mp_bitsused(&dh->key.pub) == 0 && dh->pub != NULL && dh->pubSz > 0) {
2321+
ret = wc_DhImportKeyPair((DhKey*)&dh->key, NULL, 0,
2322+
dh->pub, (word32)dh->pubSz);
2323+
if (ret != 0) {
2324+
ok = 0;
2325+
}
2326+
}
2327+
23182328
ret = wc_DhPubKeyToDer((DhKey*)&dh->key, NULL, &len);
23192329
if (ret != LENGTH_ONLY_E) {
23202330
ok = 0;
@@ -2374,6 +2384,16 @@ static int wp_dh_encode_pki_size(const wp_Dh *dh, size_t* keyLen)
23742384
int ret;
23752385
word32 len;
23762386

2387+
/* If we have a generated private key that is not set in the inner key,
2388+
* set it now */
2389+
if (mp_bitsused(&dh->key.priv) == 0 && dh->priv != NULL && dh->privSz > 0) {
2390+
ret = wc_DhImportKeyPair((DhKey*)&dh->key, dh->priv, (word32)dh->privSz,
2391+
dh->pub, (word32)dh->pubSz);
2392+
if (ret != 0) {
2393+
ok = 0;
2394+
}
2395+
}
2396+
23772397
ret = wc_DhPrivKeyToDer((DhKey*)&dh->key, NULL, &len);
23782398
if (ret != LENGTH_ONLY_E) {
23792399
ok = 0;
@@ -2639,6 +2659,7 @@ static int wp_dh_encode(wp_DhEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
26392659
(void)pwCbArg;
26402660
#endif
26412661

2662+
BIO_free(out);
26422663
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
26432664
return ok;
26442665
}

src/wp_wolfprov.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,16 @@ static const OSSL_ALGORITHM wolfprov_decoder[] = {
977977
{ WP_NAMES_DH, WP_DECODER_PROPERTIES(type-specific),
978978
wp_dh_type_specific_decoder_functions,
979979
"" },
980+
/* Add the same decoders for name "DHX" */
981+
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(SubjectPublicKeyInfo),
982+
wp_dh_spki_decoder_functions,
983+
"" },
984+
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(PrivateKeyInfo),
985+
wp_dh_pki_decoder_functions,
986+
"" },
987+
{ WP_NAMES_DHX, WP_DECODER_PROPERTIES(type-specific),
988+
wp_dh_type_specific_decoder_functions,
989+
"" },
980990
#endif
981991

982992
#ifdef WP_HAVE_ECC

test/test_dh.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "unit.h"
2222
#include <openssl/core_names.h>
23+
#include <openssl/decoder.h>
2324

2425
#ifdef WP_HAVE_DH
2526

@@ -96,6 +97,76 @@ static const unsigned char dh_g[] =
9697
0x02,
9798
};
9899

100+
/* Fixed param from krb5 DH generation, named "o2048" */
101+
static const uint8_t dh_2048[] = {
102+
0x30, 0x82, 0x02, 0x0C, 0x02, 0x82, 0x01, 0x01,
103+
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
104+
0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2,
105+
0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C,
106+
0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC,
107+
0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B,
108+
0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04,
109+
0xDD, 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43,
110+
0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14,
111+
0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2,
112+
0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E,
113+
0xC6, 0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED,
114+
0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7,
115+
0xED, 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F,
116+
0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F,
117+
0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B,
118+
0x3D, 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF,
119+
0x05, 0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3,
120+
0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF,
121+
0x5F, 0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD,
122+
0x96, 0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52,
123+
0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96,
124+
0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98,
125+
0x04, 0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21,
126+
0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE,
127+
0x3B, 0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86,
128+
0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2,
129+
0x8F, 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52,
130+
0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17,
131+
0x18, 0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A,
132+
0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05,
133+
0x10, 0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA,
134+
0x68, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
135+
0xFF, 0x02, 0x01, 0x02, 0x02, 0x82, 0x01, 0x00,
136+
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
137+
0xE4, 0x87, 0xED, 0x51, 0x10, 0xB4, 0x61, 0x1A,
138+
0x62, 0x63, 0x31, 0x45, 0xC0, 0x6E, 0x0E, 0x68,
139+
0x94, 0x81, 0x27, 0x04, 0x45, 0x33, 0xE6, 0x3A,
140+
0x01, 0x05, 0xDF, 0x53, 0x1D, 0x89, 0xCD, 0x91,
141+
0x28, 0xA5, 0x04, 0x3C, 0xC7, 0x1A, 0x02, 0x6E,
142+
0xF7, 0xCA, 0x8C, 0xD9, 0xE6, 0x9D, 0x21, 0x8D,
143+
0x98, 0x15, 0x85, 0x36, 0xF9, 0x2F, 0x8A, 0x1B,
144+
0xA7, 0xF0, 0x9A, 0xB6, 0xB6, 0xA8, 0xE1, 0x22,
145+
0xF2, 0x42, 0xDA, 0xBB, 0x31, 0x2F, 0x3F, 0x63,
146+
0x7A, 0x26, 0x21, 0x74, 0xD3, 0x1B, 0xF6, 0xB5,
147+
0x85, 0xFF, 0xAE, 0x5B, 0x7A, 0x03, 0x5B, 0xF6,
148+
0xF7, 0x1C, 0x35, 0xFD, 0xAD, 0x44, 0xCF, 0xD2,
149+
0xD7, 0x4F, 0x92, 0x08, 0xBE, 0x25, 0x8F, 0xF3,
150+
0x24, 0x94, 0x33, 0x28, 0xF6, 0x72, 0x2D, 0x9E,
151+
0xE1, 0x00, 0x3E, 0x5C, 0x50, 0xB1, 0xDF, 0x82,
152+
0xCC, 0x6D, 0x24, 0x1B, 0x0E, 0x2A, 0xE9, 0xCD,
153+
0x34, 0x8B, 0x1F, 0xD4, 0x7E, 0x92, 0x67, 0xAF,
154+
0xC1, 0xB2, 0xAE, 0x91, 0xEE, 0x51, 0xD6, 0xCB,
155+
0x0E, 0x31, 0x79, 0xAB, 0x10, 0x42, 0xA9, 0x5D,
156+
0xCF, 0x6A, 0x94, 0x83, 0xB8, 0x4B, 0x4B, 0x36,
157+
0xB3, 0x86, 0x1A, 0xA7, 0x25, 0x5E, 0x4C, 0x02,
158+
0x78, 0xBA, 0x36, 0x04, 0x65, 0x0C, 0x10, 0xBE,
159+
0x19, 0x48, 0x2F, 0x23, 0x17, 0x1B, 0x67, 0x1D,
160+
0xF1, 0xCF, 0x3B, 0x96, 0x0C, 0x07, 0x43, 0x01,
161+
0xCD, 0x93, 0xC1, 0xD1, 0x76, 0x03, 0xD1, 0x47,
162+
0xDA, 0xE2, 0xAE, 0xF8, 0x37, 0xA6, 0x29, 0x64,
163+
0xEF, 0x15, 0xE5, 0xFB, 0x4A, 0xAC, 0x0B, 0x8C,
164+
0x1C, 0xCA, 0xA4, 0xBE, 0x75, 0x4A, 0xB5, 0x72,
165+
0x8A, 0xE9, 0x13, 0x0C, 0x4C, 0x7D, 0x02, 0x88,
166+
0x0A, 0xB9, 0x47, 0x2D, 0x45, 0x56, 0x55, 0x34,
167+
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
168+
};
169+
99170
static int test_dh_pkey_keygen(EVP_PKEY *params)
100171
{
101172
int err;
@@ -472,5 +543,104 @@ int test_dh_get_params(void *data)
472543
return err;
473544
}
474545

546+
static int test_dh_krb5_keygen_ex(OSSL_LIB_CTX *libCtx)
547+
{
548+
int err = 0;
549+
EVP_PKEY *params = NULL;
550+
EVP_PKEY *key = NULL;
551+
EVP_PKEY_CTX *ctx = NULL;
552+
OSSL_DECODER_CTX *dctx = NULL;
553+
const unsigned char *inptr = dh_2048;
554+
size_t inlen = sizeof(dh_2048);
555+
unsigned char *spki = NULL;
556+
size_t spki_len = 0;
557+
unsigned char *der = NULL;
558+
size_t der_len;
559+
560+
PRINT_MSG("Testing DH key generation with krb5 parameters");
561+
562+
/* Create decoder context for DH parameters */
563+
dctx = OSSL_DECODER_CTX_new_for_pkey(&params, "DER", "type-specific", "DHX",
564+
EVP_PKEY_KEY_PARAMETERS, libCtx, NULL);
565+
err = dctx == NULL;
566+
if (err == 0) {
567+
/* Decode the parameters */
568+
err = OSSL_DECODER_from_data(dctx, &inptr, &inlen) != 1;
569+
}
570+
571+
if (err == 0) {
572+
/* Create key generation context */
573+
ctx = EVP_PKEY_CTX_new_from_pkey(libCtx, params, NULL);
574+
err = ctx == NULL;
575+
}
576+
if (err == 0) {
577+
err = EVP_PKEY_keygen_init(ctx) != 1;
578+
}
579+
if (err == 0) {
580+
/* Generate key pair */
581+
err = EVP_PKEY_keygen(ctx, &key) != 1;
582+
}
583+
584+
if (err == 0) {
585+
/* Get the size of the encoded public key */
586+
err = i2d_PUBKEY(key, NULL) <= 0;
587+
}
588+
if (err == 0) {
589+
der_len = i2d_PUBKEY(key, NULL);
590+
der = OPENSSL_malloc(der_len);
591+
err = der == NULL;
592+
}
593+
if (err == 0) {
594+
unsigned char *p = der;
595+
err = i2d_PUBKEY(key, &p) <= 0;
596+
}
597+
if (err == 0) {
598+
spki = der;
599+
der = NULL;
600+
spki_len = der_len;
601+
}
602+
/* We were previously producing an empty subject public key info which was
603+
* too short. We are still producing a dhKeyAgreement SPKI instead of a
604+
* PKCS3 key, which should be fine for now but means we can't directly
605+
* compare outputs with openssl. For now lets just make sure the SPKI
606+
* encoding length is reasonable, about 260 for an empty SPKI */
607+
if (err == 0 && spki_len < 300) {
608+
PRINT_MSG("SPKI is too short");
609+
err = 1;
610+
}
611+
612+
if (der) {
613+
OPENSSL_free(spki);
614+
}
615+
if (spki) {
616+
OPENSSL_free(spki);
617+
}
618+
EVP_PKEY_free(key);
619+
EVP_PKEY_free(params);
620+
EVP_PKEY_CTX_free(ctx);
621+
OSSL_DECODER_CTX_free(dctx);
622+
623+
return err;
624+
}
625+
626+
/**
627+
* Test DH key generation using the krb5 test parameters.
628+
*
629+
* @param [in] data Unused.
630+
* @return 1 on success.
631+
* @return 0 on failure.
632+
*/
633+
int test_dh_krb5_keygen(void *data)
634+
{
635+
int err = 0;
636+
637+
(void)data;
638+
639+
err = test_dh_krb5_keygen_ex(osslLibCtx);
640+
if (err == 0) {
641+
err = test_dh_krb5_keygen_ex(wpLibCtx);
642+
}
643+
return err;
644+
}
475645

476646
#endif /* WP_HAVE_DH */

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ TEST_CASE test_case[] = {
152152
TEST_DECL(test_dh_pgen_pkey, NULL),
153153
TEST_DECL(test_dh_pkey, NULL),
154154
TEST_DECL(test_dh_decode, NULL),
155+
TEST_DECL(test_dh_krb5_keygen, NULL),
155156
#ifndef WOLFPROV_QUICKTEST
156157
TEST_DECL(test_dh_get_params, NULL),
157158
#endif

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ int test_dh_pgen_pkey(void *data);
256256
int test_dh_pkey(void *data);
257257
int test_dh_decode(void *data);
258258
int test_dh_get_params(void *data);
259+
int test_dh_krb5_keygen(void *data);
259260
#endif /* WP_HAVE_DH */
260261

261262
#ifdef WP_HAVE_ECC

0 commit comments

Comments
 (0)