1 /*
2  * Copyright 2017-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 <string.h>
11 #include <openssl/opensslconf.h>
12 #include <openssl/trace.h>
13 #include "apps.h"
14 #include "../testutil.h"
15 
16 #ifndef OPENSSL_NO_TRACE
17 typedef struct tracedata_st {
18     BIO *bio;
19     unsigned int ingroup:1;
20 } tracedata;
21 
internal_trace_cb(const char * buf,size_t cnt,int category,int cmd,void * vdata)22 static size_t internal_trace_cb(const char *buf, size_t cnt,
23                                 int category, int cmd, void *vdata)
24 {
25     int ret = 0;
26     tracedata *trace_data = vdata;
27     char buffer[256], *hex;
28     CRYPTO_THREAD_ID tid;
29 
30     switch (cmd) {
31     case OSSL_TRACE_CTRL_BEGIN:
32         trace_data->ingroup = 1;
33 
34         tid = CRYPTO_THREAD_get_current_id();
35         hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
36         BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
37                      hex, OSSL_trace_get_category_name(category));
38         OPENSSL_free(hex);
39         BIO_set_prefix(trace_data->bio, buffer);
40         break;
41     case OSSL_TRACE_CTRL_WRITE:
42         if (cnt > INT_MAX)
43             cnt = INT_MAX;
44         ret = BIO_write(trace_data->bio, buf, (int)cnt);
45         break;
46     case OSSL_TRACE_CTRL_END:
47         trace_data->ingroup = 0;
48 
49         BIO_set_prefix(trace_data->bio, NULL);
50         break;
51     }
52 
53     return ret < 0 ? 0 : ret;
54 }
55 
56 DEFINE_STACK_OF(tracedata)
57 static STACK_OF(tracedata) *trace_data_stack;
58 
tracedata_free(tracedata * data)59 static void tracedata_free(tracedata *data)
60 {
61     BIO_free_all(data->bio);
62     OPENSSL_free(data);
63 }
64 
STACK_OF(tracedata)65 static STACK_OF(tracedata) *trace_data_stack;
66 
67 static void cleanup_trace(void)
68 {
69     sk_tracedata_pop_free(trace_data_stack, tracedata_free);
70 }
71 
setup_trace_category(int category)72 static void setup_trace_category(int category)
73 {
74     BIO *channel;
75     tracedata *trace_data;
76     BIO *bio = NULL;
77 
78     if (OSSL_trace_enabled(category))
79         return;
80 
81     bio = BIO_new(BIO_f_prefix());
82     channel = BIO_push(bio,
83                        BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
84     trace_data = OPENSSL_zalloc(sizeof(*trace_data));
85 
86     if (trace_data == NULL
87         || bio == NULL
88         || (trace_data->bio = channel) == NULL
89         || OSSL_trace_set_callback(category, internal_trace_cb,
90                                    trace_data) == 0
91         || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
92 
93         fprintf(stderr,
94                 "warning: unable to setup trace callback for category '%s'.\n",
95                 OSSL_trace_get_category_name(category));
96 
97         OPENSSL_free(trace_data);
98         OSSL_trace_set_callback(category, NULL, NULL);
99         BIO_free_all(channel);
100     }
101 }
102 
setup_trace(const char * str)103 static void setup_trace(const char *str)
104 {
105     char *val;
106 
107     /*
108      * We add this handler as early as possible to ensure it's executed
109      * as late as possible, i.e. after the TRACE code has done its cleanup
110      * (which happens last in OPENSSL_cleanup).
111      */
112     atexit(cleanup_trace);
113 
114     trace_data_stack = sk_tracedata_new_null();
115     val = OPENSSL_strdup(str);
116 
117     if (val != NULL) {
118         char *valp = val;
119         char *item;
120 
121         for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
122             int category = OSSL_trace_get_category_num(item);
123 
124             if (category == OSSL_TRACE_CATEGORY_ALL) {
125                 while (++category < OSSL_TRACE_CATEGORY_NUM)
126                     setup_trace_category(category);
127                 break;
128             } else if (category > 0) {
129                 setup_trace_category(category);
130             } else {
131                 fprintf(stderr,
132                         "warning: unknown trace category: '%s'.\n", item);
133             }
134         }
135     }
136 
137     OPENSSL_free(val);
138 }
139 #endif /* OPENSSL_NO_TRACE */
140 
global_init(void)141 int global_init(void)
142 {
143 #ifndef OPENSSL_NO_TRACE
144     setup_trace(getenv("OPENSSL_TRACE"));
145 #endif
146 
147     return 1;
148 }
149