OSSL_TRACE: ensure it's initialised
[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_WRITE,
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_(ALL),
123     TRACE_CATEGORY_(TRACE),
124     TRACE_CATEGORY_(INIT),
125     TRACE_CATEGORY_(TLS),
126     TRACE_CATEGORY_(TLS_CIPHER),
127     TRACE_CATEGORY_(CONF),
128     TRACE_CATEGORY_(ENGINE_TABLE),
129     TRACE_CATEGORY_(ENGINE_REF_COUNT),
130     TRACE_CATEGORY_(PKCS5V2),
131     TRACE_CATEGORY_(PKCS12_KEYGEN),
132     TRACE_CATEGORY_(PKCS12_DECRYPT),
133     TRACE_CATEGORY_(X509V3_POLICY),
134     TRACE_CATEGORY_(BN_CTX),
135     TRACE_CATEGORY_(PROV),
136     TRACE_CATEGORY_(FETCH),
137 };
138
139 const char *OSSL_trace_get_category_name(int num)
140 {
141     size_t i;
142
143     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
144         if (trace_categories[i].num == num)
145             return trace_categories[i].name;
146     return NULL; /* not found */
147 }
148
149 int OSSL_trace_get_category_num(const char *name)
150 {
151     size_t i;
152
153     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
154         if (strcasecmp(name, trace_categories[i].name) == 0)
155             return trace_categories[i].num;
156     return -1; /* not found */
157 }
158
159 #ifndef OPENSSL_NO_TRACE
160
161 /* We use one trace channel for each trace category */
162 static struct {
163     enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
164     BIO *bio;
165     char *prefix;
166     char *suffix;
167 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
168     { 0, NULL, NULL, NULL },
169 };
170
171 #endif
172
173 #ifndef OPENSSL_NO_TRACE
174
175 enum {
176     CHANNEL,
177     PREFIX,
178     SUFFIX
179 };
180
181 static int trace_attach_cb(int category, int type, const void *data)
182 {
183     switch (type) {
184     case CHANNEL:
185         OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
186                     data, trace_categories[category].name);
187         break;
188     case PREFIX:
189         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
190                     (const char *)data, trace_categories[category].name);
191         break;
192     case SUFFIX:
193         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
194                     (const char *)data, trace_categories[category].name);
195         break;
196     default:                     /* No clue */
197         break;
198     }
199     return 1;
200 }
201
202 static int trace_detach_cb(int category, int type, const void *data)
203 {
204     switch (type) {
205     case CHANNEL:
206         OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
207                     data, trace_categories[category].name);
208         break;
209     case PREFIX:
210         OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
211                     (const char *)data, trace_categories[category].name);
212         break;
213     case SUFFIX:
214         OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
215                     (const char *)data, trace_categories[category].name);
216         break;
217     default:                     /* No clue */
218         break;
219     }
220     return 1;
221 }
222
223 static int set_trace_data(int category, int type, BIO **channel,
224                           const char **prefix, const char **suffix,
225                           int (*attach_cb)(int, int, const void *),
226                           int (*detach_cb)(int, int, const void *))
227 {
228     BIO *curr_channel = NULL;
229     char *curr_prefix = NULL;
230     char *curr_suffix = NULL;
231
232     /* Ensure ossl_trace_init() is called */
233     OPENSSL_init_crypto(0, NULL);
234
235     curr_channel = trace_channels[category].bio;
236     curr_prefix = trace_channels[category].prefix;
237     curr_suffix = trace_channels[category].suffix;
238
239     /* Make sure to run the detach callback first on all data */
240     if (prefix != NULL && curr_prefix != NULL) {
241         detach_cb(category, PREFIX, curr_prefix);
242     }
243
244     if (suffix != NULL && curr_suffix != NULL) {
245         detach_cb(category, SUFFIX, curr_suffix);
246     }
247
248     if (channel != NULL && curr_channel != NULL) {
249         detach_cb(category, CHANNEL, curr_channel);
250     }
251
252     /* After detach callbacks are done, clear data where appropriate */
253     if (prefix != NULL && curr_prefix != NULL) {
254         OPENSSL_free(curr_prefix);
255         trace_channels[category].prefix = NULL;
256     }
257
258     if (suffix != NULL && curr_suffix != NULL) {
259         OPENSSL_free(curr_suffix);
260         trace_channels[category].suffix = NULL;
261     }
262
263     if (channel != NULL && curr_channel != NULL) {
264         BIO_free(curr_channel);
265         trace_channels[category].type = 0;
266         trace_channels[category].bio = NULL;
267     }
268
269     /* Before running callbacks are done, set new data where appropriate */
270     if (channel != NULL && *channel != NULL) {
271         trace_channels[category].type = type;
272         trace_channels[category].bio = *channel;
273     }
274
275     if (prefix != NULL && *prefix != NULL) {
276         if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
277             return 0;
278         trace_channels[category].prefix = curr_prefix;
279     }
280
281     if (suffix != NULL && *suffix != NULL) {
282         if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
283             return 0;
284         trace_channels[category].suffix = curr_suffix;
285     }
286
287     /* Finally, run the attach callback on the new data */
288     if (channel != NULL && *channel != NULL) {
289         attach_cb(category, CHANNEL, *channel);
290     }
291
292     if (prefix != NULL && *prefix != NULL) {
293         attach_cb(category, PREFIX, *prefix);
294     }
295
296     if (suffix != NULL && *suffix != NULL) {
297         attach_cb(category, SUFFIX, *suffix);
298     }
299
300     return 1;
301 }
302 #endif
303
304 int ossl_trace_init(void)
305 {
306 #ifndef OPENSSL_NO_TRACE
307     trace_lock = CRYPTO_THREAD_lock_new();
308     if (trace_lock == NULL)
309         return 0;
310 #endif
311
312     return 1;
313 }
314
315 void ossl_trace_cleanup(void)
316 {
317 #ifndef OPENSSL_NO_TRACE
318     int category;
319     BIO *channel = NULL;
320     const char *prefix = NULL;
321     const char *suffix = NULL;
322
323     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
324         /* We force the TRACE category to be treated last */
325         if (category == OSSL_TRACE_CATEGORY_TRACE)
326             continue;
327         set_trace_data(category, 0, &channel, &prefix, &suffix,
328                        trace_attach_cb, trace_detach_cb);
329     }
330     set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
331                    &prefix, &suffix,
332                    trace_attach_cb, trace_detach_cb);
333     CRYPTO_THREAD_lock_free(trace_lock);
334 #endif
335 }
336
337 int OSSL_trace_set_channel(int category, BIO *channel)
338 {
339 #ifndef OPENSSL_NO_TRACE
340     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
341         return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
342                               trace_attach_cb, trace_detach_cb);
343 #endif
344     return 0;
345 }
346
347 #ifndef OPENSSL_NO_TRACE
348 static int trace_attach_w_callback_cb(int category, int type, const void *data)
349 {
350     switch (type) {
351     case CHANNEL:
352         OSSL_TRACE2(TRACE,
353                     "Attach channel %p to category '%s' (with callback)\n",
354                     data, trace_categories[category].name);
355         break;
356     case PREFIX:
357         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
358                     (const char *)data, trace_categories[category].name);
359         break;
360     case SUFFIX:
361         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
362                     (const char *)data, trace_categories[category].name);
363         break;
364     default:                     /* No clue */
365         break;
366     }
367     return 1;
368 }
369 #endif
370
371 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
372 {
373 #ifndef OPENSSL_NO_TRACE
374     BIO *channel = NULL;
375     struct trace_data_st *trace_data = NULL;
376
377     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
378         return 0;
379
380     if (callback != NULL) {
381         if ((channel = BIO_new(&trace_method)) == NULL
382             || (trace_data =
383                 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
384             goto err;
385
386         trace_data->callback = callback;
387         trace_data->category = category;
388         trace_data->data = data;
389
390         BIO_set_data(channel, trace_data);
391     }
392
393     if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
394                         trace_attach_w_callback_cb, trace_detach_cb))
395         goto err;
396
397     return 1;
398
399  err:
400     BIO_free(channel);
401     OPENSSL_free(trace_data);
402 #endif
403
404     return 0;
405 }
406
407 int OSSL_trace_set_prefix(int category, const char *prefix)
408 {
409 #ifndef OPENSSL_NO_TRACE
410     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
411         return set_trace_data(category, 0, NULL, &prefix, NULL,
412                               trace_attach_cb, trace_detach_cb);
413 #endif
414     return 0;
415 }
416
417 int OSSL_trace_set_suffix(int category, const char *suffix)
418 {
419 #ifndef OPENSSL_NO_TRACE
420     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
421         return set_trace_data(category, 0, NULL, NULL, &suffix,
422                               trace_attach_cb, trace_detach_cb);
423 #endif
424     return 0;
425 }
426
427 #ifndef OPENSSL_NO_TRACE
428 static int ossl_trace_get_category(int category)
429 {
430     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
431         return -1;
432     if (trace_channels[category].bio != NULL)
433         return category;
434     return OSSL_TRACE_CATEGORY_ALL;
435 }
436 #endif
437
438 int OSSL_trace_enabled(int category)
439 {
440     int ret = 0;
441 #ifndef OPENSSL_NO_TRACE
442     category = ossl_trace_get_category(category);
443     if (category >= 0)
444         ret = trace_channels[category].bio != NULL;
445 #endif
446     return ret;
447 }
448
449 BIO *OSSL_trace_begin(int category)
450 {
451     BIO *channel = NULL;
452 #ifndef OPENSSL_NO_TRACE
453     char *prefix = NULL;
454
455     category = ossl_trace_get_category(category);
456     if (category < 0)
457         return NULL;
458
459     channel = trace_channels[category].bio;
460     prefix = trace_channels[category].prefix;
461
462     if (channel != NULL) {
463         CRYPTO_THREAD_write_lock(trace_lock);
464         current_channel = channel;
465         switch (trace_channels[category].type) {
466         case SIMPLE_CHANNEL:
467             if (prefix != NULL) {
468                 (void)BIO_puts(channel, prefix);
469                 (void)BIO_puts(channel, "\n");
470             }
471             break;
472         case CALLBACK_CHANNEL:
473             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
474                            prefix == NULL ? 0 : strlen(prefix), prefix);
475             break;
476         }
477     }
478 #endif
479     return channel;
480 }
481
482 void OSSL_trace_end(int category, BIO * channel)
483 {
484 #ifndef OPENSSL_NO_TRACE
485     char *suffix = NULL;
486
487     category = ossl_trace_get_category(category);
488     suffix = trace_channels[category].suffix;
489     if (channel != NULL
490         && ossl_assert(channel == current_channel)) {
491         (void)BIO_flush(channel);
492         switch (trace_channels[category].type) {
493         case SIMPLE_CHANNEL:
494             if (suffix != NULL) {
495                 (void)BIO_puts(channel, suffix);
496                 (void)BIO_puts(channel, "\n");
497             }
498             break;
499         case CALLBACK_CHANNEL:
500             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
501                            suffix == NULL ? 0 : strlen(suffix), suffix);
502             break;
503         }
504         current_channel = NULL;
505         CRYPTO_THREAD_unlock(trace_lock);
506     }
507 #endif
508 }