Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4550,6 +4550,7 @@ static void test_wolfSSL_PKCS12(void)

d2i_PKCS12_bio(bio, &pkcs12);
AssertNotNull(pkcs12);
BIO_free(bio);

/* check verify MAC fail case */
ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
Expand Down Expand Up @@ -4631,6 +4632,13 @@ static void test_wolfSSL_PKCS12(void)
X509_free(cert);
sk_X509_free(ca);

/* convert to DER then back and parse */
AssertNotNull(bio = BIO_new(BIO_s_mem()));
AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
PKCS12_free(pkcs12_2);

AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
BIO_free(bio);
AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
SSL_SUCCESS);

Expand Down Expand Up @@ -4661,7 +4669,6 @@ static void test_wolfSSL_PKCS12(void)

EVP_PKEY_free(pkey);
X509_free(cert);
BIO_free(bio);
PKCS12_free(pkcs12);
PKCS12_free(pkcs12_2);
sk_X509_free(ca);
Expand Down
144 changes: 70 additions & 74 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3426,7 +3426,8 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,

/* check key type and get OID if ECC */
if ((ret = wc_GetKeyOID(key, keySz, &curveOID, &oidSz, &algoID, heap))< 0) {
return ret;
WOLFSSL_MSG("Error getting key OID");
return ret;
}

/* PKCS#8 wrapping around key */
Expand Down Expand Up @@ -3937,6 +3938,9 @@ int ToTraditionalEnc(byte* input, word32 sz,const char* password,
* heap possible heap hint for mallocs/frees
*
* returns the total size of encrypted content on success.
*
* data returned is :
* [ seq - obj [ seq -salt,itt]] , construct with encrypted data
*/
int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
const char* password, int passwordSz, int vPKCS, int vAlgo,
Expand All @@ -3947,6 +3951,7 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
word32 tmpIdx = 0;
word32 totalSz = 0;
word32 seqSz;
word32 innerSz;
int ret;
int version, id, blockSz = 0;
#ifdef WOLFSSL_SMALL_STACK
Expand All @@ -3956,6 +3961,11 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
byte saltTmp[MAX_SALT_SIZE];
byte cbcIv[MAX_IV_SIZE];
#endif
byte seq[MAX_SEQ_SZ];
byte shr[MAX_SHORT_SZ];
word32 maxShr = MAX_SHORT_SZ;
word32 algoSz;
const byte* algoName;

(void)heap;

Expand All @@ -3976,58 +3986,51 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
return BAD_FUNC_ARG;
}

if (out == NULL) {
sz = inputSz;
switch (id) {
#if !defined(NO_DES3) && (!defined(NO_MD5) || !defined(NO_SHA))
case PBE_MD5_DES:
case PBE_SHA1_DES:
case PBE_SHA1_DES3:
/* set to block size of 8 for DES operations. This rounds up
* to the nearest multiple of 8 */
sz &= 0xfffffff8;
sz += 8;
break;
#endif /* !NO_DES3 && (!NO_MD5 || !NO_SHA) */
#if !defined(NO_RC4) && !defined(NO_SHA)
case PBE_SHA1_RC4_128:
break;
#endif
case -1:
break;

default:
return ALGO_ID_E;
}
/* calculate size */
/* size of constructed string at end */
sz = Pkcs8Pad(NULL, inputSz, blockSz);
totalSz = ASN_TAG_SZ;
totalSz += SetLength(sz, seq);
totalSz += sz;

if (saltSz == 0) {
sz += MAX_SALT_SIZE;
}
else {
sz += saltSz;
}
/* size of sequence holding object id and sub sequence of salt and itt */
algoName = OidFromId(id, oidPBEType, &algoSz);
if (algoName == NULL) {
WOLFSSL_MSG("Unknown Algorithm");
return 0;
}
innerSz = SetObjectId(algoSz, seq);
innerSz += algoSz;

/* add 2 for tags */
totalSz = sz + MAX_ALGO_SZ + MAX_SEQ_SZ + MAX_LENGTH_SZ +
MAX_LENGTH_SZ + MAX_LENGTH_SZ + MAX_SHORT_SZ + 2;
/* get subsequence of salt and itt */
if (salt == NULL || saltSz == 0) {
sz = 8;
}
else {
sz = saltSz;
}
seqSz = SetOctetString(sz, seq);
seqSz += sz;

/* adjust size to pad */
totalSz = Pkcs8Pad(NULL, totalSz, blockSz);
tmpIdx = 0;
seqSz += SetShortInt(shr, &tmpIdx, itt, maxShr);
innerSz += seqSz + SetSequence(seqSz, seq);
totalSz += innerSz + SetSequence(innerSz, seq);

/* return result */
if (out == NULL) {
*outSz = totalSz;

return LENGTH_ONLY_E;
}

if (inOutIdx + MAX_ALGO_SZ + MAX_SEQ_SZ + 1 > *outSz)
inOutIdx = 0;
if (totalSz > *outSz)
return BUFFER_E;

sz = SetAlgoID(id, out + inOutIdx, oidPBEType, 0);
inOutIdx += sz; totalSz += sz;
tmpIdx = inOutIdx;
tmpIdx += MAX_SEQ_SZ; /* save room for salt and itter sequence */
out[tmpIdx++] = ASN_OCTET_STRING;
inOutIdx += SetSequence(innerSz, out + inOutIdx);
inOutIdx += SetObjectId(algoSz, out + inOutIdx);
XMEMCPY(out + inOutIdx, algoName, algoSz);
inOutIdx += algoSz;
inOutIdx += SetSequence(seqSz, out + inOutIdx);

/* create random salt if one not provided */
if (salt == NULL || saltSz == 0) {
Expand All @@ -4047,60 +4050,53 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
return ret;
}
}

if (tmpIdx + MAX_LENGTH_SZ + saltSz + MAX_SHORT_SZ > *outSz) {
inOutIdx += SetOctetString(saltSz, out + inOutIdx);
if (saltSz + inOutIdx > *outSz) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return BUFFER_E;
}

sz = SetLength(saltSz, out + tmpIdx);
tmpIdx += sz;

XMEMCPY(out + tmpIdx, salt, saltSz);
tmpIdx += saltSz;
XMEMCPY(out + inOutIdx, salt, saltSz);
inOutIdx += saltSz;

/* place iteration setting in buffer */
ret = SetShortInt(out, &tmpIdx, itt, *outSz);
ret = SetShortInt(out, &inOutIdx, itt, *outSz);
if (ret < 0) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}

/* rewind and place sequence */
sz = tmpIdx - inOutIdx - MAX_SEQ_SZ;
seqSz = SetSequence(sz, out + inOutIdx);
XMEMMOVE(out + inOutIdx + seqSz, out + inOutIdx + MAX_SEQ_SZ, sz);
inOutIdx += seqSz; totalSz += seqSz;
inOutIdx += sz; totalSz += sz;

#ifdef WOLFSSL_SMALL_STACK
cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (cbcIv == NULL) {
if (inOutIdx + 1 > *outSz) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif

if (inOutIdx + 1 + MAX_LENGTH_SZ + inputSz > *outSz)
#endif
return BUFFER_E;

out[inOutIdx++] = ASN_CONTEXT_SPECIFIC | 0; totalSz++;
sz = SetLength(inputSz, out + inOutIdx);
inOutIdx += sz; totalSz += sz;
}
out[inOutIdx++] = ASN_CONTEXT_SPECIFIC | 0;

/* get pad size and verify buffer room */
sz = Pkcs8Pad(NULL, inputSz, blockSz);
if (sz + inOutIdx > *outSz)
if (sz + inOutIdx > *outSz) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return BUFFER_E;
}
inOutIdx += SetLength(sz, out + inOutIdx);

/* copy input to output buffer and pad end */
XMEMCPY(out + inOutIdx, input, inputSz);
sz = Pkcs8Pad(out + inOutIdx, inputSz, blockSz);
totalSz += sz;
#ifdef WOLFSSL_SMALL_STACK
cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (cbcIv == NULL) {
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif

/* encrypt */
if ((ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id,
Expand All @@ -4120,7 +4116,7 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,

(void)rng;

return totalSz;
return inOutIdx + sz;
}


Expand Down
Loading