trace.h 10 KB

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