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