wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 06:16:19 2017 +0000
Revision:
13:80fb167dafdf
wolfSSL 3.11.1: TLS1.3 Beta

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 13:80fb167dafdf 1 /* logging.c
wolfSSL 13:80fb167dafdf 2 *
wolfSSL 13:80fb167dafdf 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 13:80fb167dafdf 4 *
wolfSSL 13:80fb167dafdf 5 * This file is part of wolfSSL.
wolfSSL 13:80fb167dafdf 6 *
wolfSSL 13:80fb167dafdf 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 13:80fb167dafdf 8 * it under the terms of the GNU General Public License as published by
wolfSSL 13:80fb167dafdf 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 13:80fb167dafdf 10 * (at your option) any later version.
wolfSSL 13:80fb167dafdf 11 *
wolfSSL 13:80fb167dafdf 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 13:80fb167dafdf 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 13:80fb167dafdf 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 13:80fb167dafdf 15 * GNU General Public License for more details.
wolfSSL 13:80fb167dafdf 16 *
wolfSSL 13:80fb167dafdf 17 * You should have received a copy of the GNU General Public License
wolfSSL 13:80fb167dafdf 18 * along with this program; if not, write to the Free Software
wolfSSL 13:80fb167dafdf 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 13:80fb167dafdf 20 */
wolfSSL 13:80fb167dafdf 21
wolfSSL 13:80fb167dafdf 22
wolfSSL 13:80fb167dafdf 23 #ifdef HAVE_CONFIG_H
wolfSSL 13:80fb167dafdf 24 #include <config.h>
wolfSSL 13:80fb167dafdf 25 #endif
wolfSSL 13:80fb167dafdf 26
wolfSSL 13:80fb167dafdf 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 13:80fb167dafdf 28
wolfSSL 13:80fb167dafdf 29 /* submitted by eof */
wolfSSL 13:80fb167dafdf 30
wolfSSL 13:80fb167dafdf 31 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 13:80fb167dafdf 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 13:80fb167dafdf 33
wolfSSL 13:80fb167dafdf 34
wolfSSL 13:80fb167dafdf 35 #ifdef __cplusplus
wolfSSL 13:80fb167dafdf 36 extern "C" {
wolfSSL 13:80fb167dafdf 37 #endif
wolfSSL 13:80fb167dafdf 38 WOLFSSL_API int wolfSSL_Debugging_ON(void);
wolfSSL 13:80fb167dafdf 39 WOLFSSL_API void wolfSSL_Debugging_OFF(void);
wolfSSL 13:80fb167dafdf 40 #ifdef __cplusplus
wolfSSL 13:80fb167dafdf 41 }
wolfSSL 13:80fb167dafdf 42 #endif
wolfSSL 13:80fb167dafdf 43
wolfSSL 13:80fb167dafdf 44 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
wolfSSL 13:80fb167dafdf 45 static wolfSSL_Mutex debug_mutex; /* mutex for access to debug structure */
wolfSSL 13:80fb167dafdf 46
wolfSSL 13:80fb167dafdf 47 /* accessing any node from the queue should be wrapped in a lock of
wolfSSL 13:80fb167dafdf 48 * debug_mutex */
wolfSSL 13:80fb167dafdf 49 static void* wc_error_heap;
wolfSSL 13:80fb167dafdf 50 struct wc_error_queue {
wolfSSL 13:80fb167dafdf 51 void* heap; /* the heap hint used with nodes creation */
wolfSSL 13:80fb167dafdf 52 struct wc_error_queue* next;
wolfSSL 13:80fb167dafdf 53 struct wc_error_queue* prev;
wolfSSL 13:80fb167dafdf 54 char error[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 13:80fb167dafdf 55 char file[WOLFSSL_MAX_ERROR_SZ];
wolfSSL 13:80fb167dafdf 56 int value;
wolfSSL 13:80fb167dafdf 57 int line;
wolfSSL 13:80fb167dafdf 58 };
wolfSSL 13:80fb167dafdf 59 volatile struct wc_error_queue* wc_errors;
wolfSSL 13:80fb167dafdf 60 static struct wc_error_queue* wc_last_node;
wolfSSL 13:80fb167dafdf 61 /* pointer to last node in queue to make insertion O(1) */
wolfSSL 13:80fb167dafdf 62 #endif
wolfSSL 13:80fb167dafdf 63
wolfSSL 13:80fb167dafdf 64
wolfSSL 13:80fb167dafdf 65
wolfSSL 13:80fb167dafdf 66 #if defined(DEBUG_WOLFSSL)
wolfSSL 13:80fb167dafdf 67
wolfSSL 13:80fb167dafdf 68 /* Set these to default values initially. */
wolfSSL 13:80fb167dafdf 69 static wolfSSL_Logging_cb log_function = NULL;
wolfSSL 13:80fb167dafdf 70 static int loggingEnabled = 0;
wolfSSL 13:80fb167dafdf 71
wolfSSL 13:80fb167dafdf 72 #endif /* DEBUG_WOLFSSL */
wolfSSL 13:80fb167dafdf 73
wolfSSL 13:80fb167dafdf 74
wolfSSL 13:80fb167dafdf 75 int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
wolfSSL 13:80fb167dafdf 76 {
wolfSSL 13:80fb167dafdf 77 #ifdef DEBUG_WOLFSSL
wolfSSL 13:80fb167dafdf 78 int res = 0;
wolfSSL 13:80fb167dafdf 79
wolfSSL 13:80fb167dafdf 80 if (f)
wolfSSL 13:80fb167dafdf 81 log_function = f;
wolfSSL 13:80fb167dafdf 82 else
wolfSSL 13:80fb167dafdf 83 res = BAD_FUNC_ARG;
wolfSSL 13:80fb167dafdf 84
wolfSSL 13:80fb167dafdf 85 return res;
wolfSSL 13:80fb167dafdf 86 #else
wolfSSL 13:80fb167dafdf 87 (void)f;
wolfSSL 13:80fb167dafdf 88 return NOT_COMPILED_IN;
wolfSSL 13:80fb167dafdf 89 #endif
wolfSSL 13:80fb167dafdf 90 }
wolfSSL 13:80fb167dafdf 91
wolfSSL 13:80fb167dafdf 92
wolfSSL 13:80fb167dafdf 93 int wolfSSL_Debugging_ON(void)
wolfSSL 13:80fb167dafdf 94 {
wolfSSL 13:80fb167dafdf 95 #ifdef DEBUG_WOLFSSL
wolfSSL 13:80fb167dafdf 96 loggingEnabled = 1;
wolfSSL 13:80fb167dafdf 97 return 0;
wolfSSL 13:80fb167dafdf 98 #else
wolfSSL 13:80fb167dafdf 99 return NOT_COMPILED_IN;
wolfSSL 13:80fb167dafdf 100 #endif
wolfSSL 13:80fb167dafdf 101 }
wolfSSL 13:80fb167dafdf 102
wolfSSL 13:80fb167dafdf 103
wolfSSL 13:80fb167dafdf 104 void wolfSSL_Debugging_OFF(void)
wolfSSL 13:80fb167dafdf 105 {
wolfSSL 13:80fb167dafdf 106 #ifdef DEBUG_WOLFSSL
wolfSSL 13:80fb167dafdf 107 loggingEnabled = 0;
wolfSSL 13:80fb167dafdf 108 #endif
wolfSSL 13:80fb167dafdf 109 }
wolfSSL 13:80fb167dafdf 110
wolfSSL 13:80fb167dafdf 111
wolfSSL 13:80fb167dafdf 112 #ifdef DEBUG_WOLFSSL
wolfSSL 13:80fb167dafdf 113
wolfSSL 13:80fb167dafdf 114 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
wolfSSL 13:80fb167dafdf 115 #if MQX_USE_IO_OLD
wolfSSL 13:80fb167dafdf 116 #include <fio.h>
wolfSSL 13:80fb167dafdf 117 #else
wolfSSL 13:80fb167dafdf 118 #include <nio.h>
wolfSSL 13:80fb167dafdf 119 #endif
wolfSSL 13:80fb167dafdf 120 #else
wolfSSL 13:80fb167dafdf 121 #include <stdio.h> /* for default printf stuff */
wolfSSL 13:80fb167dafdf 122 #endif
wolfSSL 13:80fb167dafdf 123
wolfSSL 13:80fb167dafdf 124 #ifdef THREADX
wolfSSL 13:80fb167dafdf 125 int dc_log_printf(char*, ...);
wolfSSL 13:80fb167dafdf 126 #endif
wolfSSL 13:80fb167dafdf 127
wolfSSL 13:80fb167dafdf 128 static void wolfssl_log(const int logLevel, const char *const logMessage)
wolfSSL 13:80fb167dafdf 129 {
wolfSSL 13:80fb167dafdf 130 if (log_function)
wolfSSL 13:80fb167dafdf 131 log_function(logLevel, logMessage);
wolfSSL 13:80fb167dafdf 132 else {
wolfSSL 13:80fb167dafdf 133 if (loggingEnabled) {
wolfSSL 13:80fb167dafdf 134 #ifdef THREADX
wolfSSL 13:80fb167dafdf 135 dc_log_printf("%s\n", logMessage);
wolfSSL 13:80fb167dafdf 136 #elif defined(MICRIUM)
wolfSSL 13:80fb167dafdf 137 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
wolfSSL 13:80fb167dafdf 138 NetSecure_TraceOut((CPU_CHAR *)logMessage);
wolfSSL 13:80fb167dafdf 139 #endif
wolfSSL 13:80fb167dafdf 140 #elif defined(WOLFSSL_MDK_ARM)
wolfSSL 13:80fb167dafdf 141 fflush(stdout) ;
wolfSSL 13:80fb167dafdf 142 printf("%s\n", logMessage);
wolfSSL 13:80fb167dafdf 143 fflush(stdout) ;
wolfSSL 13:80fb167dafdf 144 #elif defined(WOLFSSL_LOG_PRINTF)
wolfSSL 13:80fb167dafdf 145 printf("%s\n", logMessage);
wolfSSL 13:80fb167dafdf 146 #elif defined(WOLFSSL_UTASKER)
wolfSSL 13:80fb167dafdf 147 fnDebugMsg((char*)logMessage);
wolfSSL 13:80fb167dafdf 148 fnDebugMsg("\r\n");
wolfSSL 13:80fb167dafdf 149 #else
wolfSSL 13:80fb167dafdf 150 fprintf(stderr, "%s\n", logMessage);
wolfSSL 13:80fb167dafdf 151 #endif
wolfSSL 13:80fb167dafdf 152 }
wolfSSL 13:80fb167dafdf 153 }
wolfSSL 13:80fb167dafdf 154 }
wolfSSL 13:80fb167dafdf 155
wolfSSL 13:80fb167dafdf 156
wolfSSL 13:80fb167dafdf 157 void WOLFSSL_MSG(const char* msg)
wolfSSL 13:80fb167dafdf 158 {
wolfSSL 13:80fb167dafdf 159 if (loggingEnabled)
wolfSSL 13:80fb167dafdf 160 wolfssl_log(INFO_LOG , msg);
wolfSSL 13:80fb167dafdf 161 }
wolfSSL 13:80fb167dafdf 162
wolfSSL 13:80fb167dafdf 163
wolfSSL 13:80fb167dafdf 164 void WOLFSSL_BUFFER(const byte* buffer, word32 length)
wolfSSL 13:80fb167dafdf 165 {
wolfSSL 13:80fb167dafdf 166 #define LINE_LEN 16
wolfSSL 13:80fb167dafdf 167
wolfSSL 13:80fb167dafdf 168 if (loggingEnabled) {
wolfSSL 13:80fb167dafdf 169 word32 i;
wolfSSL 13:80fb167dafdf 170 char line[80];
wolfSSL 13:80fb167dafdf 171
wolfSSL 13:80fb167dafdf 172 if (!buffer) {
wolfSSL 13:80fb167dafdf 173 wolfssl_log(INFO_LOG, "\tNULL");
wolfSSL 13:80fb167dafdf 174
wolfSSL 13:80fb167dafdf 175 return;
wolfSSL 13:80fb167dafdf 176 }
wolfSSL 13:80fb167dafdf 177
wolfSSL 13:80fb167dafdf 178 sprintf(line, "\t");
wolfSSL 13:80fb167dafdf 179
wolfSSL 13:80fb167dafdf 180 for (i = 0; i < LINE_LEN; i++) {
wolfSSL 13:80fb167dafdf 181 if (i < length)
wolfSSL 13:80fb167dafdf 182 sprintf(line + 1 + i * 3,"%02x ", buffer[i]);
wolfSSL 13:80fb167dafdf 183 else
wolfSSL 13:80fb167dafdf 184 sprintf(line + 1 + i * 3, " ");
wolfSSL 13:80fb167dafdf 185 }
wolfSSL 13:80fb167dafdf 186
wolfSSL 13:80fb167dafdf 187 sprintf(line + 1 + LINE_LEN * 3, "| ");
wolfSSL 13:80fb167dafdf 188
wolfSSL 13:80fb167dafdf 189 for (i = 0; i < LINE_LEN; i++)
wolfSSL 13:80fb167dafdf 190 if (i < length)
wolfSSL 13:80fb167dafdf 191 sprintf(line + 3 + LINE_LEN * 3 + i,
wolfSSL 13:80fb167dafdf 192 "%c", 31 < buffer[i] && buffer[i] < 127 ? buffer[i] : '.');
wolfSSL 13:80fb167dafdf 193
wolfSSL 13:80fb167dafdf 194 wolfssl_log(INFO_LOG, line);
wolfSSL 13:80fb167dafdf 195
wolfSSL 13:80fb167dafdf 196 if (length > LINE_LEN)
wolfSSL 13:80fb167dafdf 197 WOLFSSL_BUFFER(buffer + LINE_LEN, length - LINE_LEN);
wolfSSL 13:80fb167dafdf 198 }
wolfSSL 13:80fb167dafdf 199 }
wolfSSL 13:80fb167dafdf 200
wolfSSL 13:80fb167dafdf 201
wolfSSL 13:80fb167dafdf 202 void WOLFSSL_ENTER(const char* msg)
wolfSSL 13:80fb167dafdf 203 {
wolfSSL 13:80fb167dafdf 204 if (loggingEnabled) {
wolfSSL 13:80fb167dafdf 205 char buffer[80];
wolfSSL 13:80fb167dafdf 206 sprintf(buffer, "wolfSSL Entering %s", msg);
wolfSSL 13:80fb167dafdf 207 wolfssl_log(ENTER_LOG , buffer);
wolfSSL 13:80fb167dafdf 208 }
wolfSSL 13:80fb167dafdf 209 }
wolfSSL 13:80fb167dafdf 210
wolfSSL 13:80fb167dafdf 211
wolfSSL 13:80fb167dafdf 212 void WOLFSSL_LEAVE(const char* msg, int ret)
wolfSSL 13:80fb167dafdf 213 {
wolfSSL 13:80fb167dafdf 214 if (loggingEnabled) {
wolfSSL 13:80fb167dafdf 215 char buffer[80];
wolfSSL 13:80fb167dafdf 216 sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret);
wolfSSL 13:80fb167dafdf 217 wolfssl_log(LEAVE_LOG , buffer);
wolfSSL 13:80fb167dafdf 218 }
wolfSSL 13:80fb167dafdf 219 }
wolfSSL 13:80fb167dafdf 220 #endif /* DEBUG_WOLFSSL */
wolfSSL 13:80fb167dafdf 221
wolfSSL 13:80fb167dafdf 222 /*
wolfSSL 13:80fb167dafdf 223 * When using OPENSSL_EXTRA or DEBUG_WOLFSSL_VERBOSE macro then WOLFSSL_ERROR is
wolfSSL 13:80fb167dafdf 224 * mapped to new funtion WOLFSSL_ERROR_LINE which gets the line # and function
wolfSSL 13:80fb167dafdf 225 * name where WOLFSSL_ERROR is called at.
wolfSSL 13:80fb167dafdf 226 */
wolfSSL 13:80fb167dafdf 227 #if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY)
wolfSSL 13:80fb167dafdf 228 #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE))
wolfSSL 13:80fb167dafdf 229 void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line,
wolfSSL 13:80fb167dafdf 230 const char* file, void* usrCtx)
wolfSSL 13:80fb167dafdf 231 #else
wolfSSL 13:80fb167dafdf 232 void WOLFSSL_ERROR(int error)
wolfSSL 13:80fb167dafdf 233 #endif
wolfSSL 13:80fb167dafdf 234 {
wolfSSL 13:80fb167dafdf 235 #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_NGINX)
wolfSSL 13:80fb167dafdf 236 if (loggingEnabled && error != WC_PENDING_E)
wolfSSL 13:80fb167dafdf 237 #endif
wolfSSL 13:80fb167dafdf 238 {
wolfSSL 13:80fb167dafdf 239 char buffer[80];
wolfSSL 13:80fb167dafdf 240 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
wolfSSL 13:80fb167dafdf 241 (void)usrCtx; /* a user ctx for future flexibility */
wolfSSL 13:80fb167dafdf 242 (void)func;
wolfSSL 13:80fb167dafdf 243
wolfSSL 13:80fb167dafdf 244 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 245 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 246 sprintf(buffer, "wolfSSL error occurred, error = %d", error);
wolfSSL 13:80fb167dafdf 247 }
wolfSSL 13:80fb167dafdf 248 else {
wolfSSL 13:80fb167dafdf 249 if (error < 0) error = error - (2*error); /*get absolute value*/
wolfSSL 13:80fb167dafdf 250 sprintf(buffer, "wolfSSL error occurred, error = %d line:%d file:%s",
wolfSSL 13:80fb167dafdf 251 error, line, file);
wolfSSL 13:80fb167dafdf 252 if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) {
wolfSSL 13:80fb167dafdf 253 WOLFSSL_MSG("Error creating logging node");
wolfSSL 13:80fb167dafdf 254 /* with void function there is no return here, continue on
wolfSSL 13:80fb167dafdf 255 * to unlock mutex and log what buffer was created. */
wolfSSL 13:80fb167dafdf 256 }
wolfSSL 13:80fb167dafdf 257
wolfSSL 13:80fb167dafdf 258 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 259 }
wolfSSL 13:80fb167dafdf 260 #else
wolfSSL 13:80fb167dafdf 261 sprintf(buffer, "wolfSSL error occurred, error = %d", error);
wolfSSL 13:80fb167dafdf 262 #endif
wolfSSL 13:80fb167dafdf 263 #ifdef DEBUG_WOLFSSL
wolfSSL 13:80fb167dafdf 264 wolfssl_log(ERROR_LOG , buffer);
wolfSSL 13:80fb167dafdf 265 #endif
wolfSSL 13:80fb167dafdf 266 }
wolfSSL 13:80fb167dafdf 267 }
wolfSSL 13:80fb167dafdf 268
wolfSSL 13:80fb167dafdf 269 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
wolfSSL 13:80fb167dafdf 270
wolfSSL 13:80fb167dafdf 271 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
wolfSSL 13:80fb167dafdf 272 /* Internal function that is called by wolfCrypt_Init() */
wolfSSL 13:80fb167dafdf 273 int wc_LoggingInit(void)
wolfSSL 13:80fb167dafdf 274 {
wolfSSL 13:80fb167dafdf 275 if (wc_InitMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 276 WOLFSSL_MSG("Bad Init Mutex");
wolfSSL 13:80fb167dafdf 277 return BAD_MUTEX_E;
wolfSSL 13:80fb167dafdf 278 }
wolfSSL 13:80fb167dafdf 279 wc_errors = NULL;
wolfSSL 13:80fb167dafdf 280 wc_last_node = NULL;
wolfSSL 13:80fb167dafdf 281
wolfSSL 13:80fb167dafdf 282 return 0;
wolfSSL 13:80fb167dafdf 283 }
wolfSSL 13:80fb167dafdf 284
wolfSSL 13:80fb167dafdf 285
wolfSSL 13:80fb167dafdf 286 /* internal function that is called by wolfCrypt_Cleanup */
wolfSSL 13:80fb167dafdf 287 int wc_LoggingCleanup(void)
wolfSSL 13:80fb167dafdf 288 {
wolfSSL 13:80fb167dafdf 289 /* clear logging entries */
wolfSSL 13:80fb167dafdf 290 wc_ClearErrorNodes();
wolfSSL 13:80fb167dafdf 291
wolfSSL 13:80fb167dafdf 292 /* free mutex */
wolfSSL 13:80fb167dafdf 293 if (wc_FreeMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 294 WOLFSSL_MSG("Bad Mutex free");
wolfSSL 13:80fb167dafdf 295 return BAD_MUTEX_E;
wolfSSL 13:80fb167dafdf 296 }
wolfSSL 13:80fb167dafdf 297 return 0;
wolfSSL 13:80fb167dafdf 298 }
wolfSSL 13:80fb167dafdf 299
wolfSSL 13:80fb167dafdf 300
wolfSSL 13:80fb167dafdf 301 #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
wolfSSL 13:80fb167dafdf 302 /* peek at an error node
wolfSSL 13:80fb167dafdf 303 *
wolfSSL 13:80fb167dafdf 304 * idx : if -1 then the most recent node is looked at, otherwise search
wolfSSL 13:80fb167dafdf 305 * through queue for node at the given index
wolfSSL 13:80fb167dafdf 306 * file : pointer to internal file string
wolfSSL 13:80fb167dafdf 307 * reason : pointer to internal error reason
wolfSSL 13:80fb167dafdf 308 * line : line number that error happened at
wolfSSL 13:80fb167dafdf 309 *
wolfSSL 13:80fb167dafdf 310 * Returns a negative value in error case, on success returns the nodes error
wolfSSL 13:80fb167dafdf 311 * value which is positve (absolute value)
wolfSSL 13:80fb167dafdf 312 */
wolfSSL 13:80fb167dafdf 313 int wc_PeekErrorNode(int idx, const char **file, const char **reason,
wolfSSL 13:80fb167dafdf 314 int *line)
wolfSSL 13:80fb167dafdf 315 {
wolfSSL 13:80fb167dafdf 316 struct wc_error_queue* err;
wolfSSL 13:80fb167dafdf 317
wolfSSL 13:80fb167dafdf 318 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 319 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 320 return BAD_MUTEX_E;
wolfSSL 13:80fb167dafdf 321 }
wolfSSL 13:80fb167dafdf 322
wolfSSL 13:80fb167dafdf 323 if (idx < 0) {
wolfSSL 13:80fb167dafdf 324 err = wc_last_node;
wolfSSL 13:80fb167dafdf 325 if (err == NULL) {
wolfSSL 13:80fb167dafdf 326 WOLFSSL_MSG("No Errors in queue");
wolfSSL 13:80fb167dafdf 327 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 328 return BAD_STATE_E;
wolfSSL 13:80fb167dafdf 329 }
wolfSSL 13:80fb167dafdf 330 }
wolfSSL 13:80fb167dafdf 331 else {
wolfSSL 13:80fb167dafdf 332 int i;
wolfSSL 13:80fb167dafdf 333
wolfSSL 13:80fb167dafdf 334 err = (struct wc_error_queue*)wc_errors;
wolfSSL 13:80fb167dafdf 335 for (i = 0; i < idx; i++) {
wolfSSL 13:80fb167dafdf 336 if (err == NULL) {
wolfSSL 13:80fb167dafdf 337 WOLFSSL_MSG("Error node not found. Bad index?");
wolfSSL 13:80fb167dafdf 338 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 339 return BAD_FUNC_ARG;
wolfSSL 13:80fb167dafdf 340 }
wolfSSL 13:80fb167dafdf 341 err = err->next;
wolfSSL 13:80fb167dafdf 342 }
wolfSSL 13:80fb167dafdf 343 }
wolfSSL 13:80fb167dafdf 344
wolfSSL 13:80fb167dafdf 345 if (file != NULL) {
wolfSSL 13:80fb167dafdf 346 *file = err->file;
wolfSSL 13:80fb167dafdf 347 }
wolfSSL 13:80fb167dafdf 348
wolfSSL 13:80fb167dafdf 349 if (reason != NULL) {
wolfSSL 13:80fb167dafdf 350 *reason = err->error;
wolfSSL 13:80fb167dafdf 351 }
wolfSSL 13:80fb167dafdf 352
wolfSSL 13:80fb167dafdf 353 if (line != NULL) {
wolfSSL 13:80fb167dafdf 354 *line = err->line;
wolfSSL 13:80fb167dafdf 355 }
wolfSSL 13:80fb167dafdf 356
wolfSSL 13:80fb167dafdf 357 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 358
wolfSSL 13:80fb167dafdf 359 return err->value;
wolfSSL 13:80fb167dafdf 360 }
wolfSSL 13:80fb167dafdf 361
wolfSSL 13:80fb167dafdf 362
wolfSSL 13:80fb167dafdf 363 /* create new error node and add it to the queue
wolfSSL 13:80fb167dafdf 364 * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal
wolfSSL 13:80fb167dafdf 365 * function. debug_mutex should be locked before a call to this function. */
wolfSSL 13:80fb167dafdf 366 int wc_AddErrorNode(int error, int line, char* buf, char* file)
wolfSSL 13:80fb167dafdf 367 {
wolfSSL 13:80fb167dafdf 368
wolfSSL 13:80fb167dafdf 369 struct wc_error_queue* err;
wolfSSL 13:80fb167dafdf 370
wolfSSL 13:80fb167dafdf 371 err = (struct wc_error_queue*)XMALLOC(
wolfSSL 13:80fb167dafdf 372 sizeof(struct wc_error_queue), wc_error_heap, DYNAMIC_TYPE_LOG);
wolfSSL 13:80fb167dafdf 373 if (err == NULL) {
wolfSSL 13:80fb167dafdf 374 WOLFSSL_MSG("Unable to create error node for log");
wolfSSL 13:80fb167dafdf 375 return MEMORY_E;
wolfSSL 13:80fb167dafdf 376 }
wolfSSL 13:80fb167dafdf 377 else {
wolfSSL 13:80fb167dafdf 378 int sz;
wolfSSL 13:80fb167dafdf 379
wolfSSL 13:80fb167dafdf 380 XMEMSET(err, 0, sizeof(struct wc_error_queue));
wolfSSL 13:80fb167dafdf 381 err->heap = wc_error_heap;
wolfSSL 13:80fb167dafdf 382 sz = (int)XSTRLEN(buf);
wolfSSL 13:80fb167dafdf 383 if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
wolfSSL 13:80fb167dafdf 384 sz = WOLFSSL_MAX_ERROR_SZ - 1;
wolfSSL 13:80fb167dafdf 385 }
wolfSSL 13:80fb167dafdf 386 if (sz > 0) {
wolfSSL 13:80fb167dafdf 387 XMEMCPY(err->error, buf, sz);
wolfSSL 13:80fb167dafdf 388 }
wolfSSL 13:80fb167dafdf 389
wolfSSL 13:80fb167dafdf 390 sz = (int)XSTRLEN(file);
wolfSSL 13:80fb167dafdf 391 if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
wolfSSL 13:80fb167dafdf 392 sz = WOLFSSL_MAX_ERROR_SZ - 1;
wolfSSL 13:80fb167dafdf 393 }
wolfSSL 13:80fb167dafdf 394 if (sz > 0) {
wolfSSL 13:80fb167dafdf 395 XMEMCPY(err->file, file, sz);
wolfSSL 13:80fb167dafdf 396 }
wolfSSL 13:80fb167dafdf 397
wolfSSL 13:80fb167dafdf 398 err->value = error;
wolfSSL 13:80fb167dafdf 399 err->line = line;
wolfSSL 13:80fb167dafdf 400
wolfSSL 13:80fb167dafdf 401 /* make sure is terminated */
wolfSSL 13:80fb167dafdf 402 err->error[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
wolfSSL 13:80fb167dafdf 403 err->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
wolfSSL 13:80fb167dafdf 404
wolfSSL 13:80fb167dafdf 405
wolfSSL 13:80fb167dafdf 406 /* since is queue place new node at last of the list */
wolfSSL 13:80fb167dafdf 407 if (wc_last_node == NULL) {
wolfSSL 13:80fb167dafdf 408 /* case of first node added to queue */
wolfSSL 13:80fb167dafdf 409 if (wc_errors != NULL) {
wolfSSL 13:80fb167dafdf 410 /* check for unexpected case before over writing wc_errors */
wolfSSL 13:80fb167dafdf 411 WOLFSSL_MSG("ERROR in adding new node to logging queue!!\n");
wolfSSL 13:80fb167dafdf 412 }
wolfSSL 13:80fb167dafdf 413 else {
wolfSSL 13:80fb167dafdf 414 wc_errors = err;
wolfSSL 13:80fb167dafdf 415 wc_last_node = err;
wolfSSL 13:80fb167dafdf 416 }
wolfSSL 13:80fb167dafdf 417 }
wolfSSL 13:80fb167dafdf 418 else {
wolfSSL 13:80fb167dafdf 419 wc_last_node->next = err;
wolfSSL 13:80fb167dafdf 420 err->prev = wc_last_node;
wolfSSL 13:80fb167dafdf 421 wc_last_node = err;
wolfSSL 13:80fb167dafdf 422 }
wolfSSL 13:80fb167dafdf 423 }
wolfSSL 13:80fb167dafdf 424
wolfSSL 13:80fb167dafdf 425 return 0;
wolfSSL 13:80fb167dafdf 426 }
wolfSSL 13:80fb167dafdf 427
wolfSSL 13:80fb167dafdf 428 /* Removes the error node at the specified index.
wolfSSL 13:80fb167dafdf 429 * idx : if -1 then the most recent node is looked at, otherwise search
wolfSSL 13:80fb167dafdf 430 * through queue for node at the given index
wolfSSL 13:80fb167dafdf 431 */
wolfSSL 13:80fb167dafdf 432 void wc_RemoveErrorNode(int idx)
wolfSSL 13:80fb167dafdf 433 {
wolfSSL 13:80fb167dafdf 434 struct wc_error_queue* current;
wolfSSL 13:80fb167dafdf 435
wolfSSL 13:80fb167dafdf 436 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 437 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 438 return;
wolfSSL 13:80fb167dafdf 439 }
wolfSSL 13:80fb167dafdf 440
wolfSSL 13:80fb167dafdf 441 if (idx == -1)
wolfSSL 13:80fb167dafdf 442 current = wc_last_node;
wolfSSL 13:80fb167dafdf 443 else {
wolfSSL 13:80fb167dafdf 444 current = (struct wc_error_queue*)wc_errors;
wolfSSL 13:80fb167dafdf 445 for (; current != NULL && idx > 0; idx--)
wolfSSL 13:80fb167dafdf 446 current = current->next;
wolfSSL 13:80fb167dafdf 447 }
wolfSSL 13:80fb167dafdf 448 if (current != NULL) {
wolfSSL 13:80fb167dafdf 449 if (current->prev != NULL)
wolfSSL 13:80fb167dafdf 450 current->prev->next = current->next;
wolfSSL 13:80fb167dafdf 451 if (wc_last_node == current)
wolfSSL 13:80fb167dafdf 452 wc_last_node = current->prev;
wolfSSL 13:80fb167dafdf 453 if (wc_errors == current)
wolfSSL 13:80fb167dafdf 454 wc_errors = current->next;
wolfSSL 13:80fb167dafdf 455 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
wolfSSL 13:80fb167dafdf 456 }
wolfSSL 13:80fb167dafdf 457
wolfSSL 13:80fb167dafdf 458 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 459 }
wolfSSL 13:80fb167dafdf 460
wolfSSL 13:80fb167dafdf 461 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */
wolfSSL 13:80fb167dafdf 462
wolfSSL 13:80fb167dafdf 463 /* Clears out the list of error nodes.
wolfSSL 13:80fb167dafdf 464 */
wolfSSL 13:80fb167dafdf 465 void wc_ClearErrorNodes(void)
wolfSSL 13:80fb167dafdf 466 {
wolfSSL 13:80fb167dafdf 467 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 468 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 469 return;
wolfSSL 13:80fb167dafdf 470 }
wolfSSL 13:80fb167dafdf 471
wolfSSL 13:80fb167dafdf 472 /* free all nodes from error queue */
wolfSSL 13:80fb167dafdf 473 {
wolfSSL 13:80fb167dafdf 474 struct wc_error_queue* current;
wolfSSL 13:80fb167dafdf 475 struct wc_error_queue* next;
wolfSSL 13:80fb167dafdf 476
wolfSSL 13:80fb167dafdf 477 current = (struct wc_error_queue*)wc_errors;
wolfSSL 13:80fb167dafdf 478 while (current != NULL) {
wolfSSL 13:80fb167dafdf 479 next = current->next;
wolfSSL 13:80fb167dafdf 480 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
wolfSSL 13:80fb167dafdf 481 current = next;
wolfSSL 13:80fb167dafdf 482 }
wolfSSL 13:80fb167dafdf 483 }
wolfSSL 13:80fb167dafdf 484
wolfSSL 13:80fb167dafdf 485 wc_errors = NULL;
wolfSSL 13:80fb167dafdf 486 wc_last_node = NULL;
wolfSSL 13:80fb167dafdf 487 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 488 }
wolfSSL 13:80fb167dafdf 489
wolfSSL 13:80fb167dafdf 490 int wc_SetLoggingHeap(void* h)
wolfSSL 13:80fb167dafdf 491 {
wolfSSL 13:80fb167dafdf 492 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 493 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 494 return BAD_MUTEX_E;
wolfSSL 13:80fb167dafdf 495 }
wolfSSL 13:80fb167dafdf 496 wc_error_heap = h;
wolfSSL 13:80fb167dafdf 497 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 498 return 0;
wolfSSL 13:80fb167dafdf 499 }
wolfSSL 13:80fb167dafdf 500
wolfSSL 13:80fb167dafdf 501 #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
wolfSSL 13:80fb167dafdf 502 /* empties out the error queue into the file */
wolfSSL 13:80fb167dafdf 503 void wc_ERR_print_errors_fp(FILE* fp)
wolfSSL 13:80fb167dafdf 504 {
wolfSSL 13:80fb167dafdf 505 WOLFSSL_ENTER("wc_ERR_print_errors_fp");
wolfSSL 13:80fb167dafdf 506
wolfSSL 13:80fb167dafdf 507 if (wc_LockMutex(&debug_mutex) != 0) {
wolfSSL 13:80fb167dafdf 508 WOLFSSL_MSG("Lock debug mutex failed");
wolfSSL 13:80fb167dafdf 509 }
wolfSSL 13:80fb167dafdf 510 else {
wolfSSL 13:80fb167dafdf 511 /* free all nodes from error queue and print them to file */
wolfSSL 13:80fb167dafdf 512 {
wolfSSL 13:80fb167dafdf 513 struct wc_error_queue* current;
wolfSSL 13:80fb167dafdf 514 struct wc_error_queue* next;
wolfSSL 13:80fb167dafdf 515
wolfSSL 13:80fb167dafdf 516 current = (struct wc_error_queue*)wc_errors;
wolfSSL 13:80fb167dafdf 517 while (current != NULL) {
wolfSSL 13:80fb167dafdf 518 next = current->next;
wolfSSL 13:80fb167dafdf 519 fprintf(fp, "%s\n", current->error);
wolfSSL 13:80fb167dafdf 520 XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
wolfSSL 13:80fb167dafdf 521 current = next;
wolfSSL 13:80fb167dafdf 522 }
wolfSSL 13:80fb167dafdf 523
wolfSSL 13:80fb167dafdf 524 /* set global pointers to match having been freed */
wolfSSL 13:80fb167dafdf 525 wc_errors = NULL;
wolfSSL 13:80fb167dafdf 526 wc_last_node = NULL;
wolfSSL 13:80fb167dafdf 527 }
wolfSSL 13:80fb167dafdf 528
wolfSSL 13:80fb167dafdf 529 wc_UnLockMutex(&debug_mutex);
wolfSSL 13:80fb167dafdf 530 }
wolfSSL 13:80fb167dafdf 531 }
wolfSSL 13:80fb167dafdf 532 #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
wolfSSL 13:80fb167dafdf 533
wolfSSL 13:80fb167dafdf 534 #endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */
wolfSSL 13:80fb167dafdf 535
wolfSSL 13:80fb167dafdf 536