| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 6 |
* The implementation was written so as to conform with Netscapes SSL. |
| 7 |
* |
| 8 |
* This library is free for commercial and non-commercial use as long as |
| 9 |
* the following conditions are aheared to. The following conditions |
| 10 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 11 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 |
* included with this distribution is covered by the same copyright terms |
| 13 |
* except that the holder is Tim Hudson ([email protected]). |
| 14 |
* |
| 15 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 |
* the code are not to be removed. |
| 17 |
* If this package is used in a product, Eric Young should be given attribution |
| 18 |
* as the author of the parts of the library used. |
| 19 |
* This can be in the form of a textual message at program startup or |
| 20 |
* in documentation (online or textual) provided with the package. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 3. All advertising materials mentioning features or use of this software |
| 31 |
* must display the following acknowledgement: |
| 32 |
* "This product includes cryptographic software written by |
| 33 |
* Eric Young ([email protected])" |
| 34 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 35 |
* being used are not cryptographic related :-). |
| 36 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 |
* the apps directory (application code) you must include an acknowledgement: |
| 38 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 |
* SUCH DAMAGE. |
| 51 |
* |
| 52 |
* The licence and distribution terms for any publically available version or |
| 53 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 |
* copied and put under another distribution licence |
| 55 |
* [including the GNU Public Licence.] |
| 56 |
*/ |
| 57 |
/* ==================================================================== |
| 58 |
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 59 |
* |
| 60 |
* Redistribution and use in source and binary forms, with or without |
| 61 |
* modification, are permitted provided that the following conditions |
| 62 |
* are met: |
| 63 |
* |
| 64 |
* 1. Redistributions of source code must retain the above copyright |
| 65 |
* notice, this list of conditions and the following disclaimer. |
| 66 |
* |
| 67 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 68 |
* notice, this list of conditions and the following disclaimer in |
| 69 |
* the documentation and/or other materials provided with the |
| 70 |
* distribution. |
| 71 |
* |
| 72 |
* 3. All advertising materials mentioning features or use of this |
| 73 |
* software must display the following acknowledgment: |
| 74 |
* "This product includes software developed by the OpenSSL Project |
| 75 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 |
* |
| 77 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 |
* endorse or promote products derived from this software without |
| 79 |
* prior written permission. For written permission, please contact |
| 80 |
* [email protected]. |
| 81 |
* |
| 82 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 83 |
* nor may "OpenSSL" appear in their names without prior written |
| 84 |
* permission of the OpenSSL Project. |
| 85 |
* |
| 86 |
* 6. Redistributions of any form whatsoever must retain the following |
| 87 |
* acknowledgment: |
| 88 |
* "This product includes software developed by the OpenSSL Project |
| 89 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 |
* |
| 91 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 |
* ==================================================================== |
| 104 |
* |
| 105 |
* This product includes cryptographic software written by Eric Young |
| 106 |
* ([email protected]). This product includes software written by Tim |
| 107 |
* Hudson ([email protected]). */ |
| 108 |
|
| 109 |
#include <openssl/err.h> |
| 110 |
|
| 111 |
#include <assert.h> |
| 112 |
#include <errno.h> |
| 113 |
#include <inttypes.h> |
| 114 |
#include <string.h> |
| 115 |
|
| 116 |
#if defined(OPENSSL_WINDOWS) |
| 117 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 118 |
#include <windows.h> |
| 119 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 120 |
#endif |
| 121 |
|
| 122 |
#include <openssl/mem.h> |
| 123 |
#include <openssl/thread.h> |
| 124 |
|
| 125 |
#include "../internal.h" |
| 126 |
|
| 127 |
|
| 128 |
extern const uint32_t kOpenSSLReasonValues[]; |
| 129 |
extern const size_t kOpenSSLReasonValuesLen; |
| 130 |
extern const char kOpenSSLReasonStringData[]; |
| 131 |
|
| 132 |
/* err_clear_data frees the optional |data| member of the given error. */ |
| 133 |
static void err_clear_data(struct err_error_st *error) { |
| 134 |
if ((error->flags & ERR_FLAG_MALLOCED) != 0) { |
| 135 |
OPENSSL_free(error->data); |
| 136 |
} |
| 137 |
error->data = NULL; |
| 138 |
error->flags &= ~ERR_FLAG_MALLOCED; |
| 139 |
} |
| 140 |
|
| 141 |
/* err_clear clears the given queued error. */ |
| 142 |
static void err_clear(struct err_error_st *error) { |
| 143 |
err_clear_data(error); |
| 144 |
memset(error, 0, sizeof(struct err_error_st)); |
| 145 |
} |
| 146 |
|
| 147 |
/* global_next_library contains the next custom library value to return. */ |
| 148 |
static int global_next_library = ERR_NUM_LIBS; |
| 149 |
|
| 150 |
/* global_next_library_mutex protects |global_next_library| from concurrent |
| 151 |
* updates. */ |
| 152 |
static struct CRYPTO_STATIC_MUTEX global_next_library_mutex = |
| 153 |
CRYPTO_STATIC_MUTEX_INIT; |
| 154 |
|
| 155 |
static void err_state_free(void *statep) { |
| 156 |
ERR_STATE *state = statep; |
| 157 |
|
| 158 |
if (state == NULL) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
unsigned i; |
| 163 |
for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 164 |
err_clear(&state->errors[i]); |
| 165 |
} |
| 166 |
OPENSSL_free(state->to_free); |
| 167 |
OPENSSL_free(state); |
| 168 |
} |
| 169 |
|
| 170 |
/* err_get_state gets the ERR_STATE object for the current thread. */ |
| 171 |
static ERR_STATE *err_get_state(void) { |
| 172 |
ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR); |
| 173 |
if (state == NULL) { |
| 174 |
state = OPENSSL_malloc(sizeof(ERR_STATE)); |
| 175 |
if (state == NULL) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
memset(state, 0, sizeof(ERR_STATE)); |
| 179 |
if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
| 180 |
err_state_free)) { |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return state; |
| 186 |
} |
| 187 |
|
| 188 |
static uint32_t get_error_values(int inc, int top, const char **file, int *line, |
| 189 |
const char **data, int *flags) { |
| 190 |
unsigned i = 0; |
| 191 |
ERR_STATE *state; |
| 192 |
struct err_error_st *error; |
| 193 |
uint32_t ret; |
| 194 |
|
| 195 |
state = err_get_state(); |
| 196 |
if (state == NULL || state->bottom == state->top) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
if (top) { |
| 201 |
assert(!inc); |
| 202 |
/* last error */ |
| 203 |
i = state->top; |
| 204 |
} else { |
| 205 |
i = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 206 |
} |
| 207 |
|
| 208 |
error = &state->errors[i]; |
| 209 |
ret = error->packed; |
| 210 |
|
| 211 |
if (file != NULL && line != NULL) { |
| 212 |
if (error->file == NULL) { |
| 213 |
*file = "NA"; |
| 214 |
*line = 0; |
| 215 |
} else { |
| 216 |
*file = error->file; |
| 217 |
*line = error->line; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (data != NULL) { |
| 222 |
if (error->data == NULL) { |
| 223 |
*data = ""; |
| 224 |
if (flags != NULL) { |
| 225 |
*flags = 0; |
| 226 |
} |
| 227 |
} else { |
| 228 |
*data = error->data; |
| 229 |
if (flags != NULL) { |
| 230 |
*flags = error->flags & ERR_FLAG_PUBLIC_MASK; |
| 231 |
} |
| 232 |
/* If this error is being removed, take ownership of data from |
| 233 |
* the error. The semantics are such that the caller doesn't |
| 234 |
* take ownership either. Instead the error system takes |
| 235 |
* ownership and retains it until the next call that affects the |
| 236 |
* error queue. */ |
| 237 |
if (inc) { |
| 238 |
if (error->flags & ERR_FLAG_MALLOCED) { |
| 239 |
OPENSSL_free(state->to_free); |
| 240 |
state->to_free = error->data; |
| 241 |
} |
| 242 |
error->data = NULL; |
| 243 |
error->flags = 0; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (inc) { |
| 249 |
assert(!top); |
| 250 |
err_clear(error); |
| 251 |
state->bottom = i; |
| 252 |
} |
| 253 |
|
| 254 |
return ret; |
| 255 |
} |
| 256 |
|
| 257 |
uint32_t ERR_get_error(void) { |
| 258 |
return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
| 259 |
} |
| 260 |
|
| 261 |
uint32_t ERR_get_error_line(const char **file, int *line) { |
| 262 |
return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
| 263 |
} |
| 264 |
|
| 265 |
uint32_t ERR_get_error_line_data(const char **file, int *line, |
| 266 |
const char **data, int *flags) { |
| 267 |
return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
| 268 |
} |
| 269 |
|
| 270 |
uint32_t ERR_peek_error(void) { |
| 271 |
return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
| 272 |
} |
| 273 |
|
| 274 |
uint32_t ERR_peek_error_line(const char **file, int *line) { |
| 275 |
return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
| 276 |
} |
| 277 |
|
| 278 |
uint32_t ERR_peek_error_line_data(const char **file, int *line, |
| 279 |
const char **data, int *flags) { |
| 280 |
return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
| 281 |
flags); |
| 282 |
} |
| 283 |
|
| 284 |
uint32_t ERR_peek_last_error(void) { |
| 285 |
return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
| 286 |
} |
| 287 |
|
| 288 |
uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
| 289 |
return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
| 290 |
} |
| 291 |
|
| 292 |
uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
| 293 |
const char **data, int *flags) { |
| 294 |
return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
| 295 |
} |
| 296 |
|
| 297 |
void ERR_clear_error(void) { |
| 298 |
ERR_STATE *const state = err_get_state(); |
| 299 |
unsigned i; |
| 300 |
|
| 301 |
if (state == NULL) { |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 306 |
err_clear(&state->errors[i]); |
| 307 |
} |
| 308 |
OPENSSL_free(state->to_free); |
| 309 |
state->to_free = NULL; |
| 310 |
|
| 311 |
state->top = state->bottom = 0; |
| 312 |
} |
| 313 |
|
| 314 |
void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
| 315 |
if (tid != NULL) { |
| 316 |
assert(0); |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
ERR_clear_error(); |
| 321 |
} |
| 322 |
|
| 323 |
int ERR_get_next_error_library(void) { |
| 324 |
int ret; |
| 325 |
|
| 326 |
CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex); |
| 327 |
ret = global_next_library++; |
| 328 |
CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex); |
| 329 |
|
| 330 |
return ret; |
| 331 |
} |
| 332 |
|
| 333 |
void ERR_remove_state(unsigned long pid) { |
| 334 |
ERR_clear_error(); |
| 335 |
} |
| 336 |
|
| 337 |
void ERR_clear_system_error(void) { |
| 338 |
errno = 0; |
| 339 |
} |
| 340 |
|
| 341 |
char *ERR_error_string(uint32_t packed_error, char *ret) { |
| 342 |
static char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 343 |
|
| 344 |
if (ret == NULL) { |
| 345 |
/* TODO(fork): remove this. */ |
| 346 |
ret = buf; |
| 347 |
} |
| 348 |
|
| 349 |
#if !defined(NDEBUG) |
| 350 |
/* This is aimed to help catch callers who don't provide |
| 351 |
* |ERR_ERROR_STRING_BUF_LEN| bytes of space. */ |
| 352 |
memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
| 353 |
#endif |
| 354 |
|
| 355 |
ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
| 356 |
|
| 357 |
return ret; |
| 358 |
} |
| 359 |
|
| 360 |
void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
| 361 |
char lib_buf[64], reason_buf[64]; |
| 362 |
const char *lib_str, *reason_str; |
| 363 |
unsigned lib, reason; |
| 364 |
|
| 365 |
if (len == 0) { |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
lib = ERR_GET_LIB(packed_error); |
| 370 |
reason = ERR_GET_REASON(packed_error); |
| 371 |
|
| 372 |
lib_str = ERR_lib_error_string(packed_error); |
| 373 |
reason_str = ERR_reason_error_string(packed_error); |
| 374 |
|
| 375 |
if (lib_str == NULL) { |
| 376 |
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
| 377 |
lib_str = lib_buf; |
| 378 |
} |
| 379 |
|
| 380 |
if (reason_str == NULL) { |
| 381 |
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
| 382 |
reason_str = reason_buf; |
| 383 |
} |
| 384 |
|
| 385 |
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
| 386 |
packed_error, lib_str, reason_str); |
| 387 |
|
| 388 |
if (strlen(buf) == len - 1) { |
| 389 |
/* output may be truncated; make sure we always have 5 colon-separated |
| 390 |
* fields, i.e. 4 colons. */ |
| 391 |
static const unsigned num_colons = 4; |
| 392 |
unsigned i; |
| 393 |
char *s = buf; |
| 394 |
|
| 395 |
if (len <= num_colons) { |
| 396 |
/* In this situation it's not possible to ensure that the correct number |
| 397 |
* of colons are included in the output. */ |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
for (i = 0; i < num_colons; i++) { |
| 402 |
char *colon = strchr(s, ':'); |
| 403 |
char *last_pos = &buf[len - 1] - num_colons + i; |
| 404 |
|
| 405 |
if (colon == NULL || colon > last_pos) { |
| 406 |
/* set colon |i| at last possible position (buf[len-1] is the |
| 407 |
* terminating 0). If we're setting this colon, then all whole of the |
| 408 |
* rest of the string must be colons in order to have the correct |
| 409 |
* number. */ |
| 410 |
memset(last_pos, ':', num_colons - i); |
| 411 |
break; |
| 412 |
} |
| 413 |
|
| 414 |
s = colon + 1; |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
// err_string_cmp is a compare function for searching error values with |
| 420 |
// |bsearch| in |err_string_lookup|. |
| 421 |
static int err_string_cmp(const void *a, const void *b) { |
| 422 |
const uint32_t a_key = *((const uint32_t*) a) >> 15; |
| 423 |
const uint32_t b_key = *((const uint32_t*) b) >> 15; |
| 424 |
|
| 425 |
if (a_key < b_key) { |
| 426 |
return -1; |
| 427 |
} else if (a_key > b_key) { |
| 428 |
return 1; |
| 429 |
} else { |
| 430 |
return 0; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/* err_string_lookup looks up the string associated with |lib| and |key| in |
| 435 |
* |values| and |string_data|. It returns the string or NULL if not found. */ |
| 436 |
static const char *err_string_lookup(uint32_t lib, uint32_t key, |
| 437 |
const uint32_t *values, |
| 438 |
size_t num_values, |
| 439 |
const char *string_data) { |
| 440 |
/* |values| points to data in err_data.h, which is generated by |
| 441 |
* err_data_generate.go. It's an array of uint32_t values. Each value has the |
| 442 |
* following structure: |
| 443 |
* | lib | key | offset | |
| 444 |
* |6 bits| 11 bits | 15 bits | |
| 445 |
* |
| 446 |
* The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
| 447 |
* The |key| is a reason code, depending on the context. |
| 448 |
* The |offset| is the number of bytes from the start of |string_data| where |
| 449 |
* the (NUL terminated) string for this value can be found. |
| 450 |
* |
| 451 |
* Values are sorted based on treating the |lib| and |key| part as an |
| 452 |
* unsigned integer. */ |
| 453 |
if (lib >= (1 << 6) || key >= (1 << 11)) { |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
uint32_t search_key = lib << 26 | key << 15; |
| 457 |
const uint32_t *result = bsearch(&search_key, values, num_values, |
| 458 |
sizeof(uint32_t), err_string_cmp); |
| 459 |
if (result == NULL) { |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
return &string_data[(*result) & 0x7fff]; |
| 464 |
} |
| 465 |
|
| 466 |
static const char *const kLibraryNames[ERR_NUM_LIBS] = { |
| 467 |
"invalid library (0)", |
| 468 |
"unknown library", /* ERR_LIB_NONE */ |
| 469 |
"system library", /* ERR_LIB_SYS */ |
| 470 |
"bignum routines", /* ERR_LIB_BN */ |
| 471 |
"RSA routines", /* ERR_LIB_RSA */ |
| 472 |
"Diffie-Hellman routines", /* ERR_LIB_DH */ |
| 473 |
"public key routines", /* ERR_LIB_EVP */ |
| 474 |
"memory buffer routines", /* ERR_LIB_BUF */ |
| 475 |
"object identifier routines", /* ERR_LIB_OBJ */ |
| 476 |
"PEM routines", /* ERR_LIB_PEM */ |
| 477 |
"DSA routines", /* ERR_LIB_DSA */ |
| 478 |
"X.509 certificate routines", /* ERR_LIB_X509 */ |
| 479 |
"ASN.1 encoding routines", /* ERR_LIB_ASN1 */ |
| 480 |
"configuration file routines", /* ERR_LIB_CONF */ |
| 481 |
"common libcrypto routines", /* ERR_LIB_CRYPTO */ |
| 482 |
"elliptic curve routines", /* ERR_LIB_EC */ |
| 483 |
"SSL routines", /* ERR_LIB_SSL */ |
| 484 |
"BIO routines", /* ERR_LIB_BIO */ |
| 485 |
"PKCS7 routines", /* ERR_LIB_PKCS7 */ |
| 486 |
"PKCS8 routines", /* ERR_LIB_PKCS8 */ |
| 487 |
"X509 V3 routines", /* ERR_LIB_X509V3 */ |
| 488 |
"random number generator", /* ERR_LIB_RAND */ |
| 489 |
"ENGINE routines", /* ERR_LIB_ENGINE */ |
| 490 |
"OCSP routines", /* ERR_LIB_OCSP */ |
| 491 |
"UI routines", /* ERR_LIB_UI */ |
| 492 |
"COMP routines", /* ERR_LIB_COMP */ |
| 493 |
"ECDSA routines", /* ERR_LIB_ECDSA */ |
| 494 |
"ECDH routines", /* ERR_LIB_ECDH */ |
| 495 |
"HMAC routines", /* ERR_LIB_HMAC */ |
| 496 |
"Digest functions", /* ERR_LIB_DIGEST */ |
| 497 |
"Cipher functions", /* ERR_LIB_CIPHER */ |
| 498 |
"HKDF functions", /* ERR_LIB_HKDF */ |
| 499 |
"User defined functions", /* ERR_LIB_USER */ |
| 500 |
}; |
| 501 |
|
| 502 |
const char *ERR_lib_error_string(uint32_t packed_error) { |
| 503 |
const uint32_t lib = ERR_GET_LIB(packed_error); |
| 504 |
|
| 505 |
if (lib >= ERR_NUM_LIBS) { |
| 506 |
return NULL; |
| 507 |
} |
| 508 |
return kLibraryNames[lib]; |
| 509 |
} |
| 510 |
|
| 511 |
const char *ERR_func_error_string(uint32_t packed_error) { |
| 512 |
return "OPENSSL_internal"; |
| 513 |
} |
| 514 |
|
| 515 |
const char *ERR_reason_error_string(uint32_t packed_error) { |
| 516 |
const uint32_t lib = ERR_GET_LIB(packed_error); |
| 517 |
const uint32_t reason = ERR_GET_REASON(packed_error); |
| 518 |
|
| 519 |
if (lib == ERR_LIB_SYS) { |
| 520 |
if (reason < 127) { |
| 521 |
return strerror(reason); |
| 522 |
} |
| 523 |
return NULL; |
| 524 |
} |
| 525 |
|
| 526 |
if (reason < ERR_NUM_LIBS) { |
| 527 |
return kLibraryNames[reason]; |
| 528 |
} |
| 529 |
|
| 530 |
if (reason < 100) { |
| 531 |
switch (reason) { |
| 532 |
case ERR_R_MALLOC_FAILURE: |
| 533 |
return "malloc failure"; |
| 534 |
case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
| 535 |
return "function should not have been called"; |
| 536 |
case ERR_R_PASSED_NULL_PARAMETER: |
| 537 |
return "passed a null parameter"; |
| 538 |
case ERR_R_INTERNAL_ERROR: |
| 539 |
return "internal error"; |
| 540 |
case ERR_R_OVERFLOW: |
| 541 |
return "overflow"; |
| 542 |
default: |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
| 548 |
kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
| 549 |
} |
| 550 |
|
| 551 |
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { |
| 552 |
char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 553 |
char buf2[1024]; |
| 554 |
const char *file, *data; |
| 555 |
int line, flags; |
| 556 |
uint32_t packed_error; |
| 557 |
|
| 558 |
/* thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
| 559 |
* for this thread. */ |
| 560 |
const unsigned long thread_hash = (uintptr_t) err_get_state(); |
| 561 |
|
| 562 |
for (;;) { |
| 563 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 564 |
if (packed_error == 0) { |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
ERR_error_string_n(packed_error, buf, sizeof(buf)); |
| 569 |
BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, |
| 570 |
file, line, (flags & ERR_FLAG_STRING) ? data : ""); |
| 571 |
if (callback(buf2, strlen(buf2), ctx) <= 0) { |
| 572 |
break; |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) { |
| 578 |
assert(msg[msg_len] == '\0'); |
| 579 |
FILE* fp = ctx; |
| 580 |
int res = fputs(msg, fp); |
| 581 |
return res < 0 ? 0 : 1; |
| 582 |
} |
| 583 |
|
| 584 |
void ERR_print_errors_fp(FILE *file) { |
| 585 |
ERR_print_errors_cb(print_errors_to_file, file); |
| 586 |
} |
| 587 |
|
| 588 |
/* err_set_error_data sets the data on the most recent error. The |flags| |
| 589 |
* argument is a combination of the |ERR_FLAG_*| values. */ |
| 590 |
static void err_set_error_data(char *data, int flags) { |
| 591 |
ERR_STATE *const state = err_get_state(); |
| 592 |
struct err_error_st *error; |
| 593 |
|
| 594 |
if (state == NULL || state->top == state->bottom) { |
| 595 |
if (flags & ERR_FLAG_MALLOCED) { |
| 596 |
OPENSSL_free(data); |
| 597 |
} |
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
error = &state->errors[state->top]; |
| 602 |
|
| 603 |
err_clear_data(error); |
| 604 |
error->data = data; |
| 605 |
error->flags = flags; |
| 606 |
} |
| 607 |
|
| 608 |
void ERR_put_error(int library, int unused, int reason, const char *file, |
| 609 |
unsigned line) { |
| 610 |
ERR_STATE *const state = err_get_state(); |
| 611 |
struct err_error_st *error; |
| 612 |
|
| 613 |
if (state == NULL) { |
| 614 |
return; |
| 615 |
} |
| 616 |
|
| 617 |
if (library == ERR_LIB_SYS && reason == 0) { |
| 618 |
#if defined(OPENSSL_WINDOWS) |
| 619 |
reason = GetLastError(); |
| 620 |
#else |
| 621 |
reason = errno; |
| 622 |
#endif |
| 623 |
} |
| 624 |
|
| 625 |
state->top = (state->top + 1) % ERR_NUM_ERRORS; |
| 626 |
if (state->top == state->bottom) { |
| 627 |
state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 628 |
} |
| 629 |
|
| 630 |
error = &state->errors[state->top]; |
| 631 |
err_clear(error); |
| 632 |
error->file = file; |
| 633 |
error->line = line; |
| 634 |
error->packed = ERR_PACK(library, reason); |
| 635 |
} |
| 636 |
|
| 637 |
/* ERR_add_error_data_vdata takes a variable number of const char* pointers, |
| 638 |
* concatenates them and sets the result as the data on the most recent |
| 639 |
* error. */ |
| 640 |
static void err_add_error_vdata(unsigned num, va_list args) { |
| 641 |
size_t alloced, new_len, len = 0, substr_len; |
| 642 |
char *buf; |
| 643 |
const char *substr; |
| 644 |
unsigned i; |
| 645 |
|
| 646 |
alloced = 80; |
| 647 |
buf = OPENSSL_malloc(alloced + 1); |
| 648 |
if (buf == NULL) { |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
for (i = 0; i < num; i++) { |
| 653 |
substr = va_arg(args, const char *); |
| 654 |
if (substr == NULL) { |
| 655 |
continue; |
| 656 |
} |
| 657 |
|
| 658 |
substr_len = strlen(substr); |
| 659 |
new_len = len + substr_len; |
| 660 |
if (new_len > alloced) { |
| 661 |
char *new_buf; |
| 662 |
|
| 663 |
if (alloced + 20 + 1 < alloced) { |
| 664 |
/* overflow. */ |
| 665 |
OPENSSL_free(buf); |
| 666 |
return; |
| 667 |
} |
| 668 |
|
| 669 |
alloced = new_len + 20; |
| 670 |
new_buf = OPENSSL_realloc(buf, alloced + 1); |
| 671 |
if (new_buf == NULL) { |
| 672 |
OPENSSL_free(buf); |
| 673 |
return; |
| 674 |
} |
| 675 |
buf = new_buf; |
| 676 |
} |
| 677 |
|
| 678 |
memcpy(buf + len, substr, substr_len); |
| 679 |
len = new_len; |
| 680 |
} |
| 681 |
|
| 682 |
buf[len] = 0; |
| 683 |
err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING); |
| 684 |
} |
| 685 |
|
| 686 |
void ERR_add_error_data(unsigned count, ...) { |
| 687 |
va_list args; |
| 688 |
va_start(args, count); |
| 689 |
err_add_error_vdata(count, args); |
| 690 |
va_end(args); |
| 691 |
} |
| 692 |
|
| 693 |
void ERR_add_error_dataf(const char *format, ...) { |
| 694 |
va_list ap; |
| 695 |
char *buf; |
| 696 |
static const unsigned buf_len = 256; |
| 697 |
|
| 698 |
/* A fixed-size buffer is used because va_copy (which would be needed in |
| 699 |
* order to call vsnprintf twice and measure the buffer) wasn't defined until |
| 700 |
* C99. */ |
| 701 |
buf = OPENSSL_malloc(buf_len + 1); |
| 702 |
if (buf == NULL) { |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
va_start(ap, format); |
| 707 |
BIO_vsnprintf(buf, buf_len, format, ap); |
| 708 |
buf[buf_len] = 0; |
| 709 |
va_end(ap); |
| 710 |
|
| 711 |
err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING); |
| 712 |
} |
| 713 |
|
| 714 |
int ERR_set_mark(void) { |
| 715 |
ERR_STATE *const state = err_get_state(); |
| 716 |
|
| 717 |
if (state == NULL || state->bottom == state->top) { |
| 718 |
return 0; |
| 719 |
} |
| 720 |
state->errors[state->top].flags |= ERR_FLAG_MARK; |
| 721 |
return 1; |
| 722 |
} |
| 723 |
|
| 724 |
int ERR_pop_to_mark(void) { |
| 725 |
ERR_STATE *const state = err_get_state(); |
| 726 |
|
| 727 |
if (state == NULL) { |
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
while (state->bottom != state->top) { |
| 732 |
struct err_error_st *error = &state->errors[state->top]; |
| 733 |
|
| 734 |
if ((error->flags & ERR_FLAG_MARK) != 0) { |
| 735 |
error->flags &= ~ERR_FLAG_MARK; |
| 736 |
return 1; |
| 737 |
} |
| 738 |
|
| 739 |
err_clear(error); |
| 740 |
if (state->top == 0) { |
| 741 |
state->top = ERR_NUM_ERRORS - 1; |
| 742 |
} else { |
| 743 |
state->top--; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
return 0; |
| 748 |
} |
| 749 |
|
| 750 |
void ERR_load_crypto_strings(void) {} |
| 751 |
|
| 752 |
void ERR_free_strings(void) {} |
| 753 |
|
| 754 |
void ERR_load_BIO_strings(void) {} |
| 755 |
|
| 756 |
void ERR_load_ERR_strings(void) {} |