]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: ssl: prevent CN fallback when a SAN extension is present master
authorWilliam Lallemand <wlallemand@haproxy.com>
Thu, 26 Nov 2026 17:48:04 +0000 (17:48 +0000)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 26 Nov 2026 17:57:58 +0000 (18:57 +0100)
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

index 34b646f9f982295660a7ef159b7be7883bf74602..42e880843ece88d5343255354f76970551f82b6c 100644 (file)
@@ -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);