logging.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /* logging.c
  2. *
  3. * Copyright (C) 2006-2023 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. #ifdef WOLFSSL_FUNC_TIME
  32. /* WARNING: This code is only to be used for debugging performance.
  33. * The code is not thread-safe.
  34. * Do not use WOLFSSL_FUNC_TIME in production code.
  35. */
  36. static double wc_func_start[WC_FUNC_COUNT];
  37. static double wc_func_time[WC_FUNC_COUNT] = { 0, };
  38. static const char* wc_func_name[WC_FUNC_COUNT] = {
  39. "SendHelloRequest",
  40. "DoHelloRequest",
  41. "SendClientHello",
  42. "DoClientHello",
  43. "SendServerHello",
  44. "DoServerHello",
  45. "SendEncryptedExtensions",
  46. "DoEncryptedExtensions",
  47. "SendCertificateRequest",
  48. "DoCertificateRequest",
  49. "SendCertificate",
  50. "DoCertificate",
  51. "SendCertificateVerify",
  52. "DoCertificateVerify",
  53. "SendFinished",
  54. "DoFinished",
  55. "SendKeyUpdate",
  56. "DoKeyUpdate",
  57. "SendEarlyData",
  58. "DoEarlyData",
  59. "SendNewSessionTicket",
  60. "DoNewSessionTicket",
  61. "SendServerHelloDone",
  62. "DoServerHelloDone",
  63. "SendTicket",
  64. "DoTicket",
  65. "SendClientKeyExchange",
  66. "DoClientKeyExchange",
  67. "SendCertificateStatus",
  68. "DoCertificateStatus",
  69. "SendServerKeyExchange",
  70. "DoServerKeyExchange",
  71. "SendEarlyData",
  72. "DoEarlyData",
  73. };
  74. #include <sys/time.h>
  75. /* WARNING: This function is not portable. */
  76. static WC_INLINE double current_time(int reset)
  77. {
  78. struct timeval tv;
  79. gettimeofday(&tv, 0);
  80. (void)reset;
  81. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  82. }
  83. #endif /* WOLFSSL_FUNC_TIME */
  84. #ifdef HAVE_WC_INTROSPECTION
  85. const char *wolfSSL_configure_args(void) {
  86. #ifdef LIBWOLFSSL_CONFIGURE_ARGS
  87. /* the spaces on either side are to make matching simple and efficient. */
  88. return " " LIBWOLFSSL_CONFIGURE_ARGS " ";
  89. #else
  90. return NULL;
  91. #endif
  92. }
  93. PEDANTIC_EXTENSION const char *wolfSSL_global_cflags(void) {
  94. #ifdef LIBWOLFSSL_GLOBAL_CFLAGS
  95. /* the spaces on either side are to make matching simple and efficient. */
  96. return " " LIBWOLFSSL_GLOBAL_CFLAGS " ";
  97. #else
  98. return NULL;
  99. #endif
  100. }
  101. #endif /* HAVE_WC_INTROSPECTION */
  102. #ifdef HAVE_STACK_SIZE_VERBOSE
  103. THREAD_LS_T unsigned char *StackSizeCheck_myStack = NULL;
  104. THREAD_LS_T size_t StackSizeCheck_stackSize = 0;
  105. THREAD_LS_T size_t StackSizeCheck_stackSizeHWM = 0;
  106. THREAD_LS_T size_t *StackSizeCheck_stackSizeHWM_ptr = 0;
  107. THREAD_LS_T void *StackSizeCheck_stackOffsetPointer = 0;
  108. #endif /* HAVE_STACK_SIZE_VERBOSE */
  109. #ifdef DEBUG_WOLFSSL
  110. /* Set these to default values initially. */
  111. static wolfSSL_Logging_cb log_function = NULL;
  112. static int loggingEnabled = 0;
  113. THREAD_LS_T const char* log_prefix = NULL;
  114. #if defined(WOLFSSL_APACHE_MYNEWT)
  115. #include "log/log.h"
  116. static struct log mynewt_log;
  117. #endif /* WOLFSSL_APACHE_MYNEWT */
  118. #endif /* DEBUG_WOLFSSL */
  119. #ifdef DEBUG_VECTOR_REGISTER_ACCESS
  120. THREAD_LS_T int wc_svr_count = 0;
  121. THREAD_LS_T const char *wc_svr_last_file = NULL;
  122. THREAD_LS_T int wc_svr_last_line = -1;
  123. #endif
  124. /* allow this to be set to NULL, so logs can be redirected to default output */
  125. int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
  126. {
  127. #ifdef DEBUG_WOLFSSL
  128. log_function = f;
  129. return 0;
  130. #else
  131. (void)f;
  132. return NOT_COMPILED_IN;
  133. #endif
  134. }
  135. /* allow this to be set to NULL, so logs can be redirected to default output */
  136. wolfSSL_Logging_cb wolfSSL_GetLoggingCb(void)
  137. {
  138. #ifdef DEBUG_WOLFSSL
  139. return log_function;
  140. #else
  141. return NULL;
  142. #endif
  143. }
  144. int wolfSSL_Debugging_ON(void)
  145. {
  146. #ifdef DEBUG_WOLFSSL
  147. loggingEnabled = 1;
  148. #if defined(WOLFSSL_APACHE_MYNEWT)
  149. log_register("wolfcrypt", &mynewt_log, &log_console_handler, NULL, LOG_SYSLEVEL);
  150. #endif /* WOLFSSL_APACHE_MYNEWT */
  151. return 0;
  152. #else
  153. return NOT_COMPILED_IN;
  154. #endif
  155. }
  156. void wolfSSL_Debugging_OFF(void)
  157. {
  158. #ifdef DEBUG_WOLFSSL
  159. loggingEnabled = 0;
  160. #endif
  161. }
  162. WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix)
  163. {
  164. #ifdef DEBUG_WOLFSSL
  165. log_prefix = prefix;
  166. #else
  167. (void)prefix;
  168. #endif
  169. }
  170. #ifdef WOLFSSL_FUNC_TIME
  171. /* WARNING: This code is only to be used for debugging performance.
  172. * The code is not thread-safe.
  173. * Do not use WOLFSSL_FUNC_TIME in production code.
  174. */
  175. void WOLFSSL_START(int funcNum)
  176. {
  177. if (funcNum < WC_FUNC_COUNT) {
  178. double now = current_time(0) * 1000.0;
  179. #ifdef WOLFSSL_FUNC_TIME_LOG
  180. fprintf(stderr, "%17.3f: START - %s\n", now, wc_func_name[funcNum]);
  181. #endif
  182. wc_func_start[funcNum] = now;
  183. }
  184. }
  185. void WOLFSSL_END(int funcNum)
  186. {
  187. if (funcNum < WC_FUNC_COUNT) {
  188. double now = current_time(0) * 1000.0;
  189. wc_func_time[funcNum] += now - wc_func_start[funcNum];
  190. #ifdef WOLFSSL_FUNC_TIME_LOG
  191. fprintf(stderr, "%17.3f: END - %s\n", now, wc_func_name[funcNum]);
  192. #endif
  193. }
  194. }
  195. void WOLFSSL_TIME(int count)
  196. {
  197. int i;
  198. double avg, total = 0;
  199. for (i = 0; i < WC_FUNC_COUNT; i++) {
  200. if (wc_func_time[i] > 0) {
  201. avg = wc_func_time[i] / count;
  202. fprintf(stderr, "%8.3f ms: %s\n", avg, wc_func_name[i]);
  203. total += avg;
  204. }
  205. }
  206. fprintf(stderr, "%8.3f ms\n", total);
  207. }
  208. #endif
  209. #ifdef DEBUG_WOLFSSL
  210. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  211. /* see wc_port.h for fio.h and nio.h includes */
  212. #elif defined(WOLFSSL_SGX)
  213. /* Declare sprintf for ocall */
  214. int sprintf(char* buf, const char *fmt, ...);
  215. #elif defined(WOLFSSL_DEOS)
  216. #elif defined(MICRIUM)
  217. #if (BSP_SER_COMM_EN == DEF_ENABLED)
  218. #include <bsp_ser.h>
  219. #endif
  220. #elif defined(WOLFSSL_USER_LOG)
  221. /* user includes their own headers */
  222. #elif defined(WOLFSSL_ESPIDF)
  223. #include "esp_types.h"
  224. #include "esp_log.h"
  225. #elif defined(WOLFSSL_TELIT_M2MB)
  226. #include <stdio.h>
  227. #include "m2m_log.h"
  228. #elif defined(WOLFSSL_ANDROID_DEBUG)
  229. #include <android/log.h>
  230. #elif defined(WOLFSSL_XILINX)
  231. #include "xil_printf.h"
  232. #elif defined(WOLFSSL_LINUXKM)
  233. /* the requisite linux/kernel.h is included in wc_port.h, with incompatible warnings masked out. */
  234. #elif defined(FUSION_RTOS)
  235. #include <fclstdio.h>
  236. #include <wolfssl/wolfcrypt/wc_port.h>
  237. #define fprintf FCL_FPRINTF
  238. #else
  239. #include <stdio.h> /* for default printf stuff */
  240. #endif
  241. #if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
  242. int dc_log_printf(char*, ...);
  243. #endif
  244. #ifdef HAVE_STACK_SIZE_VERBOSE
  245. #include <wolfssl/wolfcrypt/mem_track.h>
  246. #endif
  247. static void wolfssl_log(const int logLevel, const char *const logMessage)
  248. {
  249. if (log_function)
  250. log_function(logLevel, logMessage);
  251. else {
  252. #if defined(WOLFSSL_USER_LOG)
  253. WOLFSSL_USER_LOG(logMessage);
  254. #elif defined(WOLFSSL_LOG_PRINTF)
  255. printf("%s\n", logMessage);
  256. #elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
  257. dc_log_printf("%s\n", logMessage);
  258. #elif defined(WOLFSSL_DEOS)
  259. printf("%s\r\n", logMessage);
  260. #elif defined(MICRIUM)
  261. BSP_Ser_Printf("%s\r\n", logMessage);
  262. #elif defined(WOLFSSL_MDK_ARM)
  263. fflush(stdout) ;
  264. printf("%s\n", logMessage);
  265. fflush(stdout) ;
  266. #elif defined(WOLFSSL_UTASKER)
  267. fnDebugMsg((char*)logMessage);
  268. fnDebugMsg("\r\n");
  269. #elif defined(MQX_USE_IO_OLD)
  270. fprintf(_mqxio_stderr, "%s\n", logMessage);
  271. #elif defined(WOLFSSL_APACHE_MYNEWT)
  272. LOG_DEBUG(&mynewt_log, LOG_MODULE_DEFAULT, "%s\n", logMessage);
  273. #elif defined(WOLFSSL_ESPIDF)
  274. ESP_LOGI("wolfssl", "%s", logMessage);
  275. #elif defined(WOLFSSL_ZEPHYR)
  276. printk("%s\n", logMessage);
  277. #elif defined(WOLFSSL_TELIT_M2MB)
  278. M2M_LOG_INFO("%s\n", logMessage);
  279. #elif defined(WOLFSSL_ANDROID_DEBUG)
  280. __android_log_print(ANDROID_LOG_VERBOSE, "[wolfSSL]", "%s", logMessage);
  281. #elif defined(WOLFSSL_XILINX)
  282. xil_printf("%s\r\n", logMessage);
  283. #elif defined(WOLFSSL_LINUXKM)
  284. printk("%s\n", logMessage);
  285. #elif defined(WOLFSSL_RENESAS_RA6M4)
  286. myprintf("%s\n", logMessage);
  287. #elif defined(STACK_SIZE_CHECKPOINT_MSG) && \
  288. defined(HAVE_STACK_SIZE_VERBOSE) && defined(HAVE_STACK_SIZE_VERBOSE_LOG)
  289. STACK_SIZE_CHECKPOINT_MSG(logMessage);
  290. #else
  291. if (log_prefix != NULL)
  292. fprintf(stderr, "[%s]: %s\n", log_prefix, logMessage);
  293. else
  294. fprintf(stderr, "%s\n", logMessage);
  295. #endif
  296. }
  297. }
  298. #ifndef WOLFSSL_DEBUG_ERRORS_ONLY
  299. #if defined(XVSNPRINTF) && !defined(NO_WOLFSSL_MSG_EX)
  300. #include <stdarg.h> /* for var args */
  301. #ifndef WOLFSSL_MSG_EX_BUF_SZ
  302. #define WOLFSSL_MSG_EX_BUF_SZ 100
  303. #endif
  304. #ifdef __clang__
  305. /* tell clang argument 1 is format */
  306. __attribute__((__format__ (__printf__, 1, 0)))
  307. #endif
  308. void WOLFSSL_MSG_EX(const char* fmt, ...)
  309. {
  310. if (loggingEnabled) {
  311. char msg[WOLFSSL_MSG_EX_BUF_SZ];
  312. int written;
  313. va_list args;
  314. va_start(args, fmt);
  315. written = XVSNPRINTF(msg, sizeof(msg), fmt, args);
  316. va_end(args);
  317. if (written > 0)
  318. wolfssl_log(INFO_LOG , msg);
  319. }
  320. }
  321. #endif
  322. void WOLFSSL_MSG(const char* msg)
  323. {
  324. if (loggingEnabled)
  325. wolfssl_log(INFO_LOG , msg);
  326. }
  327. #ifndef LINE_LEN
  328. #define LINE_LEN 16
  329. #endif
  330. void WOLFSSL_BUFFER(const byte* buffer, word32 length)
  331. {
  332. int i, buflen = (int)length;
  333. char line[(LINE_LEN * 4) + 3]; /* \t00..0F | chars...chars\0 */
  334. if (!loggingEnabled) {
  335. return;
  336. }
  337. if (!buffer) {
  338. wolfssl_log(INFO_LOG, "\tNULL");
  339. return;
  340. }
  341. while (buflen > 0) {
  342. int bufidx = 0;
  343. XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "\t");
  344. bufidx++;
  345. for (i = 0; i < LINE_LEN; i++) {
  346. if (i < buflen) {
  347. XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "%02x ", buffer[i]);
  348. }
  349. else {
  350. XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, " ");
  351. }
  352. bufidx += 3;
  353. }
  354. XSNPRINTF(&line[bufidx], sizeof(line)-bufidx, "| ");
  355. bufidx++;
  356. for (i = 0; i < LINE_LEN; i++) {
  357. if (i < buflen) {
  358. XSNPRINTF(&line[bufidx], sizeof(line)-bufidx,
  359. "%c", 31 < buffer[i] && buffer[i] < 127 ? buffer[i] : '.');
  360. bufidx++;
  361. }
  362. }
  363. wolfssl_log(INFO_LOG, line);
  364. buffer += LINE_LEN;
  365. buflen -= LINE_LEN;
  366. }
  367. }
  368. void WOLFSSL_ENTER(const char* msg)
  369. {
  370. if (loggingEnabled) {
  371. char buffer[WOLFSSL_MAX_ERROR_SZ];
  372. XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg);
  373. wolfssl_log(ENTER_LOG , buffer);
  374. }
  375. }
  376. void WOLFSSL_LEAVE(const char* msg, int ret)
  377. {
  378. if (loggingEnabled) {
  379. char buffer[WOLFSSL_MAX_ERROR_SZ];
  380. XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
  381. msg, ret);
  382. wolfssl_log(LEAVE_LOG , buffer);
  383. }
  384. }
  385. WOLFSSL_API int WOLFSSL_IS_DEBUG_ON(void)
  386. {
  387. return loggingEnabled;
  388. }
  389. #endif /* !WOLFSSL_DEBUG_ERRORS_ONLY */
  390. #endif /* DEBUG_WOLFSSL */
  391. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  392. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  393. #ifdef ERROR_QUEUE_PER_THREAD
  394. /* Keep the error queue in thread-local-storage. The only ways this
  395. * API can deliver meaningful semantics in a multi-threaded setup.
  396. */
  397. #ifndef ERROR_QUEUE_MAX
  398. /* Same as OpenSSL v1.1.x limit, note that this is per thread */
  399. #define ERROR_QUEUE_MAX 16
  400. #endif
  401. struct wc_error_entry {
  402. char reason[WOLFSSL_MAX_ERROR_SZ];
  403. char file[WOLFSSL_MAX_ERROR_SZ];
  404. int line;
  405. int err;
  406. };
  407. struct wc_error_queue {
  408. struct wc_error_entry entries[ERROR_QUEUE_MAX];
  409. size_t head_idx;
  410. size_t count;
  411. };
  412. /* The complete queue in a thread local without allocations */
  413. static THREAD_LS_T struct wc_error_queue wc_errors;
  414. /* Using thread-local-storage, we do not need a mutex. */
  415. #define ERRQ_LOCK() 0
  416. #define ERRQ_UNLOCK() (void)0
  417. /**
  418. * Given a relative index (from head of the error list), return
  419. * the absolute index in the `wc_errors->entries` array for
  420. * the entry or -1 if no such entry exists/is present.
  421. */
  422. static int get_abs_idx(int relative_idx)
  423. {
  424. if ((wc_errors.count == 0) || (relative_idx >= (int)wc_errors.count)) {
  425. return -1;
  426. }
  427. if (relative_idx < 0) {
  428. return (int)((wc_errors.head_idx + wc_errors.count - 1)
  429. % ERROR_QUEUE_MAX);
  430. }
  431. return (int)((wc_errors.head_idx + (size_t)relative_idx) % ERROR_QUEUE_MAX);
  432. }
  433. /**
  434. * Return the error entry at the given relative index, if
  435. * it exists, e.g. `relative_idx` is in a valid range.
  436. */
  437. static struct wc_error_entry *get_entry(int relative_idx)
  438. {
  439. int abs_idx;
  440. abs_idx = get_abs_idx(relative_idx);
  441. if (abs_idx < 0) {
  442. return NULL;
  443. }
  444. return &wc_errors.entries[abs_idx];
  445. }
  446. /**
  447. * Return the error code in the given error `entry` and populate
  448. * `file`, `reason` and `line` with its values.
  449. * `entry` may be NULL, in which case BAD_STATE_E is returned.
  450. */
  451. static int pass_entry(struct wc_error_entry *entry,
  452. const char **file, const char **reason,
  453. int *line)
  454. {
  455. if (entry == NULL) {
  456. WOLFSSL_MSG("No Error found in queue");
  457. return BAD_STATE_E;
  458. }
  459. if (file != NULL) {
  460. *file = entry->file;
  461. }
  462. if (reason != NULL) {
  463. *reason = entry->reason;
  464. }
  465. if (line != NULL) {
  466. *line = entry->line;
  467. }
  468. return entry->err;
  469. }
  470. /**
  471. * Assign entry with values, resets all previously present values.
  472. */
  473. static void set_entry(struct wc_error_entry *entry, int error,
  474. const char *file, const char *reason, int line)
  475. {
  476. size_t sz;
  477. XMEMSET(entry, 0, sizeof(struct wc_error_entry));
  478. entry->err = error;
  479. entry->line = line;
  480. sz = XSTRLEN(reason);
  481. if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
  482. sz = WOLFSSL_MAX_ERROR_SZ - 1;
  483. }
  484. if (sz > 0) {
  485. XMEMCPY(entry->reason, reason, sz);
  486. entry->reason[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
  487. }
  488. sz = XSTRLEN(file);
  489. if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
  490. sz = WOLFSSL_MAX_ERROR_SZ - 1;
  491. }
  492. if (sz > 0) {
  493. XMEMCPY(entry->file, file, sz);
  494. entry->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
  495. }
  496. }
  497. /* Internal function that is called by wolfCrypt_Init() */
  498. int wc_LoggingInit(void)
  499. {
  500. return 0;
  501. }
  502. /* internal function that is called by wolfCrypt_Cleanup */
  503. int wc_LoggingCleanup(void)
  504. {
  505. /* clear logging entries */
  506. wc_ClearErrorNodes();
  507. return 0;
  508. }
  509. /**
  510. * Get the values from the HEAD of the ERR queue, but keep it in place.
  511. * If the queue is empty, return BAD_STATE_E.
  512. */
  513. int wc_PeekErrorNode(int idx, const char **file, const char **reason,
  514. int *line)
  515. {
  516. return pass_entry(get_entry(idx), file, reason, line);
  517. }
  518. /**
  519. * Get the values from the HEAD of the ERR queue and remove it.
  520. * If the queue is empty, return BAD_STATE_E.
  521. */
  522. int wc_PullErrorNode(const char **file, const char **reason, int *line)
  523. {
  524. struct wc_error_entry *entry;
  525. int ret;
  526. entry = get_entry(0);
  527. ret = pass_entry(entry, file, reason, line);
  528. if (entry != NULL) {
  529. wc_RemoveErrorNode(0);
  530. }
  531. return ret;
  532. }
  533. /* create new error node and add it to the queue
  534. * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal
  535. * function. */
  536. int wc_AddErrorNode(int error, int line, char* reason, char* file)
  537. {
  538. struct wc_error_entry *entry;
  539. size_t idx;
  540. if (wc_errors.count >= ERROR_QUEUE_MAX) {
  541. WOLFSSL_MSG("Error queue is full, at ERROR_QUEUE_MAX");
  542. return MEMORY_E;
  543. }
  544. idx = (wc_errors.head_idx + wc_errors.count) % ERROR_QUEUE_MAX;
  545. entry = &wc_errors.entries[idx];
  546. set_entry(entry, error, file, reason, line);
  547. ++wc_errors.count;
  548. return 0;
  549. }
  550. /**
  551. * Remove the entry at relative position `relative_idx` from the ERR queue.
  552. * For `relative_idx == 0` it removes the queue's head entry, for -1
  553. * it removes the last entry in the queue.
  554. */
  555. void wc_RemoveErrorNode(int relative_idx)
  556. {
  557. int abs_idx = get_abs_idx(relative_idx);
  558. if (abs_idx >= 0) {
  559. size_t move_count;
  560. if (abs_idx >= (int)wc_errors.head_idx) {
  561. /* removed entry sits "above" head (or is head),
  562. * move entries below it "up" */
  563. move_count = (size_t)abs_idx - wc_errors.head_idx;
  564. if (move_count > 0) {
  565. XMEMMOVE(&wc_errors.entries[wc_errors.head_idx + 1],
  566. &wc_errors.entries[wc_errors.head_idx],
  567. sizeof(wc_errors.entries[0]) * move_count);
  568. }
  569. wc_errors.head_idx = (wc_errors.head_idx + 1) % ERROR_QUEUE_MAX;
  570. --wc_errors.count;
  571. }
  572. else {
  573. /* removed entry sits "below" head (wrap around),
  574. * move entries above it "down" */
  575. int last_idx = get_abs_idx(-1);
  576. if (last_idx >= abs_idx) { /* this SHOULD always be true */
  577. move_count = (size_t)(last_idx - abs_idx);
  578. if (move_count > 0) {
  579. XMEMMOVE(&wc_errors.entries[abs_idx],
  580. &wc_errors.entries[abs_idx + 1],
  581. sizeof(wc_errors.entries[0]) * move_count);
  582. }
  583. --wc_errors.count;
  584. }
  585. }
  586. }
  587. }
  588. /**
  589. * Clear the ERR queue.
  590. */
  591. void wc_ClearErrorNodes(void)
  592. {
  593. if (wc_errors.count > 0) {
  594. XMEMSET(&wc_errors, 0, sizeof(wc_errors));
  595. }
  596. }
  597. int wc_SetLoggingHeap(void* h)
  598. {
  599. (void)h;
  600. return 0;
  601. }
  602. int wc_ERR_remove_state(void)
  603. {
  604. wc_ClearErrorNodes();
  605. return 0;
  606. }
  607. /**
  608. * Get the first entry's values in the ERR queue that is not filtered
  609. * by the provided `ignore_err` callback. All ignored entries are removed,
  610. * making the returned entry the head of the ERR queue afterwards.
  611. *
  612. * In case all entries are ignored, the ERR queue will be empty afterwards.
  613. * For an empty ERR queue 0 is returned.
  614. *
  615. * `ignore_err` may be NULL, in which case this returns the HEAD values.
  616. *
  617. * `flags` is present for OpenSSL compatibility, but will always be
  618. * set to 0, since we do not keep flags at ERR entries.
  619. */
  620. unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
  621. const char **data, int *flags,
  622. int (*ignore_err)(int err))
  623. {
  624. WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
  625. /* No data or flags stored - error display only in Nginx. */
  626. if (data != NULL) {
  627. *data = "";
  628. }
  629. if (flags != NULL) {
  630. *flags = 0;
  631. }
  632. while (1) {
  633. int ret = wc_PeekErrorNode(0, file, NULL, line);
  634. if (ret == BAD_STATE_E) {
  635. WOLFSSL_MSG("Issue peeking at error node in queue");
  636. return 0;
  637. }
  638. /* OpenSSL uses positive error codes */
  639. if (ret < 0) {
  640. ret = -ret;
  641. }
  642. /* an error that the caller wants to ignore? */
  643. if (ignore_err && ignore_err(ret)) {
  644. wc_RemoveErrorNode(0);
  645. continue;
  646. }
  647. return (unsigned long)ret;
  648. }
  649. }
  650. /**
  651. * Get the error value at the HEAD of the ERR queue or 0 if the queue
  652. * is empty. The HEAD entry is removed by this call.
  653. */
  654. unsigned long wc_GetErrorNodeErr(void)
  655. {
  656. int ret;
  657. WOLFSSL_ENTER("wc_GetErrorNodeErr");
  658. ret = wc_PullErrorNode(NULL, NULL, NULL);
  659. if (ret < 0) {
  660. if (ret == BAD_STATE_E) {
  661. ret = 0; /* no errors in queue */
  662. }
  663. else {
  664. WOLFSSL_MSG("Error with pulling error node!");
  665. WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
  666. ret = 0 - ret; /* return absolute value of error */
  667. /* panic and try to clear out nodes */
  668. wc_ClearErrorNodes();
  669. }
  670. }
  671. return (unsigned long)ret;
  672. }
  673. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  674. /* This callback allows the application to provide a custom error printing
  675. * function. */
  676. void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
  677. void *u)
  678. {
  679. size_t i;
  680. WOLFSSL_ENTER("wc_ERR_print_errors_cb");
  681. if (cb == NULL) {
  682. /* Invalid param */
  683. return;
  684. }
  685. for (i = 0; i < wc_errors.count; ++i) {
  686. struct wc_error_entry *entry = get_entry((int)i);
  687. if (entry == NULL)
  688. break;
  689. cb(entry->reason, XSTRLEN(entry->reason), u);
  690. }
  691. wc_ClearErrorNodes();
  692. }
  693. #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
  694. #else /* ERROR_QUEUE_PER_THREAD */
  695. /* Error queue is a global list. This is the original implementation and
  696. * the fallback on platforms that do not have thread-local-storage.
  697. *
  698. * Access and manipulations of the list are protected by a mutex, however
  699. * that does not prevent errors from another thread showing up. Therefore,
  700. * its usefulness is limited to applications with restricted thread
  701. * concurrency in using wolfSSL.
  702. */
  703. #ifndef ERROR_QUEUE_MAX
  704. /* With a global list, we allow a higher limit. */
  705. #define ERROR_QUEUE_MAX 100
  706. #endif
  707. /* The information we keep about a single error */
  708. struct wc_error_queue {
  709. void* heap; /* the heap hint used with nodes creation */
  710. struct wc_error_queue* next;
  711. struct wc_error_queue* prev;
  712. char error[WOLFSSL_MAX_ERROR_SZ];
  713. char file[WOLFSSL_MAX_ERROR_SZ];
  714. int value;
  715. int line;
  716. };
  717. /* The global list of errors encountered */
  718. static struct wc_error_queue* wc_errors;
  719. static int wc_errors_count = 0;
  720. /* pointer to last node in queue to make insertion O(1) */
  721. static struct wc_error_queue* wc_last_node;
  722. /* The 'current' cursor the application is using to access the list */
  723. static struct wc_error_queue* wc_current_node;
  724. /* heap info currently used for allocation of entries */
  725. static void* wc_error_heap;
  726. /* mutex for list operation protection */
  727. static wolfSSL_Mutex wc_error_mutex;
  728. #define ERRQ_MUTEX_INIT() wc_InitMutex(&wc_error_mutex)
  729. #define ERRQ_MUTEX_FREE() wc_FreeMutex(&wc_error_mutex)
  730. #define ERRQ_LOCK() wc_LockMutex(&wc_error_mutex)
  731. #define ERRQ_UNLOCK() wc_UnLockMutex(&wc_error_mutex)
  732. /* Internal function that is called by wolfCrypt_Init() */
  733. int wc_LoggingInit(void)
  734. {
  735. if (ERRQ_MUTEX_INIT() != 0) {
  736. WOLFSSL_MSG("Bad Init Mutex");
  737. return BAD_MUTEX_E;
  738. }
  739. wc_errors_count = 0;
  740. wc_errors = NULL;
  741. wc_current_node = NULL;
  742. wc_last_node = NULL;
  743. return 0;
  744. }
  745. /* internal function that is called by wolfCrypt_Cleanup */
  746. int wc_LoggingCleanup(void)
  747. {
  748. /* clear logging entries */
  749. wc_ClearErrorNodes();
  750. /* free mutex */
  751. if (ERRQ_MUTEX_FREE() != 0) {
  752. WOLFSSL_MSG("Bad Mutex free");
  753. return BAD_MUTEX_E;
  754. }
  755. return 0;
  756. }
  757. static int peekErrorNode(int idx, const char **file, const char **reason,
  758. int *line)
  759. {
  760. struct wc_error_queue* err;
  761. if (idx < 0) {
  762. err = wc_last_node;
  763. }
  764. else {
  765. int i;
  766. err = (struct wc_error_queue*)wc_errors;
  767. for (i = 0; i < idx; i++) {
  768. if (err == NULL) {
  769. WOLFSSL_MSG("Error node not found. Bad index?");
  770. return BAD_FUNC_ARG;
  771. }
  772. err = err->next;
  773. }
  774. }
  775. if (err == NULL) {
  776. WOLFSSL_MSG("No Errors in queue");
  777. return BAD_STATE_E;
  778. }
  779. if (file != NULL) {
  780. *file = err->file;
  781. }
  782. if (reason != NULL) {
  783. *reason = err->error;
  784. }
  785. if (line != NULL) {
  786. *line = err->line;
  787. }
  788. return err->value;
  789. }
  790. /* peek at an error node
  791. *
  792. * idx : if -1 then the most recent node is looked at,
  793. * otherwise search through queue for node at the given index starting
  794. * from the absolute head wc_errors
  795. * file : pointer to internal file string
  796. * reason : pointer to internal error reason
  797. * line : line number that error happened at
  798. *
  799. * Returns a negative value in error case, on success returns the nodes error
  800. * value which is positive (absolute value)
  801. */
  802. int wc_PeekErrorNode(int idx, const char **file, const char **reason,
  803. int *line)
  804. {
  805. int ret;
  806. if (ERRQ_LOCK() != 0) {
  807. WOLFSSL_MSG("Lock debug mutex failed");
  808. return BAD_MUTEX_E;
  809. }
  810. ret = peekErrorNode(idx, file, reason, line);
  811. ERRQ_UNLOCK();
  812. return ret;
  813. }
  814. static int pullErrorNode(const char **file, const char **reason, int *line)
  815. {
  816. struct wc_error_queue* err;
  817. int value;
  818. err = wc_current_node;
  819. if (err == NULL) {
  820. WOLFSSL_MSG("No Errors in queue");
  821. return BAD_STATE_E;
  822. }
  823. if (file != NULL) {
  824. *file = err->file;
  825. }
  826. if (reason != NULL) {
  827. *reason = err->error;
  828. }
  829. if (line != NULL) {
  830. *line = err->line;
  831. }
  832. value = err->value;
  833. wc_current_node = err->next;
  834. return value;
  835. }
  836. /* Pulls the current node from error queue and increments current state.
  837. * Note: this does not delete nodes because input arguments are pointing to
  838. * node buffers.
  839. *
  840. * file pointer to file that error was in. Can be NULL to return no file.
  841. * reason error string giving reason for error. Can be NULL to return no reason.
  842. * line return line number of where error happened.
  843. *
  844. * returns the error value on success and BAD_MUTEX_E or BAD_STATE_E on failure
  845. */
  846. int wc_PullErrorNode(const char **file, const char **reason, int *line)
  847. {
  848. int value;
  849. if (ERRQ_LOCK() != 0) {
  850. WOLFSSL_MSG("Lock debug mutex failed");
  851. return BAD_MUTEX_E;
  852. }
  853. value = pullErrorNode(file, reason, line);
  854. ERRQ_UNLOCK();
  855. return value;
  856. }
  857. /* create new error node and add it to the queue
  858. * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal
  859. * function. */
  860. int wc_AddErrorNode(int error, int line, char* buf, char* file)
  861. {
  862. struct wc_error_queue* err;
  863. if (wc_errors_count >= ERROR_QUEUE_MAX) {
  864. WOLFSSL_MSG("Error queue is full, at ERROR_QUEUE_MAX");
  865. return MEMORY_E;
  866. }
  867. err = (struct wc_error_queue*)XMALLOC(
  868. sizeof(struct wc_error_queue), wc_error_heap, DYNAMIC_TYPE_LOG);
  869. if (err == NULL) {
  870. WOLFSSL_MSG("Unable to create error node for log");
  871. return MEMORY_E;
  872. }
  873. else {
  874. int sz;
  875. XMEMSET(err, 0, sizeof(struct wc_error_queue));
  876. err->heap = wc_error_heap;
  877. sz = (int)XSTRLEN(buf);
  878. if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
  879. sz = WOLFSSL_MAX_ERROR_SZ - 1;
  880. }
  881. if (sz > 0) {
  882. XMEMCPY(err->error, buf, sz);
  883. }
  884. sz = (int)XSTRLEN(file);
  885. if (sz > WOLFSSL_MAX_ERROR_SZ - 1) {
  886. sz = WOLFSSL_MAX_ERROR_SZ - 1;
  887. }
  888. if (sz > 0) {
  889. XMEMCPY(err->file, file, sz);
  890. }
  891. err->value = error;
  892. err->line = line;
  893. /* make sure is terminated */
  894. err->error[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
  895. err->file[WOLFSSL_MAX_ERROR_SZ - 1] = '\0';
  896. /* since is queue place new node at last of the list */
  897. if (wc_last_node == NULL) {
  898. /* case of first node added to queue */
  899. if (wc_errors != NULL) {
  900. /* check for unexpected case before over writing wc_errors */
  901. WOLFSSL_MSG("ERROR in adding new node to logging queue!!");
  902. /* In the event both wc_last_node and wc_errors are NULL, err
  903. * goes unassigned to external wc_errors, wc_last_node. Free
  904. * err in this instance since wc_ClearErrorNodes will not
  905. */
  906. XFREE(err, wc_error_heap, DYNAMIC_TYPE_LOG);
  907. }
  908. else {
  909. wc_errors = err;
  910. wc_last_node = err;
  911. wc_current_node = err;
  912. }
  913. }
  914. else {
  915. wc_last_node->next = err;
  916. err->prev = wc_last_node;
  917. wc_last_node = err;
  918. /* check the case where have read to the end of the queue and the
  919. * current node to read needs updated */
  920. if (wc_current_node == NULL) {
  921. wc_current_node = err;
  922. }
  923. }
  924. wc_errors_count++;
  925. }
  926. return 0;
  927. }
  928. /* returns the current index into the queue, which is the node that
  929. * wc_current_node is pointing to. It can be greater than zero in cases
  930. * where wc_PullErrorNode() has been called without the node having been
  931. * removed. */
  932. static int getErrorNodeCurrentIdx(void)
  933. {
  934. int ret = 0;
  935. struct wc_error_queue* current;
  936. current = (struct wc_error_queue*)wc_errors;
  937. while (current != wc_current_node && current != NULL) {
  938. current = current->next;
  939. ret++;
  940. }
  941. /* wc_current_node was not found in the list! use index 0 */
  942. if (current == NULL) {
  943. ret = 0;
  944. }
  945. return ret;
  946. }
  947. static void removeErrorNode(int idx)
  948. {
  949. struct wc_error_queue* current;
  950. if (idx == -1) {
  951. current = wc_last_node;
  952. }
  953. else {
  954. current = (struct wc_error_queue*)wc_errors;
  955. for (; current != NULL && idx > 0; idx--)
  956. current = current->next;
  957. }
  958. if (current != NULL) {
  959. if (current->prev != NULL)
  960. current->prev->next = current->next;
  961. if (current->next != NULL)
  962. current->next->prev = current->prev;
  963. if (wc_last_node == current)
  964. wc_last_node = current->prev;
  965. if (wc_errors == current)
  966. wc_errors = current->next;
  967. if (wc_current_node == current)
  968. wc_current_node = current->next;
  969. XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
  970. wc_errors_count--;
  971. /* last node left in list was free'd, reset list head */
  972. if (wc_errors_count == 0) {
  973. wc_errors = NULL;
  974. wc_last_node = NULL;
  975. wc_current_node = NULL;
  976. }
  977. }
  978. }
  979. /* Removes the error node at the specified index.
  980. * idx : if -1 then the most recent node is looked at,
  981. * otherwise search through queue for node at the given index starting
  982. * from the absolute head wc_errors
  983. */
  984. void wc_RemoveErrorNode(int idx)
  985. {
  986. if (ERRQ_LOCK() != 0) {
  987. WOLFSSL_MSG("Lock debug mutex failed");
  988. return;
  989. }
  990. removeErrorNode(idx);
  991. ERRQ_UNLOCK();
  992. }
  993. static void clearErrorNodes(void)
  994. {
  995. struct wc_error_queue* current;
  996. struct wc_error_queue* next;
  997. /* free all nodes from error queue (even previously 'pulled' ones) starting
  998. * at the lists absolute head of wc_errors */
  999. current = (struct wc_error_queue*)wc_errors;
  1000. while (current != NULL) {
  1001. next = current->next;
  1002. XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
  1003. current = next;
  1004. }
  1005. wc_errors_count = 0;
  1006. wc_errors = NULL;
  1007. wc_last_node = NULL;
  1008. wc_current_node = NULL;
  1009. }
  1010. /* Clears out the list of error nodes.
  1011. */
  1012. void wc_ClearErrorNodes(void)
  1013. {
  1014. if (ERRQ_LOCK() != 0) {
  1015. WOLFSSL_MSG("Lock debug mutex failed");
  1016. return;
  1017. }
  1018. clearErrorNodes();
  1019. ERRQ_UNLOCK();
  1020. }
  1021. int wc_SetLoggingHeap(void* h)
  1022. {
  1023. if (ERRQ_LOCK() != 0) {
  1024. WOLFSSL_MSG("Lock debug mutex failed");
  1025. return BAD_MUTEX_E;
  1026. }
  1027. wc_error_heap = h;
  1028. ERRQ_UNLOCK();
  1029. return 0;
  1030. }
  1031. /* frees all nodes in the queue
  1032. *
  1033. * id this is the thread id
  1034. */
  1035. int wc_ERR_remove_state(void)
  1036. {
  1037. struct wc_error_queue* current;
  1038. struct wc_error_queue* next;
  1039. if (ERRQ_LOCK() != 0) {
  1040. WOLFSSL_MSG("Lock debug mutex failed");
  1041. return BAD_MUTEX_E;
  1042. }
  1043. /* free all nodes from error queue */
  1044. current = (struct wc_error_queue*)wc_errors;
  1045. while (current != NULL) {
  1046. next = current->next;
  1047. XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
  1048. current = next;
  1049. }
  1050. wc_errors_count = 0;
  1051. wc_errors = NULL;
  1052. wc_last_node = NULL;
  1053. ERRQ_UNLOCK();
  1054. return 0;
  1055. }
  1056. unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
  1057. const char **data, int *flags,
  1058. int (*ignore_err)(int err))
  1059. {
  1060. int idx;
  1061. WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
  1062. /* No data or flags stored - error display only in Nginx. */
  1063. if (data != NULL) {
  1064. *data = "";
  1065. }
  1066. if (flags != NULL) {
  1067. *flags = 0;
  1068. }
  1069. if (ERRQ_LOCK() != 0) {
  1070. WOLFSSL_MSG("Lock debug mutex failed");
  1071. return (unsigned long)(0 - BAD_MUTEX_E);
  1072. }
  1073. idx = getErrorNodeCurrentIdx();
  1074. while (1) {
  1075. int ret = peekErrorNode(idx, file, NULL, line);
  1076. if (ret == BAD_MUTEX_E || ret == BAD_FUNC_ARG || ret == BAD_STATE_E) {
  1077. ERRQ_UNLOCK();
  1078. WOLFSSL_MSG("Issue peeking at error node in queue");
  1079. return 0;
  1080. }
  1081. /* OpenSSL uses positive error codes */
  1082. if (ret < 0) {
  1083. ret = -ret;
  1084. }
  1085. if (ignore_err && ignore_err(ret)) {
  1086. removeErrorNode(idx);
  1087. continue;
  1088. }
  1089. ERRQ_UNLOCK();
  1090. return (unsigned long)ret;
  1091. }
  1092. }
  1093. unsigned long wc_GetErrorNodeErr(void)
  1094. {
  1095. int ret;
  1096. WOLFSSL_ENTER("wc_GetErrorNodeErr");
  1097. if (ERRQ_LOCK() != 0) {
  1098. WOLFSSL_MSG("Lock debug mutex failed");
  1099. return (unsigned long)(0 - BAD_MUTEX_E);
  1100. }
  1101. ret = pullErrorNode(NULL, NULL, NULL);
  1102. if (ret < 0) {
  1103. if (ret == BAD_STATE_E) {
  1104. ret = 0; /* no errors in queue */
  1105. }
  1106. else {
  1107. WOLFSSL_MSG("Error with pulling error node!");
  1108. WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
  1109. ret = 0 - ret; /* return absolute value of error */
  1110. /* panic and try to clear out nodes */
  1111. clearErrorNodes();
  1112. }
  1113. }
  1114. else {
  1115. int idx = getErrorNodeCurrentIdx();
  1116. if (idx < 0) {
  1117. WOLFSSL_MSG("Error with getting current index!");
  1118. ret = BAD_STATE_E;
  1119. WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
  1120. /* panic and try to clear out nodes and reset queue state */
  1121. clearErrorNodes();
  1122. }
  1123. else if (idx > 0) {
  1124. idx -= 1;
  1125. removeErrorNode(idx);
  1126. }
  1127. else {
  1128. /* if current idx is 0 then the queue only had one node */
  1129. removeErrorNode(idx);
  1130. }
  1131. }
  1132. ERRQ_UNLOCK();
  1133. return ret;
  1134. }
  1135. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  1136. /* This callback allows the application to provide a custom error printing
  1137. * function. */
  1138. void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
  1139. void *u)
  1140. {
  1141. WOLFSSL_ENTER("wc_ERR_print_errors_cb");
  1142. if (cb == NULL) {
  1143. /* Invalid param */
  1144. return;
  1145. }
  1146. if (ERRQ_LOCK() != 0) {
  1147. WOLFSSL_MSG("Lock debug mutex failed");
  1148. }
  1149. else {
  1150. /* free all nodes from error queue and print them to file */
  1151. struct wc_error_queue *current;
  1152. struct wc_error_queue *next;
  1153. current = (struct wc_error_queue *)wc_errors;
  1154. while (current != NULL)
  1155. {
  1156. next = current->next;
  1157. cb(current->error, XSTRLEN(current->error), u);
  1158. XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
  1159. current = next;
  1160. }
  1161. /* set global pointers to match having been freed */
  1162. wc_errors_count = 0;
  1163. wc_errors = NULL;
  1164. wc_last_node = NULL;
  1165. ERRQ_UNLOCK();
  1166. }
  1167. }
  1168. #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
  1169. #endif /* !ERROR_QUEUE_PER_THREAD */
  1170. #else /* WOLFSSL_HAVE_ERROR_QUEUE */
  1171. /* NO ERROR_QUEUE at all */
  1172. int wc_LoggingInit(void)
  1173. {
  1174. return 0;
  1175. }
  1176. /* internal function that is called by wolfCrypt_Cleanup */
  1177. int wc_LoggingCleanup(void)
  1178. {
  1179. return 0;
  1180. }
  1181. int wc_PeekErrorNode(int idx, const char **file, const char **reason,
  1182. int *line)
  1183. {
  1184. (void)idx;
  1185. (void)file;
  1186. (void)reason;
  1187. (void)line;
  1188. WOLFSSL_MSG("Error queue turned off, can not peak nodes");
  1189. return NOT_COMPILED_IN;
  1190. }
  1191. int wc_PullErrorNode(const char **file, const char **reason, int *line)
  1192. {
  1193. (void)file;
  1194. (void)reason;
  1195. (void)line;
  1196. WOLFSSL_MSG("Error queue turned off, can not pull nodes");
  1197. return NOT_COMPILED_IN;
  1198. }
  1199. int wc_AddErrorNode(int error, int line, char* buf, char* file)
  1200. {
  1201. (void)error;
  1202. (void)line;
  1203. (void)buf;
  1204. (void)file;
  1205. WOLFSSL_MSG("Error queue turned off, can not add nodes");
  1206. return NOT_COMPILED_IN;
  1207. }
  1208. void wc_RemoveErrorNode(int idx)
  1209. {
  1210. (void)idx;
  1211. WOLFSSL_MSG("Error queue turned off, can not remove nodes");
  1212. }
  1213. void wc_ClearErrorNodes(void)
  1214. {
  1215. WOLFSSL_MSG("Error queue turned off, can not clear nodes");
  1216. }
  1217. int wc_SetLoggingHeap(void* h)
  1218. {
  1219. (void)h;
  1220. return 0;
  1221. }
  1222. int wc_ERR_remove_state(void)
  1223. {
  1224. return 0;
  1225. }
  1226. unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
  1227. const char **data, int *flags,
  1228. int (*ignore_err)(int err))
  1229. {
  1230. WOLFSSL_ENTER("wc_PeekErrorNodeLineData");
  1231. (void)line;
  1232. (void)file;
  1233. (void)ignore_err;
  1234. if (data != NULL) {
  1235. *data = "";
  1236. }
  1237. if (flags != NULL) {
  1238. *flags = 0;
  1239. }
  1240. return (unsigned long)(0 - NOT_COMPILED_IN);
  1241. }
  1242. unsigned long wc_GetErrorNodeErr(void)
  1243. {
  1244. WOLFSSL_ENTER("wc_GetErrorNodeErr");
  1245. return (unsigned long)(0 - NOT_COMPILED_IN);
  1246. }
  1247. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  1248. void wc_ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
  1249. void *u)
  1250. {
  1251. WOLFSSL_ENTER("wc_ERR_print_errors_cb");
  1252. (void)cb;
  1253. (void)u;
  1254. }
  1255. #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
  1256. #endif /* !WOLFSSL_HAVE_ERROR_QUEUE */
  1257. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  1258. /* empties out the error queue into the file */
  1259. static int wc_ERR_dump_to_file (const char *str, size_t len, void *u)
  1260. {
  1261. XFILE fp = (XFILE ) u;
  1262. if (fprintf(fp, "%-*.*s\n", (int)len, (int)len, str) < 0)
  1263. return IO_FAILED_E;
  1264. return 0;
  1265. }
  1266. void wc_ERR_print_errors_fp(XFILE fp)
  1267. {
  1268. WOLFSSL_ENTER("wc_ERR_print_errors_fp");
  1269. /* Send all errors to the wc_ERR_dump_to_file function */
  1270. wc_ERR_print_errors_cb(wc_ERR_dump_to_file, fp);
  1271. }
  1272. #endif /* !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) */
  1273. #endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */
  1274. /*
  1275. * When using OPENSSL_EXTRA or DEBUG_WOLFSSL_VERBOSE macro then WOLFSSL_ERROR is
  1276. * mapped to new function WOLFSSL_ERROR_LINE which gets the line # and function
  1277. * name where WOLFSSL_ERROR is called at.
  1278. */
  1279. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || \
  1280. defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  1281. defined(OPENSSL_EXTRA)
  1282. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  1283. void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line,
  1284. const char* file, void* usrCtx)
  1285. #else
  1286. void WOLFSSL_ERROR(int error)
  1287. #endif
  1288. {
  1289. #ifdef WOLFSSL_ASYNC_CRYPT
  1290. if (error != WC_PENDING_E)
  1291. #endif
  1292. {
  1293. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1294. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  1295. (void)usrCtx; /* a user ctx for future flexibility */
  1296. (void)func;
  1297. if (ERRQ_LOCK() != 0) {
  1298. WOLFSSL_MSG("Lock debug mutex failed");
  1299. (void)XSNPRINTF(buffer, sizeof(buffer),
  1300. "wolfSSL error occurred, error = %d", error);
  1301. }
  1302. else {
  1303. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1304. /* If running in compatibility mode do not add want read and
  1305. want right to error queue */
  1306. if (error != WANT_READ && error != WANT_WRITE) {
  1307. #endif
  1308. if (error < 0)
  1309. error = error - (2 * error); /* get absolute value */
  1310. (void)XSNPRINTF(buffer, sizeof(buffer),
  1311. "wolfSSL error occurred, error = %d line:%u file:%s",
  1312. error, line, file);
  1313. if (wc_AddErrorNode(error, (int)line, buffer, (char*)file) != 0) {
  1314. WOLFSSL_MSG("Error creating logging node");
  1315. /* with void function there is no return here, continue on
  1316. * to unlock mutex and log what buffer was created. */
  1317. }
  1318. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1319. }
  1320. else {
  1321. (void)XSNPRINTF(buffer, sizeof(buffer),
  1322. "wolfSSL error occurred, error = %d", error);
  1323. }
  1324. #endif
  1325. ERRQ_UNLOCK();
  1326. }
  1327. #else
  1328. (void)XSNPRINTF(buffer, sizeof(buffer),
  1329. "wolfSSL error occurred, error = %d", error);
  1330. #endif
  1331. #ifdef DEBUG_WOLFSSL
  1332. if (loggingEnabled)
  1333. wolfssl_log(ERROR_LOG , buffer);
  1334. #endif
  1335. }
  1336. }
  1337. void WOLFSSL_ERROR_MSG(const char* msg)
  1338. {
  1339. #ifdef DEBUG_WOLFSSL
  1340. if (loggingEnabled)
  1341. wolfssl_log(ERROR_LOG , msg);
  1342. #else
  1343. (void)msg;
  1344. #endif
  1345. }
  1346. #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */