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