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_WRITE
,
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_(TRACE
),
124 TRACE_CATEGORY_(INIT
),
125 TRACE_CATEGORY_(TLS
),
126 TRACE_CATEGORY_(TLS_CIPHER
),
127 TRACE_CATEGORY_(ENGINE_CONF
),
128 TRACE_CATEGORY_(ENGINE_TABLE
),
129 TRACE_CATEGORY_(ENGINE_REF_COUNT
),
130 TRACE_CATEGORY_(PKCS5V2
),
131 TRACE_CATEGORY_(PKCS12_KEYGEN
),
132 TRACE_CATEGORY_(PKCS12_DECRYPT
),
133 TRACE_CATEGORY_(X509V3_POLICY
),
134 TRACE_CATEGORY_(BN_CTX
),
137 const char *OSSL_trace_get_category_name(int num
)
141 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
142 if (trace_categories
[i
].num
== num
)
143 return trace_categories
[i
].name
;
144 return NULL
; /* not found */
147 int OSSL_trace_get_category_num(const char *name
)
151 for (i
= 0; i
< OSSL_NELEM(trace_categories
); i
++)
152 if (strcasecmp(name
, trace_categories
[i
].name
) == 0)
153 return trace_categories
[i
].num
;
154 return -1; /* not found */
157 #ifndef OPENSSL_NO_TRACE
159 /* We use one trace channel for each trace category */
161 enum { SIMPLE_CHANNEL
, CALLBACK_CHANNEL
} type
;
165 } trace_channels
[OSSL_TRACE_CATEGORY_NUM
] = {
166 { 0, NULL
, NULL
, NULL
},
171 #ifndef OPENSSL_NO_TRACE
179 static int trace_attach_cb(int category
, int type
, const void *data
)
183 OSSL_TRACE2(TRACE
, "Attach channel %p to category '%s'\n",
184 data
, trace_categories
[category
].name
);
187 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
188 (const char *)data
, trace_categories
[category
].name
);
191 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
192 (const char *)data
, trace_categories
[category
].name
);
194 default: /* No clue */
200 static int trace_detach_cb(int category
, int type
, const void *data
)
204 OSSL_TRACE2(TRACE
, "Detach channel %p from category '%s'\n",
205 data
, trace_categories
[category
].name
);
208 OSSL_TRACE2(TRACE
, "Detach prefix \"%s\" from category '%s'\n",
209 (const char *)data
, trace_categories
[category
].name
);
212 OSSL_TRACE2(TRACE
, "Detach suffix \"%s\" from category '%s'\n",
213 (const char *)data
, trace_categories
[category
].name
);
215 default: /* No clue */
221 static int set_trace_data(int category
, BIO
**channel
,
222 const char **prefix
, const char **suffix
,
223 int (*attach_cb
)(int, int, const void *),
224 int (*detach_cb
)(int, int, const void *))
226 BIO
*curr_channel
= trace_channels
[category
].bio
;
227 char *curr_prefix
= trace_channels
[category
].prefix
;
228 char *curr_suffix
= trace_channels
[category
].suffix
;
230 /* Make sure to run the detach callback first on all data */
231 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
232 detach_cb(category
, PREFIX
, curr_prefix
);
235 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
236 detach_cb(category
, SUFFIX
, curr_suffix
);
239 if (channel
!= NULL
&& curr_channel
!= NULL
) {
240 detach_cb(category
, CHANNEL
, curr_channel
);
243 /* After detach callbacks are done, clear data where appropriate */
244 if (prefix
!= NULL
&& curr_prefix
!= NULL
) {
245 OPENSSL_free(curr_prefix
);
246 trace_channels
[category
].prefix
= NULL
;
249 if (suffix
!= NULL
&& curr_suffix
!= NULL
) {
250 OPENSSL_free(curr_suffix
);
251 trace_channels
[category
].suffix
= NULL
;
254 if (channel
!= NULL
&& curr_channel
!= NULL
) {
255 BIO_free(curr_channel
);
256 trace_channels
[category
].bio
= NULL
;
259 /* Before running callbacks are done, set new data where appropriate */
260 if (channel
!= NULL
&& *channel
!= NULL
) {
261 trace_channels
[category
].bio
= *channel
;
264 if (prefix
!= NULL
&& *prefix
!= NULL
) {
265 if ((curr_prefix
= OPENSSL_strdup(*prefix
)) == NULL
)
267 trace_channels
[category
].prefix
= curr_prefix
;
270 if (suffix
!= NULL
&& *suffix
!= NULL
) {
271 if ((curr_suffix
= OPENSSL_strdup(*suffix
)) == NULL
)
273 trace_channels
[category
].suffix
= curr_suffix
;
276 /* Finally, run the attach callback on the new data */
277 if (channel
!= NULL
&& *channel
!= NULL
) {
278 attach_cb(category
, CHANNEL
, *channel
);
281 if (prefix
!= NULL
&& *prefix
!= NULL
) {
282 attach_cb(category
, PREFIX
, *prefix
);
285 if (suffix
!= NULL
&& *suffix
!= NULL
) {
286 attach_cb(category
, SUFFIX
, *suffix
);
293 int ossl_trace_init(void)
295 #ifndef OPENSSL_NO_TRACE
296 trace_lock
= CRYPTO_THREAD_lock_new();
297 if (trace_lock
== NULL
)
304 void ossl_trace_cleanup(void)
306 #ifndef OPENSSL_NO_TRACE
309 const char *prefix
= NULL
;
310 const char *suffix
= NULL
;
312 for (category
= 0; category
< OSSL_TRACE_CATEGORY_NUM
; category
++) {
313 /* We force the TRACE category to be treated last */
314 if (category
== OSSL_TRACE_CATEGORY_TRACE
)
316 set_trace_data(category
, &channel
, &prefix
, &suffix
,
317 trace_attach_cb
, trace_detach_cb
);
319 set_trace_data(OSSL_TRACE_CATEGORY_TRACE
, &channel
, &prefix
, &suffix
,
320 trace_attach_cb
, trace_detach_cb
);
321 CRYPTO_THREAD_lock_free(trace_lock
);
325 int OSSL_trace_set_channel(int category
, BIO
*channel
)
327 #ifndef OPENSSL_NO_TRACE
328 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
329 || !set_trace_data(category
, &channel
, NULL
, NULL
,
330 trace_attach_cb
, trace_detach_cb
))
333 trace_channels
[category
].type
= SIMPLE_CHANNEL
;
338 #ifndef OPENSSL_NO_TRACE
339 static int trace_attach_w_callback_cb(int category
, int type
, const void *data
)
344 "Attach channel %p to category '%s' (with callback)\n",
345 data
, trace_categories
[category
].name
);
348 OSSL_TRACE2(TRACE
, "Attach prefix \"%s\" to category '%s'\n",
349 (const char *)data
, trace_categories
[category
].name
);
352 OSSL_TRACE2(TRACE
, "Attach suffix \"%s\" to category '%s'\n",
353 (const char *)data
, trace_categories
[category
].name
);
355 default: /* No clue */
362 int OSSL_trace_set_callback(int category
, OSSL_trace_cb callback
, void *data
)
364 #ifndef OPENSSL_NO_TRACE
366 struct trace_data_st
*trace_data
= NULL
;
368 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
371 if (callback
!= NULL
) {
372 if ((channel
= BIO_new(&trace_method
)) == NULL
374 OPENSSL_zalloc(sizeof(struct trace_data_st
))) == NULL
)
377 trace_data
->callback
= callback
;
378 trace_data
->category
= category
;
379 trace_data
->data
= data
;
381 BIO_set_data(channel
, trace_data
);
384 if (!set_trace_data(category
, &channel
, NULL
, NULL
,
385 trace_attach_w_callback_cb
, trace_detach_cb
))
388 trace_channels
[category
].type
= CALLBACK_CHANNEL
;
393 OPENSSL_free(trace_data
);
400 int OSSL_trace_set_prefix(int category
, const char *prefix
)
404 #ifndef OPENSSL_NO_TRACE
405 if (category
>= 0 || category
< OSSL_TRACE_CATEGORY_NUM
)
406 return set_trace_data(category
, NULL
, &prefix
, NULL
,
407 trace_attach_cb
, trace_detach_cb
);
413 int OSSL_trace_set_suffix(int category
, const char *suffix
)
417 #ifndef OPENSSL_NO_TRACE
418 if (category
>= 0 || category
< OSSL_TRACE_CATEGORY_NUM
)
419 return set_trace_data(category
, NULL
, NULL
, &suffix
,
420 trace_attach_cb
, trace_detach_cb
);
426 #ifndef OPENSSL_NO_TRACE
427 static int ossl_trace_get_category(int category
)
429 if (category
< 0 || category
>= OSSL_TRACE_CATEGORY_NUM
)
431 if (trace_channels
[category
].bio
!= NULL
)
433 return OSSL_TRACE_CATEGORY_ANY
;
437 int OSSL_trace_enabled(int category
)
440 #ifndef OPENSSL_NO_TRACE
441 category
= ossl_trace_get_category(category
);
442 ret
= trace_channels
[category
].bio
!= NULL
;
447 BIO
*OSSL_trace_begin(int category
)
450 #ifndef OPENSSL_NO_TRACE
453 category
= ossl_trace_get_category(category
);
454 channel
= trace_channels
[category
].bio
;
455 prefix
= trace_channels
[category
].prefix
;
457 if (channel
!= NULL
) {
458 CRYPTO_THREAD_write_lock(trace_lock
);
459 current_channel
= channel
;
460 switch (trace_channels
[category
].type
) {
462 if (prefix
!= NULL
) {
463 (void)BIO_puts(channel
, prefix
);
464 (void)BIO_puts(channel
, "\n");
467 case CALLBACK_CHANNEL
:
468 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_BEGIN
,
469 prefix
== NULL
? 0 : strlen(prefix
), prefix
);
477 void OSSL_trace_end(int category
, BIO
* channel
)
479 #ifndef OPENSSL_NO_TRACE
482 category
= ossl_trace_get_category(category
);
483 suffix
= trace_channels
[category
].suffix
;
485 && ossl_assert(channel
== current_channel
)) {
486 (void)BIO_flush(channel
);
487 switch (trace_channels
[category
].type
) {
489 if (suffix
!= NULL
) {
490 (void)BIO_puts(channel
, suffix
);
491 (void)BIO_puts(channel
, "\n");
494 case CALLBACK_CHANNEL
:
495 (void)BIO_ctrl(channel
, OSSL_TRACE_CTRL_END
,
496 suffix
== NULL
? 0 : strlen(suffix
), suffix
);
499 current_channel
= NULL
;
500 CRYPTO_THREAD_unlock(trace_lock
);