Skip to content

Commit de5cb96

Browse files
Merge pull request #12175 from embhorn/zd22862
Fix out-of-bounds read printing CRL authority key id
2 parents 1ed596f + 76dbca6 commit de5cb96

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 {
@@ -3212,6 +3219,11 @@ int wolfSSL_X509_CRL_sign(WOLFSSL_X509_CRL* crl, WOLFSSL_EVP_PKEY* pkey,
32123219
entry->signature = newSignature;
32133220
entry->signatureSz = newSignatureSz;
32143221
entry->signatureOID = newSignatureOid;
3222+
#if !defined(NO_SKID) && !defined(NO_ASN)
3223+
/* Re-encoded TBS has no authority key id extension. */
3224+
entry->extAuthKeyIdRawIdx = 0;
3225+
entry->extAuthKeyIdRawSz = 0;
3226+
#endif
32153227
}
32163228
else {
32173229
if (newToBeSigned != NULL) {

‎src/x509.c‎

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

1082410824
#if !defined(NO_SKID)
1082510825
if (ret == 0 && crl->crlList->extAuthKeyIdSet &&
10826-
crl->crlList->extAuthKeyId[0] != 0) {
10826+
crl->crlList->extAuthKeyIdRawSz > 0) {
1082710827
word32 i;
1082810828
char val[5];
1082910829
int valSz = 5;
10830+
const byte* keyId = NULL;
10831+
word32 keyIdSz = crl->crlList->extAuthKeyIdRawSz;
10832+
10833+
/* extAuthKeyId is a normalized lookup key; print the encoded id. */
10834+
if (crl->crlList->toBeSigned == NULL ||
10835+
crl->crlList->extAuthKeyIdRawIdx > crl->crlList->tbsSz ||
10836+
keyIdSz > crl->crlList->tbsSz -
10837+
crl->crlList->extAuthKeyIdRawIdx) {
10838+
ret = WOLFSSL_FAILURE;
10839+
}
10840+
else {
10841+
keyId = crl->crlList->toBeSigned +
10842+
crl->crlList->extAuthKeyIdRawIdx;
10843+
}
1083010844

10831-
if (XSNPRINTF(tmp, MAX_WIDTH, "%*s%s", indent + 4, "",
10845+
if (ret == 0 && XSNPRINTF(tmp, MAX_WIDTH, "%*s%s", indent + 4, "",
1083210846
"X509v3 Authority Key Identifier:") >= MAX_WIDTH) {
1083310847
ret = WOLFSSL_FAILURE;
1083410848
}
@@ -10848,7 +10862,7 @@ static int X509CRLPrintExtensions(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
1084810862
}
1084910863

1085010864

10851-
for (i = 0; i < XSTRLEN((char*)crl->crlList->extAuthKeyId); i++) {
10865+
for (i = 0; ret == 0 && i < keyIdSz; i++) {
1085210866
/* check if buffer is almost full */
1085310867
if (ret == 0 && XSTRLEN(tmp) >= sizeof(tmp) - valSz) {
1085410868
if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
@@ -10857,7 +10871,7 @@ static int X509CRLPrintExtensions(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
1085710871
tmp[0] = '\0';
1085810872
}
1085910873
if (ret == 0 && XSNPRINTF(val, (size_t)valSz, ":%02X",
10860-
crl->crlList->extAuthKeyId[i]) >= valSz) {
10874+
keyId[i]) >= valSz) {
1086110875
WOLFSSL_MSG("buffer overrun");
1086210876
ret = WOLFSSL_FAILURE;
1086310877
}

‎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
@@ -39920,7 +39921,8 @@ static int PaseCRL_CheckSignature(DecodedCRL* dcrl, const byte* sigParams,
3992039921

3992139922
#ifndef NO_SKID
3992239923
#ifdef WOLFSSL_ASN_TEMPLATE
39923-
static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
39924+
static int ParseCRL_AuthKeyIdExt(const byte* input, word32 inputIdx, int sz,
39925+
DecodedCRL* dcrl)
3992439926
{
3992539927
DECL_ASNGETDATA(dataASN, authKeyIdASN_Length);
3992639928
int ret = 0;
@@ -39942,6 +39944,10 @@ static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
3994239944
}
3994339945
else {
3994439946
dcrl->extAuthKeyIdSet = 1;
39947+
dcrl->extAuthKeyIdRawIdx = inputIdx + (word32)
39948+
(dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.data - input);
39949+
dcrl->extAuthKeyIdRawSz =
39950+
dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.length;
3994539951

3994639952
/* Get the hash or hash of the hash if wrong size. */
3994739953
ret = GetHashId(dataASN[AUTHKEYIDASN_IDX_KEYID].data.ref.data,
@@ -40023,7 +40029,8 @@ static int ParseCRL_Extensions(DecodedCRL* dcrl, const byte* buf, word32 idx,
4002340029
#ifndef NO_SKID
4002440030
/* Parse Authority Key Id extension.
4002540031
* idx is at start of OCTET_STRING data. */
40026-
ret = ParseCRL_AuthKeyIdExt(buf + localIdx, length, dcrl);
40032+
ret = ParseCRL_AuthKeyIdExt(buf + localIdx, localIdx,
40033+
length, dcrl);
4002740034
if (ret != 0) {
4002840035
WOLFSSL_MSG("\tcouldn't parse AuthKeyId extension");
4002940036
}

‎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
@@ -3358,6 +3358,8 @@ struct DecodedCRL {
33583358
void* heap;
33593359
#ifndef NO_SKID
33603360
byte extAuthKeyId[SIGNER_DIGEST_SIZE]; /* Authority Key ID */
3361+
word32 extAuthKeyIdRawIdx; /* offset of encoded key id in CRL */
3362+
word32 extAuthKeyIdRawSz; /* length of encoded key id */
33613363
WC_BITFIELD extAuthKeyIdSet:1; /* Auth key identifier set indicator */
33623364
#endif
33633365
WC_BITFIELD crlNumberSet:1; /* CRL number set indicator */

0 commit comments

Comments
 (0)