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