2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
21 #include "e_os.h" /* strcasecmp for Windows */
23 #ifndef OPENSSL_NO_TRACE
25 static CRYPTO_RWLOCK *trace_lock = NULL;
27 static const BIO *current_channel = NULL;
30 * INTERNAL TRACE CHANNEL IMPLEMENTATION
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
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);
44 static const BIO_METHOD trace_method = {
53 trace_ctrl, /* ctrl */
55 trace_free, /* free */
56 NULL, /* callback_ctrl */
59 struct trace_data_st {
60 OSSL_trace_cb callback;
65 static int trace_write(BIO *channel,
66 const char *buf, size_t num, size_t *written)
68 struct trace_data_st *ctx = BIO_get_data(channel);
69 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
76 static int trace_puts(BIO *channel, const char *str)
80 if (trace_write(channel, str, strlen(str), &written))
86 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
88 struct trace_data_st *ctx = BIO_get_data(channel);
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);
99 return -2; /* Unsupported */
102 static int trace_free(BIO *channel)
106 OPENSSL_free(BIO_get_data(channel));
115 /* Helper struct and macro to get name string to number mapping */
116 struct trace_category_st {
117 const char * const name;
120 #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
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),
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),
142 const char *OSSL_trace_get_category_name(int num)
146 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
147 if (trace_categories[i].num == num)
148 return trace_categories[i].name;
149 return NULL; /* not found */
152 int OSSL_trace_get_category_num(const char *name)
156 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
157 if (strcasecmp(name, trace_categories[i].name) == 0)
158 return trace_categories[i].num;
159 return -1; /* not found */
162 #ifndef OPENSSL_NO_TRACE
164 /* We use one trace channel for each trace category */
166 enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
170 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
171 { 0, NULL, NULL, NULL },
176 #ifndef OPENSSL_NO_TRACE
184 static int trace_attach_cb(int category, int type, const void *data)
188 OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
189 data, trace_categories[category].name);
192 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
193 (const char *)data, trace_categories[category].name);
196 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
197 (const char *)data, trace_categories[category].name);
199 default: /* No clue */
205 static int trace_detach_cb(int category, int type, const void *data)
209 OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
210 data, trace_categories[category].name);
213 OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
214 (const char *)data, trace_categories[category].name);
217 OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
218 (const char *)data, trace_categories[category].name);
220 default: /* No clue */
226 static int do_ossl_trace_init(void);
227 static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
228 DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
230 return do_ossl_trace_init();
233 static int set_trace_data(int category, int type, BIO **channel,
234 const char **prefix, const char **suffix,
235 int (*attach_cb)(int, int, const void *),
236 int (*detach_cb)(int, int, const void *))
238 BIO *curr_channel = NULL;
239 char *curr_prefix = NULL;
240 char *curr_suffix = NULL;
242 /* Ensure do_ossl_trace_init() is called once */
243 if (!RUN_ONCE(&trace_inited, ossl_trace_init))
246 curr_channel = trace_channels[category].bio;
247 curr_prefix = trace_channels[category].prefix;
248 curr_suffix = trace_channels[category].suffix;
250 /* Make sure to run the detach callback first on all data */
251 if (prefix != NULL && curr_prefix != NULL) {
252 detach_cb(category, PREFIX, curr_prefix);
255 if (suffix != NULL && curr_suffix != NULL) {
256 detach_cb(category, SUFFIX, curr_suffix);
259 if (channel != NULL && curr_channel != NULL) {
260 detach_cb(category, CHANNEL, curr_channel);
263 /* After detach callbacks are done, clear data where appropriate */
264 if (prefix != NULL && curr_prefix != NULL) {
265 OPENSSL_free(curr_prefix);
266 trace_channels[category].prefix = NULL;
269 if (suffix != NULL && curr_suffix != NULL) {
270 OPENSSL_free(curr_suffix);
271 trace_channels[category].suffix = NULL;
274 if (channel != NULL && curr_channel != NULL) {
275 BIO_free(curr_channel);
276 trace_channels[category].type = 0;
277 trace_channels[category].bio = NULL;
280 /* Before running callbacks are done, set new data where appropriate */
281 if (channel != NULL && *channel != NULL) {
282 trace_channels[category].type = type;
283 trace_channels[category].bio = *channel;
286 if (prefix != NULL && *prefix != NULL) {
287 if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
289 trace_channels[category].prefix = curr_prefix;
292 if (suffix != NULL && *suffix != NULL) {
293 if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
295 trace_channels[category].suffix = curr_suffix;
298 /* Finally, run the attach callback on the new data */
299 if (channel != NULL && *channel != NULL) {
300 attach_cb(category, CHANNEL, *channel);
303 if (prefix != NULL && *prefix != NULL) {
304 attach_cb(category, PREFIX, *prefix);
307 if (suffix != NULL && *suffix != NULL) {
308 attach_cb(category, SUFFIX, *suffix);
314 static int do_ossl_trace_init(void)
316 trace_lock = CRYPTO_THREAD_lock_new();
317 return trace_lock != NULL;
322 void ossl_trace_cleanup(void)
324 #ifndef OPENSSL_NO_TRACE
327 const char *prefix = NULL;
328 const char *suffix = NULL;
330 for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
331 /* We force the TRACE category to be treated last */
332 if (category == OSSL_TRACE_CATEGORY_TRACE)
334 set_trace_data(category, 0, &channel, &prefix, &suffix,
335 trace_attach_cb, trace_detach_cb);
337 set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
339 trace_attach_cb, trace_detach_cb);
340 CRYPTO_THREAD_lock_free(trace_lock);
344 int OSSL_trace_set_channel(int category, BIO *channel)
346 #ifndef OPENSSL_NO_TRACE
347 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
348 return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
349 trace_attach_cb, trace_detach_cb);
354 #ifndef OPENSSL_NO_TRACE
355 static int trace_attach_w_callback_cb(int category, int type, const void *data)
360 "Attach channel %p to category '%s' (with callback)\n",
361 data, trace_categories[category].name);
364 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
365 (const char *)data, trace_categories[category].name);
368 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
369 (const char *)data, trace_categories[category].name);
371 default: /* No clue */
378 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
380 #ifndef OPENSSL_NO_TRACE
382 struct trace_data_st *trace_data = NULL;
384 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
387 if (callback != NULL) {
388 if ((channel = BIO_new(&trace_method)) == NULL
390 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
393 trace_data->callback = callback;
394 trace_data->category = category;
395 trace_data->data = data;
397 BIO_set_data(channel, trace_data);
400 if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
401 trace_attach_w_callback_cb, trace_detach_cb))
408 OPENSSL_free(trace_data);
414 int OSSL_trace_set_prefix(int category, const char *prefix)
416 #ifndef OPENSSL_NO_TRACE
417 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
418 return set_trace_data(category, 0, NULL, &prefix, NULL,
419 trace_attach_cb, trace_detach_cb);
424 int OSSL_trace_set_suffix(int category, const char *suffix)
426 #ifndef OPENSSL_NO_TRACE
427 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
428 return set_trace_data(category, 0, NULL, NULL, &suffix,
429 trace_attach_cb, trace_detach_cb);
434 #ifndef OPENSSL_NO_TRACE
435 static int ossl_trace_get_category(int category)
437 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
439 if (trace_channels[category].bio != NULL)
441 return OSSL_TRACE_CATEGORY_ALL;
445 int OSSL_trace_enabled(int category)
448 #ifndef OPENSSL_NO_TRACE
449 category = ossl_trace_get_category(category);
451 ret = trace_channels[category].bio != NULL;
456 BIO *OSSL_trace_begin(int category)
459 #ifndef OPENSSL_NO_TRACE
462 category = ossl_trace_get_category(category);
466 channel = trace_channels[category].bio;
467 prefix = trace_channels[category].prefix;
469 if (channel != NULL) {
470 CRYPTO_THREAD_write_lock(trace_lock);
471 current_channel = channel;
472 switch (trace_channels[category].type) {
474 if (prefix != NULL) {
475 (void)BIO_puts(channel, prefix);
476 (void)BIO_puts(channel, "\n");
479 case CALLBACK_CHANNEL:
480 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
481 prefix == NULL ? 0 : strlen(prefix), prefix);
489 void OSSL_trace_end(int category, BIO * channel)
491 #ifndef OPENSSL_NO_TRACE
494 category = ossl_trace_get_category(category);
495 suffix = trace_channels[category].suffix;
497 && ossl_assert(channel == current_channel)) {
498 (void)BIO_flush(channel);
499 switch (trace_channels[category].type) {
501 if (suffix != NULL) {
502 (void)BIO_puts(channel, suffix);
503 (void)BIO_puts(channel, "\n");
506 case CALLBACK_CHANNEL:
507 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
508 suffix == NULL ? 0 : strlen(suffix), suffix);
511 current_channel = NULL;
512 CRYPTO_THREAD_unlock(trace_lock);