trace.h 11 KB

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