Adapt OPENSSL_INIT_DEBUG to the new generic trace API
[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_(INIT),
124     TRACE_CATEGORY_(TLS),
125     TRACE_CATEGORY_(TLS_CIPHER),
126 };
127
128 const char *OSSL_trace_get_category_name(int num)
129 {
130     size_t i;
131
132     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
133         if (trace_categories[i].num == num)
134             return trace_categories[i].name;
135     return NULL; /* not found */
136 }
137
138 int OSSL_trace_get_category_num(const char *name)
139 {
140     size_t i;
141
142     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
143         if (strcasecmp(name, trace_categories[i].name) == 0)
144             return trace_categories[i].num;
145     return -1; /* not found */
146 }
147
148 #ifndef OPENSSL_NO_TRACE
149
150 /* We use one trace channel for each trace category */
151 static struct {
152     enum { t_channel, t_callback } type;
153     BIO *bio;
154     char *prefix;
155     char *suffix;
156 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
157     { 0, NULL, NULL, NULL },
158 };
159
160 #endif
161
162 int ossl_trace_init(void)
163 {
164 #ifndef OPENSSL_NO_TRACE
165     trace_lock = CRYPTO_THREAD_lock_new();
166     if (trace_lock != NULL)
167         return 1;
168 #endif
169
170     return 0;
171 }
172
173 void ossl_trace_cleanup(void)
174 {
175 #ifndef OPENSSL_NO_TRACE
176     int category;
177
178     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
179         OSSL_trace_set_channel(category, NULL);
180     CRYPTO_THREAD_lock_free(trace_lock);
181 #endif
182 }
183
184 int OSSL_trace_set_channel(int category, BIO *channel)
185 {
186 #ifndef OPENSSL_NO_TRACE
187     BIO *prev_channel;
188
189     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
190         goto err;
191
192     prev_channel = trace_channels[category].bio;
193
194     if (prev_channel != NULL) {
195         BIO_free(prev_channel);
196         trace_channels[category].bio = NULL;
197     }
198
199     if (channel == NULL)
200         return 1;                /* Done */
201
202     trace_channels[category].bio = channel;
203     trace_channels[category].type = t_channel;
204
205     return 1;
206
207  err:
208 #endif
209
210     return 0;
211 }
212
213 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
214 {
215 #ifndef OPENSSL_NO_TRACE
216     BIO *channel = trace_channels[category].bio;
217     struct trace_data_st *trace_data = NULL;
218
219     if (channel != NULL) {
220         BIO_free(channel);
221         trace_channels[category].bio = NULL;
222     }
223
224     if (callback == NULL)
225         return 1; /* done */
226
227     channel = BIO_new(&trace_method);
228     if (channel == NULL)
229         goto err;
230
231     trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
232     if (trace_data == NULL)
233         goto err;
234
235     trace_data->callback = callback;
236     trace_data->category = category;
237     trace_data->data = data;
238
239     BIO_set_data(channel, trace_data);
240
241     trace_channels[category].bio = channel;
242     trace_channels[category].type = t_callback;
243
244     return 1;
245
246  err:
247     BIO_free(channel);
248     OPENSSL_free(trace_data);
249 #endif
250
251     return 0;
252 }
253
254 int OSSL_trace_set_prefix(int category, const char *prefix)
255 {
256 #ifndef OPENSSL_NO_TRACE
257     char *curr_prefix = trace_channels[category].prefix;
258
259     if (curr_prefix != NULL) {
260         OPENSSL_free(curr_prefix);
261         trace_channels[category].prefix = NULL;
262     }
263
264     if (prefix == NULL)
265         return 1;                /* Done */
266
267     curr_prefix = OPENSSL_strdup(prefix);
268     if (curr_prefix == NULL)
269         goto err;
270
271     trace_channels[category].prefix = curr_prefix;
272
273     return 1;
274
275  err:
276 #endif
277
278     return 0;
279 }
280
281 int OSSL_trace_set_suffix(int category, const char *suffix)
282 {
283 #ifndef OPENSSL_NO_TRACE
284     char *curr_suffix = trace_channels[category].suffix;
285
286     if (curr_suffix != NULL) {
287         OPENSSL_free(curr_suffix);
288         trace_channels[category].suffix = NULL;
289     }
290
291     if (suffix == NULL)
292         return 1; /* done */
293
294     curr_suffix = OPENSSL_strdup(suffix);
295     if (curr_suffix == NULL)
296         goto err;
297
298     trace_channels[category].suffix = curr_suffix;
299
300     return 1;
301
302  err:
303 #endif
304
305     return 0;
306 }
307
308 #ifndef OPENSSL_NO_TRACE
309 static int ossl_trace_get_category(int category)
310 {
311     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
312         return -1;
313     if (trace_channels[category].bio != NULL)
314         return category;
315     return OSSL_TRACE_CATEGORY_ANY;
316 }
317 #endif
318
319 int OSSL_trace_enabled(int category)
320 {
321     int ret = 0;
322 #ifndef OPENSSL_NO_TRACE
323     category = ossl_trace_get_category(category);
324     ret = trace_channels[category].bio != NULL;
325 #endif
326     return ret;
327 }
328
329 BIO *OSSL_trace_begin(int category)
330 {
331     BIO *channel = NULL;
332 #ifndef OPENSSL_NO_TRACE
333     char *prefix = NULL;
334
335     category = ossl_trace_get_category(category);
336     channel = trace_channels[category].bio;
337     prefix = trace_channels[category].prefix;
338
339     if (channel != NULL) {
340         CRYPTO_THREAD_write_lock(trace_lock);
341         current_channel = channel;
342         switch (trace_channels[category].type) {
343         case t_channel:
344             if (prefix != NULL) {
345                 (void)BIO_puts(channel, prefix);
346                 (void)BIO_puts(channel, "\n");
347             }
348             break;
349         case t_callback:
350             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
351                            prefix == NULL ? 0 : strlen(prefix), prefix);
352             break;
353         }
354     }
355 #endif
356     return channel;
357 }
358
359 void OSSL_trace_end(int category, BIO * channel)
360 {
361 #ifndef OPENSSL_NO_TRACE
362     char *suffix = NULL;
363
364     category = ossl_trace_get_category(category);
365     suffix = trace_channels[category].suffix;
366     if (channel != NULL
367         && ossl_assert(channel == current_channel)) {
368         (void)BIO_flush(channel);
369         switch (trace_channels[category].type) {
370         case t_channel:
371             if (suffix != NULL) {
372                 (void)BIO_puts(channel, suffix);
373                 (void)BIO_puts(channel, "\n");
374             }
375             break;
376         case t_callback:
377             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
378                            suffix == NULL ? 0 : strlen(suffix), suffix);
379             break;
380         }
381         current_channel = NULL;
382         CRYPTO_THREAD_unlock(trace_lock);
383     }
384 #endif
385 }