Skip to content

Commit 4dc3470

Browse files
authored
Merge pull request #10071 from padelsbach/notbefore-notafter-bounds-check
Add bounds check on wolfSSL_X509_notBefore and wolfSSL_X509_notAfter
2 parents 6a04c03 + ec9b6cf commit 4dc3470

10 files changed

Lines changed: 385 additions & 147 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
-DWOLFSSL_EXTRA_PQC_HYBRIDS:BOOL=yes -DWOLFSSL_TLS_NO_MLKEM_STANDALONE:BOOL=no \
7777
..
7878
cmake --build .
79-
ctest -j $(nproc)
79+
ctest -j $(nproc) --output-on-failure
8080
cmake --install .
8181
8282
# clean up

src/internal.c

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13752,11 +13752,34 @@ static int CopyREQAttributes(WOLFSSL_X509* x509, DecodedCert* dCert)
1375213752
}
1375313753
#endif /* WOLFSSL_CERT_REQ */
1375413754

13755+
/* Copy an ASN-encoded date (type + length + data) into a WOLFSSL_ASN1_TIME.
13756+
* srcDate: ASN date buffer where [0]=type, [1]=length, [2..]=date bytes.
13757+
* srcDateLen: total length of srcDate (0 means no date present). */
13758+
static void CopyDateToASN1_TIME(const byte* srcDate, int srcDateLen,
13759+
WOLFSSL_ASN1_TIME* dst)
13760+
{
13761+
if (srcDateLen >= 2) {
13762+
/* Clamp the date length to the maximum allowed size.
13763+
* This needs to match the size of WOLFSSL_ASN1_TIME minus the
13764+
* the type and length fields. */
13765+
const int maxSz = CTC_DATE_SIZE - 2;
13766+
const int copySz = (int)min(srcDate[1], maxSz);
13767+
dst->type = srcDate[0];
13768+
dst->length = copySz;
13769+
XMEMCPY(dst->data, &srcDate[2], copySz);
13770+
}
13771+
else {
13772+
dst->length = 0;
13773+
}
13774+
}
13775+
1375513776
/* Copy parts X509 needs from Decoded cert, 0 on success */
1375613777
int CopyDecodedToX509(WOLFSSL_X509* x509, DecodedCert* dCert)
1375713778
{
1375813779
int ret = 0;
13780+
#ifdef WOLFSSL_SEP
1375913781
int minSz;
13782+
#endif
1376013783

1376113784
if (x509 == NULL || dCert == NULL ||
1376213785
dCert->subjectCNLen < 0)
@@ -13829,22 +13852,10 @@ int CopyDecodedToX509(WOLFSSL_X509* x509, DecodedCert* dCert)
1382913852
x509->hwSerialNumSz = 0;
1383013853
#endif /* WOLFSSL_SEP */
1383113854

13832-
if (dCert->beforeDateLen > 0) {
13833-
minSz = (int)min(dCert->beforeDate[1], MAX_DATE_SZ);
13834-
x509->notBefore.type = dCert->beforeDate[0];
13835-
x509->notBefore.length = minSz;
13836-
XMEMCPY(x509->notBefore.data, &dCert->beforeDate[2], minSz);
13837-
}
13838-
else
13839-
x509->notBefore.length = 0;
13840-
if (dCert->afterDateLen > 0) {
13841-
minSz = (int)min(dCert->afterDate[1], MAX_DATE_SZ);
13842-
x509->notAfter.type = dCert->afterDate[0];
13843-
x509->notAfter.length = minSz;
13844-
XMEMCPY(x509->notAfter.data, &dCert->afterDate[2], minSz);
13845-
}
13846-
else
13847-
x509->notAfter.length = 0;
13855+
CopyDateToASN1_TIME(dCert->beforeDate, dCert->beforeDateLen,
13856+
&x509->notBefore);
13857+
CopyDateToASN1_TIME(dCert->afterDate, dCert->afterDateLen,
13858+
&x509->notAfter);
1384813859

1384913860
if (dCert->publicKey != NULL && dCert->pubKeySize != 0) {
1385013861
x509->pubKey.buffer = (byte*)XMALLOC(
@@ -14226,29 +14237,10 @@ int CopyDecodedAcertToX509(WOLFSSL_X509_ACERT* x509, DecodedAcert* dAcert)
1422614237
}
1422714238

1422814239
/* Copy before and after dates. */
14229-
{
14230-
int minSz = 0;
14231-
14232-
if (dAcert->beforeDateLen > 0) {
14233-
minSz = (int)min(dAcert->beforeDate[1], MAX_DATE_SZ);
14234-
x509->notBefore.type = dAcert->beforeDate[0];
14235-
x509->notBefore.length = minSz;
14236-
XMEMCPY(x509->notBefore.data, &dAcert->beforeDate[2], minSz);
14237-
}
14238-
else {
14239-
x509->notBefore.length = 0;
14240-
}
14241-
14242-
if (dAcert->afterDateLen > 0) {
14243-
minSz = (int)min(dAcert->afterDate[1], MAX_DATE_SZ);
14244-
x509->notAfter.type = dAcert->afterDate[0];
14245-
x509->notAfter.length = minSz;
14246-
XMEMCPY(x509->notAfter.data, &dAcert->afterDate[2], minSz);
14247-
}
14248-
else {
14249-
x509->notAfter.length = 0;
14250-
}
14251-
}
14240+
CopyDateToASN1_TIME(dAcert->beforeDate, dAcert->beforeDateLen,
14241+
&x509->notBefore);
14242+
CopyDateToASN1_TIME(dAcert->afterDate, dAcert->afterDateLen,
14243+
&x509->notAfter);
1425214244

1425314245
/* Copy the signature. */
1425414246
if (dAcert->signature != NULL && dAcert->sigLength != 0 &&

src/ssl_api_crl_ocsp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ int wolfSSL_get_ocsp_producedDate_tm(WOLFSSL *ssl, struct tm *produced_tm) {
424424

425425
if (ExtractDate(ssl->ocspProducedDate,
426426
(unsigned char)ssl->ocspProducedDateFormat, produced_tm, &idx,
427-
MAX_DATE_SZ))
427+
MAX_DATE_SIZE))
428428
return 0;
429429
else
430430
return ASN_PARSE_E;

src/x509.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4419,8 +4419,14 @@ const byte* wolfSSL_X509_notBefore(WOLFSSL_X509* x509)
44194419
{
44204420
WOLFSSL_ENTER("wolfSSL_X509_notBefore");
44214421

4422-
if (x509 == NULL)
4422+
if (x509 == NULL) {
44234423
return NULL;
4424+
}
4425+
4426+
if (x509->notBefore.length < 0 ||
4427+
x509->notBefore.length > (int)sizeof(x509->notBeforeData) - 2) {
4428+
return NULL;
4429+
}
44244430

44254431
XMEMSET(x509->notBeforeData, 0, sizeof(x509->notBeforeData));
44264432
x509->notBeforeData[0] = (byte)x509->notBefore.type;
@@ -4437,8 +4443,14 @@ const byte* wolfSSL_X509_notAfter(WOLFSSL_X509* x509)
44374443
{
44384444
WOLFSSL_ENTER("wolfSSL_X509_notAfter");
44394445

4440-
if (x509 == NULL)
4446+
if (x509 == NULL) {
4447+
return NULL;
4448+
}
4449+
4450+
if (x509->notAfter.length < 0 ||
4451+
x509->notAfter.length > (int)sizeof(x509->notAfterData) - 2) {
44414452
return NULL;
4453+
}
44424454

44434455
XMEMSET(x509->notAfterData, 0, sizeof(x509->notAfterData));
44444456
x509->notAfterData[0] = (byte)x509->notAfter.type;
@@ -16060,6 +16072,10 @@ int wolfSSL_X509_set_notAfter(WOLFSSL_X509* x509, const WOLFSSL_ASN1_TIME* t)
1606016072
return WOLFSSL_FAILURE;
1606116073
}
1606216074

16075+
if (t->length < 0 || t->length > CTC_DATE_SIZE - 2) {
16076+
return WOLFSSL_FAILURE;
16077+
}
16078+
1606316079
x509->notAfter.type = t->type;
1606416080
x509->notAfter.length = t->length;
1606516081

@@ -16074,6 +16090,10 @@ int wolfSSL_X509_set_notBefore(WOLFSSL_X509* x509, const WOLFSSL_ASN1_TIME* t)
1607416090
return WOLFSSL_FAILURE;
1607516091
}
1607616092

16093+
if (t->length < 0 || t->length > CTC_DATE_SIZE - 2) {
16094+
return WOLFSSL_FAILURE;
16095+
}
16096+
1607716097
x509->notBefore.type = t->type;
1607816098
x509->notBefore.length = t->length;
1607916099

0 commit comments

Comments
 (0)