17d004720Schristos /*
2*b0d17251Schristos * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
37d004720Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
57d004720Schristos * this file except in compliance with the License. You can obtain a copy
67d004720Schristos * in the file LICENSE in the source distribution or at
77d004720Schristos * https://www.openssl.org/source/license.html
87d004720Schristos */
97d004720Schristos
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos #include <openssl/opensslconf.h>
12*b0d17251Schristos #include <openssl/trace.h>
13*b0d17251Schristos #include "apps.h"
147d004720Schristos #include "../testutil.h"
157d004720Schristos
16*b0d17251Schristos #ifndef OPENSSL_NO_TRACE
17*b0d17251Schristos typedef struct tracedata_st {
18*b0d17251Schristos BIO *bio;
19*b0d17251Schristos unsigned int ingroup:1;
20*b0d17251Schristos } tracedata;
21*b0d17251Schristos
internal_trace_cb(const char * buf,size_t cnt,int category,int cmd,void * vdata)22*b0d17251Schristos static size_t internal_trace_cb(const char *buf, size_t cnt,
23*b0d17251Schristos int category, int cmd, void *vdata)
24*b0d17251Schristos {
25*b0d17251Schristos int ret = 0;
26*b0d17251Schristos tracedata *trace_data = vdata;
27*b0d17251Schristos char buffer[256], *hex;
28*b0d17251Schristos CRYPTO_THREAD_ID tid;
29*b0d17251Schristos
30*b0d17251Schristos switch (cmd) {
31*b0d17251Schristos case OSSL_TRACE_CTRL_BEGIN:
32*b0d17251Schristos trace_data->ingroup = 1;
33*b0d17251Schristos
34*b0d17251Schristos tid = CRYPTO_THREAD_get_current_id();
35*b0d17251Schristos hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
36*b0d17251Schristos BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
37*b0d17251Schristos hex, OSSL_trace_get_category_name(category));
38*b0d17251Schristos OPENSSL_free(hex);
39*b0d17251Schristos BIO_set_prefix(trace_data->bio, buffer);
40*b0d17251Schristos break;
41*b0d17251Schristos case OSSL_TRACE_CTRL_WRITE:
42*b0d17251Schristos ret = BIO_write(trace_data->bio, buf, cnt);
43*b0d17251Schristos break;
44*b0d17251Schristos case OSSL_TRACE_CTRL_END:
45*b0d17251Schristos trace_data->ingroup = 0;
46*b0d17251Schristos
47*b0d17251Schristos BIO_set_prefix(trace_data->bio, NULL);
48*b0d17251Schristos break;
49*b0d17251Schristos }
50*b0d17251Schristos
51*b0d17251Schristos return ret < 0 ? 0 : ret;
52*b0d17251Schristos }
53*b0d17251Schristos
54*b0d17251Schristos DEFINE_STACK_OF(tracedata)
55*b0d17251Schristos static STACK_OF(tracedata) *trace_data_stack;
56*b0d17251Schristos
tracedata_free(tracedata * data)57*b0d17251Schristos static void tracedata_free(tracedata *data)
58*b0d17251Schristos {
59*b0d17251Schristos BIO_free_all(data->bio);
60*b0d17251Schristos OPENSSL_free(data);
61*b0d17251Schristos }
62*b0d17251Schristos
STACK_OF(tracedata)63*b0d17251Schristos static STACK_OF(tracedata) *trace_data_stack;
64*b0d17251Schristos
65*b0d17251Schristos static void cleanup_trace(void)
66*b0d17251Schristos {
67*b0d17251Schristos sk_tracedata_pop_free(trace_data_stack, tracedata_free);
68*b0d17251Schristos }
69*b0d17251Schristos
setup_trace_category(int category)70*b0d17251Schristos static void setup_trace_category(int category)
71*b0d17251Schristos {
72*b0d17251Schristos BIO *channel;
73*b0d17251Schristos tracedata *trace_data;
74*b0d17251Schristos BIO *bio = NULL;
75*b0d17251Schristos
76*b0d17251Schristos if (OSSL_trace_enabled(category))
77*b0d17251Schristos return;
78*b0d17251Schristos
79*b0d17251Schristos bio = BIO_new(BIO_f_prefix());
80*b0d17251Schristos channel = BIO_push(bio,
81*b0d17251Schristos BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
82*b0d17251Schristos trace_data = OPENSSL_zalloc(sizeof(*trace_data));
83*b0d17251Schristos
84*b0d17251Schristos if (trace_data == NULL
85*b0d17251Schristos || bio == NULL
86*b0d17251Schristos || (trace_data->bio = channel) == NULL
87*b0d17251Schristos || OSSL_trace_set_callback(category, internal_trace_cb,
88*b0d17251Schristos trace_data) == 0
89*b0d17251Schristos || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
90*b0d17251Schristos
91*b0d17251Schristos fprintf(stderr,
92*b0d17251Schristos "warning: unable to setup trace callback for category '%s'.\n",
93*b0d17251Schristos OSSL_trace_get_category_name(category));
94*b0d17251Schristos
95*b0d17251Schristos OSSL_trace_set_callback(category, NULL, NULL);
96*b0d17251Schristos BIO_free_all(channel);
97*b0d17251Schristos }
98*b0d17251Schristos }
99*b0d17251Schristos
setup_trace(const char * str)100*b0d17251Schristos static void setup_trace(const char *str)
101*b0d17251Schristos {
102*b0d17251Schristos char *val;
103*b0d17251Schristos
104*b0d17251Schristos /*
105*b0d17251Schristos * We add this handler as early as possible to ensure it's executed
106*b0d17251Schristos * as late as possible, i.e. after the TRACE code has done its cleanup
107*b0d17251Schristos * (which happens last in OPENSSL_cleanup).
108*b0d17251Schristos */
109*b0d17251Schristos atexit(cleanup_trace);
110*b0d17251Schristos
111*b0d17251Schristos trace_data_stack = sk_tracedata_new_null();
112*b0d17251Schristos val = OPENSSL_strdup(str);
113*b0d17251Schristos
114*b0d17251Schristos if (val != NULL) {
115*b0d17251Schristos char *valp = val;
116*b0d17251Schristos char *item;
117*b0d17251Schristos
118*b0d17251Schristos for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
119*b0d17251Schristos int category = OSSL_trace_get_category_num(item);
120*b0d17251Schristos
121*b0d17251Schristos if (category == OSSL_TRACE_CATEGORY_ALL) {
122*b0d17251Schristos while (++category < OSSL_TRACE_CATEGORY_NUM)
123*b0d17251Schristos setup_trace_category(category);
124*b0d17251Schristos break;
125*b0d17251Schristos } else if (category > 0) {
126*b0d17251Schristos setup_trace_category(category);
127*b0d17251Schristos } else {
128*b0d17251Schristos fprintf(stderr,
129*b0d17251Schristos "warning: unknown trace category: '%s'.\n", item);
130*b0d17251Schristos }
131*b0d17251Schristos }
132*b0d17251Schristos }
133*b0d17251Schristos
134*b0d17251Schristos OPENSSL_free(val);
135*b0d17251Schristos }
136*b0d17251Schristos #endif /* OPENSSL_NO_TRACE */
137*b0d17251Schristos
global_init(void)1387d004720Schristos int global_init(void)
1397d004720Schristos {
140*b0d17251Schristos #ifndef OPENSSL_NO_TRACE
141*b0d17251Schristos setup_trace(getenv("OPENSSL_TRACE"));
142*b0d17251Schristos #endif
143*b0d17251Schristos
1447d004720Schristos return 1;
1457d004720Schristos }
146