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