]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/err/err.c
make error tables const and separate header file
[thirdparty/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
382 es->top = (es->top + 1) % ERR_NUM_ERRORS;
383 if (es->top == es->bottom)
384 es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
385 es->err_flags[es->top] = 0;
386 es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
387 es->err_file[es->top] = file;
388 es->err_line[es->top] = line;
389 err_clear_data(es, es->top);
390 }
391
392 void ERR_clear_error(void)
393 {
394 int i;
395 ERR_STATE *es;
396
397 es = ERR_get_state();
398
399 for (i = 0; i < ERR_NUM_ERRORS; i++) {
400 err_clear(es, i);
401 }
402 es->top = es->bottom = 0;
403 }
404
405 unsigned long ERR_get_error(void)
406 {
407 return (get_error_values(1, 0, NULL, NULL, NULL, NULL));
408 }
409
410 unsigned long ERR_get_error_line(const char **file, int *line)
411 {
412 return (get_error_values(1, 0, file, line, NULL, NULL));
413 }
414
415 unsigned long ERR_get_error_line_data(const char **file, int *line,
416 const char **data, int *flags)
417 {
418 return (get_error_values(1, 0, file, line, data, flags));
419 }
420
421 unsigned long ERR_peek_error(void)
422 {
423 return (get_error_values(0, 0, NULL, NULL, NULL, NULL));
424 }
425
426 unsigned long ERR_peek_error_line(const char **file, int *line)
427 {
428 return (get_error_values(0, 0, file, line, NULL, NULL));
429 }
430
431 unsigned long ERR_peek_error_line_data(const char **file, int *line,
432 const char **data, int *flags)
433 {
434 return (get_error_values(0, 0, file, line, data, flags));
435 }
436
437 unsigned long ERR_peek_last_error(void)
438 {
439 return (get_error_values(0, 1, NULL, NULL, NULL, NULL));
440 }
441
442 unsigned long ERR_peek_last_error_line(const char **file, int *line)
443 {
444 return (get_error_values(0, 1, file, line, NULL, NULL));
445 }
446
447 unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
448 const char **data, int *flags)
449 {
450 return (get_error_values(0, 1, file, line, data, flags));
451 }
452
453 static unsigned long get_error_values(int inc, int top, const char **file,
454 int *line, const char **data,
455 int *flags)
456 {
457 int i = 0;
458 ERR_STATE *es;
459 unsigned long ret;
460
461 es = ERR_get_state();
462
463 if (inc && top) {
464 if (file)
465 *file = "";
466 if (line)
467 *line = 0;
468 if (data)
469 *data = "";
470 if (flags)
471 *flags = 0;
472
473 return ERR_R_INTERNAL_ERROR;
474 }
475
476 if (es->bottom == es->top)
477 return 0;
478 if (top)
479 i = es->top; /* last error */
480 else
481 i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
482
483 ret = es->err_buffer[i];
484 if (inc) {
485 es->bottom = i;
486 es->err_buffer[i] = 0;
487 }
488
489 if ((file != NULL) && (line != NULL)) {
490 if (es->err_file[i] == NULL) {
491 *file = "NA";
492 if (line != NULL)
493 *line = 0;
494 } else {
495 *file = es->err_file[i];
496 if (line != NULL)
497 *line = es->err_line[i];
498 }
499 }
500
501 if (data == NULL) {
502 if (inc) {
503 err_clear_data(es, i);
504 }
505 } else {
506 if (es->err_data[i] == NULL) {
507 *data = "";
508 if (flags != NULL)
509 *flags = 0;
510 } else {
511 *data = es->err_data[i];
512 if (flags != NULL)
513 *flags = es->err_data_flags[i];
514 }
515 }
516 return ret;
517 }
518
519 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
520 {
521 char lsbuf[64], fsbuf[64], rsbuf[64];
522 const char *ls, *fs, *rs;
523 unsigned long l, f, r;
524
525 if (len == 0)
526 return;
527
528 l = ERR_GET_LIB(e);
529 f = ERR_GET_FUNC(e);
530 r = ERR_GET_REASON(e);
531
532 ls = ERR_lib_error_string(e);
533 fs = ERR_func_error_string(e);
534 rs = ERR_reason_error_string(e);
535
536 if (ls == NULL)
537 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
538 if (fs == NULL)
539 BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
540 if (rs == NULL)
541 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
542
543 BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls ? ls : lsbuf,
544 fs ? fs : fsbuf, rs ? rs : rsbuf);
545 if (strlen(buf) == len - 1) {
546 /*
547 * output may be truncated; make sure we always have 5
548 * colon-separated fields, i.e. 4 colons ...
549 */
550 #define NUM_COLONS 4
551 if (len > NUM_COLONS) { /* ... if possible */
552 int i;
553 char *s = buf;
554
555 for (i = 0; i < NUM_COLONS; i++) {
556 char *colon = strchr(s, ':');
557 if (colon == NULL || colon > &buf[len - 1] - NUM_COLONS + i) {
558 /*
559 * set colon no. i at last possible position (buf[len-1]
560 * is the terminating 0)
561 */
562 colon = &buf[len - 1] - NUM_COLONS + i;
563 *colon = ':';
564 }
565 s = colon + 1;
566 }
567 }
568 }
569 }
570
571 /*
572 * ERR_error_string_n should be used instead for ret != NULL as
573 * ERR_error_string cannot know how large the buffer is
574 */
575 char *ERR_error_string(unsigned long e, char *ret)
576 {
577 static char buf[256];
578
579 if (ret == NULL)
580 ret = buf;
581 ERR_error_string_n(e, ret, 256);
582
583 return ret;
584 }
585
586 const char *ERR_lib_error_string(unsigned long e)
587 {
588 ERR_STRING_DATA d, *p;
589 unsigned long l;
590
591 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
592 return NULL;
593 }
594
595 l = ERR_GET_LIB(e);
596 d.error = ERR_PACK(l, 0, 0);
597 p = int_err_get_item(&d);
598 return ((p == NULL) ? NULL : p->string);
599 }
600
601 const char *ERR_func_error_string(unsigned long e)
602 {
603 ERR_STRING_DATA d, *p;
604 unsigned long l, f;
605
606 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
607 return NULL;
608 }
609
610 l = ERR_GET_LIB(e);
611 f = ERR_GET_FUNC(e);
612 d.error = ERR_PACK(l, f, 0);
613 p = int_err_get_item(&d);
614 return ((p == NULL) ? NULL : p->string);
615 }
616
617 const char *ERR_reason_error_string(unsigned long e)
618 {
619 ERR_STRING_DATA d, *p = NULL;
620 unsigned long l, r;
621
622 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
623 return NULL;
624 }
625
626 l = ERR_GET_LIB(e);
627 r = ERR_GET_REASON(e);
628 d.error = ERR_PACK(l, 0, r);
629 p = int_err_get_item(&d);
630 if (!p) {
631 d.error = ERR_PACK(0, 0, r);
632 p = int_err_get_item(&d);
633 }
634 return ((p == NULL) ? NULL : p->string);
635 }
636
637 void err_delete_thread_state(void)
638 {
639 ERR_STATE *state = ERR_get_state();
640 if (state == NULL)
641 return;
642
643 CRYPTO_THREAD_set_local(&err_thread_local, NULL);
644 ERR_STATE_free(state);
645 }
646
647 #if OPENSSL_API_COMPAT < 0x10100000L
648 void ERR_remove_thread_state(void *dummy)
649 {
650 }
651 #endif
652
653 #if OPENSSL_API_COMPAT < 0x10000000L
654 void ERR_remove_state(unsigned long pid)
655 {
656 }
657 #endif
658
659 DEFINE_RUN_ONCE_STATIC(err_do_init)
660 {
661 set_err_thread_local = 1;
662 return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
663 }
664
665 ERR_STATE *ERR_get_state(void)
666 {
667 ERR_STATE *state = NULL;
668
669 if (!RUN_ONCE(&err_init, err_do_init))
670 return NULL;
671
672 state = CRYPTO_THREAD_get_local(&err_thread_local);
673
674 if (state == NULL) {
675 state = OPENSSL_zalloc(sizeof(*state));
676 if (state == NULL)
677 return NULL;
678
679 if (!CRYPTO_THREAD_set_local(&err_thread_local, state)) {
680 ERR_STATE_free(state);
681 return NULL;
682 }
683
684 /* Ignore failures from these */
685 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
686 ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);
687 }
688
689 return state;
690 }
691
692 int ERR_get_next_error_library(void)
693 {
694 int ret;
695
696 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
697 return 0;
698 }
699
700 CRYPTO_THREAD_write_lock(err_string_lock);
701 ret = int_err_library_number++;
702 CRYPTO_THREAD_unlock(err_string_lock);
703 return ret;
704 }
705
706 void ERR_set_error_data(char *data, int flags)
707 {
708 ERR_STATE *es;
709 int i;
710
711 es = ERR_get_state();
712
713 i = es->top;
714 if (i == 0)
715 i = ERR_NUM_ERRORS - 1;
716
717 err_clear_data(es, i);
718 es->err_data[i] = data;
719 es->err_data_flags[i] = flags;
720 }
721
722 void ERR_add_error_data(int num, ...)
723 {
724 va_list args;
725 va_start(args, num);
726 ERR_add_error_vdata(num, args);
727 va_end(args);
728 }
729
730 void ERR_add_error_vdata(int num, va_list args)
731 {
732 int i, n, s;
733 char *str, *p, *a;
734
735 s = 80;
736 str = OPENSSL_malloc(s + 1);
737 if (str == NULL)
738 return;
739 str[0] = '\0';
740
741 n = 0;
742 for (i = 0; i < num; i++) {
743 a = va_arg(args, char *);
744 /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */
745 if (a != NULL) {
746 n += strlen(a);
747 if (n > s) {
748 s = n + 20;
749 p = OPENSSL_realloc(str, s + 1);
750 if (p == NULL) {
751 OPENSSL_free(str);
752 return;
753 }
754 str = p;
755 }
756 OPENSSL_strlcat(str, a, (size_t)s + 1);
757 }
758 }
759 ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
760 }
761
762 int ERR_set_mark(void)
763 {
764 ERR_STATE *es;
765
766 es = ERR_get_state();
767
768 if (es->bottom == es->top)
769 return 0;
770 es->err_flags[es->top] |= ERR_FLAG_MARK;
771 return 1;
772 }
773
774 int ERR_pop_to_mark(void)
775 {
776 ERR_STATE *es;
777
778 es = ERR_get_state();
779
780 while (es->bottom != es->top
781 && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
782 err_clear(es, es->top);
783 es->top -= 1;
784 if (es->top == -1)
785 es->top = ERR_NUM_ERRORS - 1;
786 }
787
788 if (es->bottom == es->top)
789 return 0;
790 es->err_flags[es->top] &= ~ERR_FLAG_MARK;
791 return 1;
792 }