]>
Commit | Line | Data |
---|---|---|
2390c573 | 1 | /* |
da1c088f | 2 | * Copyright 2019-2023 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" | |
a935791d | 19 | #include "internal/refcount.h" |
25f2138b | 20 | #include "crypto/cryptlib.h" |
0243e821 | 21 | #include "crypto/ctype.h" |
2390c573 | 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 | ||
78bd646b TM |
122 | static const struct trace_category_st |
123 | trace_categories[OSSL_TRACE_CATEGORY_NUM] = { | |
3a8269b3 | 124 | TRACE_CATEGORY_(ALL), |
3b9e1a39 | 125 | TRACE_CATEGORY_(TRACE), |
5c641735 | 126 | TRACE_CATEGORY_(INIT), |
49b26f54 | 127 | TRACE_CATEGORY_(TLS), |
77359d22 | 128 | TRACE_CATEGORY_(TLS_CIPHER), |
bc362b9b | 129 | TRACE_CATEGORY_(CONF), |
f272be67 | 130 | TRACE_CATEGORY_(ENGINE_TABLE), |
f518e3e8 | 131 | TRACE_CATEGORY_(ENGINE_REF_COUNT), |
3a9b3d2d | 132 | TRACE_CATEGORY_(PKCS5V2), |
a902e43d | 133 | TRACE_CATEGORY_(PKCS12_KEYGEN), |
5f8a5f46 | 134 | TRACE_CATEGORY_(PKCS12_DECRYPT), |
b9ce85f6 | 135 | TRACE_CATEGORY_(X509V3_POLICY), |
6e810f2d | 136 | TRACE_CATEGORY_(BN_CTX), |
e06c0a28 | 137 | TRACE_CATEGORY_(CMP), |
2897b009 | 138 | TRACE_CATEGORY_(STORE), |
de5008a4 | 139 | TRACE_CATEGORY_(DECODER), |
0b9f90f5 | 140 | TRACE_CATEGORY_(ENCODER), |
e8fdb060 DDO |
141 | TRACE_CATEGORY_(REF_COUNT), |
142 | TRACE_CATEGORY_(HTTP), | |
143 | }; /* KEEP THIS LIST IN SYNC with #define OSSL_TRACE_CATEGORY_... in trace.h */ | |
2390c573 RL |
144 | |
145 | const char *OSSL_trace_get_category_name(int num) | |
146 | { | |
78bd646b TM |
147 | if (num < 0 || (size_t)num >= OSSL_NELEM(trace_categories)) |
148 | return NULL; | |
fcff5bd4 DF |
149 | /* |
150 | * Partial check that OSSL_TRACE_CATEGORY_... macros | |
151 | * are synced with trace_categories array | |
152 | */ | |
78bd646b TM |
153 | if (!ossl_assert(trace_categories[num].name != NULL) |
154 | || !ossl_assert(trace_categories[num].num == num)) | |
fcff5bd4 | 155 | return NULL; |
78bd646b | 156 | return trace_categories[num].name; |
2390c573 RL |
157 | } |
158 | ||
159 | int OSSL_trace_get_category_num(const char *name) | |
160 | { | |
161 | size_t i; | |
162 | ||
fcff5bd4 DF |
163 | if (name == NULL) |
164 | return -1; | |
165 | ||
2390c573 | 166 | for (i = 0; i < OSSL_NELEM(trace_categories); i++) |
fba140c7 | 167 | if (OPENSSL_strcasecmp(name, trace_categories[i].name) == 0) |
2390c573 | 168 | return trace_categories[i].num; |
fcff5bd4 | 169 | |
2390c573 RL |
170 | return -1; /* not found */ |
171 | } | |
172 | ||
16a9d374 RL |
173 | #ifndef OPENSSL_NO_TRACE |
174 | ||
2390c573 RL |
175 | /* We use one trace channel for each trace category */ |
176 | static struct { | |
5afb177c | 177 | enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type; |
2390c573 RL |
178 | BIO *bio; |
179 | char *prefix; | |
180 | char *suffix; | |
181 | } trace_channels[OSSL_TRACE_CATEGORY_NUM] = { | |
182 | { 0, NULL, NULL, NULL }, | |
183 | }; | |
184 | ||
16a9d374 RL |
185 | #endif |
186 | ||
3b9e1a39 | 187 | #ifndef OPENSSL_NO_TRACE |
e474a286 DMSP |
188 | |
189 | enum { | |
190 | CHANNEL, | |
191 | PREFIX, | |
192 | SUFFIX | |
193 | }; | |
194 | ||
3b9e1a39 RL |
195 | static int trace_attach_cb(int category, int type, const void *data) |
196 | { | |
197 | switch (type) { | |
e474a286 | 198 | case CHANNEL: |
3b9e1a39 RL |
199 | OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n", |
200 | data, trace_categories[category].name); | |
201 | break; | |
e474a286 | 202 | case PREFIX: |
3b9e1a39 RL |
203 | OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n", |
204 | (const char *)data, trace_categories[category].name); | |
205 | break; | |
e474a286 | 206 | case SUFFIX: |
3b9e1a39 RL |
207 | OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n", |
208 | (const char *)data, trace_categories[category].name); | |
209 | break; | |
210 | default: /* No clue */ | |
211 | break; | |
212 | } | |
213 | return 1; | |
214 | } | |
215 | ||
216 | static int trace_detach_cb(int category, int type, const void *data) | |
217 | { | |
218 | switch (type) { | |
e474a286 | 219 | case CHANNEL: |
3b9e1a39 RL |
220 | OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n", |
221 | data, trace_categories[category].name); | |
222 | break; | |
e474a286 | 223 | case PREFIX: |
3b9e1a39 RL |
224 | OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n", |
225 | (const char *)data, trace_categories[category].name); | |
226 | break; | |
e474a286 | 227 | case SUFFIX: |
3b9e1a39 RL |
228 | OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n", |
229 | (const char *)data, trace_categories[category].name); | |
230 | break; | |
231 | default: /* No clue */ | |
232 | break; | |
233 | } | |
234 | return 1; | |
235 | } | |
236 | ||
cf0932cd DDO |
237 | static int do_ossl_trace_init(void); |
238 | static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT; | |
239 | DEFINE_RUN_ONCE_STATIC(ossl_trace_init) | |
240 | { | |
241 | return do_ossl_trace_init(); | |
242 | } | |
243 | ||
fe50e115 | 244 | static int set_trace_data(int category, int type, BIO **channel, |
3b9e1a39 RL |
245 | const char **prefix, const char **suffix, |
246 | int (*attach_cb)(int, int, const void *), | |
247 | int (*detach_cb)(int, int, const void *)) | |
248 | { | |
fe26f798 RL |
249 | BIO *curr_channel = NULL; |
250 | char *curr_prefix = NULL; | |
251 | char *curr_suffix = NULL; | |
252 | ||
cf0932cd DDO |
253 | /* Ensure do_ossl_trace_init() is called once */ |
254 | if (!RUN_ONCE(&trace_inited, ossl_trace_init)) | |
255 | return 0; | |
fe26f798 RL |
256 | |
257 | curr_channel = trace_channels[category].bio; | |
258 | curr_prefix = trace_channels[category].prefix; | |
259 | curr_suffix = trace_channels[category].suffix; | |
3b9e1a39 RL |
260 | |
261 | /* Make sure to run the detach callback first on all data */ | |
262 | if (prefix != NULL && curr_prefix != NULL) { | |
e474a286 | 263 | detach_cb(category, PREFIX, curr_prefix); |
3b9e1a39 RL |
264 | } |
265 | ||
266 | if (suffix != NULL && curr_suffix != NULL) { | |
e474a286 | 267 | detach_cb(category, SUFFIX, curr_suffix); |
3b9e1a39 RL |
268 | } |
269 | ||
270 | if (channel != NULL && curr_channel != NULL) { | |
e474a286 | 271 | detach_cb(category, CHANNEL, curr_channel); |
3b9e1a39 RL |
272 | } |
273 | ||
274 | /* After detach callbacks are done, clear data where appropriate */ | |
275 | if (prefix != NULL && curr_prefix != NULL) { | |
276 | OPENSSL_free(curr_prefix); | |
277 | trace_channels[category].prefix = NULL; | |
278 | } | |
279 | ||
280 | if (suffix != NULL && curr_suffix != NULL) { | |
281 | OPENSSL_free(curr_suffix); | |
282 | trace_channels[category].suffix = NULL; | |
283 | } | |
284 | ||
285 | if (channel != NULL && curr_channel != NULL) { | |
286 | BIO_free(curr_channel); | |
fe50e115 | 287 | trace_channels[category].type = 0; |
3b9e1a39 RL |
288 | trace_channels[category].bio = NULL; |
289 | } | |
290 | ||
291 | /* Before running callbacks are done, set new data where appropriate */ | |
3b9e1a39 RL |
292 | if (prefix != NULL && *prefix != NULL) { |
293 | if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL) | |
294 | return 0; | |
295 | trace_channels[category].prefix = curr_prefix; | |
296 | } | |
297 | ||
298 | if (suffix != NULL && *suffix != NULL) { | |
299 | if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL) | |
300 | return 0; | |
301 | trace_channels[category].suffix = curr_suffix; | |
302 | } | |
303 | ||
0fec2121 DDO |
304 | if (channel != NULL && *channel != NULL) { |
305 | trace_channels[category].type = type; | |
306 | trace_channels[category].bio = *channel; | |
307 | /* | |
308 | * This must not be done before setting prefix/suffix, | |
309 | * as those may fail, and then the caller is mislead to free *channel. | |
310 | */ | |
311 | } | |
312 | ||
3b9e1a39 RL |
313 | /* Finally, run the attach callback on the new data */ |
314 | if (channel != NULL && *channel != NULL) { | |
e474a286 | 315 | attach_cb(category, CHANNEL, *channel); |
3b9e1a39 RL |
316 | } |
317 | ||
318 | if (prefix != NULL && *prefix != NULL) { | |
e474a286 | 319 | attach_cb(category, PREFIX, *prefix); |
3b9e1a39 RL |
320 | } |
321 | ||
322 | if (suffix != NULL && *suffix != NULL) { | |
e474a286 | 323 | attach_cb(category, SUFFIX, *suffix); |
3b9e1a39 RL |
324 | } |
325 | ||
326 | return 1; | |
327 | } | |
3b9e1a39 | 328 | |
cf0932cd | 329 | static int do_ossl_trace_init(void) |
2390c573 RL |
330 | { |
331 | trace_lock = CRYPTO_THREAD_lock_new(); | |
cf0932cd | 332 | return trace_lock != NULL; |
2390c573 RL |
333 | } |
334 | ||
cf0932cd DDO |
335 | #endif |
336 | ||
2390c573 RL |
337 | void ossl_trace_cleanup(void) |
338 | { | |
16a9d374 | 339 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 340 | int category; |
3b9e1a39 RL |
341 | BIO *channel = NULL; |
342 | const char *prefix = NULL; | |
343 | const char *suffix = NULL; | |
344 | ||
345 | for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) { | |
346 | /* We force the TRACE category to be treated last */ | |
347 | if (category == OSSL_TRACE_CATEGORY_TRACE) | |
348 | continue; | |
fe50e115 | 349 | set_trace_data(category, 0, &channel, &prefix, &suffix, |
3b9e1a39 RL |
350 | trace_attach_cb, trace_detach_cb); |
351 | } | |
fe50e115 DMSP |
352 | set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel, |
353 | &prefix, &suffix, | |
3b9e1a39 | 354 | trace_attach_cb, trace_detach_cb); |
2390c573 | 355 | CRYPTO_THREAD_lock_free(trace_lock); |
16a9d374 | 356 | #endif |
2390c573 RL |
357 | } |
358 | ||
359 | int OSSL_trace_set_channel(int category, BIO *channel) | |
360 | { | |
16a9d374 | 361 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c DMSP |
362 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
363 | return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL, | |
02bd2d7f | 364 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 365 | #endif |
0fda9f7c | 366 | return 0; |
2390c573 RL |
367 | } |
368 | ||
3b9e1a39 RL |
369 | #ifndef OPENSSL_NO_TRACE |
370 | static int trace_attach_w_callback_cb(int category, int type, const void *data) | |
371 | { | |
372 | switch (type) { | |
e474a286 | 373 | case CHANNEL: |
3b9e1a39 RL |
374 | OSSL_TRACE2(TRACE, |
375 | "Attach channel %p to category '%s' (with callback)\n", | |
376 | data, trace_categories[category].name); | |
377 | break; | |
e474a286 | 378 | case PREFIX: |
3b9e1a39 RL |
379 | OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n", |
380 | (const char *)data, trace_categories[category].name); | |
381 | break; | |
e474a286 | 382 | case SUFFIX: |
3b9e1a39 RL |
383 | OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n", |
384 | (const char *)data, trace_categories[category].name); | |
385 | break; | |
386 | default: /* No clue */ | |
387 | break; | |
388 | } | |
389 | return 1; | |
390 | } | |
391 | #endif | |
392 | ||
2390c573 RL |
393 | int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data) |
394 | { | |
16a9d374 | 395 | #ifndef OPENSSL_NO_TRACE |
3b9e1a39 | 396 | BIO *channel = NULL; |
2390c573 RL |
397 | struct trace_data_st *trace_data = NULL; |
398 | ||
3b9e1a39 | 399 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) |
0fda9f7c | 400 | return 0; |
2390c573 | 401 | |
3b9e1a39 RL |
402 | if (callback != NULL) { |
403 | if ((channel = BIO_new(&trace_method)) == NULL | |
404 | || (trace_data = | |
405 | OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL) | |
406 | goto err; | |
2390c573 | 407 | |
3b9e1a39 RL |
408 | trace_data->callback = callback; |
409 | trace_data->category = category; | |
410 | trace_data->data = data; | |
2390c573 | 411 | |
3b9e1a39 RL |
412 | BIO_set_data(channel, trace_data); |
413 | } | |
2390c573 | 414 | |
fe50e115 | 415 | if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL, |
3b9e1a39 RL |
416 | trace_attach_w_callback_cb, trace_detach_cb)) |
417 | goto err; | |
2390c573 | 418 | |
0fda9f7c | 419 | return 1; |
2390c573 RL |
420 | |
421 | err: | |
422 | BIO_free(channel); | |
423 | OPENSSL_free(trace_data); | |
d33d7616 | 424 | #endif |
0fda9f7c DMSP |
425 | |
426 | return 0; | |
2390c573 RL |
427 | } |
428 | ||
429 | int OSSL_trace_set_prefix(int category, const char *prefix) | |
430 | { | |
16a9d374 | 431 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 432 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 433 | return set_trace_data(category, 0, NULL, &prefix, NULL, |
3b9e1a39 | 434 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 435 | #endif |
0fda9f7c | 436 | return 0; |
2390c573 RL |
437 | } |
438 | ||
439 | int OSSL_trace_set_suffix(int category, const char *suffix) | |
440 | { | |
16a9d374 | 441 | #ifndef OPENSSL_NO_TRACE |
0fda9f7c | 442 | if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM) |
fe50e115 | 443 | return set_trace_data(category, 0, NULL, NULL, &suffix, |
3b9e1a39 | 444 | trace_attach_cb, trace_detach_cb); |
16a9d374 | 445 | #endif |
0fda9f7c | 446 | return 0; |
2390c573 RL |
447 | } |
448 | ||
16a9d374 | 449 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
450 | static int ossl_trace_get_category(int category) |
451 | { | |
452 | if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM) | |
453 | return -1; | |
454 | if (trace_channels[category].bio != NULL) | |
455 | return category; | |
3a8269b3 | 456 | return OSSL_TRACE_CATEGORY_ALL; |
2390c573 | 457 | } |
16a9d374 | 458 | #endif |
2390c573 RL |
459 | |
460 | int OSSL_trace_enabled(int category) | |
461 | { | |
462 | int ret = 0; | |
16a9d374 | 463 | #ifndef OPENSSL_NO_TRACE |
2390c573 | 464 | category = ossl_trace_get_category(category); |
6a411436 DMSP |
465 | if (category >= 0) |
466 | ret = trace_channels[category].bio != NULL; | |
16a9d374 | 467 | #endif |
2390c573 RL |
468 | return ret; |
469 | } | |
470 | ||
471 | BIO *OSSL_trace_begin(int category) | |
472 | { | |
473 | BIO *channel = NULL; | |
16a9d374 | 474 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
475 | char *prefix = NULL; |
476 | ||
477 | category = ossl_trace_get_category(category); | |
6a411436 DMSP |
478 | if (category < 0) |
479 | return NULL; | |
480 | ||
2390c573 RL |
481 | channel = trace_channels[category].bio; |
482 | prefix = trace_channels[category].prefix; | |
483 | ||
484 | if (channel != NULL) { | |
cd3f8c1b RS |
485 | if (!CRYPTO_THREAD_write_lock(trace_lock)) |
486 | return NULL; | |
2390c573 RL |
487 | current_channel = channel; |
488 | switch (trace_channels[category].type) { | |
5afb177c | 489 | case SIMPLE_CHANNEL: |
2390c573 RL |
490 | if (prefix != NULL) { |
491 | (void)BIO_puts(channel, prefix); | |
492 | (void)BIO_puts(channel, "\n"); | |
493 | } | |
494 | break; | |
5afb177c | 495 | case CALLBACK_CHANNEL: |
2390c573 RL |
496 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN, |
497 | prefix == NULL ? 0 : strlen(prefix), prefix); | |
498 | break; | |
499 | } | |
500 | } | |
16a9d374 | 501 | #endif |
2390c573 RL |
502 | return channel; |
503 | } | |
504 | ||
bbaeadb0 | 505 | void OSSL_trace_end(int category, BIO *channel) |
2390c573 | 506 | { |
16a9d374 | 507 | #ifndef OPENSSL_NO_TRACE |
2390c573 RL |
508 | char *suffix = NULL; |
509 | ||
510 | category = ossl_trace_get_category(category); | |
ee8a61e1 MC |
511 | if (category < 0) |
512 | return; | |
2390c573 RL |
513 | suffix = trace_channels[category].suffix; |
514 | if (channel != NULL | |
515 | && ossl_assert(channel == current_channel)) { | |
516 | (void)BIO_flush(channel); | |
517 | switch (trace_channels[category].type) { | |
5afb177c | 518 | case SIMPLE_CHANNEL: |
2390c573 RL |
519 | if (suffix != NULL) { |
520 | (void)BIO_puts(channel, suffix); | |
521 | (void)BIO_puts(channel, "\n"); | |
522 | } | |
523 | break; | |
5afb177c | 524 | case CALLBACK_CHANNEL: |
2390c573 RL |
525 | (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END, |
526 | suffix == NULL ? 0 : strlen(suffix), suffix); | |
527 | break; | |
528 | } | |
529 | current_channel = NULL; | |
530 | CRYPTO_THREAD_unlock(trace_lock); | |
531 | } | |
16a9d374 | 532 | #endif |
2390c573 | 533 | } |
0243e821 DDO |
534 | |
535 | int OSSL_trace_string(BIO *out, int text, int full, | |
536 | const unsigned char *data, size_t size) | |
537 | { | |
538 | unsigned char buf[OSSL_TRACE_STRING_MAX + 1]; | |
539 | int len, i; | |
540 | ||
541 | if (!full && size > OSSL_TRACE_STRING_MAX) { | |
542 | BIO_printf(out, "[len %zu limited to %d]: ", | |
543 | size, OSSL_TRACE_STRING_MAX); | |
544 | len = OSSL_TRACE_STRING_MAX; | |
545 | } else { | |
546 | len = (int)size; | |
547 | } | |
548 | if (!text) { /* mask control characters while preserving newlines */ | |
549 | for (i = 0; i < len; i++, data++) | |
550 | buf[i] = (char)*data != '\n' && ossl_iscntrl((int)*data) | |
551 | ? ' ' : *data; | |
552 | if (len == 0 || data[-1] != '\n') | |
553 | buf[len++] = '\n'; | |
554 | data = buf; | |
555 | } | |
556 | return BIO_printf(out, "%.*s", len, data); | |
557 | } |