]>
Commit | Line | Data |
---|---|---|
2390c573 RL |
1 | /* |
2 | * Copyright 2019 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 | ||
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" | |
19 | #include "internal/cryptlib_int.h" | |
20 | ||
21 | #include "e_os.h" /* strcasecmp for Windows */ | |
22 | ||
16a9d374 RL |
23 | #ifndef OPENSSL_NO_TRACE |
24 | ||
2390c573 RL |
25 | static CRYPTO_RWLOCK *trace_lock = NULL; |
26 | ||
27 | static 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 | */ | |
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); | |
43 | ||
44 | static 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 | ||
59 | struct trace_data_st { | |
60 | OSSL_trace_cb callback; | |
61 | int category; | |
62 | void *data; | |
63 | }; | |
64 | ||
65 | static 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 | ||
76 | static 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 | ||
86 | static 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 | ||
102 | static 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 */ | |
116 | struct trace_category_st { | |
117 | const char * const name; | |
118 | const int num; | |
119 | }; | |
120 | #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name } | |
121 | ||
122 | static 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), |
f272be67 | 129 | TRACE_CATEGORY_(ENGINE_TABLE), |
f518e3e8 | 130 | TRACE_CATEGORY_(ENGINE_REF_COUNT), |
3a9b3d2d | 131 | TRACE_CATEGORY_(PKCS5V2), |
a902e43d | 132 | TRACE_CATEGORY_(PKCS12_KEYGEN), |
5f8a5f46 | 133 | TRACE_CATEGORY_(PKCS12_DECRYPT), |
b9ce85f6 | 134 | TRACE_CATEGORY_(X509V3_POLICY), |
6e810f2d | 135 | TRACE_CATEGORY_(BN_CTX), |
2390c573 RL |
136 | }; |
137 | ||
138 | const char *OSSL_trace_get_category_name(int num) | |
139 | { | |
140 | size_t i; | |
141 | ||
142 | for (i = 0; i < OSSL_NELEM(trace_categories); i++) | |
143 | if (trace_categories[i].num == num) | |
144 | return trace_categories[i].name; | |
145 | return NULL; /* not found */ | |
146 | } | |
147 | ||
148 | int OSSL_trace_get_category_num(const char *name) | |
149 | { | |
150 | size_t i; | |
151 | ||
152 | for (i = 0; i < OSSL_NELEM(trace_categories); i++) | |
153 | if (strcasecmp(name, trace_categories[i].name) == 0) | |
154 | return trace_categories[i].num; | |
155 | return -1; /* not found */ | |
156 | } | |
157 | ||
16a9d374 RL |
158 | #ifndef OPENSSL_NO_TRACE |
159 | ||
2390c573 RL |
160 | /* We use one trace channel for each trace category */ |
161 | static struct { | |
5afb177c | 162 | enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type; |
2390c573 RL |
163 | BIO *bio; |
164 | char *prefix; | |
165 | char *suffix; | |
166 | } trace_channels[OSSL_TRACE_CATEGORY_NUM] = { | |
167 | { 0, NULL, NULL, NULL }, | |
168 | }; | |
169 | ||
16a9d374 RL |
170 | #endif |
171 | ||
3b9e1a39 | 172 | #ifndef OPENSSL_NO_TRACE |
e474a286 DMSP |
173 | |
174 | enum { | |
175 | CHANNEL, | |
176 | PREFIX, | |
177 | SUFFIX | |
178 | }; | |
179 | ||
3b9e1a39 RL |
180 | static int trace_attach_cb(int category, int type, const void *data) |
181 | { | |
182 | switch (type) { | |
e474a286 | 183 | case CHANNEL: |
3b9e1a39 RL |
184 | OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n", |
185 | data, trace_categories[category].name); | |
186 | break; | |
e474a286 | 187 | case PREFIX: |
3b9e1a39 RL |
188 | OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n", |
189 | (const char *)data, trace_categories[category].name); | |
190 | break; | |
e474a286 | 191 | case SUFFIX: |
3b9e1a39 RL |
192 | OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n", |
193 | (const char *)data, trace_categories[category].name); | |
194 | break; | |
195 | default: /* No clue */ | |
196 | break; | |
197 | } | |
198 | return 1; | |
199 | } | |
200 | ||
201 | static int trace_detach_cb(int category, int type, const void *data) | |
202 | { | |
203 | switch (type) { | |
e474a286 | 204 | case CHANNEL: |
3b9e1a39 RL |
205 | OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n", |
206 | data, trace_categories[category].name); | |
207 | break; | |
e474a286 | 208 | case PREFIX: |
3b9e1a39 RL |
209 | OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n", |
210 | (const char *)data, trace_categories[category].name); | |
211 | break; | |
e474a286 | 212 | case SUFFIX: |
3b9e1a39 RL |
213 | OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n", |
214 | (const char *)data, trace_categories[category].name); | |
215 | break; | |
216 | default: /* No clue */ | |
217 | break; | |
218 | } | |
219 | return 1; | |
220 | } | |
221 | ||
cf0932cd DDO |
222 | static int do_ossl_trace_init(void); |
223 | static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT; | |
224 | DEFINE_RUN_ONCE_STATIC(ossl_trace_init) | |
225 | { | |
226 | return do_ossl_trace_init(); | |
227 | } | |
228 | ||
fe50e115 | 229 | static int set_trace_data(int category, int type, BIO **channel, |
3b9e1a39 RL |
230 | const char **prefix, const char **suffix, |
231 | int (*attach_cb)(int, int, const void *), | |
232 | int (*detach_cb)(int, int, const void *)) | |
233 | { | |
fe26f798 RL |
234 | BIO *curr_channel = NULL; |
235 | char *curr_prefix = NULL; | |
236 | char *curr_suffix = NULL; | |
237 | ||
cf0932cd DDO |
238 | /* Ensure do_ossl_trace_init() is called once */ |
239 | if (!RUN_ONCE(&trace_inited, ossl_trace_init)) | |
240 | return 0; | |
fe26f798 RL |
241 | |
242 | curr_channel = trace_channels[category].bio; | |
243 | curr_prefix = trace_channels[category].prefix; | |
244 | curr_suffix = trace_channels[category].suffix; | |
3b9e1a39 RL |
245 | |
246 | /* Make sure to run the detach callback first on all data */ | |
247 | if (prefix != NULL && curr_prefix != NULL) { | |
e474a286 | 248 | detach_cb(category, PREFIX, curr_prefix); |
3b9e1a39 RL |
249 | } |
250 | ||
251 | if (suffix != NULL && curr_suffix != NULL) { | |
e474a286 | 252 | detach_cb(category, SUFFIX, curr_suffix); |
3b9e1a39 RL |
253 | } |
254 | ||
255 | if (channel != NULL && curr_channel != NULL) { | |
e474a286 | 256 | detach_cb(category, CHANNEL, curr_channel); |
3b9e1a39 RL |
257 | } |
258 | ||
259 | /* After detach callbacks are done, clear data where appropriate */ | |
260 | if (prefix != NULL && curr_prefix != NULL) { | |
261 | OPENSSL_free(curr_prefix); | |
262 | trace_channels[category].prefix = NULL; | |
263 | } | |
264 | ||
265 | if (suffix != NULL && curr_suffix != NULL) { | |
266 | OPENSSL_free(curr_suffix); | |
267 | trace_channels[category].suffix = NULL; | |
268 | } | |
269 | ||
270 | if (channel != NULL && curr_channel != NULL) { | |
271 | BIO_free(curr_channel); | |
fe50e115 | 272 | trace_channels[category].type = 0; |
3b9e1a39 RL |
273 | trace_channels[category].bio = NULL; |
274 | } | |
275 | ||
276 | /* Before running callbacks are done, set new data where appropriate */ | |
277 | if (channel != NULL && *channel != NULL) { | |
fe50e115 | 278 | trace_channels[category].type = type; |
3b9e1a39 RL |
279 | trace_channels[category].bio = *channel; |
280 | } | |
281 | ||
282 | if (prefix != NULL && *prefix != NULL) { | |
283 | if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL) | |
284 | return 0; | |
285 | trace_channels[category].prefix = curr_prefix; | |
286 | } | |
287 | ||
288 | if (suffix != NULL && *suffix != NULL) { | |
289 | if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL) | |
290 | return 0; | |
291 | trace_channels[category].suffix = curr_suffix; | |
292 | } | |
293 | ||
294 | /* Finally, run the attach callback on the new data */ | |
295 | if (channel != NULL && *channel != NULL) { | |
e474a286 | 296 | attach_cb(category, CHANNEL, *channel); |
3b9e1a39 RL |
297 | } |
298 | ||
299 | if (prefix != NULL && *prefix != NULL) { | |
e474a286 | 300 | attach_cb(category, PREFIX, *prefix); |
3b9e1a39 RL |
301 | } |
302 | ||
303 | if (suffix != NULL && *suffix != NULL) { | |
e474a286 | 304 | attach_cb(category, SUFFIX, *suffix); |
3b9e1a39 RL |
305 | } |
306 | ||
307 | return 1; | |
308 | } | |
3b9e1a39 | 309 | |
cf0932cd | 310 | static int do_ossl_trace_init(void) |
2390c573 RL |
311 | { |
312 | trace_lock = CRYPTO_THREAD_lock_new(); | |
cf0932cd | 313 | return trace_lock != NULL; |
2390c573 RL |
314 | } |
315 | ||
cf0932cd DDO |
316 | #endif |
317 | ||
2390c573 RL |
318 | void ossl_trace_cleanup(void) |
319 | { | |
16a9d374 | 320 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 321 | int category; |
3b9e1a39 RL |
322 | BIO *channel = NULL; |
323 | const char *prefix = NULL; | |
324 | const char *suffix = NULL; | |
325 | ||
326 | for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) { | |
327 | /* We force the TRACE category to be treated last */ | |
328 | if (category == OSSL_TRACE_CATEGORY_TRACE) | |
329 | continue; | |
fe50e115 | 330 | set_trace_data(category, 0, &channel, &prefix, &suffix, |
3b9e1a39 RL |
331 | trace_attach_cb, trace_detach_cb); |
332 | } | |
fe50e115 DMSP |
333 | set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel, |
334 | &prefix, &suffix, | |
3b9e1a39 | 335 | trace_attach_cb, trace_detach_cb); |
2390c573 | 336 | CRYPTO_THREAD_lock_free(trace_lock); |
16a9d374 | 337 | #endif |
2390c573 RL |
338 | } |
339 | ||
340 | int OSSL_trace_set_channel(int category, BIO *channel) | |
341 | { | |
16a9d374 | 342 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c DMSP |
343 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
344 | return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL, | |
02bd2d7f | 345 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 346 | #endif |
0fda9f7c | 347 | return 0; |
2390c573 RL |
348 | } |
349 | ||
3b9e1a39 RL |
350 | #ifndef OPENSSL_NO_TRACE |
351 | static int trace_attach_w_callback_cb(int category, int type, const void *data) | |
352 | { | |
353 | switch (type) { | |
e474a286 | 354 | case CHANNEL: |
3b9e1a39 RL |
355 | OSSL_TRACE2(TRACE, |
356 | "Attach channel %p to category '%s' (with callback)\n", | |
357 | data, trace_categories[category].name); | |
358 | break; | |
e474a286 | 359 | case PREFIX: |
3b9e1a39 RL |
360 | OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n", |
361 | (const char *)data, trace_categories[category].name); | |
362 | break; | |
e474a286 | 363 | case SUFFIX: |
3b9e1a39 RL |
364 | OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n", |
365 | (const char *)data, trace_categories[category].name); | |
366 | break; | |
367 | default: /* No clue */ | |
368 | break; | |
369 | } | |
370 | return 1; | |
371 | } | |
372 | #endif | |
373 | ||
2390c573 RL |
374 | int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data) |
375 | { | |
16a9d374 | 376 | #ifndef OPENSSL_NO_TRACE |
3b9e1a39 | 377 | BIO *channel = NULL; |
2390c573 RL |
378 | struct trace_data_st *trace_data = NULL; |
379 | ||
3b9e1a39 | 380 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) |
0fda9f7c | 381 | return 0; |
2390c573 | 382 | |
3b9e1a39 RL |
383 | if (callback != NULL) { |
384 | if ((channel = BIO_new(&trace_method)) == NULL | |
385 | || (trace_data = | |
386 | OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL) | |
387 | goto err; | |
2390c573 | 388 | |
3b9e1a39 RL |
389 | trace_data->callback = callback; |
390 | trace_data->category = category; | |
391 | trace_data->data = data; | |
2390c573 | 392 | |
3b9e1a39 RL |
393 | BIO_set_data(channel, trace_data); |
394 | } | |
2390c573 | 395 | |
fe50e115 | 396 | if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL, |
3b9e1a39 RL |
397 | trace_attach_w_callback_cb, trace_detach_cb)) |
398 | goto err; | |
2390c573 | 399 | |
0fda9f7c | 400 | return 1; |
2390c573 RL |
401 | |
402 | err: | |
403 | BIO_free(channel); | |
404 | OPENSSL_free(trace_data); | |
d33d7616 | 405 | #endif |
0fda9f7c DMSP |
406 | |
407 | return 0; | |
2390c573 RL |
408 | } |
409 | ||
410 | int OSSL_trace_set_prefix(int category, const char *prefix) | |
411 | { | |
16a9d374 | 412 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 413 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 414 | return set_trace_data(category, 0, NULL, &prefix, NULL, |
3b9e1a39 | 415 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 416 | #endif |
0fda9f7c | 417 | return 0; |
2390c573 RL |
418 | } |
419 | ||
420 | int OSSL_trace_set_suffix(int category, const char *suffix) | |
421 | { | |
16a9d374 | 422 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 423 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 424 | return set_trace_data(category, 0, NULL, NULL, &suffix, |
3b9e1a39 | 425 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 426 | #endif |
0fda9f7c | 427 | return 0; |
2390c573 RL |
428 | } |
429 | ||
16a9d374 | 430 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
431 | static int ossl_trace_get_category(int category) |
432 | { | |
433 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) | |
434 | return -1; | |
435 | if (trace_channels[category].bio != NULL) | |
436 | return category; | |
3a8269b3 | 437 | return OSSL_TRACE_CATEGORY_ALL; |
2390c573 | 438 | } |
16a9d374 | 439 | #endif |
2390c573 RL |
440 | |
441 | int OSSL_trace_enabled(int category) | |
442 | { | |
443 | int ret = 0; | |
16a9d374 | 444 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 445 | category = ossl_trace_get_category(category); |
6a411436 DMSP |
446 | if (category >= 0) |
447 | ret = trace_channels[category].bio != NULL; | |
16a9d374 | 448 | #endif |
2390c573 RL |
449 | return ret; |
450 | } | |
451 | ||
452 | BIO *OSSL_trace_begin(int category) | |
453 | { | |
454 | BIO *channel = NULL; | |
16a9d374 | 455 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
456 | char *prefix = NULL; |
457 | ||
458 | category = ossl_trace_get_category(category); | |
6a411436 DMSP |
459 | if (category < 0) |
460 | return NULL; | |
461 | ||
2390c573 RL |
462 | channel = trace_channels[category].bio; |
463 | prefix = trace_channels[category].prefix; | |
464 | ||
465 | if (channel != NULL) { | |
466 | CRYPTO_THREAD_write_lock(trace_lock); | |
467 | current_channel = channel; | |
468 | switch (trace_channels[category].type) { | |
5afb177c | 469 | case SIMPLE_CHANNEL: |
2390c573 RL |
470 | if (prefix != NULL) { |
471 | (void)BIO_puts(channel, prefix); | |
472 | (void)BIO_puts(channel, "\n"); | |
473 | } | |
474 | break; | |
5afb177c | 475 | case CALLBACK_CHANNEL: |
2390c573 RL |
476 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN, |
477 | prefix == NULL ? 0 : strlen(prefix), prefix); | |
478 | break; | |
479 | } | |
480 | } | |
16a9d374 | 481 | #endif |
2390c573 RL |
482 | return channel; |
483 | } | |
484 | ||
485 | void OSSL_trace_end(int category, BIO * channel) | |
486 | { | |
16a9d374 | 487 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
488 | char *suffix = NULL; |
489 | ||
490 | category = ossl_trace_get_category(category); | |
491 | suffix = trace_channels[category].suffix; | |
492 | if (channel != NULL | |
493 | && ossl_assert(channel == current_channel)) { | |
494 | (void)BIO_flush(channel); | |
495 | switch (trace_channels[category].type) { | |
5afb177c | 496 | case SIMPLE_CHANNEL: |
2390c573 RL |
497 | if (suffix != NULL) { |
498 | (void)BIO_puts(channel, suffix); | |
499 | (void)BIO_puts(channel, "\n"); | |
500 | } | |
501 | break; | |
5afb177c | 502 | case CALLBACK_CHANNEL: |
2390c573 RL |
503 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END, |
504 | suffix == NULL ? 0 : strlen(suffix), suffix); | |
505 | break; | |
506 | } | |
507 | current_channel = NULL; | |
508 | CRYPTO_THREAD_unlock(trace_lock); | |
509 | } | |
16a9d374 | 510 | #endif |
2390c573 | 511 | } |