Skip to contentSkip to conversation

crypto/x509-utils: Check for error from gnutls_x509_crt_init()

PATCH In review Peter Maydell 3 participants 3 messages Jul 10, 2025 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto/x509-utils: Check for error from gnutls_x509_crt_init()

Coverity notes that in qcrypto_get_x509_cert_fingerprint() we call gnutls_x509_crt_init() but don't check for an error return. Add the missing check.

Coverity: CID 1593155

Fixes 10a1d34fc0d ("crypto: Introduce x509 utils")
---
 crypto/x509-utils.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
crypto/x509-utils.c +5 -1
index 8bad00a51b8..39bb6d4d8c3 100644
@@ -46,7 +46,11 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,
         return -1;
     }
 
-    gnutls_x509_crt_init(&crt);
+    if (gnutls_x509_crt_init(&crt) < 0) {
+        error_setg(errp, "Unable to initialize certificate: %s",
+                   gnutls_strerror(ret));
+        return -1;
+    }
 
     if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM) != 0) {
         error_setg(errp, "Failed to import certificate");
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Peter Maydell

Coverity notes that in qcrypto_get_x509_cert_fingerprint() we call gnutls_x509_crt_init() but don't check for an error return. Add the missing check.

Coverity: CID 1593155

Fixes 10a1d34fc0d ("crypto: Introduce x509 utils")
---
 crypto/x509-utils.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 20 lines
crypto/x509-utils.c +5 -1
index 8bad00a51b8..39bb6d4d8c3 100644
@@ -46,7 +46,11 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,
         return -1;
     }
 
-    gnutls_x509_crt_init(&crt);
+    if (gnutls_x509_crt_init(&crt) < 0) {
+        error_setg(errp, "Unable to initialize certificate: %s",
+                   gnutls_strerror(ret));
+        return -1;
+    }
 
     if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM) != 0) {
         error_setg(errp, "Failed to import certificate");
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

Coverity notes that in qcrypto_get_x509_cert_fingerprint() we call gnutls_x509_crt_init() but don't check for an error return. Add the missing check.

Coverity: CID 1593155

Fixes 10a1d34fc0d ("crypto: Introduce x509 utils")
---
  crypto/x509-utils.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)