Clean away unnecessary length related OSSL_PARAM key names
[openssl.git] / crypto / trace.c
1 /*
2  * Copyright 2019-2020 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 "crypto/cryptlib.h"
20
21 #include "e_os.h"                /* strcasecmp for Windows */
22
23 #ifndef OPENSSL_NO_TRACE
24
25 static CRYPTO_RWLOCK *trace_lock = NULL;
26
27 static const BIO  *current_channel = NULL;
28
29 /*-
30  * INTERNAL TRACE CHANNEL IMPLEMENTATION
31  *
32  * For our own flexibility, all trace categories are associated with a
33  * BIO sink object, also called the trace channel. Instead of a BIO object,
34  * the application can also provide a callback function, in which case an
35  * internal trace channel is attached, which simply calls the registered
36  * callback function.
37  */
38 static int trace_write(BIO *b, const char *buf,
39                                size_t num, size_t *written);
40 static int trace_puts(BIO *b, const char *str);
41 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
42 static int trace_free(BIO *b);
43
44 static const BIO_METHOD trace_method = {
45     BIO_TYPE_SOURCE_SINK,
46     "trace",
47     trace_write,
48     NULL,                        /* old write */
49     NULL,                        /* read_ex */
50     NULL,                        /* read */
51     trace_puts,
52     NULL,                        /* gets */
53     trace_ctrl,                  /* ctrl */
54     NULL,                        /* create */
55     trace_free,                  /* free */
56     NULL,                        /* callback_ctrl */
57 };
58
59 struct trace_data_st {
60     OSSL_trace_cb callback;
61     int category;
62     void *data;
63 };
64
65 static int trace_write(BIO *channel,
66                        const char *buf, size_t num, size_t *written)
67 {
68     struct trace_data_st *ctx = BIO_get_data(channel);
69     size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
70                                ctx->data);
71
72     *written = cnt;
73     return cnt != 0;
74 }
75
76 static int trace_puts(BIO *channel, const char *str)
77 {
78     size_t written;
79
80     if (trace_write(channel, str, strlen(str), &written))
81         return (int)written;
82
83     return EOF;
84 }
85
86 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
87 {
88     struct trace_data_st *ctx = BIO_get_data(channel);
89
90     switch (cmd) {
91     case OSSL_TRACE_CTRL_BEGIN:
92     case OSSL_TRACE_CTRL_END:
93         /* We know that the callback is likely to return 0 here */
94         ctx->callback("", 0, ctx->category, cmd, ctx->data);
95         return 1;
96     default:
97         break;
98     }
99     return -2;                   /* Unsupported */
100 }
101
102 static int trace_free(BIO *channel)
103 {
104     if (channel == NULL)
105         return 0;
106     OPENSSL_free(BIO_get_data(channel));
107     return 1;
108 }
109 #endif
110
111 /*-
112  * TRACE
113  */
114
115 /* Helper struct and macro to get name string to number mapping */
116 struct trace_category_st {
117     const char * const name;
118     const int num;
119 };
120 #define TRACE_CATEGORY_(name)       { #name, OSSL_TRACE_CATEGORY_##name }
121
122 static const struct trace_category_st trace_categories[] = {
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 #ifndef OPENSSL_NO_ENGINE
130     TRACE_CATEGORY_(ENGINE_TABLE),
131     TRACE_CATEGORY_(ENGINE_REF_COUNT),
132 #endif
133     TRACE_CATEGORY_(PKCS5V2),
134     TRACE_CATEGORY_(PKCS12_KEYGEN),
135     TRACE_CATEGORY_(PKCS12_DECRYPT),
136     TRACE_CATEGORY_(X509V3_POLICY),
137     TRACE_CATEGORY_(BN_CTX),
138     TRACE_CATEGORY_(STORE),
139     TRACE_CATEGORY_(DECODER),
140     TRACE_CATEGORY_(ENCODER),
141 };
142
143 const char *OSSL_trace_get_category_name(int num)
144 {
145     size_t i;
146
147     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
148         if (trace_categories[i].num == num)
149             return trace_categories[i].name;
150     return NULL; /* not found */
151 }
152
153 int OSSL_trace_get_category_num(const char *name)
154 {
155     size_t i;
156
157     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
158         if (strcasecmp(name, trace_categories[i].name) == 0)
159             return trace_categories[i].num;
160     return -1; /* not found */
161 }
162
163 #ifndef OPENSSL_NO_TRACE
164
165 /* We use one trace channel for each trace category */
166 static struct {
167     enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
168     BIO *bio;
169     char *prefix;
170     char *suffix;
171 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
172     { 0, NULL, NULL, NULL },
173 };
174
175 #endif
176
177 #ifndef OPENSSL_NO_TRACE
178
179 enum {
180     CHANNEL,
181     PREFIX,
182     SUFFIX
183 };
184
185 static int trace_attach_cb(int category, int type, const void *data)
186 {
187     switch (type) {
188     case CHANNEL:
189         OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
190                     data, trace_categories[category].name);
191         break;
192     case PREFIX:
193         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
194                     (const char *)data, trace_categories[category].name);
195         break;
196     case SUFFIX:
197         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
198                     (const char *)data, trace_categories[category].name);
199         break;
200     default:                     /* No clue */
201         break;
202     }
203     return 1;
204 }
205
206 static int trace_detach_cb(int category, int type, const void *data)
207 {
208     switch (type) {
209     case CHANNEL:
210         OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
211                     data, trace_categories[category].name);
212         break;
213     case PREFIX:
214         OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
215                     (const char *)data, trace_categories[category].name);
216         break;
217     case SUFFIX:
218         OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
219                     (const char *)data, trace_categories[category].name);
220         break;
221     default:                     /* No clue */
222         break;
223     }
224     return 1;
225 }
226
227 static int do_ossl_trace_init(void);
228 static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
229 DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
230 {
231     return do_ossl_trace_init();
232 }
233
234 static int set_trace_data(int category, int type, BIO **channel,
235                           const char **prefix, const char **suffix,
236                           int (*attach_cb)(int, int, const void *),
237                           int (*detach_cb)(int, int, const void *))
238 {
239     BIO *curr_channel = NULL;
240     char *curr_prefix = NULL;
241     char *curr_suffix = NULL;
242
243     /* Ensure do_ossl_trace_init() is called once */
244     if (!RUN_ONCE(&trace_inited, ossl_trace_init))
245         return 0;
246
247     curr_channel = trace_channels[category].bio;
248     curr_prefix = trace_channels[category].prefix;
249     curr_suffix = trace_channels[category].suffix;
250
251     /* Make sure to run the detach callback first on all data */
252     if (prefix != NULL && curr_prefix != NULL) {
253         detach_cb(category, PREFIX, curr_prefix);
254     }
255
256     if (suffix != NULL && curr_suffix != NULL) {
257         detach_cb(category, SUFFIX, curr_suffix);
258     }
259
260     if (channel != NULL && curr_channel != NULL) {
261         detach_cb(category, CHANNEL, curr_channel);
262     }
263
264     /* After detach callbacks are done, clear data where appropriate */
265     if (prefix != NULL && curr_prefix != NULL) {
266         OPENSSL_free(curr_prefix);
267         trace_channels[category].prefix = NULL;
268     }
269
270     if (suffix != NULL && curr_suffix != NULL) {
271         OPENSSL_free(curr_suffix);
272         trace_channels[category].suffix = NULL;
273     }
274
275     if (channel != NULL && curr_channel != NULL) {
276         BIO_free(curr_channel);
277         trace_channels[category].type = 0;
278         trace_channels[category].bio = NULL;
279     }
280
281     /* Before running callbacks are done, set new data where appropriate */
282     if (channel != NULL && *channel != NULL) {
283         trace_channels[category].type = type;
284         trace_channels[category].bio = *channel;
285     }
286
287     if (prefix != NULL && *prefix != NULL) {
288         if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
289             return 0;
290         trace_channels[category].prefix = curr_prefix;
291     }
292
293     if (suffix != NULL && *suffix != NULL) {
294         if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
295             return 0;
296         trace_channels[category].suffix = curr_suffix;
297     }
298
299     /* Finally, run the attach callback on the new data */
300     if (channel != NULL && *channel != NULL) {
301         attach_cb(category, CHANNEL, *channel);
302     }
303
304     if (prefix != NULL && *prefix != NULL) {
305         attach_cb(category, PREFIX, *prefix);
306     }
307
308     if (suffix != NULL && *suffix != NULL) {
309         attach_cb(category, SUFFIX, *suffix);
310     }
311
312     return 1;
313 }
314
315 static int do_ossl_trace_init(void)
316 {
317     trace_lock = CRYPTO_THREAD_lock_new();
318     return trace_lock != NULL;
319 }
320
321 #endif
322
323 void ossl_trace_cleanup(void)
324 {
325 #ifndef OPENSSL_NO_TRACE
326     int category;
327     BIO *channel = NULL;
328     const char *prefix = NULL;
329     const char *suffix = NULL;
330
331     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
332         /* We force the TRACE category to be treated last */
333         if (category == OSSL_TRACE_CATEGORY_TRACE)
334             continue;
335         set_trace_data(category, 0, &channel, &prefix, &suffix,
336                        trace_attach_cb, trace_detach_cb);
337     }
338     set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
339                    &prefix, &suffix,
340                    trace_attach_cb, trace_detach_cb);
341     CRYPTO_THREAD_lock_free(trace_lock);
342 #endif
343 }
344
345 int OSSL_trace_set_channel(int category, BIO *channel)
346 {
347 #ifndef OPENSSL_NO_TRACE
348     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
349         return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
350                               trace_attach_cb, trace_detach_cb);
351 #endif
352     return 0;
353 }
354
355 #ifndef OPENSSL_NO_TRACE
356 static int trace_attach_w_callback_cb(int category, int type, const void *data)
357 {
358     switch (type) {
359     case CHANNEL:
360         OSSL_TRACE2(TRACE,
361                     "Attach channel %p to category '%s' (with callback)\n",
362                     data, trace_categories[category].name);
363         break;
364     case PREFIX:
365         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
366                     (const char *)data, trace_categories[category].name);
367         break;
368     case SUFFIX:
369         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
370                     (const char *)data, trace_categories[category].name);
371         break;
372     default:                     /* No clue */
373         break;
374     }
375     return 1;
376 }
377 #endif
378
379 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
380 {
381 #ifndef OPENSSL_NO_TRACE
382     BIO *channel = NULL;
383     struct trace_data_st *trace_data = NULL;
384
385     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
386         return 0;
387
388     if (callback != NULL) {
389         if ((channel = BIO_new(&trace_method)) == NULL
390             || (trace_data =
391                 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
392             goto err;
393
394         trace_data->callback = callback;
395         trace_data->category = category;
396         trace_data->data = data;
397
398         BIO_set_data(channel, trace_data);
399     }
400
401     if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
402                         trace_attach_w_callback_cb, trace_detach_cb))
403         goto err;
404
405     return 1;
406
407  err:
408     BIO_free(channel);
409     OPENSSL_free(trace_data);
410 #endif
411
412     return 0;
413 }
414
415 int OSSL_trace_set_prefix(int category, const char *prefix)
416 {
417 #ifndef OPENSSL_NO_TRACE
418     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
419         return set_trace_data(category, 0, NULL, &prefix, NULL,
420                               trace_attach_cb, trace_detach_cb);
421 #endif
422     return 0;
423 }
424
425 int OSSL_trace_set_suffix(int category, const char *suffix)
426 {
427 #ifndef OPENSSL_NO_TRACE
428     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
429         return set_trace_data(category, 0, NULL, NULL, &suffix,
430                               trace_attach_cb, trace_detach_cb);
431 #endif
432     return 0;
433 }
434
435 #ifndef OPENSSL_NO_TRACE
436 static int ossl_trace_get_category(int category)
437 {
438     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
439         return -1;
440     if (trace_channels[category].bio != NULL)
441         return category;
442     return OSSL_TRACE_CATEGORY_ALL;
443 }
444 #endif
445
446 int OSSL_trace_enabled(int category)
447 {
448     int ret = 0;
449 #ifndef OPENSSL_NO_TRACE
450     category = ossl_trace_get_category(category);
451     if (category >= 0)
452         ret = trace_channels[category].bio != NULL;
453 #endif
454     return ret;
455 }
456
457 BIO *OSSL_trace_begin(int category)
458 {
459     BIO *channel = NULL;
460 #ifndef OPENSSL_NO_TRACE
461     char *prefix = NULL;
462
463     category = ossl_trace_get_category(category);
464     if (category < 0)
465         return NULL;
466
467     channel = trace_channels[category].bio;
468     prefix = trace_channels[category].prefix;
469
470     if (channel != NULL) {
471         CRYPTO_THREAD_write_lock(trace_lock);
472         current_channel = channel;
473         switch (trace_channels[category].type) {
474         case SIMPLE_CHANNEL:
475             if (prefix != NULL) {
476                 (void)BIO_puts(channel, prefix);
477                 (void)BIO_puts(channel, "\n");
478             }
479             break;
480         case CALLBACK_CHANNEL:
481             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
482                            prefix == NULL ? 0 : strlen(prefix), prefix);
483             break;
484         }
485     }
486 #endif
487     return channel;
488 }
489
490 void OSSL_trace_end(int category, BIO * channel)
491 {
492 #ifndef OPENSSL_NO_TRACE
493     char *suffix = NULL;
494
495     category = ossl_trace_get_category(category);
496     suffix = trace_channels[category].suffix;
497     if (channel != NULL
498         && ossl_assert(channel == current_channel)) {
499         (void)BIO_flush(channel);
500         switch (trace_channels[category].type) {
501         case SIMPLE_CHANNEL:
502             if (suffix != NULL) {
503                 (void)BIO_puts(channel, suffix);
504                 (void)BIO_puts(channel, "\n");
505             }
506             break;
507         case CALLBACK_CHANNEL:
508             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
509                            suffix == NULL ? 0 : strlen(suffix), suffix);
510             break;
511         }
512         current_channel = NULL;
513         CRYPTO_THREAD_unlock(trace_lock);
514     }
515 #endif
516 }