2 * Copyright 2019 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 <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/trace.h>
16 #include "internal/bio.h"
17 #include "internal/nelem.h"
18 #include "internal/cryptlib_int.h"
20 #include "e_os.h" /* strcasecmp for Windows */
22 #ifndef OPENSSL_NO_TRACE
24 static CRYPTO_RWLOCK *trace_lock = NULL;
26 static const BIO *current_channel = NULL;
29 * INTERNAL TRACE CHANNEL IMPLEMENTATION
31 * For our own flexibility, all trace categories are associated with a
32 * BIO sink object, also called the trace channel. Instead of a BIO object,
33 * the application can also provide a callback function, in which case an
34 * internal trace channel is attached, which simply calls the registered
37 static int trace_write(BIO *b, const char *buf,
38 size_t num, size_t *written);
39 static int trace_puts(BIO *b, const char *str);
40 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
41 static int trace_free(BIO *b);
43 static const BIO_METHOD trace_method = {
52 trace_ctrl, /* ctrl */
54 trace_free, /* free */
55 NULL, /* callback_ctrl */
58 struct trace_data_st {
59 OSSL_trace_cb callback;
64 static int trace_write(BIO *channel,
65 const char *buf, size_t num, size_t *written)
67 struct trace_data_st *ctx = BIO_get_data(channel);
68 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_DURING,
75 static int trace_puts(BIO *channel, const char *str)
79 if (trace_write(channel, str, strlen(str), &written))
85 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
87 struct trace_data_st *ctx = BIO_get_data(channel);
90 case OSSL_TRACE_CTRL_BEGIN:
91 case OSSL_TRACE_CTRL_END:
92 /* We know that the callback is likely to return 0 here */
93 ctx->callback("", 0, ctx->category, cmd, ctx->data);
98 return -2; /* Unsupported */
101 static int trace_free(BIO *channel)
105 OPENSSL_free(BIO_get_data(channel));
114 /* Helper struct and macro to get name string to number mapping */
115 struct trace_category_st {
116 const char * const name;
119 #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
121 static const struct trace_category_st trace_categories[] = {
122 TRACE_CATEGORY_(ANY),
123 TRACE_CATEGORY_(TLS),
126 const char *OSSL_trace_get_category_name(int num)
130 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
131 if (trace_categories[i].num == num)
132 return trace_categories[i].name;
133 return NULL; /* not found */
136 int OSSL_trace_get_category_num(const char *name)
140 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
141 if (strcasecmp(name, trace_categories[i].name) == 0)
142 return trace_categories[i].num;
143 return -1; /* not found */
146 #ifndef OPENSSL_NO_TRACE
148 /* We use one trace channel for each trace category */
150 enum { t_channel, t_callback } type;
154 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
155 { 0, NULL, NULL, NULL },
160 int ossl_trace_init(void)
162 #ifndef OPENSSL_NO_TRACE
163 trace_lock = CRYPTO_THREAD_lock_new();
164 if (trace_lock != NULL)
171 void ossl_trace_cleanup(void)
173 #ifndef OPENSSL_NO_TRACE
176 for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
177 OSSL_trace_set_channel(category, NULL);
178 CRYPTO_THREAD_lock_free(trace_lock);
182 int OSSL_trace_set_channel(int category, BIO *channel)
184 #ifndef OPENSSL_NO_TRACE
187 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
190 prev_channel = trace_channels[category].bio;
192 if (prev_channel != NULL) {
193 BIO_free(prev_channel);
194 trace_channels[category].bio = NULL;
200 trace_channels[category].bio = channel;
201 trace_channels[category].type = t_channel;
211 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
213 #ifndef OPENSSL_NO_TRACE
214 BIO *channel = trace_channels[category].bio;
215 struct trace_data_st *trace_data = NULL;
217 if (channel != NULL) {
219 trace_channels[category].bio = NULL;
222 if (callback == NULL)
225 channel = BIO_new(&trace_method);
229 trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
230 if (trace_data == NULL)
233 trace_data->callback = callback;
234 trace_data->category = category;
235 trace_data->data = data;
237 BIO_set_data(channel, trace_data);
239 trace_channels[category].bio = channel;
240 trace_channels[category].type = t_callback;
246 OPENSSL_free(trace_data);
252 int OSSL_trace_set_prefix(int category, const char *prefix)
254 #ifndef OPENSSL_NO_TRACE
255 char *curr_prefix = trace_channels[category].prefix;
257 if (curr_prefix != NULL) {
258 OPENSSL_free(curr_prefix);
259 trace_channels[category].prefix = NULL;
265 curr_prefix = OPENSSL_strdup(prefix);
266 if (curr_prefix == NULL)
269 trace_channels[category].prefix = curr_prefix;
279 int OSSL_trace_set_suffix(int category, const char *suffix)
281 #ifndef OPENSSL_NO_TRACE
282 char *curr_suffix = trace_channels[category].suffix;
284 if (curr_suffix != NULL) {
285 OPENSSL_free(curr_suffix);
286 trace_channels[category].suffix = NULL;
292 curr_suffix = OPENSSL_strdup(suffix);
293 if (curr_suffix == NULL)
296 trace_channels[category].suffix = curr_suffix;
306 #ifndef OPENSSL_NO_TRACE
307 static int ossl_trace_get_category(int category)
309 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
311 if (trace_channels[category].bio != NULL)
313 return OSSL_TRACE_CATEGORY_ANY;
317 int OSSL_trace_enabled(int category)
320 #ifndef OPENSSL_NO_TRACE
321 category = ossl_trace_get_category(category);
322 ret = trace_channels[category].bio != NULL;
327 BIO *OSSL_trace_begin(int category)
330 #ifndef OPENSSL_NO_TRACE
333 category = ossl_trace_get_category(category);
334 channel = trace_channels[category].bio;
335 prefix = trace_channels[category].prefix;
337 if (channel != NULL) {
338 CRYPTO_THREAD_write_lock(trace_lock);
339 current_channel = channel;
340 switch (trace_channels[category].type) {
342 if (prefix != NULL) {
343 (void)BIO_puts(channel, prefix);
344 (void)BIO_puts(channel, "\n");
348 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
349 prefix == NULL ? 0 : strlen(prefix), prefix);
357 void OSSL_trace_end(int category, BIO * channel)
359 #ifndef OPENSSL_NO_TRACE
362 category = ossl_trace_get_category(category);
363 suffix = trace_channels[category].suffix;
365 && ossl_assert(channel == current_channel)) {
366 (void)BIO_flush(channel);
367 switch (trace_channels[category].type) {
369 if (suffix != NULL) {
370 (void)BIO_puts(channel, suffix);
371 (void)BIO_puts(channel, "\n");
375 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
376 suffix == NULL ? 0 : strlen(suffix), suffix);
379 current_channel = NULL;
380 CRYPTO_THREAD_unlock(trace_lock);