From d90344a2d5ca3a0caacf7d0c12f981be86862d8c Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Wed, 23 Jun 2021 13:14:53 +0000 Subject: [PATCH] genprotimg: check return value of BIO_reset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing return value checks for BIO_reset. Unfortunately, the OpenSSL documentation says: "BIO_reset() normally returns 1 for success and 0 or -1 for failure. File BIOs are an exception, they return 0 for success and -1 for failure." Github-ID: https://github.com/ibm-s390-linux/s390-tools/issues/112 Reviewed-by: Patrick Steuer Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- genprotimg/src/utils/crypto.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/genprotimg/src/utils/crypto.c b/genprotimg/src/utils/crypto.c index 81367b23..36379f06 100644 --- a/genprotimg/src/utils/crypto.c +++ b/genprotimg/src/utils/crypto.c @@ -440,10 +440,14 @@ static int check_signature_algo_match(const EVP_PKEY *pkey, const X509 *subject, static X509_CRL *load_crl_from_bio(BIO *bio) { g_autoptr(X509_CRL) crl = PEM_read_bio_X509_CRL(bio, NULL, 0, NULL); + gint rc; + if (crl) return g_steal_pointer(&crl); ERR_clear_error(); - BIO_reset(bio); + rc = BIO_reset(bio); + if (rc != 1 || (rc != 0 && BIO_method_type(bio) == BIO_TYPE_FILE)) + return NULL; /* maybe the CRL is stored in DER format */ crl = d2i_X509_CRL_bio(bio, NULL); @@ -514,6 +518,7 @@ X509 *load_cert_from_file(const char *path, GError **err) { g_autoptr(BIO) bio = bio_read_from_file(path); g_autoptr(X509) cert = NULL; + gint rc; if (!bio) { g_set_error(err, PV_CRYPTO_ERROR, @@ -526,7 +531,12 @@ X509 *load_cert_from_file(const char *path, GError **err) if (cert) return g_steal_pointer(&cert); ERR_clear_error(); - BIO_reset(bio); + rc = BIO_reset(bio); + if (rc != 1 || (rc != 0 && BIO_method_type(bio) == BIO_TYPE_FILE)) { + g_set_error(err, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_READ_CERTIFICATE, + _("unable to load certificate: '%s'"), path); + return NULL; + } /* maybe the certificate is stored in DER format */ cert = d2i_X509_bio(bio, NULL);