Adapt OPENSSL_DEBUG_PKCS5V2 to the new generic trace API
[openssl.git] / crypto / trace.c
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 #include <stdio.h>
11 #include <string.h>
12
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/trace.h>
16 #include "internal/bio.h"
17 #include "internal/nelem.h"
18 #include "internal/cryptlib_int.h"
19
20 #include "e_os.h"                /* strcasecmp for Windows */
21
22 #ifndef OPENSSL_NO_TRACE
23
24 static CRYPTO_RWLOCK *trace_lock = NULL;
25
26 static const BIO  *current_channel = NULL;
27
28 /*-
29  * INTERNAL TRACE CHANNEL IMPLEMENTATION
30  *
31  * For our own flexibility, all trace categories are associated with a
32  * BIO sink object, also called the trace channel. Instead of a BIO object,
33  * the application can also provide a callback function, in which case an
34  * internal trace channel is attached, which simply calls the registered
35  * callback function.
36  */
37 static int trace_write(BIO *b, const char *buf,
38                                size_t num, size_t *written);
39 static int trace_puts(BIO *b, const char *str);
40 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
41 static int trace_free(BIO *b);
42
43 static const BIO_METHOD trace_method = {
44     BIO_TYPE_SOURCE_SINK,
45     "trace",
46     trace_write,
47     NULL,                        /* old write */
48     NULL,                        /* read_ex */
49     NULL,                        /* read */
50     trace_puts,
51     NULL,                        /* gets */
52     trace_ctrl,                  /* ctrl */
53     NULL,                        /* create */
54     trace_free,                  /* free */
55     NULL,                        /* callback_ctrl */
56 };
57
58 struct trace_data_st {
59     OSSL_trace_cb callback;
60     int category;
61     void *data;
62 };
63
64 static int trace_write(BIO *channel,
65                        const char *buf, size_t num, size_t *written)
66 {
67     struct trace_data_st *ctx = BIO_get_data(channel);
68     size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_DURING,
69                                ctx->data);
70
71     *written = cnt;
72     return cnt != 0;
73 }
74
75 static int trace_puts(BIO *channel, const char *str)
76 {
77     size_t written;
78
79     if (trace_write(channel, str, strlen(str), &written))
80         return (int)written;
81
82     return EOF;
83 }
84
85 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
86 {
87     struct trace_data_st *ctx = BIO_get_data(channel);
88
89     switch (cmd) {
90     case OSSL_TRACE_CTRL_BEGIN:
91     case OSSL_TRACE_CTRL_END:
92         /* We know that the callback is likely to return 0 here */
93         ctx->callback("", 0, ctx->category, cmd, ctx->data);
94         return 1;
95     default:
96         break;
97     }
98     return -2;                   /* Unsupported */
99 }
100
101 static int trace_free(BIO *channel)
102 {
103     if (channel == NULL)
104         return 0;
105     OPENSSL_free(BIO_get_data(channel));
106     return 1;
107 }
108 #endif
109
110 /*-
111  * TRACE
112  */
113
114 /* Helper struct and macro to get name string to number mapping */
115 struct trace_category_st {
116     const char * const name;
117     const int num;
118 };
119 #define TRACE_CATEGORY_(name)       { #name, OSSL_TRACE_CATEGORY_##name }
120
121 static const struct trace_category_st trace_categories[] = {
122     TRACE_CATEGORY_(ANY),
123     TRACE_CATEGORY_(INIT),
124     TRACE_CATEGORY_(TLS),
125     TRACE_CATEGORY_(TLS_CIPHER),
126     TRACE_CATEGORY_(ENGINE_CONF),
127     TRACE_CATEGORY_(ENGINE_TABLE),
128     TRACE_CATEGORY_(ENGINE_REF_COUNT),
129     TRACE_CATEGORY_(PKCS5V2),
130 };
131
132 const char *OSSL_trace_get_category_name(int num)
133 {
134     size_t i;
135
136     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
137         if (trace_categories[i].num == num)
138             return trace_categories[i].name;
139     return NULL; /* not found */
140 }
141
142 int OSSL_trace_get_category_num(const char *name)
143 {
144     size_t i;
145
146     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
147         if (strcasecmp(name, trace_categories[i].name) == 0)
148             return trace_categories[i].num;
149     return -1; /* not found */
150 }
151
152 #ifndef OPENSSL_NO_TRACE
153
154 /* We use one trace channel for each trace category */
155 static struct {
156     enum { t_channel, t_callback } type;
157     BIO *bio;
158     char *prefix;
159     char *suffix;
160 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
161     { 0, NULL, NULL, NULL },
162 };
163
164 #endif
165
166 int ossl_trace_init(void)
167 {
168 #ifndef OPENSSL_NO_TRACE
169     trace_lock = CRYPTO_THREAD_lock_new();
170     if (trace_lock != NULL)
171         return 1;
172 #endif
173
174     return 0;
175 }
176
177 void ossl_trace_cleanup(void)
178 {
179 #ifndef OPENSSL_NO_TRACE
180     int category;
181
182     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
183         OSSL_trace_set_channel(category, NULL);
184     CRYPTO_THREAD_lock_free(trace_lock);
185 #endif
186 }
187
188 int OSSL_trace_set_channel(int category, BIO *channel)
189 {
190 #ifndef OPENSSL_NO_TRACE
191     BIO *prev_channel;
192
193     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
194         goto err;
195
196     prev_channel = trace_channels[category].bio;
197
198     if (prev_channel != NULL) {
199         BIO_free(prev_channel);
200         trace_channels[category].bio = NULL;
201     }
202
203     if (channel == NULL)
204         return 1;                /* Done */
205
206     trace_channels[category].bio = channel;
207     trace_channels[category].type = t_channel;
208
209     return 1;
210
211  err:
212 #endif
213
214     return 0;
215 }
216
217 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
218 {
219 #ifndef OPENSSL_NO_TRACE
220     BIO *channel = trace_channels[category].bio;
221     struct trace_data_st *trace_data = NULL;
222
223     if (channel != NULL) {
224         BIO_free(channel);
225         trace_channels[category].bio = NULL;
226     }
227
228     if (callback == NULL)
229         return 1; /* done */
230
231     channel = BIO_new(&trace_method);
232     if (channel == NULL)
233         goto err;
234
235     trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
236     if (trace_data == NULL)
237         goto err;
238
239     trace_data->callback = callback;
240     trace_data->category = category;
241     trace_data->data = data;
242
243     BIO_set_data(channel, trace_data);
244
245     trace_channels[category].bio = channel;
246     trace_channels[category].type = t_callback;
247
248     return 1;
249
250  err:
251     BIO_free(channel);
252     OPENSSL_free(trace_data);
253 #endif
254
255     return 0;
256 }
257
258 int OSSL_trace_set_prefix(int category, const char *prefix)
259 {
260 #ifndef OPENSSL_NO_TRACE
261     char *curr_prefix = trace_channels[category].prefix;
262
263     if (curr_prefix != NULL) {
264         OPENSSL_free(curr_prefix);
265         trace_channels[category].prefix = NULL;
266     }
267
268     if (prefix == NULL)
269         return 1;                /* Done */
270
271     curr_prefix = OPENSSL_strdup(prefix);
272     if (curr_prefix == NULL)
273         goto err;
274
275     trace_channels[category].prefix = curr_prefix;
276
277     return 1;
278
279  err:
280 #endif
281
282     return 0;
283 }
284
285 int OSSL_trace_set_suffix(int category, const char *suffix)
286 {
287 #ifndef OPENSSL_NO_TRACE
288     char *curr_suffix = trace_channels[category].suffix;
289
290     if (curr_suffix != NULL) {
291         OPENSSL_free(curr_suffix);
292         trace_channels[category].suffix = NULL;
293     }
294
295     if (suffix == NULL)
296         return 1; /* done */
297
298     curr_suffix = OPENSSL_strdup(suffix);
299     if (curr_suffix == NULL)
300         goto err;
301
302     trace_channels[category].suffix = curr_suffix;
303
304     return 1;
305
306  err:
307 #endif
308
309     return 0;
310 }
311
312 #ifndef OPENSSL_NO_TRACE
313 static int ossl_trace_get_category(int category)
314 {
315     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
316         return -1;
317     if (trace_channels[category].bio != NULL)
318         return category;
319     return OSSL_TRACE_CATEGORY_ANY;
320 }
321 #endif
322
323 int OSSL_trace_enabled(int category)
324 {
325     int ret = 0;
326 #ifndef OPENSSL_NO_TRACE
327     category = ossl_trace_get_category(category);
328     ret = trace_channels[category].bio != NULL;
329 #endif
330     return ret;
331 }
332
333 BIO *OSSL_trace_begin(int category)
334 {
335     BIO *channel = NULL;
336 #ifndef OPENSSL_NO_TRACE
337     char *prefix = NULL;
338
339     category = ossl_trace_get_category(category);
340     channel = trace_channels[category].bio;
341     prefix = trace_channels[category].prefix;
342
343     if (channel != NULL) {
344         CRYPTO_THREAD_write_lock(trace_lock);
345         current_channel = channel;
346         switch (trace_channels[category].type) {
347         case t_channel:
348             if (prefix != NULL) {
349                 (void)BIO_puts(channel, prefix);
350                 (void)BIO_puts(channel, "\n");
351             }
352             break;
353         case t_callback:
354             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
355                            prefix == NULL ? 0 : strlen(prefix), prefix);
356             break;
357         }
358     }
359 #endif
360     return channel;
361 }
362
363 void OSSL_trace_end(int category, BIO * channel)
364 {
365 #ifndef OPENSSL_NO_TRACE
366     char *suffix = NULL;
367
368     category = ossl_trace_get_category(category);
369     suffix = trace_channels[category].suffix;
370     if (channel != NULL
371         && ossl_assert(channel == current_channel)) {
372         (void)BIO_flush(channel);
373         switch (trace_channels[category].type) {
374         case t_channel:
375             if (suffix != NULL) {
376                 (void)BIO_puts(channel, suffix);
377                 (void)BIO_puts(channel, "\n");
378             }
379             break;
380         case t_callback:
381             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
382                            suffix == NULL ? 0 : strlen(suffix), suffix);
383             break;
384         }
385         current_channel = NULL;
386         CRYPTO_THREAD_unlock(trace_lock);
387     }
388 #endif
389 }