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