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