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