Skip to content

Commit 76dbca6

Browse files
committed
Fix out-of-bounds read printing CRL authority key id
1 parent 04dae03 commit 76dbca6

10 files changed

Lines changed: 173 additions & 11 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Certificate Revocation List (CRL):
2+
Version 2 (0x1)
3+
Signature Algorithm: sha256WithRSAEncryption
4+
Issuer: CN=wolfSSL AKID Zero Octets CA
5+
Last Update: Nov 18 15:40:41 2026 GMT
6+
Next Update: Nov 15 15:40:41 2036 GMT
7+
CRL extensions:
8+
X509v3 Authority Key Identifier:
9+
00:01:00:03:04:05:06:00
10+
X509v3 CRL Number:
11+
1
12+
No Revoked Certificates.
13+
Signature Algorithm: sha256WithRSAEncryption
14+
Signature Value:
15+
be:8e:9b:0d:c7:c2:62:c8:de:20:6b:b6:d6:a9:44:71:ac:c9:
16+
51:8a:86:57:c9:be:80:ff:eb:87:a0:06:1e:76:79:a5:40:08:
17+
6b:74:9d:e7:58:e8:1c:15:11:d1:00:8d:5c:19:d9:0c:a9:e7:
18+
70:ba:d4:c1:8b:42:29:ef:58:84:31:63:a5:c2:67:6a:4d:d5:
19+
10:a0:4e:4a:35:ad:3f:83:65:8d:14:73:db:88:ec:ef:38:4b:
20+
60:a4:eb:af:2e:de:12:b6:cc:9d:d0:62:c2:03:c7:a9:f2:b4:
21+
1a:84:99:a3:0f:ee:52:6e:05:07:ac:df:ce:7b:c6:d6:5d:3a:
22+
42:0c:dd:e4:79:6c:f7:50:b7:8d:41:90:d0:bd:6a:c3:c4:65:
23+
38:e5:4c:da:dd:6f:c7:ea:37:50:fa:a3:41:3d:f4:b4:1c:87:
24+
a2:d4:c3:5b:1a:7a:a9:f6:89:b3:fd:08:f5:1e:8f:05:09:f9:
25+
f1:e3:c4:06:77:9e:d3:20:d2:61:32:ec:91:b3:00:ad:ed:37:
26+
22:58:c4:52:3b:6c:32:02:88:9c:ca:73:b0:93:f2:0d:c7:1e:
27+
74:e9:44:5a:21:0e:fc:1a:2b:cc:c4:ad:66:81:eb:d0:4a:a9:
28+
2d:e5:f3:9f:b3:b5:f0:af:bc:1a:09:8d:1a:77:8d:3b:99:db:
29+
09:7d:32:29
30+
-----BEGIN X509 CRL-----
31+
MIIBkzB9AgEBMA0GCSqGSIb3DQEBCwUAMCYxJDAiBgNVBAMMG3dvbGZTU0wgQUtJ
32+
RCBaZXJvIE9jdGV0cyBDQRcNMjYxMTE4MTU0MDQxWhcNMzYxMTE1MTU0MDQxWqAj
33+
MCEwEwYDVR0jBAwwCoAIAAEAAwQFBgAwCgYDVR0UBAMCAQEwDQYJKoZIhvcNAQEL
34+
BQADggEBAL6Omw3HwmLI3iBrttapRHGsyVGKhlfJvoD/64egBh52eaVACGt0nedY
35+
6BwVEdEAjVwZ2Qyp53C61MGLQinvWIQxY6XCZ2pN1RCgTko1rT+DZY0Uc9uI7O84
36+
S2Ck668u3hK2zJ3QYsIDx6nytBqEmaMP7lJuBQes3857xtZdOkIM3eR5bPdQt41B
37+
kNC9asPEZTjlTNrdb8fqN1D6o0E99LQch6LUw1saeqn2ibP9CPUejwUJ+fHjxAZ3
38+
ntMg0mEy7JGzAK3tNyJYxFI7bDICiJzKc7CT8g3HHnTpRFohDvwaK8zErWaB69BK
39+
qS3l85+ztfCvvBoJjRp3jTuZ2wl9Mik=
40+
-----END X509 CRL-----

‎certs/crl/gencrls.sh‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,42 @@ check_result $?
351351
mv tmp crl_reason.pem
352352
cp blank.index.txt demoCA/index.txt
353353

354+
# CRL whose 8 octet authority key id has leading, interior and trailing zeros
355+
echo "Step 40 zero octet authority key id CRL"
356+
cat > akid.cnf <<'EOF'
357+
[ ca ]
358+
default_ca = CA_default
359+
[ CA_default ]
360+
database = ./demoCA/index.txt
361+
crlnumber = ./crlnumber
362+
default_md = sha256
363+
crl_extensions = crl_ext
364+
[ crl_ext ]
365+
authorityKeyIdentifier = keyid:always
366+
[ req ]
367+
distinguished_name = dn
368+
prompt = no
369+
[ dn ]
370+
CN = wolfSSL AKID Zero Octets CA
371+
[ akid_ca ]
372+
basicConstraints = critical,CA:true
373+
keyUsage = critical,cRLSign,keyCertSign
374+
subjectKeyIdentifier = 00:01:00:03:04:05:06:00
375+
EOF
376+
echo "01" > crlnumber
377+
openssl req -new -x509 -config akid.cnf -extensions akid_ca \
378+
-key ../ca-key.pem -days 3650 -out akid-ca.pem
379+
check_result $?
380+
openssl ca -config akid.cnf -gencrl -crldays 3650 -keyfile ../ca-key.pem \
381+
-cert akid-ca.pem -out extra-crls/akid_zero_octets.pem
382+
check_result $?
383+
384+
# metadata
385+
echo "Step 41"
386+
openssl crl -in extra-crls/akid_zero_octets.pem -text > tmp
387+
check_result $?
388+
mv tmp extra-crls/akid_zero_octets.pem
389+
rm -f akid.cnf akid-ca.pem
390+
cp blank.index.txt demoCA/index.txt
391+
354392
exit 0

‎certs/crl/include.am‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ EXTRA_DIST += \
2929
certs/crl/extra-crls/crlnum_57oct.pem \
3030
certs/crl/extra-crls/crlnum_64oct.pem \
3131
certs/crl/extra-crls/claim-root.pem \
32-
certs/crl/extra-crls/crl_critical_entry.pem
32+
certs/crl/extra-crls/crl_critical_entry.pem \
33+
certs/crl/extra-crls/akid_zero_octets.pem
3334

3435
# Intermediate cert CRL's
3536
EXTRA_DIST += \

‎src/crl.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,15 @@ static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl, const byte* buff,
253253
XMEMCPY(crle->signature, dcrl->signature, crle->signatureSz);
254254
#ifndef NO_SKID
255255
crle->extAuthKeyIdSet = dcrl->extAuthKeyIdSet;
256-
if (crle->extAuthKeyIdSet)
256+
if (crle->extAuthKeyIdSet) {
257257
XMEMCPY(crle->extAuthKeyId, dcrl->extAuthKeyId, KEYID_SIZE);
258+
/* Key id lies within the TBS, keep its position for printing. */
259+
if (dcrl->extAuthKeyIdRawIdx >= dcrl->certBegin) {
260+
crle->extAuthKeyIdRawIdx =
261+
dcrl->extAuthKeyIdRawIdx - dcrl->certBegin;
262+
crle->extAuthKeyIdRawSz = dcrl->extAuthKeyIdRawSz;
263+
}
264+
}
258265
#endif
259266
}
260267
else {
@@ -3200,6 +3207,11 @@ int wolfSSL_X509_CRL_sign(WOLFSSL_X509_CRL* crl, WOLFSSL_EVP_PKEY* pkey,
32003207
entry->signature = newSignature;
32013208
entry->signatureSz = newSignatureSz;
32023209
entry->signatureOID = newSignatureOid;
3210+
#if !defined(NO_SKID) && !defined(NO_ASN)
3211+
/* Re-encoded TBS has no authority key id extension. */
3212+
entry->extAuthKeyIdRawIdx = 0;
3213+
entry->extAuthKeyIdRawSz = 0;
3214+
#endif
32033215
}
32043216
else {
32053217
if (newToBeSigned != NULL) {

‎src/x509.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10802,12 +10802,26 @@ static int X509CRLPrintExtensions(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
1080210802

1080310803
#if !defined(NO_SKID)
1080410804
if (ret == 0 && crl->crlList->extAuthKeyIdSet &&
10805-
crl->crlList->extAuthKeyId[0] != 0) {
10805+
crl->crlList->extAuthKeyIdRawSz > 0) {
1080610806
word32 i;
1080710807
char val[5];
1080810808
int valSz = 5;
10809+
const byte* keyId = NULL;
10810+
word32 keyIdSz = crl->crlList->extAuthKeyIdRawSz;
10811+
10812+
/* extAuthKeyId is a normalized lookup key; print the encoded id. */
10813+
if (crl->crlList->toBeSigned == NULL ||
10814+
crl->crlList->extAuthKeyIdRawIdx > crl->crlList->tbsSz ||
10815+
keyIdSz > crl->crlList->tbsSz -
10816+
crl->crlList->extAuthKeyIdRawIdx) {
10817+
ret = WOLFSSL_FAILURE;
10818+
}
10819+
else {
10820+
keyId = crl->crlList->toBeSigned +
10821+
crl->crlList->extAuthKeyIdRawIdx;
10822+
}
1080910823

10810-
if (XSNPRINTF(tmp, MAX_WIDTH, "%*s%s", indent + 4, "",
10824+
if (ret == 0 && XSNPRINTF(tmp, MAX_WIDTH, "%*s%s", indent + 4, "",
1081110825
"X509v3 Authority Key Identifier:") >= MAX_WIDTH) {
1081210826
ret = WOLFSSL_FAILURE;
1081310827
}
@@ -10827,7 +10841,7 @@ static int X509CRLPrintExtensions(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
1082710841
}
1082810842

1082910843

10830-
for (i = 0; i < XSTRLEN((char*)crl->crlList->extAuthKeyId); i++) {
10844+
for (i = 0; ret == 0 && i < keyIdSz; i++) {
1083110845
/* check if buffer is almost full */
1083210846
if (ret == 0 && XSTRLEN(tmp) >= sizeof(tmp) - valSz) {
1083310847
if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
@@ -10836,7 +10850,7 @@ static int X509CRLPrintExtensions(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
1083610850
tmp[0] = '\0';
1083710851
}
1083810852
if (ret == 0 && XSNPRINTF(val, (size_t)valSz, ":%02X",
10839-
crl->crlList->extAuthKeyId[i]) >= valSz) {
10853+
keyId[i]) >= valSz) {
1084010854
WOLFSSL_MSG("buffer overrun");
1084110855
ret = WOLFSSL_FAILURE;
1084210856
}

‎tests/api.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35092,8 +35092,51 @@ static int test_wolfSSL_X509_CRL_print(void)
3509235092
ExpectNull(XSTRSTR(buf, "a\r\nb"));
3509335093

3509435094
X509_NAME_free(name);
35095+
X509_CRL_free(crl);
35096+
crl = NULL;
35097+
BIO_free(bio);
35098+
bio = NULL;
35099+
35100+
#ifndef NO_SKID
35101+
/* The encoded key id is printed whatever the internal KEYID_SIZE. An
35102+
* 8 octet id with leading, interior and trailing zeros. */
35103+
ExpectTrue((fp = XFOPEN("./certs/crl/extra-crls/akid_zero_octets.pem",
35104+
"rb")) != XBADFILE);
35105+
ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
35106+
NULL, NULL));
35107+
if (fp != XBADFILE) {
35108+
XFCLOSE(fp);
35109+
fp = XBADFILE;
35110+
}
35111+
35112+
ExpectNotNull(bio = BIO_new(BIO_s_mem()));
35113+
ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
35114+
ExpectIntGT(test_bio_mem_to_str(bio, buf, (int)sizeof(buf)), 0);
35115+
ExpectNotNull(XSTRSTR(buf, "keyid:00:01:00:03:04:05:06:00\n"));
35116+
35117+
X509_CRL_free(crl);
35118+
crl = NULL;
35119+
BIO_free(bio);
35120+
bio = NULL;
35121+
35122+
#if defined(HAVE_ECC) && !defined(NO_SHA256)
35123+
/* 20 octet id with no zero octets. */
35124+
ExpectTrue((fp = XFOPEN("./certs/crl/caEccCrl.pem", "rb")) != XBADFILE);
35125+
ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
35126+
NULL, NULL));
35127+
if (fp != XBADFILE)
35128+
XFCLOSE(fp);
35129+
35130+
ExpectNotNull(bio = BIO_new(BIO_s_mem()));
35131+
ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
35132+
ExpectIntGT(test_bio_mem_to_str(bio, buf, (int)sizeof(buf)), 0);
35133+
ExpectNotNull(XSTRSTR(buf, "keyid:56:8E:9A:C3:F0:42:DE:18:B9:45:55:"
35134+
"6E:F9:93:CF:EA:C3:F3:A5:21\n"));
35135+
3509535136
X509_CRL_free(crl);
3509635137
BIO_free(bio);
35138+
#endif /* HAVE_ECC && !NO_SHA256 */
35139+
#endif /* !NO_SKID */
3509735140
#endif
3509835141
return EXPECT_RESULT();
3509935142
}

‎wolfcrypt/src/asn.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4737,7 +4737,8 @@ static int DecodeBasicOcspResponse(byte* source, word32* ioIndex,
47374737
#if defined(HAVE_CRL) && !defined(WOLFCRYPT_ONLY)
47384738
static int GetRevoked(RevokedCert* rcert, const byte* buff, word32* idx, DecodedCRL* dcrl, word32 maxIdx);
47394739
#ifndef NO_SKID
4740-
static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl);
4740+
static int ParseCRL_AuthKeyIdExt(const byte* input, word32 inputIdx, int sz,
4741+
DecodedCRL* dcrl);
47414742
#endif
47424743
static int ParseCRL_Extensions(DecodedCRL* dcrl, const byte* buf, word32* inOutIdx, word32 sz);
47434744
#endif
@@ -39866,7 +39867,8 @@ static int PaseCRL_CheckSignature(DecodedCRL* dcrl, const byte* sigParams,
3986639867

3986739868
#ifndef NO_SKID
3986839869
#ifdef WOLFSSL_ASN_TEMPLATE
39869-
static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
39870+
static int ParseCRL_AuthKeyIdExt(const byte* input, word32 inputIdx, int sz,
39871+
DecodedCRL* dcrl)
3987039872
{
3987139873
DECL_ASNGETDATA(dataASN, authKeyIdASN_Length);
3987239874
int ret = 0;
@@ -39888,6 +39890,10 @@ static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
3988839890
}
3988939891
else {
3989039892
dcrl->extAuthKeyIdSet = 1;
39893+
dcrl->extAuthKeyIdRawIdx = inputIdx + (word32)
39894+
(dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.data - input);
39895+
dcrl->extAuthKeyIdRawSz =
39896+
dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.length;
3989139897

3989239898
/* Get the hash or hash of the hash if wrong size. */
3989339899
ret = GetHashId(dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.data,
@@ -39969,7 +39975,8 @@ static int ParseCRL_Extensions(DecodedCRL* dcrl, const byte* buf, word32 idx,
3996939975
#ifndef NO_SKID
3997039976
/* Parse Authority Key Id extension.
3997139977
* idx is at start of OCTET_STRING data. */
39972-
ret = ParseCRL_AuthKeyIdExt(buf + localIdx, length, dcrl);
39978+
ret = ParseCRL_AuthKeyIdExt(buf + localIdx, localIdx,
39979+
length, dcrl);
3997339980
if (ret != 0) {
3997439981
WOLFSSL_MSG("\tcouldn't parse AuthKeyId extension");
3997539982
}

‎wolfcrypt/src/asn_orig.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9605,7 +9605,8 @@ static int ParseCRL_CertList(RevokedCert* rcert, DecodedCRL* dcrl,
96059605
}
96069606

96079607
#ifndef NO_SKID
9608-
static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
9608+
static int ParseCRL_AuthKeyIdExt(const byte* input, word32 inputIdx, int sz,
9609+
DecodedCRL* dcrl)
96099610
{
96109611
word32 idx = 0;
96119612
int length = 0, ret = 0;
@@ -9633,6 +9634,8 @@ static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
96339634
}
96349635

96359636
dcrl->extAuthKeyIdSet = 1;
9637+
dcrl->extAuthKeyIdRawIdx = inputIdx + idx;
9638+
dcrl->extAuthKeyIdRawSz = (word32)length;
96369639

96379640
/* Get the hash or hash of the hash if wrong size. */
96389641
ret = GetHashId(input + idx, length, dcrl->extAuthKeyId,
@@ -9717,7 +9720,7 @@ static int ParseCRL_Extensions(DecodedCRL* dcrl, const byte* buf,
97179720

97189721
if (oid == AUTH_KEY_OID) {
97199722
#ifndef NO_SKID
9720-
ret = ParseCRL_AuthKeyIdExt(buf + idx, length, dcrl);
9723+
ret = ParseCRL_AuthKeyIdExt(buf + idx, idx, length, dcrl);
97219724
if (ret < 0) {
97229725
WOLFSSL_MSG("\tcouldn't parse AuthKeyId extension");
97239726
return ret;

‎wolfssl/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,6 +2836,8 @@ struct CRL_Entry {
28362836
#endif
28372837
word32 signatureOID;
28382838
#if !defined(NO_SKID) && !defined(NO_ASN)
2839+
word32 extAuthKeyIdRawIdx; /* offset of encoded key id in toBeSigned */
2840+
word32 extAuthKeyIdRawSz; /* length of encoded key id */
28392841
byte extAuthKeyId[KEYID_SIZE];
28402842
byte extAuthKeyIdSet:1; /* Auth key identifier set indicator */
28412843
#endif

‎wolfssl/wolfcrypt/asn.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,8 @@ struct DecodedCRL {
33553355
void* heap;
33563356
#ifndef NO_SKID
33573357
byte extAuthKeyId[SIGNER_DIGEST_SIZE]; /* Authority Key ID */
3358+
word32 extAuthKeyIdRawIdx; /* offset of encoded key id in CRL */
3359+
word32 extAuthKeyIdRawSz; /* length of encoded key id */
33583360
WC_BITFIELD extAuthKeyIdSet:1; /* Auth key identifier set indicator */
33593361
#endif
33603362
WC_BITFIELD crlNumberSet:1; /* CRL number set indicator */

0 commit comments

Comments
 (0)