2 * Copyright 2019-2021 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 "internal/refcount.h"
20 #include "crypto/cryptlib.h"
22 #include "internal/e_os.h" /* strcasecmp for Windows */
24 #ifndef OPENSSL_NO_TRACE
26 static CRYPTO_RWLOCK
*trace_lock
= NULL
;
28 static const BIO
*current_channel
= NULL
;
31 * INTERNAL TRACE CHANNEL IMPLEMENTATION
33 * For our own flexibility, all trace categories are associated with a
34 * BIO sink object, also called the trace channel. Instead of a BIO object,
35 * the application can also provide a callback function, in which case an
36 * internal trace channel is attached, which simply calls the registered
39 static int trace_write(BIO
*b
, const char *buf
,
40 size_t num
, size_t *written
);
41 static int trace_puts(BIO
*b
, const char *str
);
42 static long trace_ctrl(BIO
*channel
, int cmd
, long argl
, void *argp
);
43 static int trace_free(BIO
*b
);
45 static const BIO_METHOD trace_method
= {
54 trace_ctrl
, /* ctrl */
56 trace_free
, /* free */
57 NULL
, /* callback_ctrl */
60 struct trace_data_st
{
61 OSSL_trace_cb callback
;
66 static int trace_write(BIO
*channel
,
67 const char *buf
, size_t num
, size_t *written
)
69 struct trace_data_st
*ctx
= BIO_get_data(channel
);
70 size_t cnt
= ctx
->callback(buf
, num
, ctx
->category
, OSSL_TRACE_CTRL_WRITE
,
77 static int trace_puts(BIO
*channel
, const char *str
)
81 if (trace_write(channel
, str
, strlen(str
), &written
))
87 static long trace_ctrl(BIO
*channel
, int cmd
, long argl
, void *argp
)
89 struct trace_data_st
*ctx
= BIO_get_data(channel
);
92 case OSSL_TRACE_CTRL_BEGIN
:
93 case OSSL_TRACE_CTRL_END
:
94 /* We know that the callback is likely to return 0 here */
95 ctx
->callback("", 0, ctx
->category
, cmd
, ctx
->data
);
100 return -2; /* Unsupported */
103 static int trace_free(BIO
*channel
)
107 OPENSSL_free(BIO_get_data(channel
));
116 /* Helper struct and macro to get name string to number mapping */
117 struct trace_category_st
{
118 const char * const name
;
121 #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
123 static const struct trace_category_st trace_categories
[] = {
124 TRACE_CATEGORY_(ALL
),
125 TRACE_CATEGORY_(TRACE
),
126 TRACE_CATEGORY_(INIT
),
127 TRACE_CATEGORY_(TLS
),
128 TRACE_CATEGORY_(TLS_CIPHER
),
129 TRACE_CATEGORY_(CONF
),
130 #ifndef OPENSSL_NO_ENGINE
131 TRACE_CATEGORY_(ENGINE_TABLE
),
132 TRACE_CATEGORY_(ENGINE_REF_COUNT
),
134 TRACE_CATEGORY_(PKCS5V2
),
135 TRACE_CATEGORY_(PKCS12_KEYGEN
),
136 TRACE_CATEGORY_(PKCS12_DECRYPT
),
137 TRACE_CATEGORY_(X509V3_POLICY
),
138 TRACE_CATEGORY_(BN_CTX
),
139 TRACE_CATEGORY_(CMP
),
140 TRACE_CATEGORY_(STORE
),
141 TRACE_CATEGORY_(DECODER
),
142 TRACE_CATEGORY_(ENCODER
),
143 TRACE_CATEGORY_(REF_COUNT
)
146 const char *OSSL_trace_get_category_name(int num
)
150 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
151 if (trace_categories
[i
].num
== num
)
152 return trace_categories
[i
].name
;
153 return NULL
; /* not found */
156 int OSSL_trace_get_category_num(const char *name
)
160 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
161 if (strcasecmp(name
, trace_categories
[i
].name
) == 0)
162 return trace_categories
[i
].num
;
163 return -1; /* not found */
166 #ifndef OPENSSL_NO_TRACE
168 /* We use one trace channel for each trace category */
170 enum { SIMPLE_CHANNEL
, CALLBACK_CHANNEL
} type
;
174 } trace_channels
[OSSL_TRACE_CATEGORY_NUM
] = {
175 { 0, NULL
, NULL
, NULL
},
180 #ifndef OPENSSL_NO_TRACE
188 static int trace_attach_cb(int category
, int type
, const void *data
)
192 OSSL_TRACE2(TRACE
, "Attach channel %p to category '%s'\n",
193 data
, trace_categories
[category
].name
);
196 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
197 (const char *)data
, trace_categories
[category
].name
);
200 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
201 (const char *)data
, trace_categories
[category
].name
);
203 default: /* No clue */
209 static int trace_detach_cb(int category
, int type
, const void *data
)
213 OSSL_TRACE2(TRACE
, "Detach channel %p from category '%s'\n",
214 data
, trace_categories
[category
].name
);
217 OSSL_TRACE2(TRACE
, "Detach prefix \"%s\" from category '%s'\n",
218 (const char *)data
, trace_categories
[category
].name
);
221 OSSL_TRACE2(TRACE
, "Detach suffix \"%s\" from category '%s'\n",
222 (const char *)data
, trace_categories
[category
].name
);
224 default: /* No clue */
230 static int do_ossl_trace_init(void);
231 static CRYPTO_ONCE trace_inited
= CRYPTO_ONCE_STATIC_INIT
;
232 DEFINE_RUN_ONCE_STATIC(ossl_trace_init
)
234 return do_ossl_trace_init();
237 static int set_trace_data(int category
, int type
, BIO
**channel
,
238 const char **prefix
, const char **suffix
,
239 int (*attach_cb
)(int, int, const void *),
240 int (*detach_cb
)(int, int, const void *))
242 BIO
*curr_channel
= NULL
;
243 char *curr_prefix
= NULL
;
244 char *curr_suffix
= NULL
;
246 /* Ensure do_ossl_trace_init() is called once */
247 if (!RUN_ONCE(&trace_inited
, ossl_trace_init
))
250 curr_channel
= trace_channels
[category
].bio
;
251 curr_prefix
= trace_channels
[category
].prefix
;
252 curr_suffix
= trace_channels
[category
].suffix
;
254 /* Make sure to run the detach callback first on all data */
255 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
256 detach_cb(category
, PREFIX
, curr_prefix
);
259 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
260 detach_cb(category
, SUFFIX
, curr_suffix
);
263 if (channel
!= NULL
&& curr_channel
!= NULL
) {
264 detach_cb(category
, CHANNEL
, curr_channel
);
267 /* After detach callbacks are done, clear data where appropriate */
268 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
269 OPENSSL_free(curr_prefix
);
270 trace_channels
[category
].prefix
= NULL
;
273 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
274 OPENSSL_free(curr_suffix
);
275 trace_channels
[category
].suffix
= NULL
;
278 if (channel
!= NULL
&& curr_channel
!= NULL
) {
279 BIO_free(curr_channel
);
280 trace_channels
[category
].type
= 0;
281 trace_channels
[category
].bio
= NULL
;
284 /* Before running callbacks are done, set new data where appropriate */
285 if (channel
!= NULL
&& *channel
!= NULL
) {
286 trace_channels
[category
].type
= type
;
287 trace_channels
[category
].bio
= *channel
;
290 if (prefix
!= NULL
&& *prefix
!= NULL
) {
291 if ((curr_prefix
= OPENSSL_strdup(*prefix
)) == NULL
)
293 trace_channels
[category
].prefix
= curr_prefix
;
296 if (suffix
!= NULL
&& *suffix
!= NULL
) {
297 if ((curr_suffix
= OPENSSL_strdup(*suffix
)) == NULL
)
299 trace_channels
[category
].suffix
= curr_suffix
;
302 /* Finally, run the attach callback on the new data */
303 if (channel
!= NULL
&& *channel
!= NULL
) {
304 attach_cb(category
, CHANNEL
, *channel
);
307 if (prefix
!= NULL
&& *prefix
!= NULL
) {
308 attach_cb(category
, PREFIX
, *prefix
);
311 if (suffix
!= NULL
&& *suffix
!= NULL
) {
312 attach_cb(category
, SUFFIX
, *suffix
);
318 static int do_ossl_trace_init(void)
320 trace_lock
= CRYPTO_THREAD_lock_new();
321 return trace_lock
!= NULL
;
326 void ossl_trace_cleanup(void)
328 #ifndef OPENSSL_NO_TRACE
331 const char *prefix
= NULL
;
332 const char *suffix
= NULL
;
334 for (category
= 0; category
< OSSL_TRACE_CATEGORY_NUM
; category
++) {
335 /* We force the TRACE category to be treated last */
336 if (category
== OSSL_TRACE_CATEGORY_TRACE
)
338 set_trace_data(category
, 0, &channel
, &prefix
, &suffix
,
339 trace_attach_cb
, trace_detach_cb
);
341 set_trace_data(OSSL_TRACE_CATEGORY_TRACE
, 0, &channel
,
343 trace_attach_cb
, trace_detach_cb
);
344 CRYPTO_THREAD_lock_free(trace_lock
);
348 int OSSL_trace_set_channel(int category
, BIO
*channel
)
350 #ifndef OPENSSL_NO_TRACE
351 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
352 return set_trace_data(category
, SIMPLE_CHANNEL
, &channel
, NULL
, NULL
,
353 trace_attach_cb
, trace_detach_cb
);
358 #ifndef OPENSSL_NO_TRACE
359 static int trace_attach_w_callback_cb(int category
, int type
, const void *data
)
364 "Attach channel %p to category '%s' (with callback)\n",
365 data
, trace_categories
[category
].name
);
368 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
369 (const char *)data
, trace_categories
[category
].name
);
372 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
373 (const char *)data
, trace_categories
[category
].name
);
375 default: /* No clue */
382 int OSSL_trace_set_callback(int category
, OSSL_trace_cb callback
, void *data
)
384 #ifndef OPENSSL_NO_TRACE
386 struct trace_data_st
*trace_data
= NULL
;
388 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
391 if (callback
!= NULL
) {
392 if ((channel
= BIO_new(&trace_method
)) == NULL
394 OPENSSL_zalloc(sizeof(struct trace_data_st
))) == NULL
)
397 trace_data
->callback
= callback
;
398 trace_data
->category
= category
;
399 trace_data
->data
= data
;
401 BIO_set_data(channel
, trace_data
);
404 if (!set_trace_data(category
, CALLBACK_CHANNEL
, &channel
, NULL
, NULL
,
405 trace_attach_w_callback_cb
, trace_detach_cb
))
412 OPENSSL_free(trace_data
);
418 int OSSL_trace_set_prefix(int category
, const char *prefix
)
420 #ifndef OPENSSL_NO_TRACE
421 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
422 return set_trace_data(category
, 0, NULL
, &prefix
, NULL
,
423 trace_attach_cb
, trace_detach_cb
);
428 int OSSL_trace_set_suffix(int category
, const char *suffix
)
430 #ifndef OPENSSL_NO_TRACE
431 if (category
>= 0 && category
< OSSL_TRACE_CATEGORY_NUM
)
432 return set_trace_data(category
, 0, NULL
, NULL
, &suffix
,
433 trace_attach_cb
, trace_detach_cb
);
438 #ifndef OPENSSL_NO_TRACE
439 static int ossl_trace_get_category(int category
)
441 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
443 if (trace_channels
[category
].bio
!= NULL
)
445 return OSSL_TRACE_CATEGORY_ALL
;
449 int OSSL_trace_enabled(int category
)
452 #ifndef OPENSSL_NO_TRACE
453 category
= ossl_trace_get_category(category
);
455 ret
= trace_channels
[category
].bio
!= NULL
;
460 BIO
*OSSL_trace_begin(int category
)
463 #ifndef OPENSSL_NO_TRACE
466 category
= ossl_trace_get_category(category
);
470 channel
= trace_channels
[category
].bio
;
471 prefix
= trace_channels
[category
].prefix
;
473 if (channel
!= NULL
) {
474 if (!CRYPTO_THREAD_write_lock(trace_lock
))
476 current_channel
= channel
;
477 switch (trace_channels
[category
].type
) {
479 if (prefix
!= NULL
) {
480 (void)BIO_puts(channel
, prefix
);
481 (void)BIO_puts(channel
, "\n");
484 case CALLBACK_CHANNEL
:
485 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_BEGIN
,
486 prefix
== NULL
? 0 : strlen(prefix
), prefix
);
494 void OSSL_trace_end(int category
, BIO
* channel
)
496 #ifndef OPENSSL_NO_TRACE
499 category
= ossl_trace_get_category(category
);
502 suffix
= trace_channels
[category
].suffix
;
504 && ossl_assert(channel
== current_channel
)) {
505 (void)BIO_flush(channel
);
506 switch (trace_channels
[category
].type
) {
508 if (suffix
!= NULL
) {
509 (void)BIO_puts(channel
, suffix
);
510 (void)BIO_puts(channel
, "\n");
513 case CALLBACK_CHANNEL
:
514 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_END
,
515 suffix
== NULL
? 0 : strlen(suffix
), suffix
);
518 current_channel
= NULL
;
519 CRYPTO_THREAD_unlock(trace_lock
);