From 33847508d5605d8dbe868d7694a4eff79d785404 Mon Sep 17 00:00:00 2001 From: "Dr. David von Oheimb" Date: Tue, 28 Jun 2022 08:17:59 +0200 Subject: [PATCH] libcrypto refactoring: make more use of ASN1_STRING_set0() Reviewed-by: Tomas Mraz Reviewed-by: Shane Lontis Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/18668) --- crypto/asn1/a_bitstr.c | 4 +--- crypto/asn1/a_int.c | 4 +--- crypto/asn1/a_mbstr.c | 4 +--- crypto/asn1/a_sign.c | 8 ++------ crypto/asn1/asn_pack.c | 5 ++--- crypto/asn1/tasn_dec.c | 4 +--- crypto/x509/x_pubkey.c | 4 +--- 7 files changed, 9 insertions(+), 24 deletions(-) diff --git a/crypto/asn1/a_bitstr.c b/crypto/asn1/a_bitstr.c index 7c25649357..f8938ad107 100644 --- a/crypto/asn1/a_bitstr.c +++ b/crypto/asn1/a_bitstr.c @@ -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; diff --git a/crypto/asn1/a_int.c b/crypto/asn1/a_int.c index 19e41ec73e..c3ab6a9222 100644 --- a/crypto/asn1/a_int.c +++ b/crypto/asn1/a_int.c @@ -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; diff --git a/crypto/asn1/a_mbstr.c b/crypto/asn1/a_mbstr.c index 22dea873ee..be2d5aa68f 100644 --- a/crypto/asn1/a_mbstr.c +++ b/crypto/asn1/a_mbstr.c @@ -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; diff --git a/crypto/asn1/a_sign.c b/crypto/asn1/a_sign.c index 47073daa4b..fc3f15007e 100644 --- a/crypto/asn1/a_sign.c +++ b/crypto/asn1/a_sign.c @@ -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 diff --git a/crypto/asn1/asn_pack.c b/crypto/asn1/asn_pack.c index 292e6d8176..bf6e273b93 100644 --- a/crypto/asn1/asn_pack.c +++ b/crypto/asn1/asn_pack.c @@ -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); diff --git a/crypto/asn1/tasn_dec.c b/crypto/asn1/tasn_dec.c index 11198087a5..1701eb9d56 100644 --- a/crypto/asn1/tasn_dec.c +++ b/crypto/asn1/tasn_dec.c @@ -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)) { diff --git a/crypto/x509/x_pubkey.c b/crypto/x509/x_pubkey.c index 6c554bc030..126c2400f6 100644 --- a/crypto/x509/x_pubkey.c +++ b/crypto/x509/x_pubkey.c @@ -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;