VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.2/crypto/trace.c@ 95087

Last change on this file since 95087 was 94404, checked in by vboxsync, 3 years ago

libs/openssl: Update to 3.0.2 and switch to it, bugref:10128

File size: 14.3 KB
Line 
1/*
2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
21
22#include "e_os.h" /* strcasecmp for Windows */
23
24#ifndef OPENSSL_NO_TRACE
25
26static CRYPTO_RWLOCK *trace_lock = NULL;
27
28static const BIO *current_channel = NULL;
29
30/*-
31 * INTERNAL TRACE CHANNEL IMPLEMENTATION
32 *
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
37 * callback function.
38 */
39static int trace_write(BIO *b, const char *buf,
40 size_t num, size_t *written);
41static int trace_puts(BIO *b, const char *str);
42static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp);
43static int trace_free(BIO *b);
44
45static const BIO_METHOD trace_method = {
46 BIO_TYPE_SOURCE_SINK,
47 "trace",
48 trace_write,
49 NULL, /* old write */
50 NULL, /* read_ex */
51 NULL, /* read */
52 trace_puts,
53 NULL, /* gets */
54 trace_ctrl, /* ctrl */
55 NULL, /* create */
56 trace_free, /* free */
57 NULL, /* callback_ctrl */
58};
59
60struct trace_data_st {
61 OSSL_trace_cb callback;
62 int category;
63 void *data;
64};
65
66static int trace_write(BIO *channel,
67 const char *buf, size_t num, size_t *written)
68{
69 struct trace_data_st *ctx = BIO_get_data(channel);
70 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE,
71 ctx->data);
72
73 *written = cnt;
74 return cnt != 0;
75}
76
77static int trace_puts(BIO *channel, const char *str)
78{
79 size_t written;
80
81 if (trace_write(channel, str, strlen(str), &written))
82 return (int)written;
83
84 return EOF;
85}
86
87static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp)
88{
89 struct trace_data_st *ctx = BIO_get_data(channel);
90
91 switch (cmd) {
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);
96 return 1;
97 default:
98 break;
99 }
100 return -2; /* Unsupported */
101}
102
103static int trace_free(BIO *channel)
104{
105 if (channel == NULL)
106 return 0;
107 OPENSSL_free(BIO_get_data(channel));
108 return 1;
109}
110#endif
111
112/*-
113 * TRACE
114 */
115
116/* Helper struct and macro to get name string to number mapping */
117struct trace_category_st {
118 const char * const name;
119 const int num;
120};
121#define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name }
122
123static 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),
133#endif
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)
144};
145
146const char *OSSL_trace_get_category_name(int num)
147{
148 size_t i;
149
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 */
154}
155
156int OSSL_trace_get_category_num(const char *name)
157{
158 size_t i;
159
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 */
164}
165
166#ifndef OPENSSL_NO_TRACE
167
168/* We use one trace channel for each trace category */
169static struct {
170 enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
171 BIO *bio;
172 char *prefix;
173 char *suffix;
174} trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
175 { 0, NULL, NULL, NULL },
176};
177
178#endif
179
180#ifndef OPENSSL_NO_TRACE
181
182enum {
183 CHANNEL,
184 PREFIX,
185 SUFFIX
186};
187
188static int trace_attach_cb(int category, int type, const void *data)
189{
190 switch (type) {
191 case CHANNEL:
192 OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
193 data, trace_categories[category].name);
194 break;
195 case PREFIX:
196 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
197 (const char *)data, trace_categories[category].name);
198 break;
199 case SUFFIX:
200 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
201 (const char *)data, trace_categories[category].name);
202 break;
203 default: /* No clue */
204 break;
205 }
206 return 1;
207}
208
209static int trace_detach_cb(int category, int type, const void *data)
210{
211 switch (type) {
212 case CHANNEL:
213 OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
214 data, trace_categories[category].name);
215 break;
216 case PREFIX:
217 OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
218 (const char *)data, trace_categories[category].name);
219 break;
220 case SUFFIX:
221 OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
222 (const char *)data, trace_categories[category].name);
223 break;
224 default: /* No clue */
225 break;
226 }
227 return 1;
228}
229
230static int do_ossl_trace_init(void);
231static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
232DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
233{
234 return do_ossl_trace_init();
235}
236
237static 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 *))
241{
242 BIO *curr_channel = NULL;
243 char *curr_prefix = NULL;
244 char *curr_suffix = NULL;
245
246 /* Ensure do_ossl_trace_init() is called once */
247 if (!RUN_ONCE(&trace_inited, ossl_trace_init))
248 return 0;
249
250 curr_channel = trace_channels[category].bio;
251 curr_prefix = trace_channels[category].prefix;
252 curr_suffix = trace_channels[category].suffix;
253
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);
257 }
258
259 if (suffix != NULL && curr_suffix != NULL) {
260 detach_cb(category, SUFFIX, curr_suffix);
261 }
262
263 if (channel != NULL && curr_channel != NULL) {
264 detach_cb(category, CHANNEL, curr_channel);
265 }
266
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;
271 }
272
273 if (suffix != NULL && curr_suffix != NULL) {
274 OPENSSL_free(curr_suffix);
275 trace_channels[category].suffix = NULL;
276 }
277
278 if (channel != NULL && curr_channel != NULL) {
279 BIO_free(curr_channel);
280 trace_channels[category].type = 0;
281 trace_channels[category].bio = NULL;
282 }
283
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;
288 }
289
290 if (prefix != NULL && *prefix != NULL) {
291 if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
292 return 0;
293 trace_channels[category].prefix = curr_prefix;
294 }
295
296 if (suffix != NULL && *suffix != NULL) {
297 if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
298 return 0;
299 trace_channels[category].suffix = curr_suffix;
300 }
301
302 /* Finally, run the attach callback on the new data */
303 if (channel != NULL && *channel != NULL) {
304 attach_cb(category, CHANNEL, *channel);
305 }
306
307 if (prefix != NULL && *prefix != NULL) {
308 attach_cb(category, PREFIX, *prefix);
309 }
310
311 if (suffix != NULL && *suffix != NULL) {
312 attach_cb(category, SUFFIX, *suffix);
313 }
314
315 return 1;
316}
317
318static int do_ossl_trace_init(void)
319{
320 trace_lock = CRYPTO_THREAD_lock_new();
321 return trace_lock != NULL;
322}
323
324#endif
325
326void ossl_trace_cleanup(void)
327{
328#ifndef OPENSSL_NO_TRACE
329 int category;
330 BIO *channel = NULL;
331 const char *prefix = NULL;
332 const char *suffix = NULL;
333
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)
337 continue;
338 set_trace_data(category, 0, &channel, &prefix, &suffix,
339 trace_attach_cb, trace_detach_cb);
340 }
341 set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
342 &prefix, &suffix,
343 trace_attach_cb, trace_detach_cb);
344 CRYPTO_THREAD_lock_free(trace_lock);
345#endif
346}
347
348int OSSL_trace_set_channel(int category, BIO *channel)
349{
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);
354#endif
355 return 0;
356}
357
358#ifndef OPENSSL_NO_TRACE
359static int trace_attach_w_callback_cb(int category, int type, const void *data)
360{
361 switch (type) {
362 case CHANNEL:
363 OSSL_TRACE2(TRACE,
364 "Attach channel %p to category '%s' (with callback)\n",
365 data, trace_categories[category].name);
366 break;
367 case PREFIX:
368 OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
369 (const char *)data, trace_categories[category].name);
370 break;
371 case SUFFIX:
372 OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
373 (const char *)data, trace_categories[category].name);
374 break;
375 default: /* No clue */
376 break;
377 }
378 return 1;
379}
380#endif
381
382int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
383{
384#ifndef OPENSSL_NO_TRACE
385 BIO *channel = NULL;
386 struct trace_data_st *trace_data = NULL;
387
388 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
389 return 0;
390
391 if (callback != NULL) {
392 if ((channel = BIO_new(&trace_method)) == NULL
393 || (trace_data =
394 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
395 goto err;
396
397 trace_data->callback = callback;
398 trace_data->category = category;
399 trace_data->data = data;
400
401 BIO_set_data(channel, trace_data);
402 }
403
404 if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
405 trace_attach_w_callback_cb, trace_detach_cb))
406 goto err;
407
408 return 1;
409
410 err:
411 BIO_free(channel);
412 OPENSSL_free(trace_data);
413#endif
414
415 return 0;
416}
417
418int OSSL_trace_set_prefix(int category, const char *prefix)
419{
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);
424#endif
425 return 0;
426}
427
428int OSSL_trace_set_suffix(int category, const char *suffix)
429{
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);
434#endif
435 return 0;
436}
437
438#ifndef OPENSSL_NO_TRACE
439static int ossl_trace_get_category(int category)
440{
441 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
442 return -1;
443 if (trace_channels[category].bio != NULL)
444 return category;
445 return OSSL_TRACE_CATEGORY_ALL;
446}
447#endif
448
449int OSSL_trace_enabled(int category)
450{
451 int ret = 0;
452#ifndef OPENSSL_NO_TRACE
453 category = ossl_trace_get_category(category);
454 if (category >= 0)
455 ret = trace_channels[category].bio != NULL;
456#endif
457 return ret;
458}
459
460BIO *OSSL_trace_begin(int category)
461{
462 BIO *channel = NULL;
463#ifndef OPENSSL_NO_TRACE
464 char *prefix = NULL;
465
466 category = ossl_trace_get_category(category);
467 if (category < 0)
468 return NULL;
469
470 channel = trace_channels[category].bio;
471 prefix = trace_channels[category].prefix;
472
473 if (channel != NULL) {
474 if (!CRYPTO_THREAD_write_lock(trace_lock))
475 return NULL;
476 current_channel = channel;
477 switch (trace_channels[category].type) {
478 case SIMPLE_CHANNEL:
479 if (prefix != NULL) {
480 (void)BIO_puts(channel, prefix);
481 (void)BIO_puts(channel, "\n");
482 }
483 break;
484 case CALLBACK_CHANNEL:
485 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
486 prefix == NULL ? 0 : strlen(prefix), prefix);
487 break;
488 }
489 }
490#endif
491 return channel;
492}
493
494void OSSL_trace_end(int category, BIO * channel)
495{
496#ifndef OPENSSL_NO_TRACE
497 char *suffix = NULL;
498
499 category = ossl_trace_get_category(category);
500 if (category < 0)
501 return;
502 suffix = trace_channels[category].suffix;
503 if (channel != NULL
504 && ossl_assert(channel == current_channel)) {
505 (void)BIO_flush(channel);
506 switch (trace_channels[category].type) {
507 case SIMPLE_CHANNEL:
508 if (suffix != NULL) {
509 (void)BIO_puts(channel, suffix);
510 (void)BIO_puts(channel, "\n");
511 }
512 break;
513 case CALLBACK_CHANNEL:
514 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
515 suffix == NULL ? 0 : strlen(suffix), suffix);
516 break;
517 }
518 current_channel = NULL;
519 CRYPTO_THREAD_unlock(trace_lock);
520 }
521#endif
522}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette