DECODER: Add tracing
[openssl.git] / crypto / trace.c
1 /*
2  * Copyright 2019-2020 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 "crypto/cryptlib.h"
20
21 #include "e_os.h"                /* strcasecmp for Windows */
22
23 #ifndef OPENSSL_NO_TRACE
24
25 static CRYPTO_RWLOCK *trace_lock = NULL;
26
27 static const BIO  *current_channel = NULL;
28
29 /*-
30  * INTERNAL TRACE CHANNEL IMPLEMENTATION
31  *
32  * For our own flexibility, all trace categories are associated with a
33  * BIO sink object, also called the trace channel. Instead of a BIO object,
34  * the application can also provide a callback function, in which case an
35  * internal trace channel is attached, which simply calls the registered
36  * callback function.
37  */
38 static int trace_write(BIO *b, const char *buf,
39                                size_t num, size_t *written);
40 static int trace_puts(BIO *b, const char *str);
41 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
42 static int trace_free(BIO *b);
43
44 static const BIO_METHOD trace_method = {
45     BIO_TYPE_SOURCE_SINK,
46     "trace",
47     trace_write,
48     NULL,                        /* old write */
49     NULL,                        /* read_ex */
50     NULL,                        /* read */
51     trace_puts,
52     NULL,                        /* gets */
53     trace_ctrl,                  /* ctrl */
54     NULL,                        /* create */
55     trace_free,                  /* free */
56     NULL,                        /* callback_ctrl */
57 };
58
59 struct trace_data_st {
60     OSSL_trace_cb callback;
61     int category;
62     void *data;
63 };
64
65 static int trace_write(BIO *channel,
66                        const char *buf, size_t num, size_t *written)
67 {
68     struct trace_data_st *ctx = BIO_get_data(channel);
69     size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
70                                ctx->data);
71
72     *written = cnt;
73     return cnt != 0;
74 }
75
76 static int trace_puts(BIO *channel, const char *str)
77 {
78     size_t written;
79
80     if (trace_write(channel, str, strlen(str), &written))
81         return (int)written;
82
83     return EOF;
84 }
85
86 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
87 {
88     struct trace_data_st *ctx = BIO_get_data(channel);
89
90     switch (cmd) {
91     case OSSL_TRACE_CTRL_BEGIN:
92     case OSSL_TRACE_CTRL_END:
93         /* We know that the callback is likely to return 0 here */
94         ctx->callback("", 0, ctx->category, cmd, ctx->data);
95         return 1;
96     default:
97         break;
98     }
99     return -2;                   /* Unsupported */
100 }
101
102 static int trace_free(BIO *channel)
103 {
104     if (channel == NULL)
105         return 0;
106     OPENSSL_free(BIO_get_data(channel));
107     return 1;
108 }
109 #endif
110
111 /*-
112  * TRACE
113  */
114
115 /* Helper struct and macro to get name string to number mapping */
116 struct trace_category_st {
117     const char * const name;
118     const int num;
119 };
120 #define TRACE_CATEGORY_(name)       { #name, OSSL_TRACE_CATEGORY_##name }
121
122 static const struct trace_category_st trace_categories[] = {
123     TRACE_CATEGORY_(ALL),
124     TRACE_CATEGORY_(TRACE),
125     TRACE_CATEGORY_(INIT),
126     TRACE_CATEGORY_(TLS),
127     TRACE_CATEGORY_(TLS_CIPHER),
128     TRACE_CATEGORY_(CONF),
129 #ifndef OPENSSL_NO_ENGINE
130     TRACE_CATEGORY_(ENGINE_TABLE),
131     TRACE_CATEGORY_(ENGINE_REF_COUNT),
132 #endif
133     TRACE_CATEGORY_(PKCS5V2),
134     TRACE_CATEGORY_(PKCS12_KEYGEN),
135     TRACE_CATEGORY_(PKCS12_DECRYPT),
136     TRACE_CATEGORY_(X509V3_POLICY),
137     TRACE_CATEGORY_(BN_CTX),
138     TRACE_CATEGORY_(STORE),
139     TRACE_CATEGORY_(DECODER),
140 };
141
142 const char *OSSL_trace_get_category_name(int num)
143 {
144     size_t i;
145
146     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
147         if (trace_categories[i].num == num)
148             return trace_categories[i].name;
149     return NULL; /* not found */
150 }
151
152 int OSSL_trace_get_category_num(const char *name)
153 {
154     size_t i;
155
156     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
157         if (strcasecmp(name, trace_categories[i].name) == 0)
158             return trace_categories[i].num;
159     return -1; /* not found */
160 }
161
162 #ifndef OPENSSL_NO_TRACE
163
164 /* We use one trace channel for each trace category */
165 static struct {
166     enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
167     BIO *bio;
168     char *prefix;
169     char *suffix;
170 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
171     { 0, NULL, NULL, NULL },
172 };
173
174 #endif
175
176 #ifndef OPENSSL_NO_TRACE
177
178 enum {
179     CHANNEL,
180     PREFIX,
181     SUFFIX
182 };
183
184 static int trace_attach_cb(int category, int type, const void *data)
185 {
186     switch (type) {
187     case CHANNEL:
188         OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
189                     data, trace_categories[category].name);
190         break;
191     case PREFIX:
192         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
193                     (const char *)data, trace_categories[category].name);
194         break;
195     case SUFFIX:
196         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
197                     (const char *)data, trace_categories[category].name);
198         break;
199     default:                     /* No clue */
200         break;
201     }
202     return 1;
203 }
204
205 static int trace_detach_cb(int category, int type, const void *data)
206 {
207     switch (type) {
208     case CHANNEL:
209         OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
210                     data, trace_categories[category].name);
211         break;
212     case PREFIX:
213         OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
214                     (const char *)data, trace_categories[category].name);
215         break;
216     case SUFFIX:
217         OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
218                     (const char *)data, trace_categories[category].name);
219         break;
220     default:                     /* No clue */
221         break;
222     }
223     return 1;
224 }
225
226 static int do_ossl_trace_init(void);
227 static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
228 DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
229 {
230     return do_ossl_trace_init();
231 }
232
233 static int set_trace_data(int category, int type, BIO **channel,
234                           const char **prefix, const char **suffix,
235                           int (*attach_cb)(int, int, const void *),
236                           int (*detach_cb)(int, int, const void *))
237 {
238     BIO *curr_channel = NULL;
239     char *curr_prefix = NULL;
240     char *curr_suffix = NULL;
241
242     /* Ensure do_ossl_trace_init() is called once */
243     if (!RUN_ONCE(&trace_inited, ossl_trace_init))
244         return 0;
245
246     curr_channel = trace_channels[category].bio;
247     curr_prefix = trace_channels[category].prefix;
248     curr_suffix = trace_channels[category].suffix;
249
250     /* Make sure to run the detach callback first on all data */
251     if (prefix != NULL && curr_prefix != NULL) {
252         detach_cb(category, PREFIX, curr_prefix);
253     }
254
255     if (suffix != NULL && curr_suffix != NULL) {
256         detach_cb(category, SUFFIX, curr_suffix);
257     }
258
259     if (channel != NULL && curr_channel != NULL) {
260         detach_cb(category, CHANNEL, curr_channel);
261     }
262
263     /* After detach callbacks are done, clear data where appropriate */
264     if (prefix != NULL && curr_prefix != NULL) {
265         OPENSSL_free(curr_prefix);
266         trace_channels[category].prefix = NULL;
267     }
268
269     if (suffix != NULL && curr_suffix != NULL) {
270         OPENSSL_free(curr_suffix);
271         trace_channels[category].suffix = NULL;
272     }
273
274     if (channel != NULL && curr_channel != NULL) {
275         BIO_free(curr_channel);
276         trace_channels[category].type = 0;
277         trace_channels[category].bio = NULL;
278     }
279
280     /* Before running callbacks are done, set new data where appropriate */
281     if (channel != NULL && *channel != NULL) {
282         trace_channels[category].type = type;
283         trace_channels[category].bio = *channel;
284     }
285
286     if (prefix != NULL && *prefix != NULL) {
287         if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
288             return 0;
289         trace_channels[category].prefix = curr_prefix;
290     }
291
292     if (suffix != NULL && *suffix != NULL) {
293         if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
294             return 0;
295         trace_channels[category].suffix = curr_suffix;
296     }
297
298     /* Finally, run the attach callback on the new data */
299     if (channel != NULL && *channel != NULL) {
300         attach_cb(category, CHANNEL, *channel);
301     }
302
303     if (prefix != NULL && *prefix != NULL) {
304         attach_cb(category, PREFIX, *prefix);
305     }
306
307     if (suffix != NULL && *suffix != NULL) {
308         attach_cb(category, SUFFIX, *suffix);
309     }
310
311     return 1;
312 }
313
314 static int do_ossl_trace_init(void)
315 {
316     trace_lock = CRYPTO_THREAD_lock_new();
317     return trace_lock != NULL;
318 }
319
320 #endif
321
322 void ossl_trace_cleanup(void)
323 {
324 #ifndef OPENSSL_NO_TRACE
325     int category;
326     BIO *channel = NULL;
327     const char *prefix = NULL;
328     const char *suffix = NULL;
329
330     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
331         /* We force the TRACE category to be treated last */
332         if (category == OSSL_TRACE_CATEGORY_TRACE)
333             continue;
334         set_trace_data(category, 0, &channel, &prefix, &suffix,
335                        trace_attach_cb, trace_detach_cb);
336     }
337     set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
338                    &prefix, &suffix,
339                    trace_attach_cb, trace_detach_cb);
340     CRYPTO_THREAD_lock_free(trace_lock);
341 #endif
342 }
343
344 int OSSL_trace_set_channel(int category, BIO *channel)
345 {
346 #ifndef OPENSSL_NO_TRACE
347     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
348         return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
349                               trace_attach_cb, trace_detach_cb);
350 #endif
351     return 0;
352 }
353
354 #ifndef OPENSSL_NO_TRACE
355 static int trace_attach_w_callback_cb(int category, int type, const void *data)
356 {
357     switch (type) {
358     case CHANNEL:
359         OSSL_TRACE2(TRACE,
360                     "Attach channel %p to category '%s' (with callback)\n",
361                     data, trace_categories[category].name);
362         break;
363     case PREFIX:
364         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
365                     (const char *)data, trace_categories[category].name);
366         break;
367     case SUFFIX:
368         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
369                     (const char *)data, trace_categories[category].name);
370         break;
371     default:                     /* No clue */
372         break;
373     }
374     return 1;
375 }
376 #endif
377
378 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
379 {
380 #ifndef OPENSSL_NO_TRACE
381     BIO *channel = NULL;
382     struct trace_data_st *trace_data = NULL;
383
384     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
385         return 0;
386
387     if (callback != NULL) {
388         if ((channel = BIO_new(&trace_method)) == NULL
389             || (trace_data =
390                 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
391             goto err;
392
393         trace_data->callback = callback;
394         trace_data->category = category;
395         trace_data->data = data;
396
397         BIO_set_data(channel, trace_data);
398     }
399
400     if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
401                         trace_attach_w_callback_cb, trace_detach_cb))
402         goto err;
403
404     return 1;
405
406  err:
407     BIO_free(channel);
408     OPENSSL_free(trace_data);
409 #endif
410
411     return 0;
412 }
413
414 int OSSL_trace_set_prefix(int category, const char *prefix)
415 {
416 #ifndef OPENSSL_NO_TRACE
417     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
418         return set_trace_data(category, 0, NULL, &prefix, NULL,
419                               trace_attach_cb, trace_detach_cb);
420 #endif
421     return 0;
422 }
423
424 int OSSL_trace_set_suffix(int category, const char *suffix)
425 {
426 #ifndef OPENSSL_NO_TRACE
427     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
428         return set_trace_data(category, 0, NULL, NULL, &suffix,
429                               trace_attach_cb, trace_detach_cb);
430 #endif
431     return 0;
432 }
433
434 #ifndef OPENSSL_NO_TRACE
435 static int ossl_trace_get_category(int category)
436 {
437     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
438         return -1;
439     if (trace_channels[category].bio != NULL)
440         return category;
441     return OSSL_TRACE_CATEGORY_ALL;
442 }
443 #endif
444
445 int OSSL_trace_enabled(int category)
446 {
447     int ret = 0;
448 #ifndef OPENSSL_NO_TRACE
449     category = ossl_trace_get_category(category);
450     if (category >= 0)
451         ret = trace_channels[category].bio != NULL;
452 #endif
453     return ret;
454 }
455
456 BIO *OSSL_trace_begin(int category)
457 {
458     BIO *channel = NULL;
459 #ifndef OPENSSL_NO_TRACE
460     char *prefix = NULL;
461
462     category = ossl_trace_get_category(category);
463     if (category < 0)
464         return NULL;
465
466     channel = trace_channels[category].bio;
467     prefix = trace_channels[category].prefix;
468
469     if (channel != NULL) {
470         CRYPTO_THREAD_write_lock(trace_lock);
471         current_channel = channel;
472         switch (trace_channels[category].type) {
473         case SIMPLE_CHANNEL:
474             if (prefix != NULL) {
475                 (void)BIO_puts(channel, prefix);
476                 (void)BIO_puts(channel, "\n");
477             }
478             break;
479         case CALLBACK_CHANNEL:
480             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
481                            prefix == NULL ? 0 : strlen(prefix), prefix);
482             break;
483         }
484     }
485 #endif
486     return channel;
487 }
488
489 void OSSL_trace_end(int category, BIO * channel)
490 {
491 #ifndef OPENSSL_NO_TRACE
492     char *suffix = NULL;
493
494     category = ossl_trace_get_category(category);
495     suffix = trace_channels[category].suffix;
496     if (channel != NULL
497         && ossl_assert(channel == current_channel)) {
498         (void)BIO_flush(channel);
499         switch (trace_channels[category].type) {
500         case SIMPLE_CHANNEL:
501             if (suffix != NULL) {
502                 (void)BIO_puts(channel, suffix);
503                 (void)BIO_puts(channel, "\n");
504             }
505             break;
506         case CALLBACK_CHANNEL:
507             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
508                            suffix == NULL ? 0 : strlen(suffix), suffix);
509             break;
510         }
511         current_channel = NULL;
512         CRYPTO_THREAD_unlock(trace_lock);
513     }
514 #endif
515 }