2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #define OSSL_FORCE_ERR_STATE
15 #include "crypto/cryptlib.h"
16 #include "internal/err.h"
17 #include "crypto/err.h"
18 #include <openssl/err.h>
19 #include <openssl/crypto.h>
20 #include <openssl/buffer.h>
21 #include <openssl/bio.h>
22 #include <openssl/opensslconf.h>
23 #include "internal/thread_once.h"
24 #include "crypto/ctype.h"
25 #include "internal/constant_time.h"
27 #include "err_local.h"
29 /* Forward declaration in case it's not published because of configuration */
30 ERR_STATE *ERR_get_state(void);
32 static int err_load_strings(const ERR_STRING_DATA *str);
34 static void ERR_STATE_free(ERR_STATE *s);
35 #ifndef OPENSSL_NO_ERR
36 static ERR_STRING_DATA ERR_str_libraries[] = {
37 {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
38 {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
39 {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
40 {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
41 {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
42 {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
43 {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
44 {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
45 {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
46 {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
47 {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
48 {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
49 {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
50 {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
51 {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
52 {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
53 {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
54 {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
55 {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
56 {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
57 {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
58 {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
59 {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
60 {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
61 {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
62 {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
63 {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
64 {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
65 {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
66 {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
67 {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"},
68 {ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines"},
69 {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
70 {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
71 {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
72 {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
73 {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
74 {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
75 {ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines"},
76 {ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines"},
77 {ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines"},
78 {ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines"},
79 {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"},
84 * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in
85 * are listed. For maintainability, please keep all reasons in the same order.
87 static ERR_STRING_DATA ERR_str_reasons[] = {
88 {ERR_R_SYS_LIB, "system lib"},
89 {ERR_R_BN_LIB, "BN lib"},
90 {ERR_R_RSA_LIB, "RSA lib"},
91 {ERR_R_DH_LIB, "DH lib"},
92 {ERR_R_EVP_LIB, "EVP lib"},
93 {ERR_R_BUF_LIB, "BUF lib"},
94 {ERR_R_OBJ_LIB, "OBJ lib"},
95 {ERR_R_PEM_LIB, "PEM lib"},
96 {ERR_R_DSA_LIB, "DSA lib"},
97 {ERR_R_X509_LIB, "X509 lib"},
98 {ERR_R_ASN1_LIB, "ASN1 lib"},
99 {ERR_R_CRYPTO_LIB, "CRYPTO lib"},
100 {ERR_R_EC_LIB, "EC lib"},
101 {ERR_R_BIO_LIB, "BIO lib"},
102 {ERR_R_PKCS7_LIB, "PKCS7 lib"},
103 {ERR_R_X509V3_LIB, "X509V3 lib"},
104 {ERR_R_ENGINE_LIB, "ENGINE lib"},
105 {ERR_R_UI_LIB, "UI lib"},
106 {ERR_R_ECDSA_LIB, "ECDSA lib"},
107 {ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib"},
108 {ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib"},
110 {ERR_R_FATAL, "fatal"},
111 {ERR_R_MALLOC_FAILURE, "malloc failure"},
112 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
113 "called a function you should not call"},
114 {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
115 {ERR_R_INTERNAL_ERROR, "internal error"},
116 {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
117 {ERR_R_INIT_FAIL, "init fail"},
118 {ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument"},
119 {ERR_R_OPERATION_FAIL, "operation fail"},
120 {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
121 {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
122 {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
123 {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
125 * Something is unsupported, exactly what is expressed with additional data
127 {ERR_R_UNSUPPORTED, "unsupported"},
129 * A fetch failed for other reasons than the name to be fetched being
132 {ERR_R_FETCH_FAILED, "fetch failed"},
133 {ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition"},
134 {ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock"},
135 {ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock"},
140 static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
141 static int set_err_thread_local;
142 static CRYPTO_THREAD_LOCAL err_thread_local;
144 static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
145 static CRYPTO_RWLOCK *err_string_lock;
147 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
153 static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
154 static int int_err_library_number = ERR_LIB_USER;
156 typedef enum ERR_GET_ACTION_e {
157 EV_POP, EV_PEEK, EV_PEEK_LAST
160 static unsigned long get_error_values(ERR_GET_ACTION g,
161 const char **file, int *line,
162 const char **func, const char **data,
165 static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
167 unsigned long ret, l;
170 ret = l ^ ERR_GET_LIB(l);
171 return (ret ^ ret % 19 * 13);
174 static int err_string_data_cmp(const ERR_STRING_DATA *a,
175 const ERR_STRING_DATA *b)
177 if (a->error == b->error)
179 return a->error > b->error ? 1 : -1;
182 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
184 ERR_STRING_DATA *p = NULL;
186 if (!CRYPTO_THREAD_read_lock(err_string_lock))
188 p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
189 CRYPTO_THREAD_unlock(err_string_lock);
194 static void ERR_STATE_free(ERR_STATE *s)
200 for (i = 0; i < ERR_NUM_ERRORS; i++) {
206 DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
208 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
210 err_string_lock = CRYPTO_THREAD_lock_new();
211 if (err_string_lock == NULL)
213 int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
214 err_string_data_cmp);
215 if (int_error_hash == NULL) {
216 CRYPTO_THREAD_lock_free(err_string_lock);
217 err_string_lock = NULL;
223 void err_cleanup(void)
225 if (set_err_thread_local != 0)
226 CRYPTO_THREAD_cleanup_local(&err_thread_local);
227 CRYPTO_THREAD_lock_free(err_string_lock);
228 err_string_lock = NULL;
229 lh_ERR_STRING_DATA_free(int_error_hash);
230 int_error_hash = NULL;
234 * Legacy; pack in the library.
236 static void err_patch(int lib, ERR_STRING_DATA *str)
238 unsigned long plib = ERR_PACK(lib, 0, 0);
240 for (; str->error != 0; str++)
245 * Hash in |str| error strings. Assumes the RUN_ONCE was done.
247 static int err_load_strings(const ERR_STRING_DATA *str)
249 if (!CRYPTO_THREAD_write_lock(err_string_lock))
251 for (; str->error; str++)
252 (void)lh_ERR_STRING_DATA_insert(int_error_hash,
253 (ERR_STRING_DATA *)str);
254 CRYPTO_THREAD_unlock(err_string_lock);
258 int ossl_err_load_ERR_strings(void)
260 #ifndef OPENSSL_NO_ERR
261 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
264 err_load_strings(ERR_str_libraries);
265 err_load_strings(ERR_str_reasons);
270 int ERR_load_strings(int lib, ERR_STRING_DATA *str)
272 if (ossl_err_load_ERR_strings() == 0)
276 err_load_strings(str);
280 int ERR_load_strings_const(const ERR_STRING_DATA *str)
282 if (ossl_err_load_ERR_strings() == 0)
284 err_load_strings(str);
288 int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
290 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
293 if (!CRYPTO_THREAD_write_lock(err_string_lock))
296 * We don't need to ERR_PACK the lib, since that was done (to
297 * the table) when it was loaded.
299 for (; str->error; str++)
300 (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
301 CRYPTO_THREAD_unlock(err_string_lock);
306 void err_free_strings_int(void)
308 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
312 /********************************************************/
314 void ERR_clear_error(void)
319 es = ossl_err_get_state_int();
323 for (i = 0; i < ERR_NUM_ERRORS; i++) {
326 es->top = es->bottom = 0;
329 unsigned long ERR_get_error(void)
331 return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
334 unsigned long ERR_get_error_all(const char **file, int *line,
336 const char **data, int *flags)
338 return get_error_values(EV_POP, file, line, func, data, flags);
341 #ifndef OPENSSL_NO_DEPRECATED_3_0
342 unsigned long ERR_get_error_line(const char **file, int *line)
344 return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
347 unsigned long ERR_get_error_line_data(const char **file, int *line,
348 const char **data, int *flags)
350 return get_error_values(EV_POP, file, line, NULL, data, flags);
354 unsigned long ERR_peek_error(void)
356 return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
359 unsigned long ERR_peek_error_line(const char **file, int *line)
361 return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
364 unsigned long ERR_peek_error_func(const char **func)
366 return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
369 unsigned long ERR_peek_error_data(const char **data, int *flags)
371 return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
374 unsigned long ERR_peek_error_all(const char **file, int *line,
376 const char **data, int *flags)
378 return get_error_values(EV_PEEK, file, line, func, data, flags);
381 #ifndef OPENSSL_NO_DEPRECATED_3_0
382 unsigned long ERR_peek_error_line_data(const char **file, int *line,
383 const char **data, int *flags)
385 return get_error_values(EV_PEEK, file, line, NULL, data, flags);
389 unsigned long ERR_peek_last_error(void)
391 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
394 unsigned long ERR_peek_last_error_line(const char **file, int *line)
396 return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
399 unsigned long ERR_peek_last_error_func(const char **func)
401 return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
404 unsigned long ERR_peek_last_error_data(const char **data, int *flags)
406 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
409 unsigned long ERR_peek_last_error_all(const char **file, int *line,
411 const char **data, int *flags)
413 return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
416 #ifndef OPENSSL_NO_DEPRECATED_3_0
417 unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
418 const char **data, int *flags)
420 return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
424 static unsigned long get_error_values(ERR_GET_ACTION g,
425 const char **file, int *line,
427 const char **data, int *flags)
433 es = ossl_err_get_state_int();
438 * Clear anything that should have been cleared earlier. We do this
439 * here because this doesn't have constant-time issues.
441 while (es->bottom != es->top) {
442 if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
443 err_clear(es, es->top, 0);
444 es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
447 i = (es->bottom + 1) % ERR_NUM_ERRORS;
448 if (es->err_flags[i] & ERR_FLAG_CLEAR) {
450 err_clear(es, es->bottom, 0);
456 /* If everything has been cleared, the stack is empty. */
457 if (es->bottom == es->top)
460 /* Which error, the top of stack (latest one) or the first one? */
461 if (g == EV_PEEK_LAST)
464 i = (es->bottom + 1) % ERR_NUM_ERRORS;
466 ret = es->err_buffer[i];
469 es->err_buffer[i] = 0;
473 *file = es->err_file[i];
478 *line = es->err_line[i];
480 *func = es->err_func[i];
485 *flags = es->err_data_flags[i];
488 err_clear_data(es, i, 0);
491 *data = es->err_data[i];
501 void ossl_err_string_int(unsigned long e, const char *func,
502 char *buf, size_t len)
504 char lsbuf[64], rsbuf[256];
505 const char *ls, *rs = NULL;
512 ls = ERR_lib_error_string(e);
514 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
519 * ERR_reason_error_string() can't safely return system error strings,
520 * since it would call openssl_strerror_r(), which needs a buffer for
521 * thread safety. So for system errors, we call openssl_strerror_r()
524 r = ERR_GET_REASON(e);
525 #ifndef OPENSSL_NO_ERR
526 if (ERR_SYSTEM_ERROR(e)) {
527 if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
530 rs = ERR_reason_error_string(e);
534 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
535 r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
539 BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
540 if (strlen(buf) == len - 1) {
541 /* Didn't fit; use a minimal format. */
542 BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
547 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
549 ossl_err_string_int(e, "", buf, len);
553 * ERR_error_string_n should be used instead for ret != NULL as
554 * ERR_error_string cannot know how large the buffer is
556 char *ERR_error_string(unsigned long e, char *ret)
558 static char buf[256];
562 ERR_error_string_n(e, ret, (int)sizeof(buf));
566 const char *ERR_lib_error_string(unsigned long e)
568 ERR_STRING_DATA d, *p;
571 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
576 d.error = ERR_PACK(l, 0, 0);
577 p = int_err_get_item(&d);
578 return ((p == NULL) ? NULL : p->string);
581 #ifndef OPENSSL_NO_DEPRECATED_3_0
582 const char *ERR_func_error_string(unsigned long e)
588 const char *ERR_reason_error_string(unsigned long e)
590 ERR_STRING_DATA d, *p = NULL;
593 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
598 * ERR_reason_error_string() can't safely return system error strings,
599 * since openssl_strerror_r() needs a buffer for thread safety, and we
600 * haven't got one that would serve any sensible purpose.
602 if (ERR_SYSTEM_ERROR(e))
606 r = ERR_GET_REASON(e);
607 d.error = ERR_PACK(l, 0, r);
608 p = int_err_get_item(&d);
610 d.error = ERR_PACK(0, 0, r);
611 p = int_err_get_item(&d);
613 return ((p == NULL) ? NULL : p->string);
616 static void err_delete_thread_state(void *unused)
618 ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
622 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
623 ERR_STATE_free(state);
626 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
627 void ERR_remove_thread_state(void *dummy)
632 #ifndef OPENSSL_NO_DEPRECATED_1_0_0
633 void ERR_remove_state(unsigned long pid)
638 DEFINE_RUN_ONCE_STATIC(err_do_init)
640 set_err_thread_local = 1;
641 return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
644 ERR_STATE *ossl_err_get_state_int(void)
647 int saveerrno = get_last_sys_error();
649 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
652 if (!RUN_ONCE(&err_init, err_do_init))
655 state = CRYPTO_THREAD_get_local(&err_thread_local);
656 if (state == (ERR_STATE*)-1)
660 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
663 if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
664 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
668 if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
669 || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
670 ERR_STATE_free(state);
671 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
675 /* Ignore failures from these */
676 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
679 set_sys_error(saveerrno);
683 #ifndef OPENSSL_NO_DEPRECATED_3_0
684 ERR_STATE *ERR_get_state(void)
686 return ossl_err_get_state_int();
692 * err_shelve_state returns the current thread local error state
693 * and freezes the error module until err_unshelve_state is called.
695 int err_shelve_state(void **state)
697 int saveerrno = get_last_sys_error();
700 * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
701 * via ossl_init_load_crypto_nodelete(), by which point the requested
702 * "base" initialization has already been performed, so the below call is a
703 * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
705 * If are no other valid callers of this function, the call below can be
706 * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
707 * potential uses that are not from inside OPENSSL_init_crypto(), then this
708 * call is needed, but some care is required to make sure that the re-entry
711 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
714 if (!RUN_ONCE(&err_init, err_do_init))
717 *state = CRYPTO_THREAD_get_local(&err_thread_local);
718 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
721 set_sys_error(saveerrno);
726 * err_unshelve_state restores the error state that was returned
727 * by err_shelve_state previously.
729 void err_unshelve_state(void* state)
731 if (state != (void*)-1)
732 CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
735 int ERR_get_next_error_library(void)
739 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
742 if (!CRYPTO_THREAD_write_lock(err_string_lock))
744 ret = int_err_library_number++;
745 CRYPTO_THREAD_unlock(err_string_lock);
749 static int err_set_error_data_int(char *data, size_t size, int flags,
754 es = ossl_err_get_state_int();
758 err_clear_data(es, es->top, deallocate);
759 err_set_data(es, es->top, data, size, flags);
764 void ERR_set_error_data(char *data, int flags)
767 * This function is void so we cannot propagate the error return. Since it
768 * is also in the public API we can't change the return type.
770 * We estimate the size of the data. If it's not flagged as allocated,
771 * then this is safe, and if it is flagged as allocated, then our size
772 * may be smaller than the actual allocation, but that doesn't matter
773 * too much, the buffer will remain untouched or will eventually be
774 * reallocated to a new size.
776 * callers should be advised that this function takes over ownership of
777 * the allocated memory, i.e. they can't count on the pointer to remain
780 err_set_error_data_int(data, strlen(data) + 1, flags, 1);
783 void ERR_add_error_data(int num, ...)
787 ERR_add_error_vdata(num, args);
791 void ERR_add_error_vdata(int num, va_list args)
794 int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
798 /* Get the current error data; if an allocated string get it. */
799 es = ossl_err_get_state_int();
805 * If err_data is allocated already, re-use the space.
806 * Otherwise, allocate a small new buffer.
808 if ((es->err_data_flags[i] & flags) == flags) {
809 str = es->err_data[i];
810 size = es->err_data_size[i];
813 * To protect the string we just grabbed from tampering by other
814 * functions we may call, or to protect them from freeing a pointer
815 * that may no longer be valid at that point, we clear away the
816 * data pointer and the flags. We will set them again at the end
819 es->err_data[i] = NULL;
820 es->err_data_flags[i] = 0;
821 } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
829 arg = va_arg(args, char *);
837 p = OPENSSL_realloc(str, size);
844 OPENSSL_strlcat(str, arg, (size_t)size);
846 if (!err_set_error_data_int(str, size, flags, 0))
850 void err_clear_last_constant_time(int clear)
855 es = ossl_err_get_state_int();
862 * Flag error as cleared but remove it elsewhere to avoid two errors
863 * accessing the same error stack location, revealing timing information.
865 clear = constant_time_select_int(constant_time_eq_int(clear, 0),
867 es->err_flags[top] |= clear;