trace: fix out-of-bound memory access
[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_WRITE,
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_(TRACE),
124     TRACE_CATEGORY_(INIT),
125     TRACE_CATEGORY_(TLS),
126     TRACE_CATEGORY_(TLS_CIPHER),
127     TRACE_CATEGORY_(ENGINE_CONF),
128     TRACE_CATEGORY_(ENGINE_TABLE),
129     TRACE_CATEGORY_(ENGINE_REF_COUNT),
130     TRACE_CATEGORY_(PKCS5V2),
131     TRACE_CATEGORY_(PKCS12_KEYGEN),
132     TRACE_CATEGORY_(PKCS12_DECRYPT),
133     TRACE_CATEGORY_(X509V3_POLICY),
134     TRACE_CATEGORY_(BN_CTX),
135 };
136
137 const char *OSSL_trace_get_category_name(int num)
138 {
139     size_t i;
140
141     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
142         if (trace_categories[i].num == num)
143             return trace_categories[i].name;
144     return NULL; /* not found */
145 }
146
147 int OSSL_trace_get_category_num(const char *name)
148 {
149     size_t i;
150
151     for (i = 0; i < OSSL_NELEM(trace_categories); i++)
152         if (strcasecmp(name, trace_categories[i].name) == 0)
153             return trace_categories[i].num;
154     return -1; /* not found */
155 }
156
157 #ifndef OPENSSL_NO_TRACE
158
159 /* We use one trace channel for each trace category */
160 static struct {
161     enum { SIMPLE_CHANNEL, CALLBACK_CHANNEL } type;
162     BIO *bio;
163     char *prefix;
164     char *suffix;
165 } trace_channels[OSSL_TRACE_CATEGORY_NUM] = {
166     { 0, NULL, NULL, NULL },
167 };
168
169 #endif
170
171 #ifndef OPENSSL_NO_TRACE
172
173 enum {
174     CHANNEL,
175     PREFIX,
176     SUFFIX
177 };
178
179 static int trace_attach_cb(int category, int type, const void *data)
180 {
181     switch (type) {
182     case CHANNEL:
183         OSSL_TRACE2(TRACE, "Attach channel %p to category '%s'\n",
184                     data, trace_categories[category].name);
185         break;
186     case PREFIX:
187         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
188                     (const char *)data, trace_categories[category].name);
189         break;
190     case SUFFIX:
191         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
192                     (const char *)data, trace_categories[category].name);
193         break;
194     default:                     /* No clue */
195         break;
196     }
197     return 1;
198 }
199
200 static int trace_detach_cb(int category, int type, const void *data)
201 {
202     switch (type) {
203     case CHANNEL:
204         OSSL_TRACE2(TRACE, "Detach channel %p from category '%s'\n",
205                     data, trace_categories[category].name);
206         break;
207     case PREFIX:
208         OSSL_TRACE2(TRACE, "Detach prefix \"%s\" from category '%s'\n",
209                     (const char *)data, trace_categories[category].name);
210         break;
211     case SUFFIX:
212         OSSL_TRACE2(TRACE, "Detach suffix \"%s\" from category '%s'\n",
213                     (const char *)data, trace_categories[category].name);
214         break;
215     default:                     /* No clue */
216         break;
217     }
218     return 1;
219 }
220
221 static int set_trace_data(int category, int type, BIO **channel,
222                           const char **prefix, const char **suffix,
223                           int (*attach_cb)(int, int, const void *),
224                           int (*detach_cb)(int, int, const void *))
225 {
226     BIO *curr_channel = trace_channels[category].bio;
227     char *curr_prefix = trace_channels[category].prefix;
228     char *curr_suffix = trace_channels[category].suffix;
229
230     /* Make sure to run the detach callback first on all data */
231     if (prefix != NULL && curr_prefix != NULL) {
232         detach_cb(category, PREFIX, curr_prefix);
233     }
234
235     if (suffix != NULL && curr_suffix != NULL) {
236         detach_cb(category, SUFFIX, curr_suffix);
237     }
238
239     if (channel != NULL && curr_channel != NULL) {
240         detach_cb(category, CHANNEL, curr_channel);
241     }
242
243     /* After detach callbacks are done, clear data where appropriate */
244     if (prefix != NULL && curr_prefix != NULL) {
245         OPENSSL_free(curr_prefix);
246         trace_channels[category].prefix = NULL;
247     }
248
249     if (suffix != NULL && curr_suffix != NULL) {
250         OPENSSL_free(curr_suffix);
251         trace_channels[category].suffix = NULL;
252     }
253
254     if (channel != NULL && curr_channel != NULL) {
255         BIO_free(curr_channel);
256         trace_channels[category].type = 0;
257         trace_channels[category].bio = NULL;
258     }
259
260     /* Before running callbacks are done, set new data where appropriate */
261     if (channel != NULL && *channel != NULL) {
262         trace_channels[category].type = type;
263         trace_channels[category].bio = *channel;
264     }
265
266     if (prefix != NULL && *prefix != NULL) {
267         if ((curr_prefix = OPENSSL_strdup(*prefix)) == NULL)
268             return 0;
269         trace_channels[category].prefix = curr_prefix;
270     }
271
272     if (suffix != NULL && *suffix != NULL) {
273         if ((curr_suffix = OPENSSL_strdup(*suffix)) == NULL)
274             return 0;
275         trace_channels[category].suffix = curr_suffix;
276     }
277
278     /* Finally, run the attach callback on the new data */
279     if (channel != NULL && *channel != NULL) {
280         attach_cb(category, CHANNEL, *channel);
281     }
282
283     if (prefix != NULL && *prefix != NULL) {
284         attach_cb(category, PREFIX, *prefix);
285     }
286
287     if (suffix != NULL && *suffix != NULL) {
288         attach_cb(category, SUFFIX, *suffix);
289     }
290
291     return 1;
292 }
293 #endif
294
295 int ossl_trace_init(void)
296 {
297 #ifndef OPENSSL_NO_TRACE
298     trace_lock = CRYPTO_THREAD_lock_new();
299     if (trace_lock == NULL)
300         return 0;
301 #endif
302
303     return 1;
304 }
305
306 void ossl_trace_cleanup(void)
307 {
308 #ifndef OPENSSL_NO_TRACE
309     int category;
310     BIO *channel = NULL;
311     const char *prefix = NULL;
312     const char *suffix = NULL;
313
314     for (category = 0; category < OSSL_TRACE_CATEGORY_NUM; category++) {
315         /* We force the TRACE category to be treated last */
316         if (category == OSSL_TRACE_CATEGORY_TRACE)
317             continue;
318         set_trace_data(category, 0, &channel, &prefix, &suffix,
319                        trace_attach_cb, trace_detach_cb);
320     }
321     set_trace_data(OSSL_TRACE_CATEGORY_TRACE, 0, &channel,
322                    &prefix, &suffix,
323                    trace_attach_cb, trace_detach_cb);
324     CRYPTO_THREAD_lock_free(trace_lock);
325 #endif
326 }
327
328 int OSSL_trace_set_channel(int category, BIO *channel)
329 {
330 #ifndef OPENSSL_NO_TRACE
331     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
332         return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
333                               trace_attach_cb, trace_detach_cb))
334 #endif
335     return 0;
336 }
337
338 #ifndef OPENSSL_NO_TRACE
339 static int trace_attach_w_callback_cb(int category, int type, const void *data)
340 {
341     switch (type) {
342     case CHANNEL:
343         OSSL_TRACE2(TRACE,
344                     "Attach channel %p to category '%s' (with callback)\n",
345                     data, trace_categories[category].name);
346         break;
347     case PREFIX:
348         OSSL_TRACE2(TRACE, "Attach prefix \"%s\" to category '%s'\n",
349                     (const char *)data, trace_categories[category].name);
350         break;
351     case SUFFIX:
352         OSSL_TRACE2(TRACE, "Attach suffix \"%s\" to category '%s'\n",
353                     (const char *)data, trace_categories[category].name);
354         break;
355     default:                     /* No clue */
356         break;
357     }
358     return 1;
359 }
360 #endif
361
362 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
363 {
364 #ifndef OPENSSL_NO_TRACE
365     BIO *channel = NULL;
366     struct trace_data_st *trace_data = NULL;
367
368     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
369         return 0;
370
371     if (callback != NULL) {
372         if ((channel = BIO_new(&trace_method)) == NULL
373             || (trace_data =
374                 OPENSSL_zalloc(sizeof(struct trace_data_st))) == NULL)
375             goto err;
376
377         trace_data->callback = callback;
378         trace_data->category = category;
379         trace_data->data = data;
380
381         BIO_set_data(channel, trace_data);
382     }
383
384     if (!set_trace_data(category, CALLBACK_CHANNEL, &channel, NULL, NULL,
385                         trace_attach_w_callback_cb, trace_detach_cb))
386         goto err;
387
388     return 1;
389
390  err:
391     BIO_free(channel);
392     OPENSSL_free(trace_data);
393 #endif
394
395     return 0;
396 }
397
398 int OSSL_trace_set_prefix(int category, const char *prefix)
399 {
400 #ifndef OPENSSL_NO_TRACE
401     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
402         return set_trace_data(category, 0, NULL, &prefix, NULL,
403                               trace_attach_cb, trace_detach_cb);
404 #endif
405     return 0;
406 }
407
408 int OSSL_trace_set_suffix(int category, const char *suffix)
409 {
410 #ifndef OPENSSL_NO_TRACE
411     if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
412         return set_trace_data(category, 0, NULL, NULL, &suffix,
413                               trace_attach_cb, trace_detach_cb);
414 #endif
415     return 0;
416 }
417
418 #ifndef OPENSSL_NO_TRACE
419 static int ossl_trace_get_category(int category)
420 {
421     if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
422         return -1;
423     if (trace_channels[category].bio != NULL)
424         return category;
425     return OSSL_TRACE_CATEGORY_ANY;
426 }
427 #endif
428
429 int OSSL_trace_enabled(int category)
430 {
431     int ret = 0;
432 #ifndef OPENSSL_NO_TRACE
433     category = ossl_trace_get_category(category);
434     if (category >= 0)
435         ret = trace_channels[category].bio != NULL;
436 #endif
437     return ret;
438 }
439
440 BIO *OSSL_trace_begin(int category)
441 {
442     BIO *channel = NULL;
443 #ifndef OPENSSL_NO_TRACE
444     char *prefix = NULL;
445
446     category = ossl_trace_get_category(category);
447     if (category < 0)
448         return NULL;
449
450     channel = trace_channels[category].bio;
451     prefix = trace_channels[category].prefix;
452
453     if (channel != NULL) {
454         CRYPTO_THREAD_write_lock(trace_lock);
455         current_channel = channel;
456         switch (trace_channels[category].type) {
457         case SIMPLE_CHANNEL:
458             if (prefix != NULL) {
459                 (void)BIO_puts(channel, prefix);
460                 (void)BIO_puts(channel, "\n");
461             }
462             break;
463         case CALLBACK_CHANNEL:
464             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_BEGIN,
465                            prefix == NULL ? 0 : strlen(prefix), prefix);
466             break;
467         }
468     }
469 #endif
470     return channel;
471 }
472
473 void OSSL_trace_end(int category, BIO * channel)
474 {
475 #ifndef OPENSSL_NO_TRACE
476     char *suffix = NULL;
477
478     category = ossl_trace_get_category(category);
479     suffix = trace_channels[category].suffix;
480     if (channel != NULL
481         && ossl_assert(channel == current_channel)) {
482         (void)BIO_flush(channel);
483         switch (trace_channels[category].type) {
484         case SIMPLE_CHANNEL:
485             if (suffix != NULL) {
486                 (void)BIO_puts(channel, suffix);
487                 (void)BIO_puts(channel, "\n");
488             }
489             break;
490         case CALLBACK_CHANNEL:
491             (void)BIO_ctrl(channel, OSSL_TRACE_CTRL_END,
492                            suffix == NULL ? 0 : strlen(suffix), suffix);
493             break;
494         }
495         current_channel = NULL;
496         CRYPTO_THREAD_unlock(trace_lock);
497     }
498 #endif
499 }