Xuyi Wang / wolfcrypt

Dependents:   OS

Committer:
sPymbed
Date:
Wed Nov 20 13:28:01 2019 +0000
Revision:
0:1387ff3eed4a
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sPymbed 0:1387ff3eed4a 1 /* logging.c
sPymbed 0:1387ff3eed4a 2 *
sPymbed 0:1387ff3eed4a 3 * Copyright (C) 2006-2017 wolfSSL Inc.
sPymbed 0:1387ff3eed4a 4 *
sPymbed 0:1387ff3eed4a 5 * This file is part of wolfSSL.
sPymbed 0:1387ff3eed4a 6 *
sPymbed 0:1387ff3eed4a 7 * wolfSSL is free software; you can redistribute it and/or modify
sPymbed 0:1387ff3eed4a 8 * it under the terms of the GNU General Public License as published by
sPymbed 0:1387ff3eed4a 9 * the Free Software Foundation; either version 2 of the License, or
sPymbed 0:1387ff3eed4a 10 * (at your option) any later version.
sPymbed 0:1387ff3eed4a 11 *
sPymbed 0:1387ff3eed4a 12 * wolfSSL is distributed in the hope that it will be useful,
sPymbed 0:1387ff3eed4a 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sPymbed 0:1387ff3eed4a 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
sPymbed 0:1387ff3eed4a 15 * GNU General Public License for more details.
sPymbed 0:1387ff3eed4a 16 *
sPymbed 0:1387ff3eed4a 17 * You should have received a copy of the GNU General Public License
sPymbed 0:1387ff3eed4a 18 * along with this program; if not, write to the Free Software
sPymbed 0:1387ff3eed4a 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
sPymbed 0:1387ff3eed4a 20 */
sPymbed 0:1387ff3eed4a 21
sPymbed 0:1387ff3eed4a 22
sPymbed 0:1387ff3eed4a 23 #ifdef HAVE_CONFIG_H
sPymbed 0:1387ff3eed4a 24 #include <config.h>
sPymbed 0:1387ff3eed4a 25 #endif
sPymbed 0:1387ff3eed4a 26
sPymbed 0:1387ff3eed4a 27 #include <wolfcrypt/settings.h>
sPymbed 0:1387ff3eed4a 28
sPymbed 0:1387ff3eed4a 29 /* submitted by eof */
sPymbed 0:1387ff3eed4a 30
sPymbed 0:1387ff3eed4a 31 #include <wolfcrypt/logging.h>
sPymbed 0:1387ff3eed4a 32 #include <wolfcrypt/error-crypt.h>
sPymbed 0:1387ff3eed4a 33 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
sPymbed 0:1387ff3eed4a 34 /* avoid adding WANT_READ and WANT_WRITE to error queue */
sPymbed 0:1387ff3eed4a 35 #include <wolfssl/error-ssl.h>
sPymbed 0:1387ff3eed4a 36 #endif
sPymbed 0:1387ff3eed4a 37
sPymbed 0:1387ff3eed4a 38 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
sPymbed 0:1387ff3eed4a 39 static wolfSSL_Mutex debug_mutex; /* mutex for access to debug structure */
sPymbed 0:1387ff3eed4a 40
sPymbed 0:1387ff3eed4a 41 /* accessing any node from the queue should be wrapped in a lock of
sPymbed 0:1387ff3eed4a 42 * debug_mutex */
sPymbed 0:1387ff3eed4a 43 static void* wc_error_heap;
sPymbed 0:1387ff3eed4a 44 struct wc_error_queue {
sPymbed 0:1387ff3eed4a 45 void* heap; /* the heap hint used with nodes creation */
sPymbed 0:1387ff3eed4a 46 struct wc_error_queue* next;
sPymbed 0:1387ff3eed4a 47 struct wc_error_queue* prev;
sPymbed 0:1387ff3eed4a 48 char error[WOLFSSL_MAX_ERROR_SZ];
sPymbed 0:1387ff3eed4a 49 char file[WOLFSSL_MAX_ERROR_SZ];
sPymbed 0:1387ff3eed4a 50 int value;
sPymbed 0:1387ff3eed4a 51 int line;
sPymbed 0:1387ff3eed4a 52 };
sPymbed 0:1387ff3eed4a 53 volatile struct wc_error_queue* wc_errors;
sPymbed 0:1387ff3eed4a 54 static struct wc_error_queue* wc_current_node;
sPymbed 0:1387ff3eed4a 55 static struct wc_error_queue* wc_last_node;
sPymbed 0:1387ff3eed4a 56 /* pointer to last node in queue to make insertion O(1) */
sPymbed 0:1387ff3eed4a 57 #endif
sPymbed 0:1387ff3eed4a 58
sPymbed 0:1387ff3eed4a 59 #ifdef WOLFSSL_FUNC_TIME
sPymbed 0:1387ff3eed4a 60 /* WARNING: This code is only to be used for debugging performance.
sPymbed 0:1387ff3eed4a 61 * The code is not thread-safe.
sPymbed 0:1387ff3eed4a 62 * Do not use WOLFSSL_FUNC_TIME in production code.
sPymbed 0:1387ff3eed4a 63 */
sPymbed 0:1387ff3eed4a 64 static double wc_func_start[WC_FUNC_COUNT];
sPymbed 0:1387ff3eed4a 65 static double wc_func_time[WC_FUNC_COUNT] = { 0, };
sPymbed 0:1387ff3eed4a 66 static const char* wc_func_name[WC_FUNC_COUNT] = {
sPymbed 0:1387ff3eed4a 67 "SendClientHello",
sPymbed 0:1387ff3eed4a 68 "DoClientHello",
sPymbed 0:1387ff3eed4a 69 "SendServerHello",
sPymbed 0:1387ff3eed4a 70 "DoServerHello",
sPymbed 0:1387ff3eed4a 71 "SendEncryptedExtensions",
sPymbed 0:1387ff3eed4a 72 "DoEncryptedExtensions",
sPymbed 0:1387ff3eed4a 73 "SendCertificateRequest",
sPymbed 0:1387ff3eed4a 74 "DoCertificateRequest",
sPymbed 0:1387ff3eed4a 75 "SendCertificate",
sPymbed 0:1387ff3eed4a 76 "DoCertificate",
sPymbed 0:1387ff3eed4a 77 "SendCertificateVerify",
sPymbed 0:1387ff3eed4a 78 "DoCertificateVerify",
sPymbed 0:1387ff3eed4a 79 "SendFinished",
sPymbed 0:1387ff3eed4a 80 "DoFinished",
sPymbed 0:1387ff3eed4a 81 "SendKeyUpdate",
sPymbed 0:1387ff3eed4a 82 "DoKeyUpdate",
sPymbed 0:1387ff3eed4a 83 "SendEarlyData",
sPymbed 0:1387ff3eed4a 84 "DoEarlyData",
sPymbed 0:1387ff3eed4a 85 "SendNewSessionTicket",
sPymbed 0:1387ff3eed4a 86 "DoNewSessionTicket",
sPymbed 0:1387ff3eed4a 87 "SendServerHelloDone",
sPymbed 0:1387ff3eed4a 88 "DoServerHelloDone",
sPymbed 0:1387ff3eed4a 89 "SendTicket",
sPymbed 0:1387ff3eed4a 90 "DoTicket",
sPymbed 0:1387ff3eed4a 91 "SendClientKeyExchange",
sPymbed 0:1387ff3eed4a 92 "DoClientKeyExchange",
sPymbed 0:1387ff3eed4a 93 "SendCertificateStatus",
sPymbed 0:1387ff3eed4a 94 "DoCertificateStatus",
sPymbed 0:1387ff3eed4a 95 "SendServerKeyExchange",
sPymbed 0:1387ff3eed4a 96 "DoServerKeyExchange",
sPymbed 0:1387ff3eed4a 97 "SendEarlyData",
sPymbed 0:1387ff3eed4a 98 "DoEarlyData",
sPymbed 0:1387ff3eed4a 99 };
sPymbed 0:1387ff3eed4a 100
sPymbed 0:1387ff3eed4a 101 #include <sys/time.h>
sPymbed 0:1387ff3eed4a 102
sPymbed 0:1387ff3eed4a 103 /* WARNING: This function is not portable. */
sPymbed 0:1387ff3eed4a 104 static WC_INLINE double current_time(int reset)
sPymbed 0:1387ff3eed4a 105 {
sPymbed 0:1387ff3eed4a 106 struct timeval tv;
sPymbed 0:1387ff3eed4a 107 gettimeofday(&tv, 0);
sPymbed 0:1387ff3eed4a 108 (void)reset;
sPymbed 0:1387ff3eed4a 109
sPymbed 0:1387ff3eed4a 110 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
sPymbed 0:1387ff3eed4a 111 }
sPymbed 0:1387ff3eed4a 112 #endif /* WOLFSSL_FUNC_TIME */
sPymbed 0:1387ff3eed4a 113
sPymbed 0:1387ff3eed4a 114 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 115
sPymbed 0:1387ff3eed4a 116 /* Set these to default values initially. */
sPymbed 0:1387ff3eed4a 117 static wolfSSL_Logging_cb log_function = NULL;
sPymbed 0:1387ff3eed4a 118 static int loggingEnabled = 0;
sPymbed 0:1387ff3eed4a 119
sPymbed 0:1387ff3eed4a 120 #endif /* DEBUG_WOLFSSL */
sPymbed 0:1387ff3eed4a 121
sPymbed 0:1387ff3eed4a 122
sPymbed 0:1387ff3eed4a 123 /* allow this to be set to NULL, so logs can be redirected to default output */
sPymbed 0:1387ff3eed4a 124 int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
sPymbed 0:1387ff3eed4a 125 {
sPymbed 0:1387ff3eed4a 126 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 127 log_function = f;
sPymbed 0:1387ff3eed4a 128 return 0;
sPymbed 0:1387ff3eed4a 129 #else
sPymbed 0:1387ff3eed4a 130 (void)f;
sPymbed 0:1387ff3eed4a 131 return NOT_COMPILED_IN;
sPymbed 0:1387ff3eed4a 132 #endif
sPymbed 0:1387ff3eed4a 133 }
sPymbed 0:1387ff3eed4a 134
sPymbed 0:1387ff3eed4a 135
sPymbed 0:1387ff3eed4a 136 int wolfSSL_Debugging_ON(void)
sPymbed 0:1387ff3eed4a 137 {
sPymbed 0:1387ff3eed4a 138 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 139 loggingEnabled = 1;
sPymbed 0:1387ff3eed4a 140 return 0;
sPymbed 0:1387ff3eed4a 141 #else
sPymbed 0:1387ff3eed4a 142 return NOT_COMPILED_IN;
sPymbed 0:1387ff3eed4a 143 #endif
sPymbed 0:1387ff3eed4a 144 }
sPymbed 0:1387ff3eed4a 145
sPymbed 0:1387ff3eed4a 146
sPymbed 0:1387ff3eed4a 147 void wolfSSL_Debugging_OFF(void)
sPymbed 0:1387ff3eed4a 148 {
sPymbed 0:1387ff3eed4a 149 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 150 loggingEnabled = 0;
sPymbed 0:1387ff3eed4a 151 #endif
sPymbed 0:1387ff3eed4a 152 }
sPymbed 0:1387ff3eed4a 153
sPymbed 0:1387ff3eed4a 154 #ifdef WOLFSSL_FUNC_TIME
sPymbed 0:1387ff3eed4a 155 /* WARNING: This code is only to be used for debugging performance.
sPymbed 0:1387ff3eed4a 156 * The code is not thread-safe.
sPymbed 0:1387ff3eed4a 157 * Do not use WOLFSSL_FUNC_TIME in production code.
sPymbed 0:1387ff3eed4a 158 */
sPymbed 0:1387ff3eed4a 159 void WOLFSSL_START(int funcNum)
sPymbed 0:1387ff3eed4a 160 {
sPymbed 0:1387ff3eed4a 161 double now = current_time(0) * 1000.0;
sPymbed 0:1387ff3eed4a 162 #ifdef WOLFSSL_FUNC_TIME_LOG
sPymbed 0:1387ff3eed4a 163 fprintf(stderr, "%17.3f: START - %s\n", now, wc_func_name[funcNum]);
sPymbed 0:1387ff3eed4a 164 #endif
sPymbed 0:1387ff3eed4a 165 wc_func_start[funcNum] = now;
sPymbed 0:1387ff3eed4a 166 }
sPymbed 0:1387ff3eed4a 167
sPymbed 0:1387ff3eed4a 168 void WOLFSSL_END(int funcNum)
sPymbed 0:1387ff3eed4a 169 {
sPymbed 0:1387ff3eed4a 170 double now = current_time(0) * 1000.0;
sPymbed 0:1387ff3eed4a 171 wc_func_time[funcNum] += now - wc_func_start[funcNum];
sPymbed 0:1387ff3eed4a 172 #ifdef WOLFSSL_FUNC_TIME_LOG
sPymbed 0:1387ff3eed4a 173 fprintf(stderr, "%17.3f: END - %s\n", now, wc_func_name[funcNum]);
sPymbed 0:1387ff3eed4a 174 #endif
sPymbed 0:1387ff3eed4a 175 }
sPymbed 0:1387ff3eed4a 176
sPymbed 0:1387ff3eed4a 177 void WOLFSSL_TIME(int count)
sPymbed 0:1387ff3eed4a 178 {
sPymbed 0:1387ff3eed4a 179 int i;
sPymbed 0:1387ff3eed4a 180 double avg, total = 0;
sPymbed 0:1387ff3eed4a 181
sPymbed 0:1387ff3eed4a 182 for (i = 0; i < WC_FUNC_COUNT; i++) {
sPymbed 0:1387ff3eed4a 183 if (wc_func_time[i] > 0) {
sPymbed 0:1387ff3eed4a 184 avg = wc_func_time[i] / count;
sPymbed 0:1387ff3eed4a 185 fprintf(stderr, "%8.3f ms: %s\n", avg, wc_func_name[i]);
sPymbed 0:1387ff3eed4a 186 total += avg;
sPymbed 0:1387ff3eed4a 187 }
sPymbed 0:1387ff3eed4a 188 }
sPymbed 0:1387ff3eed4a 189 fprintf(stderr, "%8.3f ms\n", total);
sPymbed 0:1387ff3eed4a 190 }
sPymbed 0:1387ff3eed4a 191 #endif
sPymbed 0:1387ff3eed4a 192
sPymbed 0:1387ff3eed4a 193 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 194
sPymbed 0:1387ff3eed4a 195 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
sPymbed 0:1387ff3eed4a 196 #if MQX_USE_IO_OLD
sPymbed 0:1387ff3eed4a 197 #include <fio.h>
sPymbed 0:1387ff3eed4a 198 #else
sPymbed 0:1387ff3eed4a 199 #include <nio.h>
sPymbed 0:1387ff3eed4a 200 #endif
sPymbed 0:1387ff3eed4a 201 #elif defined(WOLFSSL_SGX)
sPymbed 0:1387ff3eed4a 202 /* Declare sprintf for ocall */
sPymbed 0:1387ff3eed4a 203 int sprintf(char* buf, const char *fmt, ...);
sPymbed 0:1387ff3eed4a 204 #elif defined(MICRIUM)
sPymbed 0:1387ff3eed4a 205 #include <bsp_ser.h>
sPymbed 0:1387ff3eed4a 206 #elif defined(WOLFSSL_USER_LOG)
sPymbed 0:1387ff3eed4a 207 /* user includes their own headers */
sPymbed 0:1387ff3eed4a 208 #else
sPymbed 0:1387ff3eed4a 209 #include <stdio.h> /* for default printf stuff */
sPymbed 0:1387ff3eed4a 210 #endif
sPymbed 0:1387ff3eed4a 211
sPymbed 0:1387ff3eed4a 212 #if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
sPymbed 0:1387ff3eed4a 213 int dc_log_printf(char*, ...);
sPymbed 0:1387ff3eed4a 214 #endif
sPymbed 0:1387ff3eed4a 215
sPymbed 0:1387ff3eed4a 216 static void wolfssl_log(const int logLevel, const char *const logMessage)
sPymbed 0:1387ff3eed4a 217 {
sPymbed 0:1387ff3eed4a 218 if (log_function)
sPymbed 0:1387ff3eed4a 219 log_function(logLevel, logMessage);
sPymbed 0:1387ff3eed4a 220 else {
sPymbed 0:1387ff3eed4a 221 #if defined(WOLFSSL_USER_LOG)
sPymbed 0:1387ff3eed4a 222 WOLFSSL_USER_LOG(logMessage);
sPymbed 0:1387ff3eed4a 223 #elif defined(WOLFSSL_LOG_PRINTF)
sPymbed 0:1387ff3eed4a 224 printf("%s\n", logMessage);
sPymbed 0:1387ff3eed4a 225
sPymbed 0:1387ff3eed4a 226 #elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
sPymbed 0:1387ff3eed4a 227 dc_log_printf("%s\n", logMessage);
sPymbed 0:1387ff3eed4a 228 #elif defined(MICRIUM)
sPymbed 0:1387ff3eed4a 229 BSP_Ser_Printf("%s\r\n", logMessage);
sPymbed 0:1387ff3eed4a 230 #elif defined(WOLFSSL_MDK_ARM)
sPymbed 0:1387ff3eed4a 231 fflush(stdout) ;
sPymbed 0:1387ff3eed4a 232 printf("%s\n", logMessage);
sPymbed 0:1387ff3eed4a 233 fflush(stdout) ;
sPymbed 0:1387ff3eed4a 234 #elif defined(WOLFSSL_UTASKER)
sPymbed 0:1387ff3eed4a 235 fnDebugMsg((char*)logMessage);
sPymbed 0:1387ff3eed4a 236 fnDebugMsg("\r\n");
sPymbed 0:1387ff3eed4a 237 #elif defined(MQX_USE_IO_OLD)
sPymbed 0:1387ff3eed4a 238 fprintf(_mqxio_stderr, "%s\n", logMessage);
sPymbed 0:1387ff3eed4a 239
sPymbed 0:1387ff3eed4a 240 #else
sPymbed 0:1387ff3eed4a 241 fprintf(stderr, "%s\n", logMessage);
sPymbed 0:1387ff3eed4a 242 #endif
sPymbed 0:1387ff3eed4a 243 }
sPymbed 0:1387ff3eed4a 244 }
sPymbed 0:1387ff3eed4a 245
sPymbed 0:1387ff3eed4a 246 #ifndef WOLFSSL_DEBUG_ERRORS_ONLY
sPymbed 0:1387ff3eed4a 247 void WOLFSSL_MSG(const char* msg)
sPymbed 0:1387ff3eed4a 248 {
sPymbed 0:1387ff3eed4a 249 if (loggingEnabled)
sPymbed 0:1387ff3eed4a 250 wolfssl_log(INFO_LOG , msg);
sPymbed 0:1387ff3eed4a 251 }
sPymbed 0:1387ff3eed4a 252
sPymbed 0:1387ff3eed4a 253
sPymbed 0:1387ff3eed4a 254 void WOLFSSL_BUFFER(const byte* buffer, word32 length)
sPymbed 0:1387ff3eed4a 255 {
sPymbed 0:1387ff3eed4a 256 #define LINE_LEN 16
sPymbed 0:1387ff3eed4a 257
sPymbed 0:1387ff3eed4a 258 if (loggingEnabled) {
sPymbed 0:1387ff3eed4a 259 word32 i;
sPymbed 0:1387ff3eed4a 260 char line[80];
sPymbed 0:1387ff3eed4a 261
sPymbed 0:1387ff3eed4a 262 if (!buffer) {
sPymbed 0:1387ff3eed4a 263 wolfssl_log(INFO_LOG, "\tNULL");
sPymbed 0:1387ff3eed4a 264
sPymbed 0:1387ff3eed4a 265 return;
sPymbed 0:1387ff3eed4a 266 }
sPymbed 0:1387ff3eed4a 267
sPymbed 0:1387ff3eed4a 268 sprintf(line, "\t");
sPymbed 0:1387ff3eed4a 269
sPymbed 0:1387ff3eed4a 270 for (i = 0; i < LINE_LEN; i++) {
sPymbed 0:1387ff3eed4a 271 if (i < length)
sPymbed 0:1387ff3eed4a 272 sprintf(line + 1 + i * 3,"%02x ", buffer[i]);
sPymbed 0:1387ff3eed4a 273 else
sPymbed 0:1387ff3eed4a 274 sprintf(line + 1 + i * 3, " ");
sPymbed 0:1387ff3eed4a 275 }
sPymbed 0:1387ff3eed4a 276
sPymbed 0:1387ff3eed4a 277 sprintf(line + 1 + LINE_LEN * 3, "| ");
sPymbed 0:1387ff3eed4a 278
sPymbed 0:1387ff3eed4a 279 for (i = 0; i < LINE_LEN; i++)
sPymbed 0:1387ff3eed4a 280 if (i < length)
sPymbed 0:1387ff3eed4a 281 sprintf(line + 3 + LINE_LEN * 3 + i,
sPymbed 0:1387ff3eed4a 282 "%c", 31 < buffer[i] && buffer[i] < 127 ? buffer[i] : '.');
sPymbed 0:1387ff3eed4a 283
sPymbed 0:1387ff3eed4a 284 wolfssl_log(INFO_LOG, line);
sPymbed 0:1387ff3eed4a 285
sPymbed 0:1387ff3eed4a 286 if (length > LINE_LEN)
sPymbed 0:1387ff3eed4a 287 WOLFSSL_BUFFER(buffer + LINE_LEN, length - LINE_LEN);
sPymbed 0:1387ff3eed4a 288 }
sPymbed 0:1387ff3eed4a 289 }
sPymbed 0:1387ff3eed4a 290
sPymbed 0:1387ff3eed4a 291
sPymbed 0:1387ff3eed4a 292 void WOLFSSL_ENTER(const char* msg)
sPymbed 0:1387ff3eed4a 293 {
sPymbed 0:1387ff3eed4a 294 if (loggingEnabled) {
sPymbed 0:1387ff3eed4a 295 char buffer[WOLFSSL_MAX_ERROR_SZ];
sPymbed 0:1387ff3eed4a 296 XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg);
sPymbed 0:1387ff3eed4a 297 wolfssl_log(ENTER_LOG , buffer);
sPymbed 0:1387ff3eed4a 298 }
sPymbed 0:1387ff3eed4a 299 }
sPymbed 0:1387ff3eed4a 300
sPymbed 0:1387ff3eed4a 301
sPymbed 0:1387ff3eed4a 302 void WOLFSSL_LEAVE(const char* msg, int ret)
sPymbed 0:1387ff3eed4a 303 {
sPymbed 0:1387ff3eed4a 304 if (loggingEnabled) {
sPymbed 0:1387ff3eed4a 305 char buffer[WOLFSSL_MAX_ERROR_SZ];
sPymbed 0:1387ff3eed4a 306 XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
sPymbed 0:1387ff3eed4a 307 msg, ret);
sPymbed 0:1387ff3eed4a 308 wolfssl_log(LEAVE_LOG , buffer);
sPymbed 0:1387ff3eed4a 309 }
sPymbed 0:1387ff3eed4a 310 }
sPymbed 0:1387ff3eed4a 311 #endif /* !WOLFSSL_DEBUG_ERRORS_ONLY */
sPymbed 0:1387ff3eed4a 312 #endif /* DEBUG_WOLFSSL */
sPymbed 0:1387ff3eed4a 313
sPymbed 0:1387ff3eed4a 314 /*
sPymbed 0:1387ff3eed4a 315 * When using OPENSSL_EXTRA or DEBUG_WOLFSSL_VERBOSE macro then WOLFSSL_ERROR is
sPymbed 0:1387ff3eed4a 316 * mapped to new funtion WOLFSSL_ERROR_LINE which gets the line # and function
sPymbed 0:1387ff3eed4a 317 * name where WOLFSSL_ERROR is called at.
sPymbed 0:1387ff3eed4a 318 */
sPymbed 0:1387ff3eed4a 319 #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || \
sPymbed 0:1387ff3eed4a 320 defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
sPymbed 0:1387ff3eed4a 321
sPymbed 0:1387ff3eed4a 322 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
sPymbed 0:1387ff3eed4a 323 void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line,
sPymbed 0:1387ff3eed4a 324 const char* file, void* usrCtx)
sPymbed 0:1387ff3eed4a 325 #else
sPymbed 0:1387ff3eed4a 326 void WOLFSSL_ERROR(int error)
sPymbed 0:1387ff3eed4a 327 #endif
sPymbed 0:1387ff3eed4a 328 {
sPymbed 0:1387ff3eed4a 329 #ifdef WOLFSSL_ASYNC_CRYPT
sPymbed 0:1387ff3eed4a 330 if (error != WC_PENDING_E)
sPymbed 0:1387ff3eed4a 331 #endif
sPymbed 0:1387ff3eed4a 332 {
sPymbed 0:1387ff3eed4a 333 char buffer[WOLFSSL_MAX_ERROR_SZ];
sPymbed 0:1387ff3eed4a 334
sPymbed 0:1387ff3eed4a 335 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
sPymbed 0:1387ff3eed4a 336 (void)usrCtx; /* a user ctx for future flexibility */
sPymbed 0:1387ff3eed4a 337 (void)func;
sPymbed 0:1387ff3eed4a 338
sPymbed 0:1387ff3eed4a 339 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 340 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 341 XSNPRINTF(buffer, sizeof(buffer),
sPymbed 0:1387ff3eed4a 342 "wolfSSL error occurred, error = %d", error);
sPymbed 0:1387ff3eed4a 343 }
sPymbed 0:1387ff3eed4a 344 else {
sPymbed 0:1387ff3eed4a 345 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
sPymbed 0:1387ff3eed4a 346 /* If running in compatibility mode do not add want read and
sPymbed 0:1387ff3eed4a 347 want right to error queue */
sPymbed 0:1387ff3eed4a 348 if (error != WANT_READ && error != WANT_WRITE) {
sPymbed 0:1387ff3eed4a 349 #endif
sPymbed 0:1387ff3eed4a 350 if (error < 0)
sPymbed 0:1387ff3eed4a 351 error = error - (2 * error); /* get absolute value */
sPymbed 0:1387ff3eed4a 352 XSNPRINTF(buffer, sizeof(buffer),
sPymbed 0:1387ff3eed4a 353 "wolfSSL error occurred, error = %d line:%d file:%s",
sPymbed 0:1387ff3eed4a 354 error, line, file);
sPymbed 0:1387ff3eed4a 355 if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) {
sPymbed 0:1387ff3eed4a 356 WOLFSSL_MSG("Error creating logging node");
sPymbed 0:1387ff3eed4a 357 /* with void function there is no return here, continue on
sPymbed 0:1387ff3eed4a 358 * to unlock mutex and log what buffer was created. */
sPymbed 0:1387ff3eed4a 359 }
sPymbed 0:1387ff3eed4a 360 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
sPymbed 0:1387ff3eed4a 361 }
sPymbed 0:1387ff3eed4a 362 else {
sPymbed 0:1387ff3eed4a 363 XSNPRINTF(buffer, sizeof(buffer),
sPymbed 0:1387ff3eed4a 364 "wolfSSL error occurred, error = %d", error);
sPymbed 0:1387ff3eed4a 365
sPymbed 0:1387ff3eed4a 366 }
sPymbed 0:1387ff3eed4a 367 #endif
sPymbed 0:1387ff3eed4a 368
sPymbed 0:1387ff3eed4a 369 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 370 }
sPymbed 0:1387ff3eed4a 371 #else
sPymbed 0:1387ff3eed4a 372 XSNPRINTF(buffer, sizeof(buffer),
sPymbed 0:1387ff3eed4a 373 "wolfSSL error occurred, error = %d", error);
sPymbed 0:1387ff3eed4a 374 #endif
sPymbed 0:1387ff3eed4a 375
sPymbed 0:1387ff3eed4a 376 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 377 if (loggingEnabled)
sPymbed 0:1387ff3eed4a 378 wolfssl_log(ERROR_LOG , buffer);
sPymbed 0:1387ff3eed4a 379 #endif
sPymbed 0:1387ff3eed4a 380 }
sPymbed 0:1387ff3eed4a 381 }
sPymbed 0:1387ff3eed4a 382
sPymbed 0:1387ff3eed4a 383 void WOLFSSL_ERROR_MSG(const char* msg)
sPymbed 0:1387ff3eed4a 384 {
sPymbed 0:1387ff3eed4a 385 #ifdef DEBUG_WOLFSSL
sPymbed 0:1387ff3eed4a 386 if (loggingEnabled)
sPymbed 0:1387ff3eed4a 387 wolfssl_log(ERROR_LOG , msg);
sPymbed 0:1387ff3eed4a 388 #else
sPymbed 0:1387ff3eed4a 389 (void)msg;
sPymbed 0:1387ff3eed4a 390 #endif
sPymbed 0:1387ff3eed4a 391 }
sPymbed 0:1387ff3eed4a 392
sPymbed 0:1387ff3eed4a 393 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
sPymbed 0:1387ff3eed4a 394
sPymbed 0:1387ff3eed4a 395 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
sPymbed 0:1387ff3eed4a 396 /* Internal function that is called by wolfCrypt_Init() */
sPymbed 0:1387ff3eed4a 397 int wc_LoggingInit(void)
sPymbed 0:1387ff3eed4a 398 {
sPymbed 0:1387ff3eed4a 399 if (wc_InitMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 400 WOLFSSL_MSG("Bad Init Mutex");
sPymbed 0:1387ff3eed4a 401 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 402 }
sPymbed 0:1387ff3eed4a 403 wc_errors = NULL;
sPymbed 0:1387ff3eed4a 404 wc_current_node = NULL;
sPymbed 0:1387ff3eed4a 405 wc_last_node = NULL;
sPymbed 0:1387ff3eed4a 406
sPymbed 0:1387ff3eed4a 407 return 0;
sPymbed 0:1387ff3eed4a 408 }
sPymbed 0:1387ff3eed4a 409
sPymbed 0:1387ff3eed4a 410
sPymbed 0:1387ff3eed4a 411 /* internal function that is called by wolfCrypt_Cleanup */
sPymbed 0:1387ff3eed4a 412 int wc_LoggingCleanup(void)
sPymbed 0:1387ff3eed4a 413 {
sPymbed 0:1387ff3eed4a 414 /* clear logging entries */
sPymbed 0:1387ff3eed4a 415 wc_ClearErrorNodes();
sPymbed 0:1387ff3eed4a 416
sPymbed 0:1387ff3eed4a 417 /* free mutex */
sPymbed 0:1387ff3eed4a 418 if (wc_FreeMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 419 WOLFSSL_MSG("Bad Mutex free");
sPymbed 0:1387ff3eed4a 420 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 421 }
sPymbed 0:1387ff3eed4a 422 return 0;
sPymbed 0:1387ff3eed4a 423 }
sPymbed 0:1387ff3eed4a 424
sPymbed 0:1387ff3eed4a 425
sPymbed 0:1387ff3eed4a 426 /* peek at an error node
sPymbed 0:1387ff3eed4a 427 *
sPymbed 0:1387ff3eed4a 428 * idx : if -1 then the most recent node is looked at, otherwise search
sPymbed 0:1387ff3eed4a 429 * through queue for node at the given index
sPymbed 0:1387ff3eed4a 430 * file : pointer to internal file string
sPymbed 0:1387ff3eed4a 431 * reason : pointer to internal error reason
sPymbed 0:1387ff3eed4a 432 * line : line number that error happened at
sPymbed 0:1387ff3eed4a 433 *
sPymbed 0:1387ff3eed4a 434 * Returns a negative value in error case, on success returns the nodes error
sPymbed 0:1387ff3eed4a 435 * value which is positve (absolute value)
sPymbed 0:1387ff3eed4a 436 */
sPymbed 0:1387ff3eed4a 437 int wc_PeekErrorNode(int idx, const char **file, const char **reason,
sPymbed 0:1387ff3eed4a 438 int *line)
sPymbed 0:1387ff3eed4a 439 {
sPymbed 0:1387ff3eed4a 440 struct wc_error_queue* err;
sPymbed 0:1387ff3eed4a 441
sPymbed 0:1387ff3eed4a 442 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 443 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 444 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 445 }
sPymbed 0:1387ff3eed4a 446
sPymbed 0:1387ff3eed4a 447 if (idx < 0) {
sPymbed 0:1387ff3eed4a 448 err = wc_last_node;
sPymbed 0:1387ff3eed4a 449 if (err == NULL) {
sPymbed 0:1387ff3eed4a 450 WOLFSSL_MSG("No Errors in queue");
sPymbed 0:1387ff3eed4a 451 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 452 return BAD_STATE_E;
sPymbed 0:1387ff3eed4a 453 }
sPymbed 0:1387ff3eed4a 454 }
sPymbed 0:1387ff3eed4a 455 else {
sPymbed 0:1387ff3eed4a 456 int i;
sPymbed 0:1387ff3eed4a 457
sPymbed 0:1387ff3eed4a 458 err = (struct wc_error_queue*)wc_errors;
sPymbed 0:1387ff3eed4a 459 for (i = 0; i < idx; i++) {
sPymbed 0:1387ff3eed4a 460 if (err == NULL) {
sPymbed 0:1387ff3eed4a 461 WOLFSSL_MSG("Error node not found. Bad index?");
sPymbed 0:1387ff3eed4a 462 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 463 return BAD_FUNC_ARG;
sPymbed 0:1387ff3eed4a 464 }
sPymbed 0:1387ff3eed4a 465 err = err->next;
sPymbed 0:1387ff3eed4a 466 }
sPymbed 0:1387ff3eed4a 467 }
sPymbed 0:1387ff3eed4a 468
sPymbed 0:1387ff3eed4a 469 if (file != NULL) {
sPymbed 0:1387ff3eed4a 470 *file = err->file;
sPymbed 0:1387ff3eed4a 471 }
sPymbed 0:1387ff3eed4a 472
sPymbed 0:1387ff3eed4a 473 if (reason != NULL) {
sPymbed 0:1387ff3eed4a 474 *reason = err->error;
sPymbed 0:1387ff3eed4a 475 }
sPymbed 0:1387ff3eed4a 476
sPymbed 0:1387ff3eed4a 477 if (line != NULL) {
sPymbed 0:1387ff3eed4a 478 *line = err->line;
sPymbed 0:1387ff3eed4a 479 }
sPymbed 0:1387ff3eed4a 480
sPymbed 0:1387ff3eed4a 481 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 482
sPymbed 0:1387ff3eed4a 483 return err->value;
sPymbed 0:1387ff3eed4a 484 }
sPymbed 0:1387ff3eed4a 485
sPymbed 0:1387ff3eed4a 486
sPymbed 0:1387ff3eed4a 487 /* Pulls the current node from error queue and increments current state.
sPymbed 0:1387ff3eed4a 488 * Note: this does not delete nodes because input arguments are pointing to
sPymbed 0:1387ff3eed4a 489 * node buffers.
sPymbed 0:1387ff3eed4a 490 *
sPymbed 0:1387ff3eed4a 491 * file pointer to file that error was in. Can be NULL to return no file.
sPymbed 0:1387ff3eed4a 492 * reason error string giving reason for error. Can be NULL to return no reason.
sPymbed 0:1387ff3eed4a 493 * line retrun line number of where error happened.
sPymbed 0:1387ff3eed4a 494 *
sPymbed 0:1387ff3eed4a 495 * returns the error value on success and BAD_MUTEX_E or BAD_STATE_E on failure
sPymbed 0:1387ff3eed4a 496 */
sPymbed 0:1387ff3eed4a 497 int wc_PullErrorNode(const char **file, const char **reason, int *line)
sPymbed 0:1387ff3eed4a 498 {
sPymbed 0:1387ff3eed4a 499 struct wc_error_queue* err;
sPymbed 0:1387ff3eed4a 500 int value;
sPymbed 0:1387ff3eed4a 501
sPymbed 0:1387ff3eed4a 502 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 503 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 504 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 505 }
sPymbed 0:1387ff3eed4a 506
sPymbed 0:1387ff3eed4a 507 err = wc_current_node;
sPymbed 0:1387ff3eed4a 508 if (err == NULL) {
sPymbed 0:1387ff3eed4a 509 WOLFSSL_MSG("No Errors in queue");
sPymbed 0:1387ff3eed4a 510 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 511 return BAD_STATE_E;
sPymbed 0:1387ff3eed4a 512 }
sPymbed 0:1387ff3eed4a 513
sPymbed 0:1387ff3eed4a 514 if (file != NULL) {
sPymbed 0:1387ff3eed4a 515 *file = err->file;
sPymbed 0:1387ff3eed4a 516 }
sPymbed 0:1387ff3eed4a 517
sPymbed 0:1387ff3eed4a 518 if (reason != NULL) {
sPymbed 0:1387ff3eed4a 519 *reason = err->error;
sPymbed 0:1387ff3eed4a 520 }
sPymbed 0:1387ff3eed4a 521
sPymbed 0:1387ff3eed4a 522 if (line != NULL) {
sPymbed 0:1387ff3eed4a 523 *line = err->line;
sPymbed 0:1387ff3eed4a 524 }
sPymbed 0:1387ff3eed4a 525
sPymbed 0:1387ff3eed4a 526 value = err->value;
sPymbed 0:1387ff3eed4a 527 wc_current_node = err->next;
sPymbed 0:1387ff3eed4a 528 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 529
sPymbed 0:1387ff3eed4a 530 return value;
sPymbed 0:1387ff3eed4a 531 }
sPymbed 0:1387ff3eed4a 532
sPymbed 0:1387ff3eed4a 533
sPymbed 0:1387ff3eed4a 534 /* create new error node and add it to the queue
sPymbed 0:1387ff3eed4a 535 * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal
sPymbed 0:1387ff3eed4a 536 * function. debug_mutex should be locked before a call to this function. */
sPymbed 0:1387ff3eed4a 537 int wc_AddErrorNode(int error, int line, char* buf, char* file)
sPymbed 0:1387ff3eed4a 538 {
sPymbed 0:1387ff3eed4a 539
sPymbed 0:1387ff3eed4a 540 struct wc_error_queue* err;
sPymbed 0:1387ff3eed4a 541
sPymbed 0:1387ff3eed4a 542 err = (struct wc_error_queue*)XMALLOC(
sPymbed 0:1387ff3eed4a 543 sizeof(struct wc_error_queue), wc_error_heap, DYNAMIC_TYPE_LOG);
sPymbed 0:1387ff3eed4a 544 if (err == NULL) {
sPymbed 0:1387ff3eed4a 545 WOLFSSL_MSG("Unable to create error node for log");
sPymbed 0:1387ff3eed4a 546 return MEMORY_E;
sPymbed 0:1387ff3eed4a 547 }
sPymbed 0:1387ff3eed4a 548 else {
sPymbed 0:1387ff3eed4a 549 int sz;
sPymbed 0:1387ff3eed4a 550
sPymbed 0:1387ff3eed4a 551 XMEMSET(err, 0, sizeof(struct wc_error_queue));
sPymbed 0:1387ff3eed4a 552 err->heap = wc_error_heap;
sPymbed 0:1387ff3eed4a 553 sz = (int)XSTRLEN(buf);
sPymbed 0:1387ff3eed4a 554 if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sPymbed 0:1387ff3eed4a 555 sz = WOLFSSL_MAX_ERROR_SZ - 1;
sPymbed 0:1387ff3eed4a 556 }
sPymbed 0:1387ff3eed4a 557 if (sz > 0) {
sPymbed 0:1387ff3eed4a 558 XMEMCPY(err->error, buf, sz);
sPymbed 0:1387ff3eed4a 559 }
sPymbed 0:1387ff3eed4a 560
sPymbed 0:1387ff3eed4a 561 sz = (int)XSTRLEN(file);
sPymbed 0:1387ff3eed4a 562 if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
sPymbed 0:1387ff3eed4a 563 sz = WOLFSSL_MAX_ERROR_SZ - 1;
sPymbed 0:1387ff3eed4a 564 }
sPymbed 0:1387ff3eed4a 565 if (sz > 0) {
sPymbed 0:1387ff3eed4a 566 XMEMCPY(err->file, file, sz);
sPymbed 0:1387ff3eed4a 567 }
sPymbed 0:1387ff3eed4a 568
sPymbed 0:1387ff3eed4a 569 err->value = error;
sPymbed 0:1387ff3eed4a 570 err->line = line;
sPymbed 0:1387ff3eed4a 571
sPymbed 0:1387ff3eed4a 572 /* make sure is terminated */
sPymbed 0:1387ff3eed4a 573 err->error[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
sPymbed 0:1387ff3eed4a 574 err->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
sPymbed 0:1387ff3eed4a 575
sPymbed 0:1387ff3eed4a 576
sPymbed 0:1387ff3eed4a 577 /* since is queue place new node at last of the list */
sPymbed 0:1387ff3eed4a 578 if (wc_last_node == NULL) {
sPymbed 0:1387ff3eed4a 579 /* case of first node added to queue */
sPymbed 0:1387ff3eed4a 580 if (wc_errors != NULL) {
sPymbed 0:1387ff3eed4a 581 /* check for unexpected case before over writing wc_errors */
sPymbed 0:1387ff3eed4a 582 WOLFSSL_MSG("ERROR in adding new node to logging queue!!\n");
sPymbed 0:1387ff3eed4a 583 }
sPymbed 0:1387ff3eed4a 584 else {
sPymbed 0:1387ff3eed4a 585 wc_errors = err;
sPymbed 0:1387ff3eed4a 586 wc_last_node = err;
sPymbed 0:1387ff3eed4a 587 wc_current_node = err;
sPymbed 0:1387ff3eed4a 588 }
sPymbed 0:1387ff3eed4a 589 }
sPymbed 0:1387ff3eed4a 590 else {
sPymbed 0:1387ff3eed4a 591 wc_last_node->next = err;
sPymbed 0:1387ff3eed4a 592 err->prev = wc_last_node;
sPymbed 0:1387ff3eed4a 593 wc_last_node = err;
sPymbed 0:1387ff3eed4a 594
sPymbed 0:1387ff3eed4a 595 /* check the case where have read to the end of the queue and the
sPymbed 0:1387ff3eed4a 596 * current node to read needs updated */
sPymbed 0:1387ff3eed4a 597 if (wc_current_node == NULL) {
sPymbed 0:1387ff3eed4a 598 wc_current_node = err;
sPymbed 0:1387ff3eed4a 599 }
sPymbed 0:1387ff3eed4a 600 }
sPymbed 0:1387ff3eed4a 601 }
sPymbed 0:1387ff3eed4a 602
sPymbed 0:1387ff3eed4a 603 return 0;
sPymbed 0:1387ff3eed4a 604 }
sPymbed 0:1387ff3eed4a 605
sPymbed 0:1387ff3eed4a 606 /* Removes the error node at the specified index.
sPymbed 0:1387ff3eed4a 607 * idx : if -1 then the most recent node is looked at, otherwise search
sPymbed 0:1387ff3eed4a 608 * through queue for node at the given index
sPymbed 0:1387ff3eed4a 609 */
sPymbed 0:1387ff3eed4a 610 void wc_RemoveErrorNode(int idx)
sPymbed 0:1387ff3eed4a 611 {
sPymbed 0:1387ff3eed4a 612 struct wc_error_queue* current;
sPymbed 0:1387ff3eed4a 613
sPymbed 0:1387ff3eed4a 614 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 615 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 616 return;
sPymbed 0:1387ff3eed4a 617 }
sPymbed 0:1387ff3eed4a 618
sPymbed 0:1387ff3eed4a 619 if (idx == -1)
sPymbed 0:1387ff3eed4a 620 current = wc_last_node;
sPymbed 0:1387ff3eed4a 621 else {
sPymbed 0:1387ff3eed4a 622 current = (struct wc_error_queue*)wc_errors;
sPymbed 0:1387ff3eed4a 623 for (; current != NULL && idx > 0; idx--)
sPymbed 0:1387ff3eed4a 624 current = current->next;
sPymbed 0:1387ff3eed4a 625 }
sPymbed 0:1387ff3eed4a 626 if (current != NULL) {
sPymbed 0:1387ff3eed4a 627 if (current->prev != NULL)
sPymbed 0:1387ff3eed4a 628 current->prev->next = current->next;
sPymbed 0:1387ff3eed4a 629 if (wc_last_node == current)
sPymbed 0:1387ff3eed4a 630 wc_last_node = current->prev;
sPymbed 0:1387ff3eed4a 631 if (wc_errors == current)
sPymbed 0:1387ff3eed4a 632 wc_errors = current->next;
sPymbed 0:1387ff3eed4a 633 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
sPymbed 0:1387ff3eed4a 634 }
sPymbed 0:1387ff3eed4a 635
sPymbed 0:1387ff3eed4a 636 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 637 }
sPymbed 0:1387ff3eed4a 638
sPymbed 0:1387ff3eed4a 639
sPymbed 0:1387ff3eed4a 640 /* Clears out the list of error nodes.
sPymbed 0:1387ff3eed4a 641 */
sPymbed 0:1387ff3eed4a 642 void wc_ClearErrorNodes(void)
sPymbed 0:1387ff3eed4a 643 {
sPymbed 0:1387ff3eed4a 644 #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)
sPymbed 0:1387ff3eed4a 645 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 646 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 647 return;
sPymbed 0:1387ff3eed4a 648 }
sPymbed 0:1387ff3eed4a 649
sPymbed 0:1387ff3eed4a 650 /* free all nodes from error queue */
sPymbed 0:1387ff3eed4a 651 {
sPymbed 0:1387ff3eed4a 652 struct wc_error_queue* current;
sPymbed 0:1387ff3eed4a 653 struct wc_error_queue* next;
sPymbed 0:1387ff3eed4a 654
sPymbed 0:1387ff3eed4a 655 current = (struct wc_error_queue*)wc_errors;
sPymbed 0:1387ff3eed4a 656 while (current != NULL) {
sPymbed 0:1387ff3eed4a 657 next = current->next;
sPymbed 0:1387ff3eed4a 658 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
sPymbed 0:1387ff3eed4a 659 current = next;
sPymbed 0:1387ff3eed4a 660 }
sPymbed 0:1387ff3eed4a 661 }
sPymbed 0:1387ff3eed4a 662
sPymbed 0:1387ff3eed4a 663 wc_errors = NULL;
sPymbed 0:1387ff3eed4a 664 wc_last_node = NULL;
sPymbed 0:1387ff3eed4a 665 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 666 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */
sPymbed 0:1387ff3eed4a 667 }
sPymbed 0:1387ff3eed4a 668
sPymbed 0:1387ff3eed4a 669 int wc_SetLoggingHeap(void* h)
sPymbed 0:1387ff3eed4a 670 {
sPymbed 0:1387ff3eed4a 671 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 672 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 673 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 674 }
sPymbed 0:1387ff3eed4a 675 wc_error_heap = h;
sPymbed 0:1387ff3eed4a 676 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 677 return 0;
sPymbed 0:1387ff3eed4a 678 }
sPymbed 0:1387ff3eed4a 679
sPymbed 0:1387ff3eed4a 680
sPymbed 0:1387ff3eed4a 681 /* frees all nodes in the queue
sPymbed 0:1387ff3eed4a 682 *
sPymbed 0:1387ff3eed4a 683 * id this is the thread id
sPymbed 0:1387ff3eed4a 684 */
sPymbed 0:1387ff3eed4a 685 int wc_ERR_remove_state(void)
sPymbed 0:1387ff3eed4a 686 {
sPymbed 0:1387ff3eed4a 687 struct wc_error_queue* current;
sPymbed 0:1387ff3eed4a 688 struct wc_error_queue* next;
sPymbed 0:1387ff3eed4a 689
sPymbed 0:1387ff3eed4a 690 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 691 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 692 return BAD_MUTEX_E;
sPymbed 0:1387ff3eed4a 693 }
sPymbed 0:1387ff3eed4a 694
sPymbed 0:1387ff3eed4a 695 /* free all nodes from error queue */
sPymbed 0:1387ff3eed4a 696 current = (struct wc_error_queue*)wc_errors;
sPymbed 0:1387ff3eed4a 697 while (current != NULL) {
sPymbed 0:1387ff3eed4a 698 next = current->next;
sPymbed 0:1387ff3eed4a 699 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
sPymbed 0:1387ff3eed4a 700 current = next;
sPymbed 0:1387ff3eed4a 701 }
sPymbed 0:1387ff3eed4a 702
sPymbed 0:1387ff3eed4a 703 wc_errors = NULL;
sPymbed 0:1387ff3eed4a 704 wc_last_node = NULL;
sPymbed 0:1387ff3eed4a 705
sPymbed 0:1387ff3eed4a 706 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 707
sPymbed 0:1387ff3eed4a 708 return 0;
sPymbed 0:1387ff3eed4a 709 }
sPymbed 0:1387ff3eed4a 710
sPymbed 0:1387ff3eed4a 711
sPymbed 0:1387ff3eed4a 712 #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
sPymbed 0:1387ff3eed4a 713 /* empties out the error queue into the file */
sPymbed 0:1387ff3eed4a 714 void wc_ERR_print_errors_fp(XFILE fp)
sPymbed 0:1387ff3eed4a 715 {
sPymbed 0:1387ff3eed4a 716 WOLFSSL_ENTER("wc_ERR_print_errors_fp");
sPymbed 0:1387ff3eed4a 717
sPymbed 0:1387ff3eed4a 718 if (wc_LockMutex(&debug_mutex) != 0) {
sPymbed 0:1387ff3eed4a 719 WOLFSSL_MSG("Lock debug mutex failed");
sPymbed 0:1387ff3eed4a 720 }
sPymbed 0:1387ff3eed4a 721 else {
sPymbed 0:1387ff3eed4a 722 /* free all nodes from error queue and print them to file */
sPymbed 0:1387ff3eed4a 723 {
sPymbed 0:1387ff3eed4a 724 struct wc_error_queue* current;
sPymbed 0:1387ff3eed4a 725 struct wc_error_queue* next;
sPymbed 0:1387ff3eed4a 726
sPymbed 0:1387ff3eed4a 727 current = (struct wc_error_queue*)wc_errors;
sPymbed 0:1387ff3eed4a 728 while (current != NULL) {
sPymbed 0:1387ff3eed4a 729 next = current->next;
sPymbed 0:1387ff3eed4a 730 fprintf(fp, "%s\n", current->error);
sPymbed 0:1387ff3eed4a 731 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
sPymbed 0:1387ff3eed4a 732 current = next;
sPymbed 0:1387ff3eed4a 733 }
sPymbed 0:1387ff3eed4a 734
sPymbed 0:1387ff3eed4a 735 /* set global pointers to match having been freed */
sPymbed 0:1387ff3eed4a 736 wc_errors = NULL;
sPymbed 0:1387ff3eed4a 737 wc_last_node = NULL;
sPymbed 0:1387ff3eed4a 738 }
sPymbed 0:1387ff3eed4a 739
sPymbed 0:1387ff3eed4a 740 wc_UnLockMutex(&debug_mutex);
sPymbed 0:1387ff3eed4a 741 }
sPymbed 0:1387ff3eed4a 742 }
sPymbed 0:1387ff3eed4a 743 #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
sPymbed 0:1387ff3eed4a 744
sPymbed 0:1387ff3eed4a 745 #endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */
sPymbed 0:1387ff3eed4a 746
sPymbed 0:1387ff3eed4a 747