@@ -37,21 +37,103 @@ static int hw_get_time_sec(void)
3737{
3838 #warning Must implement your own time source if validating certificates
3939
40- return ++ gTimeMs ;
40+ return ++ gTimeMs ;
41+ }
42+
43+ static int IsLeapYear (int year )
44+ {
45+ return ((year % 4 ) == 0 && ((year % 100 ) != 0 || (year % 400 ) == 0 ));
4146}
4247
4348/* This is used by wolfCrypt asn.c for cert time checking */
44- unsigned long my_time (unsigned long * timer )
49+ time_t my_time (time_t * timer )
50+ {
51+ time_t curTime = (time_t )hw_get_time_sec ();
52+
53+ if (timer != NULL ) {
54+ * timer = curTime ;
55+ }
56+
57+ return curTime ;
58+ }
59+
60+ struct tm * my_gmtime (const time_t * timer , struct tm * tmp )
4561{
46- (void )timer ;
47- return hw_get_time_sec ();
62+ static struct tm staticTime ;
63+ static const unsigned char daysPerMonth [] =
64+ { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
65+ time_t curTime ;
66+ long days ;
67+ long rem ;
68+ int year ;
69+ int yearDays ;
70+ int month ;
71+ int monthDays ;
72+
73+ if (tmp == NULL ) {
74+ tmp = & staticTime ;
75+ }
76+
77+ curTime = (timer != NULL ) ? * timer : my_time (NULL );
78+ if (curTime < 0 ) {
79+ curTime = 0 ;
80+ }
81+
82+ days = (long )(curTime / 86400 );
83+ rem = (long )(curTime % 86400 );
84+
85+ tmp -> tm_hour = (int )(rem / 3600 );
86+ rem %= 3600 ;
87+ tmp -> tm_min = (int )(rem / 60 );
88+ tmp -> tm_sec = (int )(rem % 60 );
89+ tmp -> tm_wday = (int )((days + 4 ) % 7 );
90+
91+ year = 1970 ;
92+ while (1 ) {
93+ yearDays = IsLeapYear (year ) ? 366 : 365 ;
94+ if (days < yearDays ) {
95+ break ;
96+ }
97+ days -= yearDays ;
98+ year ++ ;
99+ }
100+
101+ tmp -> tm_year = year - 1900 ;
102+ tmp -> tm_yday = (int )days ;
103+
104+ for (month = 0 ; month < 12 ; month ++ ) {
105+ monthDays = daysPerMonth [month ];
106+ if (month == 1 && IsLeapYear (year )) {
107+ monthDays ++ ;
108+ }
109+ if (days < monthDays ) {
110+ break ;
111+ }
112+ days -= monthDays ;
113+ }
114+
115+ tmp -> tm_mon = month ;
116+ tmp -> tm_mday = (int )days + 1 ;
117+ tmp -> tm_isdst = 0 ;
118+
119+ return tmp ;
48120}
49121
50122#ifndef WOLFCRYPT_ONLY
51123/* This is used by TLS only */
52- unsigned int LowResTimer (void )
124+ word32 LowResTimer (void )
125+ {
126+ return (word32 )hw_get_time_sec ();
127+ }
128+
129+ /* This is used by TLS 1.3 ticket and PSK timeouts. */
130+ #ifdef WOLFSSL_32BIT_MILLI_TIME
131+ word32 TimeNowInMilliseconds (void )
132+ #else
133+ sword64 TimeNowInMilliseconds (void )
134+ #endif
53135{
54- return hw_get_time_sec () ;
136+ return ( sword64 ) my_time ( NULL ) * 1000 ;
55137}
56138#endif
57139
0 commit comments