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