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