1 /* 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #define OSSL_FORCE_ERR_STATE 11 12 #include <stdio.h> 13 #include <stdarg.h> 14 #include <string.h> 15 #include "crypto/cryptlib.h" 16 #include "internal/err.h" 17 #include "crypto/err.h" 18 #include <openssl/err.h> 19 #include <openssl/crypto.h> 20 #include <openssl/buffer.h> 21 #include <openssl/bio.h> 22 #include <openssl/opensslconf.h> 23 #include "internal/thread_once.h" 24 #include "crypto/ctype.h" 25 #include "internal/constant_time.h" 26 #include "internal/e_os.h" 27 #include "err_local.h" 28 29 /* Forward declaration in case it's not published because of configuration */ 30 ERR_STATE *ERR_get_state(void); 31 32 #ifndef OPENSSL_NO_ERR 33 static int err_load_strings(const ERR_STRING_DATA *str); 34 #endif 35 36 #ifndef OPENSSL_NO_ERR 37 static ERR_STRING_DATA ERR_str_libraries[] = { 38 { ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library" }, 39 { ERR_PACK(ERR_LIB_SYS, 0, 0), "system library" }, 40 { ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines" }, 41 { ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines" }, 42 { ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines" }, 43 { ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines" }, 44 { ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines" }, 45 { ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines" }, 46 { ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines" }, 47 { ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines" }, 48 { ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines" }, 49 { ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines" }, 50 { ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines" }, 51 { ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines" }, 52 { ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines" }, 53 { ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines" }, 54 { ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines" }, 55 { ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines" }, 56 { ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines" }, 57 { ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines" }, 58 { ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines" }, 59 { ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines" }, 60 { ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator" }, 61 { ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines" }, 62 { ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines" }, 63 { ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines" }, 64 { ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines" }, 65 { ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines" }, 66 { ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines" }, 67 { ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines" }, 68 { ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines" }, 69 { ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines" }, 70 { ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines" }, 71 { ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines" }, 72 { ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines" }, 73 { ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines" }, 74 { ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines" }, 75 { ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines" }, 76 { ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines" }, 77 { ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines" }, 78 { ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines" }, 79 { ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines" }, 80 { ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines" }, 81 { 0, NULL }, 82 }; 83 84 /* 85 * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in 86 * are listed. For maintainability, please keep all reasons in the same order. 87 */ 88 static ERR_STRING_DATA ERR_str_reasons[] = { 89 { ERR_R_SYS_LIB, "system lib" }, 90 { ERR_R_BN_LIB, "BN lib" }, 91 { ERR_R_RSA_LIB, "RSA lib" }, 92 { ERR_R_DH_LIB, "DH lib" }, 93 { ERR_R_EVP_LIB, "EVP lib" }, 94 { ERR_R_BUF_LIB, "BUF lib" }, 95 { ERR_R_OBJ_LIB, "OBJ lib" }, 96 { ERR_R_PEM_LIB, "PEM lib" }, 97 { ERR_R_DSA_LIB, "DSA lib" }, 98 { ERR_R_X509_LIB, "X509 lib" }, 99 { ERR_R_ASN1_LIB, "ASN1 lib" }, 100 { ERR_R_CRYPTO_LIB, "CRYPTO lib" }, 101 { ERR_R_EC_LIB, "EC lib" }, 102 { ERR_R_BIO_LIB, "BIO lib" }, 103 { ERR_R_PKCS7_LIB, "PKCS7 lib" }, 104 { ERR_R_X509V3_LIB, "X509V3 lib" }, 105 { ERR_R_ENGINE_LIB, "ENGINE lib" }, 106 { ERR_R_UI_LIB, "UI lib" }, 107 { ERR_R_ECDSA_LIB, "ECDSA lib" }, 108 { ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib" }, 109 { ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib" }, 110 111 { ERR_R_FATAL, "fatal" }, 112 { ERR_R_MALLOC_FAILURE, "malloc failure" }, 113 { ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, 114 "called a function you should not call" }, 115 { ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter" }, 116 { ERR_R_INTERNAL_ERROR, "internal error" }, 117 { ERR_R_DISABLED, "called a function that was disabled at compile-time" }, 118 { ERR_R_INIT_FAIL, "init fail" }, 119 { ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument" }, 120 { ERR_R_OPERATION_FAIL, "operation fail" }, 121 { ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions" }, 122 { ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled" }, 123 { ERR_R_NESTED_ASN1_ERROR, "nested asn1 error" }, 124 { ERR_R_MISSING_ASN1_EOS, "missing asn1 eos" }, 125 /* 126 * Something is unsupported, exactly what is expressed with additional data 127 */ 128 { ERR_R_UNSUPPORTED, "unsupported" }, 129 /* 130 * A fetch failed for other reasons than the name to be fetched being 131 * unsupported. 132 */ 133 { ERR_R_FETCH_FAILED, "fetch failed" }, 134 { ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition" }, 135 { ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock" }, 136 { ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock" }, 137 { 0, NULL }, 138 }; 139 #endif 140 141 static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; 142 static int set_err_thread_local; 143 static CRYPTO_THREAD_LOCAL err_thread_local; 144 145 static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; 146 static CRYPTO_RWLOCK *err_string_lock = NULL; 147 148 #ifndef OPENSSL_NO_ERR 149 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); 150 #endif 151 152 /* 153 * The internal state 154 */ 155 156 #ifndef OPENSSL_NO_ERR 157 static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; 158 #endif 159 static int int_err_library_number = ERR_LIB_USER; 160 161 typedef enum ERR_GET_ACTION_e { 162 EV_POP, 163 EV_PEEK, 164 EV_PEEK_LAST 165 } ERR_GET_ACTION; 166 167 static unsigned long get_error_values(ERR_GET_ACTION g, 168 const char **file, int *line, 169 const char **func, const char **data, 170 int *flags); 171 172 #ifndef OPENSSL_NO_ERR 173 static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) 174 { 175 unsigned long ret, l; 176 177 l = a->error; 178 ret = l ^ ERR_GET_LIB(l); 179 return (ret ^ ret % 19 * 13); 180 } 181 182 static int err_string_data_cmp(const ERR_STRING_DATA *a, 183 const ERR_STRING_DATA *b) 184 { 185 if (a->error == b->error) 186 return 0; 187 return a->error > b->error ? 1 : -1; 188 } 189 190 static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d) 191 { 192 ERR_STRING_DATA *p = NULL; 193 194 if (!CRYPTO_THREAD_read_lock(err_string_lock)) 195 return NULL; 196 p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d); 197 CRYPTO_THREAD_unlock(err_string_lock); 198 199 return p; 200 } 201 #endif 202 203 void OSSL_ERR_STATE_free(ERR_STATE *state) 204 { 205 int i; 206 207 if (state == NULL) 208 return; 209 for (i = 0; i < ERR_NUM_ERRORS; i++) { 210 err_clear(state, i, 1); 211 } 212 CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE); 213 } 214 215 DEFINE_RUN_ONCE_STATIC(do_err_strings_init) 216 { 217 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) 218 return 0; 219 err_string_lock = CRYPTO_THREAD_lock_new(); 220 if (err_string_lock == NULL) 221 return 0; 222 #ifndef OPENSSL_NO_ERR 223 int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash, 224 err_string_data_cmp); 225 if (int_error_hash == NULL) { 226 CRYPTO_THREAD_lock_free(err_string_lock); 227 err_string_lock = NULL; 228 return 0; 229 } 230 #endif 231 return 1; 232 } 233 234 void err_cleanup(void) 235 { 236 if (set_err_thread_local != 0) 237 CRYPTO_THREAD_cleanup_local(&err_thread_local); 238 CRYPTO_THREAD_lock_free(err_string_lock); 239 err_string_lock = NULL; 240 #ifndef OPENSSL_NO_ERR 241 lh_ERR_STRING_DATA_free(int_error_hash); 242 int_error_hash = NULL; 243 #endif 244 } 245 246 #ifndef OPENSSL_NO_ERR 247 /* 248 * Legacy; pack in the library. 249 */ 250 static void err_patch(int lib, ERR_STRING_DATA *str) 251 { 252 unsigned long plib = ERR_PACK(lib, 0, 0); 253 254 for (; str->error != 0; str++) 255 str->error |= plib; 256 } 257 258 /* 259 * Hash in |str| error strings. Assumes the RUN_ONCE was done. 260 */ 261 static int err_load_strings(const ERR_STRING_DATA *str) 262 { 263 if (!CRYPTO_THREAD_write_lock(err_string_lock)) 264 return 0; 265 for (; str->error; str++) 266 (void)lh_ERR_STRING_DATA_insert(int_error_hash, 267 (ERR_STRING_DATA *)str); 268 CRYPTO_THREAD_unlock(err_string_lock); 269 return 1; 270 } 271 #endif 272 273 int ossl_err_load_ERR_strings(void) 274 { 275 #ifndef OPENSSL_NO_ERR 276 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) 277 return 0; 278 279 err_load_strings(ERR_str_libraries); 280 err_load_strings(ERR_str_reasons); 281 #endif 282 return 1; 283 } 284 285 int ERR_load_strings(int lib, ERR_STRING_DATA *str) 286 { 287 #ifndef OPENSSL_NO_ERR 288 if (ossl_err_load_ERR_strings() == 0) 289 return 0; 290 291 err_patch(lib, str); 292 err_load_strings(str); 293 #endif 294 295 return 1; 296 } 297 298 int ERR_load_strings_const(const ERR_STRING_DATA *str) 299 { 300 #ifndef OPENSSL_NO_ERR 301 if (ossl_err_load_ERR_strings() == 0) 302 return 0; 303 err_load_strings(str); 304 #endif 305 306 return 1; 307 } 308 309 int ERR_unload_strings(int lib, ERR_STRING_DATA *str) 310 { 311 #ifndef OPENSSL_NO_ERR 312 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) 313 return 0; 314 315 if (!CRYPTO_THREAD_write_lock(err_string_lock)) 316 return 0; 317 /* 318 * We don't need to ERR_PACK the lib, since that was done (to 319 * the table) when it was loaded. 320 */ 321 for (; str->error; str++) 322 (void)lh_ERR_STRING_DATA_delete(int_error_hash, str); 323 CRYPTO_THREAD_unlock(err_string_lock); 324 #endif 325 326 return 1; 327 } 328 329 void err_free_strings_int(void) 330 { 331 /* obsolete */ 332 } 333 334 /********************************************************/ 335 336 void ERR_clear_error(void) 337 { 338 int i; 339 ERR_STATE *es; 340 341 es = ossl_err_get_state_int(); 342 if (es == NULL) 343 return; 344 345 for (i = 0; i < ERR_NUM_ERRORS; i++) { 346 err_clear(es, i, 0); 347 } 348 es->top = es->bottom = 0; 349 } 350 351 unsigned long ERR_get_error(void) 352 { 353 return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL); 354 } 355 356 unsigned long ERR_get_error_all(const char **file, int *line, 357 const char **func, 358 const char **data, int *flags) 359 { 360 return get_error_values(EV_POP, file, line, func, data, flags); 361 } 362 363 #ifndef OPENSSL_NO_DEPRECATED_3_0 364 unsigned long ERR_get_error_line(const char **file, int *line) 365 { 366 return get_error_values(EV_POP, file, line, NULL, NULL, NULL); 367 } 368 369 unsigned long ERR_get_error_line_data(const char **file, int *line, 370 const char **data, int *flags) 371 { 372 return get_error_values(EV_POP, file, line, NULL, data, flags); 373 } 374 #endif 375 376 unsigned long ERR_peek_error(void) 377 { 378 return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL); 379 } 380 381 unsigned long ERR_peek_error_line(const char **file, int *line) 382 { 383 return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL); 384 } 385 386 unsigned long ERR_peek_error_func(const char **func) 387 { 388 return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL); 389 } 390 391 unsigned long ERR_peek_error_data(const char **data, int *flags) 392 { 393 return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags); 394 } 395 396 unsigned long ERR_peek_error_all(const char **file, int *line, 397 const char **func, 398 const char **data, int *flags) 399 { 400 return get_error_values(EV_PEEK, file, line, func, data, flags); 401 } 402 403 #ifndef OPENSSL_NO_DEPRECATED_3_0 404 unsigned long ERR_peek_error_line_data(const char **file, int *line, 405 const char **data, int *flags) 406 { 407 return get_error_values(EV_PEEK, file, line, NULL, data, flags); 408 } 409 #endif 410 411 unsigned long ERR_peek_last_error(void) 412 { 413 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL); 414 } 415 416 unsigned long ERR_peek_last_error_line(const char **file, int *line) 417 { 418 return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL); 419 } 420 421 unsigned long ERR_peek_last_error_func(const char **func) 422 { 423 return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL); 424 } 425 426 unsigned long ERR_peek_last_error_data(const char **data, int *flags) 427 { 428 return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags); 429 } 430 431 unsigned long ERR_peek_last_error_all(const char **file, int *line, 432 const char **func, 433 const char **data, int *flags) 434 { 435 return get_error_values(EV_PEEK_LAST, file, line, func, data, flags); 436 } 437 438 #ifndef OPENSSL_NO_DEPRECATED_3_0 439 unsigned long ERR_peek_last_error_line_data(const char **file, int *line, 440 const char **data, int *flags) 441 { 442 return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags); 443 } 444 #endif 445 446 static unsigned long get_error_values(ERR_GET_ACTION g, 447 const char **file, int *line, 448 const char **func, 449 const char **data, int *flags) 450 { 451 int i = 0; 452 ERR_STATE *es; 453 unsigned long ret; 454 455 es = ossl_err_get_state_int(); 456 if (es == NULL) 457 return 0; 458 459 /* 460 * Clear anything that should have been cleared earlier. We do this 461 * here because this doesn't have constant-time issues. 462 */ 463 while (es->bottom != es->top) { 464 if (es->err_flags[es->top] & ERR_FLAG_CLEAR) { 465 err_clear(es, es->top, 0); 466 es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1; 467 continue; 468 } 469 i = (es->bottom + 1) % ERR_NUM_ERRORS; 470 if (es->err_flags[i] & ERR_FLAG_CLEAR) { 471 es->bottom = i; 472 err_clear(es, es->bottom, 0); 473 continue; 474 } 475 break; 476 } 477 478 /* If everything has been cleared, the stack is empty. */ 479 if (es->bottom == es->top) 480 return 0; 481 482 /* Which error, the top of stack (latest one) or the first one? */ 483 if (g == EV_PEEK_LAST) 484 i = es->top; 485 else 486 i = (es->bottom + 1) % ERR_NUM_ERRORS; 487 488 ret = es->err_buffer[i]; 489 if (g == EV_POP) { 490 es->bottom = i; 491 es->err_buffer[i] = 0; 492 } 493 494 if (file != NULL) { 495 *file = es->err_file[i]; 496 if (*file == NULL) 497 *file = ""; 498 } 499 if (line != NULL) 500 *line = es->err_line[i]; 501 if (func != NULL) { 502 *func = es->err_func[i]; 503 if (*func == NULL) 504 *func = ""; 505 } 506 if (flags != NULL) 507 *flags = es->err_data_flags[i]; 508 if (data == NULL) { 509 if (g == EV_POP) { 510 err_clear_data(es, i, 0); 511 } 512 } else { 513 *data = es->err_data[i]; 514 if (*data == NULL) { 515 *data = ""; 516 if (flags != NULL) 517 *flags = 0; 518 } 519 } 520 return ret; 521 } 522 523 void ossl_err_string_int(unsigned long e, const char *func, 524 char *buf, size_t len) 525 { 526 char lsbuf[64], rsbuf[256]; 527 const char *ls, *rs = NULL; 528 unsigned long l, r; 529 530 if (len == 0) 531 return; 532 533 l = ERR_GET_LIB(e); 534 ls = ERR_lib_error_string(e); 535 if (ls == NULL) { 536 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l); 537 ls = lsbuf; 538 } 539 540 /* 541 * ERR_reason_error_string() can't safely return system error strings, 542 * since it would call openssl_strerror_r(), which needs a buffer for 543 * thread safety. So for system errors, we call openssl_strerror_r() 544 * directly instead. 545 */ 546 r = ERR_GET_REASON(e); 547 #ifndef OPENSSL_NO_ERR 548 if (ERR_SYSTEM_ERROR(e)) { 549 if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf))) 550 rs = rsbuf; 551 } else { 552 rs = ERR_reason_error_string(e); 553 } 554 #endif 555 if (rs == NULL) { 556 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", 557 r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET)); 558 rs = rsbuf; 559 } 560 561 BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs); 562 if (strlen(buf) == len - 1) { 563 /* Didn't fit; use a minimal format. */ 564 BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r); 565 } 566 } 567 568 void ERR_error_string_n(unsigned long e, char *buf, size_t len) 569 { 570 ossl_err_string_int(e, "", buf, len); 571 } 572 573 /* 574 * ERR_error_string_n should be used instead for ret != NULL as 575 * ERR_error_string cannot know how large the buffer is 576 */ 577 char *ERR_error_string(unsigned long e, char *ret) 578 { 579 static char buf[256]; 580 581 if (ret == NULL) 582 ret = buf; 583 ERR_error_string_n(e, ret, (int)sizeof(buf)); 584 return ret; 585 } 586 587 const char *ERR_lib_error_string(unsigned long e) 588 { 589 #ifndef OPENSSL_NO_ERR 590 ERR_STRING_DATA d, *p; 591 unsigned long l; 592 593 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { 594 return NULL; 595 } 596 597 l = ERR_GET_LIB(e); 598 d.error = ERR_PACK(l, 0, 0); 599 p = int_err_get_item(&d); 600 return ((p == NULL) ? NULL : p->string); 601 #else 602 return NULL; 603 #endif 604 } 605 606 #ifndef OPENSSL_NO_DEPRECATED_3_0 607 const char *ERR_func_error_string(unsigned long e) 608 { 609 return NULL; 610 } 611 #endif 612 613 const char *ERR_reason_error_string(unsigned long e) 614 { 615 #ifndef OPENSSL_NO_ERR 616 ERR_STRING_DATA d, *p = NULL; 617 unsigned long l, r; 618 619 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { 620 return NULL; 621 } 622 623 /* 624 * ERR_reason_error_string() can't safely return system error strings, 625 * since openssl_strerror_r() needs a buffer for thread safety, and we 626 * haven't got one that would serve any sensible purpose. 627 */ 628 if (ERR_SYSTEM_ERROR(e)) 629 return NULL; 630 631 l = ERR_GET_LIB(e); 632 r = ERR_GET_REASON(e); 633 d.error = ERR_PACK(l, 0, r); 634 p = int_err_get_item(&d); 635 if (p == NULL) { 636 d.error = ERR_PACK(0, 0, r); 637 p = int_err_get_item(&d); 638 } 639 return ((p == NULL) ? NULL : p->string); 640 #else 641 return NULL; 642 #endif 643 } 644 645 static void err_delete_thread_state(void *unused) 646 { 647 ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); 648 if (state == NULL) 649 return; 650 651 CRYPTO_THREAD_set_local(&err_thread_local, NULL); 652 OSSL_ERR_STATE_free(state); 653 } 654 655 #ifndef OPENSSL_NO_DEPRECATED_1_1_0 656 void ERR_remove_thread_state(void *dummy) 657 { 658 } 659 #endif 660 661 #ifndef OPENSSL_NO_DEPRECATED_1_0_0 662 void ERR_remove_state(unsigned long pid) 663 { 664 } 665 #endif 666 667 DEFINE_RUN_ONCE_STATIC(err_do_init) 668 { 669 set_err_thread_local = 1; 670 return CRYPTO_THREAD_init_local(&err_thread_local, NULL); 671 } 672 673 ERR_STATE *ossl_err_get_state_int(void) 674 { 675 ERR_STATE *state; 676 int saveerrno = get_last_sys_error(); 677 678 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) 679 return NULL; 680 681 if (!RUN_ONCE(&err_init, err_do_init)) 682 return NULL; 683 684 state = CRYPTO_THREAD_get_local(&err_thread_local); 685 if (state == (ERR_STATE *)-1) 686 return NULL; 687 688 if (state == NULL) { 689 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1)) 690 return NULL; 691 692 state = OSSL_ERR_STATE_new(); 693 if (state == NULL) { 694 CRYPTO_THREAD_set_local(&err_thread_local, NULL); 695 return NULL; 696 } 697 698 if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state) 699 || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { 700 OSSL_ERR_STATE_free(state); 701 CRYPTO_THREAD_set_local(&err_thread_local, NULL); 702 return NULL; 703 } 704 705 /* Ignore failures from these */ 706 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); 707 } 708 709 set_sys_error(saveerrno); 710 return state; 711 } 712 713 #ifndef OPENSSL_NO_DEPRECATED_3_0 714 ERR_STATE *ERR_get_state(void) 715 { 716 return ossl_err_get_state_int(); 717 } 718 #endif 719 720 /* 721 * err_shelve_state returns the current thread local error state 722 * and freezes the error module until err_unshelve_state is called. 723 */ 724 int err_shelve_state(void **state) 725 { 726 int saveerrno = get_last_sys_error(); 727 728 /* 729 * Note, at present our only caller is OPENSSL_init_crypto(), indirectly 730 * via ossl_init_load_crypto_nodelete(), by which point the requested 731 * "base" initialization has already been performed, so the below call is a 732 * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return. 733 * 734 * If are no other valid callers of this function, the call below can be 735 * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are 736 * potential uses that are not from inside OPENSSL_init_crypto(), then this 737 * call is needed, but some care is required to make sure that the re-entry 738 * remains a NOOP. 739 */ 740 if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) 741 return 0; 742 743 if (!RUN_ONCE(&err_init, err_do_init)) 744 return 0; 745 746 *state = CRYPTO_THREAD_get_local(&err_thread_local); 747 if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1)) 748 return 0; 749 750 set_sys_error(saveerrno); 751 return 1; 752 } 753 754 /* 755 * err_unshelve_state restores the error state that was returned 756 * by err_shelve_state previously. 757 */ 758 void err_unshelve_state(void *state) 759 { 760 if (state != (void *)-1) 761 CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)state); 762 } 763 764 int ERR_get_next_error_library(void) 765 { 766 int ret; 767 768 if (!RUN_ONCE(&err_string_init, do_err_strings_init)) 769 return 0; 770 771 if (!CRYPTO_THREAD_write_lock(err_string_lock)) 772 return 0; 773 ret = int_err_library_number++; 774 CRYPTO_THREAD_unlock(err_string_lock); 775 return ret; 776 } 777 778 static int err_set_error_data_int(char *data, size_t size, int flags, 779 int deallocate) 780 { 781 ERR_STATE *es; 782 783 es = ossl_err_get_state_int(); 784 if (es == NULL) 785 return 0; 786 787 err_clear_data(es, es->top, deallocate); 788 err_set_data(es, es->top, data, size, flags); 789 790 return 1; 791 } 792 793 void ERR_set_error_data(char *data, int flags) 794 { 795 /* 796 * This function is void so we cannot propagate the error return. Since it 797 * is also in the public API we can't change the return type. 798 * 799 * We estimate the size of the data. If it's not flagged as allocated, 800 * then this is safe, and if it is flagged as allocated, then our size 801 * may be smaller than the actual allocation, but that doesn't matter 802 * too much, the buffer will remain untouched or will eventually be 803 * reallocated to a new size. 804 * 805 * callers should be advised that this function takes over ownership of 806 * the allocated memory, i.e. they can't count on the pointer to remain 807 * valid. 808 */ 809 err_set_error_data_int(data, strlen(data) + 1, flags, 1); 810 } 811 812 void ERR_add_error_data(int num, ...) 813 { 814 va_list args; 815 va_start(args, num); 816 ERR_add_error_vdata(num, args); 817 va_end(args); 818 } 819 820 void ERR_add_error_vdata(int num, va_list args) 821 { 822 int i, len, size; 823 int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING; 824 char *str, *arg; 825 ERR_STATE *es; 826 827 /* Get the current error data; if an allocated string get it. */ 828 es = ossl_err_get_state_int(); 829 if (es == NULL) 830 return; 831 i = es->top; 832 833 /* 834 * If err_data is allocated already, reuse the space. 835 * Otherwise, allocate a small new buffer. 836 */ 837 if ((es->err_data_flags[i] & flags) == flags 838 && ossl_assert(es->err_data[i] != NULL)) { 839 str = es->err_data[i]; 840 size = es->err_data_size[i]; 841 842 /* 843 * To protect the string we just grabbed from tampering by other 844 * functions we may call, or to protect them from freeing a pointer 845 * that may no longer be valid at that point, we clear away the 846 * data pointer and the flags. We will set them again at the end 847 * of this function. 848 */ 849 es->err_data[i] = NULL; 850 es->err_data_flags[i] = 0; 851 } else if ((str = OPENSSL_malloc(size = 81)) == NULL) { 852 return; 853 } else { 854 str[0] = '\0'; 855 } 856 len = strlen(str); 857 858 while (--num >= 0) { 859 arg = va_arg(args, char *); 860 if (arg == NULL) 861 arg = "<NULL>"; 862 len += strlen(arg); 863 if (len >= size) { 864 char *p; 865 866 size = len + 20; 867 p = OPENSSL_realloc(str, size); 868 if (p == NULL) { 869 OPENSSL_free(str); 870 return; 871 } 872 str = p; 873 } 874 OPENSSL_strlcat(str, arg, (size_t)size); 875 } 876 if (!err_set_error_data_int(str, size, flags, 0)) 877 OPENSSL_free(str); 878 } 879 880 void err_clear_last_constant_time(int clear) 881 { 882 ERR_STATE *es; 883 int top; 884 885 es = ossl_err_get_state_int(); 886 if (es == NULL) 887 return; 888 889 top = es->top; 890 891 /* 892 * Flag error as cleared but remove it elsewhere to avoid two errors 893 * accessing the same error stack location, revealing timing information. 894 */ 895 clear = constant_time_select_int(constant_time_eq_int(clear, 0), 896 0, ERR_FLAG_CLEAR); 897 es->err_flags[top] |= clear; 898 } 899