#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
#include <wolfssl/error-ssl.h>
#endif
#ifdef WOLFSSL_FUNC_TIME
static double wc_func_start[WC_FUNC_COUNT];
static double wc_func_time[WC_FUNC_COUNT] = { 0, };
static const char* wc_func_name[WC_FUNC_COUNT] = {
"SendHelloRequest",
"DoHelloRequest",
"SendClientHello",
"DoClientHello",
"SendServerHello",
"DoServerHello",
"SendEncryptedExtensions",
"DoEncryptedExtensions",
"SendCertificateRequest",
"DoCertificateRequest",
"SendCertificate",
"DoCertificate",
"SendCertificateVerify",
"DoCertificateVerify",
"SendFinished",
"DoFinished",
"SendKeyUpdate",
"DoKeyUpdate",
"SendEarlyData",
"DoEarlyData",
"SendNewSessionTicket",
"DoNewSessionTicket",
"SendServerHelloDone",
"DoServerHelloDone",
"SendTicket",
"DoTicket",
"SendClientKeyExchange",
"DoClientKeyExchange",
"SendCertificateStatus",
"DoCertificateStatus",
"SendServerKeyExchange",
"DoServerKeyExchange",
"SendEarlyData",
"DoEarlyData",
};
#include <sys/time.h>
static WC_INLINE double current_time(int reset)
{
struct timeval tv;
gettimeofday(&tv, 0);
(void)reset;
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}
#endif
#ifdef HAVE_WC_INTROSPECTION
const char *wolfSSL_configure_args(void) {
#ifdef LIBWOLFSSL_CONFIGURE_ARGS
return " " LIBWOLFSSL_CONFIGURE_ARGS " ";
#else
return NULL;
#endif
}
PEDANTIC_EXTENSION const char *wolfSSL_global_cflags(void) {
#ifdef LIBWOLFSSL_GLOBAL_CFLAGS
return " " LIBWOLFSSL_GLOBAL_CFLAGS " ";
#else
return NULL;
#endif
}
#endif
#ifdef HAVE_STACK_SIZE_VERBOSE
THREAD_LS_T unsigned char *StackSizeCheck_myStack = NULL;
THREAD_LS_T size_t StackSizeCheck_stackSize = 0;
THREAD_LS_T size_t StackSizeCheck_stackSizeHWM = 0;
THREAD_LS_T size_t *StackSizeCheck_stackSizeHWM_ptr = 0;
THREAD_LS_T void *StackSizeCheck_stackOffsetPointer = 0;
#endif
#if defined(DEBUG_WOLFSSL) || \
(defined(WOLFSSL_DEBUG_CERTS) && !defined(NO_WOLFSSL_DEBUG_CERTS))
static wolfSSL_Logging_cb LogFunction = NULL;
#ifndef WOLFSSL_LOGGINGENABLED_DEFAULT
#define WOLFSSL_LOGGINGENABLED_DEFAULT 0
#endif
static int loggingEnabled = WOLFSSL_LOGGINGENABLED_DEFAULT;
#ifndef WOLFSSL_CERT_LOG_ENABLED_DEFAULT
#define WOLFSSL_CERT_LOG_ENABLED_DEFAULT 0
#endif
#ifndef NO_WOLFSSL_DEBUG_CERTS
static int loggingCertEnabled = WOLFSSL_CERT_LOG_ENABLED_DEFAULT;
#endif
THREAD_LS_T const char* log_prefix = NULL;
#if defined(WOLFSSL_APACHE_MYNEWT)
#include "log/log.h"
static struct log mynewt_log;
#define WOLFSSL_APACHE_MYNEWT_NOT_INITALIZED 0
#define WOLFSSL_APACHE_MYNEWT_INITALIZED 1
static int loggingApacheNewtRegistered = WOLFSSL_APACHE_MYNEWT_NOT_INITALIZED;
#endif
#endif
int wolfSSL_CertDebugging_ON(void)
{
#if defined(WOLFSSL_DEBUG_CERTS) || defined(DEBUG_WOLFSSL)
#if defined(NO_WOLFSSL_DEBUG_CERTS)
return NOT_COMPILED_IN;
#else
loggingCertEnabled = 1;
#endif
#if defined(WOLFSSL_APACHE_MYNEWT)
if (loggingApacheNewtRegistered != WOLFSSL_APACHE_MYNEWT_INITALIZED) {
log_register("wolfcrypt", &mynewt_log, &log_console_handler,
NULL, LOG_SYSLEVEL);
loggingApacheNewtRegistered = WOLFSSL_APACHE_MYNEWT_INITALIZED;
}
#endif
return 0;
#else
return NOT_COMPILED_IN;
#endif
}
int wolfSSL_CertDebugging_OFF(void)
{
#if defined(WOLFSSL_DEBUG_CERTS) || defined(DEBUG_WOLFSSL)
#if defined(NO_WOLFSSL_DEBUG_CERTS)
return NOT_COMPILED_IN;
#else
loggingCertEnabled = 0;
#endif
return 0;
#else
return NOT_COMPILED_IN;
#endif
}
int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function)
{
#ifdef DEBUG_WOLFSSL
LogFunction = log_function;
return 0;
#else
(void)log_function;
return NOT_COMPILED_IN;
#endif
}
wolfSSL_Logging_cb wolfSSL_GetLoggingCb(void)
{
#ifdef DEBUG_WOLFSSL
return LogFunction;
#else
return NULL;
#endif
}
int wolfSSL_Debugging_ON(void)
{
#ifdef DEBUG_WOLFSSL
loggingEnabled = 1;
#ifndef NO_WOLFSSL_DEBUG_CERTS
loggingCertEnabled = 1;
#endif
#if defined(WOLFSSL_APACHE_MYNEWT)
if (loggingApacheNewtRegistered != WOLFSSL_APACHE_MYNEWT_INITALIZED) {
log_register("wolfcrypt", &mynewt_log, &log_console_handler,
NULL, LOG_SYSLEVEL);
loggingApacheNewtRegistered = WOLFSSL_APACHE_MYNEWT_INITALIZED;
}
#endif
return 0;
#else
return NOT_COMPILED_IN;
#endif
}
void wolfSSL_Debugging_OFF(void)
{
#ifdef DEBUG_WOLFSSL
loggingEnabled = 0;
#ifndef NO_WOLFSSL_DEBUG_CERTS
loggingCertEnabled = 0;
#endif
#endif
}
void wolfSSL_SetLoggingPrefix(const char* prefix)
{
#if defined(DEBUG_WOLFSSL) || \
(defined(WOLFSSL_DEBUG_CERTS) && !defined(NO_WOLFSSL_DEBUG_CERTS))
log_prefix = prefix;
#else
(void)prefix;
#endif
}
#ifdef WOLFSSL_FUNC_TIME
void WOLFSSL_START(int funcNum)
{
if (funcNum < WC_FUNC_COUNT) {
double now = current_time(0) * 1000.0;
#ifdef WOLFSSL_FUNC_TIME_LOG
WOLFSSL_DEBUG_PRINTF("%17.3f: START - %s\n",
now, wc_func_name[funcNum]);
#endif
wc_func_start[funcNum] = now;
}
}
void WOLFSSL_END(int funcNum)
{
if (funcNum < WC_FUNC_COUNT) {
double now = current_time(0) * 1000.0;
wc_func_time[funcNum] += now - wc_func_start[funcNum];
#ifdef WOLFSSL_FUNC_TIME_LOG
WOLFSSL_DEBUG_PRINTF("%17.3f: END - %s\n",
now, wc_func_name[funcNum]);
#endif
}
}
void WOLFSSL_TIME(int count)
{
int i;
double avg, total = 0;
for (i = 0; i < WC_FUNC_COUNT; i++) {
if (wc_func_time[i] > 0) {
avg = wc_func_time[i] / count;
WOLFSSL_DEBUG_PRINTF("%8.3f ms: %s\n", avg, wc_func_name[i]);
total += avg;
}
}
WOLFSSL_DEBUG_PRINTF("%8.3f ms\n", total);
}
#endif
#if defined(DEBUG_WOLFSSL) || \
(defined(WOLFSSL_DEBUG_CERTS) && !defined(NO_WOLFSSL_DEBUG_CERTS))
#ifdef HAVE_STACK_SIZE_VERBOSE
#include <wolfssl/wolfcrypt/mem_track.h>
#endif
static void wolfssl_log(const int logLevel, const char* const file_name,
int line_number, const char* const logMessage)
{
(void)file_name;
(void)line_number;
if (LogFunction)
LogFunction(logLevel, logMessage);
else {
#if defined(WOLFSSL_USER_LOG)
WOLFSSL_USER_LOG(logMessage);
#elif defined(WOLFSSL_DEBUG_PRINTF_FN)
#ifdef WOLFSSL_MDK_ARM
fflush(stdout);
#endif
#ifndef WOLFSSL_DEBUG_LINE_ENDING
#define WOLFSSL_DEBUG_LINE_ENDING "\n"
#endif
if (log_prefix != NULL) {
if (file_name != NULL) {
WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS
"[%s]: [%s L %d] %s" WOLFSSL_DEBUG_LINE_ENDING,
log_prefix, file_name, line_number, logMessage);
}
else {
WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS
"[%s]: %s" WOLFSSL_DEBUG_LINE_ENDING, log_prefix, logMessage);
}
}
else {
if (file_name != NULL) {
WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS
"[%s L %d] %s" WOLFSSL_DEBUG_LINE_ENDING,
file_name, line_number, logMessage);
}
else {
WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS
"%s" WOLFSSL_DEBUG_LINE_ENDING, logMessage);
}
}
#ifdef WOLFSSL_MDK_ARM
fflush(stdout);
#endif
#elif defined(ARDUINO)
wolfSSL_Arduino_Serial_Print(logMessage);
#elif defined(WOLFSSL_UTASKER)
fnDebugMsg((char*)logMessage);
fnDebugMsg("\r\n");
#elif defined(STACK_SIZE_CHECKPOINT_MSG) && \
defined(HAVE_STACK_SIZE_VERBOSE) && defined(HAVE_STACK_SIZE_VERBOSE_LOG)
STACK_SIZE_CHECKPOINT_MSG(logMessage);
#else
#error No log method defined.
#endif
}
}
#ifndef WOLFSSL_DEBUG_ERRORS_ONLY
#if (defined(WOLFSSL_DEBUG_CERTS) || defined(DEBUG_WOLFSSL)) && \
!defined(NO_WOLFSSL_DEBUG_CERTS)
#include <stdarg.h>
#ifndef WOLFSSL_MSG_CERT_BUF_SZ
#define WOLFSSL_MSG_CERT_BUF_SZ 140
#endif
int WOLFSSL_MSG_CERT(const char* msg)
{
(void)loggingEnabled;
if ((msg != NULL) && (loggingCertEnabled != 0)) {
wolfssl_log(CERT_LOG, NULL, 0, msg);
}
return 0;
}
#ifdef XVSNPRINTF
#ifdef __clang__
__attribute__((__format__ (__printf__, 1, 0)))
#endif
int WOLFSSL_MSG_CERT_EX(const char* fmt, ...)
{
#ifdef WOLFSSL_SMALL_STACK
char* msg;
msg = (char*)XMALLOC(WOLFSSL_MSG_CERT_BUF_SZ, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (msg == NULL) {
return MEMORY_E;
}
#else
char msg[WOLFSSL_MSG_CERT_BUF_SZ];
#endif
int written;
va_list args;
va_start(args, fmt);
written = XVSNPRINTF(msg, WOLFSSL_MSG_CERT_BUF_SZ, fmt, args);
va_end(args);
if ((written > 0) && (loggingCertEnabled != 0)) {
wolfssl_log(INFO_LOG, NULL, 0, msg);
}
WC_FREE_VAR_EX(msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return 0;
}
#endif
#else
#ifdef WOLF_NO_VARIADIC_MACROS
#ifdef __WATCOMC__
#else
int WOLFSSL_MSG_CERT(const char* msg)
{
(void)msg;
return NOT_COMPILED_IN;
}
int WOLFSSL_MSG_CERT_EX(const char* fmt, ...)
{
(void)fmt;
return NOT_COMPILED_IN;
}
#endif
#else
#endif
#endif
#if defined(XVSNPRINTF) && !defined(NO_WOLFSSL_MSG_EX)
#if defined(WOLFSSL_BSDKM)
#include <machine/stdarg.h>
#else
#include <stdarg.h>
#endif
#ifndef WOLFSSL_MSG_EX_BUF_SZ
#define WOLFSSL_MSG_EX_BUF_SZ 100
#endif
#if !defined(WOLFSSL_MSG_EX) && defined(DEBUG_WOLFSSL)
#ifdef __clang__
__attribute__((__format__ (__printf__, 1, 0)))
#endif
void WOLFSSL_MSG_EX(const char* fmt, ...)
{
if (loggingEnabled) {
char msg[WOLFSSL_MSG_EX_BUF_SZ];
int written;
va_list args;
va_start(args, fmt);
written = XVSNPRINTF(msg, sizeof(msg), fmt, args);
va_end(args);
if (written > 0)
wolfssl_log(INFO_LOG, NULL, 0, msg);
}
}
#endif
#ifdef WOLFSSL_DEBUG_CODEPOINTS
void WOLFSSL_MSG_EX2(const char *file, int line, const char* fmt, ...)
{
if (loggingEnabled) {
char msg[WOLFSSL_MSG_EX_BUF_SZ];
int written;
va_list args;
va_start(args, fmt);
written = XVSNPRINTF(msg, sizeof(msg), fmt, args);
va_end(args);
if (written > 0)
wolfssl_log(INFO_LOG, file, line, msg);
}
}
#endif
#else
#ifdef WOLF_NO_VARIADIC_MACROS
#else
#endif
#endif
#ifndef WOLFSSL_MSG
void WOLFSSL_MSG(const char* msg)
{
if (loggingEnabled)
wolfssl_log(INFO_LOG, NULL, 0, msg);
}
#endif
#ifdef WOLFSSL_DEBUG_CODEPOINTS
void WOLFSSL_MSG2(const char *file, int line, const char* msg)
{
if (loggingEnabled)
wolfssl_log(INFO_LOG, file, line, msg);
}
#endif
#ifndef WOLFSSL_BUFFER
#ifndef LINE_LEN
#define LINE_LEN 16
#endif
void WOLFSSL_BUFFER(const byte* buffer, word32 length)
{
int i, buflen = (int)length;
char line[(LINE_LEN * 4) + 3];
if (!loggingEnabled) {
return;
}
if (!buffer) {
wolfssl_log(INFO_LOG, NULL, 0, "\tNULL");
return;
}
while (buflen > 0) {
int bufidx = 0;
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "\t")
>= (int)sizeof(line) - bufidx)
{
goto errout;
}
bufidx++;
for (i = 0; i < LINE_LEN; i++) {
if (i < buflen) {
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "%02x ",
buffer[i]) >= (int)sizeof(line) - bufidx)
{
goto errout;
}
}
else {
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, " ")
>= (int)sizeof(line) - bufidx)
{
goto errout;
}
}
bufidx += 3;
}
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "| ")
>= (int)sizeof(line) - bufidx)
{
goto errout;
}
bufidx++;
for (i = 0; i < LINE_LEN; i++) {
if (i < buflen) {
if (XSNPRINTF(&line[bufidx], sizeof(line)-bufidx,
"%c", 31 < buffer[i] && buffer[i] < 127
? buffer[i]
: '.')
>= (int)sizeof(line) - bufidx)
{
goto errout;
}
bufidx++;
}
}
wolfssl_log(INFO_LOG, NULL, 0, line);
buffer += LINE_LEN;
buflen -= LINE_LEN;
}
return;
errout:
wolfssl_log(INFO_LOG, NULL, 0, "\t[Buffer error while rendering]");
}
#endif
#ifndef WOLFSSL_ENTER
void WOLFSSL_ENTER(const char* msg)
{
if (loggingEnabled) {
char buffer[WOLFSSL_MAX_ERROR_SZ];
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg)
>= (int)sizeof(buffer))
{
buffer[sizeof(buffer) - 1] = 0;
}
wolfssl_log(ENTER_LOG, NULL, 0, buffer);
}
}
#endif
#ifdef WOLFSSL_DEBUG_CODEPOINTS
void WOLFSSL_ENTER2(const char *file, int line, const char* msg)
{
if (loggingEnabled) {
char buffer[WOLFSSL_MAX_ERROR_SZ];
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg)
>= (int)sizeof(buffer))
{
buffer[sizeof(buffer) - 1] = 0;
}
wolfssl_log(ENTER_LOG, file, line, buffer);
}
}
#endif
#ifndef WOLFSSL_LEAVE
void WOLFSSL_LEAVE(const char* msg, int ret)
{
if (loggingEnabled) {
char buffer[WOLFSSL_MAX_ERROR_SZ];
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
msg, ret)
>= (int)sizeof(buffer))
{
buffer[sizeof(buffer) - 1] = 0;
}
wolfssl_log(LEAVE_LOG, NULL, 0, buffer);
}
}
#endif
#ifdef WOLFSSL_DEBUG_CODEPOINTS
void WOLFSSL_LEAVE2(const char *file, int line, const char* msg, int ret)
{
if (loggingEnabled) {
char buffer[WOLFSSL_MAX_ERROR_SZ];
if (XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
msg, ret)
>= (int)sizeof(buffer))
{
buffer[sizeof(buffer) - 1] = 0;
}
wolfssl_log(LEAVE_LOG, file, line, buffer);
}
}
#endif
#ifdef WOLFSSL_DEBUG_CODEPOINTS
#define WOLFSSL_MSG(msg) WOLFSSL_MSG2(__FILE__, __LINE__, msg)
#define WOLFSSL_ENTER(msg) WOLFSSL_ENTER2(__FILE__, __LINE__, msg)
#define WOLFSSL_LEAVE(msg, ret) WOLFSSL_LEAVE2(__FILE__, __LINE__, msg, ret)
#ifdef XVSNPRINTF
#endif
#endif
#ifndef WOLFSSL_IS_DEBUG_ON
int WOLFSSL_IS_DEBUG_ON(void)
{
return loggingEnabled;
}
#endif
#endif
#else
#ifdef WOLF_NO_VARIADIC_MACROS
#ifdef __WATCOMC__
#else
int WOLFSSL_MSG_CERT(const char* msg)
{
(void)msg;
return NOT_COMPILED_IN;
}
int WOLFSSL_MSG_CERT_EX(const char* fmt, ...)
{
(void)fmt;
return NOT_COMPILED_IN;
}
#endif
#else
#endif
#endif
#ifdef __WATCOMC__
#else
#ifdef DEBUG_WOLFSSL
#else
#if defined(HAVE_WOLFSSL_MSG_EX) || defined(WOLF_NO_VARIADIC_MACROS)
void WOLFSSL_MSG_EX(const char* fmt, ...)
{
(void)fmt;
}
#else
#endif
#endif
#endif
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) || defined(HAVE_MEMCACHED)
#ifdef WOLFSSL_HAVE_ERROR_QUEUE
#ifdef ERROR_QUEUE_PER_THREAD
#ifndef ERROR_QUEUE_MAX
#define ERROR_QUEUE_MAX 16
#endif
struct wc_error_entry {
char reason[WOLFSSL_MAX_ERROR_SZ];
char file[WOLFSSL_MAX_ERROR_SZ];
int line;
int err;
};
struct wc_error_queue {
struct wc_error_entry entries[ERROR_QUEUE_MAX];
size_t head_idx;
size_t count;
};
static THREAD_LS_T struct wc_error_queue wc_errors;
#define ERRQ_LOCK() 0
#define ERRQ_UNLOCK() (void)0
static int get_abs_idx(int relative_idx)
{
if ((wc_errors.count == 0) || (relative_idx >= (int)wc_errors.count)) {
return -1;
}
if (relative_idx < 0) {
return (int)((wc_errors.head_idx + wc_errors.count - 1)
% ERROR_QUEUE_MAX);
}
return (int)((wc_errors.head_idx + (size_t)relative_idx) % ERROR_QUEUE_MAX);
}
static struct wc_error_entry *get_entry(int relative_idx)
{
int abs_idx;
abs_idx = get_abs_idx(relative_idx);
if (abs_idx < 0) {
return NULL;
}
return &wc_errors.entries[abs_idx];
}
static int pass_entry(struct wc_error_entry *entry,
const char **file, const char **reason,
int *line)
{
if (entry == NULL) {
WOLFSSL_MSG("No Error found in queue");
return BAD_STATE_E;
}
if (file != NULL) {
*file = entry->file;
}
if (reason != NULL) {
*reason = entry->reason;
}
if (line != NULL) {
*line = entry->line;
}
return entry->err;
}
static void set_entry(struct wc_error_entry *entry, int error,
const char *file, const char *reason, int line)
{
size_t sz;
XMEMSET(entry, 0, sizeof(struct wc_error_entry));
entry->err = error;
entry->line = line;
sz = XSTRLEN(reason);
if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sz = WOLFSSL_MAX_ERROR_SZ - 1;
}
if (sz > 0) {
XMEMCPY(entry->reason, reason, sz);
entry->reason[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
}
sz = XSTRLEN(file);
if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sz = WOLFSSL_MAX_ERROR_SZ - 1;
}
if (sz > 0) {
XMEMCPY(entry->file, file, sz);
entry->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
}
}
int wc_LoggingInit(void)
{
return 0;
}
int wc_LoggingCleanup(void)
{
wc_ClearErrorNodes();
return 0;
}
int wc_PeekErrorNode(int idx, const char **file, const char **reason,
int *line)
{
return pass_entry(get_entry(idx), file, reason, line);
}
int wc_PullErrorNode(const char **file, const char **reason, int *line)
{
struct wc_error_entry *entry;
int ret;
entry = get_entry(0);
ret = pass_entry(entry, file, reason, line);
if (entry != NULL) {
wc_RemoveErrorNode(0);
}
return ret;
}
int wc_AddErrorNode(int error, int line, char* reason, char* file)
{
struct wc_error_entry *entry;
size_t idx;
if (wc_errors.count >= ERROR_QUEUE_MAX) {
WOLFSSL_MSG("Error queue is full, at ERROR_QUEUE_MAX");
return MEMORY_E;
}
idx = (wc_errors.head_idx + wc_errors.count) % ERROR_QUEUE_MAX;
entry = &wc_errors.entries[idx];
set_entry(entry, error, file, reason, line);
++wc_errors.count;
return 0;
}
void wc_RemoveErrorNode(int relative_idx)
{
int abs_idx = get_abs_idx(relative_idx);
if (abs_idx >= 0) {
size_t move_count;
if (abs_idx >= (int)wc_errors.head_idx) {
move_count = (size_t)abs_idx - wc_errors.head_idx;
if (move_count > 0) {
XMEMMOVE(&wc_errors.entries[wc_errors.head_idx + 1],
&wc_errors.entries[wc_errors.head_idx],
sizeof(wc_errors.entries[0]) * move_count);
}
wc_errors.head_idx = (wc_errors.head_idx + 1) % ERROR_QUEUE_MAX;
--wc_errors.count;
}
else {
int last_idx = get_abs_idx(-1);
if (last_idx >= abs_idx) {
move_count = (size_t)(last_idx - abs_idx);
if (move_count > 0) {
XMEMMOVE(&wc_errors.entries[abs_idx],
&wc_errors.entries[abs_idx + 1],
sizeof(wc_errors.entries[0]) * move_count);
}
--wc_errors.count;
}
}
}
}
void wc_ClearErrorNodes(void)
{
if (wc_errors.count > 0) {
XMEMSET(&wc_errors, 0, sizeof(wc_errors));
}
}
int wc_SetLoggingHeap(void* h)
{
(void)h;
return 0;
}
int wc_ERR_remove_state(void)
{
wc_ClearErrorNodes();
return 0;
}
unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
const char **data, int *flags,
int (*ignore_err)(int err))
{
WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
if (data != NULL) {
*data = "";
}
if (flags != NULL) {
*flags = 0;
}
while (1) {
int ret = wc_PeekErrorNode(0, file, NULL, line);
if (ret == WC_NO_ERR_TRACE(BAD_STATE_E)) {
WOLFSSL_MSG("Issue peeking at error node in queue");
return 0;
}
if (ret < 0) {
ret = -ret;
}
if (ignore_err && ignore_err(ret)) {
wc_RemoveErrorNode(0);
continue;
}
return (unsigned long)ret;
}
}
int wc_GetErrorNodeErr(void)
{
int ret;
WOLFSSL_ENTER("wc_GetErrorNodeErr");
ret = wc_PullErrorNode(NULL, NULL, NULL);
if (ret < 0) {
if (ret == WC_NO_ERR_TRACE(BAD_STATE_E)) {
ret = 0;
}
else {
WOLFSSL_MSG("Error with pulling error node!");
WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
ret = 0 - ret;
wc_ClearErrorNodes();
}
}
return ret;
}
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
void *u)
{
size_t i;
WOLFSSL_ENTER("wc_ERR_print_errors_cb");
if (cb == NULL) {
return;
}
for (i = 0; i < wc_errors.count; ++i) {
struct wc_error_entry *entry = get_entry((int)i);
if (entry == NULL)
break;
cb(entry->reason, XSTRLEN(entry->reason), u);
}
wc_ClearErrorNodes();
}
#endif
#else
#ifndef ERROR_QUEUE_MAX
#define ERROR_QUEUE_MAX 100
#endif
struct wc_error_queue {
void* heap;
struct wc_error_queue* next;
struct wc_error_queue* prev;
char error[WOLFSSL_MAX_ERROR_SZ];
char file[WOLFSSL_MAX_ERROR_SZ];
int value;
int line;
};
static struct wc_error_queue* wc_errors;
static int wc_errors_count = 0;
static struct wc_error_queue* wc_last_node;
static struct wc_error_queue* wc_current_node;
static void* wc_error_heap;
static wolfSSL_Mutex wc_error_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wc_error_mutex);
#define ERRQ_MUTEX_INIT() wc_InitMutex(&wc_error_mutex)
#define ERRQ_MUTEX_FREE() wc_FreeMutex(&wc_error_mutex)
#define ERRQ_LOCK() wc_LockMutex(&wc_error_mutex)
#define ERRQ_UNLOCK() wc_UnLockMutex(&wc_error_mutex)
int wc_LoggingInit(void)
{
#ifndef WOLFSSL_MUTEX_INITIALIZER
if (ERRQ_MUTEX_INIT() != 0) {
WOLFSSL_MSG("Bad Init Mutex");
return BAD_MUTEX_E;
}
#endif
wc_errors_count = 0;
wc_errors = NULL;
wc_current_node = NULL;
wc_last_node = NULL;
return 0;
}
int wc_LoggingCleanup(void)
{
wc_ClearErrorNodes();
#ifndef WOLFSSL_MUTEX_INITIALIZER
if (ERRQ_MUTEX_FREE() != 0) {
WOLFSSL_MSG("Bad Mutex free");
return BAD_MUTEX_E;
}
#endif
return 0;
}
static int peekErrorNode(int idx, const char **file, const char **reason,
int *line)
{
struct wc_error_queue* err;
if (idx < 0) {
err = wc_last_node;
}
else {
int i;
err = (struct wc_error_queue*)wc_errors;
for (i = 0; i < idx; i++) {
if (err == NULL) {
WOLFSSL_MSG("Error node not found. Bad index?");
return BAD_FUNC_ARG;
}
err = err->next;
}
}
if (err == NULL) {
WOLFSSL_MSG("No Errors in queue");
return BAD_STATE_E;
}
if (file != NULL) {
*file = err->file;
}
if (reason != NULL) {
*reason = err->error;
}
if (line != NULL) {
*line = err->line;
}
return err->value;
}
int wc_PeekErrorNode(int idx, const char **file, const char **reason,
int *line)
{
int ret;
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return BAD_MUTEX_E;
}
ret = peekErrorNode(idx, file, reason, line);
ERRQ_UNLOCK();
return ret;
}
static int pullErrorNode(const char **file, const char **reason, int *line)
{
struct wc_error_queue* err;
int value;
err = wc_current_node;
if (err == NULL) {
WOLFSSL_MSG("No Errors in queue");
return BAD_STATE_E;
}
if (file != NULL) {
*file = err->file;
}
if (reason != NULL) {
*reason = err->error;
}
if (line != NULL) {
*line = err->line;
}
value = err->value;
wc_current_node = err->next;
return value;
}
int wc_PullErrorNode(const char **file, const char **reason, int *line)
{
int value;
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return BAD_MUTEX_E;
}
value = pullErrorNode(file, reason, line);
ERRQ_UNLOCK();
return value;
}
int wc_AddErrorNode(int error, int line, char* buf, char* file)
{
struct wc_error_queue* err;
if (wc_errors_count >= ERROR_QUEUE_MAX) {
WOLFSSL_MSG("Error queue is full, at ERROR_QUEUE_MAX");
return MEMORY_E;
}
err = (struct wc_error_queue*)XMALLOC(
sizeof(struct wc_error_queue), wc_error_heap, DYNAMIC_TYPE_LOG);
if (err == NULL) {
WOLFSSL_MSG("Unable to create error node for log");
return MEMORY_E;
}
else {
int sz;
XMEMSET(err, 0, sizeof(struct wc_error_queue));
err->heap = wc_error_heap;
sz = (int)XSTRLEN(buf);
if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sz = WOLFSSL_MAX_ERROR_SZ - 1;
}
if (sz > 0) {
XMEMCPY(err->error, buf, (size_t)sz);
}
sz = (int)XSTRLEN(file);
if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sz = WOLFSSL_MAX_ERROR_SZ - 1;
}
if (sz > 0) {
XMEMCPY(err->file, file, (size_t)sz);
}
err->value = error;
err->line = line;
err->error[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
err->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
if (wc_last_node == NULL) {
if (wc_errors != NULL) {
WOLFSSL_MSG("ERROR in adding new node to logging queue!!");
XFREE(err, wc_error_heap, DYNAMIC_TYPE_LOG);
}
else {
wc_errors = err;
wc_last_node = err;
wc_current_node = err;
}
}
else {
wc_last_node->next = err;
err->prev = wc_last_node;
wc_last_node = err;
if (wc_current_node == NULL) {
wc_current_node = err;
}
}
wc_errors_count++;
}
return 0;
}
static int getErrorNodeCurrentIdx(void)
{
int ret = 0;
struct wc_error_queue* current;
current = (struct wc_error_queue*)wc_errors;
while (current != wc_current_node && current != NULL) {
current = current->next;
ret++;
}
if (current == NULL) {
ret = 0;
}
return ret;
}
static void removeErrorNode(int idx)
{
struct wc_error_queue* current;
if (idx == -1) {
current = wc_last_node;
}
else {
current = (struct wc_error_queue*)wc_errors;
for (; current != NULL && idx > 0; idx--)
current = current->next;
}
if (current != NULL) {
if (current->prev != NULL)
current->prev->next = current->next;
if (current->next != NULL)
current->next->prev = current->prev;
if (wc_last_node == current)
wc_last_node = current->prev;
if (wc_errors == current)
wc_errors = current->next;
if (wc_current_node == current)
wc_current_node = current->next;
XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
wc_errors_count--;
if (wc_errors_count == 0) {
wc_errors = NULL;
wc_last_node = NULL;
wc_current_node = NULL;
}
}
}
void wc_RemoveErrorNode(int idx)
{
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return;
}
removeErrorNode(idx);
ERRQ_UNLOCK();
}
static void clearErrorNodes(void)
{
struct wc_error_queue* current;
struct wc_error_queue* next;
current = (struct wc_error_queue*)wc_errors;
while (current != NULL) {
next = current->next;
XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
current = next;
}
wc_errors_count = 0;
wc_errors = NULL;
wc_last_node = NULL;
wc_current_node = NULL;
}
void wc_ClearErrorNodes(void)
{
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return;
}
clearErrorNodes();
ERRQ_UNLOCK();
}
int wc_SetLoggingHeap(void* h)
{
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return BAD_MUTEX_E;
}
wc_error_heap = h;
ERRQ_UNLOCK();
return 0;
}
int wc_ERR_remove_state(void)
{
struct wc_error_queue* current;
struct wc_error_queue* next;
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return BAD_MUTEX_E;
}
current = (struct wc_error_queue*)wc_errors;
while (current != NULL) {
next = current->next;
XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
current = next;
}
wc_errors_count = 0;
wc_errors = NULL;
wc_last_node = NULL;
ERRQ_UNLOCK();
return 0;
}
unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
const char **data, int *flags,
int (*ignore_err)(int err))
{
int idx;
WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
if (data != NULL) {
*data = "";
}
if (flags != NULL) {
*flags = 0;
}
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return (unsigned long)(0 - BAD_MUTEX_E);
}
idx = getErrorNodeCurrentIdx();
while (1) {
int ret = peekErrorNode(idx, file, NULL, line);
if (ret == WC_NO_ERR_TRACE(BAD_MUTEX_E) ||
ret == WC_NO_ERR_TRACE(BAD_FUNC_ARG) ||
ret == WC_NO_ERR_TRACE(BAD_STATE_E)) {
ERRQ_UNLOCK();
WOLFSSL_MSG("Issue peeking at error node in queue");
return 0;
}
if (ret < 0) {
ret = -ret;
}
if (ignore_err && ignore_err(ret)) {
removeErrorNode(idx);
continue;
}
ERRQ_UNLOCK();
return (unsigned long)ret;
}
}
int wc_GetErrorNodeErr(void)
{
int ret;
WOLFSSL_ENTER("wc_GetErrorNodeErr");
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
return (0 - BAD_MUTEX_E);
}
ret = pullErrorNode(NULL, NULL, NULL);
if (ret < 0) {
if (ret == WC_NO_ERR_TRACE(BAD_STATE_E)) {
ret = 0;
}
else {
WOLFSSL_MSG("Error with pulling error node!");
WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
ret = 0 - ret;
clearErrorNodes();
}
}
else {
int idx = getErrorNodeCurrentIdx();
if (idx < 0) {
WOLFSSL_MSG("Error with getting current index!");
ret = BAD_STATE_E;
WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
clearErrorNodes();
}
else if (idx > 0) {
idx -= 1;
removeErrorNode(idx);
}
else {
removeErrorNode(idx);
}
}
ERRQ_UNLOCK();
return ret;
}
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
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 (cb == NULL) {
return;
}
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
}
else {
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, XSTRLEN(current->error), u);
XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
current = next;
}
wc_errors_count = 0;
wc_errors = NULL;
wc_last_node = NULL;
ERRQ_UNLOCK();
}
}
#endif
#endif
#else
int wc_LoggingInit(void)
{
return 0;
}
int wc_LoggingCleanup(void)
{
return 0;
}
int wc_PeekErrorNode(int idx, const char **file, const char **reason,
int *line)
{
(void)idx;
(void)file;
(void)reason;
(void)line;
WOLFSSL_MSG("Error queue turned off, can not peak nodes");
return NOT_COMPILED_IN;
}
int wc_PullErrorNode(const char **file, const char **reason, int *line)
{
(void)file;
(void)reason;
(void)line;
WOLFSSL_MSG("Error queue turned off, can not pull nodes");
return NOT_COMPILED_IN;
}
int wc_AddErrorNode(int error, int line, char* buf, char* file)
{
(void)error;
(void)line;
(void)buf;
(void)file;
WOLFSSL_MSG("Error queue turned off, can not add nodes");
return NOT_COMPILED_IN;
}
void wc_RemoveErrorNode(int idx)
{
(void)idx;
WOLFSSL_MSG("Error queue turned off, can not remove nodes");
}
void wc_ClearErrorNodes(void)
{
WOLFSSL_MSG("Error queue turned off, can not clear nodes");
}
int wc_SetLoggingHeap(void* h)
{
(void)h;
return 0;
}
int wc_ERR_remove_state(void)
{
return 0;
}
unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
const char **data, int *flags,
int (*ignore_err)(int err))
{
WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
(void)line;
(void)file;
(void)ignore_err;
if (data != NULL) {
*data = "";
}
if (flags != NULL) {
*flags = 0;
}
return (unsigned long)(0 - NOT_COMPILED_IN);
}
int wc_GetErrorNodeErr(void)
{
WOLFSSL_ENTER("wc_GetErrorNodeErr");
return (0 - NOT_COMPILED_IN);
}
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
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");
(void)cb;
(void)u;
}
#endif
#endif
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
static int wc_ERR_dump_to_file (const char *str, size_t len, void *u)
{
XFILE fp = (XFILE ) u;
if (fprintf(fp, "%-*.*s\n", (int)len, (int)len, str) < 0)
return IO_FAILED_E;
return 0;
}
void wc_ERR_print_errors_fp(XFILE fp)
{
WOLFSSL_ENTER("wc_ERR_print_errors_fp");
wc_ERR_print_errors_cb(wc_ERR_dump_to_file, fp);
}
#endif
#endif
#if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || \
defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
defined(OPENSSL_EXTRA)
#ifdef WOLFSSL_HAVE_ERROR_QUEUE
void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line,
const char* file, void* usrCtx)
#else
void WOLFSSL_ERROR(int error)
#endif
{
#ifdef WOLFSSL_ASYNC_CRYPT
if (error != WC_NO_ERR_TRACE(WC_PENDING_E))
#endif
{
char buffer[WOLFSSL_MAX_ERROR_SZ];
#ifdef WOLFSSL_HAVE_ERROR_QUEUE
(void)usrCtx;
(void)func;
if (ERRQ_LOCK() != 0) {
WOLFSSL_MSG("Lock debug mutex failed");
(void)XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d", error);
}
else {
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
if (error != WC_NO_ERR_TRACE(WANT_READ) &&
error != WC_NO_ERR_TRACE(WANT_WRITE)) {
#endif
if (error < 0)
error = error - (2 * error);
(void)XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d line:%u file:%s",
error, line, file);
if (wc_AddErrorNode(error, (int)line, buffer, (char*)file) != 0) {
WOLFSSL_MSG("Error creating logging node");
}
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
}
else {
(void)XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d", error);
}
#endif
ERRQ_UNLOCK();
}
#else
(void)XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d", error);
#endif
#ifdef DEBUG_WOLFSSL
if (loggingEnabled)
wolfssl_log(ERROR_LOG, NULL, 0, buffer);
#endif
}
}
void WOLFSSL_ERROR_MSG(const char* msg)
{
#ifdef DEBUG_WOLFSSL
if (loggingEnabled)
wolfssl_log(ERROR_LOG, NULL, 0, msg);
#else
(void)msg;
#endif
}
#endif
#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES
#ifndef WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE
#define WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE 1
#endif
#ifdef WOLFSSL_ATOMIC_OPS
static wolfSSL_Atomic_Int wc_debug_trace_error_codes_state =
WOLFSSL_ATOMIC_INITIALIZER(WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE);
#else
static int wc_debug_trace_error_codes_state =
WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE;
#endif
int wc_debug_trace_error_codes_enabled(void) {
return WOLFSSL_ATOMIC_LOAD(wc_debug_trace_error_codes_state);
}
int wc_debug_trace_error_codes_set(int state) {
return wolfSSL_Atomic_Int_Exchange(&wc_debug_trace_error_codes_state,
state);
}
#endif
#ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
#ifdef WOLFSSL_LINUXKM
int wc_backtrace_render(void) {
dump_stack();
return 0;
}
#else
#include <backtrace-supported.h>
#if BACKTRACE_SUPPORTED != 1
#error WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES is defined but BACKTRACE_SUPPORTED is 0.
#endif
#if !defined(WOLFSSL_MUTEX_INITIALIZER) && defined(WOLFSSL_NO_ATOMICS)
#error WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES requires WOLFSSL_MUTEX_INITIALIZER or wolfSSL_Atomic_Ints.
#endif
#include <backtrace.h>
static int backtrace_callback(void *data, uintptr_t pc, const char *filename,
int lineno, const char *function)
{
if (function == NULL)
return 0;
if (*(int *)data == 0) {
*(int *)data = 1;
return 0;
}
WOLFSSL_DEBUG_PRINTF(" #%d %p in %s %s:%d\n", (*(int *)data)++,
(void *)pc, function, filename, lineno);
return 0;
}
static void backtrace_error(void *data, const char *msg, int errnum) {
(void)data;
WOLFSSL_DEBUG_PRINTF("ERR TRACE: error %d while backtracing: %s",
errnum, msg);
}
static void backtrace_creation_error(void *data, const char *msg, int errnum) {
(void)data;
WOLFSSL_DEBUG_PRINTF("ERR TRACE: internal error %d "
"while initializing backtrace facility: %s", errnum, msg);
}
static int backtrace_init(struct backtrace_state **backtrace_state) {
#ifdef WOLFSSL_MUTEX_INITIALIZER
static wolfSSL_Mutex backtrace_create_state_mutex =
WOLFSSL_MUTEX_INITIALIZER(backtrace_create_state_mutex);
if (wc_LockMutex(&backtrace_create_state_mutex) != 0)
return -1;
#elif defined(WOLFSSL_ATOMIC_OPS)
static wolfSSL_Atomic_Int init_count = 0;
if (wolfSSL_Atomic_Int_FetchAdd(&init_count, 1) != 1)
return -1;
#endif
if (*backtrace_state == NULL) {
*backtrace_state = backtrace_create_state(
NULL, 0, backtrace_creation_error, NULL);
}
#ifdef WOLFSSL_MUTEX_INITIALIZER
wc_UnLockMutex(&backtrace_create_state_mutex);
#endif
if (*backtrace_state == NULL)
return -1;
return 0;
}
int wc_backtrace_render(void) {
static wolfSSL_Mutex backtrace_mutex
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(backtrace_mutex);
static struct backtrace_state *backtrace_state = NULL;
int depth = 0;
int ret;
#ifndef WOLFSSL_MUTEX_INITIALIZER
static wolfSSL_Atomic_Int init_count = 0;
if (init_count != 1) {
int cur_init_count = wolfSSL_Atomic_Int_FetchSub(&init_count, 1);
if (cur_init_count != 0) {
(void)wolfSSL_Atomic_Int_FetchAdd(&init_count, 1);
return WC_NO_ERR_TRACE(DEADLOCK_AVERTED_E);
}
ret = wc_InitMutex(&backtrace_mutex);
if (ret != 0)
return ret;
(void)wolfSSL_Atomic_Int_FetchSub(&init_count, cur_init_count - 2);
}
#endif
ret = wc_LockMutex(&backtrace_mutex);
if (ret != 0)
return ret;
if (backtrace_state == NULL) {
if (backtrace_init(&backtrace_state) < 0) {
wc_UnLockMutex(&backtrace_mutex);
return WC_NO_ERR_TRACE(BAD_STATE_E);
}
}
backtrace_full(backtrace_state, 0, backtrace_callback, backtrace_error,
(void *)&depth);
wc_UnLockMutex(&backtrace_mutex);
return 0;
}
#endif
#endif