libcrypto refactoring: make more use of ASN1_STRING_set0()

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/18668)
This commit is contained in:
Dr. David von Oheimb 2022-06-28 08:17:59 +02:00 committed by Dr. David von Oheimb
parent 9df71587f1
commit 33847508d5
7 changed files with 9 additions and 24 deletions

View File

@ -125,9 +125,7 @@ ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
} else
s = NULL;
ret->length = (int)len;
OPENSSL_free(ret->data);
ret->data = s;
ASN1_STRING_set0(ret, s, (int)len);
ret->type = V_ASN1_BIT_STRING;
if (a != NULL)
(*a) = ret;

View File

@ -444,9 +444,7 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
p += len;
}
OPENSSL_free(ret->data);
ret->data = s;
ret->length = (int)len;
ASN1_STRING_set0(ret, s, (int)len);
if (a != NULL)
(*a) = ret;
*pp = p;

View File

@ -139,9 +139,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
if (*out) {
free_out = 0;
dest = *out;
OPENSSL_free(dest->data);
dest->data = NULL;
dest->length = 0;
ASN1_STRING_set0(dest, NULL, 0);
dest->type = str_type;
} else {
free_out = 1;

View File

@ -96,10 +96,8 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
goto err;
}
OPENSSL_free(signature->data);
signature->data = buf_out;
ASN1_STRING_set0(signature, buf_out, outl);
buf_out = NULL;
signature->length = outl;
/*
* In the interests of compatibility, I'll make sure that the bit string
* has a 'not-used bits' value of 0
@ -282,10 +280,8 @@ int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
goto err;
}
OPENSSL_free(signature->data);
signature->data = buf_out;
ASN1_STRING_set0(signature, buf_out, outl);
buf_out = NULL;
signature->length = outl;
/*
* In the interests of compatibility, I'll make sure that the bit string
* has a 'not-used bits' value of 0

View File

@ -17,7 +17,7 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
{
ASN1_STRING *octmp;
if (oct == NULL || *oct == NULL) {
if (oct == NULL || *oct == NULL) {
if ((octmp = ASN1_STRING_new()) == NULL) {
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
return NULL;
@ -26,8 +26,7 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
octmp = *oct;
}
OPENSSL_free(octmp->data);
octmp->data = NULL;
ASN1_STRING_set0(octmp, NULL, 0);
if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) == 0) {
ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);

View File

@ -935,9 +935,7 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
}
/* If we've already allocated a buffer use it */
if (*free_cont) {
OPENSSL_free(stmp->data);
stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
stmp->length = len;
ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
*free_cont = 0;
} else {
if (!ASN1_STRING_set(stmp, cont, len)) {

View File

@ -980,9 +980,7 @@ int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
unsigned char *penc, int penclen)
{
OPENSSL_free(pub->public_key->data);
pub->public_key->data = penc;
pub->public_key->length = penclen;
ASN1_STRING_set0(pub->public_key, penc, penclen);
/* Set number of unused bits to zero */
pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;