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