Skip to content

Commit 146c537

Browse files
Merge pull request #12189 from Frauschi/validity-date-no-time-t
Fix certificate validity and ASN1_TIME_to_tm for dates past 2038
2 parents 69e5ea0 + 030862d commit 146c537

6 files changed

Lines changed: 266 additions & 135 deletions

File tree

‎src/ssl_asn1.c‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4697,15 +4697,27 @@ static int wolfssl_asn1_time_to_tm(const WOLFSSL_ASN1_TIME* asnTime,
46974697
}
46984698

46994699
if (ret == 1) {
4700+
static const int monthDays[12] = {
4701+
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
4702+
};
4703+
static const int wdayOffset[12] = {
4704+
0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
4705+
};
4706+
int year = localTm.tm_year + 1900;
4707+
int leap = ((year % 4 == 0) && (year % 100 != 0)) ||
4708+
(year % 400 == 0);
4709+
4710+
localTm.tm_yday = monthDays[localTm.tm_mon] + localTm.tm_mday - 1 +
4711+
((leap && (localTm.tm_mon > 1)) ? 1 : 0);
4712+
/* 400 years are whole weeks; keep year positive for the divisions. */
4713+
year += 400;
4714+
if (localTm.tm_mon < 2)
4715+
year--;
4716+
localTm.tm_wday = (year + year / 4 - year / 100 + year / 400 +
4717+
wdayOffset[localTm.tm_mon] + localTm.tm_mday) % 7;
4718+
47004719
/* Publish to caller. */
47014720
XMEMCPY(tm, &localTm, sizeof(*tm));
4702-
#ifdef XMKTIME
4703-
/* XMKTIME may set tm_isdst/tm_gmtoff on localTm; call after the
4704-
* copy so those fields stay zero in the caller's tm. */
4705-
XMKTIME(&localTm);
4706-
tm->tm_wday = localTm.tm_wday;
4707-
tm->tm_yday = localTm.tm_yday;
4708-
#endif
47094721
}
47104722

47114723
return ret;

‎tests/api/test_asn.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5619,6 +5619,125 @@ int test_wc_MakeCert_generalizedTimeValidity(void)
56195619
return EXPECT_RESULT();
56205620
}
56215621

5622+
#if defined(TEST_SIGN_CERT_BOUNDS_RSA) && !defined(NO_ASN_TIME)
5623+
static time_t test_asn_validityNow;
5624+
static time_t test_asn_validity_time_cb(time_t* t)
5625+
{
5626+
if (t != NULL)
5627+
*t = test_asn_validityNow;
5628+
return test_asn_validityNow;
5629+
}
5630+
5631+
/* Make a cert valid for daysValid at the fixed time and check both dates. */
5632+
static int test_asn_validity_days(Cert* cert, RsaKey* key, WC_RNG* rng,
5633+
byte* der, time_t now, int daysValid, const char* expBefore,
5634+
const char* expAfter)
5635+
{
5636+
EXPECT_DECLS;
5637+
DecodedCert dc;
5638+
int derSz = 0;
5639+
word32 len;
5640+
5641+
test_asn_validityNow = now;
5642+
cert->daysValid = daysValid;
5643+
ExpectIntEQ(wc_SetTimeCb(test_asn_validity_time_cb), 0);
5644+
derSz = wc_MakeCert(cert, der, SIGN_CERT_SCRATCH_SZ, key, NULL, rng);
5645+
wc_SetTimeCb(NULL);
5646+
if (expAfter == NULL) {
5647+
ExpectIntEQ(derSz, WC_NO_ERR_TRACE(DATE_E));
5648+
return EXPECT_RESULT();
5649+
}
5650+
ExpectIntGT(derSz, 0);
5651+
if (EXPECT_SUCCESS()) {
5652+
ExpectIntGT(derSz = wc_SignCert(cert->bodySz, cert->sigType, der,
5653+
SIGN_CERT_SCRATCH_SZ, key, NULL, rng), 0);
5654+
}
5655+
if (EXPECT_SUCCESS()) {
5656+
wc_InitDecodedCert(&dc, der, (word32)derSz, NULL);
5657+
ExpectIntEQ(wc_ParseCert(&dc, CERT_TYPE, NO_VERIFY, NULL), 0);
5658+
len = (word32)XSTRLEN(expBefore);
5659+
ExpectIntEQ(dc.beforeDate[0], (len == 15) ? ASN_GENERALIZED_TIME :
5660+
ASN_UTC_TIME);
5661+
ExpectIntEQ(dc.beforeDate[1], len);
5662+
ExpectBufEQ(dc.beforeDate + 2, expBefore, len);
5663+
len = (word32)XSTRLEN(expAfter);
5664+
ExpectIntEQ(dc.afterDate[0], (len == 15) ? ASN_GENERALIZED_TIME :
5665+
ASN_UTC_TIME);
5666+
ExpectIntEQ(dc.afterDate[1], len);
5667+
ExpectBufEQ(dc.afterDate + 2, expAfter, len);
5668+
wc_FreeDecodedCert(&dc);
5669+
}
5670+
return EXPECT_RESULT();
5671+
}
5672+
#endif
5673+
5674+
int test_wc_MakeCert_validityDays(void)
5675+
{
5676+
EXPECT_DECLS;
5677+
#if defined(TEST_SIGN_CERT_BOUNDS_RSA) && !defined(NO_ASN_TIME)
5678+
WC_RNG rng;
5679+
Cert cert;
5680+
RsaKey key;
5681+
byte* der = NULL;
5682+
word32 idx = 0;
5683+
int rngInit = 0;
5684+
int keyInit = 0;
5685+
/* 2026-11-19T12:00:00Z and 2028-03-01T00:00:00Z */
5686+
const time_t now = (time_t)1795089600L;
5687+
const time_t leapNow = (time_t)1835481600L;
5688+
5689+
XMEMSET(&rng, 0, sizeof(rng));
5690+
XMEMSET(&cert, 0, sizeof(cert));
5691+
XMEMSET(&key, 0, sizeof(key));
5692+
5693+
ExpectIntEQ(wc_InitRng(&rng), 0);
5694+
if (EXPECT_SUCCESS()) rngInit = 1;
5695+
ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
5696+
if (EXPECT_SUCCESS()) keyInit = 1;
5697+
ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
5698+
sizeof_server_key_der_2048), 0);
5699+
ExpectNotNull(der = (byte*)XMALLOC(SIGN_CERT_SCRATCH_SZ, HEAP_HINT,
5700+
DYNAMIC_TYPE_TMP_BUFFER));
5701+
ExpectIntEQ(wc_InitCert(&cert), 0);
5702+
if (EXPECT_SUCCESS()) {
5703+
cert.sigType = CTC_SHA256wRSA;
5704+
cert.serial[0] = 0x01;
5705+
cert.serialSz = 1;
5706+
XSTRNCPY(cert.subject.commonName, "validity-days", CTC_NAME_SIZE);
5707+
}
5708+
5709+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, now, 9000,
5710+
"261118120000Z", "20510711120000Z"), TEST_SUCCESS);
5711+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, now, 2912120,
5712+
"261118120000Z", "99991231120000Z"), TEST_SUCCESS);
5713+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, now, 2912121,
5714+
NULL, NULL), TEST_SUCCESS);
5715+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, leapNow, 365,
5716+
"280229000000Z", "290301000000Z"), TEST_SUCCESS);
5717+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, now, -739938,
5718+
"261118120000Z", "00010101120000Z"), TEST_SUCCESS);
5719+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, now, -739939,
5720+
NULL, NULL), TEST_SUCCESS);
5721+
/* An XTIME error, then a clock before 1970 as a signed 32-bit time_t
5722+
* after 2038 gives. */
5723+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der, (time_t)-1,
5724+
365, NULL, NULL), TEST_SUCCESS);
5725+
#ifndef WOLFSSL_GMTIME
5726+
if ((time_t)-1 < (time_t)1) {
5727+
ExpectIntEQ(test_asn_validity_days(&cert, &key, &rng, der,
5728+
(time_t)-86400, 365, NULL, NULL), TEST_SUCCESS);
5729+
}
5730+
#endif
5731+
5732+
XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
5733+
if (keyInit)
5734+
wc_FreeRsaKey(&key);
5735+
if (rngInit)
5736+
wc_FreeRng(&rng);
5737+
#endif
5738+
return EXPECT_RESULT();
5739+
}
5740+
56225741
#if defined(TEST_SIGN_CERT_BOUNDS_RSA) || defined(TEST_SIGN_CERT_BOUNDS_ECC)
56235742
#define TEST_MAKECERT_SERIAL
56245743
#endif

‎tests/api/test_asn.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int test_ToTraditional_ex_negative(void);
6666
int test_ToTraditional_ex_mldsa_bad_params(void);
6767
int test_wc_SignCert_buffer_bounds(void);
6868
int test_wc_MakeCert_generalizedTimeValidity(void);
69+
int test_wc_MakeCert_validityDays(void);
6970
int test_wc_MakeCert_serial_encoding(void);
7071
int test_wc_DecodeKeyUsage_decipherOnly(void);
7172
int test_wc_DecodeExtKeyUsage_ssh(void);
@@ -119,6 +120,7 @@ int test_wc_AltNameNewEx(void);
119120
TEST_DECL_GROUP("asn", test_ToTraditional_ex_mldsa_bad_params), \
120121
TEST_DECL_GROUP("asn", test_wc_SignCert_buffer_bounds), \
121122
TEST_DECL_GROUP("asn", test_wc_MakeCert_generalizedTimeValidity), \
123+
TEST_DECL_GROUP("asn", test_wc_MakeCert_validityDays), \
122124
TEST_DECL_GROUP("asn", test_wc_MakeCert_serial_encoding), \
123125
TEST_DECL_GROUP("asn", test_wc_DecodeKeyUsage_decipherOnly), \
124126
TEST_DECL_GROUP("asn", test_wc_DecodeExtKeyUsage_ssh), \

‎tests/api/test_ossl_asn1.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,10 +2185,22 @@ int test_wolfSSL_ASN1_TIME_to_tm(void)
21852185
ExpectIntEQ(tm.tm_mon, 1);
21862186
ExpectIntEQ(tm.tm_year, 100);
21872187
ExpectIntEQ(tm.tm_isdst, 0);
2188-
#ifdef XMKTIME
21892188
ExpectIntEQ(tm.tm_wday, 2);
21902189
ExpectIntEQ(tm.tm_yday, 52);
2191-
#endif
2190+
2191+
ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "99991231235959Z"), 1);
2192+
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
2193+
ExpectIntEQ(tm.tm_year, 9999 - 1900);
2194+
ExpectIntEQ(tm.tm_wday, 5);
2195+
ExpectIntEQ(tm.tm_yday, 364);
2196+
ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20480301000000Z"), 1);
2197+
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
2198+
ExpectIntEQ(tm.tm_wday, 0);
2199+
ExpectIntEQ(tm.tm_yday, 60);
2200+
ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "00000101000000Z"), 1);
2201+
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
2202+
ExpectIntEQ(tm.tm_wday, 6);
2203+
ExpectIntEQ(tm.tm_yday, 0);
21922204

21932205
ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
21942206
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
@@ -2217,7 +2229,7 @@ int test_wolfSSL_ASN1_TIME_to_tm(void)
22172229
ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
22182230
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
22192231

2220-
#ifdef XMKTIME
2232+
#if defined(OPENSSL_EXTRA) && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
22212233
ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
22222234
ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
22232235
ExpectIntEQ(tm.tm_sec, 47);

‎wolfcrypt/src/asn.c‎

Lines changed: 107 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -29781,6 +29781,110 @@ static void SetTime(struct tm* date, byte* output, byte format)
2978129781

2978229782
output[i] = 'Z'; /* Zulu profile */
2978329783
}
29784+
29785+
/* Day numbers of 0001-01-01 and 9999-12-31, counting from 0000-03-01. */
29786+
#define ASN_VALIDITY_DAY_MIN 306
29787+
#define ASN_VALIDITY_DAY_MAX 3652364
29788+
29789+
/* Encode now shifted by days as a Time. */
29790+
static int SetValidityTime(const struct tm* now, int days, byte* output,
29791+
word32* outSz)
29792+
{
29793+
struct tm date;
29794+
sword32 y;
29795+
sword32 m;
29796+
sword32 era;
29797+
sword32 yoe;
29798+
sword32 doy;
29799+
sword32 doe;
29800+
sword32 day;
29801+
byte format;
29802+
word32 timeSz;
29803+
word32 idx;
29804+
29805+
if (now->tm_year > 9999 - 1900) {
29806+
WOLFSSL_MSG("Current time is after 9999");
29807+
return DATE_E;
29808+
}
29809+
y = (sword32)now->tm_year + 1900;
29810+
m = now->tm_mon + 1;
29811+
if (m <= 2)
29812+
y--;
29813+
era = y / 400;
29814+
yoe = y - era * 400;
29815+
doy = (153 * ((m > 2) ? m - 3 : m + 9) + 2) / 5 + now->tm_mday - 1;
29816+
doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
29817+
day = era * 146097 + doe;
29818+
29819+
if (((sword32)days < ASN_VALIDITY_DAY_MIN - day) ||
29820+
((sword32)days > ASN_VALIDITY_DAY_MAX - day)) {
29821+
WOLFSSL_MSG("Validity date outside years 1 to 9999");
29822+
return DATE_E;
29823+
}
29824+
day += days;
29825+
29826+
era = day / 146097;
29827+
doe = day - era * 146097;
29828+
yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
29829+
doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
29830+
m = (5 * doy + 2) / 153;
29831+
29832+
date = *now;
29833+
date.tm_mday = (int)(doy - (153 * m + 2) / 5 + 1);
29834+
date.tm_mon = (int)((m < 10) ? m + 3 : m - 9);
29835+
date.tm_year = (int)(yoe + era * 400 + ((date.tm_mon <= 2) ? 1 : 0));
29836+
29837+
format = ValidityTimeFormat(&date);
29838+
timeSz = (format == ASN_UTC_TIME) ? ASN_UTC_TIME_SIZE - 1 : ASN_GEN_TIME_SZ;
29839+
output[0] = format;
29840+
idx = SetLength(timeSz, output + 1) + 1;
29841+
SetTime(&date, output + idx, format);
29842+
*outSz = idx + timeSz;
29843+
29844+
return 0;
29845+
}
29846+
29847+
/* Encode notBefore as now less one day and notAfter as now plus daysValid. */
29848+
static int SetValidityTimes(byte* before, word32* beforeSz, byte* after,
29849+
word32* afterSz, int daysValid)
29850+
{
29851+
int ret = 0;
29852+
time_t now;
29853+
struct tm* ts = NULL;
29854+
struct tm* tmpTime;
29855+
struct tm nowTm;
29856+
#if defined(NEED_TMP_TIME)
29857+
struct tm tmpTimeStorage;
29858+
tmpTime = &tmpTimeStorage;
29859+
#else
29860+
tmpTime = NULL;
29861+
#endif
29862+
(void)tmpTime;
29863+
29864+
now = wc_Time(0);
29865+
if (now == (time_t)-1) {
29866+
WOLFSSL_MSG("Current time not available");
29867+
return DATE_E;
29868+
}
29869+
ts = XGMTIME(&now, tmpTime);
29870+
if (ValidateGmtime(ts)) {
29871+
WOLFSSL_MSG("XGMTIME failed");
29872+
ret = DATE_E;
29873+
}
29874+
else if (ts->tm_year < 70) {
29875+
WOLFSSL_MSG("Current time is before 1970");
29876+
ret = DATE_E;
29877+
}
29878+
if (ret == 0) {
29879+
nowTm = *ts;
29880+
ret = SetValidityTime(&nowTm, -1, before, beforeSz);
29881+
}
29882+
if (ret == 0) {
29883+
ret = SetValidityTime(&nowTm, daysValid, after, afterSz);
29884+
}
29885+
29886+
return ret;
29887+
}
2978429888
#endif
2978529889

2978629890

@@ -31603,70 +31707,10 @@ static int EncodeExtensions(Cert* cert, byte* output, word32 maxSz,
3160331707
static int SetValidity(byte* before, byte* after, int daysValid)
3160431708
{
3160531709
#ifndef NO_ASN_TIME
31606-
int ret = 0;
31607-
byte format;
31608-
word32 timeSz;
31609-
time_t now;
31610-
time_t then;
31611-
struct tm* tmpTime;
31612-
struct tm* expandedTime;
31613-
struct tm localTime;
31614-
#if defined(NEED_TMP_TIME)
31615-
/* for use with gmtime_r */
31616-
struct tm tmpTimeStorage;
31617-
tmpTime = &tmpTimeStorage;
31618-
#else
31619-
tmpTime = NULL;
31620-
#endif
31621-
(void)tmpTime;
31622-
31623-
now = wc_Time(0);
31624-
31625-
/* subtract 1 day of seconds for more compliance */
31626-
then = now - 86400;
31627-
expandedTime = XGMTIME(&then, tmpTime);
31628-
if (ValidateGmtime(expandedTime)) {
31629-
WOLFSSL_MSG("XGMTIME failed");
31630-
ret = DATE_E;
31631-
}
31632-
if (ret == 0) {
31633-
localTime = *expandedTime;
31634-
31635-
/* adjust */
31636-
localTime.tm_year += 1900;
31637-
localTime.tm_mon += 1;
31638-
31639-
format = ValidityTimeFormat(&localTime);
31640-
timeSz = (format == ASN_UTC_TIME) ? ASN_UTC_TIME_SIZE - 1
31641-
: ASN_GEN_TIME_SZ;
31642-
before[0] = format;
31643-
SetLength(timeSz, before + 1);
31644-
SetTime(&localTime, before + 2, format);
31710+
word32 beforeSz;
31711+
word32 afterSz;
3164531712

31646-
/* add daysValid of seconds */
31647-
then = now + (daysValid * (time_t)86400);
31648-
expandedTime = XGMTIME(&then, tmpTime);
31649-
if (ValidateGmtime(expandedTime)) {
31650-
WOLFSSL_MSG("XGMTIME failed");
31651-
ret = DATE_E;
31652-
}
31653-
}
31654-
if (ret == 0) {
31655-
localTime = *expandedTime;
31656-
31657-
/* adjust */
31658-
localTime.tm_year += 1900;
31659-
localTime.tm_mon += 1;
31660-
31661-
format = ValidityTimeFormat(&localTime);
31662-
timeSz = (format == ASN_UTC_TIME) ? ASN_UTC_TIME_SIZE - 1
31663-
: ASN_GEN_TIME_SZ;
31664-
after[0] = format;
31665-
SetLength(timeSz, after + 1);
31666-
SetTime(&localTime, after + 2, format);
31667-
}
31668-
31669-
return ret;
31713+
return SetValidityTimes(before, &beforeSz, after, &afterSz, daysValid);
3167031714
#else
3167131715
(void)before;
3167231716
(void)after;

0 commit comments

Comments
 (0)