xref: /openssl/include/openssl/trace.h (revision e8fdb060)
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 
10 #ifndef OPENSSL_TRACE_H
11 # define OPENSSL_TRACE_H
12 # pragma once
13 
14 # include <stdarg.h>
15 
16 # include <openssl/bio.h>
17 
18 # ifdef  __cplusplus
19 extern "C" {
20 # endif
21 
22 /*
23  * TRACE CATEGORIES
24  */
25 
26 /*
27  * The trace messages of the OpenSSL libraries are organized into different
28  * categories. For every trace category, the application can register a separate
29  * tracer callback. When a callback is registered, a so called trace channel is
30  * created for this category. This channel consists essentially of an internal
31  * BIO which sends all trace output it receives to the registered application
32  * callback.
33  *
34  * The ALL category can be used as a fallback category to register a single
35  * channel which receives the output from all categories. However, if the
36  * application intends to print the trace channel name in the line prefix,
37  * it is better to register channels for all categories separately.
38  * (This is how the openssl application does it.)
39  */
40 # define OSSL_TRACE_CATEGORY_ALL                 0 /* The fallback */
41 # define OSSL_TRACE_CATEGORY_TRACE               1
42 # define OSSL_TRACE_CATEGORY_INIT                2
43 # define OSSL_TRACE_CATEGORY_TLS                 3
44 # define OSSL_TRACE_CATEGORY_TLS_CIPHER          4
45 # define OSSL_TRACE_CATEGORY_CONF                5
46 # ifndef OPENSSL_NO_ENGINE
47 #  define OSSL_TRACE_CATEGORY_ENGINE_TABLE       6
48 #  define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT   7
49 # endif
50 # define OSSL_TRACE_CATEGORY_PKCS5V2             8
51 # define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN       9
52 # define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT     10
53 # define OSSL_TRACE_CATEGORY_X509V3_POLICY      11
54 # define OSSL_TRACE_CATEGORY_BN_CTX             12
55 # define OSSL_TRACE_CATEGORY_CMP                13
56 # define OSSL_TRACE_CATEGORY_STORE              14
57 # define OSSL_TRACE_CATEGORY_DECODER            15
58 # define OSSL_TRACE_CATEGORY_ENCODER            16
59 # define OSSL_TRACE_CATEGORY_REF_COUNT          17
60 # define OSSL_TRACE_CATEGORY_HTTP               18
61 /* Count of available categories. */
62 # define OSSL_TRACE_CATEGORY_NUM                19
63 /* KEEP THIS LIST IN SYNC with trace_categories[] in crypto/trace.c */
64 
65 /* Returns the trace category number for the given |name| */
66 int OSSL_trace_get_category_num(const char *name);
67 
68 /* Returns the trace category name for the given |num| */
69 const char *OSSL_trace_get_category_name(int num);
70 
71 /*
72  * TRACE CONSUMERS
73  */
74 
75 /*
76  * Enables tracing for the given |category| by providing a BIO sink
77  * as |channel|. If a null pointer is passed as |channel|, an existing
78  * trace channel is removed and tracing for the category is disabled.
79  *
80  * Returns 1 on success and 0 on failure
81  */
82 int OSSL_trace_set_channel(int category, BIO* channel);
83 
84 /*
85  * Attach a prefix and a suffix to the given |category|, to be printed at the
86  * beginning and at the end of each trace output group, i.e. when
87  * OSSL_trace_begin() and OSSL_trace_end() are called.
88  * If a null pointer is passed as argument, the existing prefix or suffix is
89  * removed.
90  *
91  * They return 1 on success and 0 on failure
92  */
93 int OSSL_trace_set_prefix(int category, const char *prefix);
94 int OSSL_trace_set_suffix(int category, const char *suffix);
95 
96 /*
97  * OSSL_trace_cb is the type tracing callback provided by the application.
98  * It MUST return the number of bytes written, or 0 on error (in other words,
99  * it can never write zero bytes).
100  *
101  * The |buffer| will always contain text, which may consist of several lines.
102  * The |data| argument points to whatever data was provided by the application
103  * when registering the tracer function.
104  *
105  * The |category| number is given, as well as a |cmd| number, described below.
106  */
107 typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
108                                 int category, int cmd, void *data);
109 /*
110  * Possible |cmd| numbers.
111  */
112 # define OSSL_TRACE_CTRL_BEGIN  0
113 # define OSSL_TRACE_CTRL_WRITE  1
114 # define OSSL_TRACE_CTRL_END    2
115 
116 /*
117  * Enables tracing for the given |category| by creating an internal
118  * trace channel which sends the output to the given |callback|.
119  * If a null pointer is passed as callback, an existing trace channel
120  * is removed and tracing for the category is disabled.
121  *
122  * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
123  *       exclusive.
124  *
125  * Returns 1 on success and 0 on failure
126  */
127 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
128 
129 /*
130  * TRACE PRODUCERS
131  */
132 
133 /*
134  * Returns 1 if tracing for the specified category is enabled, otherwise 0
135  */
136 int OSSL_trace_enabled(int category);
137 
138 /*
139  * Wrap a group of tracing output calls.  OSSL_trace_begin() locks tracing and
140  * returns the trace channel associated with the given category, or NULL if no
141  * channel is associated with the category.  OSSL_trace_end() unlocks tracing.
142  *
143  * Usage:
144  *
145  *    BIO *out;
146  *    if ((out = OSSL_trace_begin(category)) != NULL) {
147  *        ...
148  *        BIO_fprintf(out, ...);
149  *        ...
150  *        OSSL_trace_end(category, out);
151  *    }
152  *
153  * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
154  */
155 BIO *OSSL_trace_begin(int category);
156 void OSSL_trace_end(int category, BIO *channel);
157 
158 /*
159  * OSSL_TRACE* Convenience Macros
160  */
161 
162 /*
163  * When the tracing feature is disabled, these macros are defined to
164  * produce dead code, which a good compiler should eliminate.
165  */
166 
167 /*
168  * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
169  *
170  * These two macros can be used to create a block which is executed only
171  * if the corresponding trace category is enabled. Inside this block, a
172  * local variable named |trc_out| is defined, which points to the channel
173  * associated with the given trace category.
174  *
175  * Usage: (using 'TLS' as an example category)
176  *
177  *     OSSL_TRACE_BEGIN(TLS) {
178  *
179  *         BIO_fprintf(trc_out, ... );
180  *
181  *     } OSSL_TRACE_END(TLS);
182  *
183  *
184  * This expands to the following code
185  *
186  *     do {
187  *         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
188  *         if (trc_out != NULL) {
189  *             ...
190  *             BIO_fprintf(trc_out, ...);
191  *         }
192  *         OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
193  *     } while (0);
194  *
195  * The use of the inner '{...}' group and the trailing ';' is enforced
196  * by the definition of the macros in order to make the code look as much
197  * like C code as possible.
198  *
199  * Before returning from inside the trace block, it is necessary to
200  * call OSSL_TRACE_CANCEL(category).
201  */
202 
203 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
204 
205 #  define OSSL_TRACE_BEGIN(category) \
206     do { \
207         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
208  \
209         if (trc_out != NULL)
210 
211 #  define OSSL_TRACE_END(category) \
212         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
213     } while (0)
214 
215 #  define OSSL_TRACE_CANCEL(category) \
216         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
217 
218 # else
219 
220 #  define OSSL_TRACE_BEGIN(category)           \
221     do {                                        \
222         BIO *trc_out = NULL;                    \
223         if (0)
224 
225 #  define OSSL_TRACE_END(category)             \
226     } while(0)
227 
228 #  define OSSL_TRACE_CANCEL(category)          \
229     ((void)0)
230 
231 # endif
232 
233 /*
234  * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
235  *
236  * Usage:
237  *
238  *     if (OSSL_TRACE_ENABLED(TLS)) {
239  *         ...
240  *     }
241  */
242 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
243 
244 #  define OSSL_TRACE_ENABLED(category) \
245     OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
246 
247 # else
248 
249 #  define OSSL_TRACE_ENABLED(category) (0)
250 
251 # endif
252 
253 /*
254  * OSSL_TRACE*() - OneShot Trace Macros
255  *
256  * These macros are intended to produce a simple printf-style trace output.
257  * Unfortunately, C90 macros don't support variable arguments, so the
258  * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
259  *
260  *    OSSL_TRACEV(category, (trc_out, "format string", ...args...));
261  *
262  * Where 'channel' is the literal symbol of this name, not a variable.
263  * For that reason, it is currently not intended to be used directly,
264  * but only as helper macro for the other oneshot trace macros
265  * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
266  *
267  * Usage:
268  *
269  *    OSSL_TRACE(INIT, "Hello world!\n");
270  *    OSSL_TRACE1(TLS, "The answer is %d\n", 42);
271  *    OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
272  *                42, "What do you get when you multiply six by nine?");
273  */
274 
275 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
276 
277 #  define OSSL_TRACEV(category, args) \
278     OSSL_TRACE_BEGIN(category) \
279         BIO_printf args; \
280     OSSL_TRACE_END(category)
281 
282 # else
283 
284 #  define OSSL_TRACEV(category, args) ((void)0)
285 
286 # endif
287 
288 # define OSSL_TRACE(category, text) \
289     OSSL_TRACEV(category, (trc_out, "%s", text))
290 
291 # define OSSL_TRACE1(category, format, arg1) \
292     OSSL_TRACEV(category, (trc_out, format, arg1))
293 # define OSSL_TRACE2(category, format, arg1, arg2) \
294     OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
295 # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
296     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
297 # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
298     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
299 # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
300     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
301 # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
302     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
303 # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
304     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
305 # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
306     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
307 # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
308     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
309 
310 # ifdef  __cplusplus
311 }
312 # endif
313 
314 #endif
315