@@ -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. */
1627316273int 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
1660616614int 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);
0 commit comments