Skip to content

Commit 46f3ebb

Browse files
committed
Add missing ForceZero calls in PKCS#7
1 parent 16e1d33 commit 46f3ebb

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

wolfcrypt/src/pkcs7.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ static void wc_PKCS7_ResetStream(wc_PKCS7* pkcs7)
211211
XFREE(pkcs7->stream->tag, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
212212
XFREE(pkcs7->stream->nonce, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
213213
XFREE(pkcs7->stream->buffer, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
214+
/* stream->key is always allocated with MAX_ENCRYPTED_KEY_SZ */
215+
if (pkcs7->stream->key != NULL)
216+
ForceZero(pkcs7->stream->key, MAX_ENCRYPTED_KEY_SZ);
214217
XFREE(pkcs7->stream->key, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
215218
pkcs7->stream->aad = NULL;
216219
pkcs7->stream->tag = NULL;
@@ -7770,6 +7773,7 @@ static int wc_PKCS7_KariGenerateKEK(WC_PKCS7_KARI* kari, WC_RNG* rng,
77707773
}
77717774

77727775
if (ret != 0) {
7776+
ForceZero(secret, secretSz);
77737777
XFREE(secret, kari->heap, DYNAMIC_TYPE_PKCS7);
77747778
return ret;
77757779
}
@@ -9763,6 +9767,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
97639767
(word32)kekKeySz);
97649768
if (ret < 0) {
97659769
XFREE(recip, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
9770+
ForceZero(kek, (word32)kekKeySz);
97669771
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
97679772
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
97689773
return ret;
@@ -9774,6 +9779,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
97749779
tmpIv, (word32)kekBlockSz, encryptOID);
97759780
if (ret < 0) {
97769781
XFREE(recip, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
9782+
ForceZero(kek, (word32)kekKeySz);
97779783
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
97789784
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
97799785
return ret;
@@ -9798,6 +9804,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
97989804
ret = wc_SetContentType(PWRI_KEK_WRAP, keyEncAlgoId, sizeof(keyEncAlgoId));
97999805
if (ret <= 0) {
98009806
XFREE(recip, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
9807+
ForceZero(kek, (word32)kekKeySz);
98019808
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98029809
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98039810
return ret;
@@ -9829,6 +9836,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
98299836
ret = wc_SetContentType(kdfOID, kdfAlgoId, sizeof(kdfAlgoId));
98309837
if (ret <= 0) {
98319838
XFREE(recip, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
9839+
ForceZero(kek, (word32)kekKeySz);
98329840
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98339841
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98349842
return ret;
@@ -9854,6 +9862,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
98549862
if (totalSz > MAX_RECIP_SZ) {
98559863
WOLFSSL_MSG("CMS Recipient output buffer too small");
98569864
XFREE(recip, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
9865+
ForceZero(kek, (word32)kekKeySz);
98579866
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98589867
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98599868
return BUFFER_E;
@@ -9891,7 +9900,7 @@ int wc_PKCS7_AddRecipient_PWRI(wc_PKCS7* pkcs7, byte* passwd, word32 pLen,
98919900
XMEMCPY(recip->recip + idx, encryptedKey, encryptedKeySz);
98929901
idx += encryptedKeySz;
98939902

9894-
ForceZero(kek, (word32)kekBlockSz);
9903+
ForceZero(kek, (word32)kekKeySz);
98959904
ForceZero(encryptedKey, encryptedKeySz);
98969905
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
98979906
XFREE(encryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
@@ -10612,7 +10621,9 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1061210621
word32 pkiMsgSz = inSz;
1061310622
byte tag;
1061410623

10615-
10624+
#ifndef WC_NO_RSA_OAEP
10625+
word32 outKeySz = 0;
10626+
#endif
1061610627
#ifndef NO_PKCS7_STREAM
1061710628
word32 tmpIdx = *idx;
1061810629
#endif
@@ -10921,8 +10932,8 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1092110932
#ifndef WC_NO_RSA_OAEP
1092210933
}
1092310934
else {
10924-
word32 outLen = (word32)wc_RsaEncryptSize(privKey);
10925-
outKey = (byte*)XMALLOC(outLen, pkcs7->heap,
10935+
outKeySz = (word32)wc_RsaEncryptSize(privKey);
10936+
outKey = (byte*)XMALLOC(outKeySz, pkcs7->heap,
1092610937
DYNAMIC_TYPE_TMP_BUFFER);
1092710938
if (!outKey) {
1092810939
WOLFSSL_MSG("Failed to allocate out key buffer");
@@ -10936,9 +10947,9 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1093610947
}
1093710948

1093810949
keySz = wc_RsaPrivateDecrypt_ex(encryptedKey,
10939-
(word32)encryptedKeySz, outKey, outLen, privKey,
10940-
WC_RSA_OAEP_PAD,
10941-
WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
10950+
(word32)encryptedKeySz, outKey, outKeySz,
10951+
privKey, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
10952+
WC_MGF1SHA1, NULL, 0);
1094210953
}
1094310954
#endif
1094410955
}
@@ -10961,6 +10972,7 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1096110972
#ifndef WC_NO_RSA_OAEP
1096210973
if (encOID == RSAESOAEPk) {
1096310974
if (outKey) {
10975+
ForceZero(outKey, outKeySz);
1096410976
XFREE(outKey, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
1096510977
}
1096610978
}
@@ -10977,6 +10989,7 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1097710989
#ifndef WC_NO_RSA_OAEP
1097810990
if (encOID == RSAESOAEPk) {
1097910991
if (outKey) {
10992+
ForceZero(outKey, outKeySz);
1098010993
XFREE(outKey, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
1098110994
}
1098210995
}
@@ -11791,6 +11804,7 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1179111804
iterations, kek, (word32)kekKeySz);
1179211805
if (ret < 0) {
1179311806
XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11807+
ForceZero(kek, (word32)kekKeySz);
1179411808
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1179511809
XFREE(cek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1179611810
return ASN_PARSE_E;
@@ -11803,7 +11817,9 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1180311817
pwriEncAlgoId);
1180411818
if (ret < 0) {
1180511819
XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11820+
ForceZero(kek, (word32)kekKeySz);
1180611821
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11822+
ForceZero(cek, cekSz);
1180711823
XFREE(cek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1180811824
return ret;
1180911825
}
@@ -11812,7 +11828,9 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1181211828
if (*decryptedKeySz < cekSz) {
1181311829
WOLFSSL_MSG("Decrypted key buffer too small for CEK");
1181411830
XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11831+
ForceZero(kek, (word32)kekKeySz);
1181511832
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11833+
ForceZero(cek, cekSz);
1181611834
XFREE(cek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1181711835
return BUFFER_E;
1181811836
}
@@ -11821,7 +11839,9 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1182111839
*decryptedKeySz = cekSz;
1182211840

1182311841
XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11842+
ForceZero(kek, (word32)kekKeySz);
1182411843
XFREE(kek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11844+
ForceZero(cek, cekSz);
1182511845
XFREE(cek, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1182611846

1182711847
/* mark recipFound, since we only support one RecipientInfo for now */

0 commit comments

Comments
 (0)