trace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #ifndef OPENSSL_TRACE_H
  10. # define OPENSSL_TRACE_H
  11. # pragma once
  12. # include <stdarg.h>
  13. # include <openssl/bio.h>
  14. # ifdef __cplusplus
  15. extern "C" {
  16. # endif
  17. /*
  18. * TRACE CATEGORIES
  19. */
  20. /*
  21. * The trace messages of the OpenSSL libraries are organized into different
  22. * categories. For every trace category, the application can register a separate
  23. * tracer callback. When a callback is registered, a so called trace channel is
  24. * created for this category. This channel consists essentially of an internal
  25. * BIO which sends all trace output it receives to the registered application
  26. * callback.
  27. *
  28. * The ALL category can be used as a fallback category to register a single
  29. * channel which receives the output from all categories. However, if the
  30. * application intends to print the trace channel name in the line prefix,
  31. * it is better to register channels for all categories separately.
  32. * (This is how the openssl application does it.)
  33. */
  34. # define OSSL_TRACE_CATEGORY_ALL 0 /* The fallback */
  35. # define OSSL_TRACE_CATEGORY_TRACE 1
  36. # define OSSL_TRACE_CATEGORY_INIT 2
  37. # define OSSL_TRACE_CATEGORY_TLS 3
  38. # define OSSL_TRACE_CATEGORY_TLS_CIPHER 4
  39. # define OSSL_TRACE_CATEGORY_CONF 5
  40. # define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6
  41. # define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7
  42. # define OSSL_TRACE_CATEGORY_PKCS5V2 8
  43. # define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 9
  44. # define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 10
  45. # define OSSL_TRACE_CATEGORY_X509V3_POLICY 11
  46. # define OSSL_TRACE_CATEGORY_BN_CTX 12
  47. # define OSSL_TRACE_CATEGORY_CMP 13
  48. # define OSSL_TRACE_CATEGORY_STORE 14
  49. # define OSSL_TRACE_CATEGORY_DECODER 15
  50. # define OSSL_TRACE_CATEGORY_ENCODER 16
  51. # define OSSL_TRACE_CATEGORY_REF_COUNT 17
  52. /* Count of available categories. */
  53. # define OSSL_TRACE_CATEGORY_NUM 18
  54. /* Returns the trace category number for the given |name| */
  55. int OSSL_trace_get_category_num(const char *name);
  56. /* Returns the trace category name for the given |num| */
  57. const char *OSSL_trace_get_category_name(int num);
  58. /*
  59. * TRACE CONSUMERS
  60. */
  61. /*
  62. * Enables tracing for the given |category| by providing a BIO sink
  63. * as |channel|. If a null pointer is passed as |channel|, an existing
  64. * trace channel is removed and tracing for the category is disabled.
  65. *
  66. * Returns 1 on success and 0 on failure
  67. */
  68. int OSSL_trace_set_channel(int category, BIO* channel);
  69. /*
  70. * Attach a prefix and a suffix to the given |category|, to be printed at the
  71. * beginning and at the end of each trace output group, i.e. when
  72. * OSSL_trace_begin() and OSSL_trace_end() are called.
  73. * If a null pointer is passed as argument, the existing prefix or suffix is
  74. * removed.
  75. *
  76. * They return 1 on success and 0 on failure
  77. */
  78. int OSSL_trace_set_prefix(int category, const char *prefix);
  79. int OSSL_trace_set_suffix(int category, const char *suffix);
  80. /*
  81. * OSSL_trace_cb is the type tracing callback provided by the application.
  82. * It MUST return the number of bytes written, or 0 on error (in other words,
  83. * it can never write zero bytes).
  84. *
  85. * The |buffer| will always contain text, which may consist of several lines.
  86. * The |data| argument points to whatever data was provided by the application
  87. * when registering the tracer function.
  88. *
  89. * The |category| number is given, as well as a |cmd| number, described below.
  90. */
  91. typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
  92. int category, int cmd, void *data);
  93. /*
  94. * Possible |cmd| numbers.
  95. */
  96. # define OSSL_TRACE_CTRL_BEGIN 0
  97. # define OSSL_TRACE_CTRL_WRITE 1
  98. # define OSSL_TRACE_CTRL_END 2
  99. /*
  100. * Enables tracing for the given |category| by creating an internal
  101. * trace channel which sends the output to the given |callback|.
  102. * If a null pointer is passed as callback, an existing trace channel
  103. * is removed and tracing for the category is disabled.
  104. *
  105. * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
  106. * exclusive.
  107. *
  108. * Returns 1 on success and 0 on failure
  109. */
  110. int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
  111. /*
  112. * TRACE PRODUCERS
  113. */
  114. /*
  115. * Returns 1 if tracing for the specified category is enabled, otherwise 0
  116. */
  117. int OSSL_trace_enabled(int category);
  118. /*
  119. * Wrap a group of tracing output calls. OSSL_trace_begin() locks tracing and
  120. * returns the trace channel associated with the given category, or NULL if no
  121. * channel is associated with the category. OSSL_trace_end() unlocks tracing.
  122. *
  123. * Usage:
  124. *
  125. * BIO *out;
  126. * if ((out = OSSL_trace_begin(category)) != NULL) {
  127. * ...
  128. * BIO_fprintf(out, ...);
  129. * ...
  130. * OSSL_trace_end(category, out);
  131. * }
  132. *
  133. * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
  134. */
  135. BIO *OSSL_trace_begin(int category);
  136. void OSSL_trace_end(int category, BIO *channel);
  137. /*
  138. * OSSL_TRACE* Convenience Macros
  139. */
  140. /*
  141. * When the tracing feature is disabled, these macros are defined to
  142. * produce dead code, which a good compiler should eliminate.
  143. */
  144. /*
  145. * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
  146. *
  147. * These two macros can be used to create a block which is executed only
  148. * if the corresponding trace category is enabled. Inside this block, a
  149. * local variable named |trc_out| is defined, which points to the channel
  150. * associated with the given trace category.
  151. *
  152. * Usage: (using 'TLS' as an example category)
  153. *
  154. * OSSL_TRACE_BEGIN(TLS) {
  155. *
  156. * BIO_fprintf(trc_out, ... );
  157. *
  158. * } OSSL_TRACE_END(TLS);
  159. *
  160. *
  161. * This expands to the following code
  162. *
  163. * do {
  164. * BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  165. * if (trc_out != NULL) {
  166. * ...
  167. * BIO_fprintf(trc_out, ...);
  168. * }
  169. * OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  170. * } while (0);
  171. *
  172. * The use of the inner '{...}' group and the trailing ';' is enforced
  173. * by the definition of the macros in order to make the code look as much
  174. * like C code as possible.
  175. *
  176. * Before returning from inside the trace block, it is necessary to
  177. * call OSSL_TRACE_CANCEL(category).
  178. */
  179. # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
  180. # define OSSL_TRACE_BEGIN(category) \
  181. do { \
  182. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
  183. \
  184. if (trc_out != NULL)
  185. # define OSSL_TRACE_END(category) \
  186. OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
  187. } while (0)
  188. # define OSSL_TRACE_CANCEL(category) \
  189. OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
  190. # else
  191. # define OSSL_TRACE_BEGIN(category) \
  192. do { \
  193. BIO *trc_out = NULL; \
  194. if (0)
  195. # define OSSL_TRACE_END(category) \
  196. } while(0)
  197. # define OSSL_TRACE_CANCEL(category) \
  198. ((void)0)
  199. # endif
  200. /*
  201. * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
  202. *
  203. * Usage:
  204. *
  205. * if (OSSL_TRACE_ENABLED(TLS)) {
  206. * ...
  207. * }
  208. */
  209. # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
  210. # define OSSL_TRACE_ENABLED(category) \
  211. OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
  212. # else
  213. # define OSSL_TRACE_ENABLED(category) (0)
  214. # endif
  215. /*
  216. * OSSL_TRACE*() - OneShot Trace Macros
  217. *
  218. * These macros are intended to produce a simple printf-style trace output.
  219. * Unfortunately, C90 macros don't support variable arguments, so the
  220. * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
  221. *
  222. * OSSL_TRACEV(category, (trc_out, "format string", ...args...));
  223. *
  224. * Where 'channel' is the literal symbol of this name, not a variable.
  225. * For that reason, it is currently not intended to be used directly,
  226. * but only as helper macro for the other oneshot trace macros
  227. * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
  228. *
  229. * Usage:
  230. *
  231. * OSSL_TRACE(INIT, "Hello world!\n");
  232. * OSSL_TRACE1(TLS, "The answer is %d\n", 42);
  233. * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
  234. * 42, "What do you get when you multiply six by nine?");
  235. */
  236. # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
  237. # define OSSL_TRACEV(category, args) \
  238. OSSL_TRACE_BEGIN(category) \
  239. BIO_printf args; \
  240. OSSL_TRACE_END(category)
  241. # else
  242. # define OSSL_TRACEV(category, args) ((void)0)
  243. # endif
  244. # define OSSL_TRACE(category, text) \
  245. OSSL_TRACEV(category, (trc_out, "%s", text))
  246. # define OSSL_TRACE1(category, format, arg1) \
  247. OSSL_TRACEV(category, (trc_out, format, arg1))
  248. # define OSSL_TRACE2(category, format, arg1, arg2) \
  249. OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
  250. # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
  251. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
  252. # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
  253. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
  254. # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
  255. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
  256. # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
  257. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
  258. # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  259. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
  260. # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  261. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
  262. # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  263. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
  264. # ifdef __cplusplus
  265. }
  266. # endif
  267. #endif