trace.c 14 KB

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