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