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