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