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