2 * Copyright 1995-2018 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
13 #include "internal/cryptlib_int.h"
14 #include "internal/err.h"
15 #include "internal/err_int.h"
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
18 #include <openssl/buffer.h>
19 #include <openssl/bio.h>
20 #include <openssl/opensslconf.h>
21 #include "internal/thread_once.h"
22 #include "internal/ctype.h"
23 #include "internal/constant_time_locl.h"
26 static int err_load_strings(const ERR_STRING_DATA
*str
);
28 static void ERR_STATE_free(ERR_STATE
*s
);
29 #ifndef OPENSSL_NO_ERR
30 static ERR_STRING_DATA ERR_str_libraries
[] = {
31 {ERR_PACK(ERR_LIB_NONE
, 0, 0), "unknown library"},
32 {ERR_PACK(ERR_LIB_SYS
, 0, 0), "system library"},
33 {ERR_PACK(ERR_LIB_BN
, 0, 0), "bignum routines"},
34 {ERR_PACK(ERR_LIB_RSA
, 0, 0), "rsa routines"},
35 {ERR_PACK(ERR_LIB_DH
, 0, 0), "Diffie-Hellman routines"},
36 {ERR_PACK(ERR_LIB_EVP
, 0, 0), "digital envelope routines"},
37 {ERR_PACK(ERR_LIB_BUF
, 0, 0), "memory buffer routines"},
38 {ERR_PACK(ERR_LIB_OBJ
, 0, 0), "object identifier routines"},
39 {ERR_PACK(ERR_LIB_PEM
, 0, 0), "PEM routines"},
40 {ERR_PACK(ERR_LIB_DSA
, 0, 0), "dsa routines"},
41 {ERR_PACK(ERR_LIB_X509
, 0, 0), "x509 certificate routines"},
42 {ERR_PACK(ERR_LIB_ASN1
, 0, 0), "asn1 encoding routines"},
43 {ERR_PACK(ERR_LIB_CONF
, 0, 0), "configuration file routines"},
44 {ERR_PACK(ERR_LIB_CRYPTO
, 0, 0), "common libcrypto routines"},
45 {ERR_PACK(ERR_LIB_EC
, 0, 0), "elliptic curve routines"},
46 {ERR_PACK(ERR_LIB_ECDSA
, 0, 0), "ECDSA routines"},
47 {ERR_PACK(ERR_LIB_ECDH
, 0, 0), "ECDH routines"},
48 {ERR_PACK(ERR_LIB_SSL
, 0, 0), "SSL routines"},
49 {ERR_PACK(ERR_LIB_BIO
, 0, 0), "BIO routines"},
50 {ERR_PACK(ERR_LIB_PKCS7
, 0, 0), "PKCS7 routines"},
51 {ERR_PACK(ERR_LIB_X509V3
, 0, 0), "X509 V3 routines"},
52 {ERR_PACK(ERR_LIB_PKCS12
, 0, 0), "PKCS12 routines"},
53 {ERR_PACK(ERR_LIB_RAND
, 0, 0), "random number generator"},
54 {ERR_PACK(ERR_LIB_DSO
, 0, 0), "DSO support routines"},
55 {ERR_PACK(ERR_LIB_TS
, 0, 0), "time stamp routines"},
56 {ERR_PACK(ERR_LIB_ENGINE
, 0, 0), "engine routines"},
57 {ERR_PACK(ERR_LIB_OCSP
, 0, 0), "OCSP routines"},
58 {ERR_PACK(ERR_LIB_UI
, 0, 0), "UI routines"},
59 {ERR_PACK(ERR_LIB_FIPS
, 0, 0), "FIPS routines"},
60 {ERR_PACK(ERR_LIB_CMS
, 0, 0), "CMS routines"},
61 {ERR_PACK(ERR_LIB_CRMF
, 0, 0), "CRMF routines"},
62 {ERR_PACK(ERR_LIB_HMAC
, 0, 0), "HMAC routines"},
63 {ERR_PACK(ERR_LIB_CT
, 0, 0), "CT routines"},
64 {ERR_PACK(ERR_LIB_ASYNC
, 0, 0), "ASYNC routines"},
65 {ERR_PACK(ERR_LIB_KDF
, 0, 0), "KDF routines"},
66 {ERR_PACK(ERR_LIB_OSSL_STORE
, 0, 0), "STORE routines"},
67 {ERR_PACK(ERR_LIB_SM2
, 0, 0), "SM2 routines"},
68 {ERR_PACK(ERR_LIB_ESS
, 0, 0), "ESS routines"},
69 {ERR_PACK(ERR_LIB_PROV
, 0, 0), "Provider routines"},
73 static ERR_STRING_DATA ERR_str_functs
[] = {
74 {ERR_PACK(0, SYS_F_FOPEN
, 0), "fopen"},
75 {ERR_PACK(0, SYS_F_CONNECT
, 0), "connect"},
76 {ERR_PACK(0, SYS_F_GETSERVBYNAME
, 0), "getservbyname"},
77 {ERR_PACK(0, SYS_F_SOCKET
, 0), "socket"},
78 {ERR_PACK(0, SYS_F_IOCTLSOCKET
, 0), "ioctlsocket"},
79 {ERR_PACK(0, SYS_F_BIND
, 0), "bind"},
80 {ERR_PACK(0, SYS_F_LISTEN
, 0), "listen"},
81 {ERR_PACK(0, SYS_F_ACCEPT
, 0), "accept"},
82 # ifdef OPENSSL_SYS_WINDOWS
83 {ERR_PACK(0, SYS_F_WSASTARTUP
, 0), "WSAstartup"},
85 {ERR_PACK(0, SYS_F_OPENDIR
, 0), "opendir"},
86 {ERR_PACK(0, SYS_F_FREAD
, 0), "fread"},
87 {ERR_PACK(0, SYS_F_GETADDRINFO
, 0), "getaddrinfo"},
88 {ERR_PACK(0, SYS_F_GETNAMEINFO
, 0), "getnameinfo"},
89 {ERR_PACK(0, SYS_F_SETSOCKOPT
, 0), "setsockopt"},
90 {ERR_PACK(0, SYS_F_GETSOCKOPT
, 0), "getsockopt"},
91 {ERR_PACK(0, SYS_F_GETSOCKNAME
, 0), "getsockname"},
92 {ERR_PACK(0, SYS_F_GETHOSTBYNAME
, 0), "gethostbyname"},
93 {ERR_PACK(0, SYS_F_FFLUSH
, 0), "fflush"},
94 {ERR_PACK(0, SYS_F_OPEN
, 0), "open"},
95 {ERR_PACK(0, SYS_F_CLOSE
, 0), "close"},
96 {ERR_PACK(0, SYS_F_IOCTL
, 0), "ioctl"},
97 {ERR_PACK(0, SYS_F_STAT
, 0), "stat"},
98 {ERR_PACK(0, SYS_F_FCNTL
, 0), "fcntl"},
99 {ERR_PACK(0, SYS_F_FSTAT
, 0), "fstat"},
103 static ERR_STRING_DATA ERR_str_reasons
[] = {
104 {ERR_R_SYS_LIB
, "system lib"},
105 {ERR_R_BN_LIB
, "BN lib"},
106 {ERR_R_RSA_LIB
, "RSA lib"},
107 {ERR_R_DH_LIB
, "DH lib"},
108 {ERR_R_EVP_LIB
, "EVP lib"},
109 {ERR_R_BUF_LIB
, "BUF lib"},
110 {ERR_R_OBJ_LIB
, "OBJ lib"},
111 {ERR_R_PEM_LIB
, "PEM lib"},
112 {ERR_R_DSA_LIB
, "DSA lib"},
113 {ERR_R_X509_LIB
, "X509 lib"},
114 {ERR_R_ASN1_LIB
, "ASN1 lib"},
115 {ERR_R_EC_LIB
, "EC lib"},
116 {ERR_R_BIO_LIB
, "BIO lib"},
117 {ERR_R_PKCS7_LIB
, "PKCS7 lib"},
118 {ERR_R_X509V3_LIB
, "X509V3 lib"},
119 {ERR_R_ENGINE_LIB
, "ENGINE lib"},
120 {ERR_R_UI_LIB
, "UI lib"},
121 {ERR_R_OSSL_STORE_LIB
, "STORE lib"},
122 {ERR_R_ECDSA_LIB
, "ECDSA lib"},
124 {ERR_R_NESTED_ASN1_ERROR
, "nested asn1 error"},
125 {ERR_R_MISSING_ASN1_EOS
, "missing asn1 eos"},
127 {ERR_R_FATAL
, "fatal"},
128 {ERR_R_MALLOC_FAILURE
, "malloc failure"},
129 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
,
130 "called a function you should not call"},
131 {ERR_R_PASSED_NULL_PARAMETER
, "passed a null parameter"},
132 {ERR_R_INTERNAL_ERROR
, "internal error"},
133 {ERR_R_DISABLED
, "called a function that was disabled at compile-time"},
134 {ERR_R_INIT_FAIL
, "init fail"},
135 {ERR_R_OPERATION_FAIL
, "operation fail"},
141 static CRYPTO_ONCE err_init
= CRYPTO_ONCE_STATIC_INIT
;
142 static int set_err_thread_local
;
143 static CRYPTO_THREAD_LOCAL err_thread_local
;
145 static CRYPTO_ONCE err_string_init
= CRYPTO_ONCE_STATIC_INIT
;
146 static CRYPTO_RWLOCK
*err_string_lock
;
148 static ERR_STRING_DATA
*int_err_get_item(const ERR_STRING_DATA
*);
154 static LHASH_OF(ERR_STRING_DATA
) *int_error_hash
= NULL
;
155 static int int_err_library_number
= ERR_LIB_USER
;
157 static unsigned long get_error_values(int inc
, int top
, const char **file
,
158 int *line
, const char **data
,
161 static unsigned long err_string_data_hash(const ERR_STRING_DATA
*a
)
163 unsigned long ret
, l
;
166 ret
= l
^ ERR_GET_LIB(l
) ^ ERR_GET_FUNC(l
);
167 return (ret
^ ret
% 19 * 13);
170 static int err_string_data_cmp(const ERR_STRING_DATA
*a
,
171 const ERR_STRING_DATA
*b
)
173 if (a
->error
== b
->error
)
175 return a
->error
> b
->error
? 1 : -1;
178 static ERR_STRING_DATA
*int_err_get_item(const ERR_STRING_DATA
*d
)
180 ERR_STRING_DATA
*p
= NULL
;
182 CRYPTO_THREAD_read_lock(err_string_lock
);
183 p
= lh_ERR_STRING_DATA_retrieve(int_error_hash
, d
);
184 CRYPTO_THREAD_unlock(err_string_lock
);
189 #ifndef OPENSSL_NO_ERR
190 /* A measurement on Linux 2018-11-21 showed about 3.5kib */
191 # define SPACE_SYS_STR_REASONS 4 * 1024
192 # define NUM_SYS_STR_REASONS 127
194 static ERR_STRING_DATA SYS_str_reasons
[NUM_SYS_STR_REASONS
+ 1];
196 * SYS_str_reasons is filled with copies of strerror() results at
197 * initialization. 'errno' values up to 127 should cover all usual errors,
198 * others will be displayed numerically by ERR_error_string. It is crucial
199 * that we have something for each reason code that occurs in
200 * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
201 * which always gets an errno value and never one of those 'standard' reason
205 static void build_SYS_str_reasons(void)
207 /* OPENSSL_malloc cannot be used here, use static storage instead */
208 static char strerror_pool
[SPACE_SYS_STR_REASONS
];
209 char *cur
= strerror_pool
;
213 int saveerrno
= get_last_sys_error();
215 CRYPTO_THREAD_write_lock(err_string_lock
);
217 CRYPTO_THREAD_unlock(err_string_lock
);
221 for (i
= 1; i
<= NUM_SYS_STR_REASONS
; i
++) {
222 ERR_STRING_DATA
*str
= &SYS_str_reasons
[i
- 1];
224 str
->error
= ERR_PACK(ERR_LIB_SYS
, 0, i
);
225 if (str
->string
== NULL
) {
226 if (openssl_strerror_r(i
, cur
, sizeof(strerror_pool
) - cnt
)) {
227 size_t l
= strlen(cur
);
231 if (cnt
> sizeof(strerror_pool
))
232 cnt
= sizeof(strerror_pool
);
236 * VMS has an unusual quirk of adding spaces at the end of
237 * some (most? all?) messages. Lets trim them off.
239 while (ossl_isspace(cur
[-1])) {
247 if (str
->string
== NULL
)
248 str
->string
= "unknown";
252 * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
253 * required by ERR_load_strings.
258 CRYPTO_THREAD_unlock(err_string_lock
);
259 /* openssl_strerror_r could change errno, but we want to preserve it */
260 set_sys_error(saveerrno
);
261 err_load_strings(SYS_str_reasons
);
265 #define err_clear_data(p, i) \
267 if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
268 OPENSSL_free((p)->err_data[i]); \
269 (p)->err_data[i] = NULL; \
271 (p)->err_data_flags[i] = 0; \
274 #define err_clear(p, i) \
276 err_clear_data(p, i); \
277 (p)->err_flags[i] = 0; \
278 (p)->err_buffer[i] = 0; \
279 (p)->err_file[i] = NULL; \
280 (p)->err_line[i] = -1; \
283 static void ERR_STATE_free(ERR_STATE
*s
)
289 for (i
= 0; i
< ERR_NUM_ERRORS
; i
++) {
290 err_clear_data(s
, i
);
295 DEFINE_RUN_ONCE_STATIC(do_err_strings_init
)
297 if (!OPENSSL_init_crypto(0, NULL
))
299 err_string_lock
= CRYPTO_THREAD_lock_new();
300 if (err_string_lock
== NULL
)
302 int_error_hash
= lh_ERR_STRING_DATA_new(err_string_data_hash
,
303 err_string_data_cmp
);
304 if (int_error_hash
== NULL
) {
305 CRYPTO_THREAD_lock_free(err_string_lock
);
306 err_string_lock
= NULL
;
312 void err_cleanup(void)
314 if (set_err_thread_local
!= 0)
315 CRYPTO_THREAD_cleanup_local(&err_thread_local
);
316 CRYPTO_THREAD_lock_free(err_string_lock
);
317 err_string_lock
= NULL
;
318 lh_ERR_STRING_DATA_free(int_error_hash
);
319 int_error_hash
= NULL
;
323 * Legacy; pack in the library.
325 static void err_patch(int lib
, ERR_STRING_DATA
*str
)
327 unsigned long plib
= ERR_PACK(lib
, 0, 0);
329 for (; str
->error
!= 0; str
++)
334 * Hash in |str| error strings. Assumes the URN_ONCE was done.
336 static int err_load_strings(const ERR_STRING_DATA
*str
)
338 CRYPTO_THREAD_write_lock(err_string_lock
);
339 for (; str
->error
; str
++)
340 (void)lh_ERR_STRING_DATA_insert(int_error_hash
,
341 (ERR_STRING_DATA
*)str
);
342 CRYPTO_THREAD_unlock(err_string_lock
);
346 int ERR_load_ERR_strings(void)
348 #ifndef OPENSSL_NO_ERR
349 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
))
352 err_load_strings(ERR_str_libraries
);
353 err_load_strings(ERR_str_reasons
);
354 err_patch(ERR_LIB_SYS
, ERR_str_functs
);
355 err_load_strings(ERR_str_functs
);
356 build_SYS_str_reasons();
361 int ERR_load_strings(int lib
, ERR_STRING_DATA
*str
)
363 if (ERR_load_ERR_strings() == 0)
367 err_load_strings(str
);
371 int ERR_load_strings_const(const ERR_STRING_DATA
*str
)
373 if (ERR_load_ERR_strings() == 0)
375 err_load_strings(str
);
379 int ERR_unload_strings(int lib
, ERR_STRING_DATA
*str
)
381 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
))
384 CRYPTO_THREAD_write_lock(err_string_lock
);
386 * We don't need to ERR_PACK the lib, since that was done (to
387 * the table) when it was loaded.
389 for (; str
->error
; str
++)
390 (void)lh_ERR_STRING_DATA_delete(int_error_hash
, str
);
391 CRYPTO_THREAD_unlock(err_string_lock
);
396 void err_free_strings_int(void)
398 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
))
402 /********************************************************/
404 void ERR_put_error(int lib
, int func
, int reason
, const char *file
, int line
)
410 * In the BS2000-OSD POSIX subsystem, the compiler generates path names
411 * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
412 * something sensible. @@@ We shouldn't modify a const string, though.
414 if (strncmp(file
, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
417 /* Skip the "*POSIX(" prefix */
418 file
+= sizeof("*POSIX(") - 1;
419 end
= &file
[strlen(file
) - 1];
422 /* Optional: use the basename of the path only. */
423 if ((end
= strrchr(file
, '/')) != NULL
)
427 es
= ERR_get_state();
431 es
->top
= (es
->top
+ 1) % ERR_NUM_ERRORS
;
432 if (es
->top
== es
->bottom
)
433 es
->bottom
= (es
->bottom
+ 1) % ERR_NUM_ERRORS
;
434 es
->err_flags
[es
->top
] = 0;
435 es
->err_buffer
[es
->top
] = ERR_PACK(lib
, func
, reason
);
436 es
->err_file
[es
->top
] = file
;
437 es
->err_line
[es
->top
] = line
;
438 err_clear_data(es
, es
->top
);
441 void ERR_clear_error(void)
446 es
= ERR_get_state();
450 for (i
= 0; i
< ERR_NUM_ERRORS
; i
++) {
453 es
->top
= es
->bottom
= 0;
456 unsigned long ERR_get_error(void)
458 return get_error_values(1, 0, NULL
, NULL
, NULL
, NULL
);
461 unsigned long ERR_get_error_line(const char **file
, int *line
)
463 return get_error_values(1, 0, file
, line
, NULL
, NULL
);
466 unsigned long ERR_get_error_line_data(const char **file
, int *line
,
467 const char **data
, int *flags
)
469 return get_error_values(1, 0, file
, line
, data
, flags
);
472 unsigned long ERR_peek_error(void)
474 return get_error_values(0, 0, NULL
, NULL
, NULL
, NULL
);
477 unsigned long ERR_peek_error_line(const char **file
, int *line
)
479 return get_error_values(0, 0, file
, line
, NULL
, NULL
);
482 unsigned long ERR_peek_error_line_data(const char **file
, int *line
,
483 const char **data
, int *flags
)
485 return get_error_values(0, 0, file
, line
, data
, flags
);
488 unsigned long ERR_peek_last_error(void)
490 return get_error_values(0, 1, NULL
, NULL
, NULL
, NULL
);
493 unsigned long ERR_peek_last_error_line(const char **file
, int *line
)
495 return get_error_values(0, 1, file
, line
, NULL
, NULL
);
498 unsigned long ERR_peek_last_error_line_data(const char **file
, int *line
,
499 const char **data
, int *flags
)
501 return get_error_values(0, 1, file
, line
, data
, flags
);
504 static unsigned long get_error_values(int inc
, int top
, const char **file
,
505 int *line
, const char **data
,
512 es
= ERR_get_state();
526 return ERR_R_INTERNAL_ERROR
;
529 while (es
->bottom
!= es
->top
) {
530 if (es
->err_flags
[es
->top
] & ERR_FLAG_CLEAR
) {
531 err_clear(es
, es
->top
);
532 es
->top
= es
->top
> 0 ? es
->top
- 1 : ERR_NUM_ERRORS
- 1;
535 i
= (es
->bottom
+ 1) % ERR_NUM_ERRORS
;
536 if (es
->err_flags
[i
] & ERR_FLAG_CLEAR
) {
538 err_clear(es
, es
->bottom
);
544 if (es
->bottom
== es
->top
)
548 i
= es
->top
; /* last error */
550 i
= (es
->bottom
+ 1) % ERR_NUM_ERRORS
; /* first error */
552 ret
= es
->err_buffer
[i
];
555 es
->err_buffer
[i
] = 0;
558 if (file
!= NULL
&& line
!= NULL
) {
559 if (es
->err_file
[i
] == NULL
) {
563 *file
= es
->err_file
[i
];
564 *line
= es
->err_line
[i
];
570 err_clear_data(es
, i
);
573 if (es
->err_data
[i
] == NULL
) {
578 *data
= es
->err_data
[i
];
580 *flags
= es
->err_data_flags
[i
];
586 void ERR_error_string_n(unsigned long e
, char *buf
, size_t len
)
588 char lsbuf
[64], fsbuf
[64], rsbuf
[64];
589 const char *ls
, *fs
, *rs
;
590 unsigned long l
, f
, r
;
596 ls
= ERR_lib_error_string(e
);
598 BIO_snprintf(lsbuf
, sizeof(lsbuf
), "lib(%lu)", l
);
602 fs
= ERR_func_error_string(e
);
605 BIO_snprintf(fsbuf
, sizeof(fsbuf
), "func(%lu)", f
);
609 rs
= ERR_reason_error_string(e
);
610 r
= ERR_GET_REASON(e
);
612 BIO_snprintf(rsbuf
, sizeof(rsbuf
), "reason(%lu)", r
);
616 BIO_snprintf(buf
, len
, "error:%08lX:%s:%s:%s", e
, ls
, fs
, rs
);
617 if (strlen(buf
) == len
- 1) {
618 /* Didn't fit; use a minimal format. */
619 BIO_snprintf(buf
, len
, "err:%lx:%lx:%lx:%lx", e
, l
, f
, r
);
624 * ERR_error_string_n should be used instead for ret != NULL as
625 * ERR_error_string cannot know how large the buffer is
627 char *ERR_error_string(unsigned long e
, char *ret
)
629 static char buf
[256];
633 ERR_error_string_n(e
, ret
, (int)sizeof(buf
));
637 const char *ERR_lib_error_string(unsigned long e
)
639 ERR_STRING_DATA d
, *p
;
642 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
)) {
647 d
.error
= ERR_PACK(l
, 0, 0);
648 p
= int_err_get_item(&d
);
649 return ((p
== NULL
) ? NULL
: p
->string
);
652 const char *ERR_func_error_string(unsigned long e
)
654 ERR_STRING_DATA d
, *p
;
657 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
)) {
663 d
.error
= ERR_PACK(l
, f
, 0);
664 p
= int_err_get_item(&d
);
665 return ((p
== NULL
) ? NULL
: p
->string
);
668 const char *ERR_reason_error_string(unsigned long e
)
670 ERR_STRING_DATA d
, *p
= NULL
;
673 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
)) {
678 r
= ERR_GET_REASON(e
);
679 d
.error
= ERR_PACK(l
, 0, r
);
680 p
= int_err_get_item(&d
);
682 d
.error
= ERR_PACK(0, 0, r
);
683 p
= int_err_get_item(&d
);
685 return ((p
== NULL
) ? NULL
: p
->string
);
688 void err_delete_thread_state(void)
690 ERR_STATE
*state
= CRYPTO_THREAD_get_local(&err_thread_local
);
694 CRYPTO_THREAD_set_local(&err_thread_local
, NULL
);
695 ERR_STATE_free(state
);
698 #if !OPENSSL_API_1_1_0
699 void ERR_remove_thread_state(void *dummy
)
704 #if !OPENSSL_API_1_0_0
705 void ERR_remove_state(unsigned long pid
)
710 DEFINE_RUN_ONCE_STATIC(err_do_init
)
712 set_err_thread_local
= 1;
713 return CRYPTO_THREAD_init_local(&err_thread_local
, NULL
);
716 ERR_STATE
*ERR_get_state(void)
719 int saveerrno
= get_last_sys_error();
721 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY
, NULL
))
724 if (!RUN_ONCE(&err_init
, err_do_init
))
727 state
= CRYPTO_THREAD_get_local(&err_thread_local
);
728 if (state
== (ERR_STATE
*)-1)
732 if (!CRYPTO_THREAD_set_local(&err_thread_local
, (ERR_STATE
*)-1))
735 if ((state
= OPENSSL_zalloc(sizeof(*state
))) == NULL
) {
736 CRYPTO_THREAD_set_local(&err_thread_local
, NULL
);
740 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE
)
741 || !CRYPTO_THREAD_set_local(&err_thread_local
, state
)) {
742 ERR_STATE_free(state
);
743 CRYPTO_THREAD_set_local(&err_thread_local
, NULL
);
747 /* Ignore failures from these */
748 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
, NULL
);
751 set_sys_error(saveerrno
);
756 * err_shelve_state returns the current thread local error state
757 * and freezes the error module until err_unshelve_state is called.
759 int err_shelve_state(void **state
)
761 int saveerrno
= get_last_sys_error();
764 * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
765 * via ossl_init_load_crypto_nodelete(), by which point the requested
766 * "base" initialization has already been performed, so the below call is a
767 * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
769 * If are no other valid callers of this function, the call below can be
770 * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
771 * potential uses that are not from inside OPENSSL_init_crypto(), then this
772 * call is needed, but some care is required to make sure that the re-entry
775 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY
, NULL
))
778 if (!RUN_ONCE(&err_init
, err_do_init
))
781 *state
= CRYPTO_THREAD_get_local(&err_thread_local
);
782 if (!CRYPTO_THREAD_set_local(&err_thread_local
, (ERR_STATE
*)-1))
785 set_sys_error(saveerrno
);
790 * err_unshelve_state restores the error state that was returned
791 * by err_shelve_state previously.
793 void err_unshelve_state(void* state
)
795 if (state
!= (void*)-1)
796 CRYPTO_THREAD_set_local(&err_thread_local
, (ERR_STATE
*)state
);
799 int ERR_get_next_error_library(void)
803 if (!RUN_ONCE(&err_string_init
, do_err_strings_init
))
806 CRYPTO_THREAD_write_lock(err_string_lock
);
807 ret
= int_err_library_number
++;
808 CRYPTO_THREAD_unlock(err_string_lock
);
812 static int err_set_error_data_int(char *data
, int flags
)
817 es
= ERR_get_state();
823 err_clear_data(es
, i
);
824 es
->err_data
[i
] = data
;
825 es
->err_data_flags
[i
] = flags
;
830 void ERR_set_error_data(char *data
, int flags
)
833 * This function is void so we cannot propagate the error return. Since it
834 * is also in the public API we can't change the return type.
836 err_set_error_data_int(data
, flags
);
839 void ERR_add_error_data(int num
, ...)
843 ERR_add_error_vdata(num
, args
);
847 void ERR_add_error_vdata(int num
, va_list args
)
853 if ((str
= OPENSSL_malloc(s
+ 1)) == NULL
) {
854 /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
860 for (i
= 0; i
< num
; i
++) {
861 a
= va_arg(args
, char *);
867 p
= OPENSSL_realloc(str
, s
+ 1);
874 OPENSSL_strlcat(str
, a
, (size_t)s
+ 1);
876 if (!err_set_error_data_int(str
, ERR_TXT_MALLOCED
| ERR_TXT_STRING
))
880 int ERR_set_mark(void)
884 es
= ERR_get_state();
888 if (es
->bottom
== es
->top
)
890 es
->err_flags
[es
->top
] |= ERR_FLAG_MARK
;
894 int ERR_pop_to_mark(void)
898 es
= ERR_get_state();
902 while (es
->bottom
!= es
->top
903 && (es
->err_flags
[es
->top
] & ERR_FLAG_MARK
) == 0) {
904 err_clear(es
, es
->top
);
905 es
->top
= es
->top
> 0 ? es
->top
- 1 : ERR_NUM_ERRORS
- 1;
908 if (es
->bottom
== es
->top
)
910 es
->err_flags
[es
->top
] &= ~ERR_FLAG_MARK
;
914 int ERR_clear_last_mark(void)
919 es
= ERR_get_state();
924 while (es
->bottom
!= top
925 && (es
->err_flags
[top
] & ERR_FLAG_MARK
) == 0) {
926 top
= top
> 0 ? top
- 1 : ERR_NUM_ERRORS
- 1;
929 if (es
->bottom
== top
)
931 es
->err_flags
[top
] &= ~ERR_FLAG_MARK
;
935 void err_clear_last_constant_time(int clear
)
940 es
= ERR_get_state();
947 * Flag error as cleared but remove it elsewhere to avoid two errors
948 * accessing the same error stack location, revealing timing information.
950 clear
= constant_time_select_int(constant_time_eq_int(clear
, 0),
952 es
->err_flags
[top
] |= clear
;