You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

trace.h 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2019-2021 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. {
  17. #endif
  18. /*
  19. * TRACE CATEGORIES
  20. */
  21. /*
  22. * The trace messages of the OpenSSL libraries are organized into different
  23. * categories. For every trace category, the application can register a separate
  24. * tracer callback. When a callback is registered, a so called trace channel is
  25. * created for this category. This channel consists essentially of an internal
  26. * BIO which sends all trace output it receives to the registered application
  27. * callback.
  28. *
  29. * The ALL category can be used as a fallback category to register a single
  30. * channel which receives the output from all categories. However, if the
  31. * application intends to print the trace channel name in the line prefix,
  32. * it is better to register channels for all categories separately.
  33. * (This is how the openssl application does it.)
  34. */
  35. #define OSSL_TRACE_CATEGORY_ALL 0 /* The fallback */
  36. #define OSSL_TRACE_CATEGORY_TRACE 1
  37. #define OSSL_TRACE_CATEGORY_INIT 2
  38. #define OSSL_TRACE_CATEGORY_TLS 3
  39. #define OSSL_TRACE_CATEGORY_TLS_CIPHER 4
  40. #define OSSL_TRACE_CATEGORY_CONF 5
  41. #ifndef OPENSSL_NO_ENGINE
  42. #define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6
  43. #define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7
  44. #endif
  45. #define OSSL_TRACE_CATEGORY_PKCS5V2 8
  46. #define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 9
  47. #define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 10
  48. #define OSSL_TRACE_CATEGORY_X509V3_POLICY 11
  49. #define OSSL_TRACE_CATEGORY_BN_CTX 12
  50. #define OSSL_TRACE_CATEGORY_CMP 13
  51. #define OSSL_TRACE_CATEGORY_STORE 14
  52. #define OSSL_TRACE_CATEGORY_DECODER 15
  53. #define OSSL_TRACE_CATEGORY_ENCODER 16
  54. #define OSSL_TRACE_CATEGORY_REF_COUNT 17
  55. /* Count of available categories. */
  56. #define OSSL_TRACE_CATEGORY_NUM 18
  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, 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. { \
  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. { \
  198. BIO* trc_out = NULL; \
  199. if (0)
  200. #define OSSL_TRACE_END(category) \
  201. } \
  202. while (0)
  203. #define OSSL_TRACE_CANCEL(category) \
  204. ((void)0)
  205. #endif
  206. /*
  207. * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
  208. *
  209. * Usage:
  210. *
  211. * if (OSSL_TRACE_ENABLED(TLS)) {
  212. * ...
  213. * }
  214. */
  215. #if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
  216. #define OSSL_TRACE_ENABLED(category) \
  217. OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
  218. #else
  219. #define OSSL_TRACE_ENABLED(category) (0)
  220. #endif
  221. /*
  222. * OSSL_TRACE*() - OneShot Trace Macros
  223. *
  224. * These macros are intended to produce a simple printf-style trace output.
  225. * Unfortunately, C90 macros don't support variable arguments, so the
  226. * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
  227. *
  228. * OSSL_TRACEV(category, (trc_out, "format string", ...args...));
  229. *
  230. * Where 'channel' is the literal symbol of this name, not a variable.
  231. * For that reason, it is currently not intended to be used directly,
  232. * but only as helper macro for the other oneshot trace macros
  233. * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
  234. *
  235. * Usage:
  236. *
  237. * OSSL_TRACE(INIT, "Hello world!\n");
  238. * OSSL_TRACE1(TLS, "The answer is %d\n", 42);
  239. * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
  240. * 42, "What do you get when you multiply six by nine?");
  241. */
  242. #if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
  243. #define OSSL_TRACEV(category, args) \
  244. OSSL_TRACE_BEGIN(category) \
  245. BIO_printf args; \
  246. OSSL_TRACE_END(category)
  247. #else
  248. #define OSSL_TRACEV(category, args) ((void)0)
  249. #endif
  250. #define OSSL_TRACE(category, text) \
  251. OSSL_TRACEV(category, (trc_out, "%s", text))
  252. #define OSSL_TRACE1(category, format, arg1) \
  253. OSSL_TRACEV(category, (trc_out, format, arg1))
  254. #define OSSL_TRACE2(category, format, arg1, arg2) \
  255. OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
  256. #define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
  257. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
  258. #define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
  259. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
  260. #define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
  261. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
  262. #define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
  263. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
  264. #define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  265. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
  266. #define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  267. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
  268. #define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  269. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif