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