Skip to content

Commit

Permalink
add test case and macro guard
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Aug 6, 2024
1 parent 539339c commit cbf5a0c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
27 changes: 26 additions & 1 deletion tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -66309,6 +66309,18 @@ static int test_wolfssl_PKCS7(void)
return EXPECT_RESULT();
}

#ifdef ASN_BER_TO_DER
static int PKCS7StreamOut(PKCS7* pkcs7, const byte* output,
word32 outputSz, void* ctx)
{
(void)pkcs7;
(void)output;
(void)outputSz;
(void)ctx;
return 0;
}
#endif

static int test_wolfSSL_PKCS7_sign(void)
{
EXPECT_DECLS;
Expand Down Expand Up @@ -66549,9 +66561,22 @@ static int test_wolfSSL_PKCS7_sign(void)
}
}
ExpectIntEQ(ret, 0);
ExpectNotNull(out);
wc_PKCS7_Free(p7Ver);
p7Ver = NULL;

#ifdef ASN_BER_TO_DER
/* test for output callback with BER */
ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
if (p7Ver != NULL) {
p7Ver->content = data;
p7Ver->contentSz = sizeof(data);
}

wc_PKCS7_SetStreamMode(p7Ver, 0, NULL, PKCS7StreamOut, NULL);
ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
wc_PKCS7_Free(p7Ver);
p7Ver = NULL;
#endif
#endif /* !NO_PKCS7_STREAM */

XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand Down
12 changes: 9 additions & 3 deletions wolfcrypt/src/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -5208,6 +5208,7 @@ static int wc_PKCS7_HandleOctetStrings(PKCS7* pkcs7, byte* in, word32 inSz,
/* got partial octet string data */
/* accumulate partial octet string to buffer */
if (keepContent) {
#ifdef ASN_BER_TO_DER
if (pkcs7->streamOutCb) {
ret = wc_HashUpdate(&pkcs7->stream->hashAlg,
pkcs7->stream->hashType,
Expand All @@ -5217,7 +5218,9 @@ static int wc_PKCS7_HandleOctetStrings(PKCS7* pkcs7, byte* in, word32 inSz,
pkcs7->streamOutCb(pkcs7, msg + *idx,
pkcs7->stream->expected, pkcs7->streamCtx);
}
else {
else
#endif
{
/* store current content buffer temporarily */
tempBuf = pkcs7->stream->content;
pkcs7->stream->content = NULL;
Expand All @@ -5227,7 +5230,8 @@ static int wc_PKCS7_HandleOctetStrings(PKCS7* pkcs7, byte* in, word32 inSz,
pkcs7->stream->accumContSz += pkcs7->stream->expected;

pkcs7->stream->content =
(byte*)XMALLOC(pkcs7->stream->accumContSz, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
(byte*)XMALLOC(pkcs7->stream->accumContSz,
pkcs7->heap, DYNAMIC_TYPE_PKCS7);

if (pkcs7->stream->content == NULL) {
WOLFSSL_MSG("failed to grow content buffer.");
Expand Down Expand Up @@ -5866,13 +5870,15 @@ static int PKCS7_VerifySignedData(PKCS7* pkcs7, const byte* hashBuf,
wc_PKCS7_ChangeState(pkcs7, WC_PKCS7_VERIFY_STAGE3);

#ifndef NO_PKCS7_STREAM
#ifdef ASN_BER_TO_DER
/* setup hash struct for creating hash of content if needed */
if (pkcs7->streamOutCb) {
ret = wc_HashInit_ex(&pkcs7->stream->hashAlg,
pkcs7->stream->hashType, pkcs7->heap, pkcs7->devId);
if (ret != 0)
break;
}
#endif /* ASN_BER_TO_DER */

/* free pkcs7->stream->content buffer */
if (pkcs7->stream->content != NULL) {
Expand Down Expand Up @@ -6537,7 +6543,7 @@ static int PKCS7_VerifySignedData(PKCS7* pkcs7, const byte* hashBuf,
pkcs7->contentSz = (word32)contentSz;

if (ret == 0) {
#ifndef NO_PKCS7_STREAM
#if !defined(NO_PKCS7_STREAM) && defined(ASN_BER_TO_DER)
byte streamHash[WC_MAX_DIGEST_SIZE];

/* get final hash if having done hash updates while
Expand Down

0 comments on commit cbf5a0c

Please sign in to comment.