trace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright 2019-2021 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 "e_os.h" /* strcasecmp for Windows */
  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 trace_categories[] = {
  104. TRACE_CATEGORY_(ALL),
  105. TRACE_CATEGORY_(TRACE),
  106. TRACE_CATEGORY_(INIT),
  107. TRACE_CATEGORY_(TLS),
  108. TRACE_CATEGORY_(TLS_CIPHER),
  109. TRACE_CATEGORY_(CONF),
  110. #ifndef OPENSSL_NO_ENGINE
  111. TRACE_CATEGORY_(ENGINE_TABLE),
  112. TRACE_CATEGORY_(ENGINE_REF_COUNT),
  113. #endif
  114. TRACE_CATEGORY_(PKCS5V2),
  115. TRACE_CATEGORY_(PKCS12_KEYGEN),
  116. TRACE_CATEGORY_(PKCS12_DECRYPT),
  117. TRACE_CATEGORY_(X509V3_POLICY),
  118. TRACE_CATEGORY_(BN_CTX),
  119. TRACE_CATEGORY_(STORE),
  120. TRACE_CATEGORY_(DECODER),
  121. TRACE_CATEGORY_(ENCODER),
  122. TRACE_CATEGORY_(REF_COUNT)
  123. };
  124. const char *OSSL_trace_get_category_name(int num)
  125. {
  126. size_t i;
  127. for (i = 0; i < OSSL_NELEM(trace_categories); i++)
  128. if (trace_categories[i].num == num)
  129. return trace_categories[i].name;
  130. return NULL; /* not found */
  131. }
  132. int OSSL_trace_get_category_num(const char *name)
  133. {
  134. size_t i;
  135. for (i = 0; i < OSSL_NELEM(trace_categories); i++)
  136. if (strcasecmp(name, trace_categories[i].name) == 0)
  137. return trace_categories[i].num;
  138. return -1; /* not found */
  139. }
  140. #ifndef OPENSSL_NO_TRACE
  141. /* We use one trace channel for each trace category */
  142. static struct {
  143. enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
  144. BIO *bio;
  145. char *prefix;
  146. char *suffix;
  147. } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
  148. { 0, NULL, NULL, NULL },
  149. };
  150. #endif
  151. #ifndef OPENSSL_NO_TRACE
  152. enum {
  153. CHANNEL,
  154. PREFIX,
  155. SUFFIX
  156. };
  157. static int trace_attach_cb(int category, int type, const void *data)
  158. {
  159. switch (type) {
  160. case CHANNEL:
  161. OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
  162. data, trace_categories[category].name);
  163. break;
  164. case PREFIX:
  165. OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
  166. (const char *)data, trace_categories[category].name);
  167. break;
  168. case SUFFIX:
  169. OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
  170. (const char *)data, trace_categories[category].name);
  171. break;
  172. default: /* No clue */
  173. break;
  174. }
  175. return 1;
  176. }
  177. static int trace_detach_cb(int category, int type, const void *data)
  178. {
  179. switch (type) {
  180. case CHANNEL:
  181. OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
  182. data, trace_categories[category].name);
  183. break;
  184. case PREFIX:
  185. OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
  186. (const char *)data, trace_categories[category].name);
  187. break;
  188. case SUFFIX:
  189. OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
  190. (const char *)data, trace_categories[category].name);
  191. break;
  192. default: /* No clue */
  193. break;
  194. }
  195. return 1;
  196. }
  197. static int do_ossl_trace_init(void);
  198. static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
  199. DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
  200. {
  201. return do_ossl_trace_init();
  202. }
  203. static int set_trace_data(int category, int type, BIO **channel,
  204. const char **prefix, const char **suffix,
  205. int (*attach_cb)(int, int, const void *),
  206. int (*detach_cb)(int, int, const void *))
  207. {
  208. BIO *curr_channel = NULL;
  209. char *curr_prefix = NULL;
  210. char *curr_suffix = NULL;
  211. /* Ensure do_ossl_trace_init() is called once */
  212. if (!RUN_ONCE(&trace_inited, ossl_trace_init))
  213. return 0;
  214. curr_channel = trace_channels[category].bio;
  215. curr_prefix = trace_channels[category].prefix;
  216. curr_suffix = trace_channels[category].suffix;
  217. /* Make sure to run the detach callback first on all data */
  218. if (prefix != NULL && curr_prefix != NULL) {
  219. detach_cb(category, PREFIX, curr_prefix);
  220. }
  221. if (suffix != NULL && curr_suffix != NULL) {
  222. detach_cb(category, SUFFIX, curr_suffix);
  223. }
  224. if (channel != NULL && curr_channel != NULL) {
  225. detach_cb(category, CHANNEL, curr_channel);
  226. }
  227. /* After detach callbacks are done, clear data where appropriate */
  228. if (prefix != NULL && curr_prefix != NULL) {
  229. OPENSSL_free(curr_prefix);
  230. trace_channels[category].prefix = NULL;
  231. }
  232. if (suffix != NULL && curr_suffix != NULL) {
  233. OPENSSL_free(curr_suffix);
  234. trace_channels[category].suffix = NULL;
  235. }
  236. if (channel != NULL && curr_channel != NULL) {
  237. BIO_free(curr_channel);
  238. trace_channels[category].type = 0;
  239. trace_channels[category].bio = NULL;
  240. }
  241. /* Before running callbacks are done, set new data where appropriate */
  242. if (channel != NULL && *channel != NULL) {
  243. trace_channels[category].type = type;
  244. trace_channels[category].bio = *channel;
  245. }
  246. if (prefix != NULL && *prefix != NULL) {
  247. if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
  248. return 0;
  249. trace_channels[category].prefix = curr_prefix;
  250. }
  251. if (suffix != NULL && *suffix != NULL) {
  252. if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
  253. return 0;
  254. trace_channels[category].suffix = curr_suffix;
  255. }
  256. /* Finally, run the attach callback on the new data */
  257. if (channel != NULL && *channel != NULL) {
  258. attach_cb(category, CHANNEL, *channel);
  259. }
  260. if (prefix != NULL && *prefix != NULL) {
  261. attach_cb(category, PREFIX, *prefix);
  262. }
  263. if (suffix != NULL && *suffix != NULL) {
  264. attach_cb(category, SUFFIX, *suffix);
  265. }
  266. return 1;
  267. }
  268. static int do_ossl_trace_init(void)
  269. {
  270. trace_lock = CRYPTO_THREAD_lock_new();
  271. return trace_lock != NULL;
  272. }
  273. #endif
  274. void ossl_trace_cleanup(void)
  275. {
  276. #ifndef OPENSSL_NO_TRACE
  277. int category;
  278. BIO *channel = NULL;
  279. const char *prefix = NULL;
  280. const char *suffix = NULL;
  281. for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
  282. /* We force the TRACE category to be treated last */
  283. if (category == OSSL_TRACE_CATEGORY_TRACE)
  284. continue;
  285. set_trace_data(category, 0, &channel, &prefix, &suffix,
  286. trace_attach_cb, trace_detach_cb);
  287. }
  288. set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
  289. &prefix, &suffix,
  290. trace_attach_cb, trace_detach_cb);
  291. CRYPTO_THREAD_lock_free(trace_lock);
  292. #endif
  293. }
  294. int OSSL_trace_set_channel(int category, BIO *channel)
  295. {
  296. #ifndef OPENSSL_NO_TRACE
  297. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  298. return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
  299. trace_attach_cb, trace_detach_cb);
  300. #endif
  301. return 0;
  302. }
  303. #ifndef OPENSSL_NO_TRACE
  304. static int trace_attach_w_callback_cb(int category, int type, const void *data)
  305. {
  306. switch (type) {
  307. case CHANNEL:
  308. OSSL_TRACE2(TRACE,
  309. "Attach channel %p to category '%s' (with callback)\n",
  310. data, trace_categories[category].name);
  311. break;
  312. case PREFIX:
  313. OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
  314. (const char *)data, trace_categories[category].name);
  315. break;
  316. case SUFFIX:
  317. OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
  318. (const char *)data, trace_categories[category].name);
  319. break;
  320. default: /* No clue */
  321. break;
  322. }
  323. return 1;
  324. }
  325. #endif
  326. int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
  327. {
  328. #ifndef OPENSSL_NO_TRACE
  329. BIO *channel = NULL;
  330. struct trace_data_st *trace_data = NULL;
  331. if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
  332. return 0;
  333. if (callback != NULL) {
  334. if ((channel = BIO_new(&trace_method)) == NULL
  335. || (trace_data =
  336. OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
  337. goto err;
  338. trace_data->callback = callback;
  339. trace_data->category = category;
  340. trace_data->data = data;
  341. BIO_set_data(channel, trace_data);
  342. }
  343. if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
  344. trace_attach_w_callback_cb, trace_detach_cb))
  345. goto err;
  346. return 1;
  347. err:
  348. BIO_free(channel);
  349. OPENSSL_free(trace_data);
  350. #endif
  351. return 0;
  352. }
  353. int OSSL_trace_set_prefix(int category, const char *prefix)
  354. {
  355. #ifndef OPENSSL_NO_TRACE
  356. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  357. return set_trace_data(category, 0, NULL, &prefix, NULL,
  358. trace_attach_cb, trace_detach_cb);
  359. #endif
  360. return 0;
  361. }
  362. int OSSL_trace_set_suffix(int category, const char *suffix)
  363. {
  364. #ifndef OPENSSL_NO_TRACE
  365. if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
  366. return set_trace_data(category, 0, NULL, NULL, &suffix,
  367. trace_attach_cb, trace_detach_cb);
  368. #endif
  369. return 0;
  370. }
  371. #ifndef OPENSSL_NO_TRACE
  372. static int ossl_trace_get_category(int category)
  373. {
  374. if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
  375. return -1;
  376. if (trace_channels[category].bio != NULL)
  377. return category;
  378. return OSSL_TRACE_CATEGORY_ALL;
  379. }
  380. #endif
  381. int OSSL_trace_enabled(int category)
  382. {
  383. int ret = 0;
  384. #ifndef OPENSSL_NO_TRACE
  385. category = ossl_trace_get_category(category);
  386. if (category >= 0)
  387. ret = trace_channels[category].bio != NULL;
  388. #endif
  389. return ret;
  390. }
  391. BIO *OSSL_trace_begin(int category)
  392. {
  393. BIO *channel = NULL;
  394. #ifndef OPENSSL_NO_TRACE
  395. char *prefix = NULL;
  396. category = ossl_trace_get_category(category);
  397. if (category < 0)
  398. return NULL;
  399. channel = trace_channels[category].bio;
  400. prefix = trace_channels[category].prefix;
  401. if (channel != NULL) {
  402. if (!CRYPTO_THREAD_write_lock(trace_lock))
  403. return NULL;
  404. current_channel = channel;
  405. switch (trace_channels[category].type) {
  406. case SIMPLE_CHANNEL:
  407. if (prefix != NULL) {
  408. (void)BIO_puts(channel, prefix);
  409. (void)BIO_puts(channel, "\n");
  410. }
  411. break;
  412. case CALLBACK_CHANNEL:
  413. (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
  414. prefix == NULL ? 0 : strlen(prefix), prefix);
  415. break;
  416. }
  417. }
  418. #endif
  419. return channel;
  420. }
  421. void OSSL_trace_end(int category, BIO * channel)
  422. {
  423. #ifndef OPENSSL_NO_TRACE
  424. char *suffix = NULL;
  425. category = ossl_trace_get_category(category);
  426. suffix = trace_channels[category].suffix;
  427. if (channel != NULL
  428. && ossl_assert(channel == current_channel)) {
  429. (void)BIO_flush(channel);
  430. switch (trace_channels[category].type) {
  431. case SIMPLE_CHANNEL:
  432. if (suffix != NULL) {
  433. (void)BIO_puts(channel, suffix);
  434. (void)BIO_puts(channel, "\n");
  435. }
  436. break;
  437. case CALLBACK_CHANNEL:
  438. (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
  439. suffix == NULL ? 0 : strlen(suffix), suffix);
  440. break;
  441. }
  442. current_channel = NULL;
  443. CRYPTO_THREAD_unlock(trace_lock);
  444. }
  445. #endif
  446. }