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 static 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"},
95     {ERR_R_EC_LIB, "EC lib"},
96     {ERR_R_BIO_LIB, "BIO lib"},
97     {ERR_R_PKCS7_LIB, "PKCS7 lib"},
98     {ERR_R_X509V3_LIB, "X509V3 lib"},
99     {ERR_R_ENGINE_LIB, "ENGINE lib"},
100     {ERR_R_UI_LIB, "UI lib"},
101     {ERR_R_OSSL_STORE_LIB, "STORE lib"},
102     {ERR_R_ECDSA_LIB, "ECDSA lib"},
103 
104     {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
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"},
114     {ERR_R_INIT_FAIL, "init fail"},
115     {ERR_R_OPERATION_FAIL, "operation fail"},
116     {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
117     {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
118 
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"},
128 
129     {ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition"},
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"},
132     {0, NULL},
133 };
134 #endif
135 
136 static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
137 static int set_err_thread_local;
138 static CRYPTO_THREAD_LOCAL err_thread_local;
139 
140 static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
141 static CRYPTO_RWLOCK *err_string_lock;
142 
143 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
144 
145 /*
146  * The internal state
147  */
148 
149 static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
150 static int int_err_library_number = ERR_LIB_USER;
151 
152 typedef enum ERR_GET_ACTION_e {
153     EV_POP, EV_PEEK, EV_PEEK_LAST
154 } ERR_GET_ACTION;
155 
156 static 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);
160 
err_string_data_hash(const ERR_STRING_DATA * a)161 static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
162 {
163     unsigned long ret, l;
164 
165     l = a->error;
166     ret = l ^ ERR_GET_LIB(l);
167     return (ret ^ ret % 19 * 13);
168 }
169 
err_string_data_cmp(const ERR_STRING_DATA * a,const ERR_STRING_DATA * b)170 static int err_string_data_cmp(const ERR_STRING_DATA *a,
171                                const ERR_STRING_DATA *b)
172 {
173     if (a->error == b->error)
174         return 0;
175     return a->error > b->error ? 1 : -1;
176 }
177 
int_err_get_item(const ERR_STRING_DATA * d)178 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
179 {
180     ERR_STRING_DATA *p = NULL;
181 
182     if (!CRYPTO_THREAD_read_lock(err_string_lock))
183         return NULL;
184     p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
185     CRYPTO_THREAD_unlock(err_string_lock);
186 
187     return p;
188 }
189 
ERR_STATE_free(ERR_STATE * s)190 static void ERR_STATE_free(ERR_STATE *s)
191 {
192     int i;
193 
194     if (s == NULL)
195         return;
196     for (i = 0; i < ERR_NUM_ERRORS; i++) {
197         err_clear(s, i, 1);
198     }
199     OPENSSL_free(s);
200 }
201 
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)202 DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
203 {
204     if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
205         return 0;
206     err_string_lock = CRYPTO_THREAD_lock_new();
207     if (err_string_lock == NULL)
208         return 0;
209     int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
210                                             err_string_data_cmp);
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;
217 }
218 
err_cleanup(void)219 void err_cleanup(void)
220 {
221     if (set_err_thread_local != 0)
222         CRYPTO_THREAD_cleanup_local(&err_thread_local);
223     CRYPTO_THREAD_lock_free(err_string_lock);
224     err_string_lock = NULL;
225     lh_ERR_STRING_DATA_free(int_error_hash);
226     int_error_hash = NULL;
227 }
228 
229 /*
230  * Legacy; pack in the library.
231  */
err_patch(int lib,ERR_STRING_DATA * str)232 static 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 /*
241  * Hash in |str| error strings. Assumes the RUN_ONCE was done.
242  */
err_load_strings(const ERR_STRING_DATA * str)243 static int err_load_strings(const ERR_STRING_DATA *str)
244 {
245     if (!CRYPTO_THREAD_write_lock(err_string_lock))
246         return 0;
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;
252 }
253 
ossl_err_load_ERR_strings(void)254 int ossl_err_load_ERR_strings(void)
255 {
256 #ifndef OPENSSL_NO_ERR
257     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
258         return 0;
259 
260     err_load_strings(ERR_str_libraries);
261     err_load_strings(ERR_str_reasons);
262 #endif
263     return 1;
264 }
265 
ERR_load_strings(int lib,ERR_STRING_DATA * str)266 int ERR_load_strings(int lib, ERR_STRING_DATA *str)
267 {
268     if (ossl_err_load_ERR_strings() == 0)
269         return 0;
270 
271     err_patch(lib, str);
272     err_load_strings(str);
273     return 1;
274 }
275 
ERR_load_strings_const(const ERR_STRING_DATA * str)276 int ERR_load_strings_const(const ERR_STRING_DATA *str)
277 {
278     if (ossl_err_load_ERR_strings() == 0)
279         return 0;
280     err_load_strings(str);
281     return 1;
282 }
283 
ERR_unload_strings(int lib,ERR_STRING_DATA * str)284 int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
285 {
286     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
287         return 0;
288 
289     if (!CRYPTO_THREAD_write_lock(err_string_lock))
290         return 0;
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);
297     CRYPTO_THREAD_unlock(err_string_lock);
298 
299     return 1;
300 }
301 
err_free_strings_int(void)302 void err_free_strings_int(void)
303 {
304     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
305         return;
306 }
307 
308 /********************************************************/
309 
ERR_clear_error(void)310 void ERR_clear_error(void)
311 {
312     int i;
313     ERR_STATE *es;
314 
315     es = ossl_err_get_state_int();
316     if (es == NULL)
317         return;
318 
319     for (i = 0; i < ERR_NUM_ERRORS; i++) {
320         err_clear(es, i, 0);
321     }
322     es->top = es->bottom = 0;
323 }
324 
ERR_get_error(void)325 unsigned long ERR_get_error(void)
326 {
327     return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
328 }
329 
ERR_get_error_all(const char ** file,int * line,const char ** func,const char ** data,int * flags)330 unsigned long ERR_get_error_all(const char **file, int *line,
331                                 const char **func,
332                                 const char **data, int *flags)
333 {
334     return get_error_values(EV_POP, file, line, func, data, flags);
335 }
336 
337 #ifndef OPENSSL_NO_DEPRECATED_3_0
ERR_get_error_line(const char ** file,int * line)338 unsigned 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 
ERR_get_error_line_data(const char ** file,int * line,const char ** data,int * flags)343 unsigned long ERR_get_error_line_data(const char **file, int *line,
344                                       const char **data, int *flags)
345 {
346     return get_error_values(EV_POP, file, line, NULL, data, flags);
347 }
348 #endif
349 
ERR_peek_error(void)350 unsigned long ERR_peek_error(void)
351 {
352     return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
353 }
354 
ERR_peek_error_line(const char ** file,int * line)355 unsigned long ERR_peek_error_line(const char **file, int *line)
356 {
357     return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
358 }
359 
ERR_peek_error_func(const char ** func)360 unsigned long ERR_peek_error_func(const char **func)
361 {
362     return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
363 }
364 
ERR_peek_error_data(const char ** data,int * flags)365 unsigned long ERR_peek_error_data(const char **data, int *flags)
366 {
367     return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
368 }
369 
ERR_peek_error_all(const char ** file,int * line,const char ** func,const char ** data,int * flags)370 unsigned long ERR_peek_error_all(const char **file, int *line,
371                                  const char **func,
372                                  const char **data, int *flags)
373 {
374     return get_error_values(EV_PEEK, file, line, func, data, flags);
375 }
376 
377 #ifndef OPENSSL_NO_DEPRECATED_3_0
ERR_peek_error_line_data(const char ** file,int * line,const char ** data,int * flags)378 unsigned long ERR_peek_error_line_data(const char **file, int *line,
379                                        const char **data, int *flags)
380 {
381     return get_error_values(EV_PEEK, file, line, NULL, data, flags);
382 }
383 #endif
384 
ERR_peek_last_error(void)385 unsigned long ERR_peek_last_error(void)
386 {
387     return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
388 }
389 
ERR_peek_last_error_line(const char ** file,int * line)390 unsigned long ERR_peek_last_error_line(const char **file, int *line)
391 {
392     return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
393 }
394 
ERR_peek_last_error_func(const char ** func)395 unsigned long ERR_peek_last_error_func(const char **func)
396 {
397     return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
398 }
399 
ERR_peek_last_error_data(const char ** data,int * flags)400 unsigned long ERR_peek_last_error_data(const char **data, int *flags)
401 {
402     return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
403 }
404 
ERR_peek_last_error_all(const char ** file,int * line,const char ** func,const char ** data,int * flags)405 unsigned long ERR_peek_last_error_all(const char **file, int *line,
406                                       const char **func,
407                                       const char **data, int *flags)
408 {
409     return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
410 }
411 
412 #ifndef OPENSSL_NO_DEPRECATED_3_0
ERR_peek_last_error_line_data(const char ** file,int * line,const char ** data,int * flags)413 unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
414                                             const char **data, int *flags)
415 {
416     return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
417 }
418 #endif
419 
get_error_values(ERR_GET_ACTION g,const char ** file,int * line,const char ** func,const char ** data,int * flags)420 static unsigned long get_error_values(ERR_GET_ACTION g,
421                                       const char **file, int *line,
422                                       const char **func,
423                                       const char **data, int *flags)
424 {
425     int i = 0;
426     ERR_STATE *es;
427     unsigned long ret;
428 
429     es = ossl_err_get_state_int();
430     if (es == NULL)
431         return 0;
432 
433     /*
434      * Clear anything that should have been cleared earlier. We do this
435      * here because this doesn't have constant-time issues.
436      */
437     while (es->bottom != es->top) {
438         if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
439             err_clear(es, es->top, 0);
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;
446             err_clear(es, es->bottom, 0);
447             continue;
448         }
449         break;
450     }
451 
452     /* If everything has been cleared, the stack is empty. */
453     if (es->bottom == es->top)
454         return 0;
455 
456     /* Which error, the top of stack (latest one) or the first one? */
457     if (g == EV_PEEK_LAST)
458         i = es->top;
459     else
460         i = (es->bottom + 1) % ERR_NUM_ERRORS;
461 
462     ret = es->err_buffer[i];
463     if (g == EV_POP) {
464         es->bottom = i;
465         es->err_buffer[i] = 0;
466     }
467 
468     if (file != NULL) {
469         *file = es->err_file[i];
470         if (*file == NULL)
471             *file = "";
472     }
473     if (line != NULL)
474         *line = es->err_line[i];
475     if (func != NULL) {
476         *func = es->err_func[i];
477         if (*func == NULL)
478             *func = "";
479     }
480     if (flags != NULL)
481         *flags = es->err_data_flags[i];
482     if (data == NULL) {
483         if (g == EV_POP) {
484             err_clear_data(es, i, 0);
485         }
486     } else {
487         *data = es->err_data[i];
488         if (*data == NULL) {
489             *data = "";
490             if (flags != NULL)
491                 *flags = 0;
492         }
493     }
494     return ret;
495 }
496 
ossl_err_string_int(unsigned long e,const char * func,char * buf,size_t len)497 void ossl_err_string_int(unsigned long e, const char *func,
498                          char *buf, size_t len)
499 {
500     char lsbuf[64], rsbuf[256];
501     const char *ls, *rs = NULL;
502     unsigned long l, r;
503 
504     if (len == 0)
505         return;
506 
507     l = ERR_GET_LIB(e);
508     ls = ERR_lib_error_string(e);
509     if (ls == NULL) {
510         BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
511         ls = lsbuf;
512     }
513 
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      */
520     r = ERR_GET_REASON(e);
521 #ifndef OPENSSL_NO_ERR
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     }
528 #endif
529     if (rs == NULL) {
530         BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
531         rs = rsbuf;
532     }
533 
534     BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
535     if (strlen(buf) == len - 1) {
536         /* Didn't fit; use a minimal format. */
537         BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
538     }
539 }
540 
541 
ERR_error_string_n(unsigned long e,char * buf,size_t len)542 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
543 {
544     ossl_err_string_int(e, "", buf, len);
545 }
546 
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  */
ERR_error_string(unsigned long e,char * ret)551 char *ERR_error_string(unsigned long e, char *ret)
552 {
553     static char buf[256];
554 
555     if (ret == NULL)
556         ret = buf;
557     ERR_error_string_n(e, ret, (int)sizeof(buf));
558     return ret;
559 }
560 
ERR_lib_error_string(unsigned long e)561 const char *ERR_lib_error_string(unsigned long e)
562 {
563     ERR_STRING_DATA d, *p;
564     unsigned long l;
565 
566     if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
567         return NULL;
568     }
569 
570     l = ERR_GET_LIB(e);
571     d.error = ERR_PACK(l, 0, 0);
572     p = int_err_get_item(&d);
573     return ((p == NULL) ? NULL : p->string);
574 }
575 
576 #ifndef OPENSSL_NO_DEPRECATED_3_0
ERR_func_error_string(unsigned long e)577 const char *ERR_func_error_string(unsigned long e)
578 {
579     return NULL;
580 }
581 #endif
582 
ERR_reason_error_string(unsigned long e)583 const char *ERR_reason_error_string(unsigned long e)
584 {
585     ERR_STRING_DATA d, *p = NULL;
586     unsigned long l, r;
587 
588     if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
589         return NULL;
590     }
591 
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 
600     l = ERR_GET_LIB(e);
601     r = ERR_GET_REASON(e);
602     d.error = ERR_PACK(l, 0, r);
603     p = int_err_get_item(&d);
604     if (p == NULL) {
605         d.error = ERR_PACK(0, 0, r);
606         p = int_err_get_item(&d);
607     }
608     return ((p == NULL) ? NULL : p->string);
609 }
610 
err_delete_thread_state(void * unused)611 static void err_delete_thread_state(void *unused)
612 {
613     ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
614     if (state == NULL)
615         return;
616 
617     CRYPTO_THREAD_set_local(&err_thread_local, NULL);
618     ERR_STATE_free(state);
619 }
620 
621 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
ERR_remove_thread_state(void * dummy)622 void ERR_remove_thread_state(void *dummy)
623 {
624 }
625 #endif
626 
627 #ifndef OPENSSL_NO_DEPRECATED_1_0_0
ERR_remove_state(unsigned long pid)628 void ERR_remove_state(unsigned long pid)
629 {
630 }
631 #endif
632 
DEFINE_RUN_ONCE_STATIC(err_do_init)633 DEFINE_RUN_ONCE_STATIC(err_do_init)
634 {
635     set_err_thread_local = 1;
636     return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
637 }
638 
ossl_err_get_state_int(void)639 ERR_STATE *ossl_err_get_state_int(void)
640 {
641     ERR_STATE *state;
642     int saveerrno = get_last_sys_error();
643 
644     if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
645         return NULL;
646 
647     if (!RUN_ONCE(&err_init, err_do_init))
648         return NULL;
649 
650     state = CRYPTO_THREAD_get_local(&err_thread_local);
651     if (state == (ERR_STATE*)-1)
652         return NULL;
653 
654     if (state == NULL) {
655         if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
656             return NULL;
657 
658         if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
659             CRYPTO_THREAD_set_local(&err_thread_local, NULL);
660             return NULL;
661         }
662 
663         if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
664                 || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
665             ERR_STATE_free(state);
666             CRYPTO_THREAD_set_local(&err_thread_local, NULL);
667             return NULL;
668         }
669 
670         /* Ignore failures from these */
671         OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
672     }
673 
674     set_sys_error(saveerrno);
675     return state;
676 }
677 
678 #ifndef OPENSSL_NO_DEPRECATED_3_0
ERR_get_state(void)679 ERR_STATE *ERR_get_state(void)
680 {
681     return ossl_err_get_state_int();
682 }
683 #endif
684 
685 
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  */
err_shelve_state(void ** state)690 int err_shelve_state(void **state)
691 {
692     int saveerrno = get_last_sys_error();
693 
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      */
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 
716     set_sys_error(saveerrno);
717     return 1;
718 }
719 
720 /*
721  * err_unshelve_state restores the error state that was returned
722  * by err_shelve_state previously.
723  */
err_unshelve_state(void * state)724 void err_unshelve_state(void* state)
725 {
726     if (state != (void*)-1)
727         CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
728 }
729 
ERR_get_next_error_library(void)730 int ERR_get_next_error_library(void)
731 {
732     int ret;
733 
734     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
735         return 0;
736 
737     if (!CRYPTO_THREAD_write_lock(err_string_lock))
738         return 0;
739     ret = int_err_library_number++;
740     CRYPTO_THREAD_unlock(err_string_lock);
741     return ret;
742 }
743 
err_set_error_data_int(char * data,size_t size,int flags,int deallocate)744 static int err_set_error_data_int(char *data, size_t size, int flags,
745                                   int deallocate)
746 {
747     ERR_STATE *es;
748 
749     es = ossl_err_get_state_int();
750     if (es == NULL)
751         return 0;
752 
753     err_clear_data(es, es->top, deallocate);
754     err_set_data(es, es->top, data, size, flags);
755 
756     return 1;
757 }
758 
ERR_set_error_data(char * data,int flags)759 void 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.
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.
774      */
775     err_set_error_data_int(data, strlen(data) + 1, flags, 1);
776 }
777 
ERR_add_error_data(int num,...)778 void ERR_add_error_data(int num, ...)
779 {
780     va_list args;
781     va_start(args, num);
782     ERR_add_error_vdata(num, args);
783     va_end(args);
784 }
785 
ERR_add_error_vdata(int num,va_list args)786 void ERR_add_error_vdata(int num, va_list args)
787 {
788     int i, len, size;
789     int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
790     char *str, *arg;
791     ERR_STATE *es;
792 
793     /* Get the current error data; if an allocated string get it. */
794     es = ossl_err_get_state_int();
795     if (es == NULL)
796         return;
797     i = es->top;
798 
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) {
817         return;
818     } else {
819         str[0] = '\0';
820     }
821     len = strlen(str);
822 
823     while (--num >= 0) {
824         arg = va_arg(args, char *);
825         if (arg == NULL)
826             arg = "<NULL>";
827         len += strlen(arg);
828         if (len >= size) {
829             char *p;
830 
831             size = len + 20;
832             p = OPENSSL_realloc(str, size);
833             if (p == NULL) {
834                 OPENSSL_free(str);
835                 return;
836             }
837             str = p;
838         }
839         OPENSSL_strlcat(str, arg, (size_t)size);
840     }
841     if (!err_set_error_data_int(str, size, flags, 0))
842         OPENSSL_free(str);
843 }
844 
ERR_set_mark(void)845 int ERR_set_mark(void)
846 {
847     ERR_STATE *es;
848 
849     es = ossl_err_get_state_int();
850     if (es == NULL)
851         return 0;
852 
853     if (es->bottom == es->top)
854         return 0;
855     es->err_marks[es->top]++;
856     return 1;
857 }
858 
ERR_pop_to_mark(void)859 int ERR_pop_to_mark(void)
860 {
861     ERR_STATE *es;
862 
863     es = ossl_err_get_state_int();
864     if (es == NULL)
865         return 0;
866 
867     while (es->bottom != es->top
868            && es->err_marks[es->top] == 0) {
869         err_clear(es, es->top, 0);
870         es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
871     }
872 
873     if (es->bottom == es->top)
874         return 0;
875     es->err_marks[es->top]--;
876     return 1;
877 }
878 
ERR_clear_last_mark(void)879 int ERR_clear_last_mark(void)
880 {
881     ERR_STATE *es;
882     int top;
883 
884     es = ossl_err_get_state_int();
885     if (es == NULL)
886         return 0;
887 
888     top = es->top;
889     while (es->bottom != top
890            && es->err_marks[top] == 0) {
891         top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
892     }
893 
894     if (es->bottom == top)
895         return 0;
896     es->err_marks[top]--;
897     return 1;
898 }
899 
err_clear_last_constant_time(int clear)900 void err_clear_last_constant_time(int clear)
901 {
902     ERR_STATE *es;
903     int top;
904 
905     es = ossl_err_get_state_int();
906     if (es == NULL)
907         return;
908 
909     top = es->top;
910 
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;
918 }
919