diff --git a/doc/dox_comments/header_files/ssl.h b/doc/dox_comments/header_files/ssl.h index 27fe0e759..ca208ce56 100644 --- a/doc/dox_comments/header_files/ssl.h +++ b/doc/dox_comments/header_files/ssl.h @@ -4881,6 +4881,35 @@ WOLFSSL_API long wolfSSL_get_verify_result(const WOLFSSL *ssl); */ WOLFSSL_API void wolfSSL_ERR_print_errors_fp(FILE*, int err); +/*! + \ingroup Debug + + \brief This function uses the provided callback to handle error reporting. + The callback function is executed for each error line. The string, length, + and userdata are passed into the callback parameters. + + \return none No returns. + + \param cb the callback function. + \param u userdata to pass into the callback function. + + _Example_ + \code + int error_cb(const char *str, size_t len, void *u) + { fprintf((FILE*)u, "%-*.*s\n", (int)len, (int)len, str); return 0; } + ... + FILE* fp = ... + wolfSSL_ERR_print_errors_cb(error_cb, fp); + \endcode + + \sa wolfSSL_get_error + \sa wolfSSL_ERR_error_string + \sa wolfSSL_ERR_error_string_n + \sa wolfSSL_load_error_strings +*/ +WOLFSSL_API void wolfSSL_ERR_print_errors_cb ( + int (*cb)(const char *str, size_t len, void *u), void *u); + /*! \brief The function sets the client_psk_cb member of the WOLFSSL_CTX structure. diff --git a/tests/api.c b/tests/api.c index a2807e9aa..c1043bc5b 100644 --- a/tests/api.c +++ b/tests/api.c @@ -23229,6 +23229,41 @@ static void test_wolfSSL_ERR_print_errors(void) #endif } +#if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \ + defined(DEBUG_WOLFSSL) +static int test_wolfSSL_error_cb(const char *str, size_t len, void *u) +{ + wolfSSL_BIO_write((BIO*)u, str, len); + return 0; +} +#endif + +static void test_wolfSSL_ERR_print_errors_cb(void) +{ + #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \ + defined(DEBUG_WOLFSSL) + BIO* bio; + char buf[1024]; + + printf(testingFmt, "wolfSSL_ERR_print_errors_cb()"); + + AssertNotNull(bio = BIO_new(BIO_s_mem())); + ERR_clear_error(); /* clear out any error nodes */ + ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0); + ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100); + + ERR_print_errors_cb(test_wolfSSL_error_cb, bio); + AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108); + AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c", + buf, 53), 0); + AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c", + buf + 53, 55), 0); + AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0); + + BIO_free(bio); + printf(resultFmt, passed); + #endif +} static void test_wolfSSL_HMAC(void) { @@ -30644,6 +30679,7 @@ void ApiTest(void) #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) test_wolfSSL_ERR_peek_last_error_line(); #endif + test_wolfSSL_ERR_print_errors_cb(); test_wolfSSL_set_options(); test_wolfSSL_sk_SSL_CIPHER(); test_wolfSSL_X509_STORE_CTX(); diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index b606ddb55..0c818aa71 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -798,6 +798,11 @@ void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u), { WOLFSSL_ENTER("wc_ERR_print_errors_cb"); + if (cb == NULL) { + /* Invalid param */ + return; + } + if (wc_LockMutex(&debug_mutex) != 0) { WOLFSSL_MSG("Lock debug mutex failed"); @@ -830,7 +835,7 @@ void wc_ERR_print_errors_fp(XFILE fp) WOLFSSL_ENTER("wc_ERR_print_errors_fp"); /* Send all errors to the wc_ERR_dump_to_file function */ - wc_ERR_print_errors_cb(wc_ERR_dump_to_file,fp); + wc_ERR_print_errors_cb(wc_ERR_dump_to_file, fp); } #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */