Skip to content

Commit 89dac98

Browse files
committed
fix d2iTryAltDhKey short-circuiting the d2i probe chain
When wc_DhKeyDecode() rejected the input, d2iTryAltDhKey() returned 0 after freeing the DH object. d2i_evp_pkey_try() treats any value >= 0 as success, so a non-DH input would stop the probe chain at the DH step and never reach the Falcon, Dilithium, Ed25519, or Ed448 probes that follow. d2i_PUBKEY()/d2i_PrivateKey() consequently returned NULL for any key type past DH in the chain.
1 parent 97f1e44 commit 89dac98

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

wolfcrypt/src/evp_pk.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,23 @@ static int d2iTryAltDhKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
404404
key = (DhKey*)dhObj->internal;
405405
/* Try decoding data as a DH public key. */
406406
if (wc_DhKeyDecode(mem, &keyIdx, key, (word32)memSz) != 0) {
407-
ret = 0;
407+
wolfSSL_DH_free(dhObj);
408+
return WOLFSSL_FATAL_ERROR;
408409
}
409-
if (ret == 1) {
410-
/* DH key has data and is external to DH object. */
411-
elements = ELEMENT_P | ELEMENT_G | ELEMENT_Q | ELEMENT_PUB;
412-
if (priv) {
413-
elements |= ELEMENT_PRV;
414-
}
415-
if (SetDhExternal_ex(dhObj, elements) != WOLFSSL_SUCCESS ) {
416-
ret = 0;
417-
}
410+
/* DH key has data and is external to DH object. */
411+
elements = ELEMENT_P | ELEMENT_G | ELEMENT_Q | ELEMENT_PUB;
412+
if (priv) {
413+
elements |= ELEMENT_PRV;
414+
}
415+
if (SetDhExternal_ex(dhObj, elements) != WOLFSSL_SUCCESS ) {
416+
ret = 0;
418417
}
419418
if (ret == 1) {
420419
/* Create an EVP PKEY object. */
421420
ret = d2i_make_pkey(out, mem, keyIdx, priv, WC_EVP_PKEY_DH);
422421
}
423422
if (ret == 1) {
424-
/* Put RSA key object into EVP PKEY object. */
423+
/* Put DH key object into EVP PKEY object. */
425424
(*out)->ownDh = 1;
426425
(*out)->dh = dhObj;
427426
}

0 commit comments

Comments
 (0)