@@ -9536,10 +9536,10 @@ static int GetAlgoV2(int encAlgId, const byte** oid, int *len, int* id,
95369536 return ret;
95379537}
95389538
9539- int wc_EncryptPKCS8Key (byte* key, word32 keySz, byte* out, word32* outSz,
9539+ int wc_EncryptPKCS8Key_ex (byte* key, word32 keySz, byte* out, word32* outSz,
95409540 const char* password, int passwordSz, int vPKCS, int pbeOid,
9541- int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng ,
9542- void* heap)
9541+ int encAlgId, byte* salt, word32 saltSz, int itt, int hmacOid ,
9542+ WC_RNG* rng, void* heap)
95439543{
95449544#ifdef WOLFSSL_SMALL_STACK
95459545 byte* saltTmp = NULL;
@@ -9563,10 +9563,12 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
95639563 byte cbcIv[MAX_IV_SIZE];
95649564 word32 idx = 0;
95659565 word32 encIdx = 0;
9566+ const byte* hmacOidBuf = NULL;
9567+ word32 hmacOidBufSz = 0;
95669568
95679569 (void)heap;
95689570
9569- WOLFSSL_ENTER("wc_EncryptPKCS8Key ");
9571+ WOLFSSL_ENTER("wc_EncryptPKCS8Key_ex ");
95709572
95719573 if (key == NULL || outSz == NULL || password == NULL) {
95729574 ret = BAD_FUNC_ARG;
@@ -9594,6 +9596,11 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
95949596 pbeLen = 2 + pbeOidBufSz + 2 + innerLen;
95959597 }
95969598 else {
9599+ if (hmacOid > 0) {
9600+ hmacOidBuf = OidFromId((word32)hmacOid, oidHmacType,
9601+ &hmacOidBufSz);
9602+ innerLen += 2 + 2 + hmacOidBufSz;
9603+ }
95979604 pbeOidBuf = pbes2;
95989605 pbeOidBufSz = sizeof(pbes2);
95999606 /* kdf = OBJ pbkdf2 [ SEQ innerLen ] */
@@ -9650,7 +9657,7 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
96509657 }
96519658 if (ret == 0) {
96529659 ret = wc_CryptKey(password, passwordSz, salt, (int)saltSz, itt, pbeId,
9653- out + encIdx, (int)keySz, version, cbcIv, 1, 0 );
9660+ out + encIdx, (int)keySz, version, cbcIv, 1, hmacOid );
96549661 }
96559662 if (ret == 0) {
96569663 if (version != PKCS5v2) {
@@ -9680,6 +9687,14 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
96809687 ret = SetShortInt(out, &idx, (word32)itt, *outSz);
96819688 if (ret > 0)
96829689 ret = 0;
9690+ if (version == PKCS5v2) {
9691+ if (hmacOid > 0) {
9692+ idx += SetSequence(2+hmacOidBufSz, out + idx);
9693+ idx += (word32)SetObjectId((int)hmacOidBufSz, out + idx);
9694+ XMEMCPY(out + idx, hmacOidBuf, hmacOidBufSz);
9695+ idx += (word32)hmacOidBufSz;
9696+ }
9697+ }
96839698 }
96849699 if (ret == 0) {
96859700 if (version == PKCS5v2) {
@@ -9704,11 +9719,20 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
97049719 XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
97059720#endif
97069721
9707- WOLFSSL_LEAVE("wc_EncryptPKCS8Key ", ret);
9722+ WOLFSSL_LEAVE("wc_EncryptPKCS8Key_ex ", ret);
97089723
97099724 return ret;
97109725}
97119726
9727+ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
9728+ const char* password, int passwordSz, int vPKCS, int pbeOid,
9729+ int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng,
9730+ void* heap)
9731+ {
9732+ return wc_EncryptPKCS8Key_ex(key, keySz, out, outSz, password, passwordSz,
9733+ vPKCS, pbeOid, encAlgId, salt, saltSz, itt, 0, rng, heap);
9734+ }
9735+
97129736int wc_DecryptPKCS8Key(byte* input, word32 sz, const char* password,
97139737 int passwordSz)
97149738{
@@ -9751,10 +9775,10 @@ int wc_DecryptPKCS8Key(byte* input, word32 sz, const char* password,
97519775 * encrypted key. If out is not NULL, it will hold the encrypted key. If it's
97529776 * NULL, LENGTH_ONLY_E will be returned and outSz will have the required out
97539777 * buffer size. */
9754- int TraditionalEnc (byte* key, word32 keySz, byte* out, word32* outSz,
9778+ int TraditionalEnc_ex (byte* key, word32 keySz, byte* out, word32* outSz,
97559779 const char* password, int passwordSz, int vPKCS, int vAlgo,
9756- int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng ,
9757- void* heap)
9780+ int encAlgId, byte* salt, word32 saltSz, int itt, int hmacOid ,
9781+ WC_RNG* rng, void* heap)
97589782{
97599783 int ret = 0;
97609784 byte *pkcs8Key = NULL;
@@ -9794,8 +9818,9 @@ int TraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,
97949818 }
97959819#endif
97969820 if (ret == 0) {
9797- ret = wc_EncryptPKCS8Key(pkcs8Key, pkcs8KeySz, out, outSz, password,
9798- passwordSz, vPKCS, vAlgo, encAlgId, salt, saltSz, itt, rng, heap);
9821+ ret = wc_EncryptPKCS8Key_ex(pkcs8Key, pkcs8KeySz, out, outSz, password,
9822+ passwordSz, vPKCS, vAlgo, encAlgId, salt, saltSz, itt, hmacOid, rng,
9823+ heap);
97999824 }
98009825
98019826 if (pkcs8Key != NULL) {
@@ -9808,6 +9833,20 @@ int TraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,
98089833 return ret;
98099834}
98109835
9836+ /* Takes an unencrypted, traditional DER-encoded key and converts it to a PKCS#8
9837+ * encrypted key. If out is not NULL, it will hold the encrypted key. If it's
9838+ * NULL, LENGTH_ONLY_E will be returned and outSz will have the required out
9839+ * buffer size. */
9840+ int TraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,
9841+ const char* password, int passwordSz, int vPKCS, int vAlgo,
9842+ int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng,
9843+ void* heap)
9844+ {
9845+ return TraditionalEnc_ex(key, keySz, out, outSz, password, passwordSz,
9846+ vPKCS, vAlgo, encAlgId, salt, saltSz, itt, 0, rng, heap);
9847+
9848+ }
9849+
98119850/* Same as TraditionalEnc, but in the public API. */
98129851int wc_CreateEncryptedPKCS8Key(byte* key, word32 keySz, byte* out,
98139852 word32* outSz, const char* password, int passwordSz, int vPKCS,
0 commit comments