Skip to content

Commit 7de2631

Browse files
authored
Merge pull request #10378 from rlm2002/fenrir
Various PKCS12 Fixes
2 parents e38a120 + 3137d62 commit 7de2631

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

wolfcrypt/src/pkcs12.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ static int GetSafeContent(WC_PKCS12* pkcs12, const byte* input,
328328

329329
curIdx = localIdx;
330330
if ((ret = GetObjectId(input, &localIdx, &oid, oidIgnoreType,
331-
(word32)size)) < 0) {
331+
curIdx + (word32)curSz)) < 0) {
332332
WOLFSSL_LEAVE("Get object id failed", ret);
333333
freeSafe(safe, pkcs12->heap);
334334
return ret;
@@ -558,7 +558,8 @@ static int wc_PKCS12_create_mac(WC_PKCS12* pkcs12, byte* data, word32 dataSz,
558558
return MEMORY_E; });
559559

560560
/* unicode set up from asn.c */
561-
if ((pswSz * 2 + 2) > MAX_UNICODE_SZ) {
561+
if (pswSz >= MAX_UNICODE_SZ ||
562+
(pswSz * 2 + 2) > MAX_UNICODE_SZ) {
562563
WOLFSSL_MSG("PKCS12 max unicode size too small");
563564
ret = UNICODE_SIZE_E;
564565
goto exit_mac;
@@ -695,6 +696,9 @@ int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12)
695696
int ret;
696697
int size = 0;
697698
int version = 0;
699+
#ifdef ASN_BER_TO_DER
700+
word32 tmpSz = 0;
701+
#endif
698702

699703
WOLFSSL_ENTER("wolfSSL_d2i_PKCS12");
700704

@@ -716,22 +720,22 @@ int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12)
716720
#ifdef ASN_BER_TO_DER
717721
if (size == 0) {
718722
if (wc_BerToDer(der, totalSz, NULL,
719-
(word32*)&size) != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
723+
&tmpSz) != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
720724
WOLFSSL_MSG("Not BER sequence");
721725
return ASN_PARSE_E;
722726
}
723727

724-
pkcs12->der = (byte*)XMALLOC((size_t)size, pkcs12->heap, DYNAMIC_TYPE_PKCS);
728+
pkcs12->der = (byte*)XMALLOC((size_t)tmpSz, pkcs12->heap, DYNAMIC_TYPE_PKCS);
725729
if (pkcs12->der == NULL)
726730
return MEMORY_E;
727-
ret = wc_BerToDer(der, derSz, pkcs12->der, (word32*)&size);
731+
ret = wc_BerToDer(der, derSz, pkcs12->der, &tmpSz);
728732
if (ret < 0) {
729733
return ret;
730734
}
731735

732736
der = pkcs12->der;
733-
pkcs12->derSz = (word32)size;
734-
totalSz = (word32)size;
737+
pkcs12->derSz = tmpSz;
738+
totalSz = tmpSz;
735739
idx = 0;
736740

737741
if (GetSequence(der, &idx, &size, totalSz) < 0) {
@@ -1558,8 +1562,13 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
15581562
*pkeySz = (word32)size;
15591563
}
15601564
else {
1561-
*pkeySz = (word32)ToTraditional_ex(*pkey,
1562-
(word32)size, &algId);
1565+
ret = ToTraditional_ex(*pkey,
1566+
(word32)size, &algId);
1567+
if (ret < 0) {
1568+
*pkeySz = (word32)size;
1569+
goto exit_pk12par;
1570+
}
1571+
*pkeySz = (word32)ret;
15631572
}
15641573
}
15651574

@@ -1602,13 +1611,15 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
16021611
if (keepKeyHeader) {
16031612
if ((ret = wc_DecryptPKCS8Key(k, (word32)size, psw,
16041613
pswSz)) < 0) {
1614+
ForceZero(k, (size_t)size);
16051615
XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
16061616
goto exit_pk12par;
16071617
}
16081618
}
16091619
else {
16101620
if ((ret = ToTraditionalEnc(k, (word32)size, psw,
16111621
pswSz, &algId)) < 0) {
1622+
ForceZero(k, (size_t)size);
16121623
XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
16131624
goto exit_pk12par;
16141625
}
@@ -1619,10 +1630,12 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
16191630
byte* tmp = (byte*)XMALLOC((size_t)ret, pkcs12->heap,
16201631
DYNAMIC_TYPE_PUBLIC_KEY);
16211632
if (tmp == NULL) {
1633+
ForceZero(k, (size_t)size);
16221634
XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
16231635
ERROR_OUT(MEMORY_E, exit_pk12par);
16241636
}
16251637
XMEMCPY(tmp, k, (size_t)ret);
1638+
ForceZero(k, (size_t)size);
16261639
XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
16271640
k = tmp;
16281641
}
@@ -1633,6 +1646,7 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
16331646
*pkeySz = (word32)size;
16341647
}
16351648
else { /* only expecting one key */
1649+
ForceZero(k, (size_t)size);
16361650
XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
16371651
}
16381652
idx += (word32)size;
@@ -1798,6 +1812,7 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
17981812
if (ret != 0) {
17991813
/* failure cleanup */
18001814
if (*pkey) {
1815+
ForceZero(*pkey, *pkeySz);
18011816
XFREE(*pkey, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
18021817
*pkey = NULL;
18031818
}

wolfcrypt/src/pwdbased.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,19 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen,
564564
#ifdef WOLFSSL_SMALL_STACK
565565
out:
566566

567+
ForceZero(Ai, WC_MAX_DIGEST_SIZE);
567568
XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
569+
ForceZero(B, WC_MAX_BLOCK_SIZE);
568570
XFREE(B, heap, DYNAMIC_TYPE_TMP_BUFFER);
569571
XFREE(B1, heap, DYNAMIC_TYPE_TMP_BUFFER);
570572
XFREE(i1, heap, DYNAMIC_TYPE_TMP_BUFFER);
571573
XFREE(res, heap, DYNAMIC_TYPE_TMP_BUFFER);
574+
#else
575+
ForceZero(Ai, WC_MAX_DIGEST_SIZE);
576+
ForceZero(B, WC_MAX_BLOCK_SIZE);
572577
#endif
573578

579+
ForceZero(buffer, totalLen);
574580
if (dynamic)
575581
XFREE(buffer, heap, DYNAMIC_TYPE_KEY);
576582

wolfcrypt/src/wc_encrypt.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ int wc_CryptKey(const char* password, int passwordSz, const byte* salt,
467467
int i, idx = 0;
468468
byte unicodePasswd[MAX_UNICODE_SZ];
469469

470-
if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd)) {
470+
if (passwordSz < 0 ||
471+
passwordSz >= (int)sizeof(unicodePasswd) ||
472+
(passwordSz * 2 + 2) > (int)sizeof(unicodePasswd)) {
471473
ret = UNICODE_SIZE_E;
472474
break;
473475
}
@@ -482,16 +484,21 @@ int wc_CryptKey(const char* password, int passwordSz, const byte* salt,
482484

483485
ret = wc_PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz,
484486
iterations, (int)derivedLen, typeH, 1);
485-
if (ret < 0)
487+
if (ret < 0) {
488+
ForceZero(unicodePasswd, MAX_UNICODE_SZ);
486489
break;
490+
}
487491
if (id != PBE_SHA1_RC4_128) {
488492
i = ret;
489493
ret = wc_PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt,
490494
saltSz, iterations, 8, typeH, 2);
491-
if (ret < 0)
495+
if (ret < 0) {
496+
ForceZero(unicodePasswd, MAX_UNICODE_SZ);
492497
break;
498+
}
493499
ret += i;
494500
}
501+
ForceZero(unicodePasswd, MAX_UNICODE_SZ);
495502
break;
496503
}
497504
#endif /* HAVE_PKCS12 */

0 commit comments

Comments
 (0)