Skip to content

Commit e452870

Browse files
committed
reject crls with unrecognized critical entry extensions per rfc 5280 section 5.3
1 parent 353a379 commit e452870

7 files changed

Lines changed: 171 additions & 51 deletions

File tree

certs/crl/claim-root.pem

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDGzCCAgOgAwIBAgIUHiXBXW8CIaDwFBWcO00dcxYA5FEwDQYJKoZIhvcNAQEL
3+
BQAwFTETMBEGA1UEAwwKY2xhaW0tcm9vdDAeFw0yNjA0MTYxMTM4NTVaFw0zNjA0
4+
MTMxMTM4NTVaMBUxEzARBgNVBAMMCmNsYWltLXJvb3QwggEiMA0GCSqGSIb3DQEB
5+
AQUAA4IBDwAwggEKAoIBAQC6SYy1F8EBJG9WGqk7A+KfJLElmPs4gnhUpx9ph+SW
6+
G4EYELDAW0u/uB307nUPtUVycM5lhEQ+MHjE8+y6lnikZfxijfUp+Xw9eGwdSkzJ
7+
FS0iEOqTJrimF9MOvAyrg2P2HMyDcyl+f4N/vWOqjfp4hdI+YJVajfqPzZQ/EyjZ
8+
0IcoF3jiYY15lwGpfITAHL5fXcooa17dg6VVNBG6+ouSo96287qrfxpn/W8ghUx3
9+
p46+uPiPcONa03fJnhBgtNsMxQXhH73mee6CP1F24n9cEW3TIWnsBRGLyDmzLKaD
10+
tG+sGdZqZQ0IlFjePckMVANzDI0kCfOxXdLj61bWKGZbAgMBAAGjYzBhMA8GA1Ud
11+
EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMB0GA1UdDgQWBBRSl1hHmMr4maB+
12+
jhw4Luq76pt0MDAfBgNVHSMEGDAWgBRSl1hHmMr4maB+jhw4Luq76pt0MDANBgkq
13+
hkiG9w0BAQsFAAOCAQEAfTDUagGJO2LtFkZZD/I7td6JCBdoy0bcOabLVrCR6wOy
14+
FcQ7TWNVIgom5mRG6A+o897hQ1Tm14r0T6tWkxJxSyVxCjEYee5FpPVyZ/pB2YeX
15+
Ce9VrW9HHVqy6fciBS1agajoU7CU9mP/P1F6CKwnmlcRIqQAhHCGdjkPT1fPjpTS
16+
jkPA1TR99aFFHrfIfnz+XU1TQyUVnggBVqT/eVklySOYrwWvwQsp8eLENjGR+vK7
17+
Euhn+cehXoztkhKjK+HC4aCwDhKn0KKu1vowIQ9z/iQhXwOGac3sdhjh/bZkkKYG
18+
LhlAk1A35JDjHweu+4nD9sSQq0BnTEMsorA+YRZpjw==
19+
-----END CERTIFICATE-----

certs/crl/crl_critical_entry.pem

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN X509 CRL-----
2+
MIIBjDB2AgEBMA0GCSqGSIb3DQEBCwUAMBUxEzARBgNVBAMMCmNsYWltLXJvb3QX
3+
DTI2MDQyMTEyMzM0OFoXDTM2MDQxODEyMzM0OFowLTArAgIQABcNMjYwNDIxMTIz
4+
MzQ4WjAWMBQGA1UdAQEB/wQKDAhvYnNvbGV0ZTANBgkqhkiG9w0BAQsFAAOCAQEA
5+
PPG5bodrM2jK+6KcqRh9vEkWhLyxCkJij1om7R8BMO5bpvxZFOSqdN9GvIqYANYT
6+
ZNQzZZOer9DSXc1I9Cha179dyGnBsX33geSdhF99JxA/kZUVynfOn56El7iywkPQ
7+
yEpuY7uQQRfcp7D7tGz8S7mNhOMAeaN+jR2Psmn8q1Yc3W01vwYZos3qjHG1xDZ6
8+
97QVFX6tbnbeu/hBsF87oiAyCbXnhRQOvrVsKqjrZXGkfRKfJ1deHc1vWgio7fhr
9+
A1/Hb4sdfTWZUwtduTRI9Vyaw1IL41d+0ExdzTshIHbddB/dyByrRqlIzJdeiigt
10+
d1Gcf1EYSoWiV8Xaj9SFng==
11+
-----END X509 CRL-----

certs/crl/include.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ EXTRA_DIST += \
1818
certs/crl/crl2.der \
1919
certs/crl/crl_rsapss.pem \
2020
certs/crl/crl_reason.pem \
21-
certs/crl/bad_time_fmt.pem
21+
certs/crl/bad_time_fmt.pem \
22+
certs/crl/claim-root.pem \
23+
certs/crl/crl_critical_entry.pem
2224

2325
EXTRA_DIST += \
2426
certs/crl/crl.revoked \

tests/api/test_certman.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,31 @@ int test_wolfSSL_CRL_unknown_critical_ext(void)
24272427
return EXPECT_RESULT();
24282428
}
24292429

2430+
int test_wolfSSL_CRL_unknown_critical_entry_ext(void)
2431+
{
2432+
EXPECT_DECLS;
2433+
#if !defined(NO_CERTS) && defined(HAVE_CRL) && !defined(NO_RSA) && \
2434+
!defined(NO_FILESYSTEM)
2435+
WOLFSSL_CERT_MANAGER* cm = NULL;
2436+
2437+
ExpectNotNull(cm = wolfSSL_CertManagerNew());
2438+
ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
2439+
"./certs/crl/claim-root.pem", NULL), WOLFSSL_SUCCESS);
2440+
ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL),
2441+
WOLFSSL_SUCCESS);
2442+
2443+
/* CRL with a revoked entry that carries a critical unknown extension
2444+
* (OID 2.5.29.1, old X.509v2 AKI, permanently superseded).
2445+
* Per RFC 5280 Section 5.3, the CRL must not be used. */
2446+
ExpectIntNE(wolfSSL_CertManagerLoadCRLFile(cm,
2447+
"./certs/crl/crl_critical_entry.pem", WOLFSSL_FILETYPE_PEM),
2448+
WOLFSSL_SUCCESS);
2449+
2450+
wolfSSL_CertManagerFree(cm);
2451+
#endif
2452+
return EXPECT_RESULT();
2453+
}
2454+
24302455
int test_wolfSSL_CertManagerCheckOCSPResponse(void)
24312456
{
24322457
EXPECT_DECLS;

tests/api/test_certman.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int test_wolfSSL_CRL_static_revoked_list(void);
4242
int test_wolfSSL_CRL_duplicate_extensions(void);
4343
int test_wolfSSL_CRL_critical_idp(void);
4444
int test_wolfSSL_CRL_unknown_critical_ext(void);
45+
int test_wolfSSL_CRL_unknown_critical_entry_ext(void);
4546
int test_wolfSSL_CertManagerCheckOCSPResponse(void);
4647
int test_various_pathlen_chains(void);
4748
int test_wolfSSL_CertManagerRejectMD5Cert(void);
@@ -65,6 +66,7 @@ int test_wolfSSL_CertManagerRejectMD5Cert(void);
6566
TEST_DECL_GROUP("certman", test_wolfSSL_CRL_duplicate_extensions), \
6667
TEST_DECL_GROUP("certman", test_wolfSSL_CRL_critical_idp), \
6768
TEST_DECL_GROUP("certman", test_wolfSSL_CRL_unknown_critical_ext), \
69+
TEST_DECL_GROUP("certman", test_wolfSSL_CRL_unknown_critical_entry_ext), \
6870
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerCheckOCSPResponse), \
6971
TEST_DECL_GROUP("certman", test_various_pathlen_chains), \
7072
TEST_DECL_GROUP("certman", test_wolfSSL_CertManagerRejectMD5Cert)

wolfcrypt/src/asn.c

Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34106,40 +34106,62 @@ enum {
3410634106
/* CRL Reason Code OID: 2.5.29.21 */
3410734107
static const byte crlReasonOid[] = { 0x55, 0x1d, 0x15 };
3410834108

34109-
/* Parse CRL entry extensions to extract the reason code.
34110-
* Sets *reasonCode if found, otherwise leaves it unchanged. */
34111-
static void ParseCRL_ReasonCode(const byte* buff, word32 idx, word32 maxIdx,
34112-
int* reasonCode)
34109+
/* Parse CRL entry extensions.
34110+
* Extracts the reason code into *reasonCode if the CRL Reason extension
34111+
* is present. Per RFC 5280 Section 5.3, returns ASN_CRIT_EXT_E if any
34112+
* unknown extension is marked critical. Returns 0 on success. */
34113+
static int ParseCRL_EntryExtensions(const byte* buff, word32 idx, word32 maxIdx,
34114+
int* reasonCode)
3411334115
{
3411434116
while (idx < maxIdx) {
3411534117
int len;
34118+
int oidLen;
3411634119
word32 end;
3411734120
word32 localIdx;
34121+
word32 oidContent;
3411834122
byte tag;
34123+
int critical = 0;
34124+
int isReasonOid = 0;
3411934125

3412034126
/* Each extension is a SEQUENCE */
3412134127
if (GetSequence(buff, &idx, &len, maxIdx) < 0) {
3412234128
break;
3412334129
}
3412434130
end = idx + (word32)len;
3412534131

34126-
/* Check for CRL Reason OID: 2.5.29.21 */
34127-
if (end - idx >= (word32)(2 + sizeof(crlReasonOid)) &&
34128-
buff[idx] == ASN_OBJECT_ID &&
34129-
buff[idx + 1] == sizeof(crlReasonOid) &&
34130-
XMEMCMP(buff + idx + 2, crlReasonOid,
34132+
/* Parse OID: tag, length (short or long form), content */
34133+
if (GetASNTag(buff, &idx, &tag, end) < 0 ||
34134+
tag != ASN_OBJECT_ID) {
34135+
break;
34136+
}
34137+
if (GetLength(buff, &idx, &oidLen, end) < 0) {
34138+
break;
34139+
}
34140+
oidContent = idx;
34141+
if (idx + (word32)oidLen > end) {
34142+
break;
34143+
}
34144+
34145+
/* Check if it's the CRL Reason OID: 2.5.29.21 */
34146+
if ((word32)oidLen == sizeof(crlReasonOid) &&
34147+
XMEMCMP(buff + oidContent, crlReasonOid,
3413134148
sizeof(crlReasonOid)) == 0) {
34132-
/* Skip past the OID */
34133-
idx += 2 + (word32)sizeof(crlReasonOid);
34134-
/* Skip optional critical BOOLEAN */
34135-
localIdx = idx;
34136-
if (GetASNTag(buff, &localIdx, &tag, end) == 0 &&
34137-
tag == ASN_BOOLEAN) {
34138-
/* Consume full BOOLEAN TLV (tag + length + value). */
34139-
if (GetBoolean(buff, &idx, end) < 0) {
34140-
break;
34141-
}
34149+
isReasonOid = 1;
34150+
}
34151+
idx = oidContent + (word32)oidLen;
34152+
34153+
/* Parse optional critical BOOLEAN */
34154+
localIdx = idx;
34155+
if (GetASNTag(buff, &localIdx, &tag, end) == 0 &&
34156+
tag == ASN_BOOLEAN) {
34157+
int ret = GetBoolean(buff, &idx, end);
34158+
if (ret < 0) {
34159+
break;
3414234160
}
34161+
critical = ret;
34162+
}
34163+
34164+
if (isReasonOid) {
3414334165
/* Get OCTET STRING wrapping the ENUMERATED */
3414434166
if (GetOctetString(buff, &idx, &len, end) >= 0) {
3414534167
/* Parse ENUMERATED reason value */
@@ -34155,8 +34177,15 @@ static void ParseCRL_ReasonCode(const byte* buff, word32 idx, word32 maxIdx,
3415534177
}
3415634178
}
3415734179
}
34180+
else if (critical) {
34181+
/* RFC 5280 Section 5.3: reject CRL with unknown critical
34182+
* entry extension. */
34183+
WOLFSSL_MSG("Unknown critical CRL entry extension");
34184+
return ASN_CRIT_EXT_E;
34185+
}
3415834186
idx = end;
3415934187
}
34188+
return 0;
3416034189
}
3416134190

3416234191
#ifdef HAVE_CRL
@@ -34169,8 +34198,7 @@ WOLFSSL_TEST_VIS int wc_ParseCRLReasonFromExtensions(const byte* ext,
3416934198
return BAD_FUNC_ARG;
3417034199
}
3417134200

34172-
ParseCRL_ReasonCode(ext, 0, extSz, reasonCode);
34173-
return 0;
34201+
return ParseCRL_EntryExtensions(ext, 0, extSz, reasonCode);
3417434202
}
3417534203
#endif
3417634204

@@ -34233,49 +34261,58 @@ static int GetRevoked(RevokedCert* rcert, const byte* buff, word32* idx,
3423334261
/* Parse CRL entry extensions (v2 only) */
3423434262
if (dataASN[REVOKEDASN_IDX_TIME_EXT].length > 0) {
3423534263
word32 extOff = dataASN[REVOKEDASN_IDX_TIME_EXT].offset;
34236-
word32 extLen = dataASN[REVOKEDASN_IDX_TIME_EXT].length;
34237-
word32 extEnd = extOff + extLen;
34238-
word32 extIdx2 = extOff;
34264+
word32 extTagEnd = extOff +
34265+
dataASN[REVOKEDASN_IDX_TIME_EXT].length + 6;
34266+
int extLen;
34267+
34268+
/* .offset points at the outer SEQUENCE tag. Re-parse the
34269+
* SEQUENCE header to locate the content start (list of
34270+
* Extension SEQUENCEs), which handles long-form length.
34271+
* extTagEnd adds 6 to cover the worst-case tag+long-form-length
34272+
* header for the outer SEQUENCE. */
34273+
if (GetSequence(buff, &extOff, &extLen, extTagEnd) < 0) {
34274+
ret = ASN_PARSE_E;
34275+
}
34276+
else {
34277+
word32 extEnd = extOff + (word32)extLen;
3423934278

3424034279
#if defined(OPENSSL_EXTRA)
34241-
/* Store raw DER of extensions for OpenSSL compat API.
34242-
* Include the outer SEQUENCE tag+length. */
34243-
{
34244-
/* Back up to include the SEQUENCE header. We know the
34245-
* content starts at extOff, so the header is just before.
34246-
* Use the raw buffer start from before GetASN_Items. */
34247-
word32 seqHdrSz = 0;
34248-
/* The outer SEQUENCE header is at most 4 bytes before
34249-
* content. Rather than guess, store just the content. */
34250-
rc->extensions = (byte*)XMALLOC(extLen, dcrl->heap,
34280+
/* Store raw DER of extension contents for OpenSSL compat. */
34281+
rc->extensions = (byte*)XMALLOC((size_t)extLen, dcrl->heap,
3425134282
DYNAMIC_TYPE_REVOKED);
3425234283
if (rc->extensions != NULL) {
34253-
XMEMCPY(rc->extensions, buff + extOff, extLen);
34254-
rc->extensionsSz = extLen;
34284+
XMEMCPY(rc->extensions, buff + extOff, (size_t)extLen);
34285+
rc->extensionsSz = (word32)extLen;
3425534286
}
34256-
(void)seqHdrSz;
34257-
}
3425834287
#endif
3425934288

34260-
ParseCRL_ReasonCode(buff, extIdx2, extEnd, &rc->reasonCode);
34289+
ret = ParseCRL_EntryExtensions(buff, extOff, extEnd,
34290+
&rc->reasonCode);
34291+
}
3426134292
}
3426234293

34263-
/* Add revoked certificate to chain. */
34294+
if (ret == 0) {
34295+
/* Add revoked certificate to chain. */
3426434296
#ifndef CRL_STATIC_REVOKED_LIST
34265-
rc->next = dcrl->certs;
34266-
dcrl->certs = rc;
34297+
rc->next = dcrl->certs;
34298+
dcrl->certs = rc;
3426734299
#endif
34268-
dcrl->totalCerts++;
34300+
dcrl->totalCerts++;
34301+
}
3426934302
}
3427034303

3427134304
FREE_ASNGETDATA(dataASN, dcrl->heap);
34272-
#ifndef CRL_STATIC_REVOKED_LIST
3427334305
if ((ret != 0) && (rc != NULL)) {
3427434306
#if defined(OPENSSL_EXTRA)
3427534307
XFREE(rc->extensions, dcrl->heap, DYNAMIC_TYPE_REVOKED);
34308+
rc->extensions = NULL;
34309+
rc->extensionsSz = 0;
3427634310
#endif
34311+
#ifndef CRL_STATIC_REVOKED_LIST
3427734312
XFREE(rc, dcrl->heap, DYNAMIC_TYPE_CRL);
34313+
#endif
3427834314
}
34315+
#ifndef CRL_STATIC_REVOKED_LIST
3427934316
(void)rcert;
3428034317
#endif
3428134318
return ret;
@@ -34299,7 +34336,13 @@ static int ParseCRL_RevokedCerts(RevokedCert* rcert, DecodedCRL* dcrl,
3429934336
/* Parse each revoked certificate. */
3430034337
while ((ret == 0) && (idx < maxIdx)) {
3430134338
/* Parse a revoked certificate. */
34302-
if (GetRevoked(rcert, buff, &idx, dcrl, maxIdx) < 0) {
34339+
int r = GetRevoked(rcert, buff, &idx, dcrl, maxIdx);
34340+
if (r == WC_NO_ERR_TRACE(ASN_CRIT_EXT_E)) {
34341+
/* Preserve the specific error so callers can distinguish a
34342+
* rejected critical extension from a generic parse failure. */
34343+
ret = r;
34344+
}
34345+
else if (r < 0) {
3430334346
ret = ASN_PARSE_E;
3430434347
}
3430534348
}

wolfcrypt/src/asn_orig.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9061,18 +9061,17 @@ static int GetRevoked(RevokedCert* rcert, const byte* buff, word32* idx,
90619061
XFREE(rc, dcrl->heap, DYNAMIC_TYPE_REVOKED);
90629062
return ret;
90639063
}
9064-
/* add to list */
9065-
rc->next = dcrl->certs;
9066-
dcrl->certs = rc;
90679064

90689065
(void)rcert;
90699066
#endif /* CRL_STATIC_REVOKED_LIST */
9070-
dcrl->totalCerts++;
90719067
/* get date */
90729068
#ifndef NO_ASN_TIME
90739069
ret = GetBasicDate(buff, idx, rc->revDate, &rc->revDateFormat, maxIdx);
90749070
if (ret < 0) {
90759071
WOLFSSL_MSG("Expecting Date");
9072+
#ifndef CRL_STATIC_REVOKED_LIST
9073+
XFREE(rc, dcrl->heap, DYNAMIC_TYPE_REVOKED);
9074+
#endif
90769075
return ret;
90779076
}
90789077
#endif
@@ -9106,11 +9105,30 @@ static int GetRevoked(RevokedCert* rcert, const byte* buff, word32* idx,
91069105
}
91079106
#endif
91089107

9109-
ParseCRL_ReasonCode(buff, seqIdx, extEnd, &rc->reasonCode);
9108+
ret = ParseCRL_EntryExtensions(buff, seqIdx, extEnd,
9109+
&rc->reasonCode);
9110+
if (ret != 0) {
9111+
#if defined(OPENSSL_EXTRA)
9112+
XFREE(rc->extensions, dcrl->heap, DYNAMIC_TYPE_REVOKED);
9113+
rc->extensions = NULL;
9114+
rc->extensionsSz = 0;
9115+
#endif
9116+
#ifndef CRL_STATIC_REVOKED_LIST
9117+
XFREE(rc, dcrl->heap, DYNAMIC_TYPE_REVOKED);
9118+
#endif
9119+
return ret;
9120+
}
91109121
}
91119122
}
91129123
}
91139124

9125+
#ifndef CRL_STATIC_REVOKED_LIST
9126+
/* add to list only after all parsing succeeded */
9127+
rc->next = dcrl->certs;
9128+
dcrl->certs = rc;
9129+
#endif
9130+
dcrl->totalCerts++;
9131+
91149132
*idx = end;
91159133

91169134
return 0;

0 commit comments

Comments
 (0)