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