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