[Openvpn-devel,v14] Reject certificates with too large serial numbers
Commit Message
From: Max Fillinger <max@max-fillinger.net>
Arturs Parker and Jim Biniyaz, ResilientX Security, reported that
OpenVPN with the Mbed TLS backend can be DoS'd by sending certificates
with extremely large (8 KB) serial numbers, because the functions for
writing these serial numbers in decimal are inefficient.
This could be fixed by checking the serial number size before printing,
but we decided instead to check the certificates immediately (both in
Mbed TLS and OpenSSL) and refuse them if their serial number is too
large. This is because someone sending a certificate with an excessively
long serial number is likely up to no good, and we would rather not wait
to find out if they can also cause problems in other places.
We use 40 bytes as the limit for serial numbers, which is twice the
maximum allowed by RFC 5280.
CVE: 2026-135412
Reported-by: Arturs Parker and Jim Biniyaz, ResilientX Security
Github: OpenVPN/openvpn-private-issues/issues/187
Change-Id: I6c6dfed1557608d5b1442fb008bf84fa4f948579
Signed-off-by: Max Fillinger <max@max-fillinger.net>
Acked-by: Steffan Karger <steffan@karger.me>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn-private/+/2325
---
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-private/+/2325
This mail reflects revision 14 of this Change.
Acked-by according to Gerrit (reflected above):
Steffan Karger <steffan@karger.me>
@@ -53,6 +53,20 @@
/** Maximum length of common name */
#define TLS_USERNAME_LEN 64
+/**
+ * Maximum length for serial numbers.
+ *
+ * RFC 5280 says that conformant CAs must not issue certificates with serial
+ * numbers longer than 20 bytes. To be generous, we allow double that amount.
+ *
+ * Note that the OpenSSL and Mbed TLS backends calculate the serial size
+ * slightly differently, with Mbed TLS taking the length of the DER encoded
+ * number in the certificate, and OpenSSL the size of the bignum representation.
+ * For some numbers, there is a 1 byte difference in the sizes of these
+ * representations.
+ */
+#define MAX_SERIAL_SIZE 40
+
/** Structure containing the hash for a single certificate */
struct cert_hash
{
@@ -43,6 +43,19 @@
#define MAX_SUBJECT_LENGTH 256
+/**
+ * Check the size of a certificate serial number.
+ *
+ * @param cert Certificate to check.
+ *
+ * @return True if the size of the serial number is acceptable.
+ */
+static bool
+backend_x509_serial_size_ok(const mbedtls_x509_crt *cert)
+{
+ return cert->serial.len <= MAX_SERIAL_SIZE;
+}
+
int
verify_callback(void *session_obj, mbedtls_x509_crt *cert, int cert_depth, uint32_t *flags)
{
@@ -54,6 +67,14 @@
session->verified = false;
+ if (!backend_x509_serial_size_ok(cert))
+ {
+ *flags |= MBEDTLS_X509_BADCERT_OTHER;
+ msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, Certificate with excessively long serial, maximum is %d bytes.",
+ cert_depth, MAX_SERIAL_SIZE);
+ goto cleanup;
+ }
+
/* Remember certificate hash */
struct buffer cert_fingerprint = x509_get_sha256_fingerprint(cert, &gc);
cert_hash_remember(session, cert_depth, &cert_fingerprint);
@@ -112,6 +133,7 @@
*flags |= MBEDTLS_X509_BADCERT_OTHER;
}
+cleanup:
gc_free(&gc);
/*
@@ -46,6 +46,28 @@
#include <openssl/err.h>
#include <openssl/x509v3.h>
+/**
+ * Check the size of a certificate serial number.
+ *
+ * @param cert Certificate to check.
+ *
+ * @return True if the size of the serial number is acceptable.
+ */
+static bool
+backend_x509_serial_size_ok(const X509 *cert)
+{
+ bool size_ok = false;
+ const ASN1_INTEGER *asn1_i = X509_get0_serialNumber(cert);
+ BIGNUM *bn_serial = ASN1_INTEGER_to_BN(asn1_i, NULL);
+ if (bn_serial != NULL)
+ {
+ int size = BN_num_bytes(bn_serial);
+ size_ok = size <= MAX_SERIAL_SIZE;
+ }
+ BN_free(bn_serial);
+ return size_ok;
+}
+
int
verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
{
@@ -61,6 +83,13 @@
ASSERT(session);
X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
+ if (!backend_x509_serial_size_ok(current_cert))
+ {
+ msg(D_TLS_ERRORS, "VERIFY ERROR: depth=%d, Certificate with excessively long serial, maximum is %d bytes.",
+ X509_STORE_CTX_get_error_depth(ctx), MAX_SERIAL_SIZE);
+ goto cleanup;
+ }
+
struct buffer cert_hash = x509_get_sha256_fingerprint(current_cert, &gc);
cert_hash_remember(session, X509_STORE_CTX_get_error_depth(ctx), &cert_hash);