logging.c 40 KB

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