Learning center

Why does my client get the wrong certificate from a shared endpoint?

A client that sends no SNI name gets the server's default certificate, which often belongs to another service on the same address. Run the same handshake with and without a server name and compare the certificates. If only the nameless handshake returns the wrong one, fix the client so it sends the name.

The symptom#

The error your service logs is usually a hostname mismatch: the certificate it received lists names that belong to someone else, or the load balancer's own name. Sometimes it is a handshake failure instead. Browsers and curl reach the same URL without complaint.

Server Name Indication is a TLS extension in which the client puts the DNS name it wants into the ClientHello. RFC 6066 lets a server use that name to choose a certificate, and it does not permit literal IPv4 or IPv6 addresses as the name. TLS 1.3 says clients should send the extension when applicable, and a server may require it. RFC 6066 server name indication · RFC 8446 mandatory extensions

When the name is missing, a server that hosts several certificates on one address has to pick one without knowing which service you wanted. nginx serves the default server's certificate regardless of the requested name in that case. An Application Load Balancer listener uses its default certificate when a client connects without SNI, or when no certificate in its list matches the name. nginx name-based HTTPS servers · ALB listener certificates

Why clients stop sending SNI#

  • The client connects by IP address. Because RFC 6066 forbids address literals as the name, clients that connect to an address usually send no SNI. OpenSSL's s_client populates SNI from -connect only when it looks like a DNS name. Node.js documents that its servername option must be a host name, not an IP address. openssl s_client · Node.js tls.connect servername
  • The library leaves it to you. Some lower-level TLS APIs send SNI only if your code sets the name: Python's wrap_socket takes a server_hostname, and Node.js tls.connect takes servername. Code that resolves the name itself and then passes the address, or no name, to the TLS layer sends none. Python SSLContext.wrap_socket
  • The client is old. Very old TLS stacks predate the extension. nginx notes SNI may not be used by some old or special clients. nginx SNI compatibility

Two other server behaviours look related. A server that does not recognise the name may abort with an unrecognized_name alert or continue with a default, and a server that requires SNI should answer a ClientHello without it with missing_extension. Either one shows up as a handshake failure instead of a wrong certificate. RFC 6066 unrecognized_name · RFC 8446 mandatory extensions

Compare the handshake with and without a name#

Run both from where the service runs. -noservername suppresses SNI; -servername sets it explicitly. openssl s_client

openssl s_client -connect 203.0.113.40:443 -servername orders.example.com \
  </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName

openssl s_client -connect 203.0.113.40:443 -noservername \
  </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName

Using the address in -connect makes the comparison exact: both runs hit the same endpoint, and only the SNI differs. If the second run prints a different subject or a SAN list without your name, the endpoint serves a default certificate to nameless clients. If both print the right certificate, SNI is not the problem; go back to the hostname mismatch checks.

Next, find out whether your client actually sends a name. Read the code or configuration that builds the connection: is the URL or connection string a name or an address, and does the TLS layer receive that name? A packet capture of the ClientHello also shows it, but needs capture rights on the host and handles production traffic, so prefer the configuration read.

On an Application Load Balancer, list the listener's certificates and see which one is the default. The default certificate also appears in the list with IsDefault set to true. describe-listener-certificates

aws elbv2 describe-listener-certificates \
  --profile PROFILE --region REGION --listener-arn LISTENER_ARN \
  --query 'Certificates[].{arn:CertificateArn,default:IsDefault}' \
  --output table --no-cli-pager

Read the result#

With nameWithout namePoints to
Right certificateAnother certificateClient sends no SNI
Right certificateHandshake alertServer requires SNI; client sends none
Wrong certificateWrong certificateNot SNI: name not configured on the server
Right certificateRight certificateNot SNI: check name matching

A worked example with two outcomes#

This example is illustrative. An Application Load Balancer at 203.0.113.40 serves orders.example.com and payments.example.com, with the payments certificate as its default. checkout logs a hostname mismatch calling orders: the certificate it received is valid for payments.example.com.

In the first outcome, the s_client run with -servername orders.example.com returns the orders certificate, and the run with -noservername returns the payments certificate. checkout's new HTTP client resolves the name itself, connects to the address and wraps the socket without passing the host name, so it sends no SNI. Passing the name to the TLS layer fixes it. The load balancer is behaving as documented.

In the second outcome, both runs return the payments certificate. The listener's certificate list has no certificate for orders.example.com: the orders certificate was removed during a renewal. The client is fine, and the orders team restores the certificate on the listener.

Limits#

s_client tells you what the endpoint does for each kind of ClientHello. It does not tell you what your service sends; that comes from its code, its configuration or a capture. Behind a CDN or a chain of proxies, the hop you test may not be the one that failed, so test each hop the service talks to directly.

A server can also fail the handshake when the client's TLS version or cipher list does not fit, which looks like the missing-SNI alert case: protocol version and handshake failure alerts. For other layers, see the hub for runtime dependency failures.

Questions#

Does curl send SNI when I connect by IP address?

Not for an address in the URL: curl sets SNI only when the URL's host is a DNS name. To test a specific address while sending the right name, use --connect-to, which changes where curl connects and keeps the URL's host name for SNI and certificate checks. curl --connect-to · curl TLS peer setup

Why do browsers reach the endpoint fine?

Browsers connect by name and send it as SNI, so the server picks the matching certificate. Your service gets the default only when it leaves the name out.

Can the server avoid this?

A certificate that covers every name on the address makes the default correct for all of them, and some servers can refuse nameless handshakes. Both are changes on the dependency side; the client fix is to send the name.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading