Skip to content

Commit 4ae8ca0

Browse files
Merge pull request #8859 from kojiws/clarify_supported_pkcs12_enc_algos
Clarify supported encryption algorithms on wc_PKCS12_create()
2 parents 587d5c7 + 0260ff7 commit 4ae8ca0

2 files changed

Lines changed: 69 additions & 35 deletions

File tree

tests/api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19069,6 +19069,14 @@ static int test_wc_PKCS12_create_once(int keyEncType, int certEncType)
1906919069
static int test_wc_PKCS12_create(void)
1907019070
{
1907119071
EXPECT_DECLS;
19072+
19073+
EXPECT_TEST(test_wc_PKCS12_create_once(-1, -1));
19074+
#if !defined(NO_RC4) && !defined(NO_SHA)
19075+
EXPECT_TEST(test_wc_PKCS12_create_once(PBE_SHA1_RC4_128, PBE_SHA1_RC4_128));
19076+
#endif
19077+
#if !defined(NO_DES3) && !defined(NO_SHA)
19078+
EXPECT_TEST(test_wc_PKCS12_create_once(PBE_SHA1_DES, PBE_SHA1_DES));
19079+
#endif
1907219080
#if !defined(NO_DES3) && !defined(NO_SHA)
1907319081
EXPECT_TEST(test_wc_PKCS12_create_once(PBE_SHA1_DES3, PBE_SHA1_DES3));
1907419082
#endif

wolfcrypt/src/pkcs12.c

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,51 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
17601760
}
17611761

17621762

1763+
/* Helper function to get parameters for key and cert encryptions */
1764+
static int wc_PKCS12_get_enc_params(int inAlgo, int* vPKCS, int* outAlgo,
1765+
int* blkOid, int* hmacOid)
1766+
{
1767+
int ret = 0;
1768+
1769+
if (inAlgo == PBE_SHA1_RC4_128) {
1770+
*vPKCS = 1; /* PKCS#12 */
1771+
*outAlgo = PBE_SHA1_RC4_128;
1772+
*blkOid = 0; /* Unused */
1773+
*hmacOid = 0; /* Use SHA1 as default */
1774+
}
1775+
else if (inAlgo == PBE_SHA1_DES) {
1776+
*vPKCS = PKCS5;
1777+
*outAlgo = PBES1_SHA1_DES;
1778+
*blkOid = 0; /* Unused */
1779+
*hmacOid = 0; /* Use SHA1 as default */
1780+
}
1781+
else if (inAlgo == PBE_SHA1_DES3) {
1782+
*vPKCS = 1; /* PKCS#12 */
1783+
*outAlgo = PBE_SHA1_DES3;
1784+
*blkOid = 0; /* Unused */
1785+
*hmacOid = 0; /* Use SHA1 as default */
1786+
}
1787+
else if (inAlgo == PBE_AES256_CBC) {
1788+
*vPKCS = PKCS5;
1789+
*outAlgo = PBES2;
1790+
*blkOid = AES256CBCb;
1791+
*hmacOid = HMAC_SHA256_OID;
1792+
}
1793+
else if (inAlgo == PBE_AES128_CBC) {
1794+
*vPKCS = PKCS5;
1795+
*outAlgo = PBES2;
1796+
*blkOid = AES128CBCb;
1797+
*hmacOid = HMAC_SHA256_OID;
1798+
}
1799+
else {
1800+
WOLFSSL_MSG("Unsupported algorithm for PKCS12 encryption");
1801+
ret = ALGO_ID_E;
1802+
}
1803+
1804+
return ret;
1805+
}
1806+
1807+
17631808
/* Helper function to shroud keys.
17641809
*
17651810
* pkcs12 structure to use with shrouding key
@@ -1781,15 +1826,15 @@ static int wc_PKCS12_shroud_key(WC_PKCS12* pkcs12, WC_RNG* rng,
17811826
{
17821827
void* heap;
17831828
word32 tmpIdx = 0;
1784-
int vPKCS = 1; /* PKCS#12 default set to 1 */
17851829
word32 sz;
17861830
word32 totalSz = 0;
17871831
int ret;
17881832
byte* pkcs8Key = NULL;
17891833

1790-
/* The blkOid and hmacOid are only valid for PKCS#5v2 (PBES2) */
1834+
int vPKCS = -1;
1835+
int outAlgo = -1;
17911836
int blkOid = 0;
1792-
int hmacOid = 0; /* If 0, use the default HMAC algorithm */
1837+
int hmacOid = 0;
17931838

17941839
if (outSz == NULL || pkcs12 == NULL || rng == NULL || key == NULL ||
17951840
pass == NULL) {
@@ -1826,25 +1871,13 @@ static int wc_PKCS12_shroud_key(WC_PKCS12* pkcs12, WC_RNG* rng,
18261871
else {
18271872
WOLFSSL_MSG("creating PKCS12 Shrouded Key Bag");
18281873

1829-
/* Need to handle PKCS#5v1/v2 (=non-PKCS#12v1) encryptions */
1830-
if (vAlgo == PBE_SHA1_DES) {
1831-
vPKCS = PKCS5;
1832-
vAlgo = 10;
1833-
}
1834-
else if (vAlgo == PBE_AES256_CBC) {
1835-
vPKCS = PKCS5;
1836-
vAlgo = PBES2;
1837-
blkOid = AES256CBCb;
1838-
hmacOid = HMAC_SHA256_OID;
1839-
}
1840-
else if (vAlgo == PBE_AES128_CBC) {
1841-
vPKCS = PKCS5;
1842-
vAlgo = PBES2;
1843-
blkOid = AES128CBCb;
1844-
hmacOid = HMAC_SHA256_OID;
1874+
if ((ret = wc_PKCS12_get_enc_params(vAlgo, &vPKCS, &outAlgo, &blkOid,
1875+
&hmacOid)) < 0) {
1876+
return ret;
18451877
}
1878+
18461879
ret = TraditionalEnc_ex(key, keySz, pkcs8Key, &sz, pass, passSz,
1847-
vPKCS, vAlgo, blkOid, NULL, 0, itt, hmacOid, rng, heap);
1880+
vPKCS, outAlgo, blkOid, NULL, 0, itt, hmacOid, rng, heap);
18481881
}
18491882
if (ret == WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
18501883
*outSz = sz + MAX_LENGTH_SZ + 1;
@@ -2084,14 +2117,16 @@ static int wc_PKCS12_encrypt_content(WC_PKCS12* pkcs12, WC_RNG* rng,
20842117
const char* pass, int passSz, int iter, int type)
20852118
{
20862119
void* heap;
2087-
int vPKCS = 1; /* PKCS#12 is always set to 1 */
20882120
int ret;
20892121
byte* tmp;
20902122
word32 idx = 0;
20912123
word32 totalSz = 0;
20922124
word32 length = 0;
20932125
word32 tmpSz;
20942126
word32 encSz;
2127+
2128+
int vPKCS = -1;
2129+
int outAlgo = -1;
20952130
int blkOid = 0;
20962131
int hmacOid = 0;
20972132

@@ -2111,23 +2146,14 @@ static int wc_PKCS12_encrypt_content(WC_PKCS12* pkcs12, WC_RNG* rng,
21112146
if (type == WC_PKCS12_ENCRYPTED_DATA) {
21122147
word32 outerSz = 0;
21132148

2114-
/* Need to handle PKCS#5v1/v2 (=non-PKCS#12v1) encryptions */
2115-
if (vAlgo == PBE_AES256_CBC) {
2116-
vPKCS = PKCS5;
2117-
vAlgo = PBES2;
2118-
blkOid = AES256CBCb;
2119-
hmacOid = HMAC_SHA256_OID;
2120-
}
2121-
else if (vAlgo == PBE_AES128_CBC) {
2122-
vPKCS = PKCS5;
2123-
vAlgo = PBES2;
2124-
blkOid = AES128CBCb;
2125-
hmacOid = HMAC_SHA256_OID;
2149+
if ((ret = wc_PKCS12_get_enc_params(vAlgo, &vPKCS, &outAlgo, &blkOid,
2150+
&hmacOid)) < 0) {
2151+
return ret;
21262152
}
21272153

21282154
encSz = contentSz;
21292155
if ((ret = EncryptContent(NULL, contentSz, NULL, &encSz,
2130-
pass, passSz, vPKCS, vAlgo, blkOid, NULL, 0, iter, hmacOid,
2156+
pass, passSz, vPKCS, outAlgo, blkOid, NULL, 0, iter, hmacOid,
21312157
rng, heap)) < 0) {
21322158
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
21332159
return ret;
@@ -2180,7 +2206,7 @@ static int wc_PKCS12_encrypt_content(WC_PKCS12* pkcs12, WC_RNG* rng,
21802206
}
21812207

21822208
if ((ret = EncryptContent(content, contentSz, tmp, &encSz,
2183-
pass, passSz, vPKCS, vAlgo, blkOid, NULL, 0, iter, hmacOid,
2209+
pass, passSz, vPKCS, outAlgo, blkOid, NULL, 0, iter, hmacOid,
21842210
rng, heap)) < 0) {
21852211
XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
21862212
return ret;

0 commit comments

Comments
 (0)