Imported OpenSSL 1.1.1b
This commit is contained in:
112
crypto/err/err.c
112
crypto/err/err.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -19,6 +19,9 @@
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/opensslconf.h>
|
||||
#include "internal/thread_once.h"
|
||||
#include "internal/ctype.h"
|
||||
#include "internal/constant_time_locl.h"
|
||||
#include "e_os.h"
|
||||
|
||||
static int err_load_strings(const ERR_STRING_DATA *str);
|
||||
|
||||
@@ -181,8 +184,9 @@ static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
/* A measurement on Linux 2018-11-21 showed about 3.5kib */
|
||||
# define SPACE_SYS_STR_REASONS 4 * 1024
|
||||
# define NUM_SYS_STR_REASONS 127
|
||||
# define LEN_SYS_STR_REASON 32
|
||||
|
||||
static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
|
||||
/*
|
||||
@@ -198,9 +202,12 @@ static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
|
||||
static void build_SYS_str_reasons(void)
|
||||
{
|
||||
/* OPENSSL_malloc cannot be used here, use static storage instead */
|
||||
static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
|
||||
static char strerror_pool[SPACE_SYS_STR_REASONS];
|
||||
char *cur = strerror_pool;
|
||||
size_t cnt = 0;
|
||||
static int init = 1;
|
||||
int i;
|
||||
int saveerrno = get_last_sys_error();
|
||||
|
||||
CRYPTO_THREAD_write_lock(err_string_lock);
|
||||
if (!init) {
|
||||
@@ -213,9 +220,26 @@ static void build_SYS_str_reasons(void)
|
||||
|
||||
str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
|
||||
if (str->string == NULL) {
|
||||
char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
|
||||
if (openssl_strerror_r(i, *dest, sizeof(*dest)))
|
||||
str->string = *dest;
|
||||
if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
|
||||
size_t l = strlen(cur);
|
||||
|
||||
str->string = cur;
|
||||
cnt += l;
|
||||
if (cnt > sizeof(strerror_pool))
|
||||
cnt = sizeof(strerror_pool);
|
||||
cur += l;
|
||||
|
||||
/*
|
||||
* VMS has an unusual quirk of adding spaces at the end of
|
||||
* some (most? all?) messages. Lets trim them off.
|
||||
*/
|
||||
while (ossl_isspace(cur[-1])) {
|
||||
cur--;
|
||||
cnt--;
|
||||
}
|
||||
*cur++ = '\0';
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
if (str->string == NULL)
|
||||
str->string = "unknown";
|
||||
@@ -229,6 +253,8 @@ static void build_SYS_str_reasons(void)
|
||||
init = 0;
|
||||
|
||||
CRYPTO_THREAD_unlock(err_string_lock);
|
||||
/* openssl_strerror_r could change errno, but we want to preserve it */
|
||||
set_sys_error(saveerrno);
|
||||
err_load_strings(SYS_str_reasons);
|
||||
}
|
||||
#endif
|
||||
@@ -671,6 +697,7 @@ DEFINE_RUN_ONCE_STATIC(err_do_init)
|
||||
ERR_STATE *ERR_get_state(void)
|
||||
{
|
||||
ERR_STATE *state;
|
||||
int saveerrno = get_last_sys_error();
|
||||
|
||||
if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
||||
return NULL;
|
||||
@@ -702,6 +729,7 @@ ERR_STATE *ERR_get_state(void)
|
||||
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
|
||||
}
|
||||
|
||||
set_sys_error(saveerrno);
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -711,6 +739,20 @@ ERR_STATE *ERR_get_state(void)
|
||||
*/
|
||||
int err_shelve_state(void **state)
|
||||
{
|
||||
int saveerrno = get_last_sys_error();
|
||||
|
||||
/*
|
||||
* Note, at present our only caller is OPENSSL_init_crypto(), indirectly
|
||||
* via ossl_init_load_crypto_nodelete(), by which point the requested
|
||||
* "base" initialization has already been performed, so the below call is a
|
||||
* NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
|
||||
*
|
||||
* If are no other valid callers of this function, the call below can be
|
||||
* removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
|
||||
* potential uses that are not from inside OPENSSL_init_crypto(), then this
|
||||
* call is needed, but some care is required to make sure that the re-entry
|
||||
* remains a NOOP.
|
||||
*/
|
||||
if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
||||
return 0;
|
||||
|
||||
@@ -721,6 +763,7 @@ int err_shelve_state(void **state)
|
||||
if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
|
||||
return 0;
|
||||
|
||||
set_sys_error(saveerrno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -747,20 +790,31 @@ int ERR_get_next_error_library(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ERR_set_error_data(char *data, int flags)
|
||||
static int err_set_error_data_int(char *data, int flags)
|
||||
{
|
||||
ERR_STATE *es;
|
||||
int i;
|
||||
|
||||
es = ERR_get_state();
|
||||
if (es == NULL)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
i = es->top;
|
||||
|
||||
err_clear_data(es, i);
|
||||
es->err_data[i] = data;
|
||||
es->err_data_flags[i] = flags;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ERR_set_error_data(char *data, int flags)
|
||||
{
|
||||
/*
|
||||
* This function is void so we cannot propagate the error return. Since it
|
||||
* is also in the public API we can't change the return type.
|
||||
*/
|
||||
err_set_error_data_int(data, flags);
|
||||
}
|
||||
|
||||
void ERR_add_error_data(int num, ...)
|
||||
@@ -800,7 +854,8 @@ void ERR_add_error_vdata(int num, va_list args)
|
||||
}
|
||||
OPENSSL_strlcat(str, a, (size_t)s + 1);
|
||||
}
|
||||
ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
|
||||
if (!err_set_error_data_int(str, ERR_TXT_MALLOCED | ERR_TXT_STRING))
|
||||
OPENSSL_free(str);
|
||||
}
|
||||
|
||||
int ERR_set_mark(void)
|
||||
@@ -857,3 +912,42 @@ int ERR_clear_last_mark(void)
|
||||
es->err_flags[top] &= ~ERR_FLAG_MARK;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef UINTPTR_T
|
||||
# undef UINTPTR_T
|
||||
#endif
|
||||
/*
|
||||
* uintptr_t is the answer, but unfortunately C89, current "least common
|
||||
* denominator" doesn't define it. Most legacy platforms typedef it anyway,
|
||||
* so that attempt to fill the gaps means that one would have to identify
|
||||
* that track these gaps, which would be undesirable. Macro it is...
|
||||
*/
|
||||
#if defined(__VMS) && __INITIAL_POINTER_SIZE==64
|
||||
/*
|
||||
* But we can't use size_t on VMS, because it adheres to sizeof(size_t)==4
|
||||
* even in 64-bit builds, which means that it won't work as mask.
|
||||
*/
|
||||
# define UINTPTR_T unsigned long long
|
||||
#else
|
||||
# define UINTPTR_T size_t
|
||||
#endif
|
||||
|
||||
void err_clear_last_constant_time(int clear)
|
||||
{
|
||||
ERR_STATE *es;
|
||||
int top;
|
||||
|
||||
es = ERR_get_state();
|
||||
if (es == NULL)
|
||||
return;
|
||||
|
||||
top = es->top;
|
||||
|
||||
es->err_flags[top] &= ~(0 - clear);
|
||||
es->err_buffer[top] &= ~(0UL - clear);
|
||||
es->err_file[top] = (const char *)((UINTPTR_T)es->err_file[top] &
|
||||
~((UINTPTR_T)0 - clear));
|
||||
es->err_line[top] |= 0 - clear;
|
||||
|
||||
es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user