Skip to content

Commit c17cff7

Browse files
Merge pull request #6403 from kareem-wolfssl/gh6387
Fix wolfssl_asn1_time_to_tm setting unexpected fields in tm struct.
2 parents a474179 + f1ad379 commit c17cff7

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/ssl_asn1.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,9 @@ static int wolfssl_asn1_time_to_tm(const WOLFSSL_ASN1_TIME* asnTime,
36523652
const unsigned char* asn1TimeBuf;
36533653
int asn1TimeBufLen;
36543654
int i = 0;
3655+
#ifdef XMKTIME
3656+
struct tm localTm = {0};
3657+
#endif
36553658

36563659
/* Get the string buffer - fixed array, can't fail. */
36573660
asn1TimeBuf = wolfSSL_ASN1_TIME_get_data(asnTime);
@@ -3706,8 +3709,13 @@ static int wolfssl_asn1_time_to_tm(const WOLFSSL_ASN1_TIME* asnTime,
37063709
tm->tm_sec += (asn1TimeBuf[i] - '0');
37073710

37083711
#ifdef XMKTIME
3709-
/* Call XMKTIME on tm to get tm_wday and tm_yday fields populated. */
3710-
XMKTIME(tm);
3712+
XMEMCPY(&localTm, tm, sizeof(struct tm));
3713+
/* Call XMKTIME on tm to get tm_wday and tm_yday fields populated.
3714+
Note that localTm is used here to avoid modifying other fields,
3715+
such as tm_isdst/tm_gmtoff. */
3716+
XMKTIME(&localTm);
3717+
tm->tm_wday = localTm.tm_wday;
3718+
tm->tm_yday = localTm.tm_yday;
37113719
#endif
37123720
}
37133721

tests/api.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33143,6 +33143,7 @@ static int test_wolfSSL_ASN1_TIME_to_tm(void)
3314333143
defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
3314433144
ASN1_TIME asnTime;
3314533145
struct tm tm;
33146+
time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
3314633147

3314733148
XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
3314833149
AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
@@ -33188,6 +33189,22 @@ static int test_wolfSSL_ASN1_TIME_to_tm(void)
3318833189
AssertIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
3318933190
AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
3319033191

33192+
#ifdef XMKTIME
33193+
AssertNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
33194+
AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
33195+
AssertIntEQ(tm.tm_sec, 47);
33196+
AssertIntEQ(tm.tm_min, 22);
33197+
AssertIntEQ(tm.tm_hour, 21);
33198+
AssertIntEQ(tm.tm_mday, 12);
33199+
AssertIntEQ(tm.tm_mon, 4);
33200+
AssertIntEQ(tm.tm_year, 123);
33201+
AssertIntEQ(tm.tm_wday, 5);
33202+
AssertIntEQ(tm.tm_yday, 131);
33203+
/* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
33204+
fields are zeroed out as expected. */
33205+
AssertIntEQ(tm.tm_isdst, 0);
33206+
#endif
33207+
3319133208
res = TEST_RES_CHECK(1);
3319233209
#endif
3319333210
return res;

0 commit comments

Comments
 (0)