From 1d4cc928c6628724996e3d04b555a432249bcd4f Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Wed, 18 Mar 2015 16:15:56 -0600 Subject: [PATCH] fix Freescale MQX gmtime, cert generation --- wolfcrypt/src/asn.c | 64 +++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 0f12f6402..500b39ebb 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -89,7 +89,7 @@ #ifdef HAVE_RTP_SYS /* uses parital structures */ #define XTIME(tl) (0) - #define XGMTIME(c) my_gmtime((c)) + #define XGMTIME(c, t) my_gmtime((c)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #elif defined(MICRIUM) #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) @@ -102,11 +102,11 @@ #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) #include #define XTIME(t1) pic32_time((t1)) - #define XGMTIME(c) gmtime((c)) + #define XGMTIME(c, t) gmtime((c)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #elif defined(FREESCALE_MQX) #define XTIME(t1) mqx_time((t1)) - #define XGMTIME(c) mqx_gmtime((c)) + #define XGMTIME(c, t) mqx_gmtime((c), (t)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #elif defined(WOLFSSL_MDK_ARM) #if defined(WOLFSSL_MDK5) @@ -119,7 +119,7 @@ #undef RNG #define RNG wolfSSL_RNG /*for avoiding name conflict in "stm32f2xx.h" */ #define XTIME(tl) (0) - #define XGMTIME(c) wolfssl_MDK_gmtime((c)) + #define XGMTIME(c, t) wolfssl_MDK_gmtime((c)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #elif defined(USER_TIME) /* user time, and gmtime compatible functions, there is a gmtime @@ -146,7 +146,7 @@ struct tm* gmtime(const time_t* timer); extern time_t XTIME(time_t * timer); - #define XGMTIME(c) gmtime((c)) + #define XGMTIME(c, t) gmtime((c)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #ifdef STACK_TRAP @@ -179,7 +179,7 @@ char *tm_zone; /* timezone abbreviation */ }; #endif - extern struct tm* XGMTIME(const time_t* timer); + extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp); #ifndef HAVE_VALIDATE_DATE #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) @@ -188,8 +188,8 @@ /* default */ /* uses complete facility */ #include - #define XTIME(tl) time((tl)) - #define XGMTIME(c) gmtime((c)) + #define XTIME(tl) time((tl)) + #define XGMTIME(c, t) gmtime((c)) #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #endif @@ -350,11 +350,9 @@ time_t mqx_time(time_t* timer) } /* CodeWarrior GCC toolchain only has gmtime_r(), no gmtime() */ -struct tm* mqx_gmtime(const time_t* clock) +struct tm* mqx_gmtime(const time_t* clock, struct tm* tmpTime) { - struct tm tmpTime; - - return gmtime_r(clock, &tmpTime); + return gmtime_r(clock, tmpTime); } #endif /* FREESCALE_MQX */ @@ -2399,8 +2397,16 @@ int ValidateDate(const byte* date, byte format, int dateType) time_t ltime; struct tm certTime; struct tm* localTime; + struct tm* tmpTime; int i = 0; +#ifdef FREESCALE_MQX + struct tm mqxTime; + tmpTime = &mqxTime; +#else + (void)tmpTime; +#endif + ltime = XTIME(0); XMEMSET(&certTime, 0, sizeof(certTime)); @@ -2428,7 +2434,7 @@ int ValidateDate(const byte* date, byte format, int dateType) return 0; } - localTime = XGMTIME(<ime); + localTime = XGMTIME(<ime, tmpTime); if (dateType == BEFORE) { if (DateLessThan(localTime, &certTime)) @@ -5220,6 +5226,18 @@ static int CopyValidity(byte* output, Cert* cert) #endif +/* for systems where mktime() doesn't normalize fully */ +static void RebuildTime(time_t* in, struct tm* out) +{ + #ifdef FREESCALE_MQX + out = localtime_r(in, out); + #else + (void)in; + (void)out; + #endif +} + + /* Set Date validity from now until now + daysValid */ static int SetValidity(byte* output, int daysValid) { @@ -5231,11 +5249,21 @@ static int SetValidity(byte* output, int daysValid) int seqSz; time_t ticks; + time_t normalTime; struct tm* now; + struct tm* tmpTime; struct tm local; +#ifdef FREESCALE_MQX + /* for use with MQX gmtime_r */ + struct tm mqxTime; + tmpTime = &mqxTime; +#else + (void)tmpTime; +#endif + ticks = XTIME(0); - now = XGMTIME(&ticks); + now = XGMTIME(&ticks, tmpTime); /* before now */ local = *now; @@ -5244,7 +5272,8 @@ static int SetValidity(byte* output, int daysValid) /* subtract 1 day for more compliance */ local.tm_mday -= 1; - mktime(&local); + normalTime = mktime(&local); + RebuildTime(&normalTime, &local); /* adjust */ local.tm_year += 1900; @@ -5252,7 +5281,7 @@ static int SetValidity(byte* output, int daysValid) SetTime(&local, before + beforeSz); beforeSz += ASN_GEN_TIME_SZ; - + /* after now + daysValid */ local = *now; after[0] = ASN_GENERALIZED_TIME; @@ -5260,7 +5289,8 @@ static int SetValidity(byte* output, int daysValid) /* add daysValid */ local.tm_mday += daysValid; - mktime(&local); + normalTime = mktime(&local); + RebuildTime(&normalTime, &local); /* adjust */ local.tm_year += 1900;