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