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