Fix error number clash
[openssl.git] / crypto / err / err.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include "internal/cryptlib_int.h"
14 #include "internal/err.h"
15 #include "internal/err_int.h"
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
18 #include <openssl/buffer.h>
19 #include <openssl/bio.h>
20 #include <openssl/opensslconf.h>
21 #include "internal/thread_once.h"
22
23 static int err_load_strings(const ERR_STRING_DATA *str);
24
25 static void ERR_STATE_free(ERR_STATE *s);
26 #ifndef OPENSSL_NO_ERR
27 static ERR_STRING_DATA ERR_str_libraries[] = {
28     {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
29     {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
30     {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
31     {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
32     {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
33     {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
34     {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
35     {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
36     {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
37     {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
38     {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
39     {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
40     {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
41     {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
42     {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
43     {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
44     {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
45     {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
46     {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
47     {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
48     {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
49     {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
50     {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
51     {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
52     {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
53     {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
54     {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
55     {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
56     {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
57     {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
58     {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
59     {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
60     {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
61     {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
62     {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
63     {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
64     {0, NULL},
65 };
66
67 static ERR_STRING_DATA ERR_str_functs[] = {
68     {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"},
69     {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"},
70     {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
71     {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"},
72     {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"},
73     {ERR_PACK(0, SYS_F_BIND, 0), "bind"},
74     {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"},
75     {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"},
76 # ifdef OPENSSL_SYS_WINDOWS
77     {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"},
78 # endif
79     {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"},
80     {ERR_PACK(0, SYS_F_FREAD, 0), "fread"},
81     {ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"},
82     {ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"},
83     {ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"},
84     {ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"},
85     {ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"},
86     {ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"},
87     {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"},
88     {ERR_PACK(0, SYS_F_OPEN, 0), "open"},
89     {ERR_PACK(0, SYS_F_CLOSE, 0), "close"},
90     {ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"},
91     {ERR_PACK(0, SYS_F_STAT, 0), "stat"},
92     {ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"},
93     {ERR_PACK(0, SYS_F_FSTAT, 0), "fstat"},
94     {0, NULL},
95 };
96
97 static ERR_STRING_DATA ERR_str_reasons[] = {
98     {ERR_R_SYS_LIB, "system lib"},
99     {ERR_R_BN_LIB, "BN lib"},
100     {ERR_R_RSA_LIB, "RSA lib"},
101     {ERR_R_DH_LIB, "DH lib"},
102     {ERR_R_EVP_LIB, "EVP lib"},
103     {ERR_R_BUF_LIB, "BUF lib"},
104     {ERR_R_OBJ_LIB, "OBJ lib"},
105     {ERR_R_PEM_LIB, "PEM lib"},
106     {ERR_R_DSA_LIB, "DSA lib"},
107     {ERR_R_X509_LIB, "X509 lib"},
108     {ERR_R_ASN1_LIB, "ASN1 lib"},
109     {ERR_R_EC_LIB, "EC lib"},
110     {ERR_R_BIO_LIB, "BIO lib"},
111     {ERR_R_PKCS7_LIB, "PKCS7 lib"},
112     {ERR_R_X509V3_LIB, "X509V3 lib"},
113     {ERR_R_ENGINE_LIB, "ENGINE lib"},
114     {ERR_R_UI_LIB, "UI lib"},
115     {ERR_R_OSSL_STORE_LIB, "STORE lib"},
116     {ERR_R_ECDSA_LIB, "ECDSA lib"},
117
118     {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
119     {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
120
121     {ERR_R_FATAL, "fatal"},
122     {ERR_R_MALLOC_FAILURE, "malloc failure"},
123     {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
124      "called a function you should not call"},
125     {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
126     {ERR_R_INTERNAL_ERROR, "internal error"},
127     {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
128     {ERR_R_INIT_FAIL, "init fail"},
129     {ERR_R_OPERATION_FAIL, "operation fail"},
130
131     {0, NULL},
132 };
133 #endif
134
135 static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
136 static int set_err_thread_local;
137 static CRYPTO_THREAD_LOCAL err_thread_local;
138
139 static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
140 static CRYPTO_RWLOCK *err_string_lock;
141
142 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
143
144 /*
145  * The internal state
146  */
147
148 static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
149 static int int_err_library_number = ERR_LIB_USER;
150
151 static unsigned long get_error_values(int inc, int top, const char **file,
152                                       int *line, const char **data,
153                                       int *flags);
154
155 static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
156 {
157     unsigned long ret, l;
158
159     l = a->error;
160     ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
161     return (ret ^ ret % 19 * 13);
162 }
163
164 static int err_string_data_cmp(const ERR_STRING_DATA *a,
165                                const ERR_STRING_DATA *b)
166 {
167     if (a->error == b->error)
168         return 0;
169     return a->error > b->error ? 1 : -1;
170 }
171
172 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
173 {
174     ERR_STRING_DATA *p = NULL;
175
176     CRYPTO_THREAD_read_lock(err_string_lock);
177     p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
178     CRYPTO_THREAD_unlock(err_string_lock);
179
180     return p;
181 }
182
183 #ifndef OPENSSL_NO_ERR
184 # define NUM_SYS_STR_REASONS 127
185 # define LEN_SYS_STR_REASON 32
186
187 static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
188 /*
189  * SYS_str_reasons is filled with copies of strerror() results at
190  * initialization. 'errno' values up to 127 should cover all usual errors,
191  * others will be displayed numerically by ERR_error_string. It is crucial
192  * that we have something for each reason code that occurs in
193  * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
194  * which always gets an errno value and never one of those 'standard' reason
195  * codes.
196  */
197
198 static void build_SYS_str_reasons(void)
199 {
200     /* OPENSSL_malloc cannot be used here, use static storage instead */
201     static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
202     static int init = 1;
203     int i;
204
205     CRYPTO_THREAD_write_lock(err_string_lock);
206     if (!init) {
207         CRYPTO_THREAD_unlock(err_string_lock);
208         return;
209     }
210
211     for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
212         ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
213
214         str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
215         if (str->string == NULL) {
216             char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
217             if (openssl_strerror_r(i, *dest, sizeof(*dest)))
218                 str->string = *dest;
219         }
220         if (str->string == NULL)
221             str->string = "unknown";
222     }
223
224     /*
225      * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
226      * required by ERR_load_strings.
227      */
228
229     init = 0;
230
231     CRYPTO_THREAD_unlock(err_string_lock);
232     err_load_strings(SYS_str_reasons);
233 }
234 #endif
235
236 #define err_clear_data(p, i) \
237         do { \
238             if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
239                 OPENSSL_free((p)->err_data[i]); \
240                 (p)->err_data[i] = NULL; \
241             } \
242             (p)->err_data_flags[i] = 0; \
243         } while (0)
244
245 #define err_clear(p, i) \
246         do { \
247             err_clear_data(p, i); \
248             (p)->err_flags[i] = 0; \
249             (p)->err_buffer[i] = 0; \
250             (p)->err_file[i] = NULL; \
251             (p)->err_line[i] = -1; \
252         } while (0)
253
254 static void ERR_STATE_free(ERR_STATE *s)
255 {
256     int i;
257
258     if (s == NULL)
259         return;
260     for (i = 0; i < ERR_NUM_ERRORS; i++) {
261         err_clear_data(s, i);
262     }
263     OPENSSL_free(s);
264 }
265
266 DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
267 {
268     if (!OPENSSL_init_crypto(0, NULL))
269         return 0;
270     err_string_lock = CRYPTO_THREAD_lock_new();
271     if (err_string_lock == NULL)
272         return 0;
273     int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
274                                             err_string_data_cmp);
275     if (int_error_hash == NULL) {
276         CRYPTO_THREAD_lock_free(err_string_lock);
277         err_string_lock = NULL;
278         return 0;
279     }
280     return 1;
281 }
282
283 void err_cleanup(void)
284 {
285     if (set_err_thread_local != 0)
286         CRYPTO_THREAD_cleanup_local(&err_thread_local);
287     CRYPTO_THREAD_lock_free(err_string_lock);
288     err_string_lock = NULL;
289     lh_ERR_STRING_DATA_free(int_error_hash);
290     int_error_hash = NULL;
291 }
292
293 /*
294  * Legacy; pack in the library.
295  */
296 static void err_patch(int lib, ERR_STRING_DATA *str)
297 {
298     unsigned long plib = ERR_PACK(lib, 0, 0);
299
300     for (; str->error != 0; str++)
301         str->error |= plib;
302 }
303
304 /*
305  * Hash in |str| error strings. Assumes the URN_ONCE was done.
306  */
307 static int err_load_strings(const ERR_STRING_DATA *str)
308 {
309     CRYPTO_THREAD_write_lock(err_string_lock);
310     for (; str->error; str++)
311         (void)lh_ERR_STRING_DATA_insert(int_error_hash,
312                                        (ERR_STRING_DATA *)str);
313     CRYPTO_THREAD_unlock(err_string_lock);
314     return 1;
315 }
316
317 int ERR_load_ERR_strings(void)
318 {
319 #ifndef OPENSSL_NO_ERR
320     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
321         return 0;
322
323     err_load_strings(ERR_str_libraries);
324     err_load_strings(ERR_str_reasons);
325     err_patch(ERR_LIB_SYS, ERR_str_functs);
326     err_load_strings(ERR_str_functs);
327     build_SYS_str_reasons();
328 #endif
329     return 1;
330 }
331
332 int ERR_load_strings(int lib, ERR_STRING_DATA *str)
333 {
334     if (ERR_load_ERR_strings() == 0)
335         return 0;
336
337     err_patch(lib, str);
338     err_load_strings(str);
339     return 1;
340 }
341
342 int ERR_load_strings_const(const ERR_STRING_DATA *str)
343 {
344     if (ERR_load_ERR_strings() == 0)
345         return 0;
346     err_load_strings(str);
347     return 1;
348 }
349
350 int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
351 {
352     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
353         return 0;
354
355     CRYPTO_THREAD_write_lock(err_string_lock);
356     /*
357      * We don't need to ERR_PACK the lib, since that was done (to
358      * the table) when it was loaded.
359      */
360     for (; str->error; str++)
361         (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
362     CRYPTO_THREAD_unlock(err_string_lock);
363
364     return 1;
365 }
366
367 void err_free_strings_int(void)
368 {
369     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
370         return;
371 }
372
373 /********************************************************/
374
375 void ERR_put_error(int lib, int func, int reason, const char *file, int line)
376 {
377     ERR_STATE *es;
378
379 #ifdef _OSD_POSIX
380     /*
381      * In the BS2000-OSD POSIX subsystem, the compiler generates path names
382      * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
383      * something sensible. @@@ We shouldn't modify a const string, though.
384      */
385     if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
386         char *end;
387
388         /* Skip the "*POSIX(" prefix */
389         file += sizeof("*POSIX(") - 1;
390         end = &file[strlen(file) - 1];
391         if (*end == ')')
392             *end = '\0';
393         /* Optional: use the basename of the path only. */
394         if ((end = strrchr(file, '/')) != NULL)
395             file = &end[1];
396     }
397 #endif
398     es = ERR_get_state();
399     if (es == NULL)
400         return;
401
402     es->top = (es->top + 1) % ERR_NUM_ERRORS;
403     if (es->top == es->bottom)
404         es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
405     es->err_flags[es->top] = 0;
406     es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
407     es->err_file[es->top] = file;
408     es->err_line[es->top] = line;
409     err_clear_data(es, es->top);
410 }
411
412 void ERR_clear_error(void)
413 {
414     int i;
415     ERR_STATE *es;
416
417     es = ERR_get_state();
418     if (es == NULL)
419         return;
420
421     for (i = 0; i < ERR_NUM_ERRORS; i++) {
422         err_clear(es, i);
423     }
424     es->top = es->bottom = 0;
425 }
426
427 unsigned long ERR_get_error(void)
428 {
429     return get_error_values(1, 0, NULL, NULL, NULL, NULL);
430 }
431
432 unsigned long ERR_get_error_line(const char **file, int *line)
433 {
434     return get_error_values(1, 0, file, line, NULL, NULL);
435 }
436
437 unsigned long ERR_get_error_line_data(const char **file, int *line,
438                                       const char **data, int *flags)
439 {
440     return get_error_values(1, 0, file, line, data, flags);
441 }
442
443 unsigned long ERR_peek_error(void)
444 {
445     return get_error_values(0, 0, NULL, NULL, NULL, NULL);
446 }
447
448 unsigned long ERR_peek_error_line(const char **file, int *line)
449 {
450     return get_error_values(0, 0, file, line, NULL, NULL);
451 }
452
453 unsigned long ERR_peek_error_line_data(const char **file, int *line,
454                                        const char **data, int *flags)
455 {
456     return get_error_values(0, 0, file, line, data, flags);
457 }
458
459 unsigned long ERR_peek_last_error(void)
460 {
461     return get_error_values(0, 1, NULL, NULL, NULL, NULL);
462 }
463
464 unsigned long ERR_peek_last_error_line(const char **file, int *line)
465 {
466     return get_error_values(0, 1, file, line, NULL, NULL);
467 }
468
469 unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
470                                             const char **data, int *flags)
471 {
472     return get_error_values(0, 1, file, line, data, flags);
473 }
474
475 static unsigned long get_error_values(int inc, int top, const char **file,
476                                       int *line, const char **data,
477                                       int *flags)
478 {
479     int i = 0;
480     ERR_STATE *es;
481     unsigned long ret;
482
483     es = ERR_get_state();
484     if (es == NULL)
485         return 0;
486
487     if (inc && top) {
488         if (file)
489             *file = "";
490         if (line)
491             *line = 0;
492         if (data)
493             *data = "";
494         if (flags)
495             *flags = 0;
496
497         return ERR_R_INTERNAL_ERROR;
498     }
499
500     if (es->bottom == es->top)
501         return 0;
502     if (top)
503         i = es->top;            /* last error */
504     else
505         i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
506
507     ret = es->err_buffer[i];
508     if (inc) {
509         es->bottom = i;
510         es->err_buffer[i] = 0;
511     }
512
513     if (file != NULL && line != NULL) {
514         if (es->err_file[i] == NULL) {
515             *file = "NA";
516             *line = 0;
517         } else {
518             *file = es->err_file[i];
519             *line = es->err_line[i];
520         }
521     }
522
523     if (data == NULL) {
524         if (inc) {
525             err_clear_data(es, i);
526         }
527     } else {
528         if (es->err_data[i] == NULL) {
529             *data = "";
530             if (flags != NULL)
531                 *flags = 0;
532         } else {
533             *data = es->err_data[i];
534             if (flags != NULL)
535                 *flags = es->err_data_flags[i];
536         }
537     }
538     return ret;
539 }
540
541 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
542 {
543     char lsbuf[64], fsbuf[64], rsbuf[64];
544     const char *ls, *fs, *rs;
545     unsigned long l, f, r;
546
547     if (len == 0)
548         return;
549
550     l = ERR_GET_LIB(e);
551     ls = ERR_lib_error_string(e);
552     if (ls == NULL) {
553         BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
554         ls = lsbuf;
555     }
556
557     fs = ERR_func_error_string(e);
558     f = ERR_GET_FUNC(e);
559     if (fs == NULL) {
560         BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
561         fs = fsbuf;
562     }
563
564     rs = ERR_reason_error_string(e);
565     r = ERR_GET_REASON(e);
566     if (rs == NULL) {
567         BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
568         rs = rsbuf;
569     }
570
571     BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
572     if (strlen(buf) == len - 1) {
573         /* Didn't fit; use a minimal format. */
574         BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
575     }
576 }
577
578 /*
579  * ERR_error_string_n should be used instead for ret != NULL as
580  * ERR_error_string cannot know how large the buffer is
581  */
582 char *ERR_error_string(unsigned long e, char *ret)
583 {
584     static char buf[256];
585
586     if (ret == NULL)
587         ret = buf;
588     ERR_error_string_n(e, ret, (int)sizeof(buf));
589     return ret;
590 }
591
592 const char *ERR_lib_error_string(unsigned long e)
593 {
594     ERR_STRING_DATA d, *p;
595     unsigned long l;
596
597     if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
598         return NULL;
599     }
600
601     l = ERR_GET_LIB(e);
602     d.error = ERR_PACK(l, 0, 0);
603     p = int_err_get_item(&d);
604     return ((p == NULL) ? NULL : p->string);
605 }
606
607 const char *ERR_func_error_string(unsigned long e)
608 {
609     ERR_STRING_DATA d, *p;
610     unsigned long l, f;
611
612     if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
613         return NULL;
614     }
615
616     l = ERR_GET_LIB(e);
617     f = ERR_GET_FUNC(e);
618     d.error = ERR_PACK(l, f, 0);
619     p = int_err_get_item(&d);
620     return ((p == NULL) ? NULL : p->string);
621 }
622
623 const char *ERR_reason_error_string(unsigned long e)
624 {
625     ERR_STRING_DATA d, *p = NULL;
626     unsigned long l, r;
627
628     if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
629         return NULL;
630     }
631
632     l = ERR_GET_LIB(e);
633     r = ERR_GET_REASON(e);
634     d.error = ERR_PACK(l, 0, r);
635     p = int_err_get_item(&d);
636     if (!p) {
637         d.error = ERR_PACK(0, 0, r);
638         p = int_err_get_item(&d);
639     }
640     return ((p == NULL) ? NULL : p->string);
641 }
642
643 void err_delete_thread_state(void)
644 {
645     ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
646     if (state == NULL)
647         return;
648
649     CRYPTO_THREAD_set_local(&err_thread_local, NULL);
650     ERR_STATE_free(state);
651 }
652
653 #if OPENSSL_API_COMPAT < 0x10100000L
654 void ERR_remove_thread_state(void *dummy)
655 {
656 }
657 #endif
658
659 #if OPENSSL_API_COMPAT < 0x10000000L
660 void ERR_remove_state(unsigned long pid)
661 {
662 }
663 #endif
664
665 DEFINE_RUN_ONCE_STATIC(err_do_init)
666 {
667     set_err_thread_local = 1;
668     return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
669 }
670
671 ERR_STATE *ERR_get_state(void)
672 {
673     ERR_STATE *state;
674
675     if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
676         return NULL;
677
678     if (!RUN_ONCE(&err_init, err_do_init))
679         return NULL;
680
681     state = CRYPTO_THREAD_get_local(&err_thread_local);
682     if (state == (ERR_STATE*)-1)
683         return NULL;
684
685     if (state == NULL) {
686         if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
687             return NULL;
688
689         if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
690             CRYPTO_THREAD_set_local(&err_thread_local, NULL);
691             return NULL;
692         }
693
694         if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
695                 || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
696             ERR_STATE_free(state);
697             CRYPTO_THREAD_set_local(&err_thread_local, NULL);
698             return NULL;
699         }
700
701         /* Ignore failures from these */
702         OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
703     }
704
705     return state;
706 }
707
708 /*
709  * err_shelve_state returns the current thread local error state
710  * and freezes the error module until err_unshelve_state is called.
711  */
712 int err_shelve_state(void **state)
713 {
714     if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
715         return 0;
716
717     if (!RUN_ONCE(&err_init, err_do_init))
718         return 0;
719
720     *state = CRYPTO_THREAD_get_local(&err_thread_local);
721     if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
722         return 0;
723
724     return 1;
725 }
726
727 /*
728  * err_unshelve_state restores the error state that was returned
729  * by err_shelve_state previously.
730  */
731 void err_unshelve_state(void* state)
732 {
733     if (state != (void*)-1)
734         CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
735 }
736
737 int ERR_get_next_error_library(void)
738 {
739     int ret;
740
741     if (!RUN_ONCE(&err_string_init, do_err_strings_init))
742         return 0;
743
744     CRYPTO_THREAD_write_lock(err_string_lock);
745     ret = int_err_library_number++;
746     CRYPTO_THREAD_unlock(err_string_lock);
747     return ret;
748 }
749
750 void ERR_set_error_data(char *data, int flags)
751 {
752     ERR_STATE *es;
753     int i;
754
755     es = ERR_get_state();
756     if (es == NULL)
757         return;
758
759     i = es->top;
760
761     err_clear_data(es, i);
762     es->err_data[i] = data;
763     es->err_data_flags[i] = flags;
764 }
765
766 void ERR_add_error_data(int num, ...)
767 {
768     va_list args;
769     va_start(args, num);
770     ERR_add_error_vdata(num, args);
771     va_end(args);
772 }
773
774 void ERR_add_error_vdata(int num, va_list args)
775 {
776     int i, n, s;
777     char *str, *p, *a;
778
779     s = 80;
780     if ((str = OPENSSL_malloc(s + 1)) == NULL) {
781         /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
782         return;
783     }
784     str[0] = '\0';
785
786     n = 0;
787     for (i = 0; i < num; i++) {
788         a = va_arg(args, char *);
789         if (a == NULL)
790             a = "<NULL>";
791         n += strlen(a);
792         if (n > s) {
793             s = n + 20;
794             p = OPENSSL_realloc(str, s + 1);
795             if (p == NULL) {
796                 OPENSSL_free(str);
797                 return;
798             }
799             str = p;
800         }
801         OPENSSL_strlcat(str, a, (size_t)s + 1);
802     }
803     ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
804 }
805
806 int ERR_set_mark(void)
807 {
808     ERR_STATE *es;
809
810     es = ERR_get_state();
811     if (es == NULL)
812         return 0;
813
814     if (es->bottom == es->top)
815         return 0;
816     es->err_flags[es->top] |= ERR_FLAG_MARK;
817     return 1;
818 }
819
820 int ERR_pop_to_mark(void)
821 {
822     ERR_STATE *es;
823
824     es = ERR_get_state();
825     if (es == NULL)
826         return 0;
827
828     while (es->bottom != es->top
829            && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
830         err_clear(es, es->top);
831         es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
832     }
833
834     if (es->bottom == es->top)
835         return 0;
836     es->err_flags[es->top] &= ~ERR_FLAG_MARK;
837     return 1;
838 }
839
840 int ERR_clear_last_mark(void)
841 {
842     ERR_STATE *es;
843     int top;
844
845     es = ERR_get_state();
846     if (es == NULL)
847         return 0;
848
849     top = es->top;
850     while (es->bottom != top
851            && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
852         top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
853     }
854
855     if (es->bottom == top)
856         return 0;
857     es->err_flags[top] &= ~ERR_FLAG_MARK;
858     return 1;
859 }