[Openvpn-devel,v3] pkcs11: bound certificate size in pkcs11_management_id_get()
Commit Message
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>
@@ -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");
@@ -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.
*
@@ -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;
}