Skip to content

Commit 61749a0

Browse files
authored
Merge pull request #7287 from JacobBarthelmeh/pkcs7_verify_stream
PKCS7 checking trailing 0's on indef with verify
2 parents 30366a9 + 2708062 commit 61749a0

5 files changed

Lines changed: 89 additions & 6 deletions

File tree

certs/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ EXTRA_DIST += \
5151
certs/server-revoked-key.pem \
5252
certs/wolfssl-website-ca.pem \
5353
certs/test-degenerate.p7b \
54+
certs/test-stream-sign.p7b \
5455
certs/test-ber-exp02-05-2022.p7b \
5556
certs/test-servercert.p12 \
5657
certs/test-servercert-rc2.p12 \

certs/renewcerts.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@ run_renewcerts(){
854854
echo ""
855855
openssl crl2pkcs7 -nocrl -certfile ./client-cert.pem -out test-degenerate.p7b -outform DER
856856
check_result $? ""
857+
858+
openssl smime -sign -in ./ca-cert.pem -out test-stream-sign.p7b -signer ./ca-cert.pem -nodetach -nocerts -binary -outform DER -stream -inkey ./ca-key.pem
859+
check_result $? ""
860+
857861
echo "End of section"
858862
echo "---------------------------------------------------------------------"
859863

certs/test-stream-sign.p7b

6.08 KB
Binary file not shown.

tests/api.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26945,7 +26945,7 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2694526945
int certSz;
2694626946
int keySz;
2694726947

26948-
ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
26948+
ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
2694926949
XBADFILE);
2695026950
ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
2695126951
if (fp != XBADFILE) {
@@ -27099,6 +27099,7 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2709927099

2710027100
wc_PKCS7_Free(pkcs7);
2710127101
DoExpectIntEQ(wc_FreeRng(&rng), 0);
27102+
2710227103
#endif
2710327104
return EXPECT_RESULT();
2710427105
} /* END test_wc_PKCS7_EncodeSignedData */
@@ -28008,6 +28009,83 @@ static int test_wc_PKCS7_VerifySignedData_RSA(void)
2800828009
#endif /* !NO_PKCS7_STREAM */
2800928010

2801028011
#endif /* !NO_RSA */
28012+
#if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
28013+
!defined(NO_FILESYSTEM)
28014+
{
28015+
XFILE signedBundle = XBADFILE;
28016+
int signedBundleSz = 0;
28017+
int chunkSz = 1;
28018+
int i, rc;
28019+
byte* buf = NULL;
28020+
28021+
ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
28022+
"rb")) != XBADFILE);
28023+
ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
28024+
ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
28025+
ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
28026+
ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
28027+
DYNAMIC_TYPE_FILE));
28028+
if (buf != NULL) {
28029+
ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
28030+
signedBundleSz);
28031+
}
28032+
if (signedBundle != XBADFILE) {
28033+
XFCLOSE(signedBundle);
28034+
signedBundle = XBADFILE;
28035+
}
28036+
28037+
if (buf != NULL) {
28038+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
28039+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28040+
for (i = 0; i < signedBundleSz;) {
28041+
int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
28042+
chunkSz;
28043+
rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
28044+
if (rc < 0 ) {
28045+
if (rc == WC_PKCS7_WANT_READ_E) {
28046+
i += sz;
28047+
continue;
28048+
}
28049+
break;
28050+
}
28051+
else {
28052+
break;
28053+
}
28054+
}
28055+
ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
28056+
wc_PKCS7_Free(pkcs7);
28057+
pkcs7 = NULL;
28058+
}
28059+
28060+
/* now try with malformed bundle */
28061+
if (buf != NULL) {
28062+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
28063+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28064+
buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
28065+
for (i = 0; i < signedBundleSz;) {
28066+
int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
28067+
chunkSz;
28068+
rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
28069+
if (rc < 0 ) {
28070+
if (rc == WC_PKCS7_WANT_READ_E) {
28071+
i += sz;
28072+
continue;
28073+
}
28074+
break;
28075+
}
28076+
else {
28077+
break;
28078+
}
28079+
}
28080+
ExpectIntEQ(rc, ASN_PARSE_E);
28081+
wc_PKCS7_Free(pkcs7);
28082+
pkcs7 = NULL;
28083+
}
28084+
28085+
if (buf != NULL)
28086+
XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
28087+
}
28088+
#endif /* BER and stream */
2801128089
#endif
2801228090
return EXPECT_RESULT();
2801328091
} /* END test_wc_PKCS7_VerifySignedData()_RSA */

wolfcrypt/src/pkcs7.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6012,12 +6012,10 @@ static int PKCS7_VerifySignedData(PKCS7* pkcs7, const byte* hashBuf,
60126012
}
60136013
}
60146014

6015-
if (ret < 0)
6016-
break;
6017-
60186015
#ifndef NO_PKCS7_STREAM
60196016
/* make sure that terminating zero's follow */
6020-
if (ret >= 0 && pkcs7->stream->indefLen == 1) {
6017+
if ((ret == PKCS7_SIGNEEDS_CHECK || ret >= 0) &&
6018+
pkcs7->stream->indefLen == 1) {
60216019
int i;
60226020
for (i = 0; i < 3 * ASN_INDEF_END_SZ; i++) {
60236021
if (pkiMsg2[idx + i] != 0) {
@@ -6026,9 +6024,11 @@ static int PKCS7_VerifySignedData(PKCS7* pkcs7, const byte* hashBuf,
60266024
}
60276025
}
60286026
}
6027+
#endif /* NO_PKCS7_STREAM */
6028+
60296029
if (ret < 0)
60306030
break;
6031-
#endif /* NO_PKCS7_STREAM */
6031+
60326032

60336033
ret = 0; /* success */
60346034
#ifndef NO_PKCS7_STREAM

0 commit comments

Comments
 (0)