Skip to content

Commit dae7c7c

Browse files
committed
asn: reject trailing bytes after the last PolicyInformation in certificatePolicies
1 parent 459b883 commit dae7c7c

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

tests/api/test_asn.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,34 @@ int test_DecodeCertExtensions_empty_certpol(void)
16031603
return EXPECT_RESULT();
16041604
}
16051605

1606+
/* Trailing bytes after the last PolicyInformation must be rejected rather than
1607+
* skipped. */
1608+
int test_DecodeCertExtensions_certpol_trailing_junk(void)
1609+
{
1610+
EXPECT_DECLS;
1611+
#if (defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT)) && \
1612+
!defined(NO_CERTS) && !defined(NO_ASN)
1613+
/* One valid PolicyInformation followed by two bytes that are not one. */
1614+
static const byte trailingJunk[] = {
1615+
0x30, 0x09, /* certificatePolicies SEQUENCE */
1616+
0x30, 0x05, /* PolicyInformation SEQUENCE */
1617+
0x06, 0x03, 0x2A, 0x03, 0x04,/* policyIdentifier OID 1.2.3.4 */
1618+
0x00, 0x00 /* trailing junk */
1619+
};
1620+
DecodedCert cert;
1621+
int isUnknown = 0;
1622+
1623+
wc_InitDecodedCert(&cert, trailingJunk, (word32)sizeof(trailingJunk), NULL);
1624+
1625+
ExpectIntEQ(DecodeExtensionType(trailingJunk, (word32)sizeof(trailingJunk),
1626+
CERT_POLICY_OID, 0, &cert, &isUnknown),
1627+
WC_NO_ERR_TRACE(ASN_PARSE_E));
1628+
1629+
wc_FreeDecodedCert(&cert);
1630+
#endif
1631+
return EXPECT_RESULT();
1632+
}
1633+
16061634
int test_ParseCert_SM3wSM2_short_pubkey(void)
16071635
{
16081636
EXPECT_DECLS;

tests/api/test_asn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int test_SerialNumber0_RootCA(void);
3737
int test_DecodeAltNames_length_underflow(void);
3838
int test_DecodeCertExtensions_dup_certpol(void);
3939
int test_DecodeCertExtensions_empty_certpol(void);
40+
int test_DecodeCertExtensions_certpol_trailing_junk(void);
4041
int test_ParseCert_SM3wSM2_short_pubkey(void);
4142
int test_ParseCert_dnBufferBoundary(void);
4243
int test_wc_DecodeObjectId(void);
@@ -63,6 +64,7 @@ int test_wc_AsnFeatureCoverage(void);
6364
TEST_DECL_GROUP("asn", test_DecodeAltNames_length_underflow), \
6465
TEST_DECL_GROUP("asn", test_DecodeCertExtensions_dup_certpol), \
6566
TEST_DECL_GROUP("asn", test_DecodeCertExtensions_empty_certpol), \
67+
TEST_DECL_GROUP("asn", test_DecodeCertExtensions_certpol_trailing_junk), \
6668
TEST_DECL_GROUP("asn", test_ParseCert_SM3wSM2_short_pubkey), \
6769
TEST_DECL_GROUP("asn", test_ParseCert_dnBufferBoundary), \
6870
TEST_DECL_GROUP("asn", test_wc_DecodeObjectId), \

wolfcrypt/src/asn.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21487,8 +21487,9 @@ static int DecodeCertPolicy(const byte* input, word32 sz, DecodedCert* cert)
2148721487
}
2148821488
}
2148921489

21490-
/* Unwrap certificatePolicies */
21491-
while ((ret == 0) && ((int)idx < total_length)
21490+
/* Unwrap certificatePolicies. idx is an offset into input, so it is bound
21491+
* by sz. */
21492+
while ((ret == 0) && (idx < sz)
2149221493
#if defined(WOLFSSL_CERT_EXT)
2149321494
&& (cert->extCertPoliciesNb < MAX_CERTPOL_NB)
2149421495
#endif

0 commit comments

Comments
 (0)