1647d930437d74bacf436deb534fe1bca9f93252
[openssl.git] / crypto / err / err_prn.c
1 /*
2  * Copyright 1995-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 /* TODO: When ERR_STATE becomes opaque, this musts be removed */
11 #define OSSL_FORCE_ERR_STATE
12
13 #include <stdio.h>
14 #include "internal/cryptlib.h"
15 #include <openssl/crypto.h>
16 #include <openssl/buffer.h>
17 #include <openssl/err.h>
18 #include "err_local.h"
19
20 #define ERR_PRINT_BUF_SIZE 4096
21 void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
22                          void *u)
23 {
24     CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
25     unsigned long l;
26     char buf[ERR_PRINT_BUF_SIZE], *hex;
27     const char *lib, *reason;
28     const char *file, *data, *func;
29     int line, flags;
30
31     while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
32         lib = ERR_lib_error_string(l);
33         reason = ERR_reason_error_string(l);
34         if (func == NULL)
35             func = "unknown function";
36         if ((flags & ERR_TXT_STRING) == 0)
37             data = "";
38         hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
39         BIO_snprintf(buf, sizeof(buf), "%s:error:%s:%s:%s:%s:%d:%s\n",
40                      hex == NULL ? "<null>" : hex, lib, func, reason, file,
41                      line, data);
42         OPENSSL_free(hex);
43         if (cb(buf, strlen(buf), u) <= 0)
44             break;              /* abort outputting the error report */
45     }
46 }
47
48 /* auxiliary function for incrementally reporting texts via the error queue */
49 static void put_error(int lib, const char *func, int reason,
50                       const char *file, int line)
51 {
52     ERR_new();
53     ERR_set_debug(file, line, func);
54     ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
55 }
56
57 #define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
58 #define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
59 void ERR_add_error_txt(const char *separator, const char *txt)
60 {
61     const char *file = NULL;
62     int line;
63     const char *func = NULL;
64     const char *data = NULL;
65     int flags;
66     unsigned long err = ERR_peek_last_error();
67
68     if (separator == NULL)
69         separator = "";
70     if (err == 0)
71         put_error(ERR_LIB_NONE, NULL, 0, "", 0);
72
73     do {
74         size_t available_len, data_len;
75         const char *curr = txt, *next = txt;
76         const char *leading_separator = separator;
77         int trailing_separator = 0;
78         char *tmp;
79
80         ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
81         if ((flags & ERR_TXT_STRING) == 0) {
82             data = "";
83             leading_separator = "";
84         }
85         data_len = strlen(data);
86
87         /* workaround for limit of ERR_print_errors_cb() */
88         if (data_len >= MAX_DATA_LEN
89                 || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
90             available_len = 0;
91         else
92             available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
93         /* MAX_DATA_LEN > available_len >= 0 */
94
95         if (*separator == '\0') {
96             const size_t len_next = strlen(next);
97
98             if (len_next <= available_len) {
99                 next += len_next;
100                 curr = NULL; /* no need to split */
101             } else {
102                 next += available_len;
103                 curr = next; /* will split at this point */
104             }
105         } else {
106             while (*next != '\0' && (size_t)(next - txt) <= available_len) {
107                 curr = next;
108                 next = strstr(curr, separator);
109                 if (next != NULL) {
110                     next += strlen(separator);
111                     trailing_separator = *next == '\0';
112                 } else {
113                     next = curr + strlen(curr);
114                 }
115             }
116             if ((size_t)(next - txt) <= available_len)
117                 curr = NULL; /* the above loop implies *next == '\0' */
118         }
119         if (curr != NULL) {
120             /* split error msg at curr since error data would get too long */
121             if (curr != txt) {
122                 tmp = OPENSSL_strndup(txt, curr - txt);
123                 if (tmp == NULL)
124                     return;
125                 ERR_add_error_data(2, separator, tmp);
126                 OPENSSL_free(tmp);
127             }
128             put_error(ERR_GET_LIB(err), func, err, file, line);
129             txt = curr;
130         } else {
131             if (trailing_separator) {
132                 tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
133                 if (tmp == NULL)
134                     return;
135                 /* output txt without the trailing separator */
136                 ERR_add_error_data(2, leading_separator, tmp);
137                 OPENSSL_free(tmp);
138             } else {
139                 ERR_add_error_data(2, leading_separator, txt);
140             }
141             txt = next; /* finished */
142         }
143     } while (*txt != '\0');
144 }
145
146 void ERR_add_error_mem_bio(const char *separator, BIO *bio)
147 {
148     if (bio != NULL) {
149         char *str;
150         long len = BIO_get_mem_data(bio, &str);
151
152         if (len > 0) {
153             if (str[len - 1] != '\0') {
154                 if (BIO_write(bio, "", 1) <= 0)
155                     return;
156
157                 len = BIO_get_mem_data(bio, &str);
158             }
159             if (len > 1)
160                 ERR_add_error_txt(separator, str);
161         }
162     }
163 }
164
165 static int print_bio(const char *str, size_t len, void *bp)
166 {
167     return BIO_write((BIO *)bp, str, len);
168 }
169
170 void ERR_print_errors(BIO *bp)
171 {
172     ERR_print_errors_cb(print_bio, bp);
173 }
174
175 #ifndef OPENSSL_NO_STDIO
176 void ERR_print_errors_fp(FILE *fp)
177 {
178     BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
179     if (bio == NULL)
180         return;
181
182     ERR_print_errors_cb(print_bio, bio);
183     BIO_free(bio);
184 }
185 #endif