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