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