]>
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 | ||
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" | |
19 | ||
20 | #include "e_os.h" /* strcasecmp for Windows */ | |
21 | ||
16a9d374 RL |
22 | #ifndef OPENSSL_NO_TRACE |
23 | ||
2390c573 RL |
24 | static CRYPTO_RWLOCK *trace_lock = NULL; |
25 | ||
26 | static const BIO *current_channel = NULL; | |
27 | ||
28 | /*- | |
29 | * INTERNAL TRACE CHANNEL IMPLEMENTATION | |
30 | * | |
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 | |
35 | * callback function. | |
36 | */ | |
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); | |
42 | ||
43 | static const BIO_METHOD trace_method = { | |
44 | BIO_TYPE_SOURCE_SINK, | |
45 | "trace", | |
46 | trace_write, | |
47 | NULL, /* old write */ | |
48 | NULL, /* read_ex */ | |
49 | NULL, /* read */ | |
50 | trace_puts, | |
51 | NULL, /* gets */ | |
52 | trace_ctrl, /* ctrl */ | |
53 | NULL, /* create */ | |
54 | trace_free, /* free */ | |
55 | NULL, /* callback_ctrl */ | |
56 | }; | |
57 | ||
58 | struct trace_data_st { | |
59 | OSSL_trace_cb callback; | |
60 | int category; | |
61 | void *data; | |
62 | }; | |
63 | ||
64 | static int trace_write(BIO *channel, | |
65 | const char *buf, size_t num, size_t *written) | |
66 | { | |
67 | struct trace_data_st *ctx = BIO_get_data(channel); | |
13d06925 | 68 | size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_WRITE, |
2390c573 RL |
69 | ctx->data); |
70 | ||
71 | *written = cnt; | |
72 | return cnt != 0; | |
73 | } | |
74 | ||
75 | static int trace_puts(BIO *channel, const char *str) | |
76 | { | |
77 | size_t written; | |
78 | ||
79 | if (trace_write(channel, str, strlen(str), &written)) | |
80 | return (int)written; | |
81 | ||
82 | return EOF; | |
83 | } | |
84 | ||
85 | static long trace_ctrl(BIO *channel, int cmd, long argl, void *argp) | |
86 | { | |
87 | struct trace_data_st *ctx = BIO_get_data(channel); | |
88 | ||
89 | switch (cmd) { | |
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); | |
94 | return 1; | |
95 | default: | |
96 | break; | |
97 | } | |
98 | return -2; /* Unsupported */ | |
99 | } | |
100 | ||
101 | static int trace_free(BIO *channel) | |
102 | { | |
103 | if (channel == NULL) | |
104 | return 0; | |
105 | OPENSSL_free(BIO_get_data(channel)); | |
106 | return 1; | |
107 | } | |
16a9d374 | 108 | #endif |
2390c573 RL |
109 | |
110 | /*- | |
111 | * TRACE | |
112 | */ | |
113 | ||
114 | /* Helper struct and macro to get name string to number mapping */ | |
115 | struct trace_category_st { | |
116 | const char * const name; | |
117 | const int num; | |
118 | }; | |
119 | #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name } | |
120 | ||
121 | static const struct trace_category_st trace_categories[] = { | |
3a8269b3 | 122 | TRACE_CATEGORY_(ALL), |
3b9e1a39 | 123 | TRACE_CATEGORY_(TRACE), |
5c641735 | 124 | TRACE_CATEGORY_(INIT), |
49b26f54 | 125 | TRACE_CATEGORY_(TLS), |
77359d22 | 126 | TRACE_CATEGORY_(TLS_CIPHER), |
f4db05df | 127 | TRACE_CATEGORY_(ENGINE_CONF), |
f272be67 | 128 | TRACE_CATEGORY_(ENGINE_TABLE), |
f518e3e8 | 129 | TRACE_CATEGORY_(ENGINE_REF_COUNT), |
3a9b3d2d | 130 | TRACE_CATEGORY_(PKCS5V2), |
a902e43d | 131 | TRACE_CATEGORY_(PKCS12_KEYGEN), |
5f8a5f46 | 132 | TRACE_CATEGORY_(PKCS12_DECRYPT), |
b9ce85f6 | 133 | TRACE_CATEGORY_(X509V3_POLICY), |
6e810f2d | 134 | TRACE_CATEGORY_(BN_CTX), |
ecbfaef2 | 135 | TRACE_CATEGORY_(PROVIDER_CONF), |
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 | ||
fe50e115 | 222 | static int set_trace_data(int category, int type, BIO **channel, |
3b9e1a39 RL |
223 | const char **prefix, const char **suffix, |
224 | int (*attach_cb)(int, int, const void *), | |
225 | int (*detach_cb)(int, int, const void *)) | |
226 | { | |
227 | BIO *curr_channel = trace_channels[category].bio; | |
228 | char *curr_prefix = trace_channels[category].prefix; | |
229 | char *curr_suffix = trace_channels[category].suffix; | |
230 | ||
231 | /* Make sure to run the detach callback first on all data */ | |
232 | if (prefix != NULL && curr_prefix != NULL) { | |
e474a286 | 233 | detach_cb(category, PREFIX, curr_prefix); |
3b9e1a39 RL |
234 | } |
235 | ||
236 | if (suffix != NULL && curr_suffix != NULL) { | |
e474a286 | 237 | detach_cb(category, SUFFIX, curr_suffix); |
3b9e1a39 RL |
238 | } |
239 | ||
240 | if (channel != NULL && curr_channel != NULL) { | |
e474a286 | 241 | detach_cb(category, CHANNEL, curr_channel); |
3b9e1a39 RL |
242 | } |
243 | ||
244 | /* After detach callbacks are done, clear data where appropriate */ | |
245 | if (prefix != NULL && curr_prefix != NULL) { | |
246 | OPENSSL_free(curr_prefix); | |
247 | trace_channels[category].prefix = NULL; | |
248 | } | |
249 | ||
250 | if (suffix != NULL && curr_suffix != NULL) { | |
251 | OPENSSL_free(curr_suffix); | |
252 | trace_channels[category].suffix = NULL; | |
253 | } | |
254 | ||
255 | if (channel != NULL && curr_channel != NULL) { | |
256 | BIO_free(curr_channel); | |
fe50e115 | 257 | trace_channels[category].type = 0; |
3b9e1a39 RL |
258 | trace_channels[category].bio = NULL; |
259 | } | |
260 | ||
261 | /* Before running callbacks are done, set new data where appropriate */ | |
262 | if (channel != NULL && *channel != NULL) { | |
fe50e115 | 263 | trace_channels[category].type = type; |
3b9e1a39 RL |
264 | trace_channels[category].bio = *channel; |
265 | } | |
266 | ||
267 | if (prefix != NULL && *prefix != NULL) { | |
268 | if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL) | |
269 | return 0; | |
270 | trace_channels[category].prefix = curr_prefix; | |
271 | } | |
272 | ||
273 | if (suffix != NULL && *suffix != NULL) { | |
274 | if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL) | |
275 | return 0; | |
276 | trace_channels[category].suffix = curr_suffix; | |
277 | } | |
278 | ||
279 | /* Finally, run the attach callback on the new data */ | |
280 | if (channel != NULL && *channel != NULL) { | |
e474a286 | 281 | attach_cb(category, CHANNEL, *channel); |
3b9e1a39 RL |
282 | } |
283 | ||
284 | if (prefix != NULL && *prefix != NULL) { | |
e474a286 | 285 | attach_cb(category, PREFIX, *prefix); |
3b9e1a39 RL |
286 | } |
287 | ||
288 | if (suffix != NULL && *suffix != NULL) { | |
e474a286 | 289 | attach_cb(category, SUFFIX, *suffix); |
3b9e1a39 RL |
290 | } |
291 | ||
292 | return 1; | |
293 | } | |
294 | #endif | |
295 | ||
2390c573 RL |
296 | int ossl_trace_init(void) |
297 | { | |
16a9d374 | 298 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 299 | trace_lock = CRYPTO_THREAD_lock_new(); |
d33d7616 RL |
300 | if (trace_lock == NULL) |
301 | return 0; | |
16a9d374 RL |
302 | #endif |
303 | ||
d33d7616 | 304 | return 1; |
2390c573 RL |
305 | } |
306 | ||
307 | void ossl_trace_cleanup(void) | |
308 | { | |
16a9d374 | 309 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 310 | int category; |
3b9e1a39 RL |
311 | BIO *channel = NULL; |
312 | const char *prefix = NULL; | |
313 | const char *suffix = NULL; | |
314 | ||
315 | for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) { | |
316 | /* We force the TRACE category to be treated last */ | |
317 | if (category == OSSL_TRACE_CATEGORY_TRACE) | |
318 | continue; | |
fe50e115 | 319 | set_trace_data(category, 0, &channel, &prefix, &suffix, |
3b9e1a39 RL |
320 | trace_attach_cb, trace_detach_cb); |
321 | } | |
fe50e115 DMSP |
322 | set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel, |
323 | &prefix, &suffix, | |
3b9e1a39 | 324 | trace_attach_cb, trace_detach_cb); |
2390c573 | 325 | CRYPTO_THREAD_lock_free(trace_lock); |
16a9d374 | 326 | #endif |
2390c573 RL |
327 | } |
328 | ||
329 | int OSSL_trace_set_channel(int category, BIO *channel) | |
330 | { | |
16a9d374 | 331 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c DMSP |
332 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
333 | return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL, | |
02bd2d7f | 334 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 335 | #endif |
0fda9f7c | 336 | return 0; |
2390c573 RL |
337 | } |
338 | ||
3b9e1a39 RL |
339 | #ifndef OPENSSL_NO_TRACE |
340 | static int trace_attach_w_callback_cb(int category, int type, const void *data) | |
341 | { | |
342 | switch (type) { | |
e474a286 | 343 | case CHANNEL: |
3b9e1a39 RL |
344 | OSSL_TRACE2(TRACE, |
345 | "Attach channel %p to category '%s' (with callback)\n", | |
346 | data, trace_categories[category].name); | |
347 | break; | |
e474a286 | 348 | case PREFIX: |
3b9e1a39 RL |
349 | OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n", |
350 | (const char *)data, trace_categories[category].name); | |
351 | break; | |
e474a286 | 352 | case SUFFIX: |
3b9e1a39 RL |
353 | OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n", |
354 | (const char *)data, trace_categories[category].name); | |
355 | break; | |
356 | default: /* No clue */ | |
357 | break; | |
358 | } | |
359 | return 1; | |
360 | } | |
361 | #endif | |
362 | ||
2390c573 RL |
363 | int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data) |
364 | { | |
16a9d374 | 365 | #ifndef OPENSSL_NO_TRACE |
3b9e1a39 | 366 | BIO *channel = NULL; |
2390c573 RL |
367 | struct trace_data_st *trace_data = NULL; |
368 | ||
3b9e1a39 | 369 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) |
0fda9f7c | 370 | return 0; |
2390c573 | 371 | |
3b9e1a39 RL |
372 | if (callback != NULL) { |
373 | if ((channel = BIO_new(&trace_method)) == NULL | |
374 | || (trace_data = | |
375 | OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL) | |
376 | goto err; | |
2390c573 | 377 | |
3b9e1a39 RL |
378 | trace_data->callback = callback; |
379 | trace_data->category = category; | |
380 | trace_data->data = data; | |
2390c573 | 381 | |
3b9e1a39 RL |
382 | BIO_set_data(channel, trace_data); |
383 | } | |
2390c573 | 384 | |
fe50e115 | 385 | if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL, |
3b9e1a39 RL |
386 | trace_attach_w_callback_cb, trace_detach_cb)) |
387 | goto err; | |
2390c573 | 388 | |
0fda9f7c | 389 | return 1; |
2390c573 RL |
390 | |
391 | err: | |
392 | BIO_free(channel); | |
393 | OPENSSL_free(trace_data); | |
d33d7616 | 394 | #endif |
0fda9f7c DMSP |
395 | |
396 | return 0; | |
2390c573 RL |
397 | } |
398 | ||
399 | int OSSL_trace_set_prefix(int category, const char *prefix) | |
400 | { | |
16a9d374 | 401 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 402 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 403 | return set_trace_data(category, 0, NULL, &prefix, NULL, |
3b9e1a39 | 404 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 405 | #endif |
0fda9f7c | 406 | return 0; |
2390c573 RL |
407 | } |
408 | ||
409 | int OSSL_trace_set_suffix(int category, const char *suffix) | |
410 | { | |
16a9d374 | 411 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 412 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 413 | return set_trace_data(category, 0, NULL, NULL, &suffix, |
3b9e1a39 | 414 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 415 | #endif |
0fda9f7c | 416 | return 0; |
2390c573 RL |
417 | } |
418 | ||
16a9d374 | 419 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
420 | static int ossl_trace_get_category(int category) |
421 | { | |
422 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) | |
423 | return -1; | |
424 | if (trace_channels[category].bio != NULL) | |
425 | return category; | |
3a8269b3 | 426 | return OSSL_TRACE_CATEGORY_ALL; |
2390c573 | 427 | } |
16a9d374 | 428 | #endif |
2390c573 RL |
429 | |
430 | int OSSL_trace_enabled(int category) | |
431 | { | |
432 | int ret = 0; | |
16a9d374 | 433 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 434 | category = ossl_trace_get_category(category); |
6a411436 DMSP |
435 | if (category >= 0) |
436 | ret = trace_channels[category].bio != NULL; | |
16a9d374 | 437 | #endif |
2390c573 RL |
438 | return ret; |
439 | } | |
440 | ||
441 | BIO *OSSL_trace_begin(int category) | |
442 | { | |
443 | BIO *channel = NULL; | |
16a9d374 | 444 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
445 | char *prefix = NULL; |
446 | ||
447 | category = ossl_trace_get_category(category); | |
6a411436 DMSP |
448 | if (category < 0) |
449 | return NULL; | |
450 | ||
2390c573 RL |
451 | channel = trace_channels[category].bio; |
452 | prefix = trace_channels[category].prefix; | |
453 | ||
454 | if (channel != NULL) { | |
455 | CRYPTO_THREAD_write_lock(trace_lock); | |
456 | current_channel = channel; | |
457 | switch (trace_channels[category].type) { | |
5afb177c | 458 | case SIMPLE_CHANNEL: |
2390c573 RL |
459 | if (prefix != NULL) { |
460 | (void)BIO_puts(channel, prefix); | |
461 | (void)BIO_puts(channel, "\n"); | |
462 | } | |
463 | break; | |
5afb177c | 464 | case CALLBACK_CHANNEL: |
2390c573 RL |
465 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN, |
466 | prefix == NULL ? 0 : strlen(prefix), prefix); | |
467 | break; | |
468 | } | |
469 | } | |
16a9d374 | 470 | #endif |
2390c573 RL |
471 | return channel; |
472 | } | |
473 | ||
474 | void OSSL_trace_end(int category, BIO * channel) | |
475 | { | |
16a9d374 | 476 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
477 | char *suffix = NULL; |
478 | ||
479 | category = ossl_trace_get_category(category); | |
480 | suffix = trace_channels[category].suffix; | |
481 | if (channel != NULL | |
482 | && ossl_assert(channel == current_channel)) { | |
483 | (void)BIO_flush(channel); | |
484 | switch (trace_channels[category].type) { | |
5afb177c | 485 | case SIMPLE_CHANNEL: |
2390c573 RL |
486 | if (suffix != NULL) { |
487 | (void)BIO_puts(channel, suffix); | |
488 | (void)BIO_puts(channel, "\n"); | |
489 | } | |
490 | break; | |
5afb177c | 491 | case CALLBACK_CHANNEL: |
2390c573 RL |
492 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END, |
493 | suffix == NULL ? 0 : strlen(suffix), suffix); | |
494 | break; | |
495 | } | |
496 | current_channel = NULL; | |
497 | CRYPTO_THREAD_unlock(trace_lock); | |
498 | } | |
16a9d374 | 499 | #endif |
2390c573 | 500 | } |