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)
121OPENSSL_MSVC_PRAGMA(warning(push, 3))
122#include <windows.h>
123OPENSSL_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
133struct 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.
148typedef 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
163extern const uint32_t kOpenSSLReasonValues[];
164extern const size_t kOpenSSLReasonValuesLen;
165extern const char kOpenSSLReasonStringData[];
166
167// err_clear clears the given queued error.
168static void err_clear(struct err_error_st *error) {
169 free(ptr: error->data);
170 OPENSSL_memset(dst: error, c: 0, n: sizeof(struct err_error_st));
171}
172
173static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
174 err_clear(error: dst);
175 dst->file = src->file;
176 if (src->data != NULL) {
177 // Disable deprecated functions on msvc so it doesn't complain about strdup.
178 OPENSSL_MSVC_PRAGMA(warning(push))
179 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 dst->data = strdup(s: src->data);
183 OPENSSL_MSVC_PRAGMA(warning(pop))
184 }
185 dst->packed = src->packed;
186 dst->line = src->line;
187}
188
189
190// global_next_library contains the next custom library value to return.
191static int global_next_library = ERR_NUM_LIBS;
192
193// global_next_library_mutex protects |global_next_library| from concurrent
194// updates.
195static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
196 CRYPTO_STATIC_MUTEX_INIT;
197
198static void err_state_free(void *statep) {
199 ERR_STATE *state = statep;
200
201 if (state == NULL) {
202 return;
203 }
204
205 for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
206 err_clear(error: &state->errors[i]);
207 }
208 free(ptr: state->to_free);
209 free(ptr: state);
210}
211
212// err_get_state gets the ERR_STATE object for the current thread.
213static ERR_STATE *err_get_state(void) {
214 ERR_STATE *state = CRYPTO_get_thread_local(value: OPENSSL_THREAD_LOCAL_ERR);
215 if (state == NULL) {
216 state = malloc(size: sizeof(ERR_STATE));
217 if (state == NULL) {
218 return NULL;
219 }
220 OPENSSL_memset(dst: state, c: 0, n: sizeof(ERR_STATE));
221 if (!CRYPTO_set_thread_local(index: OPENSSL_THREAD_LOCAL_ERR, value: state,
222 destructor: err_state_free)) {
223 return NULL;
224 }
225 }
226
227 return state;
228}
229
230static uint32_t get_error_values(int inc, int top, const char **file, int *line,
231 const char **data, int *flags) {
232 unsigned i = 0;
233 ERR_STATE *state;
234 struct err_error_st *error;
235 uint32_t ret;
236
237 state = err_get_state();
238 if (state == NULL || state->bottom == state->top) {
239 return 0;
240 }
241
242 if (top) {
243 assert(!inc);
244 // last error
245 i = state->top;
246 } else {
247 i = (state->bottom + 1) % ERR_NUM_ERRORS;
248 }
249
250 error = &state->errors[i];
251 ret = error->packed;
252
253 if (file != NULL && line != NULL) {
254 if (error->file == NULL) {
255 *file = "NA";
256 *line = 0;
257 } else {
258 *file = error->file;
259 *line = error->line;
260 }
261 }
262
263 if (data != NULL) {
264 if (error->data == NULL) {
265 *data = "";
266 if (flags != NULL) {
267 *flags = 0;
268 }
269 } else {
270 *data = error->data;
271 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 *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
276 }
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 if (inc) {
283 if (error->data != NULL) {
284 free(ptr: state->to_free);
285 state->to_free = error->data;
286 }
287 error->data = NULL;
288 }
289 }
290 }
291
292 if (inc) {
293 assert(!top);
294 err_clear(error);
295 state->bottom = i;
296 }
297
298 return ret;
299}
300
301uint32_t ERR_get_error(void) {
302 return get_error_values(inc: 1 /* inc */, top: 0 /* bottom */, NULL, NULL, NULL, NULL);
303}
304
305uint32_t ERR_get_error_line(const char **file, int *line) {
306 return get_error_values(inc: 1 /* inc */, top: 0 /* bottom */, file, line, NULL, NULL);
307}
308
309uint32_t ERR_get_error_line_data(const char **file, int *line,
310 const char **data, int *flags) {
311 return get_error_values(inc: 1 /* inc */, top: 0 /* bottom */, file, line, data, flags);
312}
313
314uint32_t ERR_peek_error(void) {
315 return get_error_values(inc: 0 /* peek */, top: 0 /* bottom */, NULL, NULL, NULL, NULL);
316}
317
318uint32_t ERR_peek_error_line(const char **file, int *line) {
319 return get_error_values(inc: 0 /* peek */, top: 0 /* bottom */, file, line, NULL, NULL);
320}
321
322uint32_t ERR_peek_error_line_data(const char **file, int *line,
323 const char **data, int *flags) {
324 return get_error_values(inc: 0 /* peek */, top: 0 /* bottom */, file, line, data,
325 flags);
326}
327
328uint32_t ERR_peek_last_error(void) {
329 return get_error_values(inc: 0 /* peek */, top: 1 /* top */, NULL, NULL, NULL, NULL);
330}
331
332uint32_t ERR_peek_last_error_line(const char **file, int *line) {
333 return get_error_values(inc: 0 /* peek */, top: 1 /* top */, file, line, NULL, NULL);
334}
335
336uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
337 const char **data, int *flags) {
338 return get_error_values(inc: 0 /* peek */, top: 1 /* top */, file, line, data, flags);
339}
340
341void ERR_clear_error(void) {
342 ERR_STATE *const state = err_get_state();
343 unsigned i;
344
345 if (state == NULL) {
346 return;
347 }
348
349 for (i = 0; i < ERR_NUM_ERRORS; i++) {
350 err_clear(error: &state->errors[i]);
351 }
352 free(ptr: state->to_free);
353 state->to_free = NULL;
354
355 state->top = state->bottom = 0;
356}
357
358void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
359 if (tid != NULL) {
360 assert(0);
361 return;
362 }
363
364 ERR_clear_error();
365}
366
367int ERR_get_next_error_library(void) {
368 int ret;
369
370 CRYPTO_STATIC_MUTEX_lock_write(lock: &global_next_library_mutex);
371 ret = global_next_library++;
372 CRYPTO_STATIC_MUTEX_unlock_write(lock: &global_next_library_mutex);
373
374 return ret;
375}
376
377void ERR_remove_state(unsigned long pid) {
378 ERR_clear_error();
379}
380
381void ERR_clear_system_error(void) {
382 errno = 0;
383}
384
385// err_string_cmp is a compare function for searching error values with
386// |bsearch| in |err_string_lookup|.
387static int err_string_cmp(const void *a, const void *b) {
388 const uint32_t a_key = *((const uint32_t*) a) >> 15;
389 const uint32_t b_key = *((const uint32_t*) b) >> 15;
390
391 if (a_key < b_key) {
392 return -1;
393 } else if (a_key > b_key) {
394 return 1;
395 } else {
396 return 0;
397 }
398}
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.
402static const char *err_string_lookup(uint32_t lib, uint32_t key,
403 const uint32_t *values,
404 size_t num_values,
405 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 if (lib >= (1 << 6) || key >= (1 << 11)) {
420 return NULL;
421 }
422 uint32_t search_key = lib << 26 | key << 15;
423 const uint32_t *result = bsearch(key: &search_key, base: values, nmemb: num_values,
424 size: sizeof(uint32_t), compar: err_string_cmp);
425 if (result == NULL) {
426 return NULL;
427 }
428
429 return &string_data[(*result) & 0x7fff];
430}
431
432static 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
469static const char *err_lib_error_string(uint32_t packed_error) {
470 const uint32_t lib = ERR_GET_LIB(packed_error);
471
472 if (lib >= ERR_NUM_LIBS) {
473 return NULL;
474 }
475 return kLibraryNames[lib];
476}
477
478const char *ERR_lib_error_string(uint32_t packed_error) {
479 const char *ret = err_lib_error_string(packed_error);
480 return ret == NULL ? "unknown library" : ret;
481}
482
483const char *ERR_func_error_string(uint32_t packed_error) {
484 return "OPENSSL_internal";
485}
486
487static const char *err_reason_error_string(uint32_t packed_error) {
488 const uint32_t lib = ERR_GET_LIB(packed_error);
489 const uint32_t reason = ERR_GET_REASON(packed_error);
490
491 if (lib == ERR_LIB_SYS) {
492 if (reason < 127) {
493 return strerror(errnum: reason);
494 }
495 return NULL;
496 }
497
498 if (reason < ERR_NUM_LIBS) {
499 return kLibraryNames[reason];
500 }
501
502 if (reason < 100) {
503 switch (reason) {
504 case ERR_R_MALLOC_FAILURE:
505 return "malloc failure";
506 case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
507 return "function should not have been called";
508 case ERR_R_PASSED_NULL_PARAMETER:
509 return "passed a null parameter";
510 case ERR_R_INTERNAL_ERROR:
511 return "internal error";
512 case ERR_R_OVERFLOW:
513 return "overflow";
514 default:
515 return NULL;
516 }
517 }
518
519 return err_string_lookup(lib, key: reason, values: kOpenSSLReasonValues,
520 num_values: kOpenSSLReasonValuesLen, string_data: kOpenSSLReasonStringData);
521}
522
523const char *ERR_reason_error_string(uint32_t packed_error) {
524 const char *ret = err_reason_error_string(packed_error);
525 return ret == NULL ? "unknown error" : ret;
526}
527
528char *ERR_error_string(uint32_t packed_error, char *ret) {
529 static char buf[ERR_ERROR_STRING_BUF_LEN];
530
531 if (ret == NULL) {
532 // TODO(fork): remove this.
533 ret = buf;
534 }
535
536#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 OPENSSL_memset(dst: ret, c: 0, ERR_ERROR_STRING_BUF_LEN);
540#endif
541
542 return ERR_error_string_n(packed_error, buf: ret, ERR_ERROR_STRING_BUF_LEN);
543}
544
545char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
546 if (len == 0) {
547 return NULL;
548 }
549
550 unsigned lib = ERR_GET_LIB(packed_error);
551 unsigned reason = ERR_GET_REASON(packed_error);
552
553 const char *lib_str = err_lib_error_string(packed_error);
554 const char *reason_str = err_reason_error_string(packed_error);
555
556 char lib_buf[64], reason_buf[64];
557 if (lib_str == NULL) {
558 BIO_snprintf(buf: lib_buf, n: sizeof(lib_buf), format: "lib(%u)", lib);
559 lib_str = lib_buf;
560 }
561
562 if (reason_str == NULL) {
563 BIO_snprintf(buf: reason_buf, n: sizeof(reason_buf), format: "reason(%u)", reason);
564 reason_str = reason_buf;
565 }
566
567 BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
568 packed_error, lib_str, reason_str);
569
570 if (strlen(s: buf) == len - 1) {
571 // output may be truncated; make sure we always have 5 colon-separated
572 // fields, i.e. 4 colons.
573 static const unsigned num_colons = 4;
574 unsigned i;
575 char *s = buf;
576
577 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 return buf;
581 }
582
583 for (i = 0; i < num_colons; i++) {
584 char *colon = strchr(s: s, c: ':');
585 char *last_pos = &buf[len - 1] - num_colons + i;
586
587 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 OPENSSL_memset(dst: last_pos, c: ':', n: num_colons - i);
593 break;
594 }
595
596 s = colon + 1;
597 }
598 }
599
600 return buf;
601}
602
603void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
604 char buf[ERR_ERROR_STRING_BUF_LEN];
605 char buf2[1024];
606 const char *file, *data;
607 int line, flags;
608 uint32_t packed_error;
609
610 // thread_hash is the least-significant bits of the |ERR_STATE| pointer value
611 // for this thread.
612 const unsigned long thread_hash = (uintptr_t) err_get_state();
613
614 for (;;) {
615 packed_error = ERR_get_error_line_data(file: &file, line: &line, data: &data, flags: &flags);
616 if (packed_error == 0) {
617 break;
618 }
619
620 ERR_error_string_n(packed_error, buf, len: sizeof(buf));
621 BIO_snprintf(buf: buf2, n: sizeof(buf2), format: "%lu:%s:%s:%d:%s\n", thread_hash, buf,
622 file, line, (flags & ERR_FLAG_STRING) ? data : "");
623 if (callback(buf2, strlen(s: buf2), ctx) <= 0) {
624 break;
625 }
626 }
627}
628
629static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
630 assert(msg[msg_len] == '\0');
631 FILE* fp = ctx;
632 int res = fputs(s: msg, stream: fp);
633 return res < 0 ? 0 : 1;
634}
635
636void ERR_print_errors_fp(FILE *file) {
637 ERR_print_errors_cb(callback: print_errors_to_file, ctx: file);
638}
639
640// err_set_error_data sets the data on the most recent error.
641static void err_set_error_data(char *data) {
642 ERR_STATE *const state = err_get_state();
643 struct err_error_st *error;
644
645 if (state == NULL || state->top == state->bottom) {
646 free(ptr: data);
647 return;
648 }
649
650 error = &state->errors[state->top];
651
652 free(ptr: error->data);
653 error->data = data;
654}
655
656void ERR_put_error(int library, int unused, int reason, const char *file,
657 unsigned line) {
658 ERR_STATE *const state = err_get_state();
659 struct err_error_st *error;
660
661 if (state == NULL) {
662 return;
663 }
664
665 if (library == ERR_LIB_SYS && reason == 0) {
666#if defined(OPENSSL_WINDOWS)
667 reason = GetLastError();
668#else
669 reason = errno;
670#endif
671 }
672
673 state->top = (state->top + 1) % ERR_NUM_ERRORS;
674 if (state->top == state->bottom) {
675 state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
676 }
677
678 error = &state->errors[state->top];
679 err_clear(error);
680 error->file = file;
681 error->line = line;
682 error->packed = ERR_PACK(library, reason);
683}
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.
688static void err_add_error_vdata(unsigned num, va_list args) {
689 size_t total_size = 0;
690 const char *substr;
691 char *buf;
692
693 va_list args_copy;
694 va_copy(args_copy, args);
695 for (size_t i = 0; i < num; i++) {
696 substr = va_arg(args_copy, const char *);
697 if (substr == NULL) {
698 continue;
699 }
700 size_t substr_len = strlen(s: substr);
701 if (SIZE_MAX - total_size < substr_len) {
702 return; // Would overflow.
703 }
704 total_size += substr_len;
705 }
706 va_end(args_copy);
707 if (total_size == SIZE_MAX) {
708 return; // Would overflow.
709 }
710 total_size += 1; // NUL terminator.
711 if ((buf = malloc(size: total_size)) == NULL) {
712 return;
713 }
714 buf[0] = '\0';
715 for (size_t i = 0; i < num; i++) {
716 substr = va_arg(args, const char *);
717 if (substr == NULL) {
718 continue;
719 }
720 if (OPENSSL_strlcat(dst: buf, src: substr, dst_size: total_size) >= total_size) {
721 assert(0); // should not be possible.
722 }
723 }
724 va_end(args);
725 err_set_error_data(data: buf);
726}
727
728void ERR_add_error_data(unsigned count, ...) {
729 va_list args;
730 va_start(args, count);
731 err_add_error_vdata(num: count, args);
732 va_end(args);
733}
734
735void ERR_add_error_dataf(const char *format, ...) {
736 char *buf = NULL;
737 va_list ap;
738
739 va_start(ap, format);
740 if (OPENSSL_vasprintf_internal(str: &buf, format, args: ap, /*system_malloc=*/1) == -1) {
741 return;
742 }
743 va_end(ap);
744
745 err_set_error_data(data: buf);
746}
747
748void ERR_set_error_data(char *data, int flags) {
749 if (!(flags & ERR_FLAG_STRING)) {
750 // We do not support non-string error data.
751 assert(0);
752 return;
753 }
754 // Disable deprecated functions on msvc so it doesn't complain about strdup.
755 OPENSSL_MSVC_PRAGMA(warning(push))
756 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 char *copy = strdup(s: data);
760 OPENSSL_MSVC_PRAGMA(warning(pop))
761 if (copy != NULL) {
762 err_set_error_data(data: copy);
763 }
764 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 OPENSSL_free(ptr: data);
768 }
769}
770
771int ERR_set_mark(void) {
772 ERR_STATE *const state = err_get_state();
773
774 if (state == NULL || state->bottom == state->top) {
775 return 0;
776 }
777 state->errors[state->top].mark = 1;
778 return 1;
779}
780
781int ERR_pop_to_mark(void) {
782 ERR_STATE *const state = err_get_state();
783
784 if (state == NULL) {
785 return 0;
786 }
787
788 while (state->bottom != state->top) {
789 struct err_error_st *error = &state->errors[state->top];
790
791 if (error->mark) {
792 error->mark = 0;
793 return 1;
794 }
795
796 err_clear(error);
797 if (state->top == 0) {
798 state->top = ERR_NUM_ERRORS - 1;
799 } else {
800 state->top--;
801 }
802 }
803
804 return 0;
805}
806
807void ERR_load_crypto_strings(void) {}
808
809void ERR_free_strings(void) {}
810
811void ERR_load_BIO_strings(void) {}
812
813void ERR_load_ERR_strings(void) {}
814
815void ERR_load_RAND_strings(void) {}
816
817struct err_save_state_st {
818 struct err_error_st *errors;
819 size_t num_errors;
820};
821
822void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
823 if (state == NULL) {
824 return;
825 }
826 for (size_t i = 0; i < state->num_errors; i++) {
827 err_clear(error: &state->errors[i]);
828 }
829 free(ptr: state->errors);
830 free(ptr: state);
831}
832
833ERR_SAVE_STATE *ERR_save_state(void) {
834 ERR_STATE *const state = err_get_state();
835 if (state == NULL || state->top == state->bottom) {
836 return NULL;
837 }
838
839 ERR_SAVE_STATE *ret = malloc(size: sizeof(ERR_SAVE_STATE));
840 if (ret == NULL) {
841 return NULL;
842 }
843
844 // Errors are stored in the range (bottom, top].
845 size_t num_errors = state->top >= state->bottom
846 ? state->top - state->bottom
847 : ERR_NUM_ERRORS + state->top - state->bottom;
848 assert(num_errors < ERR_NUM_ERRORS);
849 ret->errors = malloc(size: num_errors * sizeof(struct err_error_st));
850 if (ret->errors == NULL) {
851 free(ptr: ret);
852 return NULL;
853 }
854 OPENSSL_memset(dst: ret->errors, c: 0, n: num_errors * sizeof(struct err_error_st));
855 ret->num_errors = num_errors;
856
857 for (size_t i = 0; i < num_errors; i++) {
858 size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
859 err_copy(dst: &ret->errors[i], src: &state->errors[j]);
860 }
861 return ret;
862}
863
864void ERR_restore_state(const ERR_SAVE_STATE *state) {
865 if (state == NULL || state->num_errors == 0) {
866 ERR_clear_error();
867 return;
868 }
869
870 ERR_STATE *const dst = err_get_state();
871 if (dst == NULL) {
872 return;
873 }
874
875 for (size_t i = 0; i < state->num_errors; i++) {
876 err_copy(dst: &dst->errors[i], src: &state->errors[i]);
877 }
878 dst->top = state->num_errors - 1;
879 dst->bottom = ERR_NUM_ERRORS - 1;
880}
881

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com

source code of flutter_engine/third_party/boringssl/src/crypto/err/err.c