]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/err/err.c
QUIC err handling: Properly report network errors
[thirdparty/openssl.git] / crypto / err / err.c
CommitLineData
aa6bb135 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
0a150c5c 3 *
4ad239b8 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
0a150c5c 8 */
d02b48c6 9
14e275e8
RL
10#define OSSL_FORCE_ERR_STATE
11
d02b48c6 12#include <stdio.h>
4565c3e3 13#include <stdarg.h>
0a150c5c 14#include <string.h>
25f2138b 15#include "crypto/cryptlib.h"
176db6dc 16#include "internal/err.h"
25f2138b 17#include "crypto/err.h"
52df25cf 18#include <openssl/err.h>
ec577822 19#include <openssl/crypto.h>
ec577822 20#include <openssl/buffer.h>
e5c84d51 21#include <openssl/bio.h>
98186eb4 22#include <openssl/opensslconf.h>
176db6dc 23#include "internal/thread_once.h"
25f2138b 24#include "crypto/ctype.h"
706457b7 25#include "internal/constant_time.h"
d5f9166b 26#include "internal/e_os.h"
706457b7 27#include "err_local.h"
3c1d6bbc 28
14e275e8
RL
29/* Forward declaration in case it's not published because of configuration */
30ERR_STATE *ERR_get_state(void);
31
11e85b89 32#ifndef OPENSSL_NO_ERR
52df25cf 33static int err_load_strings(const ERR_STRING_DATA *str);
11e85b89 34#endif
cf5bfbfc 35
58964a49 36static void ERR_STATE_free(ERR_STATE *s);
cf1b7d96 37#ifndef OPENSSL_NO_ERR
0f113f3e
MC
38static ERR_STRING_DATA ERR_str_libraries[] = {
39 {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
40 {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
41 {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
42 {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
43 {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
44 {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
45 {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
46 {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
47 {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
48 {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
49 {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
50 {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
51 {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
52 {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
53 {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
54 {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
55 {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
56 {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
57 {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
58 {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
59 {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
60 {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
61 {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
62 {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
63 {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
64 {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
65 {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
49844486 66 {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
0f113f3e
MC
67 {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
68 {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
a61b7f2f 69 {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"},
8869ad4a 70 {ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines"},
0f113f3e 71 {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
0cea8832 72 {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
079a1a90 73 {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
7c6a0d90 74 {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
71a5516d 75 {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
3d328a44 76 {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
e85d19c6 77 {ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines"},
6caf7f3a 78 {ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines"},
ece9304c
RL
79 {ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines"},
80 {ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines"},
29f178bd 81 {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"},
0f113f3e
MC
82 {0, NULL},
83};
84
3ae55288
DDO
85/*
86 * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in
87 * are listed. For maintainability, please keep all reasons in the same order.
88 */
0f113f3e
MC
89static ERR_STRING_DATA ERR_str_reasons[] = {
90 {ERR_R_SYS_LIB, "system lib"},
91 {ERR_R_BN_LIB, "BN lib"},
92 {ERR_R_RSA_LIB, "RSA lib"},
93 {ERR_R_DH_LIB, "DH lib"},
94 {ERR_R_EVP_LIB, "EVP lib"},
95 {ERR_R_BUF_LIB, "BUF lib"},
96 {ERR_R_OBJ_LIB, "OBJ lib"},
97 {ERR_R_PEM_LIB, "PEM lib"},
98 {ERR_R_DSA_LIB, "DSA lib"},
99 {ERR_R_X509_LIB, "X509 lib"},
100 {ERR_R_ASN1_LIB, "ASN1 lib"},
3ae55288 101 {ERR_R_CRYPTO_LIB, "CRYPTO lib"},
0f113f3e 102 {ERR_R_EC_LIB, "EC lib"},
0f113f3e
MC
103 {ERR_R_BIO_LIB, "BIO lib"},
104 {ERR_R_PKCS7_LIB, "PKCS7 lib"},
105 {ERR_R_X509V3_LIB, "X509V3 lib"},
0f113f3e 106 {ERR_R_ENGINE_LIB, "ENGINE lib"},
c785fd48 107 {ERR_R_UI_LIB, "UI lib"},
0f113f3e 108 {ERR_R_ECDSA_LIB, "ECDSA lib"},
3ae55288
DDO
109 {ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib"},
110 {ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib"},
0f113f3e
MC
111
112 {ERR_R_FATAL, "fatal"},
113 {ERR_R_MALLOC_FAILURE, "malloc failure"},
114 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
115 "called a function you should not call"},
116 {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
117 {ERR_R_INTERNAL_ERROR, "internal error"},
118 {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
302f7588 119 {ERR_R_INIT_FAIL, "init fail"},
3ae55288 120 {ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument"},
9a32dcf4 121 {ERR_R_OPERATION_FAIL, "operation fail"},
0d003c52 122 {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
866234ac 123 {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
3ae55288
DDO
124 {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
125 {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
d6d42cda
RL
126 /*
127 * Something is unsupported, exactly what is expressed with additional data
128 */
129 {ERR_R_UNSUPPORTED, "unsupported"},
130 /*
131 * A fetch failed for other reasons than the name to be fetched being
132 * unsupported.
133 */
134 {ERR_R_FETCH_FAILED, "fetch failed"},
9379bf94 135 {ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition"},
e3c50779
P
136 {ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock"},
137 {ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock"},
0f113f3e
MC
138 {0, NULL},
139};
d02b48c6 140#endif
0a150c5c 141
8509dcc9 142static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
73bc5370 143static int set_err_thread_local;
8509dcc9
AG
144static CRYPTO_THREAD_LOCAL err_thread_local;
145
7b8e12d2 146static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
11e85b89 147static CRYPTO_RWLOCK *err_string_lock = NULL;
7b8e12d2 148
11e85b89 149#ifndef OPENSSL_NO_ERR
566bdf2b 150static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
11e85b89 151#endif
566bdf2b 152
0f113f3e 153/*
3e47caff 154 * The internal state
0f113f3e 155 */
f3f1cf84 156
11e85b89 157#ifndef OPENSSL_NO_ERR
3c1d6bbc 158static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
11e85b89 159#endif
0f113f3e 160static int int_err_library_number = ERR_LIB_USER;
566bdf2b 161
b4570683
RS
162typedef enum ERR_GET_ACTION_e {
163 EV_POP, EV_PEEK, EV_PEEK_LAST
164} ERR_GET_ACTION;
165
166static unsigned long get_error_values(ERR_GET_ACTION g,
167 const char **file, int *line,
168 const char **func, const char **data,
169 int *flags);
566bdf2b 170
11e85b89 171#ifndef OPENSSL_NO_ERR
3c1d6bbc 172static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
0f113f3e
MC
173{
174 unsigned long ret, l;
175
176 l = a->error;
aac96e27 177 ret = l ^ ERR_GET_LIB(l);
0f113f3e
MC
178 return (ret ^ ret % 19 * 13);
179}
3c1d6bbc 180
3c1d6bbc 181static int err_string_data_cmp(const ERR_STRING_DATA *a,
0f113f3e
MC
182 const ERR_STRING_DATA *b)
183{
c784a838
RS
184 if (a->error == b->error)
185 return 0;
186 return a->error > b->error ? 1 : -1;
0f113f3e
MC
187}
188
566bdf2b 189static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
0f113f3e 190{
3e47caff 191 ERR_STRING_DATA *p = NULL;
78f79235 192
cd3f8c1b
RS
193 if (!CRYPTO_THREAD_read_lock(err_string_lock))
194 return NULL;
52df25cf 195 p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
7b8e12d2 196 CRYPTO_THREAD_unlock(err_string_lock);
78f79235 197
0f113f3e
MC
198 return p;
199}
11e85b89 200#endif
567fef89 201
5639ee79 202static void ERR_STATE_free(ERR_STATE *state)
0f113f3e
MC
203{
204 int i;
58964a49 205
5639ee79 206 if (state == NULL)
e6e9170d 207 return;
0f113f3e 208 for (i = 0; i < ERR_NUM_ERRORS; i++) {
5639ee79 209 err_clear(state, i, 1);
0f113f3e 210 }
5639ee79 211 CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE);
0f113f3e 212}
58964a49 213
c2e4e5d2 214DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
7b8e12d2 215{
12b4e582 216 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
eb2b9892 217 return 0;
63ab5ea1 218 err_string_lock = CRYPTO_THREAD_lock_new();
eb2b9892
BE
219 if (err_string_lock == NULL)
220 return 0;
11e85b89 221#ifndef OPENSSL_NO_ERR
52df25cf
RS
222 int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
223 err_string_data_cmp);
eb2b9892
BE
224 if (int_error_hash == NULL) {
225 CRYPTO_THREAD_lock_free(err_string_lock);
226 err_string_lock = NULL;
227 return 0;
228 }
11e85b89 229#endif
eb2b9892 230 return 1;
ff234405
MC
231}
232
233void err_cleanup(void)
234{
73bc5370
RS
235 if (set_err_thread_local != 0)
236 CRYPTO_THREAD_cleanup_local(&err_thread_local);
ff234405
MC
237 CRYPTO_THREAD_lock_free(err_string_lock);
238 err_string_lock = NULL;
11e85b89 239#ifndef OPENSSL_NO_ERR
52df25cf
RS
240 lh_ERR_STRING_DATA_free(int_error_hash);
241 int_error_hash = NULL;
11e85b89 242#endif
52df25cf
RS
243}
244
11e85b89 245#ifndef OPENSSL_NO_ERR
52df25cf
RS
246/*
247 * Legacy; pack in the library.
248 */
249static void err_patch(int lib, ERR_STRING_DATA *str)
250{
251 unsigned long plib = ERR_PACK(lib, 0, 0);
252
253 for (; str->error != 0; str++)
254 str->error |= plib;
255}
256
257/*
5884b051 258 * Hash in |str| error strings. Assumes the RUN_ONCE was done.
52df25cf
RS
259 */
260static int err_load_strings(const ERR_STRING_DATA *str)
261{
cd3f8c1b
RS
262 if (!CRYPTO_THREAD_write_lock(err_string_lock))
263 return 0;
52df25cf
RS
264 for (; str->error; str++)
265 (void)lh_ERR_STRING_DATA_insert(int_error_hash,
266 (ERR_STRING_DATA *)str);
267 CRYPTO_THREAD_unlock(err_string_lock);
268 return 1;
7b8e12d2 269}
11e85b89 270#endif
7b8e12d2 271
b93f6c2d 272int ossl_err_load_ERR_strings(void)
0f113f3e 273{
cf1b7d96 274#ifndef OPENSSL_NO_ERR
69588edb
KR
275 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
276 return 0;
7b8e12d2 277
52df25cf
RS
278 err_load_strings(ERR_str_libraries);
279 err_load_strings(ERR_str_reasons);
d02b48c6 280#endif
69588edb 281 return 1;
0f113f3e 282}
d02b48c6 283
52df25cf 284int ERR_load_strings(int lib, ERR_STRING_DATA *str)
0f113f3e 285{
11e85b89 286#ifndef OPENSSL_NO_ERR
b93f6c2d 287 if (ossl_err_load_ERR_strings() == 0)
52df25cf
RS
288 return 0;
289
290 err_patch(lib, str);
291 err_load_strings(str);
11e85b89
DMSP
292#endif
293
52df25cf 294 return 1;
0f113f3e 295}
d02b48c6 296
52df25cf 297int ERR_load_strings_const(const ERR_STRING_DATA *str)
0f113f3e 298{
11e85b89 299#ifndef OPENSSL_NO_ERR
b93f6c2d 300 if (ossl_err_load_ERR_strings() == 0)
69588edb 301 return 0;
52df25cf 302 err_load_strings(str);
11e85b89
DMSP
303#endif
304
69588edb 305 return 1;
0f113f3e 306}
cf5bfbfc 307
c2e4e5d2 308int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
0f113f3e 309{
11e85b89 310#ifndef OPENSSL_NO_ERR
c2e4e5d2
RL
311 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
312 return 0;
7b8e12d2 313
cd3f8c1b
RS
314 if (!CRYPTO_THREAD_write_lock(err_string_lock))
315 return 0;
52df25cf
RS
316 /*
317 * We don't need to ERR_PACK the lib, since that was done (to
318 * the table) when it was loaded.
319 */
320 for (; str->error; str++)
321 (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
7b8e12d2 322 CRYPTO_THREAD_unlock(err_string_lock);
11e85b89 323#endif
c2e4e5d2
RL
324
325 return 1;
0f113f3e 326}
1738bb61 327
b3599dbb 328void err_free_strings_int(void)
0f113f3e 329{
1c8787d5 330 /* obsolete */
0f113f3e 331}
d02b48c6 332
58964a49
RE
333/********************************************************/
334
6b691a5c 335void ERR_clear_error(void)
0f113f3e
MC
336{
337 int i;
338 ERR_STATE *es;
d02b48c6 339
78715dcc 340 es = ossl_err_get_state_int();
4fc426b7
BE
341 if (es == NULL)
342 return;
d02b48c6 343
0f113f3e 344 for (i = 0; i < ERR_NUM_ERRORS; i++) {
10f8b368 345 err_clear(es, i, 0);
0f113f3e
MC
346 }
347 es->top = es->bottom = 0;
348}
d02b48c6 349
6b691a5c 350unsigned long ERR_get_error(void)
0f113f3e 351{
b4570683 352 return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
0f113f3e 353}
d02b48c6 354
b13342e9
RL
355unsigned long ERR_get_error_all(const char **file, int *line,
356 const char **func,
357 const char **data, int *flags)
358{
b4570683 359 return get_error_values(EV_POP, file, line, func, data, flags);
b13342e9
RL
360}
361
936c2b9e 362#ifndef OPENSSL_NO_DEPRECATED_3_0
af2f14ac
RL
363unsigned long ERR_get_error_line(const char **file, int *line)
364{
365 return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
366}
367
6b691a5c 368unsigned long ERR_get_error_line_data(const char **file, int *line,
0f113f3e
MC
369 const char **data, int *flags)
370{
b4570683 371 return get_error_values(EV_POP, file, line, NULL, data, flags);
0f113f3e 372}
b13342e9 373#endif
273db408 374
6b691a5c 375unsigned long ERR_peek_error(void)
0f113f3e 376{
b4570683 377 return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
0f113f3e 378}
d02b48c6 379
a14e2d9d 380unsigned long ERR_peek_error_line(const char **file, int *line)
0f113f3e 381{
b4570683 382 return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
b13342e9
RL
383}
384
385unsigned long ERR_peek_error_func(const char **func)
386{
b4570683 387 return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
b13342e9
RL
388}
389
390unsigned long ERR_peek_error_data(const char **data, int *flags)
391{
b4570683 392 return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
b13342e9
RL
393}
394
395unsigned long ERR_peek_error_all(const char **file, int *line,
396 const char **func,
397 const char **data, int *flags)
398{
b4570683 399 return get_error_values(EV_PEEK, file, line, func, data, flags);
0f113f3e 400}
0fc5cf08 401
936c2b9e 402#ifndef OPENSSL_NO_DEPRECATED_3_0
6b691a5c 403unsigned long ERR_peek_error_line_data(const char **file, int *line,
0f113f3e
MC
404 const char **data, int *flags)
405{
b4570683 406 return get_error_values(EV_PEEK, file, line, NULL, data, flags);
0f113f3e 407}
b13342e9 408#endif
273db408
BM
409
410unsigned long ERR_peek_last_error(void)
0f113f3e 411{
b4570683 412 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
0f113f3e 413}
273db408
BM
414
415unsigned long ERR_peek_last_error_line(const char **file, int *line)
0f113f3e 416{
b4570683 417 return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
b13342e9
RL
418}
419
420unsigned long ERR_peek_last_error_func(const char **func)
421{
b4570683 422 return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
b13342e9
RL
423}
424
425unsigned long ERR_peek_last_error_data(const char **data, int *flags)
426{
b4570683 427 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
b13342e9
RL
428}
429
430unsigned long ERR_peek_last_error_all(const char **file, int *line,
431 const char **func,
432 const char **data, int *flags)
433{
b4570683 434 return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
0f113f3e 435}
273db408 436
936c2b9e 437#ifndef OPENSSL_NO_DEPRECATED_3_0
a14e2d9d 438unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
0f113f3e
MC
439 const char **data, int *flags)
440{
b4570683 441 return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
0f113f3e 442}
b13342e9 443#endif
0f113f3e 444
b4570683
RS
445static unsigned long get_error_values(ERR_GET_ACTION g,
446 const char **file, int *line,
447 const char **func,
b13342e9 448 const char **data, int *flags)
0f113f3e
MC
449{
450 int i = 0;
451 ERR_STATE *es;
452 unsigned long ret;
453
78715dcc 454 es = ossl_err_get_state_int();
4fc426b7
BE
455 if (es == NULL)
456 return 0;
0f113f3e 457
b4570683
RS
458 /*
459 * Clear anything that should have been cleared earlier. We do this
460 * here because this doesn't have constant-time issues.
461 */
94dc53a3
BE
462 while (es->bottom != es->top) {
463 if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
10f8b368 464 err_clear(es, es->top, 0);
94dc53a3
BE
465 es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
466 continue;
467 }
468 i = (es->bottom + 1) % ERR_NUM_ERRORS;
469 if (es->err_flags[i] & ERR_FLAG_CLEAR) {
470 es->bottom = i;
10f8b368 471 err_clear(es, es->bottom, 0);
94dc53a3
BE
472 continue;
473 }
474 break;
475 }
476
b4570683 477 /* If everything has been cleared, the stack is empty. */
0f113f3e
MC
478 if (es->bottom == es->top)
479 return 0;
94dc53a3 480
b4570683
RS
481 /* Which error, the top of stack (latest one) or the first one? */
482 if (g == EV_PEEK_LAST)
483 i = es->top;
0f113f3e 484 else
b4570683 485 i = (es->bottom + 1) % ERR_NUM_ERRORS;
0f113f3e
MC
486
487 ret = es->err_buffer[i];
b4570683 488 if (g == EV_POP) {
0f113f3e
MC
489 es->bottom = i;
490 es->err_buffer[i] = 0;
491 }
492
df082268
DDO
493 if (file != NULL) {
494 *file = es->err_file[i];
495 if (*file == NULL)
496 *file = "";
0f113f3e 497 }
df082268
DDO
498 if (line != NULL)
499 *line = es->err_line[i];
b13342e9
RL
500 if (func != NULL) {
501 *func = es->err_func[i];
502 if (*func == NULL)
df082268 503 *func = "";
b13342e9 504 }
df082268
DDO
505 if (flags != NULL)
506 *flags = es->err_data_flags[i];
0f113f3e 507 if (data == NULL) {
b4570683 508 if (g == EV_POP) {
10f8b368 509 err_clear_data(es, i, 0);
0f113f3e
MC
510 }
511 } else {
df082268
DDO
512 *data = es->err_data[i];
513 if (*data == NULL) {
0f113f3e
MC
514 *data = "";
515 if (flags != NULL)
516 *flags = 0;
0f113f3e
MC
517 }
518 }
519 return ret;
520}
d02b48c6 521
63132c53
RL
522void ossl_err_string_int(unsigned long e, const char *func,
523 char *buf, size_t len)
0f113f3e 524{
71f2994b
RL
525 char lsbuf[64], rsbuf[256];
526 const char *ls, *rs = NULL;
63132c53 527 unsigned long l, r;
0f113f3e 528
e5c13615
MC
529 if (len == 0)
530 return;
531
0f113f3e 532 l = ERR_GET_LIB(e);
0f113f3e 533 ls = ERR_lib_error_string(e);
fa4dd546 534 if (ls == NULL) {
0f113f3e 535 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
fa4dd546
RS
536 ls = lsbuf;
537 }
538
71f2994b
RL
539 /*
540 * ERR_reason_error_string() can't safely return system error strings,
541 * since it would call openssl_strerror_r(), which needs a buffer for
542 * thread safety. So for system errors, we call openssl_strerror_r()
543 * directly instead.
544 */
fa4dd546 545 r = ERR_GET_REASON(e);
63132c53 546#ifndef OPENSSL_NO_ERR
71f2994b
RL
547 if (ERR_SYSTEM_ERROR(e)) {
548 if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
549 rs = rsbuf;
550 } else {
551 rs = ERR_reason_error_string(e);
552 }
63132c53 553#endif
fa4dd546 554 if (rs == NULL) {
9350aaa4
DDO
555 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
556 r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
fa4dd546
RS
557 rs = rsbuf;
558 }
0f113f3e 559
63132c53 560 BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
0f113f3e 561 if (strlen(buf) == len - 1) {
fa4dd546 562 /* Didn't fit; use a minimal format. */
63132c53 563 BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
0f113f3e
MC
564 }
565}
e5c84d51 566
63132c53
RL
567
568void ERR_error_string_n(unsigned long e, char *buf, size_t len)
569{
570 ossl_err_string_int(e, "", buf, len);
571}
572
0f113f3e
MC
573/*
574 * ERR_error_string_n should be used instead for ret != NULL as
575 * ERR_error_string cannot know how large the buffer is
576 */
e5c84d51 577char *ERR_error_string(unsigned long e, char *ret)
0f113f3e
MC
578{
579 static char buf[256];
e5c84d51 580
0f113f3e
MC
581 if (ret == NULL)
582 ret = buf;
fa4dd546 583 ERR_error_string_n(e, ret, (int)sizeof(buf));
0f113f3e
MC
584 return ret;
585}
d02b48c6 586
6b691a5c 587const char *ERR_lib_error_string(unsigned long e)
0f113f3e 588{
11e85b89 589#ifndef OPENSSL_NO_ERR
0f113f3e
MC
590 ERR_STRING_DATA d, *p;
591 unsigned long l;
d02b48c6 592
c2e4e5d2
RL
593 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
594 return NULL;
595 }
7b8e12d2 596
0f113f3e
MC
597 l = ERR_GET_LIB(e);
598 d.error = ERR_PACK(l, 0, 0);
3e47caff 599 p = int_err_get_item(&d);
0f113f3e 600 return ((p == NULL) ? NULL : p->string);
11e85b89
DMSP
601#else
602 return NULL;
603#endif
0f113f3e 604}
d02b48c6 605
936c2b9e 606#ifndef OPENSSL_NO_DEPRECATED_3_0
6b691a5c 607const char *ERR_func_error_string(unsigned long e)
0f113f3e 608{
b13342e9 609 return NULL;
0f113f3e 610}
b13342e9 611#endif
d02b48c6 612
6b691a5c 613const char *ERR_reason_error_string(unsigned long e)
0f113f3e 614{
11e85b89 615#ifndef OPENSSL_NO_ERR
0f113f3e
MC
616 ERR_STRING_DATA d, *p = NULL;
617 unsigned long l, r;
618
c2e4e5d2
RL
619 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
620 return NULL;
621 }
7b8e12d2 622
71f2994b
RL
623 /*
624 * ERR_reason_error_string() can't safely return system error strings,
625 * since openssl_strerror_r() needs a buffer for thread safety, and we
626 * haven't got one that would serve any sensible purpose.
627 */
628 if (ERR_SYSTEM_ERROR(e))
629 return NULL;
630
0f113f3e
MC
631 l = ERR_GET_LIB(e);
632 r = ERR_GET_REASON(e);
633 d.error = ERR_PACK(l, 0, r);
3e47caff 634 p = int_err_get_item(&d);
12a765a5 635 if (p == NULL) {
0f113f3e 636 d.error = ERR_PACK(0, 0, r);
3e47caff 637 p = int_err_get_item(&d);
0f113f3e
MC
638 }
639 return ((p == NULL) ? NULL : p->string);
11e85b89
DMSP
640#else
641 return NULL;
642#endif
0f113f3e 643}
d02b48c6 644
edb7afb8 645static void err_delete_thread_state(void *unused)
0f113f3e 646{
af6de400 647 ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
8509dcc9
AG
648 if (state == NULL)
649 return;
0f113f3e 650
8509dcc9
AG
651 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
652 ERR_STATE_free(state);
0f113f3e 653}
d02b48c6 654
00db8c60 655#ifndef OPENSSL_NO_DEPRECATED_1_1_0
21e00174
RL
656void ERR_remove_thread_state(void *dummy)
657{
658}
659#endif
660
00db8c60 661#ifndef OPENSSL_NO_DEPRECATED_1_0_0
4c329696 662void ERR_remove_state(unsigned long pid)
0f113f3e 663{
0f113f3e 664}
4c329696
GT
665#endif
666
c2e4e5d2 667DEFINE_RUN_ONCE_STATIC(err_do_init)
8509dcc9 668{
73bc5370 669 set_err_thread_local = 1;
c2e4e5d2 670 return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
8509dcc9
AG
671}
672
78715dcc 673ERR_STATE *ossl_err_get_state_int(void)
0f113f3e 674{
eb2b9892 675 ERR_STATE *state;
91c54730 676 int saveerrno = get_last_sys_error();
eb2b9892
BE
677
678 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
679 return NULL;
8509dcc9 680
c2e4e5d2
RL
681 if (!RUN_ONCE(&err_init, err_do_init))
682 return NULL;
8509dcc9
AG
683
684 state = CRYPTO_THREAD_get_local(&err_thread_local);
6b49b308
BE
685 if (state == (ERR_STATE*)-1)
686 return NULL;
8509dcc9
AG
687
688 if (state == NULL) {
6b49b308
BE
689 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
690 return NULL;
691
5639ee79
DDO
692 /* calling CRYPTO_zalloc(.., NULL, 0) prevents mem alloc error loop */
693 state = CRYPTO_zalloc(sizeof(*state), NULL, 0);
694 if (state == NULL) {
6b49b308 695 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
8509dcc9 696 return NULL;
cdb10bae 697 }
8509dcc9 698
6913f5fe 699 if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
6b49b308 700 || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
8509dcc9 701 ERR_STATE_free(state);
6b49b308 702 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
8509dcc9 703 return NULL;
0f113f3e 704 }
0fc32b07
MC
705
706 /* Ignore failures from these */
f672aee4 707 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
0f113f3e 708 }
8509dcc9 709
91c54730 710 set_sys_error(saveerrno);
8509dcc9 711 return state;
0f113f3e 712}
d02b48c6 713
936c2b9e 714#ifndef OPENSSL_NO_DEPRECATED_3_0
e5d4233f
RL
715ERR_STATE *ERR_get_state(void)
716{
78715dcc 717 return ossl_err_get_state_int();
e5d4233f
RL
718}
719#endif
720
721
eb2b9892
BE
722/*
723 * err_shelve_state returns the current thread local error state
724 * and freezes the error module until err_unshelve_state is called.
725 */
726int err_shelve_state(void **state)
727{
91c54730
RL
728 int saveerrno = get_last_sys_error();
729
df1f538f
VD
730 /*
731 * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
732 * via ossl_init_load_crypto_nodelete(), by which point the requested
733 * "base" initialization has already been performed, so the below call is a
734 * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
735 *
736 * If are no other valid callers of this function, the call below can be
737 * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
738 * potential uses that are not from inside OPENSSL_init_crypto(), then this
739 * call is needed, but some care is required to make sure that the re-entry
740 * remains a NOOP.
741 */
eb2b9892
BE
742 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
743 return 0;
744
745 if (!RUN_ONCE(&err_init, err_do_init))
746 return 0;
747
748 *state = CRYPTO_THREAD_get_local(&err_thread_local);
749 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
750 return 0;
751
91c54730 752 set_sys_error(saveerrno);
eb2b9892
BE
753 return 1;
754}
755
756/*
757 * err_unshelve_state restores the error state that was returned
758 * by err_shelve_state previously.
759 */
760void err_unshelve_state(void* state)
761{
762 if (state != (void*)-1)
763 CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
764}
765
6b691a5c 766int ERR_get_next_error_library(void)
0f113f3e 767{
3e47caff
RS
768 int ret;
769
eb2b9892 770 if (!RUN_ONCE(&err_string_init, do_err_strings_init))
c2e4e5d2 771 return 0;
7b8e12d2 772
cd3f8c1b
RS
773 if (!CRYPTO_THREAD_write_lock(err_string_lock))
774 return 0;
3e47caff 775 ret = int_err_library_number++;
7b8e12d2 776 CRYPTO_THREAD_unlock(err_string_lock);
3e47caff 777 return ret;
0f113f3e 778}
58964a49 779
10f8b368
RL
780static int err_set_error_data_int(char *data, size_t size, int flags,
781 int deallocate)
0f113f3e
MC
782{
783 ERR_STATE *es;
58964a49 784
78715dcc 785 es = ossl_err_get_state_int();
4fc426b7 786 if (es == NULL)
fa6b1ee1 787 return 0;
58964a49 788
10f8b368 789 err_clear_data(es, es->top, deallocate);
8a4dc425 790 err_set_data(es, es->top, data, size, flags);
fa6b1ee1
MC
791
792 return 1;
793}
794
795void ERR_set_error_data(char *data, int flags)
796{
797 /*
798 * This function is void so we cannot propagate the error return. Since it
799 * is also in the public API we can't change the return type.
10f8b368
RL
800 *
801 * We estimate the size of the data. If it's not flagged as allocated,
802 * then this is safe, and if it is flagged as allocated, then our size
803 * may be smaller than the actual allocation, but that doesn't matter
804 * too much, the buffer will remain untouched or will eventually be
805 * reallocated to a new size.
806 *
807 * callers should be advised that this function takes over ownership of
808 * the allocated memory, i.e. they can't count on the pointer to remain
809 * valid.
fa6b1ee1 810 */
10f8b368 811 err_set_error_data_int(data, strlen(data) + 1, flags, 1);
0f113f3e 812}
58964a49 813
4565c3e3 814void ERR_add_error_data(int num, ...)
0f113f3e
MC
815{
816 va_list args;
817 va_start(args, num);
818 ERR_add_error_vdata(num, args);
819 va_end(args);
820}
78c45722
DSH
821
822void ERR_add_error_vdata(int num, va_list args)
0f113f3e 823{
8908d18c 824 int i, len, size;
10f8b368
RL
825 int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
826 char *str, *arg;
8908d18c
RS
827 ERR_STATE *es;
828
829 /* Get the current error data; if an allocated string get it. */
78715dcc 830 es = ossl_err_get_state_int();
8908d18c
RS
831 if (es == NULL)
832 return;
833 i = es->top;
0f113f3e 834
10f8b368
RL
835 /*
836 * If err_data is allocated already, re-use the space.
837 * Otherwise, allocate a small new buffer.
838 */
839 if ((es->err_data_flags[i] & flags) == flags) {
840 str = es->err_data[i];
841 size = es->err_data_size[i];
842
843 /*
844 * To protect the string we just grabbed from tampering by other
845 * functions we may call, or to protect them from freeing a pointer
846 * that may no longer be valid at that point, we clear away the
847 * data pointer and the flags. We will set them again at the end
848 * of this function.
849 */
850 es->err_data[i] = NULL;
851 es->err_data_flags[i] = 0;
852 } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
0f113f3e 853 return;
10f8b368
RL
854 } else {
855 str[0] = '\0';
cdb10bae 856 }
10f8b368 857 len = strlen(str);
8908d18c 858
51fe9b00 859 while (--num >= 0) {
8908d18c
RS
860 arg = va_arg(args, char *);
861 if (arg == NULL)
862 arg = "<NULL>";
863 len += strlen(arg);
189dbdd9 864 if (len >= size) {
10f8b368
RL
865 char *p;
866
8908d18c 867 size = len + 20;
189dbdd9 868 p = OPENSSL_realloc(str, size);
0791bef0
RS
869 if (p == NULL) {
870 OPENSSL_free(str);
871 return;
0f113f3e 872 }
0791bef0 873 str = p;
0f113f3e 874 }
189dbdd9 875 OPENSSL_strlcat(str, arg, (size_t)size);
0f113f3e 876 }
10f8b368 877 if (!err_set_error_data_int(str, size, flags, 0))
fa6b1ee1 878 OPENSSL_free(str);
0f113f3e 879}
54f64516 880
f658a3b6
AP
881void err_clear_last_constant_time(int clear)
882{
883 ERR_STATE *es;
884 int top;
885
78715dcc 886 es = ossl_err_get_state_int();
f658a3b6
AP
887 if (es == NULL)
888 return;
889
890 top = es->top;
891
94dc53a3
BE
892 /*
893 * Flag error as cleared but remove it elsewhere to avoid two errors
894 * accessing the same error stack location, revealing timing information.
895 */
896 clear = constant_time_select_int(constant_time_eq_int(clear, 0),
897 0, ERR_FLAG_CLEAR);
898 es->err_flags[top] |= clear;
f658a3b6 899}