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