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