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
),
140 TRACE_CATEGORY_(ENCODER
),
143 const char *OSSL_trace_get_category_name(int num
)
147 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
148 if (trace_categories
[i
].num
== num
)
149 return trace_categories
[i
].name
;
150 return NULL
; /* not found */
153 int OSSL_trace_get_category_num(const char *name
)
157 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
158 if (strcasecmp(name
, trace_categories
[i
].name
) == 0)
159 return trace_categories
[i
].num
;
160 return -1; /* not found */
163 #ifndef OPENSSL_NO_TRACE
165 /* We use one trace channel for each trace category */
167 enum { SIMPLE_CHANNEL
, CALLBACK_CHANNEL
} type
;
171 } trace_channels
[OSSL_TRACE_CATEGORY_NUM
] = {
172 { 0, NULL
, NULL
, NULL
},
177 #ifndef OPENSSL_NO_TRACE
185 static int trace_attach_cb(int category
, int type
, const void *data
)
189 OSSL_TRACE2(TRACE
, "Attach channel %p to category '%s'\n",
190 data
, trace_categories
[category
].name
);
193 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
194 (const char *)data
, trace_categories
[category
].name
);
197 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
198 (const char *)data
, trace_categories
[category
].name
);
200 default: /* No clue */
206 static int trace_detach_cb(int category
, int type
, const void *data
)
210 OSSL_TRACE2(TRACE
, "Detach channel %p from category '%s'\n",
211 data
, trace_categories
[category
].name
);
214 OSSL_TRACE2(TRACE
, "Detach prefix \"%s\" from category '%s'\n",
215 (const char *)data
, trace_categories
[category
].name
);
218 OSSL_TRACE2(TRACE
, "Detach suffix \"%s\" from category '%s'\n",
219 (const char *)data
, trace_categories
[category
].name
);
221 default: /* No clue */
227 static int do_ossl_trace_init(void);
228 static CRYPTO_ONCE trace_inited
= CRYPTO_ONCE_STATIC_INIT
;
229 DEFINE_RUN_ONCE_STATIC(ossl_trace_init
)
231 return do_ossl_trace_init();
234 static int set_trace_data(int category
, int type
, BIO
**channel
,
235 const char **prefix
, const char **suffix
,
236 int (*attach_cb
)(int, int, const void *),
237 int (*detach_cb
)(int, int, const void *))
239 BIO
*curr_channel
= NULL
;
240 char *curr_prefix
= NULL
;
241 char *curr_suffix
= NULL
;
243 /* Ensure do_ossl_trace_init() is called once */
244 if (!RUN_ONCE(&trace_inited
, ossl_trace_init
))
247 curr_channel
= trace_channels
[category
].bio
;
248 curr_prefix
= trace_channels
[category
].prefix
;
249 curr_suffix
= trace_channels
[category
].suffix
;
251 /* Make sure to run the detach callback first on all data */
252 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
253 detach_cb(category
, PREFIX
, curr_prefix
);
256 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
257 detach_cb(category
, SUFFIX
, curr_suffix
);
260 if (channel
!= NULL
&& curr_channel
!= NULL
) {
261 detach_cb(category
, CHANNEL
, curr_channel
);
264 /* After detach callbacks are done, clear data where appropriate */
265 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
266 OPENSSL_free(curr_prefix
);
267 trace_channels
[category
].prefix
= NULL
;
270 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
271 OPENSSL_free(curr_suffix
);
272 trace_channels
[category
].suffix
= NULL
;
275 if (channel
!= NULL
&& curr_channel
!= NULL
) {
276 BIO_free(curr_channel
);
277 trace_channels
[category
].type
= 0;
278 trace_channels
[category
].bio
= NULL
;
281 /* Before running callbacks are done, set new data where appropriate */
282 if (channel
!= NULL
&& *channel
!= NULL
) {
283 trace_channels
[category
].type
= type
;
284 trace_channels
[category
].bio
= *channel
;
287 if (prefix
!= NULL
&& *prefix
!= NULL
) {
288 if ((curr_prefix
= OPENSSL_strdup(*prefix
)) == NULL
)
290 trace_channels
[category
].prefix
= curr_prefix
;
293 if (suffix
!= NULL
&& *suffix
!= NULL
) {
294 if ((curr_suffix
= OPENSSL_strdup(*suffix
)) == NULL
)
296 trace_channels
[category
].suffix
= curr_suffix
;
299 /* Finally, run the attach callback on the new data */
300 if (channel
!= NULL
&& *channel
!= NULL
) {
301 attach_cb(category
, CHANNEL
, *channel
);
304 if (prefix
!= NULL
&& *prefix
!= NULL
) {
305 attach_cb(category
, PREFIX
, *prefix
);
308 if (suffix
!= NULL
&& *suffix
!= NULL
) {
309 attach_cb(category
, SUFFIX
, *suffix
);
315 static int do_ossl_trace_init(void)
317 trace_lock
= CRYPTO_THREAD_lock_new();
318 return trace_lock
!= NULL
;
323 void ossl_trace_cleanup(void)
325 #ifndef OPENSSL_NO_TRACE
328 const char *prefix
= NULL
;
329 const char *suffix
= NULL
;
331 for (category
= 0; category
< OSSL_TRACE_CATEGORY_NUM
; category
++) {
332 /* We force the TRACE category to be treated last */
333 if (category
== OSSL_TRACE_CATEGORY_TRACE
)
335 set_trace_data(category
, 0, &channel
, &prefix
, &suffix
,
336 trace_attach_cb
, trace_detach_cb
);
338 set_trace_data(OSSL_TRACE_CATEGORY_TRACE
, 0, &channel
,
340 trace_attach_cb
, trace_detach_cb
);
341 CRYPTO_THREAD_lock_free(trace_lock
);
345 int OSSL_trace_set_channel(int category
, BIO
*channel
)
347 #ifndef OPENSSL_NO_TRACE
348 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
349 return set_trace_data(category
, SIMPLE_CHANNEL
, &channel
, NULL
, NULL
,
350 trace_attach_cb
, trace_detach_cb
);
355 #ifndef OPENSSL_NO_TRACE
356 static int trace_attach_w_callback_cb(int category
, int type
, const void *data
)
361 "Attach channel %p to category '%s' (with callback)\n",
362 data
, trace_categories
[category
].name
);
365 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
366 (const char *)data
, trace_categories
[category
].name
);
369 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
370 (const char *)data
, trace_categories
[category
].name
);
372 default: /* No clue */
379 int OSSL_trace_set_callback(int category
, OSSL_trace_cb callback
, void *data
)
381 #ifndef OPENSSL_NO_TRACE
383 struct trace_data_st
*trace_data
= NULL
;
385 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
388 if (callback
!= NULL
) {
389 if ((channel
= BIO_new(&trace_method
)) == NULL
391 OPENSSL_zalloc(sizeof(struct trace_data_st
))) == NULL
)
394 trace_data
->callback
= callback
;
395 trace_data
->category
= category
;
396 trace_data
->data
= data
;
398 BIO_set_data(channel
, trace_data
);
401 if (!set_trace_data(category
, CALLBACK_CHANNEL
, &channel
, NULL
, NULL
,
402 trace_attach_w_callback_cb
, trace_detach_cb
))
409 OPENSSL_free(trace_data
);
415 int OSSL_trace_set_prefix(int category
, const char *prefix
)
417 #ifndef OPENSSL_NO_TRACE
418 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
419 return set_trace_data(category
, 0, NULL
, &prefix
, NULL
,
420 trace_attach_cb
, trace_detach_cb
);
425 int OSSL_trace_set_suffix(int category
, const char *suffix
)
427 #ifndef OPENSSL_NO_TRACE
428 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
429 return set_trace_data(category
, 0, NULL
, NULL
, &suffix
,
430 trace_attach_cb
, trace_detach_cb
);
435 #ifndef OPENSSL_NO_TRACE
436 static int ossl_trace_get_category(int category
)
438 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
440 if (trace_channels
[category
].bio
!= NULL
)
442 return OSSL_TRACE_CATEGORY_ALL
;
446 int OSSL_trace_enabled(int category
)
449 #ifndef OPENSSL_NO_TRACE
450 category
= ossl_trace_get_category(category
);
452 ret
= trace_channels
[category
].bio
!= NULL
;
457 BIO
*OSSL_trace_begin(int category
)
460 #ifndef OPENSSL_NO_TRACE
463 category
= ossl_trace_get_category(category
);
467 channel
= trace_channels
[category
].bio
;
468 prefix
= trace_channels
[category
].prefix
;
470 if (channel
!= NULL
) {
471 if (!CRYPTO_THREAD_write_lock(trace_lock
))
473 current_channel
= channel
;
474 switch (trace_channels
[category
].type
) {
476 if (prefix
!= NULL
) {
477 (void)BIO_puts(channel
, prefix
);
478 (void)BIO_puts(channel
, "\n");
481 case CALLBACK_CHANNEL
:
482 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_BEGIN
,
483 prefix
== NULL
? 0 : strlen(prefix
), prefix
);
491 void OSSL_trace_end(int category
, BIO
* channel
)
493 #ifndef OPENSSL_NO_TRACE
496 category
= ossl_trace_get_category(category
);
497 suffix
= trace_channels
[category
].suffix
;
499 && ossl_assert(channel
== current_channel
)) {
500 (void)BIO_flush(channel
);
501 switch (trace_channels
[category
].type
) {
503 if (suffix
!= NULL
) {
504 (void)BIO_puts(channel
, suffix
);
505 (void)BIO_puts(channel
, "\n");
508 case CALLBACK_CHANNEL
:
509 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_END
,
510 suffix
== NULL
? 0 : strlen(suffix
), suffix
);
513 current_channel
= NULL
;
514 CRYPTO_THREAD_unlock(trace_lock
);