trace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "internal/thread_once.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/trace.h>
  15. #include "internal/bio.h"
  16. #include "internal/nelem.h"
  17. #include "internal/refcount.h"
  18. #include "crypto/cryptlib.h"
  19. #include "crypto/ctype.h"
  20. #ifndef OPENSSL_NO_TRACE
  21. static CRYPTO_RWLOCK *trace_lock = NULL;
  22. static const BIO *current_channel = NULL;
  23. /*-
  24. * INTERNAL TRACE CHANNEL IMPLEMENTATION
  25. *
  26. * For our own flexibility, all trace categories are associated with a
  27. * BIO sink object, also called the trace channel. Instead of a BIO object,
  28. * the application can also provide a callback function, in which case an
  29. * internal trace channel is attached, which simply calls the registered
  30. * callback function.
  31. */
  32. static int trace_write(BIO *b, const char *buf,
  33. size_t num, size_t *written);
  34. static int trace_puts(BIO *b, const char *str);
  35. static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
  36. static int trace_free(BIO *b);
  37. static const BIO_METHOD trace_method = {
  38. BIO_TYPE_SOURCE_SINK,
  39. "trace",
  40. trace_write,
  41. NULL, /* old write */
  42. NULL, /* read_ex */
  43. NULL, /* read */
  44. trace_puts,
  45. NULL, /* gets */
  46. trace_ctrl, /* ctrl */
  47. NULL, /* create */
  48. trace_free, /* free */
  49. NULL, /* callback_ctrl */
  50. };
  51. struct trace_data_st {
  52. OSSL_trace_cb callback;
  53. int category;
  54. void *data;
  55. };
  56. static int trace_write(BIO *channel,
  57. const char *buf, size_t num, size_t *written)
  58. {
  59. struct trace_data_st *ctx = BIO_get_data(channel);
  60. size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
  61. ctx->data);
  62. *written = cnt;
  63. return cnt != 0;
  64. }
  65. static int trace_puts(BIO *channel, const char *str)
  66. {
  67. size_t written;
  68. if (trace_write(channel, str, strlen(str), &written))
  69. return (int)written;
  70. return EOF;
  71. }
  72. static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
  73. {
  74. struct trace_data_st *ctx = BIO_get_data(channel);
  75. switch (cmd) {
  76. case OSSL_TRACE_CTRL_BEGIN:
  77. case OSSL_TRACE_CTRL_END:
  78. /* We know that the callback is likely to return 0 here */
  79. ctx->callback("", 0, ctx->category, cmd, ctx->data);
  80. return 1;
  81. default:
  82. break;
  83. }
  84. return -2; /* Unsupported */
  85. }
  86. static int trace_free(BIO *channel)
  87. {
  88. if (channel == NULL)
  89. return 0;
  90. OPENSSL_free(BIO_get_data(channel));
  91. return 1;
  92. }
  93. #endif
  94. /*-
  95. * TRACE
  96. */
  97. /* Helper struct and macro to get name string to number mapping */
  98. struct trace_category_st {
  99. const char * const name;
  100. const int num;
  101. };
  102. #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
  103. static const struct trace_category_st
  104. trace_categories[OSSL_TRACE_CATEGORY_NUM] = {
  105. TRACE_CATEGORY_(ALL),
  106. TRACE_CATEGORY_(TRACE),
  107. TRACE_CATEGORY_(INIT),
  108. TRACE_CATEGORY_(TLS),
  109. TRACE_CATEGORY_(TLS_CIPHER),
  110. TRACE_CATEGORY_(CONF),
  111. TRACE_CATEGORY_(ENGINE_TABLE),
  112. TRACE_CATEGORY_(ENGINE_REF_COUNT),
  113. TRACE_CATEGORY_(PKCS5V2),
  114. TRACE_CATEGORY_(PKCS12_KEYGEN),
  115. TRACE_CATEGORY_(PKCS12_DECRYPT),
  116. TRACE_CATEGORY_(X509V3_POLICY),
  117. TRACE_CATEGORY_(BN_CTX),
  118. TRACE_CATEGORY_(CMP),
  119. TRACE_CATEGORY_(STORE),
  120. TRACE_CATEGORY_(DECODER),
  121. TRACE_CATEGORY_(ENCODER),
  122. TRACE_CATEGORY_(REF_COUNT),
  123. TRACE_CATEGORY_(HTTP),
  124. }; /* KEEP THIS LIST IN SYNC with #define OSSL_TRACE_CATEGORY_... in trace.h */
  125. const char *OSSL_trace_get_category_name(int num)
  126. {
  127. if (num < 0 || (size_t)num >= OSSL_NELEM(trace_categories))
  128. return NULL;
  129. /*
  130. * Partial check that OSSL_TRACE_CATEGORY_... macros
  131. * are synced with trace_categories array
  132. */
  133. if (!ossl_assert(trace_categories[num].name != NULL)
  134. || !ossl_assert(trace_categories[num].num == num))
  135. return NULL;
  136. return trace_categories[num].name;
  137. }
  138. int OSSL_trace_get_category_num(const char *name)
  139. {
  140. size_t i;
  141. if (name == NULL)
  142. return -1;
  143. for (i = 0; i < OSSL_NELEM(trace_categories); i++)
  144. if (OPENSSL_strcasecmp(name, trace_categories[i].name) == 0)
  145. return trace_categories[i].num;
  146. return -1; /* not found */
  147. }
  148. #ifndef OPENSSL_NO_TRACE
  149. /* We use one trace channel for each trace category */
  150. static struct {
  151. enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
  152. BIO *bio;
  153. char *prefix;
  154. char *suffix;
  155. } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
  156. { 0, NULL, NULL, NULL },
  157. };
  158. #endif
  159. #ifndef OPENSSL_NO_TRACE
  160. enum {
  161. CHANNEL,
  162. PREFIX,
  163. SUFFIX
  164. };
  165. static int trace_attach_cb(int category, int type, const void *data)
  166. {
  167. switch (type) {
  168. case CHANNEL:
  169. OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
  170. data, trace_categories[category].name);
  171. break;
  172. case PREFIX:
  173. OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
  174. (const char *)data, trace_categories[category].name);
  175. break;
  176. case SUFFIX:
  177. OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
  178. (const char *)data, trace_categories[category].name);
  179. break;
  180. default: /* No clue */
  181. break;
  182. }
  183. return 1;
  184. }
  185. static int trace_detach_cb(int category, int type, const void *data)
  186. {
  187. switch (type) {
  188. case CHANNEL:
  189. OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
  190. data, trace_categories[category].name);
  191. break;
  192. case PREFIX:
  193. OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
  194. (const char *)data, trace_categories[category].name);
  195. break;
  196. case SUFFIX:
  197. OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
  198. (const char *)data, trace_categories[category].name);
  199. break;
  200. default: /* No clue */
  201. break;
  202. }
  203. return 1;
  204. }
  205. static int do_ossl_trace_init(void);
  206. static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
  207. DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
  208. {
  209. return do_ossl_trace_init();
  210. }
  211. static int set_trace_data(int category, int type, BIO **channel,
  212. const char **prefix, const char **suffix,
  213. int (*attach_cb)(int, int, const void *),
  214. int (*detach_cb)(int, int, const void *))
  215. {
  216. BIO *curr_channel = NULL;
  217. char *curr_prefix = NULL;
  218. char *curr_suffix = NULL;
  219. /* Ensure do_ossl_trace_init() is called once */
  220. if (!RUN_ONCE(&trace_inited, ossl_trace_init))
  221. return 0;
  222. curr_channel = trace_channels[category].bio;
  223. curr_prefix = trace_channels[category].prefix;
  224. curr_suffix = trace_channels[category].suffix;
  225. /* Make sure to run the detach callback first on all data */
  226. if (prefix != NULL && curr_prefix != NULL) {
  227. detach_cb(category, PREFIX, curr_prefix);
  228. }
  229. if (suffix != NULL && curr_suffix != NULL) {
  230. detach_cb(category, SUFFIX, curr_suffix);
  231. }
  232. if (channel != NULL && curr_channel != NULL) {
  233. detach_cb(category, CHANNEL, curr_channel);
  234. }
  235. /* After detach callbacks are done, clear data where appropriate */
  236. if (prefix != NULL && curr_prefix != NULL) {
  237. OPENSSL_free(curr_prefix);
  238. trace_channels[category].prefix = NULL;
  239. }
  240. if (suffix != NULL && curr_suffix != NULL) {
  241. OPENSSL_free(curr_suffix);
  242. trace_channels[category].suffix = NULL;
  243. }
  244. if (channel != NULL && curr_channel != NULL) {
  245. BIO_free(curr_channel);
  246. trace_channels[category].type = 0;
  247. trace_channels[category].bio = NULL;
  248. }
  249. /* Before running callbacks are done, set new data where appropriate */
  250. if (prefix != NULL && *prefix != NULL) {
  251. if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
  252. return 0;
  253. trace_channels[category].prefix = curr_prefix;
  254. }
  255. if (suffix != NULL && *suffix != NULL) {
  256. if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
  257. return 0;
  258. trace_channels[category].suffix = curr_suffix;
  259. }
  260. if (channel != NULL && *channel != NULL) {
  261. trace_channels[category].type = type;
  262. trace_channels[category].bio = *channel;
  263. /*
  264. * This must not be done before setting prefix/suffix,
  265. * as those may fail, and then the caller is mislead to free *channel.
  266. */
  267. }
  268. /* Finally, run the attach callback on the new data */
  269. if (channel != NULL && *channel != NULL) {
  270. attach_cb(category, CHANNEL, *channel);
  271. }
  272. if (prefix != NULL && *prefix != NULL) {
  273. attach_cb(category, PREFIX, *prefix);
  274. }
  275. if (suffix != NULL && *suffix != NULL) {
  276. attach_cb(category, SUFFIX, *suffix);
  277. }
  278. return 1;
  279. }
  280. static int do_ossl_trace_init(void)
  281. {
  282. trace_lock = CRYPTO_THREAD_lock_new();
  283. return trace_lock != NULL;
  284. }
  285. #endif
  286. void ossl_trace_cleanup(void)
  287. {
  288. #ifndef OPENSSL_NO_TRACE
  289. int category;
  290. BIO *channel = NULL;
  291. const char *prefix = NULL;
  292. const char *suffix = NULL;
  293. for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
  294. /* We force the TRACE category to be treated last */
  295. if (category == OSSL_TRACE_CATEGORY_TRACE)
  296. continue;
  297. set_trace_data(category, 0, &channel, &prefix, &suffix,
  298. trace_attach_cb, trace_detach_cb);
  299. }
  300. set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
  301. &prefix, &suffix,
  302. trace_attach_cb, trace_detach_cb);
  303. CRYPTO_THREAD_lock_free(trace_lock);
  304. #endif
  305. }
  306. int OSSL_trace_set_channel(int category, BIO *channel)
  307. {
  308. #ifndef OPENSSL_NO_TRACE
  309. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  310. return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
  311. trace_attach_cb, trace_detach_cb);
  312. #endif
  313. return 0;
  314. }
  315. #ifndef OPENSSL_NO_TRACE
  316. static int trace_attach_w_callback_cb(int category, int type, const void *data)
  317. {
  318. switch (type) {
  319. case CHANNEL:
  320. OSSL_TRACE2(TRACE,
  321. "Attach channel %p to category '%s' (with callback)\n",
  322. data, trace_categories[category].name);
  323. break;
  324. case PREFIX:
  325. OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
  326. (const char *)data, trace_categories[category].name);
  327. break;
  328. case SUFFIX:
  329. OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
  330. (const char *)data, trace_categories[category].name);
  331. break;
  332. default: /* No clue */
  333. break;
  334. }
  335. return 1;
  336. }
  337. #endif
  338. int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
  339. {
  340. #ifndef OPENSSL_NO_TRACE
  341. BIO *channel = NULL;
  342. struct trace_data_st *trace_data = NULL;
  343. if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
  344. return 0;
  345. if (callback != NULL) {
  346. if ((channel = BIO_new(&trace_method)) == NULL
  347. || (trace_data =
  348. OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
  349. goto err;
  350. trace_data->callback = callback;
  351. trace_data->category = category;
  352. trace_data->data = data;
  353. BIO_set_data(channel, trace_data);
  354. }
  355. if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
  356. trace_attach_w_callback_cb, trace_detach_cb))
  357. goto err;
  358. return 1;
  359. err:
  360. BIO_free(channel);
  361. OPENSSL_free(trace_data);
  362. #endif
  363. return 0;
  364. }
  365. int OSSL_trace_set_prefix(int category, const char *prefix)
  366. {
  367. #ifndef OPENSSL_NO_TRACE
  368. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  369. return set_trace_data(category, 0, NULL, &prefix, NULL,
  370. trace_attach_cb, trace_detach_cb);
  371. #endif
  372. return 0;
  373. }
  374. int OSSL_trace_set_suffix(int category, const char *suffix)
  375. {
  376. #ifndef OPENSSL_NO_TRACE
  377. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  378. return set_trace_data(category, 0, NULL, NULL, &suffix,
  379. trace_attach_cb, trace_detach_cb);
  380. #endif
  381. return 0;
  382. }
  383. #ifndef OPENSSL_NO_TRACE
  384. static int ossl_trace_get_category(int category)
  385. {
  386. if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
  387. return -1;
  388. if (trace_channels[category].bio != NULL)
  389. return category;
  390. return OSSL_TRACE_CATEGORY_ALL;
  391. }
  392. #endif
  393. int OSSL_trace_enabled(int category)
  394. {
  395. int ret = 0;
  396. #ifndef OPENSSL_NO_TRACE
  397. category = ossl_trace_get_category(category);
  398. if (category >= 0)
  399. ret = trace_channels[category].bio != NULL;
  400. #endif
  401. return ret;
  402. }
  403. BIO *OSSL_trace_begin(int category)
  404. {
  405. BIO *channel = NULL;
  406. #ifndef OPENSSL_NO_TRACE
  407. char *prefix = NULL;
  408. category = ossl_trace_get_category(category);
  409. if (category < 0)
  410. return NULL;
  411. channel = trace_channels[category].bio;
  412. prefix = trace_channels[category].prefix;
  413. if (channel != NULL) {
  414. if (!CRYPTO_THREAD_write_lock(trace_lock))
  415. return NULL;
  416. current_channel = channel;
  417. switch (trace_channels[category].type) {
  418. case SIMPLE_CHANNEL:
  419. if (prefix != NULL) {
  420. (void)BIO_puts(channel, prefix);
  421. (void)BIO_puts(channel, "\n");
  422. }
  423. break;
  424. case CALLBACK_CHANNEL:
  425. (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
  426. prefix == NULL ? 0 : strlen(prefix), prefix);
  427. break;
  428. }
  429. }
  430. #endif
  431. return channel;
  432. }
  433. void OSSL_trace_end(int category, BIO *channel)
  434. {
  435. #ifndef OPENSSL_NO_TRACE
  436. char *suffix = NULL;
  437. category = ossl_trace_get_category(category);
  438. if (category < 0)
  439. return;
  440. suffix = trace_channels[category].suffix;
  441. if (channel != NULL
  442. && ossl_assert(channel == current_channel)) {
  443. (void)BIO_flush(channel);
  444. switch (trace_channels[category].type) {
  445. case SIMPLE_CHANNEL:
  446. if (suffix != NULL) {
  447. (void)BIO_puts(channel, suffix);
  448. (void)BIO_puts(channel, "\n");
  449. }
  450. break;
  451. case CALLBACK_CHANNEL:
  452. (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
  453. suffix == NULL ? 0 : strlen(suffix), suffix);
  454. break;
  455. }
  456. current_channel = NULL;
  457. CRYPTO_THREAD_unlock(trace_lock);
  458. }
  459. #endif
  460. }
  461. int OSSL_trace_string(BIO *out, int text, int full,
  462. const unsigned char *data, size_t size)
  463. {
  464. unsigned char buf[OSSL_TRACE_STRING_MAX + 1];
  465. int len, i;
  466. if (!full && size > OSSL_TRACE_STRING_MAX) {
  467. BIO_printf(out, "[len %zu limited to %d]: ",
  468. size, OSSL_TRACE_STRING_MAX);
  469. len = OSSL_TRACE_STRING_MAX;
  470. } else {
  471. len = (int)size;
  472. }
  473. if (!text) { /* mask control characters while preserving newlines */
  474. for (i = 0; i < len; i++, data++)
  475. buf[i] = (char)*data != '\n' && ossl_iscntrl((int)*data)
  476. ? ' ' : *data;
  477. if (len == 0 || data[-1] != '\n')
  478. buf[len++] = '\n';
  479. data = buf;
  480. }
  481. return BIO_printf(out, "%.*s", len, data);
  482. }