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