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