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