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