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