From 20643226b64c1773ac56124e0e06d445bc064097 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 14 Feb 2025 18:10:05 +0000 Subject: [PATCH] Use DER-specified hash algorithm for PEM decryption The PemToDer function was using a hardcoded MD5 hash for key derivation. This change extracts the hash algorithm from the PBES2 parameters in the DER buffer, returning an error if the hash algorithm cannot be determined. Co-Authored-By: lealem@wolfssl.com --- wolfcrypt/src/asn.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index daff303cc..0be4c2069 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -25785,8 +25785,27 @@ int PemToDer(const unsigned char* buff, long longSz, int type, int padVal = 0; #endif - ret = wc_BufferKeyDecrypt(info, der->buffer, der->length, - (byte*)password, passwordSz, WC_MD5); + /* Extract hash type from PBES2 parameters */ + word32 idx = 0; + int hashType; + DECL_ASNGETDATA(dataASN, pbes2ParamsASN_Length); + CALLOC_ASNGETDATA(dataASN, pbes2ParamsASN_Length, ret, NULL); + if (ret == 0) { + GetASN_OID(&dataASN[PBES2PARAMSASN_IDX_PBKDF2_PARAMS_PRF_OID], oidHmacType); + ret = GetASN_Items(pbes2ParamsASN, dataASN, pbes2ParamsASN_Length, + 0, der->buffer, &idx, der->length); + if (ret == 0) { + hashType = wc_OidGetHash(dataASN[PBES2PARAMSASN_IDX_PBKDF2_PARAMS_PRF_OID].data.oid.sum); + if (hashType == WC_HASH_TYPE_NONE) { + WOLFSSL_MSG("Hash algorithm not supported"); + ret = ASN_PARSE_E; + } + } + } + FREE_ASNGETDATA(dataASN, NULL); + if (ret == 0) + ret = wc_BufferKeyDecrypt(info, der->buffer, der->length, + (byte*)password, passwordSz, hashType); #ifndef NO_WOLFSSL_SKIP_TRAILING_PAD #ifndef NO_DES3