| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // Ensure we can't call OPENSSL_malloc circularly. |
| 16 | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
| 17 | #include <openssl/err.h> |
| 18 |
| 19 | #include <assert.h> |
| 20 | #include <errno.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <string.h> |
| 25 |
| 26 | #if defined(OPENSSL_WINDOWS) |
| 27 | #include <windows.h> |
| 28 | #endif |
| 29 |
| 30 | #include <openssl/mem.h> |
| 31 | #include <openssl/thread.h> |
| 32 |
| 33 | #include "../internal.h" |
| 34 | #include "./internal.h" |
| 35 |
| 36 |
| 37 | namespace { |
| 38 | struct err_error_st { |
| 39 | // file contains the filename where the error occurred. |
| 40 | const char *file; |
| 41 | // data contains a NUL-terminated string with optional data. It is allocated |
| 42 | // with system |malloc| and must be freed with |free| (not |OPENSSL_free|) |
| 43 | char *data; |
| 44 | // packed contains the error library and reason, as packed by ERR_PACK. |
| 45 | uint32_t packed; |
| 46 | // line contains the line number where the error occurred. |
| 47 | uint16_t line; |
| 48 | // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|. |
| 49 | unsigned mark : 1; |
| 50 | }; |
| 51 |
| 52 | // ERR_STATE contains the per-thread, error queue. |
| 53 | typedef struct err_state_st { |
| 54 | // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a |
| 55 | // ring buffer. |
| 56 | struct err_error_st errors[ERR_NUM_ERRORS]; |
| 57 | // top contains the index of the most recent error. If |top| equals |bottom| |
| 58 | // then the queue is empty. |
| 59 | unsigned top; |
| 60 | // bottom contains the index before the least recent error in the queue. |
| 61 | unsigned bottom; |
| 62 |
| 63 | // to_free, if not NULL, contains a pointer owned by this structure that was |
| 64 | // previously a |data| pointer of one of the elements of |errors|. |
| 65 | void *to_free; |
| 66 | } ERR_STATE; |
| 67 | } // namespace |
| 68 |
| 69 | extern const uint32_t kOpenSSLReasonValues[]; |
| 70 | extern const size_t kOpenSSLReasonValuesLen; |
| 71 | extern const char kOpenSSLReasonStringData[]; |
| 72 |
| 73 | static char *strdup_libc_malloc(const char *str) { |
| 74 | // |strdup| is not in C until C23, so MSVC triggers deprecation warnings, and |
| 75 | // glibc and musl gate it on a feature macro. Reimplementing it is easier. |
| 76 | size_t len = strlen(str); |
| 77 | char *ret = reinterpret_cast<char *>(malloc(len + 1)); |
| 78 | if (ret != NULL) { |
| 79 | memcpy(ret, str, len + 1); |
| 80 | } |
| 81 | return ret; |
| 82 | } |
| 83 |
| 84 | // err_clear clears the given queued error. |
| 85 | static void err_clear(struct err_error_st *error) { |
| 86 | free(error->data); |
| 87 | OPENSSL_memset(error, 0, sizeof(struct err_error_st)); |
| 88 | } |
| 89 |
| 90 | static void err_copy(struct err_error_st *dst, const struct err_error_st *src) { |
| 91 | err_clear(dst); |
| 92 | dst->file = src->file; |
| 93 | if (src->data != NULL) { |
| 94 | // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
| 95 | // which can affect the error stack. |
| 96 | dst->data = strdup_libc_malloc(src->data); |
| 97 | } |
| 98 | dst->packed = src->packed; |
| 99 | dst->line = src->line; |
| 100 | } |
| 101 |
| 102 |
| 103 | // global_next_library contains the next custom library value to return. |
| 104 | static int global_next_library = ERR_NUM_LIBS; |
| 105 |
| 106 | // global_next_library_mutex protects |global_next_library| from concurrent |
| 107 | // updates. |
| 108 | static CRYPTO_MUTEX global_next_library_mutex = CRYPTO_MUTEX_INIT; |
| 109 |
| 110 | static void err_state_free(void *statep) { |
| 111 | ERR_STATE *state = reinterpret_cast<ERR_STATE *>(statep); |
| 112 |
| 113 | if (state == NULL) { |
| 114 | return; |
| 115 | } |
| 116 |
| 117 | for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) { |
| 118 | err_clear(&state->errors[i]); |
| 119 | } |
| 120 | free(state->to_free); |
| 121 | free(state); |
| 122 | } |
| 123 |
| 124 | // err_get_state gets the ERR_STATE object for the current thread. |
| 125 | static ERR_STATE *err_get_state(void) { |
| 126 | ERR_STATE *state = reinterpret_cast<ERR_STATE *>( |
| 127 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR)); |
| 128 | if (state == NULL) { |
| 129 | state = reinterpret_cast<ERR_STATE *>(malloc(sizeof(ERR_STATE))); |
| 130 | if (state == NULL) { |
| 131 | return NULL; |
| 132 | } |
| 133 | OPENSSL_memset(state, 0, sizeof(ERR_STATE)); |
| 134 | if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
| 135 | err_state_free)) { |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 |
| 140 | return state; |
| 141 | } |
| 142 |
| 143 | static uint32_t get_error_values(int inc, int top, const char **file, int *line, |
| 144 | const char **data, int *flags) { |
| 145 | unsigned i = 0; |
| 146 | ERR_STATE *state; |
| 147 | struct err_error_st *error; |
| 148 | uint32_t ret; |
| 149 |
| 150 | state = err_get_state(); |
| 151 | if (state == NULL || state->bottom == state->top) { |
| 152 | return 0; |
| 153 | } |
| 154 |
| 155 | if (top) { |
| 156 | assert(!inc); |
| 157 | // last error |
| 158 | i = state->top; |
| 159 | } else { |
| 160 | i = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 161 | } |
| 162 |
| 163 | error = &state->errors[i]; |
| 164 | ret = error->packed; |
| 165 |
| 166 | if (file != NULL && line != NULL) { |
| 167 | if (error->file == NULL) { |
| 168 | *file = "NA"; |
| 169 | *line = 0; |
| 170 | } else { |
| 171 | *file = error->file; |
| 172 | *line = error->line; |
| 173 | } |
| 174 | } |
| 175 |
| 176 | if (data != NULL) { |
| 177 | if (error->data == NULL) { |
| 178 | *data = ""; |
| 179 | if (flags != NULL) { |
| 180 | *flags = 0; |
| 181 | } |
| 182 | } else { |
| 183 | *data = error->data; |
| 184 | if (flags != NULL) { |
| 185 | // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a |
| 186 | // static lifetime. In both cases, we retain ownership of the string, |
| 187 | // and the caller is not expected to free it. |
| 188 | *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED; |
| 189 | } |
| 190 | // If this error is being removed, take ownership of data from |
| 191 | // the error. The semantics are such that the caller doesn't |
| 192 | // take ownership either. Instead the error system takes |
| 193 | // ownership and retains it until the next call that affects the |
| 194 | // error queue. |
| 195 | if (inc) { |
| 196 | if (error->data != NULL) { |
| 197 | free(state->to_free); |
| 198 | state->to_free = error->data; |
| 199 | } |
| 200 | error->data = NULL; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 |
| 205 | if (inc) { |
| 206 | assert(!top); |
| 207 | err_clear(error); |
| 208 | state->bottom = i; |
| 209 | } |
| 210 |
| 211 | return ret; |
| 212 | } |
| 213 |
| 214 | uint32_t ERR_get_error(void) { |
| 215 | return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
| 216 | } |
| 217 |
| 218 | uint32_t ERR_get_error_line(const char **file, int *line) { |
| 219 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
| 220 | } |
| 221 |
| 222 | uint32_t ERR_get_error_line_data(const char **file, int *line, |
| 223 | const char **data, int *flags) { |
| 224 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
| 225 | } |
| 226 |
| 227 | uint32_t ERR_peek_error(void) { |
| 228 | return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
| 229 | } |
| 230 |
| 231 | uint32_t ERR_peek_error_line(const char **file, int *line) { |
| 232 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
| 233 | } |
| 234 |
| 235 | uint32_t ERR_peek_error_line_data(const char **file, int *line, |
| 236 | const char **data, int *flags) { |
| 237 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
| 238 | flags); |
| 239 | } |
| 240 |
| 241 | uint32_t ERR_peek_last_error(void) { |
| 242 | return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
| 243 | } |
| 244 |
| 245 | uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
| 246 | return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
| 247 | } |
| 248 |
| 249 | uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
| 250 | const char **data, int *flags) { |
| 251 | return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
| 252 | } |
| 253 |
| 254 | void ERR_clear_error(void) { |
| 255 | ERR_STATE *const state = err_get_state(); |
| 256 | unsigned i; |
| 257 |
| 258 | if (state == NULL) { |
| 259 | return; |
| 260 | } |
| 261 |
| 262 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 263 | err_clear(&state->errors[i]); |
| 264 | } |
| 265 | free(state->to_free); |
| 266 | state->to_free = NULL; |
| 267 |
| 268 | state->top = state->bottom = 0; |
| 269 | } |
| 270 |
| 271 | void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
| 272 | if (tid != NULL) { |
| 273 | assert(0); |
| 274 | return; |
| 275 | } |
| 276 |
| 277 | ERR_clear_error(); |
| 278 | } |
| 279 |
| 280 | int ERR_get_next_error_library(void) { |
| 281 | int ret; |
| 282 |
| 283 | CRYPTO_MUTEX_lock_write(&global_next_library_mutex); |
| 284 | ret = global_next_library++; |
| 285 | CRYPTO_MUTEX_unlock_write(&global_next_library_mutex); |
| 286 |
| 287 | return ret; |
| 288 | } |
| 289 |
| 290 | void ERR_remove_state(unsigned long pid) { ERR_clear_error(); } |
| 291 |
| 292 | void ERR_clear_system_error(void) { errno = 0; } |
| 293 |
| 294 | // err_string_cmp is a compare function for searching error values with |
| 295 | // |bsearch| in |err_string_lookup|. |
| 296 | static int err_string_cmp(const void *a, const void *b) { |
| 297 | const uint32_t a_key = *((const uint32_t *)a) >> 15; |
| 298 | const uint32_t b_key = *((const uint32_t *)b) >> 15; |
| 299 |
| 300 | if (a_key < b_key) { |
| 301 | return -1; |
| 302 | } else if (a_key > b_key) { |
| 303 | return 1; |
| 304 | } else { |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 |
| 309 | // err_string_lookup looks up the string associated with |lib| and |key| in |
| 310 | // |values| and |string_data|. It returns the string or NULL if not found. |
| 311 | static const char *err_string_lookup(uint32_t lib, uint32_t key, |
| 312 | const uint32_t *values, size_t num_values, |
| 313 | const char *string_data) { |
| 314 | // |values| points to data in err_data.h, which is generated by |
| 315 | // err_data_generate.go. It's an array of uint32_t values. Each value has the |
| 316 | // following structure: |
| 317 | // | lib | key | offset | |
| 318 | // |6 bits| 11 bits | 15 bits | |
| 319 | // |
| 320 | // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
| 321 | // The |key| is a reason code, depending on the context. |
| 322 | // The |offset| is the number of bytes from the start of |string_data| where |
| 323 | // the (NUL terminated) string for this value can be found. |
| 324 | // |
| 325 | // Values are sorted based on treating the |lib| and |key| part as an |
| 326 | // unsigned integer. |
| 327 | if (lib >= (1 << 6) || key >= (1 << 11)) { |
| 328 | return NULL; |
| 329 | } |
| 330 | uint32_t search_key = lib << 26 | key << 15; |
| 331 | const uint32_t *result = reinterpret_cast<const uint32_t *>(bsearch( |
| 332 | &search_key, values, num_values, sizeof(uint32_t), err_string_cmp)); |
| 333 | if (result == NULL) { |
| 334 | return NULL; |
| 335 | } |
| 336 |
| 337 | return &string_data[(*result) & 0x7fff]; |
| 338 | } |
| 339 |
| 340 | namespace { |
| 341 | typedef struct library_name_st { |
| 342 | const char *str; |
| 343 | const char *symbol; |
| 344 | const char *reason_symbol; |
| 345 | } LIBRARY_NAME; |
| 346 | } // namespace |
| 347 |
| 348 | static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = { |
| 349 | {"invalid library (0)", NULL, NULL}, |
| 350 | {"unknown library", "NONE", "NONE_LIB"}, |
| 351 | {"system library", "SYS", "SYS_LIB"}, |
| 352 | {"bignum routines", "BN", "BN_LIB"}, |
| 353 | {"RSA routines", "RSA", "RSA_LIB"}, |
| 354 | {"Diffie-Hellman routines", "DH", "DH_LIB"}, |
| 355 | {"public key routines", "EVP", "EVP_LIB"}, |
| 356 | {"memory buffer routines", "BUF", "BUF_LIB"}, |
| 357 | {"object identifier routines", "OBJ", "OBJ_LIB"}, |
| 358 | {"PEM routines", "PEM", "PEM_LIB"}, |
| 359 | {"DSA routines", "DSA", "DSA_LIB"}, |
| 360 | {"X.509 certificate routines", "X509", "X509_LIB"}, |
| 361 | {"ASN.1 encoding routines", "ASN1", "ASN1_LIB"}, |
| 362 | {"configuration file routines", "CONF", "CONF_LIB"}, |
| 363 | {"common libcrypto routines", "CRYPTO", "CRYPTO_LIB"}, |
| 364 | {"elliptic curve routines", "EC", "EC_LIB"}, |
| 365 | {"SSL routines", "SSL", "SSL_LIB"}, |
| 366 | {"BIO routines", "BIO", "BIO_LIB"}, |
| 367 | {"PKCS7 routines", "PKCS7", "PKCS7_LIB"}, |
| 368 | {"PKCS8 routines", "PKCS8", "PKCS8_LIB"}, |
| 369 | {"X509 V3 routines", "X509V3", "X509V3_LIB"}, |
| 370 | {"random number generator", "RAND", "RAND_LIB"}, |
| 371 | {"ENGINE routines", "ENGINE", "ENGINE_LIB"}, |
| 372 | {"OCSP routines", "OCSP", "OCSP_LIB"}, |
| 373 | {"UI routines", "UI", "UI_LIB"}, |
| 374 | {"COMP routines", "COMP", "COMP_LIB"}, |
| 375 | {"ECDSA routines", "ECDSA", "ECDSA_LIB"}, |
| 376 | {"ECDH routines", "ECDH", "ECDH_LIB"}, |
| 377 | {"HMAC routines", "HMAC", "HMAC_LIB"}, |
| 378 | {"Digest functions", "DIGEST", "DIGEST_LIB"}, |
| 379 | {"Cipher functions", "CIPHER", "CIPHER_LIB"}, |
| 380 | {"HKDF functions", "HKDF", "HKDF_LIB"}, |
| 381 | {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"}, |
| 382 | {"User defined functions", "USER", "USER_LIB"}, |
| 383 | }; |
| 384 |
| 385 | static const char *err_lib_error_string(uint32_t packed_error) { |
| 386 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 387 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].str; |
| 388 | } |
| 389 |
| 390 | const char *ERR_lib_error_string(uint32_t packed_error) { |
| 391 | const char *ret = err_lib_error_string(packed_error); |
| 392 | return ret == NULL ? "unknown library" : ret; |
| 393 | } |
| 394 |
| 395 | const char *ERR_lib_symbol_name(uint32_t packed_error) { |
| 396 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 397 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].symbol; |
| 398 | } |
| 399 |
| 400 | const char *ERR_func_error_string(uint32_t packed_error) { |
| 401 | return "OPENSSL_internal"; |
| 402 | } |
| 403 |
| 404 | static const char *err_reason_error_string(uint32_t packed_error, int symbol) { |
| 405 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 406 | const uint32_t reason = ERR_GET_REASON(packed_error); |
| 407 |
| 408 | if (lib == ERR_LIB_SYS) { |
| 409 | if (!symbol && reason < 127) { |
| 410 | return strerror(reason); |
| 411 | } |
| 412 | return NULL; |
| 413 | } |
| 414 |
| 415 | if (reason < ERR_NUM_LIBS) { |
| 416 | return symbol ? kLibraryNames[reason].reason_symbol |
| 417 | : kLibraryNames[reason].str; |
| 418 | } |
| 419 |
| 420 | if (reason < 100) { |
| 421 | // TODO(davidben): All our other reason strings match the symbol name. Only |
| 422 | // the common ones differ. Should we just consistently return the symbol |
| 423 | // name? |
| 424 | switch (reason) { |
| 425 | case ERR_R_MALLOC_FAILURE: |
| 426 | return symbol ? "MALLOC_FAILURE" : "malloc failure"; |
| 427 | case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
| 428 | return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED" |
| 429 | : "function should not have been called"; |
| 430 | case ERR_R_PASSED_NULL_PARAMETER: |
| 431 | return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter"; |
| 432 | case ERR_R_INTERNAL_ERROR: |
| 433 | return symbol ? "INTERNAL_ERROR" : "internal error"; |
| 434 | case ERR_R_OVERFLOW: |
| 435 | return symbol ? "OVERFLOW" : "overflow"; |
| 436 | default: |
| 437 | return NULL; |
| 438 | } |
| 439 | } |
| 440 |
| 441 | // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we |
| 442 | // do not need to check |symbol|. |
| 443 | return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
| 444 | kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
| 445 | } |
| 446 |
| 447 | const char *ERR_reason_error_string(uint32_t packed_error) { |
| 448 | const char *ret = err_reason_error_string(packed_error, /*symbol=*/0); |
| 449 | return ret == NULL ? "unknown error" : ret; |
| 450 | } |
| 451 |
| 452 | const char *ERR_reason_symbol_name(uint32_t packed_error) { |
| 453 | return err_reason_error_string(packed_error, /*symbol=*/1); |
| 454 | } |
| 455 |
| 456 | char *ERR_error_string(uint32_t packed_error, char *ret) { |
| 457 | static char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 458 |
| 459 | if (ret == NULL) { |
| 460 | // TODO(fork): remove this. |
| 461 | ret = buf; |
| 462 | } |
| 463 |
| 464 | #if !defined(NDEBUG) |
| 465 | // This is aimed to help catch callers who don't provide |
| 466 | // |ERR_ERROR_STRING_BUF_LEN| bytes of space. |
| 467 | OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
| 468 | #endif |
| 469 |
| 470 | return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
| 471 | } |
| 472 |
| 473 | char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
| 474 | if (len == 0) { |
| 475 | return NULL; |
| 476 | } |
| 477 |
| 478 | unsigned lib = ERR_GET_LIB(packed_error); |
| 479 | unsigned reason = ERR_GET_REASON(packed_error); |
| 480 |
| 481 | const char *lib_str = err_lib_error_string(packed_error); |
| 482 | const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0); |
| 483 |
| 484 | char lib_buf[32], reason_buf[32]; |
| 485 | if (lib_str == NULL) { |
| 486 | snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
| 487 | lib_str = lib_buf; |
| 488 | } |
| 489 |
| 490 | if (reason_str == NULL) { |
| 491 | snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
| 492 | reason_str = reason_buf; |
| 493 | } |
| 494 |
| 495 | int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
| 496 | packed_error, lib_str, reason_str); |
| 497 | if (ret >= 0 && (size_t)ret >= len) { |
| 498 | // The output was truncated; make sure we always have 5 colon-separated |
| 499 | // fields, i.e. 4 colons. |
| 500 | static const unsigned num_colons = 4; |
| 501 | unsigned i; |
| 502 | char *s = buf; |
| 503 |
| 504 | if (len <= num_colons) { |
| 505 | // In this situation it's not possible to ensure that the correct number |
| 506 | // of colons are included in the output. |
| 507 | return buf; |
| 508 | } |
| 509 |
| 510 | for (i = 0; i < num_colons; i++) { |
| 511 | char *colon = strchr(s, ':'); |
| 512 | char *last_pos = &buf[len - 1] - num_colons + i; |
| 513 |
| 514 | if (colon == NULL || colon > last_pos) { |
| 515 | // set colon |i| at last possible position (buf[len-1] is the |
| 516 | // terminating 0). If we're setting this colon, then all whole of the |
| 517 | // rest of the string must be colons in order to have the correct |
| 518 | // number. |
| 519 | OPENSSL_memset(last_pos, ':', num_colons - i); |
| 520 | break; |
| 521 | } |
| 522 |
| 523 | s = colon + 1; |
| 524 | } |
| 525 | } |
| 526 |
| 527 | return buf; |
| 528 | } |
| 529 |
| 530 | void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { |
| 531 | char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 532 | char buf2[1024]; |
| 533 | const char *file, *data; |
| 534 | int line, flags; |
| 535 | uint32_t packed_error; |
| 536 |
| 537 | // thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
| 538 | // for this thread. |
| 539 | const unsigned long thread_hash = (uintptr_t)err_get_state(); |
| 540 |
| 541 | for (;;) { |
| 542 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 543 | if (packed_error == 0) { |
| 544 | break; |
| 545 | } |
| 546 |
| 547 | ERR_error_string_n(packed_error, buf, sizeof(buf)); |
| 548 | snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file, |
| 549 | line, (flags & ERR_FLAG_STRING) ? data : ""); |
| 550 | if (callback(buf2, strlen(buf2), ctx) <= 0) { |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 |
| 556 | static int print_errors_to_file(const char *msg, size_t msg_len, void *ctx) { |
| 557 | assert(msg[msg_len] == '\0'); |
| 558 | FILE *fp = reinterpret_cast<FILE *>(ctx); |
| 559 | int res = fputs(msg, fp); |
| 560 | return res < 0 ? 0 : 1; |
| 561 | } |
| 562 |
| 563 | void ERR_print_errors_fp(FILE *file) { |
| 564 | ERR_print_errors_cb(print_errors_to_file, file); |
| 565 | } |
| 566 |
| 567 | // err_set_error_data sets the data on the most recent error. |
| 568 | static void err_set_error_data(char *data) { |
| 569 | ERR_STATE *const state = err_get_state(); |
| 570 | struct err_error_st *error; |
| 571 |
| 572 | if (state == NULL || state->top == state->bottom) { |
| 573 | free(data); |
| 574 | return; |
| 575 | } |
| 576 |
| 577 | error = &state->errors[state->top]; |
| 578 |
| 579 | free(error->data); |
| 580 | error->data = data; |
| 581 | } |
| 582 |
| 583 | void ERR_put_error(int library, int unused, int reason, const char *file, |
| 584 | unsigned line) { |
| 585 | ERR_STATE *const state = err_get_state(); |
| 586 | struct err_error_st *error; |
| 587 |
| 588 | if (state == NULL) { |
| 589 | return; |
| 590 | } |
| 591 |
| 592 | if (library == ERR_LIB_SYS && reason == 0) { |
| 593 | #if defined(OPENSSL_WINDOWS) |
| 594 | reason = GetLastError(); |
| 595 | #else |
| 596 | reason = errno; |
| 597 | #endif |
| 598 | } |
| 599 |
| 600 | state->top = (state->top + 1) % ERR_NUM_ERRORS; |
| 601 | if (state->top == state->bottom) { |
| 602 | state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 603 | } |
| 604 |
| 605 | error = &state->errors[state->top]; |
| 606 | err_clear(error); |
| 607 | error->file = file; |
| 608 | error->line = line; |
| 609 | error->packed = ERR_PACK(library, reason); |
| 610 | } |
| 611 |
| 612 | // ERR_add_error_data_vdata takes a variable number of const char* pointers, |
| 613 | // concatenates them and sets the result as the data on the most recent |
| 614 | // error. |
| 615 | static void err_add_error_vdata(unsigned num, va_list args) { |
| 616 | size_t total_size = 0; |
| 617 | const char *substr; |
| 618 | char *buf; |
| 619 |
| 620 | va_list args_copy; |
| 621 | va_copy(args_copy, args); |
| 622 | for (size_t i = 0; i < num; i++) { |
| 623 | substr = va_arg(args_copy, const char *); |
| 624 | if (substr == NULL) { |
| 625 | continue; |
| 626 | } |
| 627 | size_t substr_len = strlen(substr); |
| 628 | if (SIZE_MAX - total_size < substr_len) { |
| 629 | return; // Would overflow. |
| 630 | } |
| 631 | total_size += substr_len; |
| 632 | } |
| 633 | va_end(args_copy); |
| 634 | if (total_size == SIZE_MAX) { |
| 635 | return; // Would overflow. |
| 636 | } |
| 637 | total_size += 1; // NUL terminator. |
| 638 | if ((buf = reinterpret_cast<char *>(malloc(total_size))) == NULL) { |
| 639 | return; |
| 640 | } |
| 641 | buf[0] = '\0'; |
| 642 | for (size_t i = 0; i < num; i++) { |
| 643 | substr = va_arg(args, const char *); |
| 644 | if (substr == NULL) { |
| 645 | continue; |
| 646 | } |
| 647 | if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) { |
| 648 | assert(0); // should not be possible. |
| 649 | } |
| 650 | } |
| 651 | err_set_error_data(buf); |
| 652 | } |
| 653 |
| 654 | void ERR_add_error_data(unsigned count, ...) { |
| 655 | va_list args; |
| 656 | va_start(args, count); |
| 657 | err_add_error_vdata(count, args); |
| 658 | va_end(args); |
| 659 | } |
| 660 |
| 661 | void ERR_add_error_dataf(const char *format, ...) { |
| 662 | char *buf = NULL; |
| 663 | va_list ap; |
| 664 |
| 665 | va_start(ap, format); |
| 666 | if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) { |
| 667 | return; |
| 668 | } |
| 669 | va_end(ap); |
| 670 |
| 671 | err_set_error_data(buf); |
| 672 | } |
| 673 |
| 674 | void ERR_set_error_data(char *data, int flags) { |
| 675 | if (!(flags & ERR_FLAG_STRING)) { |
| 676 | // We do not support non-string error data. |
| 677 | assert(0); |
| 678 | return; |
| 679 | } |
| 680 | // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
| 681 | // which can affect the error stack. |
| 682 | char *copy = strdup_libc_malloc(data); |
| 683 | if (copy != NULL) { |
| 684 | err_set_error_data(copy); |
| 685 | } |
| 686 | if (flags & ERR_FLAG_MALLOCED) { |
| 687 | // We can not take ownership of |data| directly because it is allocated with |
| 688 | // |OPENSSL_malloc| and we will free it with system |free| later. |
| 689 | OPENSSL_free(data); |
| 690 | } |
| 691 | } |
| 692 |
| 693 | int ERR_set_mark(void) { |
| 694 | ERR_STATE *const state = err_get_state(); |
| 695 |
| 696 | if (state == NULL || state->bottom == state->top) { |
| 697 | return 0; |
| 698 | } |
| 699 | state->errors[state->top].mark = 1; |
| 700 | return 1; |
| 701 | } |
| 702 |
| 703 | int ERR_pop_to_mark(void) { |
| 704 | ERR_STATE *const state = err_get_state(); |
| 705 |
| 706 | if (state == NULL) { |
| 707 | return 0; |
| 708 | } |
| 709 |
| 710 | while (state->bottom != state->top) { |
| 711 | struct err_error_st *error = &state->errors[state->top]; |
| 712 |
| 713 | if (error->mark) { |
| 714 | error->mark = 0; |
| 715 | return 1; |
| 716 | } |
| 717 |
| 718 | err_clear(error); |
| 719 | if (state->top == 0) { |
| 720 | state->top = ERR_NUM_ERRORS - 1; |
| 721 | } else { |
| 722 | state->top--; |
| 723 | } |
| 724 | } |
| 725 |
| 726 | return 0; |
| 727 | } |
| 728 |
| 729 | void ERR_load_crypto_strings(void) {} |
| 730 |
| 731 | void ERR_free_strings(void) {} |
| 732 |
| 733 | void ERR_load_BIO_strings(void) {} |
| 734 |
| 735 | void ERR_load_ERR_strings(void) {} |
| 736 |
| 737 | void ERR_load_RAND_strings(void) {} |
| 738 |
| 739 | struct err_save_state_st { |
| 740 | struct err_error_st *errors; |
| 741 | size_t num_errors; |
| 742 | }; |
| 743 |
| 744 | void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) { |
| 745 | if (state == NULL) { |
| 746 | return; |
| 747 | } |
| 748 | for (size_t i = 0; i < state->num_errors; i++) { |
| 749 | err_clear(&state->errors[i]); |
| 750 | } |
| 751 | free(state->errors); |
| 752 | free(state); |
| 753 | } |
| 754 |
| 755 | ERR_SAVE_STATE *ERR_save_state(void) { |
| 756 | ERR_STATE *const state = err_get_state(); |
| 757 | if (state == NULL || state->top == state->bottom) { |
| 758 | return NULL; |
| 759 | } |
| 760 |
| 761 | ERR_SAVE_STATE *ret = |
| 762 | reinterpret_cast<ERR_SAVE_STATE *>(malloc(sizeof(ERR_SAVE_STATE))); |
| 763 | if (ret == NULL) { |
| 764 | return NULL; |
| 765 | } |
| 766 |
| 767 | // Errors are stored in the range (bottom, top]. |
| 768 | size_t num_errors = state->top >= state->bottom |
| 769 | ? state->top - state->bottom |
| 770 | : ERR_NUM_ERRORS + state->top - state->bottom; |
| 771 | assert(num_errors < ERR_NUM_ERRORS); |
| 772 | ret->errors = reinterpret_cast<err_error_st *>( |
| 773 | malloc(num_errors * sizeof(struct err_error_st))); |
| 774 | if (ret->errors == NULL) { |
| 775 | free(ret); |
| 776 | return NULL; |
| 777 | } |
| 778 | OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st)); |
| 779 | ret->num_errors = num_errors; |
| 780 |
| 781 | for (size_t i = 0; i < num_errors; i++) { |
| 782 | size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS; |
| 783 | err_copy(&ret->errors[i], &state->errors[j]); |
| 784 | } |
| 785 | return ret; |
| 786 | } |
| 787 |
| 788 | void ERR_restore_state(const ERR_SAVE_STATE *state) { |
| 789 | if (state == NULL || state->num_errors == 0) { |
| 790 | ERR_clear_error(); |
| 791 | return; |
| 792 | } |
| 793 |
| 794 | if (state->num_errors >= ERR_NUM_ERRORS) { |
| 795 | abort(); |
| 796 | } |
| 797 |
| 798 | ERR_STATE *const dst = err_get_state(); |
| 799 | if (dst == NULL) { |
| 800 | return; |
| 801 | } |
| 802 |
| 803 | for (size_t i = 0; i < state->num_errors; i++) { |
| 804 | err_copy(&dst->errors[i], &state->errors[i]); |
| 805 | } |
| 806 | dst->top = (unsigned)(state->num_errors - 1); |
| 807 | dst->bottom = ERR_NUM_ERRORS - 1; |
| 808 | } |
| 809 |