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 static CRYPTO_RWLOCK *trace_lock = NULL;
24 static const BIO *current_channel = NULL;
27 * INTERNAL TRACE CHANNEL IMPLEMENTATION
29 * For our own flexibility, all trace categories are associated with a
30 * BIO sink object, also called the trace channel. Instead of a BIO object,
31 * the application can also provide a callback function, in which case an
32 * internal trace channel is attached, which simply calls the registered
35 static int trace_write(BIO *b, const char *buf,
36 size_t num, size_t *written);
37 static int trace_puts(BIO *b, const char *str);
38 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
39 static int trace_free(BIO *b);
41 static const BIO_METHOD trace_method = {
50 trace_ctrl, /* ctrl */
52 trace_free, /* free */
53 NULL, /* callback_ctrl */
56 struct trace_data_st {
57 OSSL_trace_cb callback;
62 static int trace_write(BIO *channel,
63 const char *buf, size_t num, size_t *written)
65 struct trace_data_st *ctx = BIO_get_data(channel);
66 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_DURING,
73 static int trace_puts(BIO *channel, const char *str)
77 if (trace_write(channel, str, strlen(str), &written))
83 static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
85 struct trace_data_st *ctx = BIO_get_data(channel);
88 case OSSL_TRACE_CTRL_BEGIN:
89 case OSSL_TRACE_CTRL_END:
90 /* We know that the callback is likely to return 0 here */
91 ctx->callback("", 0, ctx->category, cmd, ctx->data);
96 return -2; /* Unsupported */
99 static int trace_free(BIO *channel)
103 OPENSSL_free(BIO_get_data(channel));
111 /* Helper struct and macro to get name string to number mapping */
112 struct trace_category_st {
113 const char * const name;
116 #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
118 static const struct trace_category_st trace_categories[] = {
119 TRACE_CATEGORY_(ANY),
122 const char *OSSL_trace_get_category_name(int num)
126 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
127 if (trace_categories[i].num == num)
128 return trace_categories[i].name;
129 return NULL; /* not found */
132 int OSSL_trace_get_category_num(const char *name)
136 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
137 if (strcasecmp(name, trace_categories[i].name) == 0)
138 return trace_categories[i].num;
139 return -1; /* not found */
142 /* We use one trace channel for each trace category */
144 enum { t_channel, t_callback } type;
148 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
149 { 0, NULL, NULL, NULL },
152 int ossl_trace_init(void)
154 trace_lock = CRYPTO_THREAD_lock_new();
155 if (trace_lock == NULL)
160 void ossl_trace_cleanup(void)
164 for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
165 OSSL_trace_set_channel(category, NULL);
166 CRYPTO_THREAD_lock_free(trace_lock);
169 int OSSL_trace_set_channel(int category, BIO *channel)
173 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
176 prev_channel = trace_channels[category].bio;
178 if (prev_channel != NULL) {
179 BIO_free(prev_channel);
180 trace_channels[category].bio = NULL;
186 trace_channels[category].bio = channel;
187 trace_channels[category].type = t_channel;
192 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
194 BIO *channel = trace_channels[category].bio;
195 struct trace_data_st *trace_data = NULL;
197 if (channel != NULL) {
199 trace_channels[category].bio = NULL;
202 if (callback == NULL)
205 channel = BIO_new(&trace_method);
209 trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
210 if (trace_data == NULL)
213 trace_data->callback = callback;
214 trace_data->category = category;
215 trace_data->data = data;
217 BIO_set_data(channel, trace_data);
219 trace_channels[category].bio = channel;
220 trace_channels[category].type = t_callback;
226 OPENSSL_free(trace_data);
231 int OSSL_trace_set_prefix(int category, const char *prefix)
233 char *curr_prefix = trace_channels[category].prefix;
235 if (curr_prefix != NULL) {
236 OPENSSL_free(curr_prefix);
237 trace_channels[category].prefix = NULL;
243 curr_prefix = OPENSSL_strdup(prefix);
244 if (curr_prefix == NULL)
247 trace_channels[category].prefix = curr_prefix;
252 int OSSL_trace_set_suffix(int category, const char *suffix)
254 char *curr_suffix = trace_channels[category].suffix;
256 if (curr_suffix != NULL) {
257 OPENSSL_free(curr_suffix);
258 trace_channels[category].suffix = NULL;
264 curr_suffix = OPENSSL_strdup(suffix);
265 if (curr_suffix == NULL)
268 trace_channels[category].suffix = curr_suffix;
273 static int ossl_trace_get_category(int category)
275 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
277 if (trace_channels[category].bio != NULL)
279 return OSSL_TRACE_CATEGORY_ANY;
282 int OSSL_trace_enabled(int category)
285 category = ossl_trace_get_category(category);
286 ret = trace_channels[category].bio != NULL;
290 BIO *OSSL_trace_begin(int category)
295 category = ossl_trace_get_category(category);
296 channel = trace_channels[category].bio;
297 prefix = trace_channels[category].prefix;
299 if (channel != NULL) {
300 CRYPTO_THREAD_write_lock(trace_lock);
301 current_channel = channel;
302 switch (trace_channels[category].type) {
304 if (prefix != NULL) {
305 (void)BIO_puts(channel, prefix);
306 (void)BIO_puts(channel, "\n");
310 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
311 prefix == NULL ? 0 : strlen(prefix), prefix);
318 void OSSL_trace_end(int category, BIO * channel)
322 category = ossl_trace_get_category(category);
323 suffix = trace_channels[category].suffix;
325 && ossl_assert(channel == current_channel)) {
326 (void)BIO_flush(channel);
327 switch (trace_channels[category].type) {
329 if (suffix != NULL) {
330 (void)BIO_puts(channel, suffix);
331 (void)BIO_puts(channel, "\n");
335 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
336 suffix == NULL ? 0 : strlen(suffix), suffix);
339 current_channel = NULL;
340 CRYPTO_THREAD_unlock(trace_lock);