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