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