Skip to content

Commit 8b8eed8

Browse files
committed
Rebase and refactor to pass in length for bounds checking.
1 parent 0fe54ba commit 8b8eed8

File tree

9 files changed

+59
-40
lines changed

9 files changed

+59
-40
lines changed

src/crl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial,
446446
#endif
447447
{
448448
#if !defined(NO_ASN_TIME) && !defined(WOLFSSL_NO_CRL_DATE_CHECK)
449-
if (!XVALIDATE_DATE(crle->nextDate,crle->nextDateFormat, ASN_AFTER)) {
449+
if (!XVALIDATE_DATE(crle->nextDate, crle->nextDateFormat,
450+
ASN_AFTER, MAX_DATE_SIZE)) {
450451
WOLFSSL_MSG("CRL next date is no longer valid");
451452
nextDateValid = 0;
452453
}

src/ocsp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ static int GetOcspStatus(WOLFSSL_OCSP* ocsp, OcspRequest* request,
296296
else if (*status) {
297297
#ifndef NO_ASN_TIME
298298
if (XVALIDATE_DATE((*status)->thisDate,
299-
(*status)->thisDateFormat, ASN_BEFORE)
299+
(*status)->thisDateFormat, ASN_BEFORE, MAX_DATE_SIZE)
300300
&& ((*status)->nextDate[0] != 0)
301301
&& XVALIDATE_DATE((*status)->nextDate,
302-
(*status)->nextDateFormat, ASN_AFTER))
302+
(*status)->nextDateFormat, ASN_AFTER, MAX_DATE_SIZE))
303303
#endif
304304
{
305305
ret = xstat2err((*status)->status);

src/ssl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22021,7 +22021,8 @@ int wolfSSL_get_ocsp_producedDate_tm(WOLFSSL *ssl, struct tm *produced_tm) {
2202122021
return BAD_FUNC_ARG;
2202222022

2202322023
if (ExtractDate(ssl->ocspProducedDate,
22024-
(unsigned char)ssl->ocspProducedDateFormat, produced_tm, &idx))
22024+
(unsigned char)ssl->ocspProducedDateFormat, produced_tm, &idx,
22025+
MAX_DATE_SZ))
2202522026
return 0;
2202622027
else
2202722028
return ASN_PARSE_E;

src/ssl_asn1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4194,7 +4194,7 @@ char* wolfSSL_ASN1_TIME_to_string(WOLFSSL_ASN1_TIME* t, char* buf, int len)
41944194
}
41954195

41964196
/* Get time as human readable string. */
4197-
if ((buf != NULL) && !GetTimeString(t->data, t->type, buf, len)) {
4197+
if ((buf != NULL) && !GetTimeString(t->data, t->type, buf, len, t->length)) {
41984198
buf = NULL;
41994199
}
42004200

src/x509.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6440,9 +6440,9 @@ static int X509PrintValidity(WOLFSSL_BIO* bio, WOLFSSL_ASN1_TIME * notBefore,
64406440
}
64416441
if (notBefore->length > 0) {
64426442
if (GetTimeString(notBefore->data, ASN_UTC_TIME,
6443-
tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
6443+
tmp, sizeof(tmp), notBefore->length) != WOLFSSL_SUCCESS) {
64446444
if (GetTimeString(notBefore->data, ASN_GENERALIZED_TIME,
6445-
tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
6445+
tmp, sizeof(tmp), notBefore->length) != WOLFSSL_SUCCESS) {
64466446
WOLFSSL_MSG("Error getting not before date");
64476447
return WOLFSSL_FAILURE;
64486448
}
@@ -6462,9 +6462,9 @@ static int X509PrintValidity(WOLFSSL_BIO* bio, WOLFSSL_ASN1_TIME * notBefore,
64626462
}
64636463
if (notAfter->length > 0) {
64646464
if (GetTimeString(notAfter->data, ASN_UTC_TIME,
6465-
tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
6465+
tmp, sizeof(tmp), notAfter->length) != WOLFSSL_SUCCESS) {
64666466
if (GetTimeString(notAfter->data, ASN_GENERALIZED_TIME,
6467-
tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
6467+
tmp, sizeof(tmp), notAfter->length) != WOLFSSL_SUCCESS) {
64686468
WOLFSSL_MSG("Error getting not after date");
64696469
return WOLFSSL_FAILURE;
64706470
}
@@ -9018,9 +9018,9 @@ static int X509CRLPrintRevoked(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
90189018

90199019
if (revoked->revDate[0] != 0) {
90209020
if (GetTimeString(revoked->revDate, ASN_UTC_TIME,
9021-
tmp, MAX_WIDTH) != WOLFSSL_SUCCESS) {
9021+
tmp, MAX_WIDTH, MAX_DATE_SIZE) != WOLFSSL_SUCCESS) {
90229022
if (GetTimeString(revoked->revDate, ASN_GENERALIZED_TIME,
9023-
tmp, MAX_WIDTH) != WOLFSSL_SUCCESS) {
9023+
tmp, MAX_WIDTH, MAX_DATE_SIZE) != WOLFSSL_SUCCESS) {
90249024
WOLFSSL_MSG("Error getting revocation date");
90259025
return WOLFSSL_FAILURE;
90269026
}
@@ -9072,7 +9072,7 @@ static int X509CRLPrintDates(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
90729072

90739073
if (crl->crlList->lastDate[0] != 0) {
90749074
if (GetTimeString(crl->crlList->lastDate, crl->crlList->lastDateFormat,
9075-
tmp, MAX_WIDTH) != WOLFSSL_SUCCESS) {
9075+
tmp, MAX_WIDTH, MAX_DATE_SIZE) != WOLFSSL_SUCCESS) {
90769076
WOLFSSL_MSG("Error getting last update date");
90779077
return WOLFSSL_FAILURE;
90789078
}
@@ -9100,7 +9100,7 @@ static int X509CRLPrintDates(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
91009100

91019101
if (crl->crlList->nextDate[0] != 0) {
91029102
if (GetTimeString(crl->crlList->nextDate, crl->crlList->nextDateFormat,
9103-
tmp, MAX_WIDTH) != WOLFSSL_SUCCESS) {
9103+
tmp, MAX_WIDTH, MAX_DATE_SIZE) != WOLFSSL_SUCCESS) {
91049104
WOLFSSL_MSG("Error getting next update date");
91059105
return WOLFSSL_FAILURE;
91069106
}

src/x509_str.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,25 @@ static int X509StoreVerifyCertDate(WOLFSSL_X509_STORE_CTX* ctx, int ret)
375375
WOLFSSL_MSG("Override date validation, WOLFSSL_USE_CHECK_TIME");
376376
if (wc_ValidateDateWithTime(afterDate,
377377
(byte)ctx->current_cert->notAfter.type, ASN_AFTER,
378-
checkTime) < 1) {
378+
checkTime, ctx->current_cert->notAfter.length) < 1) {
379379
ret = ASN_AFTER_DATE_E;
380380
}
381381
else if (wc_ValidateDateWithTime(beforeDate,
382382
(byte)ctx->current_cert->notBefore.type, ASN_BEFORE,
383-
checkTime) < 1) {
383+
checkTime, ctx->current_cert->notBefore.length) < 1) {
384384
ret = ASN_BEFORE_DATE_E;
385385
}
386386
}
387387
}
388388
#else
389389
if (XVALIDATE_DATE(afterDate,
390-
(byte)ctx->current_cert->notAfter.type, ASN_AFTER) < 1) {
390+
(byte)ctx->current_cert->notAfter.type, ASN_AFTER,
391+
ctx->current_cert->notAfter.length) < 1) {
391392
ret = ASN_AFTER_DATE_E;
392393
}
393394
else if (XVALIDATE_DATE(beforeDate,
394-
(byte)ctx->current_cert->notBefore.type, ASN_BEFORE) < 1) {
395+
(byte)ctx->current_cert->notBefore.type, ASN_BEFORE,
396+
ctx->current_cert->notBefore.length) < 1) {
395397
ret = ASN_BEFORE_DATE_E;
396398
}
397399
#endif /* USE_WOLF_VALIDDATE */

wolfcrypt/src/asn.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16271,7 +16271,7 @@ static WC_INLINE int GetTime_Long(long* value, const byte* date, int* idx)
1627116271
* Reminder: idx is incremented in each call to GetTime()
1627216272
* Return 0 on failure, 1 for success. */
1627316273
int ExtractDate(const unsigned char* date, unsigned char format,
16274-
struct tm* certTime, int* idx)
16274+
struct tm* certTime, int* idx, int len)
1627516275
{
1627616276
int i = *idx;
1627716277

@@ -16280,13 +16280,21 @@ int ExtractDate(const unsigned char* date, unsigned char format,
1628016280
* Subtract 2; one for zero indexing and one to exclude null terminator
1628116281
* built into macro values. */
1628216282
if (format == ASN_UTC_TIME) {
16283-
/* UTCTime format requires YYMMDDHHMMSSZ. */
16283+
/* UTCTime format requires YYMMDDHHMMSSZ (13 chars). */
16284+
/* Bounds check: ensure we have enough data before accessing. */
16285+
if (len < i + ASN_UTC_TIME_SIZE - 1) {
16286+
return 0;
16287+
}
1628416288
if (date[i + ASN_UTC_TIME_SIZE - 2] != 'Z') {
1628516289
return 0;
1628616290
}
1628716291
}
1628816292
else if (format == ASN_GENERALIZED_TIME) {
16289-
/* GeneralizedTime format requires YYYYMMDDHHMMSSZ. */
16293+
/* GeneralizedTime format requires YYYYMMDDHHMMSSZ (15 chars). */
16294+
/* Bounds check: ensure we have enough data before accessing. */
16295+
if (len < i + ASN_GENERALIZED_TIME_SIZE - 1) {
16296+
return 0;
16297+
}
1629016298
if (date[ i + ASN_GENERALIZED_TIME_SIZE - 2] != 'Z') {
1629116299
return 0;
1629216300
}
@@ -16363,12 +16371,12 @@ int ExtractDate(const unsigned char* date, unsigned char format,
1636316371

1636416372

1636516373
#ifdef WOLFSSL_ASN_TIME_STRING
16366-
int GetTimeString(byte* date, int format, char* buf, int len)
16374+
int GetTimeString(byte* date, int format, char* buf, int len, int dateLen)
1636716375
{
1636816376
struct tm t;
1636916377
int idx = 0;
1637016378

16371-
if (!ExtractDate(date, (unsigned char)format, &t, &idx)) {
16379+
if (!ExtractDate(date, (unsigned char)format, &t, &idx, dateLen)) {
1637216380
return 0;
1637316381
}
1637416382

@@ -16598,13 +16606,13 @@ static WC_INLINE int DateLessThan(const struct tm* a, const struct tm* b)
1659816606
/* date = ASN.1 raw */
1659916607
/* format = ASN_UTC_TIME or ASN_GENERALIZED_TIME */
1660016608
/* dateType = ASN_AFTER or ASN_BEFORE */
16601-
int wc_ValidateDate(const byte* date, byte format, int dateType)
16609+
int wc_ValidateDate(const byte* date, byte format, int dateType, int len)
1660216610
{
16603-
return wc_ValidateDateWithTime(date, format, dateType, 0);
16611+
return wc_ValidateDateWithTime(date, format, dateType, 0, len);
1660416612
}
1660516613

1660616614
int wc_ValidateDateWithTime(const byte* date, byte format, int dateType,
16607-
time_t checkTime)
16615+
time_t checkTime, int len)
1660816616
{
1660916617
time_t ltime;
1661016618
struct tm certTime;
@@ -16653,7 +16661,7 @@ int wc_ValidateDateWithTime(const byte* date, byte format, int dateType,
1665316661
}
1665416662
#endif
1665516663

16656-
if (!ExtractDate(date, format, &certTime, &i)) {
16664+
if (!ExtractDate(date, format, &certTime, &i, len)) {
1665716665
WOLFSSL_MSG("Error extracting the date");
1665816666
return 0;
1665916667
}
@@ -16875,7 +16883,7 @@ static int GetDate(DecodedCert* cert, int dateType, int verify, int maxIdx)
1687516883
#ifndef NO_ASN_TIME_CHECK
1687616884
if (verify != NO_VERIFY && verify != VERIFY_SKIP_DATE &&
1687716885
(! AsnSkipDateCheck) &&
16878-
!XVALIDATE_DATE(date, format, dateType)) {
16886+
!XVALIDATE_DATE(date, format, dateType, length)) {
1687916887
if (dateType == ASN_BEFORE) {
1688016888
WOLFSSL_ERROR_VERBOSE(ASN_BEFORE_DATE_E);
1688116889
return ASN_BEFORE_DATE_E;
@@ -16933,7 +16941,7 @@ int wc_GetDateAsCalendarTime(const byte* date, int length, byte format,
1693316941
{
1693416942
int idx = 0;
1693516943
(void)length;
16936-
if (!ExtractDate(date, format, timearg, &idx))
16944+
if (!ExtractDate(date, format, timearg, &idx, length))
1693716945
return ASN_TIME_E;
1693816946
return 0;
1693916947
}
@@ -23612,7 +23620,8 @@ static int CheckDate(ASNGetData *dataASN, int dateType)
2361223620
#ifndef NO_ASN_TIME_CHECK
2361323621
/* Check date is a valid string and ASN_BEFORE or ASN_AFTER now. */
2361423622
if ((ret == 0) && (! AsnSkipDateCheck)) {
23615-
if (!XVALIDATE_DATE(dataASN->data.ref.data, dataASN->tag, dateType)) {
23623+
if (!XVALIDATE_DATE(dataASN->data.ref.data, dataASN->tag, dateType,
23624+
(int)dataASN->data.ref.length)) {
2361623625
if (dateType == ASN_BEFORE) {
2361723626
ret = ASN_BEFORE_DATE_E;
2361823627
}
@@ -38403,7 +38412,7 @@ static int DecodeSingleResponse(byte* source, word32* ioIndex, word32 size,
3840338412
#ifndef NO_ASN_TIME_CHECK
3840438413
#ifndef WOLFSSL_NO_OCSP_DATE_CHECK
3840538414
if ((! AsnSkipDateCheck) && !XVALIDATE_DATE(single->status->thisDate,
38406-
single->status->thisDateFormat, ASN_BEFORE))
38415+
single->status->thisDateFormat, ASN_BEFORE, MAX_DATE_SIZE))
3840738416
return ASN_BEFORE_DATE_E;
3840838417
#endif
3840938418
#endif
@@ -38441,7 +38450,7 @@ static int DecodeSingleResponse(byte* source, word32* ioIndex, word32 size,
3844138450
#ifndef WOLFSSL_NO_OCSP_DATE_CHECK
3844238451
if ((! AsnSkipDateCheck) &&
3844338452
!XVALIDATE_DATE(single->status->nextDate,
38444-
single->status->nextDateFormat, ASN_AFTER))
38453+
single->status->nextDateFormat, ASN_AFTER, MAX_DATE_SIZE))
3844538454
return ASN_AFTER_DATE_E;
3844638455
#endif
3844738456
#endif
@@ -38515,7 +38524,8 @@ static int DecodeSingleResponse(byte* source, word32* ioIndex, word32 size,
3851538524
#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFSSL_NO_OCSP_DATE_CHECK)
3851638525
/* Check date is a valid string and ASN_BEFORE now. */
3851738526
if ((! AsnSkipDateCheck) &&
38518-
!XVALIDATE_DATE(cs->thisDate, ASN_GENERALIZED_TIME, ASN_BEFORE))
38527+
!XVALIDATE_DATE(cs->thisDate, ASN_GENERALIZED_TIME, ASN_BEFORE,
38528+
MAX_DATE_SIZE))
3851938529
{
3852038530
ret = ASN_BEFORE_DATE_E;
3852138531
}
@@ -38540,7 +38550,8 @@ static int DecodeSingleResponse(byte* source, word32* ioIndex, word32 size,
3854038550
#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFSSL_NO_OCSP_DATE_CHECK)
3854138551
/* Check date is a valid string and ASN_AFTER now. */
3854238552
if ((! AsnSkipDateCheck) &&
38543-
!XVALIDATE_DATE(cs->nextDate, ASN_GENERALIZED_TIME, ASN_AFTER))
38553+
!XVALIDATE_DATE(cs->nextDate, ASN_GENERALIZED_TIME, ASN_AFTER,
38554+
MAX_DATE_SIZE))
3854438555
{
3854538556
ret = ASN_AFTER_DATE_E;
3854638557
}
@@ -40627,7 +40638,8 @@ static int ParseCRL_CertList(RevokedCert* rcert, DecodedCRL* dcrl,
4062740638
#if !defined(NO_ASN_TIME) && !defined(WOLFSSL_NO_CRL_DATE_CHECK)
4062840639
if (verify != NO_VERIFY &&
4062940640
(! AsnSkipDateCheck) &&
40630-
!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, ASN_AFTER)) {
40641+
!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, ASN_AFTER,
40642+
MAX_DATE_SIZE)) {
4063140643
WOLFSSL_MSG("CRL after date is no longer valid");
4063240644
WOLFSSL_ERROR_VERBOSE(CRL_CERT_DATE_ERR);
4063340645
return CRL_CERT_DATE_ERR;
@@ -41289,7 +41301,8 @@ int ParseCRL(RevokedCert* rcert, DecodedCRL* dcrl, const byte* buff, word32 sz,
4128941301
/* Next date was set, so validate it. */
4129041302
if (verify != NO_VERIFY &&
4129141303
(! AsnSkipDateCheck) &&
41292-
!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, ASN_AFTER)) {
41304+
!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, ASN_AFTER,
41305+
MAX_DATE_SIZE)) {
4129341306
WOLFSSL_MSG("CRL after date is no longer valid");
4129441307
ret = CRL_CERT_DATE_ERR;
4129541308
WOLFSSL_ERROR_VERBOSE(ret);

wolfssl/wolfcrypt/asn.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,20 +2244,22 @@ WOLFSSL_LOCAL int wc_GetKeyOID(byte* key, word32 keySz, const byte** curveOID,
22442244

22452245
typedef struct tm wolfssl_tm;
22462246
#ifdef WOLFSSL_ASN_TIME_STRING
2247-
WOLFSSL_LOCAL int GetTimeString(byte* date, int format, char* buf, int len);
2247+
WOLFSSL_LOCAL int GetTimeString(byte* date, int format, char* buf, int len,
2248+
int dateLen);
22482249
#endif
22492250
#if !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
22502251
!defined(TIME_OVERRIDES) && (defined(OPENSSL_EXTRA) || defined(HAVE_PKCS7))
22512252
WOLFSSL_LOCAL int GetFormattedTime(void* currTime, byte* buf, word32 len);
22522253
WOLFSSL_LOCAL int GetAsnTimeString(void* currTime, byte* buf, word32 len);
22532254
#endif
22542255
WOLFSSL_LOCAL int ExtractDate(const unsigned char* date, unsigned char format,
2255-
wolfssl_tm* certTime, int* idx);
2256+
wolfssl_tm* certTime, int* idx, int len);
22562257
WOLFSSL_LOCAL int DateGreaterThan(const struct tm* a, const struct tm* b);
2257-
WOLFSSL_LOCAL int wc_ValidateDate(const byte* date, byte format, int dateType);
2258+
WOLFSSL_LOCAL int wc_ValidateDate(const byte* date, byte format, int dateType,
2259+
int len);
22582260
#ifndef NO_ASN_TIME
22592261
WOLFSSL_LOCAL int wc_ValidateDateWithTime(const byte* date, byte format,
2260-
int dateType, time_t checkTime);
2262+
int dateType, time_t checkTime, int len);
22612263
#endif
22622264
WOLFSSL_TEST_VIS int wc_AsnSetSkipDateCheck(int skip_p);
22632265
WOLFSSL_LOCAL int wc_AsnGetSkipDateCheck(void);

wolfssl/wolfcrypt/wc_port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
15861586
#endif
15871587
#if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
15881588
#define USE_WOLF_VALIDDATE
1589-
#define XVALIDATE_DATE(d, f, t) wc_ValidateDate((d), (f), (t))
1589+
#define XVALIDATE_DATE(d, f, t, l) wc_ValidateDate((d), (f), (t), (l))
15901590
#endif
15911591

15921592
/* wolf struct tm and time_t */

0 commit comments

Comments
 (0)