]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/trace.c
Adapt ENGINE_CONF_DEBUG to the new generic trace API
[thirdparty/openssl.git] / crypto / trace.c
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
22 #ifndef OPENSSL_NO_TRACE
23
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);
68 size_t cnt = ctx->callback(buf, num, ctx->category, OSSL_TRACE_CTRL_DURING,
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 }
108 #endif
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[] = {
122 TRACE_CATEGORY_(ANY),
123 TRACE_CATEGORY_(INIT),
124 TRACE_CATEGORY_(TLS),
125 TRACE_CATEGORY_(TLS_CIPHER),
126 TRACE_CATEGORY_(ENGINE_CONF),
127 };
128
129 const char *OSSL_trace_get_category_name(int num)
130 {
131 size_t i;
132
133 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
134 if (trace_categories[i].num == num)
135 return trace_categories[i].name;
136 return NULL; /* not found */
137 }
138
139 int OSSL_trace_get_category_num(const char *name)
140 {
141 size_t i;
142
143 for (i = 0; i < OSSL_NELEM(trace_categories); i++)
144 if (strcasecmp(name, trace_categories[i].name) == 0)
145 return trace_categories[i].num;
146 return -1; /* not found */
147 }
148
149 #ifndef OPENSSL_NO_TRACE
150
151 /* We use one trace channel for each trace category */
152 static struct {
153 enum { t_channel, t_callback } type;
154 BIO *bio;
155 char *prefix;
156 char *suffix;
157 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
158 { 0, NULL, NULL, NULL },
159 };
160
161 #endif
162
163 int ossl_trace_init(void)
164 {
165 #ifndef OPENSSL_NO_TRACE
166 trace_lock = CRYPTO_THREAD_lock_new();
167 if (trace_lock != NULL)
168 return 1;
169 #endif
170
171 return 0;
172 }
173
174 void ossl_trace_cleanup(void)
175 {
176 #ifndef OPENSSL_NO_TRACE
177 int category;
178
179 for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++)
180 OSSL_trace_set_channel(category, NULL);
181 CRYPTO_THREAD_lock_free(trace_lock);
182 #endif
183 }
184
185 int OSSL_trace_set_channel(int category, BIO *channel)
186 {
187 #ifndef OPENSSL_NO_TRACE
188 BIO *prev_channel;
189
190 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
191 goto err;
192
193 prev_channel = trace_channels[category].bio;
194
195 if (prev_channel != NULL) {
196 BIO_free(prev_channel);
197 trace_channels[category].bio = NULL;
198 }
199
200 if (channel == NULL)
201 return 1; /* Done */
202
203 trace_channels[category].bio = channel;
204 trace_channels[category].type = t_channel;
205
206 return 1;
207
208 err:
209 #endif
210
211 return 0;
212 }
213
214 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
215 {
216 #ifndef OPENSSL_NO_TRACE
217 BIO *channel = trace_channels[category].bio;
218 struct trace_data_st *trace_data = NULL;
219
220 if (channel != NULL) {
221 BIO_free(channel);
222 trace_channels[category].bio = NULL;
223 }
224
225 if (callback == NULL)
226 return 1; /* done */
227
228 channel = BIO_new(&trace_method);
229 if (channel == NULL)
230 goto err;
231
232 trace_data = OPENSSL_zalloc(sizeof(struct trace_data_st));
233 if (trace_data == NULL)
234 goto err;
235
236 trace_data->callback = callback;
237 trace_data->category = category;
238 trace_data->data = data;
239
240 BIO_set_data(channel, trace_data);
241
242 trace_channels[category].bio = channel;
243 trace_channels[category].type = t_callback;
244
245 return 1;
246
247 err:
248 BIO_free(channel);
249 OPENSSL_free(trace_data);
250 #endif
251
252 return 0;
253 }
254
255 int OSSL_trace_set_prefix(int category, const char *prefix)
256 {
257 #ifndef OPENSSL_NO_TRACE
258 char *curr_prefix = trace_channels[category].prefix;
259
260 if (curr_prefix != NULL) {
261 OPENSSL_free(curr_prefix);
262 trace_channels[category].prefix = NULL;
263 }
264
265 if (prefix == NULL)
266 return 1; /* Done */
267
268 curr_prefix = OPENSSL_strdup(prefix);
269 if (curr_prefix == NULL)
270 goto err;
271
272 trace_channels[category].prefix = curr_prefix;
273
274 return 1;
275
276 err:
277 #endif
278
279 return 0;
280 }
281
282 int OSSL_trace_set_suffix(int category, const char *suffix)
283 {
284 #ifndef OPENSSL_NO_TRACE
285 char *curr_suffix = trace_channels[category].suffix;
286
287 if (curr_suffix != NULL) {
288 OPENSSL_free(curr_suffix);
289 trace_channels[category].suffix = NULL;
290 }
291
292 if (suffix == NULL)
293 return 1; /* done */
294
295 curr_suffix = OPENSSL_strdup(suffix);
296 if (curr_suffix == NULL)
297 goto err;
298
299 trace_channels[category].suffix = curr_suffix;
300
301 return 1;
302
303 err:
304 #endif
305
306 return 0;
307 }
308
309 #ifndef OPENSSL_NO_TRACE
310 static int ossl_trace_get_category(int category)
311 {
312 if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
313 return -1;
314 if (trace_channels[category].bio != NULL)
315 return category;
316 return OSSL_TRACE_CATEGORY_ANY;
317 }
318 #endif
319
320 int OSSL_trace_enabled(int category)
321 {
322 int ret = 0;
323 #ifndef OPENSSL_NO_TRACE
324 category = ossl_trace_get_category(category);
325 ret = trace_channels[category].bio != NULL;
326 #endif
327 return ret;
328 }
329
330 BIO *OSSL_trace_begin(int category)
331 {
332 BIO *channel = NULL;
333 #ifndef OPENSSL_NO_TRACE
334 char *prefix = NULL;
335
336 category = ossl_trace_get_category(category);
337 channel = trace_channels[category].bio;
338 prefix = trace_channels[category].prefix;
339
340 if (channel != NULL) {
341 CRYPTO_THREAD_write_lock(trace_lock);
342 current_channel = channel;
343 switch (trace_channels[category].type) {
344 case t_channel:
345 if (prefix != NULL) {
346 (void)BIO_puts(channel, prefix);
347 (void)BIO_puts(channel, "\n");
348 }
349 break;
350 case t_callback:
351 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
352 prefix == NULL ? 0 : strlen(prefix), prefix);
353 break;
354 }
355 }
356 #endif
357 return channel;
358 }
359
360 void OSSL_trace_end(int category, BIO * channel)
361 {
362 #ifndef OPENSSL_NO_TRACE
363 char *suffix = NULL;
364
365 category = ossl_trace_get_category(category);
366 suffix = trace_channels[category].suffix;
367 if (channel != NULL
368 && ossl_assert(channel == current_channel)) {
369 (void)BIO_flush(channel);
370 switch (trace_channels[category].type) {
371 case t_channel:
372 if (suffix != NULL) {
373 (void)BIO_puts(channel, suffix);
374 (void)BIO_puts(channel, "\n");
375 }
376 break;
377 case t_callback:
378 (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
379 suffix == NULL ? 0 : strlen(suffix), suffix);
380 break;
381 }
382 current_channel = NULL;
383 CRYPTO_THREAD_unlock(trace_lock);
384 }
385 #endif
386 }