1 /* $OpenBSD: err.c,v 1.63 2024/08/31 10:09:15 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111 112 #include <pthread.h> 113 #include <stdarg.h> 114 #include <stdio.h> 115 #include <string.h> 116 117 #include <openssl/opensslconf.h> 118 119 #include <openssl/bio.h> 120 #include <openssl/buffer.h> 121 #include <openssl/crypto.h> 122 #include <openssl/err.h> 123 #include <openssl/lhash.h> 124 125 #include "crypto_local.h" 126 127 DECLARE_LHASH_OF(ERR_STRING_DATA); 128 DECLARE_LHASH_OF(ERR_STATE); 129 130 typedef struct st_ERR_FNS ERR_FNS; 131 132 typedef struct err_state_st { 133 CRYPTO_THREADID tid; 134 int err_flags[ERR_NUM_ERRORS]; 135 unsigned long err_buffer[ERR_NUM_ERRORS]; 136 char *err_data[ERR_NUM_ERRORS]; 137 int err_data_flags[ERR_NUM_ERRORS]; 138 const char *err_file[ERR_NUM_ERRORS]; 139 int err_line[ERR_NUM_ERRORS]; 140 int top, bottom; 141 } ERR_STATE; 142 143 static void err_load_strings(int lib, ERR_STRING_DATA *str); 144 145 static ERR_STATE *ERR_get_state(void); 146 static void ERR_STATE_free(ERR_STATE *s); 147 148 #ifndef OPENSSL_NO_ERR 149 static ERR_STRING_DATA ERR_str_libraries[] = { 150 {ERR_PACK(ERR_LIB_NONE,0,0), "unknown library"}, 151 {ERR_PACK(ERR_LIB_SYS,0,0), "system library"}, 152 {ERR_PACK(ERR_LIB_BN,0,0), "bignum routines"}, 153 {ERR_PACK(ERR_LIB_RSA,0,0), "rsa routines"}, 154 {ERR_PACK(ERR_LIB_DH,0,0), "Diffie-Hellman routines"}, 155 {ERR_PACK(ERR_LIB_EVP,0,0), "digital envelope routines"}, 156 {ERR_PACK(ERR_LIB_BUF,0,0), "memory buffer routines"}, 157 {ERR_PACK(ERR_LIB_OBJ,0,0), "object identifier routines"}, 158 {ERR_PACK(ERR_LIB_PEM,0,0), "PEM routines"}, 159 {ERR_PACK(ERR_LIB_DSA,0,0), "dsa routines"}, 160 {ERR_PACK(ERR_LIB_X509,0,0), "x509 certificate routines"}, 161 {ERR_PACK(ERR_LIB_ASN1,0,0), "asn1 encoding routines"}, 162 {ERR_PACK(ERR_LIB_CONF,0,0), "configuration file routines"}, 163 {ERR_PACK(ERR_LIB_CRYPTO,0,0), "common libcrypto routines"}, 164 {ERR_PACK(ERR_LIB_EC,0,0), "elliptic curve routines"}, 165 {ERR_PACK(ERR_LIB_SSL,0,0), "SSL routines"}, 166 {ERR_PACK(ERR_LIB_BIO,0,0), "BIO routines"}, 167 {ERR_PACK(ERR_LIB_PKCS7,0,0), "PKCS7 routines"}, 168 {ERR_PACK(ERR_LIB_X509V3,0,0), "X509 V3 routines"}, 169 {ERR_PACK(ERR_LIB_PKCS12,0,0), "PKCS12 routines"}, 170 {ERR_PACK(ERR_LIB_RAND,0,0), "random number generator"}, 171 {ERR_PACK(ERR_LIB_DSO,0,0), "DSO support routines"}, 172 {ERR_PACK(ERR_LIB_TS,0,0), "time stamp routines"}, 173 {ERR_PACK(ERR_LIB_ENGINE,0,0), "engine routines"}, 174 {ERR_PACK(ERR_LIB_OCSP,0,0), "OCSP routines"}, 175 {ERR_PACK(ERR_LIB_FIPS,0,0), "FIPS routines"}, 176 {ERR_PACK(ERR_LIB_CMS,0,0), "CMS routines"}, 177 {ERR_PACK(ERR_LIB_HMAC,0,0), "HMAC routines"}, 178 {ERR_PACK(ERR_LIB_GOST,0,0), "GOST routines"}, 179 {0, NULL}, 180 }; 181 182 static ERR_STRING_DATA ERR_str_functs[] = { 183 {ERR_PACK(0,SYS_F_FOPEN, 0), "fopen"}, 184 {ERR_PACK(0,SYS_F_CONNECT, 0), "connect"}, 185 {ERR_PACK(0,SYS_F_GETSERVBYNAME, 0), "getservbyname"}, 186 {ERR_PACK(0,SYS_F_SOCKET, 0), "socket"}, 187 {ERR_PACK(0,SYS_F_IOCTLSOCKET, 0), "ioctl"}, 188 {ERR_PACK(0,SYS_F_BIND, 0), "bind"}, 189 {ERR_PACK(0,SYS_F_LISTEN, 0), "listen"}, 190 {ERR_PACK(0,SYS_F_ACCEPT, 0), "accept"}, 191 {ERR_PACK(0,SYS_F_OPENDIR, 0), "opendir"}, 192 {ERR_PACK(0,SYS_F_FREAD, 0), "fread"}, 193 {0, NULL}, 194 }; 195 196 static ERR_STRING_DATA ERR_str_reasons[] = { 197 {ERR_R_SYS_LIB, "system lib"}, 198 {ERR_R_BN_LIB, "BN lib"}, 199 {ERR_R_RSA_LIB, "RSA lib"}, 200 {ERR_R_DH_LIB, "DH lib"}, 201 {ERR_R_EVP_LIB, "EVP lib"}, 202 {ERR_R_BUF_LIB, "BUF lib"}, 203 {ERR_R_OBJ_LIB, "OBJ lib"}, 204 {ERR_R_PEM_LIB, "PEM lib"}, 205 {ERR_R_DSA_LIB, "DSA lib"}, 206 {ERR_R_X509_LIB, "X509 lib"}, 207 {ERR_R_ASN1_LIB, "ASN1 lib"}, 208 {ERR_R_CONF_LIB, "CONF lib"}, 209 {ERR_R_CRYPTO_LIB, "CRYPTO lib"}, 210 {ERR_R_EC_LIB, "EC lib"}, 211 {ERR_R_SSL_LIB, "SSL lib"}, 212 {ERR_R_BIO_LIB, "BIO lib"}, 213 {ERR_R_PKCS7_LIB, "PKCS7 lib"}, 214 {ERR_R_X509V3_LIB, "X509V3 lib"}, 215 {ERR_R_PKCS12_LIB, "PKCS12 lib"}, 216 {ERR_R_RAND_LIB, "RAND lib"}, 217 {ERR_R_DSO_LIB, "DSO lib"}, 218 {ERR_R_ENGINE_LIB, "ENGINE lib"}, 219 {ERR_R_OCSP_LIB, "OCSP lib"}, 220 {ERR_R_TS_LIB, "TS lib"}, 221 222 {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"}, 223 {ERR_R_BAD_ASN1_OBJECT_HEADER, "bad asn1 object header"}, 224 {ERR_R_BAD_GET_ASN1_OBJECT_CALL, "bad get asn1 object call"}, 225 {ERR_R_EXPECTING_AN_ASN1_SEQUENCE, "expecting an asn1 sequence"}, 226 {ERR_R_ASN1_LENGTH_MISMATCH, "asn1 length mismatch"}, 227 {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"}, 228 229 {ERR_R_FATAL, "fatal"}, 230 {ERR_R_MALLOC_FAILURE, "malloc failure"}, 231 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "called a function you should not call"}, 232 {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"}, 233 {ERR_R_INTERNAL_ERROR, "internal error"}, 234 {ERR_R_DISABLED , "called a function that was disabled at compile-time"}, 235 {ERR_R_INIT_FAIL, "initialization failure"}, 236 237 {0, NULL}, 238 }; 239 #endif 240 241 242 /* Define the predeclared (but externally opaque) "ERR_FNS" type */ 243 struct st_ERR_FNS { 244 /* Works on the "error_hash" string table */ 245 LHASH_OF(ERR_STRING_DATA) *(*cb_err_get)(int create); 246 void (*cb_err_del)(void); 247 const ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *); 248 const ERR_STRING_DATA *(*cb_err_set_item)(const ERR_STRING_DATA *); 249 const ERR_STRING_DATA *(*cb_err_del_item)(const ERR_STRING_DATA *); 250 /* Works on the "thread_hash" error-state table */ 251 LHASH_OF(ERR_STATE) *(*cb_thread_get)(int create); 252 void (*cb_thread_release)(LHASH_OF(ERR_STATE) **hash); 253 ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *); 254 ERR_STATE *(*cb_thread_set_item)(ERR_STATE *); 255 void (*cb_thread_del_item)(const ERR_STATE *); 256 /* Returns the next available error "library" numbers */ 257 int (*cb_get_next_lib)(void); 258 }; 259 260 /* Predeclarations of the "err_defaults" functions */ 261 static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create); 262 static void int_err_del(void); 263 static const ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); 264 static const ERR_STRING_DATA *int_err_set_item(const ERR_STRING_DATA *); 265 static const ERR_STRING_DATA *int_err_del_item(const ERR_STRING_DATA *); 266 static LHASH_OF(ERR_STATE) *int_thread_get(int create); 267 static void int_thread_release(LHASH_OF(ERR_STATE) **hash); 268 static ERR_STATE *int_thread_get_item(const ERR_STATE *); 269 static ERR_STATE *int_thread_set_item(ERR_STATE *); 270 static void int_thread_del_item(const ERR_STATE *); 271 static int int_err_get_next_lib(void); 272 273 /* The static ERR_FNS table using these defaults functions */ 274 static const ERR_FNS err_defaults = { 275 int_err_get, 276 int_err_del, 277 int_err_get_item, 278 int_err_set_item, 279 int_err_del_item, 280 int_thread_get, 281 int_thread_release, 282 int_thread_get_item, 283 int_thread_set_item, 284 int_thread_del_item, 285 int_err_get_next_lib 286 }; 287 288 /* The replacable table of ERR_FNS functions we use at run-time */ 289 static const ERR_FNS *err_fns = NULL; 290 291 /* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */ 292 #define ERRFN(a) err_fns->cb_##a 293 294 /* The internal state used by "err_defaults" - as such, the setting, reading, 295 * creating, and deleting of this data should only be permitted via the 296 * "err_defaults" functions. This way, a linked module can completely defer all 297 * ERR state operation (together with requisite locking) to the implementations 298 * and state in the loading application. */ 299 static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; 300 static LHASH_OF(ERR_STATE) *int_thread_hash = NULL; 301 static int int_thread_hash_references = 0; 302 static int int_err_library_number = ERR_LIB_USER; 303 304 static pthread_t err_init_thread; 305 306 /* Internal function that checks whether "err_fns" is set and if not, sets it to 307 * the defaults. */ 308 static void 309 err_fns_check(void) 310 { 311 if (err_fns) 312 return; 313 314 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 315 if (!err_fns) 316 err_fns = &err_defaults; 317 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 318 } 319 320 /* These are the callbacks provided to "lh_new()" when creating the LHASH tables 321 * internal to the "err_defaults" implementation. */ 322 323 static unsigned long get_error_values(int inc, int top, const char **file, 324 int *line, const char **data, int *flags); 325 326 /* The internal functions used in the "err_defaults" implementation */ 327 328 static unsigned long 329 err_string_data_hash(const ERR_STRING_DATA *a) 330 { 331 unsigned long ret, l; 332 333 l = a->error; 334 ret = l^ERR_GET_LIB(l)^ERR_GET_FUNC(l); 335 return (ret^ret % 19*13); 336 } 337 static IMPLEMENT_LHASH_HASH_FN(err_string_data, ERR_STRING_DATA) 338 339 static int 340 err_string_data_cmp(const ERR_STRING_DATA *a, const ERR_STRING_DATA *b) 341 { 342 return (int)(a->error - b->error); 343 } 344 static IMPLEMENT_LHASH_COMP_FN(err_string_data, ERR_STRING_DATA) 345 346 static LHASH_OF(ERR_STRING_DATA) * 347 int_err_get(int create) 348 { 349 LHASH_OF(ERR_STRING_DATA) *ret = NULL; 350 351 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 352 if (!int_error_hash && create) 353 int_error_hash = lh_ERR_STRING_DATA_new(); 354 if (int_error_hash) 355 ret = int_error_hash; 356 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 357 358 return ret; 359 } 360 361 static void 362 int_err_del(void) 363 { 364 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 365 if (int_error_hash) { 366 lh_ERR_STRING_DATA_free(int_error_hash); 367 int_error_hash = NULL; 368 } 369 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 370 } 371 372 static const ERR_STRING_DATA * 373 int_err_get_item(const ERR_STRING_DATA *d) 374 { 375 ERR_STRING_DATA *p; 376 LHASH_OF(ERR_STRING_DATA) *hash; 377 378 err_fns_check(); 379 hash = ERRFN(err_get)(0); 380 if (!hash) 381 return NULL; 382 383 CRYPTO_r_lock(CRYPTO_LOCK_ERR); 384 p = lh_ERR_STRING_DATA_retrieve(hash, d); 385 CRYPTO_r_unlock(CRYPTO_LOCK_ERR); 386 387 return p; 388 } 389 390 static const ERR_STRING_DATA * 391 int_err_set_item(const ERR_STRING_DATA *d) 392 { 393 const ERR_STRING_DATA *p; 394 LHASH_OF(ERR_STRING_DATA) *hash; 395 396 err_fns_check(); 397 hash = ERRFN(err_get)(1); 398 if (!hash) 399 return NULL; 400 401 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 402 p = lh_ERR_STRING_DATA_insert(hash, (void *)d); 403 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 404 405 return p; 406 } 407 408 static const ERR_STRING_DATA * 409 int_err_del_item(const ERR_STRING_DATA *d) 410 { 411 ERR_STRING_DATA *p; 412 LHASH_OF(ERR_STRING_DATA) *hash; 413 414 err_fns_check(); 415 hash = ERRFN(err_get)(0); 416 if (!hash) 417 return NULL; 418 419 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 420 p = lh_ERR_STRING_DATA_delete(hash, d); 421 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 422 423 return p; 424 } 425 426 static unsigned long 427 err_state_hash(const ERR_STATE *a) 428 { 429 return CRYPTO_THREADID_hash(&a->tid) * 13; 430 } 431 static IMPLEMENT_LHASH_HASH_FN(err_state, ERR_STATE) 432 433 static int 434 err_state_cmp(const ERR_STATE *a, const ERR_STATE *b) 435 { 436 return CRYPTO_THREADID_cmp(&a->tid, &b->tid); 437 } 438 static IMPLEMENT_LHASH_COMP_FN(err_state, ERR_STATE) 439 440 static LHASH_OF(ERR_STATE) * 441 int_thread_get(int create) 442 { 443 LHASH_OF(ERR_STATE) *ret = NULL; 444 445 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 446 if (!int_thread_hash && create) 447 int_thread_hash = lh_ERR_STATE_new(); 448 if (int_thread_hash) { 449 int_thread_hash_references++; 450 ret = int_thread_hash; 451 } 452 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 453 return ret; 454 } 455 456 static void 457 int_thread_release(LHASH_OF(ERR_STATE) **hash) 458 { 459 int i; 460 461 if (hash == NULL || *hash == NULL) 462 return; 463 464 i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR); 465 if (i > 0) 466 return; 467 468 *hash = NULL; 469 } 470 471 static ERR_STATE * 472 int_thread_get_item(const ERR_STATE *d) 473 { 474 ERR_STATE *p; 475 LHASH_OF(ERR_STATE) *hash; 476 477 err_fns_check(); 478 hash = ERRFN(thread_get)(0); 479 if (!hash) 480 return NULL; 481 482 CRYPTO_r_lock(CRYPTO_LOCK_ERR); 483 p = lh_ERR_STATE_retrieve(hash, d); 484 CRYPTO_r_unlock(CRYPTO_LOCK_ERR); 485 486 ERRFN(thread_release)(&hash); 487 return p; 488 } 489 490 static ERR_STATE * 491 int_thread_set_item(ERR_STATE *d) 492 { 493 ERR_STATE *p; 494 LHASH_OF(ERR_STATE) *hash; 495 496 err_fns_check(); 497 hash = ERRFN(thread_get)(1); 498 if (!hash) 499 return NULL; 500 501 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 502 p = lh_ERR_STATE_insert(hash, d); 503 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 504 505 ERRFN(thread_release)(&hash); 506 return p; 507 } 508 509 static void 510 int_thread_del_item(const ERR_STATE *d) 511 { 512 ERR_STATE *p; 513 LHASH_OF(ERR_STATE) *hash; 514 515 err_fns_check(); 516 hash = ERRFN(thread_get)(0); 517 if (!hash) 518 return; 519 520 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 521 p = lh_ERR_STATE_delete(hash, d); 522 /* make sure we don't leak memory */ 523 if (int_thread_hash_references == 1 && 524 int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0) { 525 lh_ERR_STATE_free(int_thread_hash); 526 int_thread_hash = NULL; 527 } 528 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 529 530 ERRFN(thread_release)(&hash); 531 if (p) 532 ERR_STATE_free(p); 533 } 534 535 static int 536 int_err_get_next_lib(void) 537 { 538 int ret; 539 540 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 541 ret = int_err_library_number++; 542 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 543 544 return ret; 545 } 546 547 548 #ifndef OPENSSL_NO_ERR 549 #define NUM_SYS_STR_REASONS 127 550 #define LEN_SYS_STR_REASON 32 551 552 static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1]; 553 /* SYS_str_reasons is filled with copies of strerror() results at 554 * initialization. 555 * 'errno' values up to 127 should cover all usual errors, 556 * others will be displayed numerically by ERR_error_string. 557 * It is crucial that we have something for each reason code 558 * that occurs in ERR_str_reasons, or bogus reason strings 559 * will be returned for SYSerror(which always gets an errno 560 * value and never one of those 'standard' reason codes. */ 561 562 static void 563 build_SYS_str_reasons(void) 564 { 565 /* malloc cannot be used here, use static storage instead */ 566 static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON]; 567 int i; 568 static int init = 1; 569 int save_errno; 570 571 CRYPTO_r_lock(CRYPTO_LOCK_ERR); 572 if (!init) { 573 CRYPTO_r_unlock(CRYPTO_LOCK_ERR); 574 return; 575 } 576 577 CRYPTO_r_unlock(CRYPTO_LOCK_ERR); 578 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 579 if (!init) { 580 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 581 return; 582 } 583 584 /* strerror(3) will set errno to EINVAL when i is an unknown errno. */ 585 save_errno = errno; 586 for (i = 1; i <= NUM_SYS_STR_REASONS; i++) { 587 ERR_STRING_DATA *str = &SYS_str_reasons[i - 1]; 588 589 str->error = (unsigned long)i; 590 if (str->string == NULL) { 591 char (*dest)[LEN_SYS_STR_REASON] = 592 &(strerror_tab[i - 1]); 593 const char *src = strerror(i); 594 if (src != NULL) { 595 strlcpy(*dest, src, sizeof *dest); 596 str->string = *dest; 597 } 598 } 599 if (str->string == NULL) 600 str->string = "unknown"; 601 } 602 errno = save_errno; 603 604 /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, 605 * as required by ERR_load_strings. */ 606 607 init = 0; 608 609 CRYPTO_w_unlock(CRYPTO_LOCK_ERR); 610 } 611 #endif 612 613 #define err_clear_data(p,i) \ 614 do { \ 615 if (((p)->err_data[i] != NULL) && \ 616 (p)->err_data_flags[i] & ERR_TXT_MALLOCED) { \ 617 free((p)->err_data[i]); \ 618 (p)->err_data[i] = NULL; \ 619 } \ 620 (p)->err_data_flags[i] = 0; \ 621 } while(0) 622 623 #define err_clear(p,i) \ 624 do { \ 625 (p)->err_flags[i] = 0; \ 626 (p)->err_buffer[i] = 0; \ 627 err_clear_data(p, i); \ 628 (p)->err_file[i] = NULL; \ 629 (p)->err_line[i] = -1; \ 630 } while(0) 631 632 static void 633 ERR_STATE_free(ERR_STATE *s) 634 { 635 int i; 636 637 if (s == NULL) 638 return; 639 640 for (i = 0; i < ERR_NUM_ERRORS; i++) { 641 err_clear_data(s, i); 642 } 643 free(s); 644 } 645 646 void 647 ERR_load_ERR_strings_internal(void) 648 { 649 err_init_thread = pthread_self(); 650 err_fns_check(); 651 #ifndef OPENSSL_NO_ERR 652 err_load_strings(0, ERR_str_libraries); 653 err_load_strings(0, ERR_str_reasons); 654 err_load_strings(ERR_LIB_SYS, ERR_str_functs); 655 build_SYS_str_reasons(); 656 err_load_strings(ERR_LIB_SYS, SYS_str_reasons); 657 #endif 658 } 659 660 661 void 662 ERR_load_ERR_strings(void) 663 { 664 static pthread_once_t once = PTHREAD_ONCE_INIT; 665 666 if (pthread_equal(pthread_self(), err_init_thread)) 667 return; /* don't recurse */ 668 669 /* Prayer and clean living lets you ignore errors, OpenSSL style */ 670 (void) OPENSSL_init_crypto(0, NULL); 671 672 (void) pthread_once(&once, ERR_load_ERR_strings_internal); 673 } 674 LCRYPTO_ALIAS(ERR_load_ERR_strings); 675 676 static void 677 err_load_strings(int lib, ERR_STRING_DATA *str) 678 { 679 while (str->error) { 680 if (lib) 681 str->error |= ERR_PACK(lib, 0, 0); 682 ERRFN(err_set_item)(str); 683 str++; 684 } 685 } 686 687 void 688 ERR_load_strings(int lib, ERR_STRING_DATA *str) 689 { 690 ERR_load_ERR_strings(); 691 err_load_strings(lib, str); 692 } 693 LCRYPTO_ALIAS(ERR_load_strings); 694 695 void 696 ERR_load_const_strings(const ERR_STRING_DATA *str) 697 { 698 ERR_load_ERR_strings(); 699 while (str->error) { 700 ERRFN(err_set_item)(str); 701 str++; 702 } 703 } 704 705 void 706 ERR_unload_strings(int lib, ERR_STRING_DATA *str) 707 { 708 /* Prayer and clean living lets you ignore errors, OpenSSL style */ 709 (void) OPENSSL_init_crypto(0, NULL); 710 711 while (str->error) { 712 if (lib) 713 str->error |= ERR_PACK(lib, 0, 0); 714 ERRFN(err_del_item)(str); 715 str++; 716 } 717 } 718 LCRYPTO_ALIAS(ERR_unload_strings); 719 720 void 721 ERR_free_strings(void) 722 { 723 /* Prayer and clean living lets you ignore errors, OpenSSL style */ 724 (void) OPENSSL_init_crypto(0, NULL); 725 726 err_fns_check(); 727 ERRFN(err_del)(); 728 } 729 LCRYPTO_ALIAS(ERR_free_strings); 730 731 /********************************************************/ 732 733 void 734 ERR_put_error(int lib, int func, int reason, const char *file, int line) 735 { 736 ERR_STATE *es; 737 int save_errno = errno; 738 739 es = ERR_get_state(); 740 741 es->top = (es->top + 1) % ERR_NUM_ERRORS; 742 if (es->top == es->bottom) 743 es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS; 744 es->err_flags[es->top] = 0; 745 es->err_buffer[es->top] = ERR_PACK(lib, func, reason); 746 es->err_file[es->top] = file; 747 es->err_line[es->top] = line; 748 err_clear_data(es, es->top); 749 errno = save_errno; 750 } 751 LCRYPTO_ALIAS(ERR_put_error); 752 753 void 754 ERR_clear_error(void) 755 { 756 int i; 757 ERR_STATE *es; 758 759 es = ERR_get_state(); 760 761 for (i = 0; i < ERR_NUM_ERRORS; i++) { 762 err_clear(es, i); 763 } 764 es->top = es->bottom = 0; 765 } 766 LCRYPTO_ALIAS(ERR_clear_error); 767 768 769 unsigned long 770 ERR_get_error(void) 771 { 772 return (get_error_values(1, 0, NULL, NULL, NULL, NULL)); 773 } 774 LCRYPTO_ALIAS(ERR_get_error); 775 776 unsigned long 777 ERR_get_error_line(const char **file, int *line) 778 { 779 return (get_error_values(1, 0, file, line, NULL, NULL)); 780 } 781 LCRYPTO_ALIAS(ERR_get_error_line); 782 783 unsigned long 784 ERR_get_error_line_data(const char **file, int *line, 785 const char **data, int *flags) 786 { 787 return (get_error_values(1, 0, file, line, data, flags)); 788 } 789 LCRYPTO_ALIAS(ERR_get_error_line_data); 790 791 792 unsigned long 793 ERR_peek_error(void) 794 { 795 return (get_error_values(0, 0, NULL, NULL, NULL, NULL)); 796 } 797 LCRYPTO_ALIAS(ERR_peek_error); 798 799 unsigned long 800 ERR_peek_error_line(const char **file, int *line) 801 { 802 return (get_error_values(0, 0, file, line, NULL, NULL)); 803 } 804 LCRYPTO_ALIAS(ERR_peek_error_line); 805 806 unsigned long 807 ERR_peek_error_line_data(const char **file, int *line, 808 const char **data, int *flags) 809 { 810 return (get_error_values(0, 0, file, line, data, flags)); 811 } 812 LCRYPTO_ALIAS(ERR_peek_error_line_data); 813 814 unsigned long 815 ERR_peek_last_error(void) 816 { 817 return (get_error_values(0, 1, NULL, NULL, NULL, NULL)); 818 } 819 LCRYPTO_ALIAS(ERR_peek_last_error); 820 821 unsigned long 822 ERR_peek_last_error_line(const char **file, int *line) 823 { 824 return (get_error_values(0, 1, file, line, NULL, NULL)); 825 } 826 LCRYPTO_ALIAS(ERR_peek_last_error_line); 827 828 unsigned long 829 ERR_peek_last_error_line_data(const char **file, int *line, 830 const char **data, int *flags) 831 { 832 return (get_error_values(0, 1, file, line, data, flags)); 833 } 834 LCRYPTO_ALIAS(ERR_peek_last_error_line_data); 835 836 static unsigned long 837 get_error_values(int inc, int top, const char **file, int *line, 838 const char **data, int *flags) 839 { 840 int i = 0; 841 ERR_STATE *es; 842 unsigned long ret; 843 844 es = ERR_get_state(); 845 846 if (inc && top) { 847 if (file) 848 *file = ""; 849 if (line) 850 *line = 0; 851 if (data) 852 *data = ""; 853 if (flags) 854 *flags = 0; 855 856 return ERR_R_INTERNAL_ERROR; 857 } 858 859 if (es->bottom == es->top) 860 return 0; 861 if (top) 862 i = es->top; /* last error */ 863 else 864 i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ 865 866 ret = es->err_buffer[i]; 867 if (inc) { 868 es->bottom = i; 869 es->err_buffer[i] = 0; 870 } 871 872 if ((file != NULL) && (line != NULL)) { 873 if (es->err_file[i] == NULL) { 874 *file = "NA"; 875 if (line != NULL) 876 *line = 0; 877 } else { 878 *file = es->err_file[i]; 879 if (line != NULL) 880 *line = es->err_line[i]; 881 } 882 } 883 884 if (data == NULL) { 885 if (inc) { 886 err_clear_data(es, i); 887 } 888 } else { 889 if (es->err_data[i] == NULL) { 890 *data = ""; 891 if (flags != NULL) 892 *flags = 0; 893 } else { 894 *data = es->err_data[i]; 895 if (flags != NULL) 896 *flags = es->err_data_flags[i]; 897 } 898 } 899 return ret; 900 } 901 902 void 903 ERR_error_string_n(unsigned long e, char *buf, size_t len) 904 { 905 char lsbuf[30], fsbuf[30], rsbuf[30]; 906 const char *ls, *fs, *rs; 907 int l, f, r, ret; 908 909 l = ERR_GET_LIB(e); 910 f = ERR_GET_FUNC(e); 911 r = ERR_GET_REASON(e); 912 913 ls = ERR_lib_error_string(e); 914 fs = ERR_func_error_string(e); 915 rs = ERR_reason_error_string(e); 916 917 if (ls == NULL) { 918 (void) snprintf(lsbuf, sizeof(lsbuf), "lib(%d)", l); 919 ls = lsbuf; 920 } 921 if (fs == NULL) { 922 (void) snprintf(fsbuf, sizeof(fsbuf), "func(%d)", f); 923 fs = fsbuf; 924 } 925 if (rs == NULL) { 926 (void) snprintf(rsbuf, sizeof(rsbuf), "reason(%d)", r); 927 rs = rsbuf; 928 } 929 930 ret = snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs); 931 if (ret == -1) 932 return; /* can't happen, and can't do better if it does */ 933 if (ret >= len) { 934 /* output may be truncated; make sure we always have 5 935 * colon-separated fields, i.e. 4 colons ... */ 936 #define NUM_COLONS 4 937 if (len > NUM_COLONS) /* ... if possible */ 938 { 939 int i; 940 char *s = buf; 941 942 for (i = 0; i < NUM_COLONS; i++) { 943 char *colon = strchr(s, ':'); 944 if (colon == NULL || 945 colon > &buf[len - 1] - NUM_COLONS + i) { 946 /* set colon no. i at last possible position 947 * (buf[len-1] is the terminating 0)*/ 948 colon = &buf[len - 1] - NUM_COLONS + i; 949 *colon = ':'; 950 } 951 s = colon + 1; 952 } 953 } 954 } 955 } 956 LCRYPTO_ALIAS(ERR_error_string_n); 957 958 /* BAD for multi-threading: uses a local buffer if ret == NULL */ 959 /* ERR_error_string_n should be used instead for ret != NULL 960 * as ERR_error_string cannot know how large the buffer is */ 961 char * 962 ERR_error_string(unsigned long e, char *ret) 963 { 964 static char buf[256]; 965 966 if (ret == NULL) 967 ret = buf; 968 ERR_error_string_n(e, ret, 256); 969 970 return ret; 971 } 972 LCRYPTO_ALIAS(ERR_error_string); 973 974 const char * 975 ERR_lib_error_string(unsigned long e) 976 { 977 const ERR_STRING_DATA *p; 978 ERR_STRING_DATA d; 979 unsigned long l; 980 981 if (!OPENSSL_init_crypto(0, NULL)) 982 return NULL; 983 984 err_fns_check(); 985 l = ERR_GET_LIB(e); 986 d.error = ERR_PACK(l, 0, 0); 987 p = ERRFN(err_get_item)(&d); 988 return ((p == NULL) ? NULL : p->string); 989 } 990 LCRYPTO_ALIAS(ERR_lib_error_string); 991 992 const char * 993 ERR_func_error_string(unsigned long e) 994 { 995 const ERR_STRING_DATA *p; 996 ERR_STRING_DATA d; 997 unsigned long l, f; 998 999 err_fns_check(); 1000 l = ERR_GET_LIB(e); 1001 f = ERR_GET_FUNC(e); 1002 d.error = ERR_PACK(l, f, 0); 1003 p = ERRFN(err_get_item)(&d); 1004 return ((p == NULL) ? NULL : p->string); 1005 } 1006 LCRYPTO_ALIAS(ERR_func_error_string); 1007 1008 const char * 1009 ERR_reason_error_string(unsigned long e) 1010 { 1011 const ERR_STRING_DATA *p = NULL; 1012 ERR_STRING_DATA d; 1013 unsigned long l, r; 1014 1015 err_fns_check(); 1016 l = ERR_GET_LIB(e); 1017 r = ERR_GET_REASON(e); 1018 d.error = ERR_PACK(l, 0, r); 1019 p = ERRFN(err_get_item)(&d); 1020 if (!p) { 1021 d.error = ERR_PACK(0, 0, r); 1022 p = ERRFN(err_get_item)(&d); 1023 } 1024 return ((p == NULL) ? NULL : p->string); 1025 } 1026 LCRYPTO_ALIAS(ERR_reason_error_string); 1027 1028 void 1029 ERR_remove_thread_state(const CRYPTO_THREADID *id) 1030 { 1031 ERR_STATE tmp; 1032 1033 if (id) 1034 CRYPTO_THREADID_cpy(&tmp.tid, id); 1035 else 1036 CRYPTO_THREADID_current(&tmp.tid); 1037 err_fns_check(); 1038 /* thread_del_item automatically destroys the LHASH if the number of 1039 * items reaches zero. */ 1040 ERRFN(thread_del_item)(&tmp); 1041 } 1042 LCRYPTO_ALIAS(ERR_remove_thread_state); 1043 1044 void 1045 ERR_remove_state(unsigned long pid) 1046 { 1047 ERR_remove_thread_state(NULL); 1048 } 1049 LCRYPTO_ALIAS(ERR_remove_state); 1050 1051 static ERR_STATE * 1052 ERR_get_state(void) 1053 { 1054 static ERR_STATE fallback; 1055 ERR_STATE *ret, tmp, *tmpp = NULL; 1056 int i; 1057 CRYPTO_THREADID tid; 1058 1059 err_fns_check(); 1060 CRYPTO_THREADID_current(&tid); 1061 CRYPTO_THREADID_cpy(&tmp.tid, &tid); 1062 ret = ERRFN(thread_get_item)(&tmp); 1063 1064 /* ret == the error state, if NULL, make a new one */ 1065 if (ret == NULL) { 1066 ret = malloc(sizeof(ERR_STATE)); 1067 if (ret == NULL) 1068 return (&fallback); 1069 CRYPTO_THREADID_cpy(&ret->tid, &tid); 1070 ret->top = 0; 1071 ret->bottom = 0; 1072 for (i = 0; i < ERR_NUM_ERRORS; i++) { 1073 ret->err_data[i] = NULL; 1074 ret->err_data_flags[i] = 0; 1075 } 1076 tmpp = ERRFN(thread_set_item)(ret); 1077 /* To check if insertion failed, do a get. */ 1078 if (ERRFN(thread_get_item)(ret) != ret) { 1079 ERR_STATE_free(ret); /* could not insert it */ 1080 return (&fallback); 1081 } 1082 /* If a race occurred in this function and we came second, tmpp 1083 * is the first one that we just replaced. */ 1084 if (tmpp) 1085 ERR_STATE_free(tmpp); 1086 } 1087 return ret; 1088 } 1089 1090 int 1091 ERR_get_next_error_library(void) 1092 { 1093 err_fns_check(); 1094 return ERRFN(get_next_lib)(); 1095 } 1096 LCRYPTO_ALIAS(ERR_get_next_error_library); 1097 1098 void 1099 ERR_set_error_data(char *data, int flags) 1100 { 1101 ERR_STATE *es; 1102 int i; 1103 1104 es = ERR_get_state(); 1105 1106 i = es->top; 1107 if (i == 0) 1108 i = ERR_NUM_ERRORS - 1; 1109 1110 err_clear_data(es, i); 1111 es->err_data[i] = data; 1112 es->err_data_flags[i] = flags; 1113 } 1114 LCRYPTO_ALIAS(ERR_set_error_data); 1115 1116 void 1117 ERR_asprintf_error_data(char * format, ...) 1118 { 1119 char *errbuf = NULL; 1120 va_list ap; 1121 int r; 1122 1123 va_start(ap, format); 1124 r = vasprintf(&errbuf, format, ap); 1125 va_end(ap); 1126 if (r == -1) 1127 ERR_set_error_data("malloc failed", ERR_TXT_STRING); 1128 else 1129 ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); 1130 } 1131 LCRYPTO_ALIAS(ERR_asprintf_error_data); 1132 1133 int 1134 ERR_set_mark(void) 1135 { 1136 ERR_STATE *es; 1137 1138 es = ERR_get_state(); 1139 1140 if (es->bottom == es->top) 1141 return 0; 1142 es->err_flags[es->top] |= ERR_FLAG_MARK; 1143 return 1; 1144 } 1145 LCRYPTO_ALIAS(ERR_set_mark); 1146 1147 int 1148 ERR_pop_to_mark(void) 1149 { 1150 ERR_STATE *es; 1151 1152 es = ERR_get_state(); 1153 1154 while (es->bottom != es->top && 1155 (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) { 1156 err_clear(es, es->top); 1157 es->top -= 1; 1158 if (es->top == -1) 1159 es->top = ERR_NUM_ERRORS - 1; 1160 } 1161 1162 if (es->bottom == es->top) 1163 return 0; 1164 es->err_flags[es->top]&=~ERR_FLAG_MARK; 1165 return 1; 1166 } 1167 LCRYPTO_ALIAS(ERR_pop_to_mark); 1168 1169 void 1170 err_clear_last_constant_time(int clear) 1171 { 1172 ERR_STATE *es; 1173 int top; 1174 1175 es = ERR_get_state(); 1176 if (es == NULL) 1177 return; 1178 1179 top = es->top; 1180 1181 es->err_flags[top] &= ~(0 - clear); 1182 es->err_buffer[top] &= ~(0UL - clear); 1183 es->err_file[top] = (const char *)((uintptr_t)es->err_file[top] & 1184 ~((uintptr_t)0 - clear)); 1185 es->err_line[top] |= 0 - clear; 1186 1187 es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS; 1188 } 1189