Skip to content

Commit 8bc6b48

Browse files
committed
Reject a PKCS#7 verify input inside a buffer the object owns
After a verify, pkcs7->content can point into a buffer the wc_PKCS7 owns: stream->content in streaming builds, contentDynamic for an eContent joined from several parts, or the DER copy of a BER message. Verifying that content on the same object, as an application might for a nested SignedData, freed the buffer during the call while the input still pointed into it. Streaming builds released stream->content in stage 2 and parsed freed memory in wc_PKCS7_HandleOctetStrings(), and builds without streaming freed contentDynamic in stage 3. The DER copy is already refused. Refuse an input or footer inside any of these buffers with BAD_FUNC_ARG on every call. When a verify starts, drop a pkcs7->content pointer left inside stream->content or contentDynamic, as is already done for the DER copy, keeping contentSz for a header/footer verify, which takes it from the caller; before, stage 3 zeroed it after a verify of multi-part content and the header/footer verify failed with BUFFER_E. The checks use the allocated sizes, kept in a new stream field and a new wc_PKCS7 member contentDynamicSz at the end of the structure, so a pointer into the middle of a buffer is caught however the caller changed pkcs7->content. The documentation says the content has to be copied or verified with another structure. Content that points into the caller's own buffer is not affected, so a nested verify without streaming of a message the caller holds still works.
1 parent 76f6fa4 commit 8bc6b48

5 files changed

Lines changed: 279 additions & 9 deletions

File tree

‎doc/dox_comments/header_files/pkcs7.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ int wc_PKCS7_EncodeSignedData_ex(wc_PKCS7* pkcs7, const byte* hashBuf,
350350
is valid only until the next verify on this structure or wc_PKCS7_Free();
351351
when the message is verified from a single input buffer it may point into
352352
that buffer, so it is also invalid once the input buffer is freed or reused.
353+
Depending on the build that content can lie in memory this structure owns,
354+
so copy it first or use another structure to verify it as a nested
355+
message; an input inside such memory is rejected with BAD_FUNC_ARG, and
356+
pkcs7->content left pointing into it is dropped when the next verify
357+
starts.
353358
354359
\return 0 Returned on successfully extracting the information
355360
from the message
@@ -430,7 +435,9 @@ int wc_PKCS7_VerifySignedData(wc_PKCS7* pkcs7,
430435
hash/header/footer, then extracts the certificate list and certificate
431436
revocation list, and then verifies the signature. It stores the extracted
432437
content in the given PKCS7 structure, in the same form as
433-
wc_PKCS7_VerifySignedData() reports it.
438+
wc_PKCS7_VerifySignedData() reports it. The same lifetime rules apply,
439+
and a header or footer inside memory this structure owns is rejected
440+
with BAD_FUNC_ARG.
434441
435442
\return 0 Returned on successfully extracting the information
436443
from the message

‎tests/api/test_pkcs7.c‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11259,3 +11259,206 @@ int test_wc_PKCS7_VerifySignedData_ReuseAfterBer(void)
1125911259
#endif
1126011260
return EXPECT_RESULT();
1126111261
}
11262+
11263+
#if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_SHA256) && \
11264+
defined(USE_CERT_BUFFERS_2048)
11265+
/* Split the eContent OCTET STRING of msg, contentSz bytes encoded with a
11266+
* two-byte length, into two parts and return the new size. */
11267+
static int pkcs7_split_content(byte* out, const byte* msg, int msgSz,
11268+
word32 contentSz)
11269+
{
11270+
static const byte dataOid[] = {
11271+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
11272+
};
11273+
word32 half = contentSz / 2;
11274+
word32 oct = 0;
11275+
word32 i;
11276+
word32 o;
11277+
11278+
for (i = 4; i + sizeof(dataOid) + 8 <= (word32)msgSz; i++) {
11279+
if (XMEMCMP(msg + i, dataOid, sizeof(dataOid)) == 0 &&
11280+
msg[i + sizeof(dataOid)] ==
11281+
(ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0) &&
11282+
msg[i + sizeof(dataOid) + 4] == ASN_OCTET_STRING &&
11283+
msg[i + sizeof(dataOid) + 5] == (ASN_LONG_LENGTH | 2)) {
11284+
oct = i + (word32)sizeof(dataOid) + 4;
11285+
break;
11286+
}
11287+
}
11288+
if (oct == 0 || half < 0x80 || msg[1] != (ASN_LONG_LENGTH | 2) ||
11289+
msg[16] != (ASN_LONG_LENGTH | 2) ||
11290+
msg[20] != (ASN_LONG_LENGTH | 2) ||
11291+
msg[oct - 18] != (ASN_LONG_LENGTH | 2) ||
11292+
msg[oct - 3] != (ASN_LONG_LENGTH | 2) ||
11293+
oct + 4 + contentSz > (word32)msgSz)
11294+
return -1;
11295+
11296+
XMEMCPY(out, msg, oct);
11297+
if (pkcs7_grow_der_len(out, 0, 8) != 0 ||
11298+
pkcs7_grow_der_len(out, 15, 8) != 0 ||
11299+
pkcs7_grow_der_len(out, 19, 8) != 0 ||
11300+
pkcs7_grow_der_len(out, oct - 19, 8) != 0 ||
11301+
pkcs7_grow_der_len(out, oct - 4, 8) != 0)
11302+
return -1;
11303+
o = oct;
11304+
out[o++] = ASN_OCTET_STRING | ASN_CONSTRUCTED;
11305+
out[o++] = ASN_LONG_LENGTH | 2;
11306+
out[o++] = (byte)((contentSz + 8) >> 8);
11307+
out[o++] = (byte)(contentSz + 8);
11308+
out[o++] = ASN_OCTET_STRING;
11309+
out[o++] = ASN_LONG_LENGTH | 2;
11310+
out[o++] = (byte)(half >> 8);
11311+
out[o++] = (byte)half;
11312+
XMEMCPY(out + o, msg + oct + 4, half);
11313+
o += half;
11314+
out[o++] = ASN_OCTET_STRING;
11315+
out[o++] = ASN_LONG_LENGTH | 2;
11316+
out[o++] = (byte)((contentSz - half) >> 8);
11317+
out[o++] = (byte)(contentSz - half);
11318+
XMEMCPY(out + o, msg + oct + 4 + half, contentSz - half);
11319+
o += contentSz - half;
11320+
XMEMCPY(out + o, msg + oct + 4 + contentSz,
11321+
(word32)msgSz - oct - 4 - contentSz);
11322+
o += (word32)msgSz - oct - 4 - contentSz;
11323+
return (int)o;
11324+
}
11325+
#endif
11326+
11327+
/* An input inside a buffer the object owns is refused, and a content pointer
11328+
* left inside one is dropped. */
11329+
int test_wc_PKCS7_VerifySignedData_NestedSameObject(void)
11330+
{
11331+
EXPECT_DECLS;
11332+
#if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_SHA256) && \
11333+
defined(USE_CERT_BUFFERS_2048)
11334+
byte data[] = { 0x01, 0x02, 0x03, 0x04 };
11335+
PKCS7* pkcs7 = NULL;
11336+
WC_RNG rng;
11337+
byte* inner = NULL;
11338+
byte* outer = NULL;
11339+
byte* split = NULL;
11340+
byte* in = NULL;
11341+
int innerSz = 0;
11342+
int outerSz = 0;
11343+
int splitSz = 0;
11344+
byte hash[WC_SHA256_DIGEST_SIZE];
11345+
word32 inSz = 0;
11346+
word32 headSz = 0;
11347+
word32 footSz = 0;
11348+
word32 msgMax = 2 * sizeof_client_cert_der_2048 + FOURK_BUF;
11349+
11350+
XMEMSET(&rng, 0, sizeof(WC_RNG));
11351+
ExpectIntEQ(wc_InitRng(&rng), 0);
11352+
ExpectNotNull(inner = (byte*)XMALLOC(msgMax, HEAP_HINT,
11353+
DYNAMIC_TYPE_TMP_BUFFER));
11354+
ExpectNotNull(outer = (byte*)XMALLOC(msgMax, HEAP_HINT,
11355+
DYNAMIC_TYPE_TMP_BUFFER));
11356+
ExpectNotNull(split = (byte*)XMALLOC(msgMax + 8, HEAP_HINT,
11357+
DYNAMIC_TYPE_TMP_BUFFER));
11358+
if (EXPECT_SUCCESS()) {
11359+
ExpectIntGT((innerSz = pkcs7_sign_client(inner, msgMax, data,
11360+
sizeof(data), 0, &rng)), 0);
11361+
}
11362+
if (EXPECT_SUCCESS()) {
11363+
ExpectIntGT((outerSz = pkcs7_sign_client(outer, msgMax, inner,
11364+
(word32)innerSz, 0, &rng)), 0);
11365+
}
11366+
if (EXPECT_SUCCESS()) {
11367+
ExpectIntGT((splitSz = pkcs7_split_content(split, outer, outerSz,
11368+
(word32)innerSz)), 0);
11369+
}
11370+
11371+
/* content in the caller's buffer without streaming, else in the stream */
11372+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
11373+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
11374+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, outer, (word32)outerSz), 0);
11375+
if (pkcs7 != NULL) {
11376+
in = pkcs7->content;
11377+
inSz = pkcs7->contentSz;
11378+
pkcs7->content = NULL;
11379+
pkcs7->contentSz = 0;
11380+
}
11381+
ExpectIntEQ(inSz, (word32)innerSz);
11382+
#ifdef NO_PKCS7_STREAM
11383+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, in, inSz), 0);
11384+
if (pkcs7 != NULL) {
11385+
ExpectIntEQ(pkcs7->contentSz, sizeof(data));
11386+
}
11387+
#else
11388+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, in + 1, inSz - 1),
11389+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11390+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, in, inSz),
11391+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11392+
if (pkcs7 != NULL) {
11393+
pkcs7->content = in + 1;
11394+
pkcs7->contentSz = 1;
11395+
}
11396+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, outer, (word32)outerSz), 0);
11397+
if (pkcs7 != NULL) {
11398+
ExpectIntEQ(pkcs7->contentSz, (word32)innerSz);
11399+
}
11400+
#endif
11401+
wc_PKCS7_Free(pkcs7);
11402+
pkcs7 = NULL;
11403+
11404+
/* content joined from two parts */
11405+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
11406+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
11407+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, split, (word32)splitSz), 0);
11408+
if (pkcs7 != NULL) {
11409+
in = pkcs7->content;
11410+
inSz = pkcs7->contentSz;
11411+
pkcs7->content = NULL;
11412+
pkcs7->contentSz = 0;
11413+
}
11414+
ExpectIntEQ(inSz, (word32)innerSz);
11415+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, in + 1, inSz - 1),
11416+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11417+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, in, inSz),
11418+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11419+
if (pkcs7 != NULL) {
11420+
pkcs7->content = in + 1;
11421+
pkcs7->contentSz = 1;
11422+
}
11423+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, split, (word32)splitSz), 0);
11424+
if (pkcs7 != NULL) {
11425+
ExpectIntEQ(pkcs7->contentSz, (word32)innerSz);
11426+
}
11427+
11428+
/* header and footer verify, with contentSz set by the caller */
11429+
ExpectIntEQ(wc_Sha256Hash(data, sizeof(data), hash), 0);
11430+
if (EXPECT_SUCCESS()) {
11431+
PKCS7* signer = NULL;
11432+
11433+
headSz = msgMax;
11434+
footSz = msgMax;
11435+
ExpectNotNull(signer = wc_PKCS7_New(HEAP_HINT, testDevId));
11436+
ExpectIntEQ(wc_PKCS7_InitWithCert(signer,
11437+
(byte*)client_cert_der_2048, sizeof_client_cert_der_2048), 0);
11438+
if (signer != NULL) {
11439+
signer->contentSz = sizeof(data);
11440+
signer->contentOID = DATA;
11441+
signer->hashOID = SHA256h;
11442+
signer->encryptOID = RSAk;
11443+
signer->privateKey = (byte*)client_key_der_2048;
11444+
signer->privateKeySz = sizeof_client_key_der_2048;
11445+
signer->rng = &rng;
11446+
}
11447+
ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(signer, hash, sizeof(hash),
11448+
outer, &headSz, inner, &footSz), 0);
11449+
wc_PKCS7_Free(signer);
11450+
}
11451+
if (pkcs7 != NULL) {
11452+
pkcs7->contentSz = sizeof(data);
11453+
}
11454+
ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hash, sizeof(hash), outer,
11455+
headSz, inner, footSz), 0);
11456+
11457+
wc_PKCS7_Free(pkcs7);
11458+
XFREE(split, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
11459+
XFREE(outer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
11460+
XFREE(inner, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
11461+
wc_FreeRng(&rng);
11462+
#endif
11463+
return EXPECT_RESULT();
11464+
}

‎tests/api/test_pkcs7.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ int test_wc_PKCS7_VerifySignedData_StreamOutWithCerts(void);
116116
int test_wc_PKCS7_VerifySignedData_ReuseAfterBadCertSet(void);
117117
int test_wc_PKCS7_VerifySignedData_DegenerateClearsVerifyCert(void);
118118
int test_wc_PKCS7_VerifySignedData_ReuseAfterBer(void);
119+
int test_wc_PKCS7_VerifySignedData_NestedSameObject(void);
119120

120121

121122
#define TEST_PKCS7_DECLS \
@@ -194,7 +195,8 @@ int test_wc_PKCS7_VerifySignedData_ReuseAfterBer(void);
194195
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_StreamOutWithCerts), \
195196
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_ReuseAfterBadCertSet), \
196197
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_DegenerateClearsVerifyCert), \
197-
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_ReuseAfterBer)
198+
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_ReuseAfterBer), \
199+
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_NestedSameObject)
198200

199201
#define TEST_PKCS7_ENCRYPTED_DATA_DECLS \
200202
TEST_DECL_GROUP("pkcs7_ed", test_wc_PKCS7_DecodeEnvelopedData_stream), \

0 commit comments

Comments
 (0)