[Openvpn-devel,v3] pkcs11: bound certificate size in pkcs11_management_id_get()

Message ID 20261117183203.18936-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v3] pkcs11: bound certificate size in pkcs11_management_id_get() |

Commit Message

Gert Doering Nov. 17, 2026, 6:31 p.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

0a8e80aa ("Avoid unbounded allocations in pkcs11_mbedtls.c") capped
the certificate blob size reported by the PKCS#11 provider only in the
mbedTLS backend. pkcs11_management_id_get() still allocated whatever
size the provider returned. Move the limit to pkcs11_backend.h as
PKCS11_MAX_CERT_SIZE and check it there too.

Change-Id: Ia9f328b1c0ea29adf811a933d1fd22cb32e35e6d
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2121
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2121
This mail reflects revision 3 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <gert@greenie.muc.de>
  

Patch

diff --git a/src/openvpn/pkcs11.c b/src/openvpn/pkcs11.c
index b4bed12..3610224 100644
--- a/src/openvpn/pkcs11.c
+++ b/src/openvpn/pkcs11.c
@@ -509,6 +509,13 @@ 
         goto cleanup;
     }
 
+    if (certificate_blob_size > PKCS11_MAX_CERT_SIZE)
+    {
+        msg(M_WARN, "PKCS#11: Certificate too large: %zu bytes, maximum is %zu",
+            certificate_blob_size, PKCS11_MAX_CERT_SIZE);
+        goto cleanup;
+    }
+
     if ((certificate_blob = (unsigned char *)malloc(certificate_blob_size)) == NULL)
     {
         msg(M_FATAL, "PKCS#11: Cannot allocate memory");
diff --git a/src/openvpn/pkcs11_backend.h b/src/openvpn/pkcs11_backend.h
index b8b0eb0..972c3a2 100644
--- a/src/openvpn/pkcs11_backend.h
+++ b/src/openvpn/pkcs11_backend.h
@@ -37,6 +37,20 @@ 
 
 #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
 
+/*
+ * Maximum size of a certificate retrieved from a PKCS #11 provider, so that
+ * the provider cannot crash OpenVPN by making it try to allocate 2^64 bytes.
+ * The maximum of 100.000 bytes is picked as a round number that easily
+ * accomodates the currently standardized quantum-safe signature algorithms.
+ * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
+ *
+ * However, there are additional digital signature schemes currently on the
+ * NIST on-ramp (e.g., some parameter settings for LESS) that have even larger
+ * public keys or signatures, so if those ever see use on smartcards, we will
+ * need to increase this number.
+ */
+#define PKCS11_MAX_CERT_SIZE ((size_t)100000)
+
 /**
  * Retrieve PKCS #11 Certificate's DN in a printable format.
  *
diff --git a/src/openvpn/pkcs11_mbedtls.c b/src/openvpn/pkcs11_mbedtls.c
index 2183217..3ee3837 100644
--- a/src/openvpn/pkcs11_mbedtls.c
+++ b/src/openvpn/pkcs11_mbedtls.c
@@ -42,16 +42,6 @@ 
 static bool
 pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert)
 {
-    /* We set a maximum size for certificates so that the PKCS provider cannot crash OpenVPN by
-     * making it try to allocate 2^64 bytes. The maximum of 100.000 bytes is picked as a round
-     * number that easily accomodates the currently standardized quantum-safe signature algorithms.
-     * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
-     *
-     * However, there are additional digital signature schemes currently on the NIST on-ramp
-     * (e.g., some parameter settings for LESS) that have even larger public keys or signatures, so
-     * if those ever see use on smartcards, we will need to increase this number. */
-    const size_t max_cert_size = 100000;
-
     unsigned char *cert_blob = NULL;
     size_t cert_blob_size = 0;
     bool ret = false;
@@ -62,9 +52,10 @@ 
         goto cleanup;
     }
 
-    if (cert_blob_size > max_cert_size)
+    if (cert_blob_size > PKCS11_MAX_CERT_SIZE)
     {
-        msg(M_WARN, "PKCS#11: Certificate too large: %zu bytes, maximum is %zu", cert_blob_size, max_cert_size);
+        msg(M_WARN, "PKCS#11: Certificate too large: %zu bytes, maximum is %zu", cert_blob_size,
+            PKCS11_MAX_CERT_SIZE);
         goto cleanup;
     }