Attention is currently required from: Arne Schwabe.
Hello Arne Schwabe,
I'd like you to do a code review.
Please visit
https://gerrit.openvpn.net/c/openvpn/+/2359?usp=email
to review the following change.
Change subject: Reject certificates with too large serial numbers
......................................................................
Reject certificates with too large serial numbers
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 <[email protected]>
---
M src/openvpn/cryptoapi.c
M src/openvpn/ssl_mbedtls.c
M src/openvpn/ssl_openssl.c
M src/openvpn/ssl_verify.h
M src/openvpn/ssl_verify_backend.h
M src/openvpn/ssl_verify_mbedtls.c
M src/openvpn/ssl_verify_openssl.c
7 files changed, 139 insertions(+), 13 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/59/2359/1
diff --git a/src/openvpn/cryptoapi.c b/src/openvpn/cryptoapi.c
index 30a5144..d32100e 100644
--- a/src/openvpn/cryptoapi.c
+++ b/src/openvpn/cryptoapi.c
@@ -52,6 +52,8 @@
#include "win32.h"
#include "xkey_common.h"
#include "crypto_openssl.h"
+#include "ssl_verif.h"
+#include "ssl_verify_backend.h"
#ifndef HAVE_XKEY_PROVIDER
@@ -618,6 +620,10 @@
{
return ret;
}
+ if (!backend_x509_serial_size_ok(cert))
+ {
+ msg(M_FATAL, "The certificate serial number is too large, maximum size
is %d.", MAX_SERIAL_SIZE);
+ }
if (SSL_CTX_use_certificate(ssl_ctx, cert) &&
SSL_CTX_use_PrivateKey(ssl_ctx, privkey))
{
crypto_print_openssl_errors(M_WARN);
diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c
index dbc1b9d..0bdca37 100644
--- a/src/openvpn/ssl_mbedtls.c
+++ b/src/openvpn/ssl_mbedtls.c
@@ -44,6 +44,8 @@
#include "pkcs11_backend.h"
#include "ssl_common.h"
#include "ssl_util.h"
+#include "ssl_verify.h"
+#include "ssl_verify_backend.h"
#include "ssl_verify_mbedtls.h"
#include <mbedtls/debug.h>
@@ -427,6 +429,21 @@
}
#endif /* _WIN32 */
+static bool
+cert_chain_serial_size_ok(const mbedtls_x509_crt *cert_chain)
+{
+ const mbedtls_x509_crt *cert = cert_chain;
+ while (cert != NULL)
+ {
+ if (!backend_x509_serial_size_ok(cert))
+ {
+ return false;
+ }
+ cert = cert->next;
+ }
+ return true;
+}
+
void
tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool
cert_inline)
{
@@ -456,6 +473,11 @@
msg(M_FATAL, "Cannot load certificate file %s", cert_file);
}
}
+
+ if (!cert_chain_serial_size_ok(ctx->crt_chain))
+ {
+ msg(M_FATAL, "The serial number of a certificate is too large, maximum
size is %d.", MAX_SERIAL_SIZE);
+ }
}
int
@@ -605,6 +627,11 @@
msg(M_FATAL, "Cannot load CA certificate file %s", ca_file);
}
}
+
+ if (!cert_chain_serial_size_ok(ctx->ca_chain))
+ {
+ msg(M_FATAL, "The serial number of a CA certificate is too large,
maximum size is %d.", MAX_SERIAL_SIZE);
+ }
}
void
@@ -633,6 +660,12 @@
msg(M_FATAL, "Cannot load extra-certs file: %s", extra_certs_file);
}
}
+
+ if (!cert_chain_serial_size_ok(ctx->crt_chain))
+ {
+ msg(M_FATAL, "The certificate serial number of an extra certificate is
too large, maximum size is %d.",
+ MAX_SERIAL_SIZE);
+ }
}
/* **************************************
diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c
index 03e0279..52b05dd 100644
--- a/src/openvpn/ssl_openssl.c
+++ b/src/openvpn/ssl_openssl.c
@@ -41,6 +41,8 @@
#include "memdbg.h"
#include "ssl_backend.h"
#include "ssl_common.h"
+#include "ssl_verify.h"
+#include "ssl_verify_backend.h"
#include "base64.h"
#include "openssl_compat.h"
#include "xkey_common.h"
@@ -912,7 +914,7 @@
FILE *fp;
EVP_PKEY *pkey;
X509 *cert;
- STACK_OF(X509) *ca = NULL;
+ STACK_OF(X509) *ca_stack = NULL;
PKCS12 *p12;
char password[256];
@@ -948,12 +950,12 @@
}
/* Parse the PKCS #12 file */
- if (!PKCS12_parse(p12, "", &pkey, &cert, &ca))
+ if (!PKCS12_parse(p12, "", &pkey, &cert, &ca_stack))
{
pem_password_callback(password, sizeof(password) - 1, 0, NULL);
/* Reparse the PKCS #12 file with password */
- ca = NULL;
- if (!PKCS12_parse(p12, password, &pkey, &cert, &ca))
+ ca_stack = NULL;
+ if (!PKCS12_parse(p12, password, &pkey, &cert, &ca_stack))
{
crypto_msg(M_WARN, "Decoding PKCS12 failed. Probably wrong
password "
"or unsupported/legacy encryption");
@@ -970,6 +972,10 @@
PKCS12_free(p12);
/* Load Certificate */
+ if (!backend_x509_serial_size_ok(cert))
+ {
+ msg(M_FATAL, "The certificate serial number is too large, maximum size
is %d.", MAX_SERIAL_SIZE);
+ }
if (!SSL_CTX_use_certificate(ctx->ctx, cert))
{
crypto_print_openssl_errors(M_WARN);
@@ -995,24 +1001,29 @@
* They're also used to fill in the chain of intermediate certs as
* necessary.
*/
- if (ca && sk_X509_num(ca))
+ if (ca_stack && sk_X509_num(ca_stack))
{
- for (openssl_stack_size_t i = 0; i < sk_X509_num(ca); i++)
+ for (openssl_stack_size_t i = 0; i < sk_X509_num(ca_stack); i++)
{
+ X509 *ca = sk_X509_value(ca_stack, i);
+ if (!backend_x509_serial_size_ok(ca))
+ {
+ msg(M_FATAL, "The certificate serial number is too large,
maximum size is %d.", MAX_SERIAL_SIZE);
+ }
X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx->ctx);
- if (!X509_STORE_add_cert(cert_store, sk_X509_value(ca, i)))
+ if (!X509_STORE_add_cert(cert_store, ca))
{
crypto_msg(M_FATAL,
"Cannot add certificate to certificate chain
(X509_STORE_add_cert)");
}
- if (!SSL_CTX_add_client_CA(ctx->ctx, sk_X509_value(ca, i)))
+ if (!SSL_CTX_add_client_CA(ctx->ctx, ca))
{
crypto_msg(M_FATAL,
"Cannot add certificate to client CA list
(SSL_CTX_add_client_CA)");
}
}
}
- sk_X509_pop_free(ca, X509_free);
+ sk_X509_pop_free(ca_stack, X509_free);
}
else
{
@@ -1021,11 +1032,16 @@
* certs chain just in case they include intermediate CAs needed to
* prove my identity to the other end. This does not make them trusted.
*/
- if (ca && sk_X509_num(ca))
+ if (ca_stack && sk_X509_num(ca_stack))
{
- for (openssl_stack_size_t i = 0; i < sk_X509_num(ca); i++)
+ for (openssl_stack_size_t i = 0; i < sk_X509_num(ca_stack); i++)
{
- if (!SSL_CTX_add_extra_chain_cert(ctx->ctx, sk_X509_value(ca,
i)))
+ const X509 *chain_cert = sk_X509_value(ca_stack, i);
+ if (!backend_x509_serial_size_ok(chain_cert))
+ {
+ msg(M_FATAL, "The certificate serial number is too large,
maximum size is %d.", MAX_SERIAL_SIZE);
+ }
+ if (!SSL_CTX_add_extra_chain_cert(ctx->ctx, chain_cert))
{
crypto_msg(
M_FATAL,
@@ -1034,7 +1050,7 @@
}
}
/* the certs now belong to ctx */
- sk_X509_free(ca);
+ sk_X509_free(ca_stack);
}
X509_free(cert);
EVP_PKEY_free(pkey);
@@ -1079,6 +1095,12 @@
/* Otherwise, bail out with error */
crypto_msg(M_FATAL, "Error reading extra certificate");
}
+
+ if (!backend_x509_serial_size_ok(cert))
+ {
+ msg(M_FATAL, "The certificate serial number is too large, maximum
size is %d.", MAX_SERIAL_SIZE);
+ }
+
/* takes ownership of cert like a set1 method */
if (SSL_CTX_add_extra_chain_cert(ctx->ctx, cert) != 1)
{
@@ -1154,6 +1176,11 @@
}
msg(D_TLS_DEBUG_MED, "Found cert in store using URI: %s", uri);
+ if (!backend_x509_serial_size_ok(x))
+ {
+ msg(M_FATAL, "The certificate serial number is too large, maximum size
is %d.", MAX_SERIAL_SIZE);
+ }
+
ret = SSL_CTX_use_certificate(tls_ctx->ctx, x);
if (!ret)
{
@@ -1233,6 +1260,11 @@
goto end;
}
+ if (!backend_x509_serial_size_ok(x))
+ {
+ msg(M_FATAL, "The certificate serial number is too large, maximum size
is %d.", MAX_SERIAL_SIZE);
+ }
+
ret = SSL_CTX_use_certificate(ctx->ctx, x);
if (ret)
{
diff --git a/src/openvpn/ssl_verify.h b/src/openvpn/ssl_verify.h
index e77bc4a..cfb71a9 100644
--- a/src/openvpn/ssl_verify.h
+++ b/src/openvpn/ssl_verify.h
@@ -53,6 +53,14 @@
/** 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.
+ */
+#define MAX_SERIAL_SIZE 40
+
/** Structure containing the hash for a single certificate */
struct cert_hash
{
diff --git a/src/openvpn/ssl_verify_backend.h b/src/openvpn/ssl_verify_backend.h
index 46720d6..c1f68c52 100644
--- a/src/openvpn/ssl_verify_backend.h
+++ b/src/openvpn/ssl_verify_backend.h
@@ -265,4 +265,13 @@
*/
bool tls_verify_crl_missing(const struct tls_options *opt);
+/**
+ * Check the size of a certificate serial number.
+ *
+ * @param cert Certificate to check.
+ *
+ * @return True if the size of the serial number is acceptable.
+ */
+bool backend_x509_serial_size_ok(const openvpn_x509_cert_t *cert);
+
#endif /* SSL_VERIFY_BACKEND_H_ */
diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbedtls.c
index bba2f2f..81f891e 100644
--- a/src/openvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ssl_verify_mbedtls.c
@@ -37,6 +37,7 @@
#include "crypto_mbedtls.h"
#include "ssl_verify.h"
+#include "ssl_verify_backend.h"
#include <mbedtls/asn1.h>
#include <mbedtls/error.h>
#include <mbedtls/oid.h>
@@ -54,6 +55,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 +121,7 @@
*flags |= MBEDTLS_X509_BADCERT_OTHER;
}
+cleanup:
gc_free(&gc);
/*
@@ -120,6 +130,12 @@
return 0;
}
+bool
+backend_x509_serial_size_ok(const mbedtls_x509_crt *cert)
+{
+ return cert->serial.len <= MAX_SERIAL_SIZE;
+}
+
/* not supported for mbedTLS yet */
bool
x509_username_field_ext_supported(const char *fieldname)
diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_openssl.c
index 731ab3e..266bbf4 100644
--- a/src/openvpn/ssl_verify_openssl.c
+++ b/src/openvpn/ssl_verify_openssl.c
@@ -46,6 +46,21 @@
#include <openssl/err.h>
#include <openssl/x509v3.h>
+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 +76,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: Certificate with excessively long
serial, maximum is %d bytes.",
+ 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);
--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2359?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6c6dfed1557608d5b1442fb008bf84fa4f948579
Gerrit-Change-Number: 2359
Gerrit-PatchSet: 1
Gerrit-Owner: Max Fillinger <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel