sync with OpenBSD -current

This commit is contained in:
purplerain 2024-10-12 21:28:57 +00:00
parent e43a9c3d43
commit dde1236ee1
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
53 changed files with 809 additions and 771 deletions

View file

@ -1,4 +1,4 @@
/* $OpenBSD: ec_asn1.c,v 1.57 2024/10/03 05:07:49 tb Exp $ */
/* $OpenBSD: ec_asn1.c,v 1.68 2024/10/11 18:58:04 tb Exp $ */
/*
* Written by Nils Larsch for the OpenSSL project.
*/
@ -124,6 +124,10 @@ typedef struct ec_parameters_st {
ASN1_INTEGER *cofactor;
} ECPARAMETERS;
#define ECPK_PARAM_NAMED_CURVE 0
#define ECPK_PARAM_EXPLICIT 1
#define ECPK_PARAM_IMPLICITLY_CA 2
typedef struct ecpk_parameters_st {
int type;
union {
@ -555,56 +559,52 @@ EC_PRIVATEKEY_free(EC_PRIVATEKEY *a)
static int
ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
{
int ok = 0, nid;
BIGNUM *tmp = NULL;
BIGNUM *p = NULL;
int nid;
int ret = 0;
if (group == NULL || field == NULL)
return 0;
/* clear the old values (if necessary) */
if (field->fieldType != NULL)
ASN1_OBJECT_free(field->fieldType);
if (field->p.other != NULL)
ASN1_TYPE_free(field->p.other);
goto err;
nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
/* set OID for the field */
if (nid == NID_X9_62_characteristic_two_field) {
ECerror(EC_R_GF2M_NOT_SUPPORTED);
goto err;
}
if (nid != NID_X9_62_prime_field) {
ECerror(EC_R_INVALID_FIELD);
goto err;
}
if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
ECerror(ERR_R_OBJ_LIB);
goto err;
}
if (nid == NID_X9_62_prime_field) {
if ((tmp = BN_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
/* the parameters are specified by the prime number p */
if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
ECerror(ERR_R_EC_LIB);
goto err;
}
/* set the prime number */
field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
if (field->p.prime == NULL) {
ECerror(ERR_R_ASN1_LIB);
goto err;
}
} else {
ECerror(EC_R_GF2M_NOT_SUPPORTED);
if ((p = BN_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_GROUP_get_curve(group, p, NULL, NULL, NULL)) {
ECerror(ERR_R_EC_LIB);
goto err;
}
if ((field->p.prime = BN_to_ASN1_INTEGER(p, NULL)) == NULL) {
ECerror(ERR_R_ASN1_LIB);
goto err;
}
ok = 1;
ret = 1;
err:
BN_free(tmp);
return (ok);
BN_free(p);
return ret;
}
static int
ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
{
BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
BIGNUM *a = NULL, *b = NULL;
unsigned char *buffer_1 = NULL, *buffer_2 = NULL, *a_buf = NULL,
*b_buf = NULL;
size_t len_1, len_2;
@ -614,18 +614,18 @@ ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
if (!group || !curve || !curve->a || !curve->b)
return 0;
if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
if ((a = BN_new()) == NULL || (b = BN_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
/* get a and b */
if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
if (!EC_GROUP_get_curve(group, NULL, a, b, NULL)) {
ECerror(ERR_R_EC_LIB);
goto err;
}
len_1 = (size_t) BN_num_bytes(tmp_1);
len_2 = (size_t) BN_num_bytes(tmp_2);
len_1 = (size_t) BN_num_bytes(a);
len_2 = (size_t) BN_num_bytes(b);
if (len_1 == 0) {
/* len_1 == 0 => a == 0 */
@ -636,7 +636,7 @@ ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
if ((len_1 = BN_bn2bin(a, buffer_1)) == 0) {
ECerror(ERR_R_BN_LIB);
goto err;
}
@ -652,7 +652,7 @@ ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
if ((len_2 = BN_bn2bin(b, buffer_2)) == 0) {
ECerror(ERR_R_BN_LIB);
goto err;
}
@ -691,13 +691,14 @@ ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
err:
free(buffer_1);
free(buffer_2);
BN_free(tmp_1);
BN_free(tmp_2);
BN_free(a);
BN_free(b);
return (ok);
}
static ECPARAMETERS *
ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param)
ec_asn1_group2parameters(const EC_GROUP *group)
{
int ok = 0;
size_t len = 0;
@ -711,13 +712,10 @@ ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param)
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if (param == NULL) {
if ((ret = ECPARAMETERS_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
} else
ret = param;
if ((ret = ECPARAMETERS_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
/* set the version (always one) */
ret->version = (long) 0x1;
@ -781,8 +779,7 @@ ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param)
err:
if (!ok) {
if (ret && !param)
ECPARAMETERS_free(ret);
ECPARAMETERS_free(ret);
ret = NULL;
}
BN_free(tmp);
@ -791,49 +788,39 @@ ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param)
}
ECPKPARAMETERS *
ec_asn1_group2pkparameters(const EC_GROUP *group, ECPKPARAMETERS *params)
ec_asn1_group2pkparameters(const EC_GROUP *group)
{
int ok = 1, tmp;
ECPKPARAMETERS *ret = params;
ECPKPARAMETERS *pkparameters;
ECPARAMETERS *parameters;
ASN1_OBJECT *aobj;
int nid;
if (ret == NULL) {
if ((ret = ECPKPARAMETERS_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
return NULL;
}
if ((pkparameters = ECPKPARAMETERS_new()) == NULL) {
ECerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if ((EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) != 0) {
if ((nid = EC_GROUP_get_curve_name(group)) == NID_undef)
goto err;
if ((aobj = OBJ_nid2obj(nid)) == NULL)
goto err;
pkparameters->type = ECPK_PARAM_NAMED_CURVE;
pkparameters->value.named_curve = aobj;
} else {
if (ret->type == 0 && ret->value.named_curve)
ASN1_OBJECT_free(ret->value.named_curve);
else if (ret->type == 1 && ret->value.parameters)
ECPARAMETERS_free(ret->value.parameters);
if ((parameters = ec_asn1_group2parameters(group)) == NULL)
goto err;
pkparameters->type = ECPK_PARAM_EXPLICIT;
pkparameters->value.parameters = parameters;
parameters = NULL;
}
if (EC_GROUP_get_asn1_flag(group)) {
/*
* use the asn1 OID to describe the elliptic curve
* parameters
*/
tmp = EC_GROUP_get_curve_name(group);
if (tmp) {
ret->type = 0;
if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
ok = 0;
} else
/* we don't know the group => ERROR */
ok = 0;
} else {
/* use the ECPARAMETERS structure */
ret->type = 1;
if ((ret->value.parameters = ec_asn1_group2parameters(group,
NULL)) == NULL)
ok = 0;
}
return pkparameters;
if (!ok) {
ECPKPARAMETERS_free(ret);
return NULL;
}
return ret;
err:
ECPKPARAMETERS_free(pkparameters);
return NULL;
}
static EC_GROUP *
@ -975,36 +962,34 @@ ec_asn1_parameters2group(const ECPARAMETERS *params)
EC_GROUP *
ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
{
EC_GROUP *ret = NULL;
int tmp = 0;
EC_GROUP *group;
int nid;
if (params == NULL) {
ECerror(EC_R_MISSING_PARAMETERS);
return NULL;
}
if (params->type == 0) {/* the curve is given by an OID */
tmp = OBJ_obj2nid(params->value.named_curve);
if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
if (params->type == ECPK_PARAM_NAMED_CURVE) {
if ((nid = OBJ_obj2nid(params->value.named_curve)) == NID_undef) {
ECerror(EC_R_UNKNOWN_GROUP);
return NULL;
}
if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
ECerror(EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
return NULL;
}
EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
} else if (params->type == 1) { /* the parameters are given by a
* ECPARAMETERS structure */
ret = ec_asn1_parameters2group(params->value.parameters);
if (!ret) {
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
} else if (params->type == ECPK_PARAM_EXPLICIT) {
group = ec_asn1_parameters2group(params->value.parameters);
if (group == NULL) {
ECerror(ERR_R_EC_LIB);
return NULL;
}
EC_GROUP_set_asn1_flag(ret, 0x0);
} else if (params->type == 2) { /* implicitlyCA */
EC_GROUP_set_asn1_flag(group, 0);
} else if (params->type == ECPK_PARAM_IMPLICITLY_CA) {
return NULL;
} else {
ECerror(EC_R_ASN1_ERROR);
return NULL;
}
return ret;
return group;
}
EC_GROUP *
@ -1034,21 +1019,24 @@ d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
LCRYPTO_ALIAS(d2i_ECPKParameters);
int
i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
i2d_ECPKParameters(const EC_GROUP *group, unsigned char **out_der)
{
ECPKPARAMETERS *parameters;
int ret = 0;
ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
if (tmp == NULL) {
if ((parameters = ec_asn1_group2pkparameters(group)) == NULL) {
ECerror(EC_R_GROUP2PKPARAMETERS_FAILURE);
return 0;
goto err;
}
if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
if ((ret = i2d_ECPKPARAMETERS(parameters, out_der)) <= 0) {
ECerror(EC_R_I2D_ECPKPARAMETERS_FAILURE);
ECPKPARAMETERS_free(tmp);
return 0;
goto err;
}
ECPKPARAMETERS_free(tmp);
return (ret);
err:
ECPKPARAMETERS_free(parameters);
return ret;
}
LCRYPTO_ALIAS(i2d_ECPKParameters);
@ -1179,11 +1167,13 @@ i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
goto err;
}
if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
if ((priv_key->parameters = ec_asn1_group2pkparameters(
a->group, priv_key->parameters)) == NULL) {
ECPKPARAMETERS *parameters;
if ((parameters = ec_asn1_group2pkparameters(a->group)) == NULL) {
ECerror(ERR_R_EC_LIB);
goto err;
}
priv_key->parameters = parameters;
}
if (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key != NULL) {
priv_key->publicKey = ASN1_BIT_STRING_new();

View file

@ -1,4 +1,4 @@
/* $OpenBSD: err.c,v 1.65 2024/10/02 15:21:39 jsing Exp $ */
/* $OpenBSD: err.c,v 1.73 2024/10/11 13:32:22 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@ -127,8 +127,6 @@
DECLARE_LHASH_OF(ERR_STRING_DATA);
DECLARE_LHASH_OF(ERR_STATE);
typedef struct st_ERR_FNS ERR_FNS;
typedef struct err_state_st {
CRYPTO_THREADID tid;
int err_flags[ERR_NUM_ERRORS];
@ -141,54 +139,54 @@ typedef struct err_state_st {
} ERR_STATE;
#ifndef OPENSSL_NO_ERR
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_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"},
{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"},
{ERR_PACK(ERR_LIB_GOST,0,0), "GOST routines"},
static const 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_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"},
{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"},
{ERR_PACK(ERR_LIB_GOST, 0, 0), "GOST routines"},
{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), "ioctl"},
{ERR_PACK(0,SYS_F_BIND, 0), "bind"},
{ERR_PACK(0,SYS_F_LISTEN, 0), "listen"},
{ERR_PACK(0,SYS_F_ACCEPT, 0), "accept"},
{ERR_PACK(0,SYS_F_OPENDIR, 0), "opendir"},
{ERR_PACK(0,SYS_F_FREAD, 0), "fread"},
static const ERR_STRING_DATA ERR_str_functs[] = {
{ERR_PACK(ERR_LIB_SYS, SYS_F_FOPEN, 0), "fopen"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_CONNECT, 0), "connect"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_SOCKET, 0), "socket"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_IOCTLSOCKET, 0), "ioctl"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_BIND, 0), "bind"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_LISTEN, 0), "listen"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_ACCEPT, 0), "accept"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_OPENDIR, 0), "opendir"},
{ERR_PACK(ERR_LIB_SYS, SYS_F_FREAD, 0), "fread"},
{0, NULL},
};
static ERR_STRING_DATA ERR_str_reasons[] = {
static const ERR_STRING_DATA ERR_str_reasons[] = {
{ERR_R_SYS_LIB, "system lib"},
{ERR_R_BN_LIB, "BN lib"},
{ERR_R_RSA_LIB, "RSA lib"},
@ -226,7 +224,7 @@ static ERR_STRING_DATA ERR_str_reasons[] = {
{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"},
{ERR_R_DISABLED, "called a function that was disabled at compile-time"},
{ERR_R_INIT_FAIL, "initialization failure"},
{0, NULL},
@ -254,8 +252,6 @@ static pthread_t err_init_thread;
* internal to the "err_defaults" implementation.
*/
/* The internal functions used in the "err_defaults" implementation */
static unsigned long
err_string_data_hash(const ERR_STRING_DATA *a)
{
@ -475,36 +471,23 @@ err_get_next_lib(void)
#define LEN_SYS_STR_REASON 32
static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
/* 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 SYSerror(which always gets an errno
* value and never one of those 'standard' reason codes. */
/*
* 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 SYSerror(), which always gets an
* errno value and never one of those 'standard' reason codes.
*/
static void
build_SYS_str_reasons(void)
err_build_SYS_str_reasons(void)
{
/* malloc cannot be used here, use static storage instead */
static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
int i;
static int init = 1;
int save_errno;
CRYPTO_r_lock(CRYPTO_LOCK_ERR);
if (!init) {
CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
return;
}
CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
CRYPTO_w_lock(CRYPTO_LOCK_ERR);
if (!init) {
CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
return;
}
int i;
/* strerror(3) will set errno to EINVAL when i is an unknown errno. */
save_errno = errno;
@ -526,33 +509,33 @@ build_SYS_str_reasons(void)
}
errno = save_errno;
/* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
* as required by ERR_load_strings. */
init = 0;
CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
/*
* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
* as required by ERR_load_strings.
*/
}
#endif
#define err_clear_data(p,i) \
do { \
if (((p)->err_data[i] != NULL) && \
(p)->err_data_flags[i] & ERR_TXT_MALLOCED) { \
free((p)->err_data[i]); \
(p)->err_data[i] = NULL; \
} \
(p)->err_data_flags[i] = 0; \
} while(0)
static void
err_clear_data(ERR_STATE *s, int i)
{
if ((s->err_data_flags[i] & ERR_TXT_MALLOCED) != 0)
free(s->err_data[i]);
#define err_clear(p,i) \
do { \
(p)->err_flags[i] = 0; \
(p)->err_buffer[i] = 0; \
err_clear_data(p, i); \
(p)->err_file[i] = NULL; \
(p)->err_line[i] = -1; \
} while(0)
s->err_data[i] = NULL;
s->err_data_flags[i] = 0;
}
static void
err_clear(ERR_STATE *s, int i)
{
s->err_flags[i] = 0;
s->err_buffer[i] = 0;
s->err_file[i] = NULL;
s->err_line[i] = -1;
err_clear_data(s, i);
}
static void
ERR_STATE_free(ERR_STATE *s)
@ -562,9 +545,9 @@ ERR_STATE_free(ERR_STATE *s)
if (s == NULL)
return;
for (i = 0; i < ERR_NUM_ERRORS; i++) {
for (i = 0; i < ERR_NUM_ERRORS; i++)
err_clear_data(s, i);
}
free(s);
}
@ -598,8 +581,10 @@ ERR_get_state(void)
ERR_STATE_free(ret); /* could not insert it */
return (&fallback);
}
/* If a race occurred in this function and we came second, tmpp
* is the first one that we just replaced. */
/*
* If a race occurred in this function and we came second,
* tmpp is the first one that we just replaced.
*/
if (tmpp)
ERR_STATE_free(tmpp);
}
@ -609,7 +594,7 @@ ERR_get_state(void)
static void
err_load_strings(int lib, ERR_STRING_DATA *str)
{
while (str->error) {
while (str->error != 0) {
if (lib)
str->error |= ERR_PACK(lib, 0, 0);
err_set_item(str);
@ -617,6 +602,15 @@ err_load_strings(int lib, ERR_STRING_DATA *str)
}
}
static void
err_load_const_strings(const ERR_STRING_DATA *str)
{
while (str->error != 0) {
err_set_item(str);
str++;
}
}
static unsigned long
get_error_values(int inc, int top, const char **file, int *line,
const char **data, int *flags)
@ -688,10 +682,10 @@ ERR_load_ERR_strings_internal(void)
{
err_init_thread = pthread_self();
#ifndef OPENSSL_NO_ERR
err_load_strings(0, ERR_str_libraries);
err_load_strings(0, ERR_str_reasons);
err_load_strings(ERR_LIB_SYS, ERR_str_functs);
build_SYS_str_reasons();
err_load_const_strings(ERR_str_libraries);
err_load_const_strings(ERR_str_reasons);
err_load_const_strings(ERR_str_functs);
err_build_SYS_str_reasons();
err_load_strings(ERR_LIB_SYS, SYS_str_reasons);
#endif
}
@ -723,10 +717,7 @@ void
ERR_load_const_strings(const ERR_STRING_DATA *str)
{
ERR_load_ERR_strings();
while (str->error) {
err_set_item(str);
str++;
}
err_load_const_strings(str);
}
void
@ -770,8 +761,11 @@ ERR_remove_thread_state(const CRYPTO_THREADID *id)
CRYPTO_THREADID_cpy(&tmp.tid, id);
else
CRYPTO_THREADID_current(&tmp.tid);
/* err_thread_del_item automatically destroys the LHASH if the number of
* items reaches zero. */
/*
* err_thread_del_item automatically destroys the LHASH if the number of
* items reaches zero.
*/
err_thread_del_item(&tmp);
}
LCRYPTO_ALIAS(ERR_remove_thread_state);
@ -827,9 +821,9 @@ ERR_clear_error(void)
es = ERR_get_state();
for (i = 0; i < ERR_NUM_ERRORS; i++) {
for (i = 0; i < ERR_NUM_ERRORS; i++)
err_clear(es, i);
}
es->top = es->bottom = 0;
}
LCRYPTO_ALIAS(ERR_clear_error);
@ -1059,8 +1053,10 @@ ERR_error_string_n(unsigned long e, char *buf, size_t len)
if (ret == -1)
return; /* can't happen, and can't do better if it does */
if (ret >= len) {
/* output may be truncated; make sure we always have 5
* colon-separated fields, i.e. 4 colons ... */
/*
* output may be truncated; make sure we always have 5
* colon-separated fields, i.e. 4 colons ...
*/
#define NUM_COLONS 4
if (len > NUM_COLONS) /* ... if possible */
{
@ -1083,9 +1079,12 @@ ERR_error_string_n(unsigned long e, char *buf, size_t len)
}
LCRYPTO_ALIAS(ERR_error_string_n);
/* BAD for multi-threading: uses a local buffer if ret == NULL */
/* ERR_error_string_n should be used instead for ret != NULL
* as ERR_error_string cannot know how large the buffer is */
/*
* ERR_error_string_n should be used instead for ret != NULL
* as ERR_error_string cannot know how large the buffer is.
*
* BAD for multi-threading: uses a local buffer if ret == NULL.
*/
char *
ERR_error_string(unsigned long e, char *ret)
{