From 936312f77e9cbc0e04427a0c3479068ce7e88792 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Thu, 13 Feb 2020 12:32:59 -0600 Subject: [PATCH] Adding ERR_print_errors_cb --- src/ssl.c | 6 ++++ wolfcrypt/src/logging.c | 72 ++++++++++++++++++++++--------------- wolfssl/openssl/ssl.h | 1 + wolfssl/ssl.h | 2 ++ wolfssl/wolfcrypt/logging.h | 4 ++- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 3da79be3c..bf3cfba2d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -4377,6 +4377,12 @@ void wolfSSL_ERR_dump_errors_fp(XFILE fp) { wc_ERR_print_errors_fp(fp); } + +void wolfSSL_ERR_print_errors_cb (int (*cb)(const char *str, size_t len, + void *u), void *u) +{ + wc_ERR_print_errors_cb(cb, u); +} #endif #endif diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 6b6ab7512..b606ddb55 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -782,41 +782,57 @@ int wc_ERR_remove_state(void) return 0; } - #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) /* empties out the error queue into the file */ +static int wc_ERR_dump_to_file (const char *str, size_t len, void *u) +{ + XFILE fp = (XFILE ) u; + fprintf(fp, "%-*.*s\n", (int)len, (int)len, str); + return 0; +} + +/* This callback allows the application to provide a custom error printing + * function. */ +void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u), + void *u) +{ + WOLFSSL_ENTER("wc_ERR_print_errors_cb"); + + if (wc_LockMutex(&debug_mutex) != 0) + { + WOLFSSL_MSG("Lock debug mutex failed"); + } + else + { + /* free all nodes from error queue and print them to file */ + struct wc_error_queue *current; + struct wc_error_queue *next; + + current = (struct wc_error_queue *)wc_errors; + while (current != NULL) + { + next = current->next; + cb(current->error, strlen(current->error), u); + XFREE(current, current->heap, DYNAMIC_TYPE_LOG); + current = next; + } + + /* set global pointers to match having been freed */ + wc_errors = NULL; + wc_last_node = NULL; + + wc_UnLockMutex(&debug_mutex); + } +} + void wc_ERR_print_errors_fp(XFILE fp) { WOLFSSL_ENTER("wc_ERR_print_errors_fp"); - if (wc_LockMutex(&debug_mutex) != 0) - { - WOLFSSL_MSG("Lock debug mutex failed"); - } - else - { - /* free all nodes from error queue and print them to file */ - { - struct wc_error_queue *current; - struct wc_error_queue *next; - - current = (struct wc_error_queue *)wc_errors; - while (current != NULL) - { - next = current->next; - fprintf(fp, "%s\n", current->error); - XFREE(current, current->heap, DYNAMIC_TYPE_LOG); - current = next; - } - - /* set global pointers to match having been freed */ - wc_errors = NULL; - wc_last_node = NULL; - } - - wc_UnLockMutex(&debug_mutex); - } + /* Send all errors to the wc_ERR_dump_to_file function */ + wc_ERR_print_errors_cb(wc_ERR_dump_to_file,fp); } + #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */ #endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */ diff --git a/wolfssl/openssl/ssl.h b/wolfssl/openssl/ssl.h index 6c4c9757e..0edecbab5 100644 --- a/wolfssl/openssl/ssl.h +++ b/wolfssl/openssl/ssl.h @@ -769,6 +769,7 @@ wolfSSL_X509_STORE_set_verify_cb((WOLFSSL_X509_STORE *)(s), (WOLFSSL_X509_STORE_ #define ERR_get_error_line_data wolfSSL_ERR_get_error_line_data #define ERR_get_error wolfSSL_ERR_get_error #define ERR_print_errors_fp(file) wolfSSL_ERR_dump_errors_fp((file)) +#define ERR_print_errors_cb wolfSSL_ERR_print_errors_cb #define ERR_print_errors wolfSSL_ERR_print_errors #define ERR_clear_error wolfSSL_ERR_clear_error #define ERR_free_strings wolfSSL_ERR_free_strings diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 867ca7815..5d53934c7 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -1781,6 +1781,8 @@ enum { WOLFSSL_API void wolfSSL_ERR_print_errors_fp(XFILE, int err); #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) WOLFSSL_API void wolfSSL_ERR_dump_errors_fp(XFILE fp); +WOLFSSL_API void wolfSSL_ERR_print_errors_cb(int (*cb)(const char *str, + size_t len, void *u), void *u); #endif #endif WOLFSSL_API void wolfSSL_ERR_print_errors(WOLFSSL_BIO *bio); diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index 51d9b23b9..3dcd7de29 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -115,7 +115,9 @@ WOLFSSL_API void wolfSSL_Debugging_OFF(void); WOLFSSL_API int wc_SetLoggingHeap(void* h); WOLFSSL_API int wc_ERR_remove_state(void); #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) - WOLFSSL_API void wc_ERR_print_errors_fp(XFILE fp); + WOLFSSL_API void wc_ERR_print_errors_fp(XFILE fp); + WOLFSSL_API void wc_ERR_print_errors_cb(int (*cb)(const char *str, + size_t len, void *u), void *u); #endif #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */