trace.c 14 KB

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