/src/boringssl/crypto/err/err.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] |
56 | | */ |
57 | | /* ==================================================================== |
58 | | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
59 | | * |
60 | | * Redistribution and use in source and binary forms, with or without |
61 | | * modification, are permitted provided that the following conditions |
62 | | * are met: |
63 | | * |
64 | | * 1. Redistributions of source code must retain the above copyright |
65 | | * notice, this list of conditions and the following disclaimer. |
66 | | * |
67 | | * 2. Redistributions in binary form must reproduce the above copyright |
68 | | * notice, this list of conditions and the following disclaimer in |
69 | | * the documentation and/or other materials provided with the |
70 | | * distribution. |
71 | | * |
72 | | * 3. All advertising materials mentioning features or use of this |
73 | | * software must display the following acknowledgment: |
74 | | * "This product includes software developed by the OpenSSL Project |
75 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
76 | | * |
77 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
78 | | * endorse or promote products derived from this software without |
79 | | * prior written permission. For written permission, please contact |
80 | | * openssl-core@openssl.org. |
81 | | * |
82 | | * 5. Products derived from this software may not be called "OpenSSL" |
83 | | * nor may "OpenSSL" appear in their names without prior written |
84 | | * permission of the OpenSSL Project. |
85 | | * |
86 | | * 6. Redistributions of any form whatsoever must retain the following |
87 | | * acknowledgment: |
88 | | * "This product includes software developed by the OpenSSL Project |
89 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
90 | | * |
91 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
92 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
93 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
94 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
95 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
96 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
97 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
98 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
99 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
100 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
101 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
102 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
103 | | * ==================================================================== |
104 | | * |
105 | | * This product includes cryptographic software written by Eric Young |
106 | | * (eay@cryptsoft.com). This product includes software written by Tim |
107 | | * Hudson (tjh@cryptsoft.com). */ |
108 | | |
109 | | // Ensure we can't call OPENSSL_malloc circularly. |
110 | | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
111 | | #include <openssl/err.h> |
112 | | |
113 | | #include <assert.h> |
114 | | #include <errno.h> |
115 | | #include <inttypes.h> |
116 | | #include <limits.h> |
117 | | #include <stdarg.h> |
118 | | #include <string.h> |
119 | | |
120 | | #if defined(OPENSSL_WINDOWS) |
121 | | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
122 | | #include <windows.h> |
123 | | OPENSSL_MSVC_PRAGMA(warning(pop)) |
124 | | #endif |
125 | | |
126 | | #include <openssl/mem.h> |
127 | | #include <openssl/thread.h> |
128 | | |
129 | | #include "../internal.h" |
130 | | #include "./internal.h" |
131 | | |
132 | | |
133 | | struct err_error_st { |
134 | | // file contains the filename where the error occurred. |
135 | | const char *file; |
136 | | // data contains a NUL-terminated string with optional data. It is allocated |
137 | | // with system |malloc| and must be freed with |free| (not |OPENSSL_free|) |
138 | | char *data; |
139 | | // packed contains the error library and reason, as packed by ERR_PACK. |
140 | | uint32_t packed; |
141 | | // line contains the line number where the error occurred. |
142 | | uint16_t line; |
143 | | // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|. |
144 | | unsigned mark : 1; |
145 | | }; |
146 | | |
147 | | // ERR_STATE contains the per-thread, error queue. |
148 | | typedef struct err_state_st { |
149 | | // errors contains the ERR_NUM_ERRORS most recent errors, organised as a ring |
150 | | // buffer. |
151 | | struct err_error_st errors[ERR_NUM_ERRORS]; |
152 | | // top contains the index one past the most recent error. If |top| equals |
153 | | // |bottom| then the queue is empty. |
154 | | unsigned top; |
155 | | // bottom contains the index of the last error in the queue. |
156 | | unsigned bottom; |
157 | | |
158 | | // to_free, if not NULL, contains a pointer owned by this structure that was |
159 | | // previously a |data| pointer of one of the elements of |errors|. |
160 | | void *to_free; |
161 | | } ERR_STATE; |
162 | | |
163 | | extern const uint32_t kOpenSSLReasonValues[]; |
164 | | extern const size_t kOpenSSLReasonValuesLen; |
165 | | extern const char kOpenSSLReasonStringData[]; |
166 | | |
167 | | // err_clear clears the given queued error. |
168 | 3.83M | static void err_clear(struct err_error_st *error) { |
169 | 3.83M | free(error->data); |
170 | 3.83M | OPENSSL_memset(error, 0, sizeof(struct err_error_st)); |
171 | 3.83M | } |
172 | | |
173 | 47.8k | static void err_copy(struct err_error_st *dst, const struct err_error_st *src) { |
174 | 47.8k | err_clear(dst); |
175 | 47.8k | dst->file = src->file; |
176 | 47.8k | if (src->data != NULL) { |
177 | | // Disable deprecated functions on msvc so it doesn't complain about strdup. |
178 | 6.94k | OPENSSL_MSVC_PRAGMA(warning(push)) |
179 | 6.94k | OPENSSL_MSVC_PRAGMA(warning(disable : 4996)) |
180 | | // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
181 | | // which can affect the error stack. |
182 | 6.94k | dst->data = strdup(src->data); |
183 | 6.94k | OPENSSL_MSVC_PRAGMA(warning(pop)) |
184 | 6.94k | } |
185 | 47.8k | dst->packed = src->packed; |
186 | 47.8k | dst->line = src->line; |
187 | 47.8k | } |
188 | | |
189 | | |
190 | | // global_next_library contains the next custom library value to return. |
191 | | static int global_next_library = ERR_NUM_LIBS; |
192 | | |
193 | | // global_next_library_mutex protects |global_next_library| from concurrent |
194 | | // updates. |
195 | | static struct CRYPTO_STATIC_MUTEX global_next_library_mutex = |
196 | | CRYPTO_STATIC_MUTEX_INIT; |
197 | | |
198 | 0 | static void err_state_free(void *statep) { |
199 | 0 | ERR_STATE *state = statep; |
200 | |
|
201 | 0 | if (state == NULL) { |
202 | 0 | return; |
203 | 0 | } |
204 | | |
205 | 0 | for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) { |
206 | 0 | err_clear(&state->errors[i]); |
207 | 0 | } |
208 | 0 | free(state->to_free); |
209 | 0 | free(state); |
210 | 0 | } |
211 | | |
212 | | // err_get_state gets the ERR_STATE object for the current thread. |
213 | 734k | static ERR_STATE *err_get_state(void) { |
214 | 734k | ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR); |
215 | 734k | if (state == NULL) { |
216 | 18 | state = malloc(sizeof(ERR_STATE)); |
217 | 18 | if (state == NULL) { |
218 | 0 | return NULL; |
219 | 0 | } |
220 | 18 | OPENSSL_memset(state, 0, sizeof(ERR_STATE)); |
221 | 18 | if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
222 | 18 | err_state_free)) { |
223 | 0 | return NULL; |
224 | 0 | } |
225 | 18 | } |
226 | | |
227 | 734k | return state; |
228 | 734k | } |
229 | | |
230 | | static uint32_t get_error_values(int inc, int top, const char **file, int *line, |
231 | 33.1k | const char **data, int *flags) { |
232 | 33.1k | unsigned i = 0; |
233 | 33.1k | ERR_STATE *state; |
234 | 33.1k | struct err_error_st *error; |
235 | 33.1k | uint32_t ret; |
236 | | |
237 | 33.1k | state = err_get_state(); |
238 | 33.1k | if (state == NULL || state->bottom == state->top) { |
239 | 9.18k | return 0; |
240 | 9.18k | } |
241 | | |
242 | 24.0k | if (top) { |
243 | 1.00k | assert(!inc); |
244 | | // last error |
245 | 1.00k | i = state->top; |
246 | 23.0k | } else { |
247 | 23.0k | i = (state->bottom + 1) % ERR_NUM_ERRORS; |
248 | 23.0k | } |
249 | | |
250 | 24.0k | error = &state->errors[i]; |
251 | 24.0k | ret = error->packed; |
252 | | |
253 | 24.0k | if (file != NULL && line != NULL) { |
254 | 8.12k | if (error->file == NULL) { |
255 | 0 | *file = "NA"; |
256 | 0 | *line = 0; |
257 | 8.12k | } else { |
258 | 8.12k | *file = error->file; |
259 | 8.12k | *line = error->line; |
260 | 8.12k | } |
261 | 8.12k | } |
262 | | |
263 | 24.0k | if (data != NULL) { |
264 | 8.12k | if (error->data == NULL) { |
265 | 7.61k | *data = ""; |
266 | 7.61k | if (flags != NULL) { |
267 | 7.61k | *flags = 0; |
268 | 7.61k | } |
269 | 7.61k | } else { |
270 | 514 | *data = error->data; |
271 | 514 | if (flags != NULL) { |
272 | | // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a |
273 | | // static lifetime. In both cases, we retain ownership of the string, |
274 | | // and the caller is not expected to free it. |
275 | 514 | *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED; |
276 | 514 | } |
277 | | // If this error is being removed, take ownership of data from |
278 | | // the error. The semantics are such that the caller doesn't |
279 | | // take ownership either. Instead the error system takes |
280 | | // ownership and retains it until the next call that affects the |
281 | | // error queue. |
282 | 514 | if (inc) { |
283 | 514 | if (error->data != NULL) { |
284 | 514 | free(state->to_free); |
285 | 514 | state->to_free = error->data; |
286 | 514 | } |
287 | 514 | error->data = NULL; |
288 | 514 | } |
289 | 514 | } |
290 | 8.12k | } |
291 | | |
292 | 24.0k | if (inc) { |
293 | 8.12k | assert(!top); |
294 | 8.12k | err_clear(error); |
295 | 8.12k | state->bottom = i; |
296 | 8.12k | } |
297 | | |
298 | 24.0k | return ret; |
299 | 24.0k | } |
300 | | |
301 | 0 | uint32_t ERR_get_error(void) { |
302 | 0 | return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
303 | 0 | } |
304 | | |
305 | 0 | uint32_t ERR_get_error_line(const char **file, int *line) { |
306 | 0 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
307 | 0 | } |
308 | | |
309 | | uint32_t ERR_get_error_line_data(const char **file, int *line, |
310 | 10.2k | const char **data, int *flags) { |
311 | 10.2k | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
312 | 10.2k | } |
313 | | |
314 | 21.9k | uint32_t ERR_peek_error(void) { |
315 | 21.9k | return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
316 | 21.9k | } |
317 | | |
318 | 0 | uint32_t ERR_peek_error_line(const char **file, int *line) { |
319 | 0 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
320 | 0 | } |
321 | | |
322 | | uint32_t ERR_peek_error_line_data(const char **file, int *line, |
323 | 0 | const char **data, int *flags) { |
324 | 0 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
325 | 0 | flags); |
326 | 0 | } |
327 | | |
328 | 1.00k | uint32_t ERR_peek_last_error(void) { |
329 | 1.00k | return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
330 | 1.00k | } |
331 | | |
332 | 0 | uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
333 | 0 | return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
334 | 0 | } |
335 | | |
336 | | uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
337 | 0 | const char **data, int *flags) { |
338 | 0 | return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
339 | 0 | } |
340 | | |
341 | 208k | void ERR_clear_error(void) { |
342 | 208k | ERR_STATE *const state = err_get_state(); |
343 | 208k | unsigned i; |
344 | | |
345 | 208k | if (state == NULL) { |
346 | 0 | return; |
347 | 0 | } |
348 | | |
349 | 3.54M | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
350 | 3.33M | err_clear(&state->errors[i]); |
351 | 3.33M | } |
352 | 208k | free(state->to_free); |
353 | 208k | state->to_free = NULL; |
354 | | |
355 | 208k | state->top = state->bottom = 0; |
356 | 208k | } |
357 | | |
358 | 0 | void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
359 | 0 | if (tid != NULL) { |
360 | 0 | assert(0); |
361 | 0 | return; |
362 | 0 | } |
363 | | |
364 | 0 | ERR_clear_error(); |
365 | 0 | } |
366 | | |
367 | 0 | int ERR_get_next_error_library(void) { |
368 | 0 | int ret; |
369 | |
|
370 | 0 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex); |
371 | 0 | ret = global_next_library++; |
372 | 0 | CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex); |
373 | |
|
374 | 0 | return ret; |
375 | 0 | } |
376 | | |
377 | 0 | void ERR_remove_state(unsigned long pid) { |
378 | 0 | ERR_clear_error(); |
379 | 0 | } |
380 | | |
381 | 83.2k | void ERR_clear_system_error(void) { |
382 | 83.2k | errno = 0; |
383 | 83.2k | } |
384 | | |
385 | | // err_string_cmp is a compare function for searching error values with |
386 | | // |bsearch| in |err_string_lookup|. |
387 | 67.0k | static int err_string_cmp(const void *a, const void *b) { |
388 | 67.0k | const uint32_t a_key = *((const uint32_t*) a) >> 15; |
389 | 67.0k | const uint32_t b_key = *((const uint32_t*) b) >> 15; |
390 | | |
391 | 67.0k | if (a_key < b_key) { |
392 | 32.7k | return -1; |
393 | 34.3k | } else if (a_key > b_key) { |
394 | 26.2k | return 1; |
395 | 26.2k | } else { |
396 | 8.12k | return 0; |
397 | 8.12k | } |
398 | 67.0k | } |
399 | | |
400 | | // err_string_lookup looks up the string associated with |lib| and |key| in |
401 | | // |values| and |string_data|. It returns the string or NULL if not found. |
402 | | static const char *err_string_lookup(uint32_t lib, uint32_t key, |
403 | | const uint32_t *values, |
404 | | size_t num_values, |
405 | 8.12k | const char *string_data) { |
406 | | // |values| points to data in err_data.h, which is generated by |
407 | | // err_data_generate.go. It's an array of uint32_t values. Each value has the |
408 | | // following structure: |
409 | | // | lib | key | offset | |
410 | | // |6 bits| 11 bits | 15 bits | |
411 | | // |
412 | | // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
413 | | // The |key| is a reason code, depending on the context. |
414 | | // The |offset| is the number of bytes from the start of |string_data| where |
415 | | // the (NUL terminated) string for this value can be found. |
416 | | // |
417 | | // Values are sorted based on treating the |lib| and |key| part as an |
418 | | // unsigned integer. |
419 | 8.12k | if (lib >= (1 << 6) || key >= (1 << 11)) { |
420 | 0 | return NULL; |
421 | 0 | } |
422 | 8.12k | uint32_t search_key = lib << 26 | key << 15; |
423 | 8.12k | const uint32_t *result = bsearch(&search_key, values, num_values, |
424 | 8.12k | sizeof(uint32_t), err_string_cmp); |
425 | 8.12k | if (result == NULL) { |
426 | 0 | return NULL; |
427 | 0 | } |
428 | | |
429 | 8.12k | return &string_data[(*result) & 0x7fff]; |
430 | 8.12k | } |
431 | | |
432 | | static const char *const kLibraryNames[ERR_NUM_LIBS] = { |
433 | | "invalid library (0)", |
434 | | "unknown library", // ERR_LIB_NONE |
435 | | "system library", // ERR_LIB_SYS |
436 | | "bignum routines", // ERR_LIB_BN |
437 | | "RSA routines", // ERR_LIB_RSA |
438 | | "Diffie-Hellman routines", // ERR_LIB_DH |
439 | | "public key routines", // ERR_LIB_EVP |
440 | | "memory buffer routines", // ERR_LIB_BUF |
441 | | "object identifier routines", // ERR_LIB_OBJ |
442 | | "PEM routines", // ERR_LIB_PEM |
443 | | "DSA routines", // ERR_LIB_DSA |
444 | | "X.509 certificate routines", // ERR_LIB_X509 |
445 | | "ASN.1 encoding routines", // ERR_LIB_ASN1 |
446 | | "configuration file routines", // ERR_LIB_CONF |
447 | | "common libcrypto routines", // ERR_LIB_CRYPTO |
448 | | "elliptic curve routines", // ERR_LIB_EC |
449 | | "SSL routines", // ERR_LIB_SSL |
450 | | "BIO routines", // ERR_LIB_BIO |
451 | | "PKCS7 routines", // ERR_LIB_PKCS7 |
452 | | "PKCS8 routines", // ERR_LIB_PKCS8 |
453 | | "X509 V3 routines", // ERR_LIB_X509V3 |
454 | | "random number generator", // ERR_LIB_RAND |
455 | | "ENGINE routines", // ERR_LIB_ENGINE |
456 | | "OCSP routines", // ERR_LIB_OCSP |
457 | | "UI routines", // ERR_LIB_UI |
458 | | "COMP routines", // ERR_LIB_COMP |
459 | | "ECDSA routines", // ERR_LIB_ECDSA |
460 | | "ECDH routines", // ERR_LIB_ECDH |
461 | | "HMAC routines", // ERR_LIB_HMAC |
462 | | "Digest functions", // ERR_LIB_DIGEST |
463 | | "Cipher functions", // ERR_LIB_CIPHER |
464 | | "HKDF functions", // ERR_LIB_HKDF |
465 | | "Trust Token functions", // ERR_LIB_TRUST_TOKEN |
466 | | "User defined functions", // ERR_LIB_USER |
467 | | }; |
468 | | |
469 | 8.12k | static const char *err_lib_error_string(uint32_t packed_error) { |
470 | 8.12k | const uint32_t lib = ERR_GET_LIB(packed_error); |
471 | | |
472 | 8.12k | if (lib >= ERR_NUM_LIBS) { |
473 | 0 | return NULL; |
474 | 0 | } |
475 | 8.12k | return kLibraryNames[lib]; |
476 | 8.12k | } |
477 | | |
478 | 0 | const char *ERR_lib_error_string(uint32_t packed_error) { |
479 | 0 | const char *ret = err_lib_error_string(packed_error); |
480 | 0 | return ret == NULL ? "unknown library" : ret; |
481 | 0 | } |
482 | | |
483 | 0 | const char *ERR_func_error_string(uint32_t packed_error) { |
484 | 0 | return "OPENSSL_internal"; |
485 | 0 | } |
486 | | |
487 | 8.12k | static const char *err_reason_error_string(uint32_t packed_error) { |
488 | 8.12k | const uint32_t lib = ERR_GET_LIB(packed_error); |
489 | 8.12k | const uint32_t reason = ERR_GET_REASON(packed_error); |
490 | | |
491 | 8.12k | if (lib == ERR_LIB_SYS) { |
492 | 0 | if (reason < 127) { |
493 | 0 | return strerror(reason); |
494 | 0 | } |
495 | 0 | return NULL; |
496 | 0 | } |
497 | | |
498 | 8.12k | if (reason < ERR_NUM_LIBS) { |
499 | 3 | return kLibraryNames[reason]; |
500 | 3 | } |
501 | | |
502 | 8.12k | if (reason < 100) { |
503 | 0 | switch (reason) { |
504 | 0 | case ERR_R_MALLOC_FAILURE: |
505 | 0 | return "malloc failure"; |
506 | 0 | case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
507 | 0 | return "function should not have been called"; |
508 | 0 | case ERR_R_PASSED_NULL_PARAMETER: |
509 | 0 | return "passed a null parameter"; |
510 | 0 | case ERR_R_INTERNAL_ERROR: |
511 | 0 | return "internal error"; |
512 | 0 | case ERR_R_OVERFLOW: |
513 | 0 | return "overflow"; |
514 | 0 | default: |
515 | 0 | return NULL; |
516 | 0 | } |
517 | 0 | } |
518 | | |
519 | 8.12k | return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
520 | 8.12k | kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
521 | 8.12k | } |
522 | | |
523 | 0 | const char *ERR_reason_error_string(uint32_t packed_error) { |
524 | 0 | const char *ret = err_reason_error_string(packed_error); |
525 | 0 | return ret == NULL ? "unknown error" : ret; |
526 | 0 | } |
527 | | |
528 | 0 | char *ERR_error_string(uint32_t packed_error, char *ret) { |
529 | 0 | static char buf[ERR_ERROR_STRING_BUF_LEN]; |
530 | |
|
531 | 0 | if (ret == NULL) { |
532 | | // TODO(fork): remove this. |
533 | 0 | ret = buf; |
534 | 0 | } |
535 | |
|
536 | 0 | #if !defined(NDEBUG) |
537 | | // This is aimed to help catch callers who don't provide |
538 | | // |ERR_ERROR_STRING_BUF_LEN| bytes of space. |
539 | 0 | OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
540 | 0 | #endif |
541 | |
|
542 | 0 | return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
543 | 0 | } |
544 | | |
545 | 8.12k | char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
546 | 8.12k | if (len == 0) { |
547 | 0 | return NULL; |
548 | 0 | } |
549 | | |
550 | 8.12k | unsigned lib = ERR_GET_LIB(packed_error); |
551 | 8.12k | unsigned reason = ERR_GET_REASON(packed_error); |
552 | | |
553 | 8.12k | const char *lib_str = err_lib_error_string(packed_error); |
554 | 8.12k | const char *reason_str = err_reason_error_string(packed_error); |
555 | | |
556 | 8.12k | char lib_buf[64], reason_buf[64]; |
557 | 8.12k | if (lib_str == NULL) { |
558 | 0 | BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
559 | 0 | lib_str = lib_buf; |
560 | 0 | } |
561 | | |
562 | 8.12k | if (reason_str == NULL) { |
563 | 0 | BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
564 | 0 | reason_str = reason_buf; |
565 | 0 | } |
566 | | |
567 | 8.12k | BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
568 | 8.12k | packed_error, lib_str, reason_str); |
569 | | |
570 | 8.12k | if (strlen(buf) == len - 1) { |
571 | | // output may be truncated; make sure we always have 5 colon-separated |
572 | | // fields, i.e. 4 colons. |
573 | 0 | static const unsigned num_colons = 4; |
574 | 0 | unsigned i; |
575 | 0 | char *s = buf; |
576 | |
|
577 | 0 | if (len <= num_colons) { |
578 | | // In this situation it's not possible to ensure that the correct number |
579 | | // of colons are included in the output. |
580 | 0 | return buf; |
581 | 0 | } |
582 | | |
583 | 0 | for (i = 0; i < num_colons; i++) { |
584 | 0 | char *colon = strchr(s, ':'); |
585 | 0 | char *last_pos = &buf[len - 1] - num_colons + i; |
586 | |
|
587 | 0 | if (colon == NULL || colon > last_pos) { |
588 | | // set colon |i| at last possible position (buf[len-1] is the |
589 | | // terminating 0). If we're setting this colon, then all whole of the |
590 | | // rest of the string must be colons in order to have the correct |
591 | | // number. |
592 | 0 | OPENSSL_memset(last_pos, ':', num_colons - i); |
593 | 0 | break; |
594 | 0 | } |
595 | | |
596 | 0 | s = colon + 1; |
597 | 0 | } |
598 | 0 | } |
599 | | |
600 | 8.12k | return buf; |
601 | 8.12k | } |
602 | | |
603 | 2.13k | void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { |
604 | 2.13k | char buf[ERR_ERROR_STRING_BUF_LEN]; |
605 | 2.13k | char buf2[1024]; |
606 | 2.13k | const char *file, *data; |
607 | 2.13k | int line, flags; |
608 | 2.13k | uint32_t packed_error; |
609 | | |
610 | | // thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
611 | | // for this thread. |
612 | 2.13k | const unsigned long thread_hash = (uintptr_t) err_get_state(); |
613 | | |
614 | 10.2k | for (;;) { |
615 | 10.2k | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
616 | 10.2k | if (packed_error == 0) { |
617 | 2.13k | break; |
618 | 2.13k | } |
619 | | |
620 | 8.12k | ERR_error_string_n(packed_error, buf, sizeof(buf)); |
621 | 8.12k | BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, |
622 | 8.12k | file, line, (flags & ERR_FLAG_STRING) ? data : ""); |
623 | 8.12k | if (callback(buf2, strlen(buf2), ctx) <= 0) { |
624 | 0 | break; |
625 | 0 | } |
626 | 8.12k | } |
627 | 2.13k | } |
628 | | |
629 | 0 | static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) { |
630 | 0 | assert(msg[msg_len] == '\0'); |
631 | 0 | FILE* fp = ctx; |
632 | 0 | int res = fputs(msg, fp); |
633 | 0 | return res < 0 ? 0 : 1; |
634 | 0 | } |
635 | | |
636 | 0 | void ERR_print_errors_fp(FILE *file) { |
637 | 0 | ERR_print_errors_cb(print_errors_to_file, file); |
638 | 0 | } |
639 | | |
640 | | // err_set_error_data sets the data on the most recent error. |
641 | 47.9k | static void err_set_error_data(char *data) { |
642 | 47.9k | ERR_STATE *const state = err_get_state(); |
643 | 47.9k | struct err_error_st *error; |
644 | | |
645 | 47.9k | if (state == NULL || state->top == state->bottom) { |
646 | 0 | free(data); |
647 | 0 | return; |
648 | 0 | } |
649 | | |
650 | 47.9k | error = &state->errors[state->top]; |
651 | | |
652 | 47.9k | free(error->data); |
653 | 47.9k | error->data = data; |
654 | 47.9k | } |
655 | | |
656 | | void ERR_put_error(int library, int unused, int reason, const char *file, |
657 | 417k | unsigned line) { |
658 | 417k | ERR_STATE *const state = err_get_state(); |
659 | 417k | struct err_error_st *error; |
660 | | |
661 | 417k | if (state == NULL) { |
662 | 0 | return; |
663 | 0 | } |
664 | | |
665 | 417k | if (library == ERR_LIB_SYS && reason == 0) { |
666 | | #if defined(OPENSSL_WINDOWS) |
667 | | reason = GetLastError(); |
668 | | #else |
669 | 0 | reason = errno; |
670 | 0 | #endif |
671 | 0 | } |
672 | | |
673 | 417k | state->top = (state->top + 1) % ERR_NUM_ERRORS; |
674 | 417k | if (state->top == state->bottom) { |
675 | 211k | state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
676 | 211k | } |
677 | | |
678 | 417k | error = &state->errors[state->top]; |
679 | 417k | err_clear(error); |
680 | 417k | error->file = file; |
681 | 417k | error->line = line; |
682 | 417k | error->packed = ERR_PACK(library, reason); |
683 | 417k | } |
684 | | |
685 | | // ERR_add_error_data_vdata takes a variable number of const char* pointers, |
686 | | // concatenates them and sets the result as the data on the most recent |
687 | | // error. |
688 | 34.2k | static void err_add_error_vdata(unsigned num, va_list args) { |
689 | 34.2k | size_t total_size = 0; |
690 | 34.2k | const char *substr; |
691 | 34.2k | char *buf; |
692 | | |
693 | 34.2k | va_list args_copy; |
694 | 34.2k | va_copy(args_copy, args); |
695 | 154k | for (size_t i = 0; i < num; i++) { |
696 | 119k | substr = va_arg(args_copy, const char *); |
697 | 119k | if (substr == NULL) { |
698 | 2.48k | continue; |
699 | 2.48k | } |
700 | 117k | size_t substr_len = strlen(substr); |
701 | 117k | if (SIZE_MAX - total_size < substr_len) { |
702 | 0 | return; // Would overflow. |
703 | 0 | } |
704 | 117k | total_size += substr_len; |
705 | 117k | } |
706 | 34.2k | va_end(args_copy); |
707 | 34.2k | if (total_size == SIZE_MAX) { |
708 | 0 | return; // Would overflow. |
709 | 0 | } |
710 | 34.2k | total_size += 1; // NUL terminator. |
711 | 34.2k | if ((buf = malloc(total_size)) == NULL) { |
712 | 0 | return; |
713 | 0 | } |
714 | 34.2k | buf[0] = '\0'; |
715 | 154k | for (size_t i = 0; i < num; i++) { |
716 | 119k | substr = va_arg(args, const char *); |
717 | 119k | if (substr == NULL) { |
718 | 2.48k | continue; |
719 | 2.48k | } |
720 | 117k | if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) { |
721 | 0 | assert(0); // should not be possible. |
722 | 0 | } |
723 | 117k | } |
724 | 34.2k | va_end(args); |
725 | 34.2k | err_set_error_data(buf); |
726 | 34.2k | } |
727 | | |
728 | 34.2k | void ERR_add_error_data(unsigned count, ...) { |
729 | 34.2k | va_list args; |
730 | 34.2k | va_start(args, count); |
731 | 34.2k | err_add_error_vdata(count, args); |
732 | 34.2k | va_end(args); |
733 | 34.2k | } |
734 | | |
735 | 13.7k | void ERR_add_error_dataf(const char *format, ...) { |
736 | 13.7k | char *buf = NULL; |
737 | 13.7k | va_list ap; |
738 | | |
739 | 13.7k | va_start(ap, format); |
740 | 13.7k | if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) { |
741 | 0 | return; |
742 | 0 | } |
743 | 13.7k | va_end(ap); |
744 | | |
745 | 13.7k | err_set_error_data(buf); |
746 | 13.7k | } |
747 | | |
748 | 0 | void ERR_set_error_data(char *data, int flags) { |
749 | 0 | if (!(flags & ERR_FLAG_STRING)) { |
750 | | // We do not support non-string error data. |
751 | 0 | assert(0); |
752 | 0 | return; |
753 | 0 | } |
754 | | // Disable deprecated functions on msvc so it doesn't complain about strdup. |
755 | 0 | OPENSSL_MSVC_PRAGMA(warning(push)) |
756 | 0 | OPENSSL_MSVC_PRAGMA(warning(disable : 4996)) |
757 | | // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
758 | | // which can affect the error stack. |
759 | 0 | char *copy = strdup(data); |
760 | 0 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
761 | 0 | if (copy != NULL) { |
762 | 0 | err_set_error_data(copy); |
763 | 0 | } |
764 | 0 | if (flags & ERR_FLAG_MALLOCED) { |
765 | | // We can not take ownership of |data| directly because it is allocated with |
766 | | // |OPENSSL_malloc| and we will free it with system |free| later. |
767 | 0 | OPENSSL_free(data); |
768 | 0 | } |
769 | 0 | } |
770 | | |
771 | 0 | int ERR_set_mark(void) { |
772 | 0 | ERR_STATE *const state = err_get_state(); |
773 | |
|
774 | 0 | if (state == NULL || state->bottom == state->top) { |
775 | 0 | return 0; |
776 | 0 | } |
777 | 0 | state->errors[state->top].mark = 1; |
778 | 0 | return 1; |
779 | 0 | } |
780 | | |
781 | 0 | int ERR_pop_to_mark(void) { |
782 | 0 | ERR_STATE *const state = err_get_state(); |
783 | |
|
784 | 0 | if (state == NULL) { |
785 | 0 | return 0; |
786 | 0 | } |
787 | | |
788 | 0 | while (state->bottom != state->top) { |
789 | 0 | struct err_error_st *error = &state->errors[state->top]; |
790 | |
|
791 | 0 | if (error->mark) { |
792 | 0 | error->mark = 0; |
793 | 0 | return 1; |
794 | 0 | } |
795 | | |
796 | 0 | err_clear(error); |
797 | 0 | if (state->top == 0) { |
798 | 0 | state->top = ERR_NUM_ERRORS - 1; |
799 | 0 | } else { |
800 | 0 | state->top--; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | 0 | return 0; |
805 | 0 | } |
806 | | |
807 | 0 | void ERR_load_crypto_strings(void) {} |
808 | | |
809 | 0 | void ERR_free_strings(void) {} |
810 | | |
811 | 0 | void ERR_load_BIO_strings(void) {} |
812 | | |
813 | 0 | void ERR_load_ERR_strings(void) {} |
814 | | |
815 | 0 | void ERR_load_RAND_strings(void) {} |
816 | | |
817 | | struct err_save_state_st { |
818 | | struct err_error_st *errors; |
819 | | size_t num_errors; |
820 | | }; |
821 | | |
822 | 16.5k | void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) { |
823 | 16.5k | if (state == NULL) { |
824 | 0 | return; |
825 | 0 | } |
826 | 48.7k | for (size_t i = 0; i < state->num_errors; i++) { |
827 | 32.2k | err_clear(&state->errors[i]); |
828 | 32.2k | } |
829 | 16.5k | free(state->errors); |
830 | 16.5k | free(state); |
831 | 16.5k | } |
832 | | |
833 | 17.0k | ERR_SAVE_STATE *ERR_save_state(void) { |
834 | 17.0k | ERR_STATE *const state = err_get_state(); |
835 | 17.0k | if (state == NULL || state->top == state->bottom) { |
836 | 493 | return NULL; |
837 | 493 | } |
838 | | |
839 | 16.5k | ERR_SAVE_STATE *ret = malloc(sizeof(ERR_SAVE_STATE)); |
840 | 16.5k | if (ret == NULL) { |
841 | 0 | return NULL; |
842 | 0 | } |
843 | | |
844 | | // Errors are stored in the range (bottom, top]. |
845 | 16.5k | size_t num_errors = state->top >= state->bottom |
846 | 16.5k | ? state->top - state->bottom |
847 | 16.5k | : ERR_NUM_ERRORS + state->top - state->bottom; |
848 | 16.5k | assert(num_errors < ERR_NUM_ERRORS); |
849 | 16.5k | ret->errors = malloc(num_errors * sizeof(struct err_error_st)); |
850 | 16.5k | if (ret->errors == NULL) { |
851 | 0 | free(ret); |
852 | 0 | return NULL; |
853 | 0 | } |
854 | 16.5k | OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st)); |
855 | 16.5k | ret->num_errors = num_errors; |
856 | | |
857 | 48.7k | for (size_t i = 0; i < num_errors; i++) { |
858 | 32.2k | size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS; |
859 | 32.2k | err_copy(&ret->errors[i], &state->errors[j]); |
860 | 32.2k | } |
861 | 16.5k | return ret; |
862 | 16.5k | } |
863 | | |
864 | 8.43k | void ERR_restore_state(const ERR_SAVE_STATE *state) { |
865 | 8.43k | if (state == NULL || state->num_errors == 0) { |
866 | 443 | ERR_clear_error(); |
867 | 443 | return; |
868 | 443 | } |
869 | | |
870 | 7.99k | ERR_STATE *const dst = err_get_state(); |
871 | 7.99k | if (dst == NULL) { |
872 | 0 | return; |
873 | 0 | } |
874 | | |
875 | 23.6k | for (size_t i = 0; i < state->num_errors; i++) { |
876 | 15.6k | err_copy(&dst->errors[i], &state->errors[i]); |
877 | 15.6k | } |
878 | 7.99k | dst->top = state->num_errors - 1; |
879 | 7.99k | dst->bottom = ERR_NUM_ERRORS - 1; |
880 | 7.99k | } |