logging.c 41 KB

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