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