libcrypto refactoring: make more use of ASN1_STRING_set0()
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Tue, 28 Jun 2022 06:17:59 +0000 (08:17 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Tue, 19 Jul 2022 06:44:19 +0000 (08:44 +0200)
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)

crypto/asn1/a_bitstr.c
crypto/asn1/a_int.c
crypto/asn1/a_mbstr.c
crypto/asn1/a_sign.c
crypto/asn1/asn_pack.c
crypto/asn1/tasn_dec.c
crypto/x509/x_pubkey.c

index 7c256493571e6005c07c5efd72769a7ee1f86992..f8938ad1073556314be209adabe1fb5a499da1ee 100644 (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;
index 19e41ec73e3571dee06bd8e4447bd0f0ca1f7a6d..c3ab6a92221f64ed104f418834050c1fd4d7bac7 100644 (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;
index 22dea873eeba566e398829f5d7602ef4ba9f2724..be2d5aa68f67733ea9e7a7120da32c1991254000 100644 (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;
index 47073daa4be044bd3b6cb447a14f50abddf18dfd..fc3f15007eab460d16e307d671ba2d04cff87aed 100644 (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
index 292e6d817697366ae4ae95919c11ad5178475961..bf6e273b93ca529af7bc160a467f114ed59a9202 100644 (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);
index 11198087a57b37989cdd2f6253738079db16962c..1701eb9d5671673fcf796ea09a90225c1461f207 100644 (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)) {
index 6c554bc030f60d13a3c83e439792fb3076894a0a..126c2400f6d9f5d1d51da6fefa23f5b2923d5d04 100644 (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;