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