diff --git a/src/openvpn/ssl_verify.h b/src/openvpn/ssl_verify.h
index e77bc4a..39f39b0 100644
--- a/src/openvpn/ssl_verify.h
+++ b/src/openvpn/ssl_verify.h
@@ -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
 {
diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbedtls.c
index bba2f2f..3e0a4ea 100644
--- a/src/openvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ssl_verify_mbedtls.c
@@ -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);
 
     /*
diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_openssl.c
index 731ab3e..ee29691 100644
--- a/src/openvpn/ssl_verify_openssl.c
+++ b/src/openvpn/ssl_verify_openssl.c
@@ -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);
 
