apps/openssl.c: Adapt to enable tracing output
[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 static CRYPTO_RWLOCK *trace_lock = NULL;
23
24 static const BIO  *current_channel = NULL;
25
26 /*-
27  * INTERNAL TRACE CHANNEL IMPLEMENTATION
28  *
29  * For our own flexibility, all trace categories are associated with a
30  * BIO sink object, also called the trace channel. Instead of a BIO object,
31  * the application can also provide a callback function, in which case an
32  * internal trace channel is attached, which simply calls the registered
33  * callback function.
34  */
35 static int trace_write(BIO *b, const char *buf,
36                                size_t num, size_t *written);
37 static int trace_puts(BIO *b, const char *str);
38 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
39 static int trace_free(BIO *b);
40
41 static const BIO_METHOD trace_method = {
42     BIO_TYPE_SOURCE_SINK,
43     "trace",
44     trace_write,
45     NULL,                        /* old write */
46     NULL,                        /* read_ex */
47     NULL,                        /* read */
48     trace_puts,
49     NULL,                        /* gets */
50     trace_ctrl,                  /* ctrl */
51     NULL,                        /* create */
52     trace_free,                  /* free */
53     NULL,                        /* callback_ctrl */
54 };
55
56 struct trace_data_st {
57     OSSL_trace_cb callback;
58     int category;
59     void *data;
60 };
61
62 static int trace_write(BIO *channel,
63                        const char *buf, size_t num, size_t *written)
64 {
65     struct trace_data_st *ctx = BIO_get_data(channel);
66     size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_DURING,
67                                ctx->data);
68
69     *written = cnt;
70     return cnt != 0;
71 }
72
73 static int trace_puts(BIO *channel, const char *str)
74 {
75     size_t written;
76
77     if (trace_write(channel, str, strlen(str), &written))
78         return (int)written;
79
80     return EOF;
81 }
82
83 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
84 {
85     struct trace_data_st *ctx = BIO_get_data(channel);
86
87     switch (cmd) {
88     case OSSL_TRACE_CTRL_BEGIN:
89     case OSSL_TRACE_CTRL_END:
90         /* We know that the callback is likely to return 0 here */
91         ctx->callback("", 0, ctx->category, cmd, ctx->data);
92         return 1;
93     default:
94         break;
95     }
96     return -2;                   /* Unsupported */
97 }
98
99 static int trace_free(BIO *channel)
100 {
101     if (channel == NULL)
102         return 0;
103     OPENSSL_free(BIO_get_data(channel));
104     return 1;
105 }
106
107 /*-
108  * TRACE
109  */
110
111 /* Helper struct and macro to get name string to number mapping */
112 struct trace_category_st {
113     const char * const name;
114     const int num;
115 };
116 #define TRACE_CATEGORY_(name)       { #name, OSSL_TRACE_CATEGORY_##name }
117
118 static const struct trace_category_st trace_categories[] = {
119     TRACE_CATEGORY_(ANY),
120 };
121
122 const char *OSSL_trace_get_category_name(int num)
123 {
124     size_t i;
125
126     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
127         if (trace_categories[i].num == num)
128             return trace_categories[i].name;
129     return NULL; /* not found */
130 }
131
132 int OSSL_trace_get_category_num(const char *name)
133 {
134     size_t i;
135
136     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
137         if (strcasecmp(name, trace_categories[i].name) == 0)
138             return trace_categories[i].num;
139     return -1; /* not found */
140 }
141
142 /* We use one trace channel for each trace category */
143 static struct {
144     enum { t_channel, t_callback } type;
145     BIO *bio;
146     char *prefix;
147     char *suffix;
148 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
149     { 0, NULL, NULL, NULL },
150 };
151
152 int ossl_trace_init(void)
153 {
154     trace_lock = CRYPTO_THREAD_lock_new();
155     if (trace_lock == NULL)
156         return 0;
157     return 1;
158 }
159
160 void ossl_trace_cleanup(void)
161 {
162     int category;
163
164     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
165         OSSL_trace_set_channel(category, NULL);
166     CRYPTO_THREAD_lock_free(trace_lock);
167 }
168
169 int OSSL_trace_set_channel(int category, BIO *channel)
170 {
171     BIO *prev_channel;
172
173     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
174         return 0;
175
176     prev_channel = trace_channels[category].bio;
177
178     if (prev_channel != NULL) {
179         BIO_free(prev_channel);
180         trace_channels[category].bio = NULL;
181     }
182
183     if (channel == NULL)
184         return 1; /* done */
185
186     trace_channels[category].bio = channel;
187     trace_channels[category].type = t_channel;
188
189     return 1;
190 }
191
192 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
193 {
194     BIO *channel = trace_channels[category].bio;
195     struct trace_data_st *trace_data = NULL;
196
197     if (channel != NULL) {
198         BIO_free(channel);
199         trace_channels[category].bio = NULL;
200     }
201
202     if (callback == NULL)
203         return 1; /* done */
204
205     channel = BIO_new(&trace_method);
206     if (channel == NULL)
207         goto err;
208
209     trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
210     if (trace_data == NULL)
211         goto err;
212
213     trace_data->callback = callback;
214     trace_data->category = category;
215     trace_data->data = data;
216
217     BIO_set_data(channel, trace_data);
218
219     trace_channels[category].bio = channel;
220     trace_channels[category].type = t_callback;
221
222     return 1;
223
224  err:
225     BIO_free(channel);
226     OPENSSL_free(trace_data);
227
228     return 0;
229 }
230
231 int OSSL_trace_set_prefix(int category, const char *prefix)
232 {
233     char *curr_prefix = trace_channels[category].prefix;
234
235     if (curr_prefix != NULL) {
236         OPENSSL_free(curr_prefix);
237         trace_channels[category].prefix = NULL;
238     }
239
240     if (prefix == NULL)
241         return 1; /* done */
242
243     curr_prefix = OPENSSL_strdup(prefix);
244     if (curr_prefix == NULL)
245         return 0;
246
247     trace_channels[category].prefix = curr_prefix;
248
249     return 1;
250 }
251
252 int OSSL_trace_set_suffix(int category, const char *suffix)
253 {
254     char *curr_suffix = trace_channels[category].suffix;
255
256     if (curr_suffix != NULL) {
257         OPENSSL_free(curr_suffix);
258         trace_channels[category].suffix = NULL;
259     }
260
261     if (suffix == NULL)
262         return 1; /* done */
263
264     curr_suffix = OPENSSL_strdup(suffix);
265     if (curr_suffix == NULL)
266         return 0;
267
268     trace_channels[category].suffix = curr_suffix;
269
270     return 1;
271 }
272
273 static int ossl_trace_get_category(int category)
274 {
275     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
276         return -1;
277     if (trace_channels[category].bio != NULL)
278         return category;
279     return OSSL_TRACE_CATEGORY_ANY;
280 }
281
282 int OSSL_trace_enabled(int category)
283 {
284     int ret = 0;
285     category = ossl_trace_get_category(category);
286     ret = trace_channels[category].bio != NULL;
287     return ret;
288 }
289
290 BIO *OSSL_trace_begin(int category)
291 {
292     BIO *channel = NULL;
293     char *prefix = NULL;
294
295     category = ossl_trace_get_category(category);
296     channel = trace_channels[category].bio;
297     prefix = trace_channels[category].prefix;
298
299     if (channel != NULL) {
300         CRYPTO_THREAD_write_lock(trace_lock);
301         current_channel = channel;
302         switch (trace_channels[category].type) {
303         case t_channel:
304             if (prefix != NULL) {
305                 (void)BIO_puts(channel, prefix);
306                 (void)BIO_puts(channel, "\n");
307             }
308             break;
309         case t_callback:
310             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
311                            prefix == NULL ? 0 : strlen(prefix), prefix);
312             break;
313         }
314     }
315     return channel;
316 }
317
318 void OSSL_trace_end(int category, BIO * channel)
319 {
320     char *suffix = NULL;
321
322     category = ossl_trace_get_category(category);
323     suffix = trace_channels[category].suffix;
324     if (channel != NULL
325         && ossl_assert(channel == current_channel)) {
326         (void)BIO_flush(channel);
327         switch (trace_channels[category].type) {
328         case t_channel:
329             if (suffix != NULL) {
330                 (void)BIO_puts(channel, suffix);
331                 (void)BIO_puts(channel, "\n");
332             }
333             break;
334         case t_callback:
335             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
336                            suffix == NULL ? 0 : strlen(suffix), suffix);
337             break;
338         }
339         current_channel = NULL;
340         CRYPTO_THREAD_unlock(trace_lock);
341     }
342 }