X509_STORE_new: memory needs to be freed
authorPeiwei Hu <jlu.hpw@foxmail.com>
Wed, 15 Dec 2021 08:24:21 +0000 (16:24 +0800)
committerTomas Mraz <tomas@openssl.org>
Fri, 17 Dec 2021 14:10:32 +0000 (15:10 +0100)
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17278)

(cherry picked from commit c81eed84e4e9025e933778f5e8326b1e4435e094)

demos/cms/cms_ver.c
demos/smime/smver.c

index cd2b01e1b097d5cff45b5db429523c8a43d51b8d..3c0a7aa19edec5c8287d42c984f76ed26e339e6c 100644 (file)
@@ -27,16 +27,18 @@ int main(int argc, char **argv)
     /* Set up trusted CA certificate store */
 
     st = X509_STORE_new();
+    if (st == NULL)
+        goto err;
 
     /* Read in CA certificate */
     tbio = BIO_new_file("cacert.pem", "r");
 
-    if (!tbio)
+    if (tbio == NULL)
         goto err;
 
     cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
 
-    if (!cacert)
+    if (cacert == NULL)
         goto err;
 
     if (!X509_STORE_add_cert(st, cacert))
@@ -46,18 +48,18 @@ int main(int argc, char **argv)
 
     in = BIO_new_file("smout.txt", "r");
 
-    if (!in)
+    if (in == NULL)
         goto err;
 
     /* parse message */
     cms = SMIME_read_CMS(in, &cont);
 
-    if (!cms)
+    if (cms == NULL)
         goto err;
 
     /* File to output verified content to */
     out = BIO_new_file("smver.txt", "w");
-    if (!out)
+    if (out == NULL)
         goto err;
 
     if (!CMS_verify(cms, NULL, st, cont, out, 0)) {
@@ -76,6 +78,7 @@ int main(int argc, char **argv)
         ERR_print_errors_fp(stderr);
     }
 
+    X509_STORE_free(st);
     CMS_ContentInfo_free(cms);
     X509_free(cacert);
     BIO_free(in);
index 601462a041612bdf47221b13c27168db1ab7f759..5d552b18082ab5a77bfabbc8c0746ef77f29726e 100644 (file)
@@ -27,16 +27,18 @@ int main(int argc, char **argv)
     /* Set up trusted CA certificate store */
 
     st = X509_STORE_new();
+    if (st == NULL)
+        goto err;
 
     /* Read in signer certificate and private key */
     tbio = BIO_new_file("cacert.pem", "r");
 
-    if (!tbio)
+    if (tbio == NULL)
         goto err;
 
     cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
 
-    if (!cacert)
+    if (cacert == NULL)
         goto err;
 
     if (!X509_STORE_add_cert(st, cacert))
@@ -46,18 +48,18 @@ int main(int argc, char **argv)
 
     in = BIO_new_file("smout.txt", "r");
 
-    if (!in)
+    if (in == NULL)
         goto err;
 
     /* Sign content */
     p7 = SMIME_read_PKCS7(in, &cont);
 
-    if (!p7)
+    if (p7 == NULL)
         goto err;
 
     /* File to output verified content to */
     out = BIO_new_file("smver.txt", "w");
-    if (!out)
+    if (out == NULL)
         goto err;
 
     if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
@@ -74,6 +76,8 @@ int main(int argc, char **argv)
         fprintf(stderr, "Error Verifying Data\n");
         ERR_print_errors_fp(stderr);
     }
+
+    X509_STORE_free(st);
     PKCS7_free(p7);
     X509_free(cacert);
     BIO_free(in);