From 47521745280e2570f68b8a1ef1153a26acc5cb81 Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Thu, 26 Nov 2026 17:48:04 +0000 Subject: [PATCH] BUG/MEDIUM: ssl: prevent CN fallback when a SAN extension is present Yang Hailong of Alipay Security Team reported that backend hostname verification accepts a certificate with a URI-only SAN and a matching subject CN. The fallback was disabled only after encountering a DNS SAN, so certificates containing other SAN types could still match their CN. Use the status returned by X509_get_ext_d2i() to allow CN fallback only when the SAN extension is absent. Present but undecodable or duplicate SAN extensions do not permit fallback either. RFC 9525 obsoletes RFC 6125 and prohibits using the subject Common Name to identify a service. This change does not fully implement that policy: CN fallback is retained for certificates without a SAN extension to preserve compatibility with legacy deployments. This intentionally tightens certificate acceptance: non-DNS SANs now suppress CN matching, while certificates without SAN retain CN fallback. Verified with the existing verifyhost regression and live backend TLS checks covering URI-only SAN rejection, mixed URI/DNS SAN acceptance, and CN-only compatibility, using both verifyhost and SNI. This change must not be backported because it changes certificate acceptance and could break existing deployments on stable branches. Reported-by: Yang Hailong --- src/ssl_sock.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ssl_sock.c b/src/ssl_sock.c index 34b646f9f..42e880843 100644 --- a/src/ssl_sock.c +++ b/src/ssl_sock.c @@ -5385,7 +5385,7 @@ static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) __X509_NAME_CONST__ X509_NAME *cert_subject; char *str; int len; - int dnsname_present = 0; + int san_status = 0; if (ok == 0) return ok; @@ -5429,12 +5429,11 @@ static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) if (!cert) return ok; - alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); + alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, &san_status, NULL); if (alt_names) { for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); if (name->type == GEN_DNS) { - dnsname_present = 1; #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L if ((len = ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5)) >= 0) { #else @@ -5454,9 +5453,10 @@ static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); } + /* Only an absent SAN extension (status -1) permits CN fallback. */ cert_subject = X509_get_subject_name(cert); i = -1; - while (!dnsname_present && !ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { + while (san_status == -1 && !ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { __X509_NAME_CONST__ X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); __X509_NAME_CONST__ ASN1_STRING *value; value = X509_NAME_ENTRY_get_data(entry); -- 2.47.3