/src/boringssl/crypto/err/err.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Ensure we can't call OPENSSL_malloc circularly. |
16 | | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
17 | | #include <openssl/err.h> |
18 | | |
19 | | #include <assert.h> |
20 | | #include <errno.h> |
21 | | #include <inttypes.h> |
22 | | #include <limits.h> |
23 | | #include <stdarg.h> |
24 | | #include <string.h> |
25 | | |
26 | | #if defined(OPENSSL_WINDOWS) |
27 | | #include <windows.h> |
28 | | #endif |
29 | | |
30 | | #include <openssl/mem.h> |
31 | | |
32 | | #include "../internal.h" |
33 | | #include "./internal.h" |
34 | | |
35 | | |
36 | | namespace { |
37 | | struct err_error_st { |
38 | | // file contains the filename where the error occurred. |
39 | | const char *file; |
40 | | // data contains a NUL-terminated string with optional data. It is allocated |
41 | | // with system |malloc| and must be freed with |free| (not |OPENSSL_free|) |
42 | | char *data; |
43 | | // packed contains the error library and reason, as packed by ERR_PACK. |
44 | | uint32_t packed; |
45 | | // line contains the line number where the error occurred. |
46 | | uint16_t line; |
47 | | // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|. |
48 | | unsigned mark : 1; |
49 | | }; |
50 | | |
51 | | // ERR_STATE contains the per-thread, error queue. |
52 | | typedef struct err_state_st { |
53 | | // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a |
54 | | // ring buffer. |
55 | | struct err_error_st errors[ERR_NUM_ERRORS]; |
56 | | // top contains the index of the most recent error. If |top| equals |bottom| |
57 | | // then the queue is empty. |
58 | | unsigned top; |
59 | | // bottom contains the index before the least recent error in the queue. |
60 | | unsigned bottom; |
61 | | |
62 | | // to_free, if not NULL, contains a pointer owned by this structure that was |
63 | | // previously a |data| pointer of one of the elements of |errors|. |
64 | | void *to_free; |
65 | | } ERR_STATE; |
66 | | } // namespace |
67 | | |
68 | | extern const uint32_t kOpenSSLReasonValues[]; |
69 | | extern const size_t kOpenSSLReasonValuesLen; |
70 | | extern const char kOpenSSLReasonStringData[]; |
71 | | |
72 | 0 | static char *strdup_libc_malloc(const char *str) { |
73 | | // |strdup| is not in C until C23, so MSVC triggers deprecation warnings, and |
74 | | // glibc and musl gate it on a feature macro. Reimplementing it is easier. |
75 | 0 | size_t len = strlen(str); |
76 | 0 | char *ret = reinterpret_cast<char *>(malloc(len + 1)); |
77 | 0 | if (ret != NULL) { |
78 | 0 | memcpy(ret, str, len + 1); |
79 | 0 | } |
80 | 0 | return ret; |
81 | 0 | } |
82 | | |
83 | | // err_clear clears the given queued error. |
84 | 37.5k | static void err_clear(struct err_error_st *error) { |
85 | 37.5k | free(error->data); |
86 | 37.5k | OPENSSL_memset(error, 0, sizeof(struct err_error_st)); |
87 | 37.5k | } |
88 | | |
89 | 0 | static void err_copy(struct err_error_st *dst, const struct err_error_st *src) { |
90 | 0 | err_clear(dst); |
91 | 0 | dst->file = src->file; |
92 | 0 | if (src->data != NULL) { |
93 | | // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
94 | | // which can affect the error stack. |
95 | 0 | dst->data = strdup_libc_malloc(src->data); |
96 | 0 | } |
97 | 0 | dst->packed = src->packed; |
98 | 0 | dst->line = src->line; |
99 | 0 | } |
100 | | |
101 | | |
102 | | // global_next_library contains the next custom library value to return. |
103 | | static int global_next_library = ERR_NUM_LIBS; |
104 | | |
105 | | // global_next_library_mutex protects |global_next_library| from concurrent |
106 | | // updates. |
107 | | static CRYPTO_MUTEX global_next_library_mutex = CRYPTO_MUTEX_INIT; |
108 | | |
109 | 0 | static void err_state_free(void *statep) { |
110 | 0 | ERR_STATE *state = reinterpret_cast<ERR_STATE *>(statep); |
111 | |
|
112 | 0 | if (state == NULL) { |
113 | 0 | return; |
114 | 0 | } |
115 | | |
116 | 0 | for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) { |
117 | 0 | err_clear(&state->errors[i]); |
118 | 0 | } |
119 | 0 | free(state->to_free); |
120 | 0 | free(state); |
121 | 0 | } |
122 | | |
123 | | // err_get_state gets the ERR_STATE object for the current thread. |
124 | 58.2k | static ERR_STATE *err_get_state(void) { |
125 | 58.2k | ERR_STATE *state = reinterpret_cast<ERR_STATE *>( |
126 | 58.2k | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR)); |
127 | 58.2k | if (state == NULL) { |
128 | 1 | state = reinterpret_cast<ERR_STATE *>(malloc(sizeof(ERR_STATE))); |
129 | 1 | if (state == NULL) { |
130 | 0 | return NULL; |
131 | 0 | } |
132 | 1 | OPENSSL_memset(state, 0, sizeof(ERR_STATE)); |
133 | 1 | if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
134 | 1 | err_state_free)) { |
135 | 0 | return NULL; |
136 | 0 | } |
137 | 1 | } |
138 | | |
139 | 58.2k | return state; |
140 | 58.2k | } |
141 | | |
142 | | static uint32_t get_error_values(int inc, int top, const char **file, int *line, |
143 | 0 | const char **data, int *flags) { |
144 | 0 | unsigned i = 0; |
145 | 0 | ERR_STATE *state; |
146 | 0 | struct err_error_st *error; |
147 | 0 | uint32_t ret; |
148 | |
|
149 | 0 | state = err_get_state(); |
150 | 0 | if (state == NULL || state->bottom == state->top) { |
151 | 0 | return 0; |
152 | 0 | } |
153 | | |
154 | 0 | if (top) { |
155 | 0 | assert(!inc); |
156 | | // last error |
157 | 0 | i = state->top; |
158 | 0 | } else { |
159 | 0 | i = (state->bottom + 1) % ERR_NUM_ERRORS; |
160 | 0 | } |
161 | | |
162 | 0 | error = &state->errors[i]; |
163 | 0 | ret = error->packed; |
164 | |
|
165 | 0 | if (file != NULL && line != NULL) { |
166 | 0 | if (error->file == NULL) { |
167 | 0 | *file = "NA"; |
168 | 0 | *line = 0; |
169 | 0 | } else { |
170 | 0 | *file = error->file; |
171 | 0 | *line = error->line; |
172 | 0 | } |
173 | 0 | } |
174 | |
|
175 | 0 | if (data != NULL) { |
176 | 0 | if (error->data == NULL) { |
177 | 0 | *data = ""; |
178 | 0 | if (flags != NULL) { |
179 | 0 | *flags = 0; |
180 | 0 | } |
181 | 0 | } else { |
182 | 0 | *data = error->data; |
183 | 0 | if (flags != NULL) { |
184 | | // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a |
185 | | // static lifetime. In both cases, we retain ownership of the string, |
186 | | // and the caller is not expected to free it. |
187 | 0 | *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED; |
188 | 0 | } |
189 | | // If this error is being removed, take ownership of data from |
190 | | // the error. The semantics are such that the caller doesn't |
191 | | // take ownership either. Instead the error system takes |
192 | | // ownership and retains it until the next call that affects the |
193 | | // error queue. |
194 | 0 | if (inc) { |
195 | 0 | if (error->data != NULL) { |
196 | 0 | free(state->to_free); |
197 | 0 | state->to_free = error->data; |
198 | 0 | } |
199 | 0 | error->data = NULL; |
200 | 0 | } |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | 0 | if (inc) { |
205 | 0 | assert(!top); |
206 | 0 | err_clear(error); |
207 | 0 | state->bottom = i; |
208 | 0 | } |
209 | | |
210 | 0 | return ret; |
211 | 0 | } |
212 | | |
213 | 0 | uint32_t ERR_get_error(void) { |
214 | 0 | return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
215 | 0 | } |
216 | | |
217 | 0 | uint32_t ERR_get_error_line(const char **file, int *line) { |
218 | 0 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
219 | 0 | } |
220 | | |
221 | | uint32_t ERR_get_error_line_data(const char **file, int *line, |
222 | 0 | const char **data, int *flags) { |
223 | 0 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
224 | 0 | } |
225 | | |
226 | 0 | uint32_t ERR_peek_error(void) { |
227 | 0 | return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
228 | 0 | } |
229 | | |
230 | 0 | uint32_t ERR_peek_error_line(const char **file, int *line) { |
231 | 0 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
232 | 0 | } |
233 | | |
234 | | uint32_t ERR_peek_error_line_data(const char **file, int *line, |
235 | 0 | const char **data, int *flags) { |
236 | 0 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
237 | 0 | flags); |
238 | 0 | } |
239 | | |
240 | 0 | uint32_t ERR_peek_last_error(void) { |
241 | 0 | return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
242 | 0 | } |
243 | | |
244 | 0 | uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
245 | 0 | return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
246 | 0 | } |
247 | | |
248 | | uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
249 | 0 | const char **data, int *flags) { |
250 | 0 | return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
251 | 0 | } |
252 | | |
253 | 0 | void ERR_clear_error(void) { |
254 | 0 | ERR_STATE *const state = err_get_state(); |
255 | 0 | unsigned i; |
256 | |
|
257 | 0 | if (state == NULL) { |
258 | 0 | return; |
259 | 0 | } |
260 | | |
261 | 0 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
262 | 0 | err_clear(&state->errors[i]); |
263 | 0 | } |
264 | 0 | free(state->to_free); |
265 | 0 | state->to_free = NULL; |
266 | |
|
267 | 0 | state->top = state->bottom = 0; |
268 | 0 | } |
269 | | |
270 | 0 | void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
271 | 0 | if (tid != NULL) { |
272 | 0 | assert(0); |
273 | 0 | return; |
274 | 0 | } |
275 | | |
276 | 0 | ERR_clear_error(); |
277 | 0 | } |
278 | | |
279 | 0 | int ERR_get_next_error_library(void) { |
280 | 0 | int ret; |
281 | |
|
282 | 0 | CRYPTO_MUTEX_lock_write(&global_next_library_mutex); |
283 | 0 | ret = global_next_library++; |
284 | 0 | CRYPTO_MUTEX_unlock_write(&global_next_library_mutex); |
285 | |
|
286 | 0 | return ret; |
287 | 0 | } |
288 | | |
289 | 0 | void ERR_remove_state(unsigned long pid) { ERR_clear_error(); } |
290 | | |
291 | 0 | void ERR_clear_system_error(void) { errno = 0; } |
292 | | |
293 | | // err_string_cmp is a compare function for searching error values with |
294 | | // |bsearch| in |err_string_lookup|. |
295 | 0 | static int err_string_cmp(const void *a, const void *b) { |
296 | 0 | const uint32_t a_key = *((const uint32_t *)a) >> 15; |
297 | 0 | const uint32_t b_key = *((const uint32_t *)b) >> 15; |
298 | |
|
299 | 0 | if (a_key < b_key) { |
300 | 0 | return -1; |
301 | 0 | } else if (a_key > b_key) { |
302 | 0 | return 1; |
303 | 0 | } else { |
304 | 0 | return 0; |
305 | 0 | } |
306 | 0 | } |
307 | | |
308 | | // err_string_lookup looks up the string associated with |lib| and |key| in |
309 | | // |values| and |string_data|. It returns the string or NULL if not found. |
310 | | static const char *err_string_lookup(uint32_t lib, uint32_t key, |
311 | | const uint32_t *values, size_t num_values, |
312 | 0 | const char *string_data) { |
313 | | // |values| points to data in err_data.h, which is generated by |
314 | | // err_data_generate.go. It's an array of uint32_t values. Each value has the |
315 | | // following structure: |
316 | | // | lib | key | offset | |
317 | | // |6 bits| 11 bits | 15 bits | |
318 | | // |
319 | | // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
320 | | // The |key| is a reason code, depending on the context. |
321 | | // The |offset| is the number of bytes from the start of |string_data| where |
322 | | // the (NUL terminated) string for this value can be found. |
323 | | // |
324 | | // Values are sorted based on treating the |lib| and |key| part as an |
325 | | // unsigned integer. |
326 | 0 | if (lib >= (1 << 6) || key >= (1 << 11)) { |
327 | 0 | return NULL; |
328 | 0 | } |
329 | 0 | uint32_t search_key = lib << 26 | key << 15; |
330 | 0 | const uint32_t *result = reinterpret_cast<const uint32_t *>(bsearch( |
331 | 0 | &search_key, values, num_values, sizeof(uint32_t), err_string_cmp)); |
332 | 0 | if (result == NULL) { |
333 | 0 | return NULL; |
334 | 0 | } |
335 | | |
336 | 0 | return &string_data[(*result) & 0x7fff]; |
337 | 0 | } |
338 | | |
339 | | namespace { |
340 | | typedef struct library_name_st { |
341 | | const char *str; |
342 | | const char *symbol; |
343 | | const char *reason_symbol; |
344 | | } LIBRARY_NAME; |
345 | | } // namespace |
346 | | |
347 | | static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = { |
348 | | {"invalid library (0)", NULL, NULL}, |
349 | | {"unknown library", "NONE", "NONE_LIB"}, |
350 | | {"system library", "SYS", "SYS_LIB"}, |
351 | | {"bignum routines", "BN", "BN_LIB"}, |
352 | | {"RSA routines", "RSA", "RSA_LIB"}, |
353 | | {"Diffie-Hellman routines", "DH", "DH_LIB"}, |
354 | | {"public key routines", "EVP", "EVP_LIB"}, |
355 | | {"memory buffer routines", "BUF", "BUF_LIB"}, |
356 | | {"object identifier routines", "OBJ", "OBJ_LIB"}, |
357 | | {"PEM routines", "PEM", "PEM_LIB"}, |
358 | | {"DSA routines", "DSA", "DSA_LIB"}, |
359 | | {"X.509 certificate routines", "X509", "X509_LIB"}, |
360 | | {"ASN.1 encoding routines", "ASN1", "ASN1_LIB"}, |
361 | | {"configuration file routines", "CONF", "CONF_LIB"}, |
362 | | {"common libcrypto routines", "CRYPTO", "CRYPTO_LIB"}, |
363 | | {"elliptic curve routines", "EC", "EC_LIB"}, |
364 | | {"SSL routines", "SSL", "SSL_LIB"}, |
365 | | {"BIO routines", "BIO", "BIO_LIB"}, |
366 | | {"PKCS7 routines", "PKCS7", "PKCS7_LIB"}, |
367 | | {"PKCS8 routines", "PKCS8", "PKCS8_LIB"}, |
368 | | {"X509 V3 routines", "X509V3", "X509V3_LIB"}, |
369 | | {"random number generator", "RAND", "RAND_LIB"}, |
370 | | {"ENGINE routines", "ENGINE", "ENGINE_LIB"}, |
371 | | {"OCSP routines", "OCSP", "OCSP_LIB"}, |
372 | | {"UI routines", "UI", "UI_LIB"}, |
373 | | {"COMP routines", "COMP", "COMP_LIB"}, |
374 | | {"ECDSA routines", "ECDSA", "ECDSA_LIB"}, |
375 | | {"ECDH routines", "ECDH", "ECDH_LIB"}, |
376 | | {"HMAC routines", "HMAC", "HMAC_LIB"}, |
377 | | {"Digest functions", "DIGEST", "DIGEST_LIB"}, |
378 | | {"Cipher functions", "CIPHER", "CIPHER_LIB"}, |
379 | | {"HKDF functions", "HKDF", "HKDF_LIB"}, |
380 | | {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"}, |
381 | | {"User defined functions", "USER", "USER_LIB"}, |
382 | | }; |
383 | | |
384 | 0 | static const char *err_lib_error_string(uint32_t packed_error) { |
385 | 0 | const uint32_t lib = ERR_GET_LIB(packed_error); |
386 | 0 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].str; |
387 | 0 | } |
388 | | |
389 | 0 | const char *ERR_lib_error_string(uint32_t packed_error) { |
390 | 0 | const char *ret = err_lib_error_string(packed_error); |
391 | 0 | return ret == NULL ? "unknown library" : ret; |
392 | 0 | } |
393 | | |
394 | 0 | const char *ERR_lib_symbol_name(uint32_t packed_error) { |
395 | 0 | const uint32_t lib = ERR_GET_LIB(packed_error); |
396 | 0 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].symbol; |
397 | 0 | } |
398 | | |
399 | 0 | const char *ERR_func_error_string(uint32_t packed_error) { |
400 | 0 | return "OPENSSL_internal"; |
401 | 0 | } |
402 | | |
403 | 0 | static const char *err_reason_error_string(uint32_t packed_error, int symbol) { |
404 | 0 | const uint32_t lib = ERR_GET_LIB(packed_error); |
405 | 0 | const uint32_t reason = ERR_GET_REASON(packed_error); |
406 | |
|
407 | 0 | if (lib == ERR_LIB_SYS) { |
408 | 0 | if (!symbol && reason < 127) { |
409 | 0 | return strerror(reason); |
410 | 0 | } |
411 | 0 | return NULL; |
412 | 0 | } |
413 | | |
414 | 0 | if (reason < ERR_NUM_LIBS) { |
415 | 0 | return symbol ? kLibraryNames[reason].reason_symbol |
416 | 0 | : kLibraryNames[reason].str; |
417 | 0 | } |
418 | | |
419 | 0 | if (reason < 100) { |
420 | | // TODO(davidben): All our other reason strings match the symbol name. Only |
421 | | // the common ones differ. Should we just consistently return the symbol |
422 | | // name? |
423 | 0 | switch (reason) { |
424 | 0 | case ERR_R_MALLOC_FAILURE: |
425 | 0 | return symbol ? "MALLOC_FAILURE" : "malloc failure"; |
426 | 0 | case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
427 | 0 | return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED" |
428 | 0 | : "function should not have been called"; |
429 | 0 | case ERR_R_PASSED_NULL_PARAMETER: |
430 | 0 | return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter"; |
431 | 0 | case ERR_R_INTERNAL_ERROR: |
432 | 0 | return symbol ? "INTERNAL_ERROR" : "internal error"; |
433 | 0 | case ERR_R_OVERFLOW: |
434 | 0 | return symbol ? "OVERFLOW" : "overflow"; |
435 | 0 | default: |
436 | 0 | return NULL; |
437 | 0 | } |
438 | 0 | } |
439 | | |
440 | | // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we |
441 | | // do not need to check |symbol|. |
442 | 0 | return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
443 | 0 | kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
444 | 0 | } |
445 | | |
446 | 0 | const char *ERR_reason_error_string(uint32_t packed_error) { |
447 | 0 | const char *ret = err_reason_error_string(packed_error, /*symbol=*/0); |
448 | 0 | return ret == NULL ? "unknown error" : ret; |
449 | 0 | } |
450 | | |
451 | 0 | const char *ERR_reason_symbol_name(uint32_t packed_error) { |
452 | 0 | return err_reason_error_string(packed_error, /*symbol=*/1); |
453 | 0 | } |
454 | | |
455 | 0 | char *ERR_error_string(uint32_t packed_error, char *ret) { |
456 | 0 | static char buf[ERR_ERROR_STRING_BUF_LEN]; |
457 | |
|
458 | 0 | if (ret == NULL) { |
459 | | // TODO(fork): remove this. |
460 | 0 | ret = buf; |
461 | 0 | } |
462 | |
|
463 | 0 | #if !defined(NDEBUG) |
464 | | // This is aimed to help catch callers who don't provide |
465 | | // |ERR_ERROR_STRING_BUF_LEN| bytes of space. |
466 | 0 | OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
467 | 0 | #endif |
468 | |
|
469 | 0 | return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
470 | 0 | } |
471 | | |
472 | 0 | char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
473 | 0 | if (len == 0) { |
474 | 0 | return NULL; |
475 | 0 | } |
476 | | |
477 | 0 | unsigned lib = ERR_GET_LIB(packed_error); |
478 | 0 | unsigned reason = ERR_GET_REASON(packed_error); |
479 | |
|
480 | 0 | const char *lib_str = err_lib_error_string(packed_error); |
481 | 0 | const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0); |
482 | |
|
483 | 0 | char lib_buf[32], reason_buf[32]; |
484 | 0 | if (lib_str == NULL) { |
485 | 0 | snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
486 | 0 | lib_str = lib_buf; |
487 | 0 | } |
488 | |
|
489 | 0 | if (reason_str == NULL) { |
490 | 0 | snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
491 | 0 | reason_str = reason_buf; |
492 | 0 | } |
493 | |
|
494 | 0 | int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
495 | 0 | packed_error, lib_str, reason_str); |
496 | 0 | if (ret >= 0 && (size_t)ret >= len) { |
497 | | // The output was truncated; make sure we always have 5 colon-separated |
498 | | // fields, i.e. 4 colons. |
499 | 0 | static const unsigned num_colons = 4; |
500 | 0 | unsigned i; |
501 | 0 | char *s = buf; |
502 | |
|
503 | 0 | if (len <= num_colons) { |
504 | | // In this situation it's not possible to ensure that the correct number |
505 | | // of colons are included in the output. |
506 | 0 | return buf; |
507 | 0 | } |
508 | | |
509 | 0 | for (i = 0; i < num_colons; i++) { |
510 | 0 | char *colon = strchr(s, ':'); |
511 | 0 | char *last_pos = &buf[len - 1] - num_colons + i; |
512 | |
|
513 | 0 | if (colon == NULL || colon > last_pos) { |
514 | | // set colon |i| at last possible position (buf[len-1] is the |
515 | | // terminating 0). If we're setting this colon, then all whole of the |
516 | | // rest of the string must be colons in order to have the correct |
517 | | // number. |
518 | 0 | OPENSSL_memset(last_pos, ':', num_colons - i); |
519 | 0 | break; |
520 | 0 | } |
521 | | |
522 | 0 | s = colon + 1; |
523 | 0 | } |
524 | 0 | } |
525 | | |
526 | 0 | return buf; |
527 | 0 | } |
528 | | |
529 | 0 | void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { |
530 | 0 | char buf[ERR_ERROR_STRING_BUF_LEN]; |
531 | 0 | char buf2[1024]; |
532 | 0 | const char *file, *data; |
533 | 0 | int line, flags; |
534 | 0 | uint32_t packed_error; |
535 | | |
536 | | // thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
537 | | // for this thread. |
538 | 0 | const unsigned long thread_hash = (uintptr_t)err_get_state(); |
539 | |
|
540 | 0 | for (;;) { |
541 | 0 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
542 | 0 | if (packed_error == 0) { |
543 | 0 | break; |
544 | 0 | } |
545 | | |
546 | 0 | ERR_error_string_n(packed_error, buf, sizeof(buf)); |
547 | 0 | snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file, |
548 | 0 | line, (flags & ERR_FLAG_STRING) ? data : ""); |
549 | 0 | if (callback(buf2, strlen(buf2), ctx) <= 0) { |
550 | 0 | break; |
551 | 0 | } |
552 | 0 | } |
553 | 0 | } |
554 | | |
555 | 0 | static int print_errors_to_file(const char *msg, size_t msg_len, void *ctx) { |
556 | 0 | assert(msg[msg_len] == '\0'); |
557 | 0 | FILE *fp = reinterpret_cast<FILE *>(ctx); |
558 | 0 | int res = fputs(msg, fp); |
559 | 0 | return res < 0 ? 0 : 1; |
560 | 0 | } |
561 | | |
562 | 0 | void ERR_print_errors_fp(FILE *file) { |
563 | 0 | ERR_print_errors_cb(print_errors_to_file, file); |
564 | 0 | } |
565 | | |
566 | | // err_set_error_data sets the data on the most recent error. |
567 | 20.6k | static void err_set_error_data(char *data) { |
568 | 20.6k | ERR_STATE *const state = err_get_state(); |
569 | 20.6k | struct err_error_st *error; |
570 | | |
571 | 20.6k | if (state == NULL || state->top == state->bottom) { |
572 | 0 | free(data); |
573 | 0 | return; |
574 | 0 | } |
575 | | |
576 | 20.6k | error = &state->errors[state->top]; |
577 | | |
578 | 20.6k | free(error->data); |
579 | 20.6k | error->data = data; |
580 | 20.6k | } |
581 | | |
582 | | void ERR_put_error(int library, int unused, int reason, const char *file, |
583 | 37.5k | unsigned line) { |
584 | 37.5k | ERR_STATE *const state = err_get_state(); |
585 | 37.5k | struct err_error_st *error; |
586 | | |
587 | 37.5k | if (state == NULL) { |
588 | 0 | return; |
589 | 0 | } |
590 | | |
591 | 37.5k | if (library == ERR_LIB_SYS && reason == 0) { |
592 | | #if defined(OPENSSL_WINDOWS) |
593 | | reason = GetLastError(); |
594 | | #else |
595 | 0 | reason = errno; |
596 | 0 | #endif |
597 | 0 | } |
598 | | |
599 | 37.5k | state->top = (state->top + 1) % ERR_NUM_ERRORS; |
600 | 37.5k | if (state->top == state->bottom) { |
601 | 37.5k | state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
602 | 37.5k | } |
603 | | |
604 | 37.5k | error = &state->errors[state->top]; |
605 | 37.5k | err_clear(error); |
606 | 37.5k | error->file = file; |
607 | 37.5k | error->line = line; |
608 | 37.5k | error->packed = ERR_PACK(library, reason); |
609 | 37.5k | } |
610 | | |
611 | | // ERR_add_error_data_vdata takes a variable number of const char* pointers, |
612 | | // concatenates them and sets the result as the data on the most recent |
613 | | // error. |
614 | 20.1k | static void err_add_error_vdata(unsigned num, va_list args) { |
615 | 20.1k | size_t total_size = 0; |
616 | 20.1k | const char *substr; |
617 | 20.1k | char *buf; |
618 | | |
619 | 20.1k | va_list args_copy; |
620 | 20.1k | va_copy(args_copy, args); |
621 | 89.4k | for (size_t i = 0; i < num; i++) { |
622 | 69.3k | substr = va_arg(args_copy, const char *); |
623 | 69.3k | if (substr == NULL) { |
624 | 3.77k | continue; |
625 | 3.77k | } |
626 | 65.5k | size_t substr_len = strlen(substr); |
627 | 65.5k | if (SIZE_MAX - total_size < substr_len) { |
628 | 0 | return; // Would overflow. |
629 | 0 | } |
630 | 65.5k | total_size += substr_len; |
631 | 65.5k | } |
632 | 20.1k | va_end(args_copy); |
633 | 20.1k | if (total_size == SIZE_MAX) { |
634 | 0 | return; // Would overflow. |
635 | 0 | } |
636 | 20.1k | total_size += 1; // NUL terminator. |
637 | 20.1k | if ((buf = reinterpret_cast<char *>(malloc(total_size))) == NULL) { |
638 | 0 | return; |
639 | 0 | } |
640 | 20.1k | buf[0] = '\0'; |
641 | 89.4k | for (size_t i = 0; i < num; i++) { |
642 | 69.3k | substr = va_arg(args, const char *); |
643 | 69.3k | if (substr == NULL) { |
644 | 3.77k | continue; |
645 | 3.77k | } |
646 | 65.5k | if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) { |
647 | 0 | assert(0); // should not be possible. |
648 | 0 | } |
649 | 65.5k | } |
650 | 20.1k | err_set_error_data(buf); |
651 | 20.1k | } |
652 | | |
653 | 20.1k | void ERR_add_error_data(unsigned count, ...) { |
654 | 20.1k | va_list args; |
655 | 20.1k | va_start(args, count); |
656 | 20.1k | err_add_error_vdata(count, args); |
657 | 20.1k | va_end(args); |
658 | 20.1k | } |
659 | | |
660 | 511 | void ERR_add_error_dataf(const char *format, ...) { |
661 | 511 | char *buf = NULL; |
662 | 511 | va_list ap; |
663 | | |
664 | 511 | va_start(ap, format); |
665 | 511 | if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) { |
666 | 0 | return; |
667 | 0 | } |
668 | 511 | va_end(ap); |
669 | | |
670 | 511 | err_set_error_data(buf); |
671 | 511 | } |
672 | | |
673 | 0 | void ERR_set_error_data(char *data, int flags) { |
674 | 0 | if (!(flags & ERR_FLAG_STRING)) { |
675 | | // We do not support non-string error data. |
676 | 0 | assert(0); |
677 | 0 | return; |
678 | 0 | } |
679 | | // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
680 | | // which can affect the error stack. |
681 | 0 | char *copy = strdup_libc_malloc(data); |
682 | 0 | if (copy != NULL) { |
683 | 0 | err_set_error_data(copy); |
684 | 0 | } |
685 | 0 | if (flags & ERR_FLAG_MALLOCED) { |
686 | | // We can not take ownership of |data| directly because it is allocated with |
687 | | // |OPENSSL_malloc| and we will free it with system |free| later. |
688 | 0 | OPENSSL_free(data); |
689 | 0 | } |
690 | 0 | } |
691 | | |
692 | 0 | int ERR_set_mark(void) { |
693 | 0 | ERR_STATE *const state = err_get_state(); |
694 | |
|
695 | 0 | if (state == NULL || state->bottom == state->top) { |
696 | 0 | return 0; |
697 | 0 | } |
698 | 0 | state->errors[state->top].mark = 1; |
699 | 0 | return 1; |
700 | 0 | } |
701 | | |
702 | 0 | int ERR_pop_to_mark(void) { |
703 | 0 | ERR_STATE *const state = err_get_state(); |
704 | |
|
705 | 0 | if (state == NULL) { |
706 | 0 | return 0; |
707 | 0 | } |
708 | | |
709 | 0 | while (state->bottom != state->top) { |
710 | 0 | struct err_error_st *error = &state->errors[state->top]; |
711 | |
|
712 | 0 | if (error->mark) { |
713 | 0 | error->mark = 0; |
714 | 0 | return 1; |
715 | 0 | } |
716 | | |
717 | 0 | err_clear(error); |
718 | 0 | if (state->top == 0) { |
719 | 0 | state->top = ERR_NUM_ERRORS - 1; |
720 | 0 | } else { |
721 | 0 | state->top--; |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | 0 | return 0; |
726 | 0 | } |
727 | | |
728 | 0 | void ERR_load_crypto_strings(void) {} |
729 | | |
730 | 0 | void ERR_free_strings(void) {} |
731 | | |
732 | 0 | void ERR_load_BIO_strings(void) {} |
733 | | |
734 | 0 | void ERR_load_ERR_strings(void) {} |
735 | | |
736 | 0 | void ERR_load_RAND_strings(void) {} |
737 | | |
738 | | struct err_save_state_st { |
739 | | struct err_error_st *errors; |
740 | | size_t num_errors; |
741 | | }; |
742 | | |
743 | 0 | void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) { |
744 | 0 | if (state == NULL) { |
745 | 0 | return; |
746 | 0 | } |
747 | 0 | for (size_t i = 0; i < state->num_errors; i++) { |
748 | 0 | err_clear(&state->errors[i]); |
749 | 0 | } |
750 | 0 | free(state->errors); |
751 | 0 | free(state); |
752 | 0 | } |
753 | | |
754 | 0 | ERR_SAVE_STATE *ERR_save_state(void) { |
755 | 0 | ERR_STATE *const state = err_get_state(); |
756 | 0 | if (state == NULL || state->top == state->bottom) { |
757 | 0 | return NULL; |
758 | 0 | } |
759 | | |
760 | 0 | ERR_SAVE_STATE *ret = |
761 | 0 | reinterpret_cast<ERR_SAVE_STATE *>(malloc(sizeof(ERR_SAVE_STATE))); |
762 | 0 | if (ret == NULL) { |
763 | 0 | return NULL; |
764 | 0 | } |
765 | | |
766 | | // Errors are stored in the range (bottom, top]. |
767 | 0 | size_t num_errors = state->top >= state->bottom |
768 | 0 | ? state->top - state->bottom |
769 | 0 | : ERR_NUM_ERRORS + state->top - state->bottom; |
770 | 0 | assert(num_errors < ERR_NUM_ERRORS); |
771 | 0 | ret->errors = reinterpret_cast<err_error_st *>( |
772 | 0 | malloc(num_errors * sizeof(struct err_error_st))); |
773 | 0 | if (ret->errors == NULL) { |
774 | 0 | free(ret); |
775 | 0 | return NULL; |
776 | 0 | } |
777 | 0 | OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st)); |
778 | 0 | ret->num_errors = num_errors; |
779 | |
|
780 | 0 | for (size_t i = 0; i < num_errors; i++) { |
781 | 0 | size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS; |
782 | 0 | err_copy(&ret->errors[i], &state->errors[j]); |
783 | 0 | } |
784 | 0 | return ret; |
785 | 0 | } |
786 | | |
787 | 0 | void ERR_restore_state(const ERR_SAVE_STATE *state) { |
788 | 0 | if (state == NULL || state->num_errors == 0) { |
789 | 0 | ERR_clear_error(); |
790 | 0 | return; |
791 | 0 | } |
792 | | |
793 | 0 | if (state->num_errors >= ERR_NUM_ERRORS) { |
794 | 0 | abort(); |
795 | 0 | } |
796 | | |
797 | 0 | ERR_STATE *const dst = err_get_state(); |
798 | 0 | if (dst == NULL) { |
799 | 0 | return; |
800 | 0 | } |
801 | | |
802 | 0 | for (size_t i = 0; i < state->num_errors; i++) { |
803 | 0 | err_copy(&dst->errors[i], &state->errors[i]); |
804 | 0 | } |
805 | 0 | dst->top = (unsigned)(state->num_errors - 1); |
806 | 0 | dst->bottom = ERR_NUM_ERRORS - 1; |
807 | 0 | } |