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
__X509_NAME_CONST__ X509_NAME *cert_subject;
char *str;
int len;
__X509_NAME_CONST__ X509_NAME *cert_subject;
char *str;
int len;
- int dnsname_present = 0;
- 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) {
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) {
#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
if ((len = ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5)) >= 0) {
#else
#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
if ((len = ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5)) >= 0) {
#else
sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
}
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;
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);
__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);