logging.c 24 KB

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