Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/ssl_p7p12.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,14 @@ int wolfSSL_PKCS7_verify(PKCS7* pkcs7, WOLFSSL_STACK* certs,
p7->pkcs7.content = mem;
p7->pkcs7.contentSz = (word32)memSz;
}
else {
/* No detached content supplied: drop any leftover content pointer a
* prior verify (e.g. the internal verify in d2i_PKCS7) set to the
* decoded eContent, so the bundle's own eContent drives verification
* rather than being compared against a stale pointer. */
p7->pkcs7.content = NULL;
p7->pkcs7.contentSz = 0;
}

ret = wc_PKCS7_VerifySignedData(&p7->pkcs7, p7->data, p7->len);
if (ret != 0)
Expand Down
23 changes: 23 additions & 0 deletions wolfcrypt/src/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -7496,6 +7496,12 @@ static int wc_PKCS7_HandleOctetStrings(wc_PKCS7* pkcs7, byte* in, word32 inSz,
if (keepContent) {
#ifdef ASN_BER_TO_DER
if (pkcs7->streamOutCb) {
/* refuse if both streamed eContent and caller-supplied
* content are present */
if (pkcs7->content != NULL && pkcs7->contentSz > 0) {
ret = SIG_VERIFY_E;
break;
}
ret = wc_HashUpdate(&pkcs7->stream->hashAlg,
pkcs7->stream->hashType,
msg + *idx, pkcs7->stream->expected);
Expand Down Expand Up @@ -8246,6 +8252,14 @@ static int PKCS7_VerifySignedData(wc_PKCS7* pkcs7, const byte* hashBuf,
pkcs7->contentSz = 0;
}
XFREE(pkcs7->contentDynamic, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
/* embedded eContent must match caller-supplied content */
if (pkcs7->content != NULL &&
(pkcs7->contentSz != pkcs7->stream->contentSz ||
XMEMCMP(pkcs7->content, pkcs7->stream->content,
pkcs7->stream->contentSz) != 0)) {
Comment thread
mattia-moffa marked this conversation as resolved.
Resolved
ret = SIG_VERIFY_E;
break;
}
pkcs7->contentDynamic = (byte*)XMALLOC(pkcs7->stream->contentSz,
pkcs7->heap, DYNAMIC_TYPE_PKCS7);
if (pkcs7->contentDynamic == NULL) {
Expand Down Expand Up @@ -8405,6 +8419,15 @@ static int PKCS7_VerifySignedData(wc_PKCS7* pkcs7, const byte* hashBuf,
content = pkcs7->content;
contentSz = pkcs7->contentSz;
}
else if (pkcs7->content != NULL && !degenerate) {
/* embedded eContent must match caller-supplied content */
if (pkcs7->contentSz != (word32)contentSz ||
(content != NULL && contentSz > 0 &&
XMEMCMP(content, pkcs7->content, (word32)contentSz) != 0)) {
ret = SIG_VERIFY_E;
break;
}
}
#endif /* !NO_PKCS7_STREAM */
wc_PKCS7_ChangeState(pkcs7, WC_PKCS7_VERIFY_STAGE4);

Expand Down
173 changes: 173 additions & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -92794,6 +92794,17 @@ static wc_test_ret_t pkcs7_signed_multi_cert_malformed_test(
* the eContent and compute the messageDigest over the empty content; the
* verifier must accept the absent eContent and check that digest without any
* caller-supplied content or hash. */
#if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM)
static int pkcs7_signed_no_content_streamOutCb(wc_PKCS7* pkcs7,
const byte* output, word32 outputSz, void* ctx)
{
(void)pkcs7;
(void)output;
(void)outputSz;
(void)ctx;
return 0;
}
#endif
static wc_test_ret_t pkcs7_signed_no_content_test(byte* cert, word32 certSz,
byte* key, word32 keySz)
{
Expand Down Expand Up @@ -92903,6 +92914,168 @@ static wc_test_ret_t pkcs7_signed_no_content_test(byte* cert, word32 certSz,
if (ret != WC_NO_ERR_TRACE(SIG_VERIFY_E))
ERROR_OUT(WC_TEST_RET_ENC_NC, out_lbl);

wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

/* Attached SignedData carrying its own eContent. When the caller also
* supplies pkcs7->content for detached verification, the embedded eContent
* must equal that content, otherwise a genuinely-signed eContent would
* verify arbitrary caller content. */
pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);

ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);

pkcs7->rng = &rng;
pkcs7->content = (byte*)content;
pkcs7->contentSz = (word32)XSTRLEN((const char*)content);
pkcs7->contentOID = DATA;
pkcs7->hashOID = SHA256h;
pkcs7->encryptOID = RSAk;
pkcs7->privateKey = key;
pkcs7->privateKeySz = keySz;

encSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
if (encSz <= 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(encSz), out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

/* caller content equal to the embedded eContent still verifies */
pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
pkcs7->content = (byte*)content;
pkcs7->contentSz = (word32)XSTRLEN((const char*)content);
ret = wc_PKCS7_VerifySignedData(pkcs7, out, (word32)encSz);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

/* caller content differing from the embedded eContent (same length) must
* be rejected, not silently verified against the signed eContent */
{
byte evil[sizeof(content)];
XMEMCPY(evil, content, sizeof(content));
evil[0] ^= 0xFF;

pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
pkcs7->content = evil;
pkcs7->contentSz = (word32)XSTRLEN((const char*)content);
ret = wc_PKCS7_VerifySignedData(pkcs7, out, (word32)encSz);
if (ret != WC_NO_ERR_TRACE(SIG_VERIFY_E))
ERROR_OUT(WC_TEST_RET_ENC_NC, out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;
}

/* caller content buffer present but zero-length must not let a non-empty
* eContent verify: the length mismatch must be refused */
pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
pkcs7->content = (byte*)content;
pkcs7->contentSz = 0;
ret = wc_PKCS7_VerifySignedData(pkcs7, out, (word32)encSz);
if (ret != WC_NO_ERR_TRACE(SIG_VERIFY_E))
ERROR_OUT(WC_TEST_RET_ENC_NC, out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

#if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM)
/* With streamOutCb set the eContent is hashed and emitted directly without
* being buffered, so the content comparison above never runs on that path.
* A caller content differing from the signed eContent must still be
* refused there. This can be reached via a cert-less bundle with no signed
* attributes (signature directly over the eContent) verified with the
* signer certificate preloaded and the content streamed in. */
{
byte evil[sizeof(content)];
int z2;

XMEMCPY(evil, content, sizeof(content));
evil[0] ^= 0xFF;

pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
pkcs7->rng = &rng;
pkcs7->content = (byte*)content;
pkcs7->contentSz = (word32)XSTRLEN((const char*)content);
pkcs7->contentOID = DATA;
pkcs7->hashOID = SHA256h;
pkcs7->encryptOID = RSAk;
pkcs7->privateKey = key;
pkcs7->privateKeySz = keySz;
ret = wc_PKCS7_SetDefaultSignedAttribs(pkcs7, WOLFSSL_NO_ATTRIBUTES);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
ret = wc_PKCS7_SetNoCerts(pkcs7, 1);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
encSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
if (encSz <= 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(encSz), out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

/* no caller content: the streamed eContent verifies, confirming this
* path is actually reached */
pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
ret = wc_PKCS7_SetStreamMode(pkcs7, 0, NULL,
pkcs7_signed_no_content_streamOutCb, NULL);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
ret = WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E);
for (z2 = 0; z2 < encSz &&
ret == WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E); z2++) {
ret = wc_PKCS7_VerifySignedData(pkcs7, out + z2, 1);
}
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;

/* differing caller content must be refused, not verified against the
* streamed eContent */
pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
if (pkcs7 == NULL)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out_lbl);
ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
ret = wc_PKCS7_SetStreamMode(pkcs7, 0, NULL,
pkcs7_signed_no_content_streamOutCb, NULL);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out_lbl);
pkcs7->content = evil;
pkcs7->contentSz = (word32)XSTRLEN((const char*)content);
ret = WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E);
for (z2 = 0; z2 < encSz &&
ret == WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E); z2++) {
ret = wc_PKCS7_VerifySignedData(pkcs7, out + z2, 1);
}
if (ret != WC_NO_ERR_TRACE(SIG_VERIFY_E))
ERROR_OUT(WC_TEST_RET_ENC_NC, out_lbl);
wc_PKCS7_Free(pkcs7);
pkcs7 = NULL;
}
#endif /* ASN_BER_TO_DER && !NO_PKCS7_STREAM */

ret = 0;

out_lbl:
Expand Down
Loading