trace.c 14 KB

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