OSSL_trace_set_channel.pod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. =pod
  2. =for openssl foreign manual atexit(3)
  3. =head1 NAME
  4. OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix,
  5. OSSL_trace_set_callback, OSSL_trace_cb - Enabling trace output
  6. =head1 SYNOPSIS
  7. #include <openssl/trace.h>
  8. typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
  9. int category, int cmd, void *data);
  10. void OSSL_trace_set_channel(int category, BIO *bio);
  11. void OSSL_trace_set_prefix(int category, const char *prefix);
  12. void OSSL_trace_set_suffix(int category, const char *suffix);
  13. void OSSL_trace_set_callback(int category, OSSL_trace_cb cb, void *data);
  14. =head1 DESCRIPTION
  15. If available (see L</Configure Tracing> below), the application can request
  16. internal trace output.
  17. This output comes in form of free text for humans to read.
  18. The trace output is divided into categories which can be
  19. enabled individually.
  20. Every category can be enabled individually by attaching a so-called
  21. I<trace channel> to it, which in the simplest case is just a BIO object
  22. to which the application can write the tracing output for this category.
  23. Alternatively, the application can provide a tracer callback in order to
  24. get more finegrained trace information. This callback will be wrapped
  25. internally by a dedicated BIO object.
  26. For the tracing code, both trace channel types are indistinguishable.
  27. These are called a I<simple trace channel> and a I<callback trace channel>,
  28. respectively.
  29. L<OSSL_TRACE_ENABLED(3)> can be used to check whether tracing is currently
  30. enabled for the given category.
  31. Functions like L<OSSL_TRACE1(3)> and macros like L<OSSL_TRACE_BEGIN(3)>
  32. can be used for producing free-text trace output.
  33. =head2 Functions
  34. OSSL_trace_set_channel() is used to enable the given trace C<category>
  35. by attaching the B<BIO> I<bio> object as (simple) trace channel.
  36. On success the ownership of the BIO is transferred to the channel,
  37. so the caller must not free it directly.
  38. OSSL_trace_set_prefix() and OSSL_trace_set_suffix() can be used to add
  39. an extra line for each channel, to be output before and after group of
  40. tracing output.
  41. What constitutes an output group is decided by the code that produces
  42. the output.
  43. The lines given here are considered immutable; for more dynamic
  44. tracing prefixes, consider setting a callback with
  45. OSSL_trace_set_callback() instead.
  46. OSSL_trace_set_callback() is used to enable the given trace
  47. I<category> by giving it the tracer callback I<cb> with the associated
  48. data I<data>, which will simply be passed through to I<cb> whenever
  49. it's called. The callback function is internally wrapped by a
  50. dedicated BIO object, the so-called I<callback trace channel>.
  51. This should be used when it's desirable to do form the trace output to
  52. something suitable for application needs where a prefix and suffix
  53. line aren't enough.
  54. OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
  55. exclusive, calling one of them will clear whatever was set by the
  56. previous call.
  57. Calling OSSL_trace_set_channel() with NULL for I<channel> or
  58. OSSL_trace_set_callback() with NULL for I<cb> disables tracing for
  59. the given I<category>.
  60. =head2 Trace callback
  61. The tracer callback must return a B<size_t>, which must be zero on
  62. error and otherwise return the number of bytes that were output.
  63. It receives a text buffer I<buf> with I<cnt> bytes of text, as well as
  64. the I<category>, a control number I<cmd>, and the I<data> that was
  65. passed to OSSL_trace_set_callback().
  66. The possible control numbers are:
  67. =over 4
  68. =item B<OSSL_TRACE_CTRL_BEGIN>
  69. The callback is called from OSSL_trace_begin(), which gives the
  70. callback the possibility to output a dynamic starting line, or set a
  71. prefix that should be output at the beginning of each line, or
  72. something other.
  73. =item B<OSSL_TRACE_CTRL_WRITE>
  74. This callback is called whenever data is written to the BIO by some
  75. regular BIO output routine.
  76. An arbitrary number of B<OSSL_TRACE_CTRL_WRITE> callbacks can occur
  77. inside a group marked by a pair of B<OSSL_TRACE_CTRL_BEGIN> and
  78. B<OSSL_TRACE_CTRL_END> calls, but never outside such a group.
  79. =item B<OSSL_TRACE_CTRL_END>
  80. The callback is called from OSSL_trace_end(), which gives the callback
  81. the possibility to output a dynamic ending line, or reset the line
  82. prefix that was set with B<OSSL_TRACE_CTRL_BEGIN>, or something other.
  83. =back
  84. =head2 Trace categories
  85. The trace categories are simple numbers available through macros.
  86. =over 4
  87. =item B<OSSL_TRACE_CATEGORY_TRACE>
  88. Traces the OpenSSL trace API itself.
  89. More precisely, this will generate trace output any time a new
  90. trace hook is set.
  91. =item B<OSSL_TRACE_CATEGORY_INIT>
  92. Traces OpenSSL library initialization and cleanup.
  93. This needs special care, as OpenSSL will do automatic cleanup after
  94. exit from C<main()>, and any tracing output done during this cleanup
  95. will be lost if the tracing channel or callback were cleaned away
  96. prematurely.
  97. A suggestion is to make such cleanup part of a function that's
  98. registered very early with L<atexit(3)>.
  99. =item B<OSSL_TRACE_CATEGORY_TLS>
  100. Traces the TLS/SSL protocol.
  101. =item B<OSSL_TRACE_CATEGORY_TLS_CIPHER>
  102. Traces the ciphers used by the TLS/SSL protocol.
  103. =item B<OSSL_TRACE_CATEGORY_CONF>
  104. Traces details about the provider and engine configuration.
  105. =item B<OSSL_TRACE_CATEGORY_ENGINE_TABLE>
  106. Traces the ENGINE algorithm table selection.
  107. More precisely, functions like ENGINE_get_pkey_asn1_meth_engine(),
  108. ENGINE_get_pkey_meth_engine(), ENGINE_get_cipher_engine(),
  109. ENGINE_get_digest_engine(), will generate trace summaries of the
  110. handling of internal tables.
  111. =item B<OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT>
  112. Traces the ENGINE reference counting.
  113. More precisely, both reference counts in the ENGINE structure will be
  114. monitored with a line of trace output generated for each change.
  115. =item B<OSSL_TRACE_CATEGORY_PKCS5V2>
  116. Traces PKCS#5 v2 key generation.
  117. =item B<OSSL_TRACE_CATEGORY_PKCS12_KEYGEN>
  118. Traces PKCS#12 key generation.
  119. =item B<OSSL_TRACE_CATEGORY_PKCS12_DECRYPT>
  120. Traces PKCS#12 decryption.
  121. =item B<OSSL_TRACE_CATEGORY_X509V3_POLICY>
  122. Traces X509v3 policy processing.
  123. More precisely, this generates the complete policy tree at various
  124. point during evaluation.
  125. =item B<OSSL_TRACE_CATEGORY_BN_CTX>
  126. Traces BIGNUM context operations.
  127. =item B<OSSL_TRACE_CATEGORY_CMP>
  128. Traces CMP client and server activity.
  129. =item B<OSSL_TRACE_CATEGORY_STORE>
  130. Traces STORE operations.
  131. =item B<OSSL_TRACE_CATEGORY_DECODER>
  132. Traces decoder operations.
  133. =item B<OSSL_TRACE_CATEGORY_ENCODER>
  134. Traces encoder operations.
  135. =item B<OSSL_TRACE_CATEGORY_REF_COUNT>
  136. Traces decrementing certain ASN.1 structure references.
  137. =item B<OSSL_TRACE_CATEGORY_HTTP>
  138. Traces the HTTP client, such as message headers being sent and received.
  139. =back
  140. There is also B<OSSL_TRACE_CATEGORY_ALL>, which works as a fallback
  141. and can be used to get I<all> trace output.
  142. Note, however, that in this case all trace output will effectively be
  143. associated with the 'ALL' category, which is undesirable if the
  144. application intends to include the category name in the trace output.
  145. In this case it is better to register separate channels for each
  146. trace category instead.
  147. =head1 RETURN VALUES
  148. OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
  149. OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on
  150. success, or 0 on failure.
  151. =head1 EXAMPLES
  152. In all examples below, the trace producing code is assumed to be
  153. the following:
  154. int foo = 42;
  155. const char bar[] = { 0, 1, 2, 3, 4, 5, 6, 7,
  156. 8, 9, 10, 11, 12, 13, 14, 15 };
  157. OSSL_TRACE_BEGIN(TLS) {
  158. BIO_puts(trc_out, "foo: ");
  159. BIO_printf(trc_out, "%d\n", foo);
  160. BIO_dump(trc_out, bar, sizeof(bar));
  161. } OSSL_TRACE_END(TLS);
  162. =head2 Simple example
  163. An example with just a channel and constant prefix / suffix.
  164. int main(int argc, char *argv[])
  165. {
  166. BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  167. OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
  168. OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
  169. OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");
  170. /* ... work ... */
  171. }
  172. When the trace producing code above is performed, this will be output
  173. on standard error:
  174. BEGIN TRACE[TLS]
  175. foo: 42
  176. 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
  177. END TRACE[TLS]
  178. =head2 Advanced example
  179. This example uses the callback, and depends on pthreads functionality.
  180. static size_t cb(const char *buf, size_t cnt,
  181. int category, int cmd, void *vdata)
  182. {
  183. BIO *bio = vdata;
  184. const char *label = NULL;
  185. switch (cmd) {
  186. case OSSL_TRACE_CTRL_BEGIN:
  187. label = "BEGIN";
  188. break;
  189. case OSSL_TRACE_CTRL_END:
  190. label = "END";
  191. break;
  192. }
  193. if (label != NULL) {
  194. union {
  195. pthread_t tid;
  196. unsigned long ltid;
  197. } tid;
  198. tid.tid = pthread_self();
  199. BIO_printf(bio, "%s TRACE[%s]:%lx\n",
  200. label, OSSL_trace_get_category_name(category), tid.ltid);
  201. }
  202. return (size_t)BIO_puts(bio, buf);
  203. }
  204. int main(int argc, char *argv[])
  205. {
  206. BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  207. OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
  208. /* ... work ... */
  209. }
  210. The output is almost the same as for the simple example above.
  211. BEGIN TRACE[TLS]:7f9eb0193b80
  212. foo: 42
  213. 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
  214. END TRACE[TLS]:7f9eb0193b80
  215. =head1 NOTES
  216. =head2 Configure Tracing
  217. By default, the OpenSSL library is built with tracing disabled. To
  218. use the tracing functionality documented here, it is therefore
  219. necessary to configure and build OpenSSL with the 'enable-trace' option.
  220. When the library is built with tracing disabled, the macro
  221. B<OPENSSL_NO_TRACE> is defined in F<< <openssl/opensslconf.h> >> and all
  222. functions described here are inoperational, i.e. will do nothing.
  223. =head1 SEE ALSO
  224. L<OSSL_TRACE_ENABLED(3)>, L<OSSL_TRACE_BEGIN(3)>, L<OSSL_TRACE1(3)>,
  225. L<atexit(3)>
  226. =head1 HISTORY
  227. OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
  228. OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
  229. in OpenSSL 3.0.
  230. =head1 COPYRIGHT
  231. Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  232. Licensed under the Apache License 2.0 (the "License"). You may not use
  233. this file except in compliance with the License. You can obtain a copy
  234. in the file LICENSE in the source distribution or at
  235. L<https://www.openssl.org/source/license.html>.
  236. =cut