wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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