]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/trace.c
ENCODER: Add tracing
[thirdparty/openssl.git] / crypto / trace.c
CommitLineData
2390c573 1/*
e39e295e 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
2390c573
RL
3 *
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
8 */
9
10#include <stdio.h>
11#include <string.h>
12
cf0932cd 13#include "internal/thread_once.h"
2390c573
RL
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"
25f2138b 19#include "crypto/cryptlib.h"
2390c573
RL
20
21#include "e_os.h" /* strcasecmp for Windows */
22
16a9d374
RL
23#ifndef OPENSSL_NO_TRACE
24
2390c573
RL
25static CRYPTO_RWLOCK *trace_lock = NULL;
26
27static const BIO *current_channel = NULL;
28
29/*-
30 * INTERNAL TRACE CHANNEL IMPLEMENTATION
31 *
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
36 * callback function.
37 */
38static int trace_write(BIO *b, const char *buf,
39 size_t num, size_t *written);
40static int trace_puts(BIO *b, const char *str);
41static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
42static int trace_free(BIO *b);
43
44static const BIO_METHOD trace_method = {
45 BIO_TYPE_SOURCE_SINK,
46 "trace",
47 trace_write,
48 NULL, /* old write */
49 NULL, /* read_ex */
50 NULL, /* read */
51 trace_puts,
52 NULL, /* gets */
53 trace_ctrl, /* ctrl */
54 NULL, /* create */
55 trace_free, /* free */
56 NULL, /* callback_ctrl */
57};
58
59struct trace_data_st {
60 OSSL_trace_cb callback;
61 int category;
62 void *data;
63};
64
65static int trace_write(BIO *channel,
66 const char *buf, size_t num, size_t *written)
67{
68 struct trace_data_st *ctx = BIO_get_data(channel);
13d06925 69 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
2390c573
RL
70 ctx->data);
71
72 *written = cnt;
73 return cnt != 0;
74}
75
76static int trace_puts(BIO *channel, const char *str)
77{
78 size_t written;
79
80 if (trace_write(channel, str, strlen(str), &written))
81 return (int)written;
82
83 return EOF;
84}
85
86static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
87{
88 struct trace_data_st *ctx = BIO_get_data(channel);
89
90 switch (cmd) {
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);
95 return 1;
96 default:
97 break;
98 }
99 return -2; /* Unsupported */
100}
101
102static int trace_free(BIO *channel)
103{
104 if (channel == NULL)
105 return 0;
106 OPENSSL_free(BIO_get_data(channel));
107 return 1;
108}
16a9d374 109#endif
2390c573
RL
110
111/*-
112 * TRACE
113 */
114
115/* Helper struct and macro to get name string to number mapping */
116struct trace_category_st {
117 const char * const name;
118 const int num;
119};
120#define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
121
122static const struct trace_category_st trace_categories[] = {
3a8269b3 123 TRACE_CATEGORY_(ALL),
3b9e1a39 124 TRACE_CATEGORY_(TRACE),
5c641735 125 TRACE_CATEGORY_(INIT),
49b26f54 126 TRACE_CATEGORY_(TLS),
77359d22 127 TRACE_CATEGORY_(TLS_CIPHER),
bc362b9b 128 TRACE_CATEGORY_(CONF),
184fb690 129#ifndef OPENSSL_NO_ENGINE
f272be67 130 TRACE_CATEGORY_(ENGINE_TABLE),
f518e3e8 131 TRACE_CATEGORY_(ENGINE_REF_COUNT),
184fb690 132#endif
3a9b3d2d 133 TRACE_CATEGORY_(PKCS5V2),
a902e43d 134 TRACE_CATEGORY_(PKCS12_KEYGEN),
5f8a5f46 135 TRACE_CATEGORY_(PKCS12_DECRYPT),
b9ce85f6 136 TRACE_CATEGORY_(X509V3_POLICY),
6e810f2d 137 TRACE_CATEGORY_(BN_CTX),
2897b009 138 TRACE_CATEGORY_(STORE),
de5008a4 139 TRACE_CATEGORY_(DECODER),
0b9f90f5 140 TRACE_CATEGORY_(ENCODER),
2390c573
RL
141};
142
143const char *OSSL_trace_get_category_name(int num)
144{
145 size_t i;
146
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 */
151}
152
153int OSSL_trace_get_category_num(const char *name)
154{
155 size_t i;
156
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 */
161}
162
16a9d374
RL
163#ifndef OPENSSL_NO_TRACE
164
2390c573
RL
165/* We use one trace channel for each trace category */
166static struct {
5afb177c 167 enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
2390c573
RL
168 BIO *bio;
169 char *prefix;
170 char *suffix;
171} trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
172 { 0, NULL, NULL, NULL },
173};
174
16a9d374
RL
175#endif
176
3b9e1a39 177#ifndef OPENSSL_NO_TRACE
e474a286
DMSP
178
179enum {
180 CHANNEL,
181 PREFIX,
182 SUFFIX
183};
184
3b9e1a39
RL
185static int trace_attach_cb(int category, int type, const void *data)
186{
187 switch (type) {
e474a286 188 case CHANNEL:
3b9e1a39
RL
189 OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
190 data, trace_categories[category].name);
191 break;
e474a286 192 case PREFIX:
3b9e1a39
RL
193 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
194 (const char *)data, trace_categories[category].name);
195 break;
e474a286 196 case SUFFIX:
3b9e1a39
RL
197 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
198 (const char *)data, trace_categories[category].name);
199 break;
200 default: /* No clue */
201 break;
202 }
203 return 1;
204}
205
206static int trace_detach_cb(int category, int type, const void *data)
207{
208 switch (type) {
e474a286 209 case CHANNEL:
3b9e1a39
RL
210 OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
211 data, trace_categories[category].name);
212 break;
e474a286 213 case PREFIX:
3b9e1a39
RL
214 OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
215 (const char *)data, trace_categories[category].name);
216 break;
e474a286 217 case SUFFIX:
3b9e1a39
RL
218 OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
219 (const char *)data, trace_categories[category].name);
220 break;
221 default: /* No clue */
222 break;
223 }
224 return 1;
225}
226
cf0932cd
DDO
227static int do_ossl_trace_init(void);
228static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
229DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
230{
231 return do_ossl_trace_init();
232}
233
fe50e115 234static int set_trace_data(int category, int type, BIO **channel,
3b9e1a39
RL
235 const char **prefix, const char **suffix,
236 int (*attach_cb)(int, int, const void *),
237 int (*detach_cb)(int, int, const void *))
238{
fe26f798
RL
239 BIO *curr_channel = NULL;
240 char *curr_prefix = NULL;
241 char *curr_suffix = NULL;
242
cf0932cd
DDO
243 /* Ensure do_ossl_trace_init() is called once */
244 if (!RUN_ONCE(&trace_inited, ossl_trace_init))
245 return 0;
fe26f798
RL
246
247 curr_channel = trace_channels[category].bio;
248 curr_prefix = trace_channels[category].prefix;
249 curr_suffix = trace_channels[category].suffix;
3b9e1a39
RL
250
251 /* Make sure to run the detach callback first on all data */
252 if (prefix != NULL && curr_prefix != NULL) {
e474a286 253 detach_cb(category, PREFIX, curr_prefix);
3b9e1a39
RL
254 }
255
256 if (suffix != NULL && curr_suffix != NULL) {
e474a286 257 detach_cb(category, SUFFIX, curr_suffix);
3b9e1a39
RL
258 }
259
260 if (channel != NULL && curr_channel != NULL) {
e474a286 261 detach_cb(category, CHANNEL, curr_channel);
3b9e1a39
RL
262 }
263
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;
268 }
269
270 if (suffix != NULL && curr_suffix != NULL) {
271 OPENSSL_free(curr_suffix);
272 trace_channels[category].suffix = NULL;
273 }
274
275 if (channel != NULL && curr_channel != NULL) {
276 BIO_free(curr_channel);
fe50e115 277 trace_channels[category].type = 0;
3b9e1a39
RL
278 trace_channels[category].bio = NULL;
279 }
280
281 /* Before running callbacks are done, set new data where appropriate */
282 if (channel != NULL && *channel != NULL) {
fe50e115 283 trace_channels[category].type = type;
3b9e1a39
RL
284 trace_channels[category].bio = *channel;
285 }
286
287 if (prefix != NULL && *prefix != NULL) {
288 if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
289 return 0;
290 trace_channels[category].prefix = curr_prefix;
291 }
292
293 if (suffix != NULL && *suffix != NULL) {
294 if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
295 return 0;
296 trace_channels[category].suffix = curr_suffix;
297 }
298
299 /* Finally, run the attach callback on the new data */
300 if (channel != NULL && *channel != NULL) {
e474a286 301 attach_cb(category, CHANNEL, *channel);
3b9e1a39
RL
302 }
303
304 if (prefix != NULL && *prefix != NULL) {
e474a286 305 attach_cb(category, PREFIX, *prefix);
3b9e1a39
RL
306 }
307
308 if (suffix != NULL && *suffix != NULL) {
e474a286 309 attach_cb(category, SUFFIX, *suffix);
3b9e1a39
RL
310 }
311
312 return 1;
313}
3b9e1a39 314
cf0932cd 315static int do_ossl_trace_init(void)
2390c573
RL
316{
317 trace_lock = CRYPTO_THREAD_lock_new();
cf0932cd 318 return trace_lock != NULL;
2390c573
RL
319}
320
cf0932cd
DDO
321#endif
322
2390c573
RL
323void ossl_trace_cleanup(void)
324{
16a9d374 325#ifndef OPENSSL_NO_TRACE
2390c573 326 int category;
3b9e1a39
RL
327 BIO *channel = NULL;
328 const char *prefix = NULL;
329 const char *suffix = NULL;
330
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)
334 continue;
fe50e115 335 set_trace_data(category, 0, &channel, &prefix, &suffix,
3b9e1a39
RL
336 trace_attach_cb, trace_detach_cb);
337 }
fe50e115
DMSP
338 set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
339 &prefix, &suffix,
3b9e1a39 340 trace_attach_cb, trace_detach_cb);
2390c573 341 CRYPTO_THREAD_lock_free(trace_lock);
16a9d374 342#endif
2390c573
RL
343}
344
345int OSSL_trace_set_channel(int category, BIO *channel)
346{
16a9d374 347#ifndef OPENSSL_NO_TRACE
0fda9f7c
DMSP
348 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
349 return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
02bd2d7f 350 trace_attach_cb, trace_detach_cb);
16a9d374 351#endif
0fda9f7c 352 return 0;
2390c573
RL
353}
354
3b9e1a39
RL
355#ifndef OPENSSL_NO_TRACE
356static int trace_attach_w_callback_cb(int category, int type, const void *data)
357{
358 switch (type) {
e474a286 359 case CHANNEL:
3b9e1a39
RL
360 OSSL_TRACE2(TRACE,
361 "Attach channel %p to category '%s' (with callback)\n",
362 data, trace_categories[category].name);
363 break;
e474a286 364 case PREFIX:
3b9e1a39
RL
365 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
366 (const char *)data, trace_categories[category].name);
367 break;
e474a286 368 case SUFFIX:
3b9e1a39
RL
369 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
370 (const char *)data, trace_categories[category].name);
371 break;
372 default: /* No clue */
373 break;
374 }
375 return 1;
376}
377#endif
378
2390c573
RL
379int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
380{
16a9d374 381#ifndef OPENSSL_NO_TRACE
3b9e1a39 382 BIO *channel = NULL;
2390c573
RL
383 struct trace_data_st *trace_data = NULL;
384
3b9e1a39 385 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
0fda9f7c 386 return 0;
2390c573 387
3b9e1a39
RL
388 if (callback != NULL) {
389 if ((channel = BIO_new(&trace_method)) == NULL
390 || (trace_data =
391 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
392 goto err;
2390c573 393
3b9e1a39
RL
394 trace_data->callback = callback;
395 trace_data->category = category;
396 trace_data->data = data;
2390c573 397
3b9e1a39
RL
398 BIO_set_data(channel, trace_data);
399 }
2390c573 400
fe50e115 401 if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
3b9e1a39
RL
402 trace_attach_w_callback_cb, trace_detach_cb))
403 goto err;
2390c573 404
0fda9f7c 405 return 1;
2390c573
RL
406
407 err:
408 BIO_free(channel);
409 OPENSSL_free(trace_data);
d33d7616 410#endif
0fda9f7c
DMSP
411
412 return 0;
2390c573
RL
413}
414
415int OSSL_trace_set_prefix(int category, const char *prefix)
416{
16a9d374 417#ifndef OPENSSL_NO_TRACE
0fda9f7c 418 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
fe50e115 419 return set_trace_data(category, 0, NULL, &prefix, NULL,
3b9e1a39 420 trace_attach_cb, trace_detach_cb);
16a9d374 421#endif
0fda9f7c 422 return 0;
2390c573
RL
423}
424
425int OSSL_trace_set_suffix(int category, const char *suffix)
426{
16a9d374 427#ifndef OPENSSL_NO_TRACE
0fda9f7c 428 if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
fe50e115 429 return set_trace_data(category, 0, NULL, NULL, &suffix,
3b9e1a39 430 trace_attach_cb, trace_detach_cb);
16a9d374 431#endif
0fda9f7c 432 return 0;
2390c573
RL
433}
434
16a9d374 435#ifndef OPENSSL_NO_TRACE
2390c573
RL
436static int ossl_trace_get_category(int category)
437{
438 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
439 return -1;
440 if (trace_channels[category].bio != NULL)
441 return category;
3a8269b3 442 return OSSL_TRACE_CATEGORY_ALL;
2390c573 443}
16a9d374 444#endif
2390c573
RL
445
446int OSSL_trace_enabled(int category)
447{
448 int ret = 0;
16a9d374 449#ifndef OPENSSL_NO_TRACE
2390c573 450 category = ossl_trace_get_category(category);
6a411436
DMSP
451 if (category >= 0)
452 ret = trace_channels[category].bio != NULL;
16a9d374 453#endif
2390c573
RL
454 return ret;
455}
456
457BIO *OSSL_trace_begin(int category)
458{
459 BIO *channel = NULL;
16a9d374 460#ifndef OPENSSL_NO_TRACE
2390c573
RL
461 char *prefix = NULL;
462
463 category = ossl_trace_get_category(category);
6a411436
DMSP
464 if (category < 0)
465 return NULL;
466
2390c573
RL
467 channel = trace_channels[category].bio;
468 prefix = trace_channels[category].prefix;
469
470 if (channel != NULL) {
471 CRYPTO_THREAD_write_lock(trace_lock);
472 current_channel = channel;
473 switch (trace_channels[category].type) {
5afb177c 474 case SIMPLE_CHANNEL:
2390c573
RL
475 if (prefix != NULL) {
476 (void)BIO_puts(channel, prefix);
477 (void)BIO_puts(channel, "\n");
478 }
479 break;
5afb177c 480 case CALLBACK_CHANNEL:
2390c573
RL
481 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
482 prefix == NULL ? 0 : strlen(prefix), prefix);
483 break;
484 }
485 }
16a9d374 486#endif
2390c573
RL
487 return channel;
488}
489
490void OSSL_trace_end(int category, BIO * channel)
491{
16a9d374 492#ifndef OPENSSL_NO_TRACE
2390c573
RL
493 char *suffix = NULL;
494
495 category = ossl_trace_get_category(category);
496 suffix = trace_channels[category].suffix;
497 if (channel != NULL
498 && ossl_assert(channel == current_channel)) {
499 (void)BIO_flush(channel);
500 switch (trace_channels[category].type) {
5afb177c 501 case SIMPLE_CHANNEL:
2390c573
RL
502 if (suffix != NULL) {
503 (void)BIO_puts(channel, suffix);
504 (void)BIO_puts(channel, "\n");
505 }
506 break;
5afb177c 507 case CALLBACK_CHANNEL:
2390c573
RL
508 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
509 suffix == NULL ? 0 : strlen(suffix), suffix);
510 break;
511 }
512 current_channel = NULL;
513 CRYPTO_THREAD_unlock(trace_lock);
514 }
16a9d374 515#endif
2390c573 516}