2016-05-17 19:38:09 +00:00
|
|
|
/*
|
2019-02-26 14:05:09 +00:00
|
|
|
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
2000-03-04 01:36:53 +00:00
|
|
|
*
|
2016-05-17 19:38:09 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2000-03-04 01:36:53 +00:00
|
|
|
*/
|
1998-12-21 10:52:47 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-05-05 12:35:27 +00:00
|
|
|
#include <stdarg.h>
|
2000-03-04 01:36:53 +00:00
|
|
|
#include <string.h>
|
2017-08-22 12:35:43 +00:00
|
|
|
#include "internal/cryptlib_int.h"
|
|
|
|
#include "internal/err.h"
|
|
|
|
#include "internal/err_int.h"
|
2017-06-07 19:12:03 +00:00
|
|
|
#include <openssl/err.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/buffer.h>
|
2000-04-14 23:36:15 +00:00
|
|
|
#include <openssl/bio.h>
|
2016-01-05 04:00:33 +00:00
|
|
|
#include <openssl/opensslconf.h>
|
2017-08-22 12:35:43 +00:00
|
|
|
#include "internal/thread_once.h"
|
2018-11-24 12:08:56 +00:00
|
|
|
#include "internal/ctype.h"
|
2018-09-01 10:19:30 +00:00
|
|
|
#include "internal/constant_time_locl.h"
|
2018-11-21 11:44:42 +00:00
|
|
|
#include "e_os.h"
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2017-06-07 19:12:03 +00:00
|
|
|
static int err_load_strings(const ERR_STRING_DATA *str);
|
2001-09-04 11:49:14 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
static void ERR_STATE_free(ERR_STATE *s);
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_ERR
|
2015-01-22 03:40:55 +00:00
|
|
|
static ERR_STRING_DATA ERR_str_libraries[] = {
|
|
|
|
{ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
|
|
|
|
{ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
|
|
|
|
{ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
|
|
|
|
{ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
|
2016-12-08 10:16:37 +00:00
|
|
|
{ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
|
2015-01-22 03:40:55 +00:00
|
|
|
{ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
|
|
|
|
{ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
|
2016-02-25 18:11:16 +00:00
|
|
|
{ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
|
2015-10-06 13:47:00 +00:00
|
|
|
{ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
|
2016-03-02 04:11:02 +00:00
|
|
|
{ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
|
2016-12-08 14:51:31 +00:00
|
|
|
{ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
|
2018-01-24 16:56:02 +00:00
|
|
|
{ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
|
2015-01-22 03:40:55 +00:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static ERR_STRING_DATA ERR_str_functs[] = {
|
|
|
|
{ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"},
|
|
|
|
{ERR_PACK(0, SYS_F_CONNECT, 0), "connect"},
|
|
|
|
{ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
|
|
|
|
{ERR_PACK(0, SYS_F_SOCKET, 0), "socket"},
|
|
|
|
{ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"},
|
|
|
|
{ERR_PACK(0, SYS_F_BIND, 0), "bind"},
|
|
|
|
{ERR_PACK(0, SYS_F_LISTEN, 0), "listen"},
|
|
|
|
{ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"},
|
|
|
|
# ifdef OPENSSL_SYS_WINDOWS
|
|
|
|
{ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"},
|
|
|
|
# endif
|
|
|
|
{ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"},
|
|
|
|
{ERR_PACK(0, SYS_F_FREAD, 0), "fread"},
|
2016-02-02 15:25:42 +00:00
|
|
|
{ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"},
|
|
|
|
{ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"},
|
2016-02-02 20:04:54 +00:00
|
|
|
{ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"},
|
|
|
|
{ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"},
|
|
|
|
{ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"},
|
2016-02-04 14:20:26 +00:00
|
|
|
{ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"},
|
2017-04-28 18:14:59 +00:00
|
|
|
{ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"},
|
2017-03-24 15:19:00 +00:00
|
|
|
{ERR_PACK(0, SYS_F_OPEN, 0), "open"},
|
|
|
|
{ERR_PACK(0, SYS_F_CLOSE, 0), "close"},
|
|
|
|
{ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"},
|
2017-02-07 15:19:40 +00:00
|
|
|
{ERR_PACK(0, SYS_F_STAT, 0), "stat"},
|
2017-07-27 15:27:20 +00:00
|
|
|
{ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"},
|
2018-03-05 19:40:02 +00:00
|
|
|
{ERR_PACK(0, SYS_F_FSTAT, 0), "fstat"},
|
2015-01-22 03:40:55 +00:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static ERR_STRING_DATA ERR_str_reasons[] = {
|
|
|
|
{ERR_R_SYS_LIB, "system lib"},
|
|
|
|
{ERR_R_BN_LIB, "BN lib"},
|
|
|
|
{ERR_R_RSA_LIB, "RSA lib"},
|
|
|
|
{ERR_R_DH_LIB, "DH lib"},
|
|
|
|
{ERR_R_EVP_LIB, "EVP lib"},
|
|
|
|
{ERR_R_BUF_LIB, "BUF lib"},
|
|
|
|
{ERR_R_OBJ_LIB, "OBJ lib"},
|
|
|
|
{ERR_R_PEM_LIB, "PEM lib"},
|
|
|
|
{ERR_R_DSA_LIB, "DSA lib"},
|
|
|
|
{ERR_R_X509_LIB, "X509 lib"},
|
|
|
|
{ERR_R_ASN1_LIB, "ASN1 lib"},
|
|
|
|
{ERR_R_EC_LIB, "EC lib"},
|
|
|
|
{ERR_R_BIO_LIB, "BIO lib"},
|
|
|
|
{ERR_R_PKCS7_LIB, "PKCS7 lib"},
|
|
|
|
{ERR_R_X509V3_LIB, "X509V3 lib"},
|
|
|
|
{ERR_R_ENGINE_LIB, "ENGINE lib"},
|
2016-12-06 03:29:08 +00:00
|
|
|
{ERR_R_UI_LIB, "UI lib"},
|
2016-12-08 14:51:31 +00:00
|
|
|
{ERR_R_OSSL_STORE_LIB, "STORE lib"},
|
2015-01-22 03:40:55 +00:00
|
|
|
{ERR_R_ECDSA_LIB, "ECDSA lib"},
|
|
|
|
|
|
|
|
{ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
|
|
|
|
{ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
|
|
|
|
|
|
|
|
{ERR_R_FATAL, "fatal"},
|
|
|
|
{ERR_R_MALLOC_FAILURE, "malloc failure"},
|
|
|
|
{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
|
|
|
|
"called a function you should not call"},
|
|
|
|
{ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
|
|
|
|
{ERR_R_INTERNAL_ERROR, "internal error"},
|
|
|
|
{ERR_R_DISABLED, "called a function that was disabled at compile-time"},
|
2016-02-10 15:16:06 +00:00
|
|
|
{ERR_R_INIT_FAIL, "init fail"},
|
2017-06-27 14:05:12 +00:00
|
|
|
{ERR_R_OPERATION_FAIL, "operation fail"},
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
{0, NULL},
|
|
|
|
};
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
2000-03-04 01:36:53 +00:00
|
|
|
|
2016-03-02 17:36:17 +00:00
|
|
|
static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
|
2017-05-31 16:14:55 +00:00
|
|
|
static int set_err_thread_local;
|
2016-03-02 17:36:17 +00:00
|
|
|
static CRYPTO_THREAD_LOCAL err_thread_local;
|
|
|
|
|
2016-03-05 21:31:43 +00:00
|
|
|
static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
|
|
|
|
static CRYPTO_RWLOCK *err_string_lock;
|
|
|
|
|
2001-08-25 17:51:59 +00:00
|
|
|
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2015-04-28 14:50:54 +00:00
|
|
|
* The internal state
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
2016-01-30 17:04:25 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
static int int_err_library_number = ERR_LIB_USER;
|
2001-08-25 17:51:59 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static unsigned long get_error_values(int inc, int top, const char **file,
|
|
|
|
int *line, const char **data,
|
|
|
|
int *flags);
|
2001-08-25 17:51:59 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned long ret, l;
|
|
|
|
|
|
|
|
l = a->error;
|
|
|
|
ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
|
|
|
|
return (ret ^ ret % 19 * 13);
|
|
|
|
}
|
2008-05-26 11:24:29 +00:00
|
|
|
|
|
|
|
static int err_string_data_cmp(const ERR_STRING_DATA *a,
|
2015-01-22 03:40:55 +00:00
|
|
|
const ERR_STRING_DATA *b)
|
|
|
|
{
|
2017-07-08 16:43:55 +00:00
|
|
|
if (a->error == b->error)
|
|
|
|
return 0;
|
|
|
|
return a->error > b->error ? 1 : -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2001-08-25 17:51:59 +00:00
|
|
|
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-04-28 14:50:54 +00:00
|
|
|
ERR_STRING_DATA *p = NULL;
|
2001-09-06 12:37:36 +00:00
|
|
|
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_read_lock(err_string_lock);
|
2017-06-07 19:12:03 +00:00
|
|
|
p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
2001-09-06 12:37:36 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return p;
|
|
|
|
}
|
2001-09-04 11:15:55 +00:00
|
|
|
|
2003-02-18 12:14:57 +00:00
|
|
|
#ifndef OPENSSL_NO_ERR
|
2019-06-04 11:24:59 +00:00
|
|
|
/* 2019-05-21: Russian and Ukrainian locales on Linux require more than 6,5 kB */
|
|
|
|
# define SPACE_SYS_STR_REASONS 8 * 1024
|
2015-01-22 03:40:55 +00:00
|
|
|
# define NUM_SYS_STR_REASONS 127
|
2000-03-04 01:36:53 +00:00
|
|
|
|
|
|
|
static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* SYS_str_reasons is filled with copies of strerror() results at
|
|
|
|
* initialization. 'errno' values up to 127 should cover all usual errors,
|
|
|
|
* others will be displayed numerically by ERR_error_string. It is crucial
|
|
|
|
* that we have something for each reason code that occurs in
|
|
|
|
* ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
|
|
|
|
* which always gets an errno value and never one of those 'standard' reason
|
|
|
|
* codes.
|
|
|
|
*/
|
2000-03-04 01:36:53 +00:00
|
|
|
|
2005-03-31 09:26:39 +00:00
|
|
|
static void build_SYS_str_reasons(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
/* OPENSSL_malloc cannot be used here, use static storage instead */
|
2018-11-21 17:25:53 +00:00
|
|
|
static char strerror_pool[SPACE_SYS_STR_REASONS];
|
|
|
|
char *cur = strerror_pool;
|
|
|
|
size_t cnt = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
static int init = 1;
|
2015-04-28 14:50:54 +00:00
|
|
|
int i;
|
2018-11-21 11:44:42 +00:00
|
|
|
int saveerrno = get_last_sys_error();
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_write_lock(err_string_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!init) {
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
|
|
|
|
ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
|
|
|
|
|
2017-06-07 19:12:03 +00:00
|
|
|
str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
|
2019-06-04 11:24:59 +00:00
|
|
|
/*
|
|
|
|
* If we have used up all the space in strerror_pool,
|
|
|
|
* there's no point in calling openssl_strerror_r()
|
|
|
|
*/
|
|
|
|
if (str->string == NULL && cnt < sizeof(strerror_pool)) {
|
2018-11-21 17:25:53 +00:00
|
|
|
if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
|
2018-11-24 12:08:56 +00:00
|
|
|
size_t l = strlen(cur);
|
2018-11-21 17:25:53 +00:00
|
|
|
|
|
|
|
str->string = cur;
|
|
|
|
cnt += l;
|
|
|
|
cur += l;
|
2018-11-24 12:08:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* VMS has an unusual quirk of adding spaces at the end of
|
2019-06-04 11:24:59 +00:00
|
|
|
* some (most? all?) messages. Lets trim them off.
|
2018-11-24 12:08:56 +00:00
|
|
|
*/
|
2019-06-04 11:24:59 +00:00
|
|
|
while (cur > strerror_pool && ossl_isspace(cur[-1])) {
|
2018-11-24 12:08:56 +00:00
|
|
|
cur--;
|
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
*cur++ = '\0';
|
|
|
|
cnt++;
|
2018-11-21 17:25:53 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
if (str->string == NULL)
|
|
|
|
str->string = "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
|
|
|
|
* required by ERR_load_strings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
init = 0;
|
|
|
|
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
2018-11-21 11:44:42 +00:00
|
|
|
/* openssl_strerror_r could change errno, but we want to preserve it */
|
|
|
|
set_sys_error(saveerrno);
|
2017-06-07 19:12:03 +00:00
|
|
|
err_load_strings(SYS_str_reasons);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
2018-03-27 20:25:08 +00:00
|
|
|
#define err_clear_data(p, i) \
|
2015-01-22 03:40:55 +00:00
|
|
|
do { \
|
2018-03-27 20:25:08 +00:00
|
|
|
if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free((p)->err_data[i]); \
|
2018-03-27 20:25:08 +00:00
|
|
|
(p)->err_data[i] = NULL; \
|
|
|
|
} \
|
|
|
|
(p)->err_data_flags[i] = 0; \
|
|
|
|
} while (0)
|
2003-06-11 20:49:58 +00:00
|
|
|
|
2018-03-27 20:25:08 +00:00
|
|
|
#define err_clear(p, i) \
|
2015-01-22 03:40:55 +00:00
|
|
|
do { \
|
2018-03-27 20:25:08 +00:00
|
|
|
err_clear_data(p, i); \
|
|
|
|
(p)->err_flags[i] = 0; \
|
|
|
|
(p)->err_buffer[i] = 0; \
|
|
|
|
(p)->err_file[i] = NULL; \
|
|
|
|
(p)->err_line[i] = -1; \
|
|
|
|
} while (0)
|
1998-12-21 10:56:39 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static void ERR_STATE_free(ERR_STATE *s)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2018-03-27 20:25:08 +00:00
|
|
|
if (s == NULL)
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
|
|
|
err_clear_data(s, i);
|
|
|
|
}
|
|
|
|
OPENSSL_free(s);
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
|
2016-03-05 21:31:43 +00:00
|
|
|
{
|
2018-04-20 13:45:06 +00:00
|
|
|
if (!OPENSSL_init_crypto(0, NULL))
|
|
|
|
return 0;
|
Revert the crypto "global lock" implementation
Conceptually, this is a squashed version of:
Revert "Address feedback"
This reverts commit 75551e07bd2339dfea06ef1d31d69929e13a4495.
and
Revert "Add CRYPTO_thread_glock_new"
This reverts commit ed6b2c7938ec6f07b15745d4183afc276e74c6dd.
But there were some intervening commits that made neither revert apply
cleanly, so instead do it all as one shot.
The crypto global locks were an attempt to cope with the awkward
POSIX semantics for pthread_atfork(); its documentation (the "RATIONALE"
section) indicates that the expected usage is to have the prefork handler
lock all "global" locks, and the parent and child handlers release those
locks, to ensure that forking happens with a consistent (lock) state.
However, the set of functions available in the child process is limited
to async-signal-safe functions, and pthread_mutex_unlock() is not on
the list of async-signal-safe functions! The only synchronization
primitives that are async-signal-safe are the semaphore primitives,
which are not really appropriate for general-purpose usage.
However, the state consistency problem that the global locks were
attempting to solve is not actually a serious problem, particularly for
OpenSSL. That is, we can consider four cases of forking application
that might use OpenSSL:
(1) Single-threaded, does not call into OpenSSL in the child (e.g.,
the child calls exec() immediately)
For this class of process, no locking is needed at all, since there is
only ever a single thread of execution and the only reentrancy is due to
signal handlers (which are themselves limited to async-signal-safe
operation and should not be doing much work at all).
(2) Single-threaded, calls into OpenSSL after fork()
The application must ensure that it does not fork() with an unexpected
lock held (that is, one that would get unlocked in the parent but
accidentally remain locked in the child and cause deadlock). Since
OpenSSL does not expose any of its internal locks to the application
and the application is single-threaded, the OpenSSL internal locks
will be unlocked for the fork(), and the state will be consistent.
(OpenSSL will need to reseed its PRNG in the child, but that is
an orthogonal issue.) If the application makes use of locks from
libcrypto, proper handling for those locks is the responsibility of
the application, as for any other locking primitive that is available
for application programming.
(3) Multi-threaded, does not call into OpenSSL after fork()
As for (1), the OpenSSL state is only relevant in the parent, so
no particular fork()-related handling is needed. The internal locks
are relevant, but there is no interaction with the child to consider.
(4) Multi-threaded, calls into OpenSSL after fork()
This is the case where the pthread_atfork() hooks to ensure that all
global locks are in a known state across fork() would come into play,
per the above discussion. However, these "calls into OpenSSL after
fork()" are still subject to the restriction to async-signal-safe
functions. Since OpenSSL uses all sorts of locking and libc functions
that are not on the list of safe functions (e.g., malloc()), this
case is not currently usable and is unlikely to ever be usable,
independently of the locking situation. So, there is no need to
go through contortions to attempt to support this case in the one small
area of locking interaction with fork().
In light of the above analysis (thanks @davidben and @achernya), go
back to the simpler implementation that does not need to distinguish
"library-global" locks or to have complicated atfork handling for locks.
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/5089)
2018-01-16 15:49:54 +00:00
|
|
|
err_string_lock = CRYPTO_THREAD_lock_new();
|
2018-04-20 13:45:06 +00:00
|
|
|
if (err_string_lock == NULL)
|
|
|
|
return 0;
|
2017-06-07 19:12:03 +00:00
|
|
|
int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
|
|
|
|
err_string_data_cmp);
|
2018-04-20 13:45:06 +00:00
|
|
|
if (int_error_hash == NULL) {
|
|
|
|
CRYPTO_THREAD_lock_free(err_string_lock);
|
|
|
|
err_string_lock = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2016-03-11 21:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void err_cleanup(void)
|
|
|
|
{
|
2017-05-31 16:14:55 +00:00
|
|
|
if (set_err_thread_local != 0)
|
|
|
|
CRYPTO_THREAD_cleanup_local(&err_thread_local);
|
2016-03-11 21:53:18 +00:00
|
|
|
CRYPTO_THREAD_lock_free(err_string_lock);
|
|
|
|
err_string_lock = NULL;
|
2017-06-07 19:12:03 +00:00
|
|
|
lh_ERR_STRING_DATA_free(int_error_hash);
|
|
|
|
int_error_hash = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Legacy; pack in the library.
|
|
|
|
*/
|
|
|
|
static void err_patch(int lib, ERR_STRING_DATA *str)
|
|
|
|
{
|
|
|
|
unsigned long plib = ERR_PACK(lib, 0, 0);
|
|
|
|
|
|
|
|
for (; str->error != 0; str++)
|
|
|
|
str->error |= plib;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash in |str| error strings. Assumes the URN_ONCE was done.
|
|
|
|
*/
|
|
|
|
static int err_load_strings(const ERR_STRING_DATA *str)
|
|
|
|
{
|
|
|
|
CRYPTO_THREAD_write_lock(err_string_lock);
|
|
|
|
for (; str->error; str++)
|
|
|
|
(void)lh_ERR_STRING_DATA_insert(int_error_hash,
|
|
|
|
(ERR_STRING_DATA *)str);
|
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
|
|
|
return 1;
|
2016-03-05 21:31:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 13:50:06 +00:00
|
|
|
int ERR_load_ERR_strings(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_ERR
|
2016-07-12 13:50:06 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
|
|
|
return 0;
|
2016-03-05 21:31:43 +00:00
|
|
|
|
2017-06-07 19:12:03 +00:00
|
|
|
err_load_strings(ERR_str_libraries);
|
|
|
|
err_load_strings(ERR_str_reasons);
|
|
|
|
err_patch(ERR_LIB_SYS, ERR_str_functs);
|
|
|
|
err_load_strings(ERR_str_functs);
|
2015-01-22 03:40:55 +00:00
|
|
|
build_SYS_str_reasons();
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
2016-07-12 13:50:06 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2017-06-07 19:12:03 +00:00
|
|
|
int ERR_load_strings(int lib, ERR_STRING_DATA *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-06-07 19:12:03 +00:00
|
|
|
if (ERR_load_ERR_strings() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_patch(lib, str);
|
|
|
|
err_load_strings(str);
|
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2017-06-07 19:12:03 +00:00
|
|
|
int ERR_load_strings_const(const ERR_STRING_DATA *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-07-12 13:50:06 +00:00
|
|
|
if (ERR_load_ERR_strings() == 0)
|
|
|
|
return 0;
|
2017-06-07 19:12:03 +00:00
|
|
|
err_load_strings(str);
|
2016-07-12 13:50:06 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-09-04 11:49:14 +00:00
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
|
|
|
return 0;
|
2016-03-05 21:31:43 +00:00
|
|
|
|
|
|
|
CRYPTO_THREAD_write_lock(err_string_lock);
|
2017-06-07 19:12:03 +00:00
|
|
|
/*
|
|
|
|
* We don't need to ERR_PACK the lib, since that was done (to
|
|
|
|
* the table) when it was loaded.
|
|
|
|
*/
|
|
|
|
for (; str->error; str++)
|
|
|
|
(void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
2016-07-19 17:42:11 +00:00
|
|
|
|
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-09-03 18:24:56 +00:00
|
|
|
|
2016-04-12 11:20:16 +00:00
|
|
|
void err_free_strings_int(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1998-12-21 10:56:39 +00:00
|
|
|
/********************************************************/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
void ERR_put_error(int lib, int func, int reason, const char *file, int line)
|
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-06-04 21:35:58 +00:00
|
|
|
#ifdef _OSD_POSIX
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* In the BS2000-OSD POSIX subsystem, the compiler generates path names
|
|
|
|
* in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
|
|
|
|
* something sensible. @@@ We shouldn't modify a const string, though.
|
|
|
|
*/
|
|
|
|
if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
/* Skip the "*POSIX(" prefix */
|
|
|
|
file += sizeof("*POSIX(") - 1;
|
|
|
|
end = &file[strlen(file) - 1];
|
|
|
|
if (*end == ')')
|
|
|
|
*end = '\0';
|
|
|
|
/* Optional: use the basename of the path only. */
|
|
|
|
if ((end = strrchr(file, '/')) != NULL)
|
|
|
|
file = &end[1];
|
|
|
|
}
|
1999-06-04 21:35:58 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
es->top = (es->top + 1) % ERR_NUM_ERRORS;
|
|
|
|
if (es->top == es->bottom)
|
|
|
|
es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
|
|
|
|
es->err_flags[es->top] = 0;
|
|
|
|
es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
|
|
|
|
es->err_file[es->top] = file;
|
|
|
|
es->err_line[es->top] = line;
|
|
|
|
err_clear_data(es, es->top);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void ERR_clear_error(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ERR_STATE *es;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
|
|
|
return;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
|
|
|
err_clear(es, i);
|
|
|
|
}
|
|
|
|
es->top = es->bottom = 0;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long ERR_get_error(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(1, 0, NULL, NULL, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long ERR_get_error_line(const char **file, int *line)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(1, 0, file, line, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long ERR_get_error_line_data(const char **file, int *line,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char **data, int *flags)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(1, 0, file, line, data, flags);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2002-01-24 17:17:33 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long ERR_peek_error(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 0, NULL, NULL, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2002-01-24 16:16:43 +00:00
|
|
|
unsigned long ERR_peek_error_line(const char **file, int *line)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 0, file, line, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2002-01-22 22:29:58 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long ERR_peek_error_line_data(const char **file, int *line,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char **data, int *flags)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 0, file, line, data, flags);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2002-01-24 17:17:33 +00:00
|
|
|
|
|
|
|
unsigned long ERR_peek_last_error(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 1, NULL, NULL, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2002-01-24 17:17:33 +00:00
|
|
|
|
|
|
|
unsigned long ERR_peek_last_error_line(const char **file, int *line)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 1, file, line, NULL, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2002-01-24 17:17:33 +00:00
|
|
|
|
2002-01-24 16:16:43 +00:00
|
|
|
unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char **data, int *flags)
|
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return get_error_values(0, 1, file, line, data, flags);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long get_error_values(int inc, int top, const char **file,
|
|
|
|
int *line, const char **data,
|
|
|
|
int *flags)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
ERR_STATE *es;
|
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (inc && top) {
|
|
|
|
if (file)
|
|
|
|
*file = "";
|
|
|
|
if (line)
|
|
|
|
*line = 0;
|
|
|
|
if (data)
|
|
|
|
*data = "";
|
|
|
|
if (flags)
|
|
|
|
*flags = 0;
|
|
|
|
|
|
|
|
return ERR_R_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:01:12 +00:00
|
|
|
while (es->bottom != es->top) {
|
|
|
|
if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
|
|
|
|
err_clear(es, es->top);
|
|
|
|
es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = (es->bottom + 1) % ERR_NUM_ERRORS;
|
|
|
|
if (es->err_flags[i] & ERR_FLAG_CLEAR) {
|
|
|
|
es->bottom = i;
|
|
|
|
err_clear(es, es->bottom);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (es->bottom == es->top)
|
|
|
|
return 0;
|
2019-03-20 19:01:12 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (top)
|
|
|
|
i = es->top; /* last error */
|
|
|
|
else
|
|
|
|
i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
|
|
|
|
|
|
|
|
ret = es->err_buffer[i];
|
|
|
|
if (inc) {
|
|
|
|
es->bottom = i;
|
|
|
|
es->err_buffer[i] = 0;
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:49:41 +00:00
|
|
|
if (file != NULL && line != NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
if (es->err_file[i] == NULL) {
|
|
|
|
*file = "NA";
|
2017-12-08 15:49:41 +00:00
|
|
|
*line = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
} else {
|
|
|
|
*file = es->err_file[i];
|
2017-12-08 15:49:41 +00:00
|
|
|
*line = es->err_line[i];
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data == NULL) {
|
|
|
|
if (inc) {
|
|
|
|
err_clear_data(es, i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (es->err_data[i] == NULL) {
|
|
|
|
*data = "";
|
|
|
|
if (flags != NULL)
|
|
|
|
*flags = 0;
|
|
|
|
} else {
|
|
|
|
*data = es->err_data[i];
|
|
|
|
if (flags != NULL)
|
|
|
|
*flags = es->err_data_flags[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-04-14 23:36:15 +00:00
|
|
|
void ERR_error_string_n(unsigned long e, char *buf, size_t len)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
char lsbuf[64], fsbuf[64], rsbuf[64];
|
|
|
|
const char *ls, *fs, *rs;
|
|
|
|
unsigned long l, f, r;
|
|
|
|
|
2016-10-12 15:43:03 +00:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
l = ERR_GET_LIB(e);
|
|
|
|
ls = ERR_lib_error_string(e);
|
2017-10-06 15:06:12 +00:00
|
|
|
if (ls == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
|
2017-10-06 15:06:12 +00:00
|
|
|
ls = lsbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = ERR_func_error_string(e);
|
|
|
|
f = ERR_GET_FUNC(e);
|
|
|
|
if (fs == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
|
2017-10-06 15:06:12 +00:00
|
|
|
fs = fsbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
rs = ERR_reason_error_string(e);
|
|
|
|
r = ERR_GET_REASON(e);
|
|
|
|
if (rs == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
|
2017-10-06 15:06:12 +00:00
|
|
|
rs = rsbuf;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-10-06 15:06:12 +00:00
|
|
|
BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (strlen(buf) == len - 1) {
|
2017-10-06 15:06:12 +00:00
|
|
|
/* Didn't fit; use a minimal format. */
|
|
|
|
BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-14 23:36:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* ERR_error_string_n should be used instead for ret != NULL as
|
|
|
|
* ERR_error_string cannot know how large the buffer is
|
|
|
|
*/
|
2000-04-14 23:36:15 +00:00
|
|
|
char *ERR_error_string(unsigned long e, char *ret)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
static char buf[256];
|
2000-04-14 23:36:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
ret = buf;
|
2017-10-06 15:06:12 +00:00
|
|
|
ERR_error_string_n(e, ret, (int)sizeof(buf));
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
const char *ERR_lib_error_string(unsigned long e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STRING_DATA d, *p;
|
|
|
|
unsigned long l;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-05 21:31:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
l = ERR_GET_LIB(e);
|
|
|
|
d.error = ERR_PACK(l, 0, 0);
|
2015-04-28 14:50:54 +00:00
|
|
|
p = int_err_get_item(&d);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ((p == NULL) ? NULL : p->string);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
const char *ERR_func_error_string(unsigned long e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STRING_DATA d, *p;
|
|
|
|
unsigned long l, f;
|
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-05 21:31:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
l = ERR_GET_LIB(e);
|
|
|
|
f = ERR_GET_FUNC(e);
|
|
|
|
d.error = ERR_PACK(l, f, 0);
|
2015-04-28 14:50:54 +00:00
|
|
|
p = int_err_get_item(&d);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ((p == NULL) ? NULL : p->string);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
const char *ERR_reason_error_string(unsigned long e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STRING_DATA d, *p = NULL;
|
|
|
|
unsigned long l, r;
|
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-05 21:31:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
l = ERR_GET_LIB(e);
|
|
|
|
r = ERR_GET_REASON(e);
|
|
|
|
d.error = ERR_PACK(l, 0, r);
|
2015-04-28 14:50:54 +00:00
|
|
|
p = int_err_get_item(&d);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!p) {
|
|
|
|
d.error = ERR_PACK(0, 0, r);
|
2015-04-28 14:50:54 +00:00
|
|
|
p = int_err_get_item(&d);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
return ((p == NULL) ? NULL : p->string);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-05-08 15:01:09 +00:00
|
|
|
void err_delete_thread_state(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-06-19 09:18:44 +00:00
|
|
|
ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
|
2016-03-02 17:36:17 +00:00
|
|
|
if (state == NULL)
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-03-02 17:36:17 +00:00
|
|
|
CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
|
|
|
ERR_STATE_free(state);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-06-27 10:28:29 +00:00
|
|
|
#if OPENSSL_API_COMPAT < 0x10100000L
|
2016-05-08 15:01:09 +00:00
|
|
|
void ERR_remove_thread_state(void *dummy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-05 04:00:33 +00:00
|
|
|
#if OPENSSL_API_COMPAT < 0x10000000L
|
2008-08-06 15:54:15 +00:00
|
|
|
void ERR_remove_state(unsigned long pid)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
}
|
2008-08-06 15:54:15 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
DEFINE_RUN_ONCE_STATIC(err_do_init)
|
2016-03-02 17:36:17 +00:00
|
|
|
{
|
2017-05-31 16:14:55 +00:00
|
|
|
set_err_thread_local = 1;
|
2016-07-19 17:42:11 +00:00
|
|
|
return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
|
2016-03-02 17:36:17 +00:00
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
ERR_STATE *ERR_get_state(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2018-04-20 13:45:06 +00:00
|
|
|
ERR_STATE *state;
|
2018-12-14 18:33:55 +00:00
|
|
|
int saveerrno = get_last_sys_error();
|
2018-04-20 13:45:06 +00:00
|
|
|
|
|
|
|
if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
|
|
|
return NULL;
|
2016-03-02 17:36:17 +00:00
|
|
|
|
2016-07-19 17:42:11 +00:00
|
|
|
if (!RUN_ONCE(&err_init, err_do_init))
|
|
|
|
return NULL;
|
2016-03-02 17:36:17 +00:00
|
|
|
|
|
|
|
state = CRYPTO_THREAD_get_local(&err_thread_local);
|
2018-04-03 21:47:10 +00:00
|
|
|
if (state == (ERR_STATE*)-1)
|
|
|
|
return NULL;
|
2016-03-02 17:36:17 +00:00
|
|
|
|
|
|
|
if (state == NULL) {
|
2018-04-03 21:47:10 +00:00
|
|
|
if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
|
|
|
|
return NULL;
|
|
|
|
|
2018-04-03 15:31:16 +00:00
|
|
|
if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
|
2018-04-03 21:47:10 +00:00
|
|
|
CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
2016-03-02 17:36:17 +00:00
|
|
|
return NULL;
|
2018-04-03 15:31:16 +00:00
|
|
|
}
|
2016-03-02 17:36:17 +00:00
|
|
|
|
2017-06-19 09:18:44 +00:00
|
|
|
if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
|
2018-04-03 21:47:10 +00:00
|
|
|
|| !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
|
2016-03-02 17:36:17 +00:00
|
|
|
ERR_STATE_free(state);
|
2018-04-03 21:47:10 +00:00
|
|
|
CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
2016-03-02 17:36:17 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-02-10 13:59:15 +00:00
|
|
|
|
|
|
|
/* Ignore failures from these */
|
2016-02-09 16:52:40 +00:00
|
|
|
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-03-02 17:36:17 +00:00
|
|
|
|
2018-12-14 18:33:55 +00:00
|
|
|
set_sys_error(saveerrno);
|
2016-03-02 17:36:17 +00:00
|
|
|
return state;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2018-04-20 13:45:06 +00:00
|
|
|
/*
|
|
|
|
* err_shelve_state returns the current thread local error state
|
|
|
|
* and freezes the error module until err_unshelve_state is called.
|
|
|
|
*/
|
|
|
|
int err_shelve_state(void **state)
|
|
|
|
{
|
2018-12-14 18:33:55 +00:00
|
|
|
int saveerrno = get_last_sys_error();
|
|
|
|
|
2019-01-01 07:53:24 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-04-20 13:45:06 +00:00
|
|
|
if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!RUN_ONCE(&err_init, err_do_init))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*state = CRYPTO_THREAD_get_local(&err_thread_local);
|
|
|
|
if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
|
|
|
|
return 0;
|
|
|
|
|
2018-12-14 18:33:55 +00:00
|
|
|
set_sys_error(saveerrno);
|
2018-04-20 13:45:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* err_unshelve_state restores the error state that was returned
|
|
|
|
* by err_shelve_state previously.
|
|
|
|
*/
|
|
|
|
void err_unshelve_state(void* state)
|
|
|
|
{
|
|
|
|
if (state != (void*)-1)
|
|
|
|
CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int ERR_get_next_error_library(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-04-28 14:50:54 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-04-20 13:45:06 +00:00
|
|
|
if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
2016-07-19 17:42:11 +00:00
|
|
|
return 0;
|
2016-03-05 21:31:43 +00:00
|
|
|
|
|
|
|
CRYPTO_THREAD_write_lock(err_string_lock);
|
2015-04-28 14:50:54 +00:00
|
|
|
ret = int_err_library_number++;
|
2016-03-05 21:31:43 +00:00
|
|
|
CRYPTO_THREAD_unlock(err_string_lock);
|
2015-04-28 14:50:54 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2019-01-28 17:17:59 +00:00
|
|
|
static int err_set_error_data_int(char *data, int flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
|
|
|
int i;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
2019-01-28 17:17:59 +00:00
|
|
|
return 0;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
i = es->top;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err_clear_data(es, i);
|
|
|
|
es->err_data[i] = data;
|
|
|
|
es->err_data_flags[i] = flags;
|
2019-01-28 17:17:59 +00:00
|
|
|
|
|
|
|
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);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
1999-05-05 12:35:27 +00:00
|
|
|
void ERR_add_error_data(int num, ...)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, num);
|
|
|
|
ERR_add_error_vdata(num, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2011-01-14 15:13:37 +00:00
|
|
|
|
|
|
|
void ERR_add_error_vdata(int num, va_list args)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i, n, s;
|
|
|
|
char *str, *p, *a;
|
|
|
|
|
|
|
|
s = 80;
|
2018-04-03 15:31:16 +00:00
|
|
|
if ((str = OPENSSL_malloc(s + 1)) == NULL) {
|
|
|
|
/* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
|
2015-01-22 03:40:55 +00:00
|
|
|
return;
|
2018-04-03 15:31:16 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
str[0] = '\0';
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
a = va_arg(args, char *);
|
2017-06-15 22:51:10 +00:00
|
|
|
if (a == NULL)
|
|
|
|
a = "<NULL>";
|
2017-06-14 17:54:04 +00:00
|
|
|
n += strlen(a);
|
|
|
|
if (n > s) {
|
|
|
|
s = n + 20;
|
|
|
|
p = OPENSSL_realloc(str, s + 1);
|
|
|
|
if (p == NULL) {
|
|
|
|
OPENSSL_free(str);
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-06-14 17:54:04 +00:00
|
|
|
str = p;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-06-14 17:54:04 +00:00
|
|
|
OPENSSL_strlcat(str, a, (size_t)s + 1);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2019-01-28 17:17:59 +00:00
|
|
|
if (!err_set_error_data_int(str, ERR_TXT_MALLOCED | ERR_TXT_STRING))
|
|
|
|
OPENSSL_free(str);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2003-06-11 20:49:58 +00:00
|
|
|
|
|
|
|
int ERR_set_mark(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
2003-06-11 20:49:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
|
|
|
return 0;
|
2003-06-11 20:49:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (es->bottom == es->top)
|
|
|
|
return 0;
|
|
|
|
es->err_flags[es->top] |= ERR_FLAG_MARK;
|
|
|
|
return 1;
|
|
|
|
}
|
2003-06-11 20:49:58 +00:00
|
|
|
|
|
|
|
int ERR_pop_to_mark(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
|
|
|
|
|
|
|
es = ERR_get_state();
|
2017-06-13 17:00:35 +00:00
|
|
|
if (es == NULL)
|
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
while (es->bottom != es->top
|
|
|
|
&& (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
|
|
|
|
err_clear(es, es->top);
|
2017-08-16 21:08:03 +00:00
|
|
|
es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (es->bottom == es->top)
|
|
|
|
return 0;
|
|
|
|
es->err_flags[es->top] &= ~ERR_FLAG_MARK;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-05 19:47:00 +00:00
|
|
|
|
|
|
|
int ERR_clear_last_mark(void)
|
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
|
|
|
int top;
|
|
|
|
|
|
|
|
es = ERR_get_state();
|
|
|
|
if (es == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
top = es->top;
|
|
|
|
while (es->bottom != top
|
|
|
|
&& (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
|
2017-08-16 21:08:03 +00:00
|
|
|
top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
|
2017-08-05 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (es->bottom == top)
|
|
|
|
return 0;
|
|
|
|
es->err_flags[top] &= ~ERR_FLAG_MARK;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-01 10:19:30 +00:00
|
|
|
|
|
|
|
void err_clear_last_constant_time(int clear)
|
|
|
|
{
|
|
|
|
ERR_STATE *es;
|
|
|
|
int top;
|
|
|
|
|
|
|
|
es = ERR_get_state();
|
|
|
|
if (es == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
top = es->top;
|
|
|
|
|
2019-03-20 19:01:12 +00:00
|
|
|
/*
|
|
|
|
* Flag error as cleared but remove it elsewhere to avoid two errors
|
|
|
|
* accessing the same error stack location, revealing timing information.
|
|
|
|
*/
|
|
|
|
clear = constant_time_select_int(constant_time_eq_int(clear, 0),
|
|
|
|
0, ERR_FLAG_CLEAR);
|
|
|
|
es->err_flags[top] |= clear;
|
2018-09-01 10:19:30 +00:00
|
|
|
}
|