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 } else
s = NULL; s = NULL;
ret->length = (int)len; ASN1_STRING_set0(ret, s, (int)len);
OPENSSL_free(ret->data);
ret->data = s;
ret->type = V_ASN1_BIT_STRING; ret->type = V_ASN1_BIT_STRING;
if (a != NULL) if (a != NULL)
(*a) = ret; (*a) = ret;

View File

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

View File

@ -139,9 +139,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
if (*out) { if (*out) {
free_out = 0; free_out = 0;
dest = *out; dest = *out;
OPENSSL_free(dest->data); ASN1_STRING_set0(dest, NULL, 0);
dest->data = NULL;
dest->length = 0;
dest->type = str_type; dest->type = str_type;
} else { } else {
free_out = 1; 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); ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
goto err; goto err;
} }
OPENSSL_free(signature->data); ASN1_STRING_set0(signature, buf_out, outl);
signature->data = buf_out;
buf_out = NULL; buf_out = NULL;
signature->length = outl;
/* /*
* In the interests of compatibility, I'll make sure that the bit string * In the interests of compatibility, I'll make sure that the bit string
* has a 'not-used bits' value of 0 * 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); ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
goto err; goto err;
} }
OPENSSL_free(signature->data); ASN1_STRING_set0(signature, buf_out, outl);
signature->data = buf_out;
buf_out = NULL; buf_out = NULL;
signature->length = outl;
/* /*
* In the interests of compatibility, I'll make sure that the bit string * In the interests of compatibility, I'll make sure that the bit string
* has a 'not-used bits' value of 0 * 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; ASN1_STRING *octmp;
if (oct == NULL || *oct == NULL) { if (oct == NULL || *oct == NULL) {
if ((octmp = ASN1_STRING_new()) == NULL) { if ((octmp = ASN1_STRING_new()) == NULL) {
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
return NULL; return NULL;
@ -26,8 +26,7 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
octmp = *oct; octmp = *oct;
} }
OPENSSL_free(octmp->data); ASN1_STRING_set0(octmp, NULL, 0);
octmp->data = NULL;
if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) == 0) { if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) == 0) {
ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR); 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 we've already allocated a buffer use it */
if (*free_cont) { if (*free_cont) {
OPENSSL_free(stmp->data); ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
stmp->length = len;
*free_cont = 0; *free_cont = 0;
} else { } else {
if (!ASN1_STRING_set(stmp, cont, len)) { 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, void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
unsigned char *penc, int penclen) unsigned char *penc, int penclen)
{ {
OPENSSL_free(pub->public_key->data); ASN1_STRING_set0(pub->public_key, penc, penclen);
pub->public_key->data = penc;
pub->public_key->length = penclen;
/* Set number of unused bits to zero */ /* 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 | 0x07);
pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;