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