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