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