diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 68c55f366..4973efd38 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -32,15 +32,6 @@ #include -#ifdef __cplusplus - extern "C" { -#endif - WOLFSSL_API int wolfSSL_Debugging_ON(void); - WOLFSSL_API void wolfSSL_Debugging_OFF(void); -#ifdef __cplusplus - } -#endif - #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) static wolfSSL_Mutex debug_mutex; /* mutex for access to debug structure */ @@ -63,7 +54,7 @@ static struct wc_error_queue* wc_last_node; -#if defined(DEBUG_WOLFSSL) +#ifdef DEBUG_WOLFSSL /* Set these to default values initially. */ static wolfSSL_Logging_cb log_function = NULL; @@ -72,17 +63,12 @@ static int loggingEnabled = 0; #endif /* DEBUG_WOLFSSL */ +/* allow this to be set to NULL, so logs can be redirected to default output */ int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f) { #ifdef DEBUG_WOLFSSL - int res = 0; - - if (f) - log_function = f; - else - res = BAD_FUNC_ARG; - - return res; + log_function = f; + return 0; #else (void)f; return NOT_COMPILED_IN; @@ -122,6 +108,8 @@ void wolfSSL_Debugging_OFF(void) int sprintf(char* buf, const char *fmt, ...); #elif defined(MICRIUM) #include +#elif defined(WOLFSSL_USER_LOG) + /* user includes their own headers */ #else #include /* for default printf stuff */ #endif @@ -135,30 +123,32 @@ static void wolfssl_log(const int logLevel, const char *const logMessage) if (log_function) log_function(logLevel, logMessage); else { - if (loggingEnabled) { -#if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) - dc_log_printf("%s\n", logMessage); -#elif defined(MICRIUM) - BSP_Ser_Printf("%s\r\n", logMessage); -#elif defined(WOLFSSL_MDK_ARM) - fflush(stdout) ; - printf("%s\n", logMessage); - fflush(stdout) ; +#if defined(WOLFSSL_USER_LOG) + WOLFSSL_USER_LOG(logMessage); #elif defined(WOLFSSL_LOG_PRINTF) - printf("%s\n", logMessage); + printf("%s\n", logMessage); + +#elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) + dc_log_printf("%s\n", logMessage); +#elif defined(MICRIUM) + BSP_Ser_Printf("%s\r\n", logMessage); +#elif defined(WOLFSSL_MDK_ARM) + fflush(stdout) ; + printf("%s\n", logMessage); + fflush(stdout) ; #elif defined(WOLFSSL_UTASKER) - fnDebugMsg((char*)logMessage); - fnDebugMsg("\r\n"); + fnDebugMsg((char*)logMessage); + fnDebugMsg("\r\n"); #elif defined(MQX_USE_IO_OLD) - fprintf(_mqxio_stderr, "%s\n", logMessage); + fprintf(_mqxio_stderr, "%s\n", logMessage); + #else - fprintf(stderr, "%s\n", logMessage); + fprintf(stderr, "%s\n", logMessage); #endif - } } } - +#ifndef WOLFSSL_DEBUG_ERRORS_ONLY void WOLFSSL_MSG(const char* msg) { if (loggingEnabled) @@ -223,58 +213,74 @@ void WOLFSSL_LEAVE(const char* msg, int ret) wolfssl_log(LEAVE_LOG , buffer); } } -#endif /* DEBUG_WOLFSSL */ +#endif /* !WOLFSSL_DEBUG_ERRORS_ONLY */ +#endif /* DEBUG_WOLFSSL */ /* * When using OPENSSL_EXTRA or DEBUG_WOLFSSL_VERBOSE macro then WOLFSSL_ERROR is * mapped to new funtion WOLFSSL_ERROR_LINE which gets the line # and function * name where WOLFSSL_ERROR is called at. */ -#if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY) - #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) +#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) + +#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line, - const char* file, void* usrCtx) - #else + const char* file, void* usrCtx) +#else void WOLFSSL_ERROR(int error) - #endif +#endif { - #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_NGINX) - if (loggingEnabled && error != WC_PENDING_E) - #endif +#ifdef WOLFSSL_ASYNC_CRYPT + if (error != WC_PENDING_E) +#endif { char buffer[WOLFSSL_MAX_ERROR_SZ]; - #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) - (void)usrCtx; /* a user ctx for future flexibility */ - (void)func; - if (wc_LockMutex(&debug_mutex) != 0) { - WOLFSSL_MSG("Lock debug mutex failed"); - XSNPRINTF(buffer, sizeof(buffer), - "wolfSSL error occurred, error = %d", error); - } - else { - if (error < 0) error = error - (2*error); /*get absolute value*/ - XSNPRINTF(buffer, sizeof(buffer), - "wolfSSL error occurred, error = %d line:%d file:%s", - error, line, file); - if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) { - WOLFSSL_MSG("Error creating logging node"); - /* with void function there is no return here, continue on - * to unlock mutex and log what buffer was created. */ - } + #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) + (void)usrCtx; /* a user ctx for future flexibility */ + (void)func; - wc_UnLockMutex(&debug_mutex); - } - #else + if (wc_LockMutex(&debug_mutex) != 0) { + WOLFSSL_MSG("Lock debug mutex failed"); XSNPRINTF(buffer, sizeof(buffer), "wolfSSL error occurred, error = %d", error); - #endif - #ifdef DEBUG_WOLFSSL - wolfssl_log(ERROR_LOG , buffer); - #endif + } + else { + if (error < 0) + error = error - (2 * error); /* get absolute value */ + XSNPRINTF(buffer, sizeof(buffer), + "wolfSSL error occurred, error = %d line:%d file:%s", + error, line, file); + if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) { + WOLFSSL_MSG("Error creating logging node"); + /* with void function there is no return here, continue on + * to unlock mutex and log what buffer was created. */ + } + + wc_UnLockMutex(&debug_mutex); + } + #else + XSNPRINTF(buffer, sizeof(buffer), + "wolfSSL error occurred, error = %d", error); + #endif + + #ifdef DEBUG_WOLFSSL + if (loggingEnabled) + wolfssl_log(ERROR_LOG , buffer); + #endif } } +void WOLFSSL_ERROR_MSG(const char* msg) +{ +#ifdef DEBUG_WOLFSSL + if (loggingEnabled) + wolfssl_log(ERROR_LOG , msg); +#else + (void)msg; +#endif +} + #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */ #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index cce0db4b0..bcf9dcd2d 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -324,16 +324,6 @@ int memcb_test(void); int blob_test(void); #endif -#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \ - !defined(OPENSSL_EXTRA) && !defined(HAVE_STACK_SIZE) -#ifdef __cplusplus - extern "C" { -#endif - WOLFSSL_API int wolfSSL_Debugging_ON(void); -#ifdef __cplusplus - } /* extern "C" */ -#endif -#endif /* General big buffer size for many tests. */ #define FOURK_BUF 4096 @@ -861,12 +851,10 @@ int wolfcrypt_test(void* args) printf( "mp test passed!\n"); #endif -#ifdef HAVE_VALGRIND if ( (ret = logging_test()) != 0) return err_sys("logging test failed!\n", ret); else printf( "logging test passed!\n"); -#endif if ( (ret = mutex_test()) != 0) return err_sys("mutex test failed!\n", ret); @@ -15092,24 +15080,15 @@ done: } #endif -#ifdef HAVE_VALGRIND -/* Need a static build to have access to symbols. */ - -#ifndef WOLFSSL_SSL_H -/* APIs hiding in ssl.h */ -extern int wolfSSL_Debugging_ON(void); -extern void wolfSSL_Debugging_OFF(void); -#endif - #ifdef DEBUG_WOLFSSL -static int log_cnt = 0; +static THREAD_LS_T int log_cnt = 0; static void my_Logging_cb(const int logLevel, const char *const logMessage) { (void)logLevel; (void)logMessage; log_cnt++; } -#endif +#endif /* DEBUG_WOLFSSL */ int logging_test(void) { @@ -15117,55 +15096,60 @@ int logging_test(void) const char* msg = "Testing, testing. 1, 2, 3, 4 ..."; byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; byte b[256]; - size_t i; + int i; - for (i = 0; i < sizeof(b); i++) + for (i = 0; i < (int)sizeof(b); i++) b[i] = i; if (wolfSSL_Debugging_ON() != 0) return -7900; - if (wolfSSL_SetLoggingCb(NULL) != BAD_FUNC_ARG) + + if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0) return -7901; WOLFSSL_MSG(msg); WOLFSSL_BUFFER(a, sizeof(a)); WOLFSSL_BUFFER(b, sizeof(b)); WOLFSSL_BUFFER(NULL, 0); + WOLFSSL_ERROR(MEMORY_E); + WOLFSSL_ERROR_MSG(msg); + /* turn off logs */ wolfSSL_Debugging_OFF(); + /* capture log count */ + i = log_cnt; + + /* validate no logs are output when disabled */ WOLFSSL_MSG(msg); + WOLFSSL_BUFFER(a, sizeof(a)); WOLFSSL_BUFFER(b, sizeof(b)); + WOLFSSL_BUFFER(NULL, 0); + WOLFSSL_ERROR(MEMORY_E); + WOLFSSL_ERROR_MSG(msg); - if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0) - return -7902; - - wolfSSL_Debugging_OFF(); - - WOLFSSL_MSG(msg); - WOLFSSL_BUFFER(b, sizeof(b)); - - if (log_cnt != 0) - return -7903; - if (wolfSSL_Debugging_ON() != 0) + /* check the logs were disabled */ + if (i != log_cnt) return -7904; - WOLFSSL_MSG(msg); - WOLFSSL_BUFFER(b, sizeof(b)); + /* restore callback and leave logging enabled */ + wolfSSL_SetLoggingCb(NULL); + wolfSSL_Debugging_ON(); + + /* suppress unused args */ + (void)a; + (void)b; - /* One call for each line of output. */ - if (log_cnt != 17) - return -7905; #else if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN) return -7906; wolfSSL_Debugging_OFF(); if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN) return -7907; -#endif +#endif /* DEBUG_WOLFSSL */ return 0; } -#endif + int mutex_test(void) { diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index d157314d3..a7594402d 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -30,6 +30,7 @@ /* for users not using preprocessor flags*/ #include #include +#include #ifdef HAVE_WOLF_EVENT #include @@ -1321,11 +1322,6 @@ WOLFSSL_API const char* wolfSSL_lib_version(void); /* which library version do we have in hex */ WOLFSSL_API unsigned int wolfSSL_lib_version_hex(void); -/* turn logging on, only if compiled in */ -WOLFSSL_API int wolfSSL_Debugging_ON(void); -/* turn logging off */ -WOLFSSL_API void wolfSSL_Debugging_OFF(void); - /* do accept or connect depedning on side */ WOLFSSL_API int wolfSSL_negotiate(WOLFSSL* ssl); /* turn on wolfSSL data compression */ diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index a9db18d0e..4f11bed23 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -33,7 +33,7 @@ #endif -enum CYA_Log_Levels { +enum wc_LogLevels { ERROR_LOG = 0, INFO_LOG, ENTER_LOG, @@ -42,10 +42,16 @@ enum CYA_Log_Levels { }; typedef void (*wolfSSL_Logging_cb)(const int logLevel, - const char *const logMessage); + const char *const logMessage); WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); +/* turn logging on, only if compiled in */ +WOLFSSL_API int wolfSSL_Debugging_ON(void); +/* turn logging off */ +WOLFSSL_API void wolfSSL_Debugging_OFF(void); + + #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) WOLFSSL_LOCAL int wc_LoggingInit(void); WOLFSSL_LOCAL int wc_LoggingCleanup(void); @@ -59,9 +65,10 @@ WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) WOLFSSL_API void wc_ERR_print_errors_fp(FILE* fp); #endif -#endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */ +#endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */ -#ifdef DEBUG_WOLFSSL + +#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_DEBUG_ERRORS_ONLY) #if defined(_WIN32) #if defined(INTIME_RTOS) #define __func__ NULL @@ -73,15 +80,15 @@ WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); /* a is prepended to m and b is appended, creating a log msg a + m + b */ #define WOLFSSL_LOG_CAT(a, m, b) #a " " m " " #b - void WOLFSSL_ENTER(const char* msg); - void WOLFSSL_LEAVE(const char* msg, int ret); + WOLFSSL_API void WOLFSSL_ENTER(const char* msg); + WOLFSSL_API void WOLFSSL_LEAVE(const char* msg, int ret); #define WOLFSSL_STUB(m) \ WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented)) - void WOLFSSL_MSG(const char* msg); - void WOLFSSL_BUFFER(const byte* buffer, word32 length); + WOLFSSL_API void WOLFSSL_MSG(const char* msg); + WOLFSSL_API void WOLFSSL_BUFFER(const byte* buffer, word32 length); -#else /* DEBUG_WOLFSSL */ +#else #define WOLFSSL_ENTER(m) #define WOLFSSL_LEAVE(m, r) @@ -90,18 +97,23 @@ WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); #define WOLFSSL_MSG(m) #define WOLFSSL_BUFFER(b, l) -#endif /* DEBUG_WOLFSSL */ +#endif /* DEBUG_WOLFSSL && !WOLFSSL_DEBUG_ERRORS_ONLY */ -#if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY) - #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) - void WOLFSSL_ERROR_LINE(int err, const char* func, unsigned int line, +#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) + + #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) + WOLFSSL_API void WOLFSSL_ERROR_LINE(int err, const char* func, unsigned int line, const char* file, void* ctx); - #define WOLFSSL_ERROR(x) WOLFSSL_ERROR_LINE((x), __func__, __LINE__, __FILE__,NULL) + #define WOLFSSL_ERROR(x) \ + WOLFSSL_ERROR_LINE((x), __func__, __LINE__, __FILE__, NULL) #else - void WOLFSSL_ERROR(int); + WOLFSSL_API void WOLFSSL_ERROR(int err); #endif + WOLFSSL_API void WOLFSSL_ERROR_MSG(const char* msg); + #else #define WOLFSSL_ERROR(e) + #define WOLFSSL_ERROR_MSG(m) #endif #ifdef __cplusplus