Skip to content

Commit 4a650f5

Browse files
committed
Fix PKCS#7 regression with --enable-all and NO_PKCS7_STREAM
1 parent d5af526 commit 4a650f5

2 files changed

Lines changed: 22 additions & 23 deletions

File tree

tests/api/test_pkcs7.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,7 +2397,8 @@ static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
23972397
HAVE_AES_KEYWRAP */
23982398

23992399

2400-
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER) && !defined(NO_RSA)
2400+
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER) && !defined(NO_RSA) && \
2401+
!defined(NO_PKCS7_STREAM)
24012402
#define MAX_TEST_DECODE_SIZE 6000
24022403
static int test_wc_PKCS7_DecodeEnvelopedData_stream_decrypt_cb(wc_PKCS7* pkcs7,
24032404
const byte* output, word32 outputSz, void* ctx) {
@@ -2430,7 +2431,8 @@ static int test_wc_PKCS7_DecodeEnvelopedData_stream_decrypt_cb(wc_PKCS7* pkcs7,
24302431
int test_wc_PKCS7_DecodeEnvelopedData_stream(void)
24312432
{
24322433
EXPECT_DECLS;
2433-
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER) && !defined(NO_RSA)
2434+
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER) && !defined(NO_RSA) && \
2435+
!defined(NO_PKCS7_STREAM)
24342436
PKCS7* pkcs7 = NULL;
24352437
int ret = 0;
24362438
XFILE f = XBADFILE;
@@ -2579,7 +2581,7 @@ int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
25792581
EXPECT_DECLS;
25802582
#if defined(HAVE_PKCS7)
25812583
PKCS7* pkcs7 = NULL;
2582-
#ifdef ASN_BER_TO_DER
2584+
#if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM)
25832585
int encodedSz = 0;
25842586
#endif
25852587
#ifdef ECC_TIMING_RESISTANT
@@ -2784,7 +2786,7 @@ int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
27842786

27852787
testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
27862788
for (i = 0; i < testSz; i++) {
2787-
#ifdef ASN_BER_TO_DER
2789+
#if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM)
27882790
encodeSignedDataStream strm;
27892791

27902792
/* test setting stream mode, the first one using IO callbacks */
@@ -2950,17 +2952,11 @@ int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
29502952
pkcs7->singleCert = NULL;
29512953
}
29522954
#ifndef NO_RSA
2953-
#if defined(NO_PKCS7_STREAM)
2954-
/* when none streaming mode is used and PKCS7 is in bad state buffer error
2955-
* is returned from kari parse which gets set to bad func arg */
2956-
ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
2957-
(word32)sizeof(output), decoded, (word32)sizeof(decoded)),
2958-
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2959-
#else
2955+
/* With corrupted singleCert, decode should fail with a parse error.
2956+
* State is properly reset on error so re-decode starts from scratch. */
29602957
ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
29612958
(word32)sizeof(output), decoded, (word32)sizeof(decoded)),
29622959
WC_NO_ERR_TRACE(ASN_PARSE_E));
2963-
#endif
29642960
#endif /* !NO_RSA */
29652961
if (pkcs7 != NULL) {
29662962
pkcs7->singleCert = tmpBytePtr;
@@ -3991,7 +3987,8 @@ int test_wc_PKCS7_Degenerate(void)
39913987
} /* END test_wc_PKCS7_Degenerate() */
39923988

39933989
#if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
3994-
defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
3990+
defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA) && \
3991+
!defined(NO_PKCS7_STREAM)
39953992
static byte berContent[] = {
39963993
0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
39973994
0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
@@ -4182,7 +4179,7 @@ static byte berContent[] = {
41824179
0x00, 0x00, 0x00, 0x00, 0x00
41834180
};
41844181
#endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
4185-
* !NO_DES3 && !NO_SHA
4182+
* !NO_DES3 && !NO_SHA && !NO_PKCS7_STREAM
41864183
*/
41874184

41884185
/*
@@ -4197,7 +4194,7 @@ int test_wc_PKCS7_BER(void)
41974194
char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
41984195
XFILE f = XBADFILE;
41994196
byte der[4096];
4200-
#ifndef NO_DES3
4197+
#if !defined(NO_DES3) && !defined(NO_PKCS7_STREAM)
42014198
byte decoded[2048];
42024199
#endif
42034200
word32 derSz = 0;
@@ -4242,8 +4239,9 @@ int test_wc_PKCS7_BER(void)
42424239
wc_PKCS7_Free(pkcs7);
42434240
pkcs7 = NULL;
42444241

4245-
#ifndef NO_DES3
4246-
/* decode BER content */
4242+
#if !defined(NO_DES3) && !defined(NO_PKCS7_STREAM)
4243+
/* decode BER content - requires PKCS7 streaming to handle indefinite
4244+
* length encoding in the EnvelopedData structure */
42474245
ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
42484246
ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
42494247
if (f != XBADFILE) {
@@ -4280,7 +4278,7 @@ int test_wc_PKCS7_BER(void)
42804278
sizeof(berContent), decoded, sizeof(decoded)), WC_NO_ERR_TRACE(NOT_COMPILED_IN));
42814279
#endif
42824280
wc_PKCS7_Free(pkcs7);
4283-
#endif /* !NO_DES3 */
4281+
#endif /* !NO_DES3 && !NO_PKCS7_STREAM */
42844282
#endif
42854283
return EXPECT_RESULT();
42864284
} /* END test_wc_PKCS7_BER() */

wolfcrypt/src/pkcs7.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10758,15 +10758,13 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1075810758
if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1075910759
return ASN_PARSE_E;
1076010760

10761-
/* Validate SKID container and keyIdSize against buffer */
10761+
/* Validate SKID container is within buffer */
1076210762
if ((word32)length > pkiMsgSz - (*idx))
1076310763
return BUFFER_E;
1076410764

10765-
if (length < keyIdSize)
10766-
return ASN_PARSE_E;
10767-
1076810765
/* if we found correct recipient, SKID will match */
10769-
if (XMEMCMP(pkiMsg + (*idx), pkcs7->issuerSubjKeyId,
10766+
if (length == keyIdSize &&
10767+
XMEMCMP(pkiMsg + (*idx), pkcs7->issuerSubjKeyId,
1077010768
(word32)keyIdSize) == 0) {
1077110769
*recipFound = 1;
1077210770
}
@@ -13389,6 +13387,9 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1338913387
}
1339013388
}
1339113389
#else
13390+
if (ret < 0) {
13391+
wc_PKCS7_ChangeState(pkcs7, WC_PKCS7_START);
13392+
}
1339213393
if (decryptedKey != NULL && ret < 0) {
1339313394
ForceZero(decryptedKey, MAX_ENCRYPTED_KEY_SZ);
1339413395
XFREE(decryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);

0 commit comments

Comments
 (0)