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