#include <openssl/err.h>
#include <assert.h>
#include <errno.h>
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <string.h>
#if defined(OPENSSL_WINDOWS)
OPENSSL_MSVC_PRAGMA(warning(push, 3))
#include <windows.h>
OPENSSL_MSVC_PRAGMA(warning(pop))
#endif
#include <openssl/mem.h>
#include <openssl/thread.h>
#include "../internal.h"
#include "./internal.h"
struct err_error_st {
const char *file;
char *data;
uint32_t packed;
uint16_t line;
unsigned mark : 1;
};
typedef struct err_state_st {
struct err_error_st errors[ERR_NUM_ERRORS];
unsigned top;
unsigned bottom;
void *to_free;
} ERR_STATE;
extern const uint32_t kOpenSSLReasonValues[];
extern const size_t kOpenSSLReasonValuesLen;
extern const char kOpenSSLReasonStringData[];
static void err_clear(struct err_error_st *error) {
OPENSSL_free(error->data);
OPENSSL_memset(error, 0, sizeof(struct err_error_st));
}
static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
err_clear(dst);
dst->file = src->file;
if (src->data != NULL) {
dst->data = OPENSSL_strdup(src->data);
}
dst->packed = src->packed;
dst->line = src->line;
}
static int global_next_library = ERR_NUM_LIBS;
static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
CRYPTO_STATIC_MUTEX_INIT;
static void err_state_free(void *statep) {
ERR_STATE *state = statep;
if (state == NULL) {
return;
}
for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
err_clear(&state->errors[i]);
}
OPENSSL_free(state->to_free);
OPENSSL_free(state);
}
static ERR_STATE *err_get_state(void) {
ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
if (state == NULL) {
state = OPENSSL_malloc(sizeof(ERR_STATE));
if (state == NULL) {
return NULL;
}
OPENSSL_memset(state, 0, sizeof(ERR_STATE));
if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
err_state_free)) {
return NULL;
}
}
return state;
}
static uint32_t get_error_values(int inc, int top, const char **file, int *line,
const char **data, int *flags) {
unsigned i = 0;
ERR_STATE *state;
struct err_error_st *error;
uint32_t ret;
state = err_get_state();
if (state == NULL || state->bottom == state->top) {
return 0;
}
if (top) {
assert(!inc);
i = state->top;
} else {
i = (state->bottom + 1) % ERR_NUM_ERRORS;
}
error = &state->errors[i];
ret = error->packed;
if (file != NULL && line != NULL) {
if (error->file == NULL) {
*file = "NA";
*line = 0;
} else {
*file = error->file;
*line = error->line;
}
}
if (data != NULL) {
if (error->data == NULL) {
*data = "";
if (flags != NULL) {
*flags = 0;
}
} else {
*data = error->data;
if (flags != NULL) {
*flags = ERR_FLAG_STRING;
}
if (inc) {
if (error->data != NULL) {
OPENSSL_free(state->to_free);
state->to_free = error->data;
}
error->data = NULL;
}
}
}
if (inc) {
assert(!top);
err_clear(error);
state->bottom = i;
}
return ret;
}
uint32_t ERR_get_error(void) {
return get_error_values(1 , 0 , NULL, NULL, NULL, NULL);
}
uint32_t ERR_get_error_line(const char **file, int *line) {
return get_error_values(1 , 0 , file, line, NULL, NULL);
}
uint32_t ERR_get_error_line_data(const char **file, int *line,
const char **data, int *flags) {
return get_error_values(1 , 0 , file, line, data, flags);
}
uint32_t ERR_peek_error(void) {
return get_error_values(0 , 0 , NULL, NULL, NULL, NULL);
}
uint32_t ERR_peek_error_line(const char **file, int *line) {
return get_error_values(0 , 0 , file, line, NULL, NULL);
}
uint32_t ERR_peek_error_line_data(const char **file, int *line,
const char **data, int *flags) {
return get_error_values(0 , 0 , file, line, data,
flags);
}
uint32_t ERR_peek_last_error(void) {
return get_error_values(0 , 1 , NULL, NULL, NULL, NULL);
}
uint32_t ERR_peek_last_error_line(const char **file, int *line) {
return get_error_values(0 , 1 , file, line, NULL, NULL);
}
uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
const char **data, int *flags) {
return get_error_values(0 , 1 , file, line, data, flags);
}
void ERR_clear_error(void) {
ERR_STATE *const state = err_get_state();
unsigned i;
if (state == NULL) {
return;
}
for (i = 0; i < ERR_NUM_ERRORS; i++) {
err_clear(&state->errors[i]);
}
OPENSSL_free(state->to_free);
state->to_free = NULL;
state->top = state->bottom = 0;
}
void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
if (tid != NULL) {
assert(0);
return;
}
ERR_clear_error();
}
int ERR_get_next_error_library(void) {
int ret;
CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
ret = global_next_library++;
CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex);
return ret;
}
void ERR_remove_state(unsigned long pid) {
ERR_clear_error();
}
void ERR_clear_system_error(void) {
errno = 0;
}
static int err_string_cmp(const void *a, const void *b) {
const uint32_t a_key = *((const uint32_t*) a) >> 15;
const uint32_t b_key = *((const uint32_t*) b) >> 15;
if (a_key < b_key) {
return -1;
} else if (a_key > b_key) {
return 1;
} else {
return 0;
}
}
static const char *err_string_lookup(uint32_t lib, uint32_t key,
const uint32_t *values,
size_t num_values,
const char *string_data) {
if (lib >= (1 << 6) || key >= (1 << 11)) {
return NULL;
}
uint32_t search_key = lib << 26 | key << 15;
const uint32_t *result = bsearch(&search_key, values, num_values,
sizeof(uint32_t), err_string_cmp);
if (result == NULL) {
return NULL;
}
return &string_data[(*result) & 0x7fff];
}
static const char *const kLibraryNames[ERR_NUM_LIBS] = {
"invalid library (0)",
"unknown library", "system library", "bignum routines", "RSA routines", "Diffie-Hellman routines", "public key routines", "memory buffer routines", "object identifier routines", "PEM routines", "DSA routines", "X.509 certificate routines", "ASN.1 encoding routines", "configuration file routines", "common libcrypto routines", "elliptic curve routines", "SSL routines", "BIO routines", "PKCS7 routines", "PKCS8 routines", "X509 V3 routines", "random number generator", "ENGINE routines", "OCSP routines", "UI routines", "COMP routines", "ECDSA routines", "ECDH routines", "HMAC routines", "Digest functions", "Cipher functions", "HKDF functions", "Trust Token functions", "User defined functions", };
static const char *err_lib_error_string(uint32_t packed_error) {
const uint32_t lib = ERR_GET_LIB(packed_error);
if (lib >= ERR_NUM_LIBS) {
return NULL;
}
return kLibraryNames[lib];
}
const char *ERR_lib_error_string(uint32_t packed_error) {
const char *ret = err_lib_error_string(packed_error);
return ret == NULL ? "unknown library" : ret;
}
const char *ERR_func_error_string(uint32_t packed_error) {
return "OPENSSL_internal";
}
static const char *err_reason_error_string(uint32_t packed_error) {
const uint32_t lib = ERR_GET_LIB(packed_error);
const uint32_t reason = ERR_GET_REASON(packed_error);
if (lib == ERR_LIB_SYS) {
if (reason < 127) {
return strerror(reason);
}
return NULL;
}
if (reason < ERR_NUM_LIBS) {
return kLibraryNames[reason];
}
if (reason < 100) {
switch (reason) {
case ERR_R_MALLOC_FAILURE:
return "malloc failure";
case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
return "function should not have been called";
case ERR_R_PASSED_NULL_PARAMETER:
return "passed a null parameter";
case ERR_R_INTERNAL_ERROR:
return "internal error";
case ERR_R_OVERFLOW:
return "overflow";
default:
return NULL;
}
}
return err_string_lookup(lib, reason, kOpenSSLReasonValues,
kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
}
const char *ERR_reason_error_string(uint32_t packed_error) {
const char *ret = err_reason_error_string(packed_error);
return ret == NULL ? "unknown error" : ret;
}
char *ERR_error_string(uint32_t packed_error, char *ret) {
static char buf[ERR_ERROR_STRING_BUF_LEN];
if (ret == NULL) {
ret = buf;
}
#if !defined(NDEBUG)
OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
#endif
return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
}
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
if (len == 0) {
return NULL;
}
unsigned lib = ERR_GET_LIB(packed_error);
unsigned reason = ERR_GET_REASON(packed_error);
const char *lib_str = err_lib_error_string(packed_error);
const char *reason_str = err_reason_error_string(packed_error);
char lib_buf[64], reason_buf[64];
if (lib_str == NULL) {
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
lib_str = lib_buf;
}
if (reason_str == NULL) {
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
reason_str = reason_buf;
}
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
packed_error, lib_str, reason_str);
if (strlen(buf) == len - 1) {
static const unsigned num_colons = 4;
unsigned i;
char *s = buf;
if (len <= num_colons) {
return buf;
}
for (i = 0; i < num_colons; i++) {
char *colon = strchr(s, ':');
char *last_pos = &buf[len - 1] - num_colons + i;
if (colon == NULL || colon > last_pos) {
OPENSSL_memset(last_pos, ':', num_colons - i);
break;
}
s = colon + 1;
}
}
return buf;
}
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
char buf[ERR_ERROR_STRING_BUF_LEN];
char buf2[1024];
const char *file, *data;
int line, flags;
uint32_t packed_error;
const unsigned long thread_hash = (uintptr_t) err_get_state();
for (;;) {
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
if (packed_error == 0) {
break;
}
ERR_error_string_n(packed_error, buf, sizeof(buf));
BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
file, line, (flags & ERR_FLAG_STRING) ? data : "");
if (callback(buf2, strlen(buf2), ctx) <= 0) {
break;
}
}
}
static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
assert(msg[msg_len] == '\0');
FILE* fp = ctx;
int res = fputs(msg, fp);
return res < 0 ? 0 : 1;
}
void ERR_print_errors_fp(FILE *file) {
ERR_print_errors_cb(print_errors_to_file, file);
}
static void err_set_error_data(char *data) {
ERR_STATE *const state = err_get_state();
struct err_error_st *error;
if (state == NULL || state->top == state->bottom) {
OPENSSL_free(data);
return;
}
error = &state->errors[state->top];
OPENSSL_free(error->data);
error->data = data;
}
void ERR_put_error(int library, int unused, int reason, const char *file,
unsigned line) {
ERR_STATE *const state = err_get_state();
struct err_error_st *error;
if (state == NULL) {
return;
}
if (library == ERR_LIB_SYS && reason == 0) {
#if defined(OPENSSL_WINDOWS)
reason = GetLastError();
#else
reason = errno;
#endif
}
state->top = (state->top + 1) % ERR_NUM_ERRORS;
if (state->top == state->bottom) {
state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
}
error = &state->errors[state->top];
err_clear(error);
error->file = file;
error->line = line;
error->packed = ERR_PACK(library, reason);
}
static void err_add_error_vdata(unsigned num, va_list args) {
size_t alloced, new_len, len = 0, substr_len;
char *buf;
const char *substr;
unsigned i;
alloced = 80;
buf = OPENSSL_malloc(alloced + 1);
if (buf == NULL) {
return;
}
for (i = 0; i < num; i++) {
substr = va_arg(args, const char *);
if (substr == NULL) {
continue;
}
substr_len = strlen(substr);
new_len = len + substr_len;
if (new_len > alloced) {
char *new_buf;
if (alloced + 20 + 1 < alloced) {
OPENSSL_free(buf);
return;
}
alloced = new_len + 20;
new_buf = OPENSSL_realloc(buf, alloced + 1);
if (new_buf == NULL) {
OPENSSL_free(buf);
return;
}
buf = new_buf;
}
OPENSSL_memcpy(buf + len, substr, substr_len);
len = new_len;
}
buf[len] = 0;
err_set_error_data(buf);
}
void ERR_add_error_data(unsigned count, ...) {
va_list args;
va_start(args, count);
err_add_error_vdata(count, args);
va_end(args);
}
void ERR_add_error_dataf(const char *format, ...) {
va_list ap;
char *buf;
static const unsigned buf_len = 256;
buf = OPENSSL_malloc(buf_len + 1);
if (buf == NULL) {
return;
}
va_start(ap, format);
BIO_vsnprintf(buf, buf_len, format, ap);
buf[buf_len] = 0;
va_end(ap);
err_set_error_data(buf);
}
void ERR_set_error_data(char *data, int flags) {
if (!(flags & ERR_FLAG_STRING)) {
assert(0);
return;
}
if (flags & ERR_FLAG_MALLOCED) {
err_set_error_data(data);
} else {
char *copy = OPENSSL_strdup(data);
if (copy != NULL) {
err_set_error_data(copy);
}
}
}
int ERR_set_mark(void) {
ERR_STATE *const state = err_get_state();
if (state == NULL || state->bottom == state->top) {
return 0;
}
state->errors[state->top].mark = 1;
return 1;
}
int ERR_pop_to_mark(void) {
ERR_STATE *const state = err_get_state();
if (state == NULL) {
return 0;
}
while (state->bottom != state->top) {
struct err_error_st *error = &state->errors[state->top];
if (error->mark) {
error->mark = 0;
return 1;
}
err_clear(error);
if (state->top == 0) {
state->top = ERR_NUM_ERRORS - 1;
} else {
state->top--;
}
}
return 0;
}
void ERR_load_crypto_strings(void) {}
void ERR_free_strings(void) {}
void ERR_load_BIO_strings(void) {}
void ERR_load_ERR_strings(void) {}
void ERR_load_RAND_strings(void) {}
struct err_save_state_st {
struct err_error_st *errors;
size_t num_errors;
};
void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
if (state == NULL) {
return;
}
for (size_t i = 0; i < state->num_errors; i++) {
err_clear(&state->errors[i]);
}
OPENSSL_free(state->errors);
OPENSSL_free(state);
}
ERR_SAVE_STATE *ERR_save_state(void) {
ERR_STATE *const state = err_get_state();
if (state == NULL || state->top == state->bottom) {
return NULL;
}
ERR_SAVE_STATE *ret = OPENSSL_malloc(sizeof(ERR_SAVE_STATE));
if (ret == NULL) {
return NULL;
}
size_t num_errors = state->top >= state->bottom
? state->top - state->bottom
: ERR_NUM_ERRORS + state->top - state->bottom;
assert(num_errors < ERR_NUM_ERRORS);
ret->errors = OPENSSL_malloc(num_errors * sizeof(struct err_error_st));
if (ret->errors == NULL) {
OPENSSL_free(ret);
return NULL;
}
OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
ret->num_errors = num_errors;
for (size_t i = 0; i < num_errors; i++) {
size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
err_copy(&ret->errors[i], &state->errors[j]);
}
return ret;
}
void ERR_restore_state(const ERR_SAVE_STATE *state) {
if (state == NULL || state->num_errors == 0) {
ERR_clear_error();
return;
}
ERR_STATE *const dst = err_get_state();
if (dst == NULL) {
return;
}
for (size_t i = 0; i < state->num_errors; i++) {
err_copy(&dst->errors[i], &state->errors[i]);
}
dst->top = state->num_errors - 1;
dst->bottom = ERR_NUM_ERRORS - 1;
}