diff --git a/src/io.c b/src/io.c index 3c54becc3..46a8c44c7 100644 --- a/src/io.c +++ b/src/io.c @@ -39,7 +39,7 @@ /* if user writes own I/O callbacks they can define WOLFSSL_USER_IO to remove automatic setting of default I/O functions EmbedSend() and EmbedReceive() - but they'll still need SetCallback xxx() at end of file + but they'll still need SetCallback xxx() at end of file */ #ifndef WOLFSSL_USER_IO @@ -86,8 +86,13 @@ #include #endif #include - #if !(defined(DEVKITPRO) || defined(HAVE_RTP_SYS) || defined(EBSNET)) \ - && !(defined(WOLFSSL_PICOTCP)) + + #if defined(HAVE_RTP_SYS) + #include + #elif defined(EBSNET) + #include "rtipapi.h" /* errno */ + #include "socket.h" + #elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP) #include #include #include @@ -98,13 +103,6 @@ #include #endif #endif - #ifdef HAVE_RTP_SYS - #include - #endif - #ifdef EBSNET - #include "rtipapi.h" /* errno */ - #include "socket.h" - #endif #endif #endif /* USE_WINDOWS_API */ @@ -112,7 +110,7 @@ #include #endif -#ifdef USE_WINDOWS_API +#ifdef USE_WINDOWS_API /* no epipe yet */ #ifndef WSAEPIPE #define WSAEPIPE -12345 @@ -218,8 +216,8 @@ #endif -/* Translates return codes returned from - * send() and recv() if need be. +/* Translates return codes returned from + * send() and recv() if need be. */ static INLINE int TranslateReturnCode(int old, int sd) { @@ -247,7 +245,7 @@ static INLINE int TranslateReturnCode(int old, int sd) static INLINE int LastError(void) { -#ifdef USE_WINDOWS_API +#ifdef USE_WINDOWS_API return WSAGetLastError(); #elif defined(EBSNET) return xn_getlasterror(); @@ -372,7 +370,7 @@ int EmbedSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) return WOLFSSL_CBIO_ERR_GENERAL; } } - + return sent; } @@ -513,7 +511,7 @@ int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx) return WOLFSSL_CBIO_ERR_GENERAL; } } - + return sent; } @@ -709,14 +707,14 @@ static int decode_url(const char* url, int urlSz, else { int i, cur; - + /* need to break the url down into scheme, address, and port */ /* "http://example.com:8080/" */ /* "http://[::1]:443/" */ if (XSTRNCMP(url, "http://", 7) == 0) { cur = 7; } else cur = 0; - + i = 0; if (url[cur] == '[') { cur++; @@ -734,7 +732,7 @@ static int decode_url(const char* url, int urlSz, } outName[i] = 0; /* Need to pick out the path after the domain name */ - + if (cur < urlSz && url[cur] == ':') { char port[6]; int j; @@ -745,7 +743,7 @@ static int decode_url(const char* url, int urlSz, i < 6) { port[i++] = url[cur++]; } - + for (j = 0; j < i; j++) { if (port[j] < '0' || port[j] > '9') return -1; bigPort = (bigPort * 10) + (port[j] - '0'); @@ -754,7 +752,7 @@ static int decode_url(const char* url, int urlSz, } else *outPort = 80; - + if (cur < urlSz && url[cur] == '/') { i = 0; while (cur < urlSz && url[cur] != 0 && i < 80) { @@ -841,7 +839,7 @@ static int process_http_response(int sfd, byte** respBuf, WOLFSSL_MSG("process_http_response not ocsp-response"); return -1; } - + if (state == phr_http_start) state = phr_have_type; else if (state == phr_have_length) state = phr_wait_end; else { @@ -861,7 +859,7 @@ static int process_http_response(int sfd, byte** respBuf, return -1; } } - + start = end + 2; } } while (state != phr_http_end); @@ -912,7 +910,7 @@ int EmbedOcspLookup(void* ctx, const char* url, int urlSz, path = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (path == NULL) return -1; - + domainName = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (domainName == NULL) { XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -935,7 +933,7 @@ int EmbedOcspLookup(void* ctx, const char* url, int urlSz, /* Note, the library uses the EmbedOcspRespFree() callback to * free this buffer. */ int httpBufSz = SCRATCH_BUFFER_SIZE; - byte* httpBuf = (byte*)XMALLOC(httpBufSz, NULL, + byte* httpBuf = (byte*)XMALLOC(httpBufSz, NULL, DYNAMIC_TYPE_OCSP); if (httpBuf == NULL) { @@ -1002,13 +1000,13 @@ WOLFSSL_API void wolfSSL_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend) WOLFSSL_API void wolfSSL_SetIOReadCtx(WOLFSSL* ssl, void *rctx) { - ssl->IOCB_ReadCtx = rctx; + ssl->IOCB_ReadCtx = rctx; } WOLFSSL_API void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *wctx) { - ssl->IOCB_WriteCtx = wctx; + ssl->IOCB_WriteCtx = wctx; } @@ -1032,7 +1030,7 @@ WOLFSSL_API void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl) WOLFSSL_API void wolfSSL_SetIOReadFlags(WOLFSSL* ssl, int flags) { - ssl->rflags = flags; + ssl->rflags = flags; } @@ -1052,14 +1050,14 @@ WOLFSSL_API void wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX* ctx, CallbackGenCookie cb WOLFSSL_API void wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx) { - ssl->IOCB_CookieCtx = ctx; + ssl->IOCB_CookieCtx = ctx; } WOLFSSL_API void* wolfSSL_GetCookieCtx(WOLFSSL* ssl) { if (ssl) - return ssl->IOCB_CookieCtx; + return ssl->IOCB_CookieCtx; return NULL; } diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 1f96f0ac5..bc1c4c827 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -95,62 +95,44 @@ #endif -#ifdef HAVE_RTP_SYS +#if defined(HAVE_RTP_SYS) /* uses parital structures */ - #define XTIME(tl) (0) - #define XGMTIME(c, t) my_gmtime((c)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) + #define XTIME(tl) (0) + #define XGMTIME(c, t) rtpsys_gmtime((c)) + #elif defined(MICRIUM) #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) - #define XVALIDATE_DATE(d,f,t) NetSecure_ValidateDateHandler((d),(f),(t)) + #define XVALIDATE_DATE(d, f, t) NetSecure_ValidateDateHandler((d), (f), (t)) #else #define XVALIDATE_DATE(d, f, t) (0) #endif #define NO_TIME_H /* since Micrium not defining XTIME or XGMTIME, CERT_GEN not available */ + #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) #include - #define XTIME(t1) pic32_time((t1)) - #define XGMTIME(c, t) gmtime((c)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) + #define XTIME(t1) pic32_time((t1)) + #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) - #define XTIME(t1) mqx_time((t1)) - #define XGMTIME(c, t) mqx_gmtime((c), (t)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) + #define XTIME(t1) mqx_time((t1)) + #define HAVE_GMTIME_R + #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) #include - #define XTIME(t1) ksdk_time((t1)) - #define XGMTIME(c, t) gmtime((c)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) + #define XTIME(t1) ksdk_time((t1)) #elif defined(USER_TIME) /* user time, and gmtime compatible functions, there is a gmtime implementation here that WINCE uses, so really just need some ticks since the EPOCH */ - - struct tm { - int tm_sec; /* seconds after the minute [0-60] */ - int tm_min; /* minutes after the hour [0-59] */ - int tm_hour; /* hours since midnight [0-23] */ - int tm_mday; /* day of the month [1-31] */ - int tm_mon; /* months since January [0-11] */ - int tm_year; /* years since 1900 */ - int tm_wday; /* days since Sunday [0-6] */ - int tm_yday; /* days since January 1 [0-365] */ - int tm_isdst; /* Daylight Savings Time flag */ - long tm_gmtoff; /* offset from CUT in seconds */ - char *tm_zone; /* timezone abbreviation */ - }; - typedef long time_t; + #define WOLFSSL_GMTIME + #define USE_WOLF_TM + #define USE_WOLF_TIME_T /* forward declaration */ - struct tm* gmtime(const time_t* timer); extern time_t XTIME(time_t * timer); - #define XGMTIME(c, t) gmtime((c)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) - #ifdef STACK_TRAP /* for stack trap tracking, don't call os gmtime on OS X/linux, uses a lot of stack spce */ @@ -160,63 +142,75 @@ #elif defined(TIME_OVERRIDES) /* user would like to override time() and gmtime() functionality */ - #ifndef HAVE_TIME_T_TYPE - typedef long time_t; + #define USE_WOLF_TIME_T #endif - extern time_t XTIME(time_t * timer); - #ifndef HAVE_TM_TYPE - struct tm { - int tm_sec; /* seconds after the minute [0-60] */ - int tm_min; /* minutes after the hour [0-59] */ - int tm_hour; /* hours since midnight [0-23] */ - int tm_mday; /* day of the month [1-31] */ - int tm_mon; /* months since January [0-11] */ - int tm_year; /* years since 1900 */ - int tm_wday; /* days since Sunday [0-6] */ - int tm_yday; /* days since January 1 [0-365] */ - int tm_isdst; /* Daylight Savings Time flag */ - long tm_gmtoff; /* offset from CUT in seconds */ - char *tm_zone; /* timezone abbreviation */ - }; + #define USE_WOLF_TM #endif - 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)) - #endif + /* forward declarations */ + extern time_t XTIME(time_t * timer); + extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp); + #define NEED_TMP_TIME #elif defined(IDIRECT_DEV_TIME) /*Gets the timestamp from cloak software owned by VT iDirect in place of time() from */ #include - #define XTIME(t1) idirect_time((t1)) - #define XGMTIME(c) gmtime((c)) - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) + #define XTIME(t1) idirect_time((t1)) + +#elif defined(_WIN32_WCE) + #include + #define XTIME(t1) windows_time((t1)) + #define WOLFSSL_GMTIME #else /* default */ /* uses complete facility */ #include - #define XTIME(tl) time((tl)) - #ifdef HAVE_GMTIME_R - #define XGMTIME(c, t) gmtime_r((c), (t)) - #define NEED_TMP_TIME - #else - #define XGMTIME(c, t) gmtime((c)) - #endif - #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) #endif -#ifdef _WIN32_WCE -/* no time() or gmtime() even though in time.h header?? */ +/* Map default time functions */ +#if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME) + #define XTIME(tl) time((tl)) +#endif +#if !defined(XGMTIME) && !defined(TIME_OVERRIDES) + #ifdef HAVE_GMTIME_R + #define XGMTIME(c, t) gmtime_r((c), (t)) + #define NEED_TMP_TIME + #else + #define XGMTIME(c, t) gmtime((c)) + #endif +#endif +#if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE) + #define USE_WOLF_VALIDDATE + #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) +#endif -#include +#if defined(USE_WOLF_TM) + struct tm { + int tm_sec; /* seconds after the minute [0-60] */ + int tm_min; /* minutes after the hour [0-59] */ + int tm_hour; /* hours since midnight [0-23] */ + int tm_mday; /* day of the month [1-31] */ + int tm_mon; /* months since January [0-11] */ + int tm_year; /* years since 1900 */ + int tm_wday; /* days since Sunday [0-6] */ + int tm_yday; /* days since January 1 [0-365] */ + int tm_isdst; /* Daylight Savings Time flag */ + long tm_gmtoff; /* offset from CUT in seconds */ + char *tm_zone; /* timezone abbreviation */ + }; +#endif /* USE_WOLF_TM */ +#if defined(USE_WOLF_TIME_T) + typedef long time_t; +#endif -time_t time(time_t* timer) +#if defined(_WIN32_WCE) +time_t windows_time(time_t* timer) { SYSTEMTIME sysTime; FILETIME fTime; @@ -238,10 +232,9 @@ time_t time(time_t* timer) return *timer; } - #endif /* _WIN32_WCE */ -#if defined( _WIN32_WCE ) || defined( USER_TIME ) +#if defined(WOLFSSL_GMTIME) struct tm* gmtime(const time_t* timer) { #define YEAR0 1900 @@ -289,15 +282,13 @@ struct tm* gmtime(const time_t* timer) return ret; } - -#endif /* _WIN32_WCE || USER_TIME */ +#endif /* WOLFSSL_GMTIME */ -#ifdef HAVE_RTP_SYS - +#if defined(HAVE_RTP_SYS) #define YEAR0 1900 -struct tm* my_gmtime(const time_t* timer) /* has a gmtime() but hangs */ +struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */ { static struct tm st_time; struct tm* ret = &st_time; @@ -365,12 +356,6 @@ time_t mqx_time(time_t* timer) return *timer; } -/* CodeWarrior GCC toolchain only has gmtime_r(), no gmtime() */ -struct tm* mqx_gmtime(const time_t* clock, struct tm* tmpTime) -{ - return gmtime_r(clock, tmpTime); -} - #endif /* FREESCALE_MQX */ #if defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) @@ -390,7 +375,7 @@ time_t ksdk_time(time_t* timer) #endif /* FREESCALE_KSDK_BM */ -#ifdef WOLFSSL_TIRTOS +#if defined(WOLFSSL_TIRTOS) time_t XTIME(time_t * timer) { @@ -406,6 +391,7 @@ time_t XTIME(time_t * timer) #endif /* WOLFSSL_TIRTOS */ + static INLINE word32 btoi(byte b) { return b - 0x30; @@ -496,7 +482,7 @@ time_t idirect_time(time_t * timer) return sec; } -#endif +#endif /* IDIRECT_DEV_TIME */ WOLFSSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len, @@ -2954,7 +2940,7 @@ static int GetName(DecodedCert* cert, int nameType) } -#ifndef NO_TIME_H +#if !defined(NO_TIME_H) && defined(USE_WOLF_VALIDDATE) /* to the second */ static int DateGreaterThan(const struct tm* a, const struct tm* b) @@ -2992,7 +2978,6 @@ static INLINE int DateLessThan(const struct tm* a, const struct tm* b) return DateGreaterThan(b,a); } - /* like atoi but only use first byte */ /* Make sure before and after dates are valid */ int ValidateDate(const byte* date, byte format, int dateType) @@ -3006,8 +2991,7 @@ int ValidateDate(const byte* date, byte format, int dateType) int diffHH = 0 ; int diffMM = 0 ; int diffSign = 0 ; -#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) || \ - defined(TIME_OVERRIDES) || defined(NEED_TMP_TIME) +#if defined(NEED_TMP_TIME) struct tm tmpTimeStorage; tmpTime = &tmpTimeStorage; #else @@ -3065,8 +3049,7 @@ int ValidateDate(const byte* date, byte format, int dateType) return 1; } - -#endif /* NO_TIME_H */ +#endif /* !NO_TIME_H && USE_WOLF_VALIDDATE */ static int GetDate(DecodedCert* cert, int dateType) @@ -6022,8 +6005,7 @@ static int SetValidity(byte* output, int daysValid) struct tm* tmpTime = NULL; struct tm local; -#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) || \ - defined(TIME_OVERRIDES) || defined(NEED_TMP_TIME) +#if defined(NEED_TMP_TIME) /* for use with gmtime_r */ struct tm tmpTimeStorage; tmpTime = &tmpTimeStorage;