How the error appears#
Each client words it differently. OpenSSL defines X509_V_ERR_HOSTNAME_MISMATCH (hostname mismatch) and X509_V_ERR_IP_ADDRESS_MISMATCH. curl prints SSL: no alternative certificate subject name matches target hostname followed by the name. Python reports Hostname mismatch, certificate is not valid for the name. Node.js raises ERR_TLS_CERT_ALTNAME_INVALID with Hostname/IP does not match certificate's altnames, and its reason lists the names it found. Go prints x509: certificate is valid for the names it found, not the name it wanted, or cannot validate certificate for an address because it doesn't contain any IP SANs. OpenSSL verify error codes · curl OpenSSL backend · CPython ssl module source · Node.js tls.checkServerIdentity · Go x509 verify
The certificate itself is usually fine and current. The mismatch is between it and the name you asked for.
The rules the client applies#
The client compares the name it used to open the connection with the certificate's subject alternative names. RFC 9525, which replaced RFC 6125 in 2023, says server identity is expressed only in the subject alternative name extension; matching the subject Common Name is no longer valid. Go returns x509: certificate relies on legacy Common Name field, use SANs instead for such a certificate. RFC 9525 changes from RFC 6125 · Go x509 verify
Three rules cause most surprises:
- IP addresses match only IP entries. An address reference must exactly match an
iPAddressentry in the SAN list. A certificate with only DNS names cannot validate a connection made to10.0.4.17. RFC 9525 IP address matching - A wildcard matches one label. The wildcard must be the whole left-most label, and it matches exactly one label.
*.example.comcoversorders.example.combut noteu.orders.example.com, and notexample.comitself. RFC 9525 wildcard rules - Aliases are not followed. The client checks the name it used to open the connection. RFC 9525 builds the reference identifier from the client's inputs or its configuration, and the older rule quoted in RFC 6125's prior-art appendix says it plainly: CNAME canonicalization is not done. A CNAME from
orders.example.internalto a load balancer's own name still needsorders.example.internalin the certificate. RFC 9525 reference identifiers · RFC 6125 Appendix B.1
See which name and which certificate#
First find the exact name the client uses: the host part of the URL or connection string in the service's configuration, after any environment substitution. Then resolve it the way the service does, and see whether it is an alias. dig
dig +short orders.example.internal
dig +short CNAME orders.example.internal
Fetch the certificate the server presents for that name and print its SAN list. Set -servername to the same name, so the server picks the certificate it would pick for your client. -checkhost tests one name against the certificate and prints whether it matches. openssl s_client · openssl x509 -ext and -checkhost
openssl s_client -connect orders.example.internal:443 -servername orders.example.internal \
</dev/null 2>/dev/null > served.txt
openssl x509 -in served.txt -noout -subject -ext subjectAltName
openssl x509 -in served.txt -noout -checkhost orders.example.internal
openssl x509 reads the first certificate in served.txt, which is the leaf. If the client connects by address, use -checkip 10.0.4.17 in place of -checkhost.
curl shows the same comparison in one step. With -v it prints subjectAltName: lines that say which entry matched, or subjectAltName does not match before the error. curl OpenSSL backend
curl -v -o /dev/null https://orders.example.internal/ 2>&1 | grep -E 'subject:|subjectAltName|no alternative'
If the SAN list on the served certificate looks like another service's, the server may have returned its default certificate. Repeat the s_client call with and without -servername and compare: SNI and the default certificate.
Read the result#
| Client uses | SAN list shows | Points to |
|---|---|---|
| An IP address | DNS names only | Connect by a name in the SAN list |
| An alias (CNAME) | Only the target's name | Alias missing from the certificate |
a.b.example.com | *.example.com | Wildcard covers one label |
| The right name | Another service's names | Default or wrong certificate served |
| The right name | The right name | Client checks a different name; read its config |
The last row happens when a library or proxy sets the verify name separately from the connect address, for example a client that connects to an address from service discovery and verifies against a configured name.
A worked example with two outcomes#
This example is illustrative. After a configuration change, checkout fails every call to the orders API.
In the first outcome, the new configuration sets ORDERS_URL=https://10.0.4.17/, an address copied from a dashboard. The certificate's SAN list is DNS:orders.example.internal. Go prints x509: cannot validate certificate for 10.0.4.17 because it doesn't contain any IP SANs. Restoring the name fixes it; the certificate is correct.
In the second outcome, the configuration says https://eu.orders.example.internal/, a new regional name that is a CNAME to the same load balancer. The certificate covers orders.example.internal and *.example.internal. The wildcard covers one label, so eu.orders.example.internal is not covered. The orders team adds the regional name to the certificate, or checkout keeps the name the certificate covers.
Limits#
The s_client check uses your resolver and network path. A service that resolves the name differently, through a split-horizon zone or a stale cache, may reach a different endpoint with a different certificate; check the address the service actually connected to. Split-horizon DNS answers
Some clients let configuration override the name used for verification, and some proxies re-originate TLS with their own name. A clean check from the host does not prove what the service sent. A chain the client cannot complete fails with another message: unable to get local issuer certificate. For other layers, see the hub for runtime dependency failures.
Questions#
Does a certificate for example.com also cover www.example.com?
Only if www.example.com or *.example.com is in its SAN list. A name and its subdomains are separate identities, and the wildcard does not cover the bare domain.
Can I put an IP address in a certificate?
Yes, as an iPAddress entry in the subject alternative name extension, and the address must match exactly, with no network-level matching. Calling by name is easier to keep working when addresses change.
Why does the browser show a different certificate than my service gets?
The browser may send a different SNI name, reach a different endpoint through its own DNS, or go through a proxy. Compare the subject and SAN list from the service's network position with the one the browser shows.