“Hostname not verified” on Android: causes and fix

The app reports javax.net.ssl.SSLPeerUnverifiedException: Hostname … not verified, although the certificate is valid. Here Android is not checking the chain but whether the name the app uses for the server is in the certificate. Usually an entry is missing from the subject alternative names.

What the error looks like

javax.net.ssl.SSLPeerUnverifiedException: Hostname 192.168.10.20 not verified:
    certificate: sha256/3Sx1...=
    DN: CN=nas.example.lan
    subjectAltNames: []

The message already contains almost the whole diagnosis: the name the app called, the common name and the list of alternative names. If subjectAltNames is empty or does not contain the name called, the check fails.

The most common causes

  1. Certificate without SAN. Android ignores the common name (CN) when checking the name, even if the certificate has no SANs at all. Older internal certificates with only a CN therefore don't work in apps.
  2. Access by IP address. If the certificate only contains nas.example.lan but the app calls https://192.168.10.20, the certificate needs a SAN entry of type IP.
  3. Wrong name. Typical: the certificate is for www.example.com, the app uses example.com. Or the server has a different name internally than externally.
  4. Wildcard doesn't match. *.example.com covers cloud.example.com, but neither example.com nor a.b.example.com.
  5. The server sends a different certificate. With several websites on one IP, the default virtual host answers when the name doesn't match or a reverse proxy doesn't pass the name on.

Check

The SSL check shows the SANs of the certificate sent. You can open a file in the certificate converter on the “Inspect” tab. On the command line:

openssl s_client -connect 192.168.10.20:443 -servername nas.example.lan </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -ext subjectAltName

Fix

Issue a new certificate with every name the app uses to reach the server. In the CSR generator you enter DNS names and IP addresses; the type is detected automatically:

nas.example.lan
nas
192.168.10.20

Public CAs don't issue certificates for private IP addresses and internal names. For that you need an internal CA. Its root has to be installed on the device, and the app has to trust user CAs, see certificate not trusted in Android apps. The simpler alternative is a real DNS name with a Let's Encrypt certificate via the DNS challenge, even if the service is only reachable internally. The ACME helper shows how.

What not to do

A custom HostnameVerifier that always returns true is offered as a “fix” in many forums. With it the app accepts any certificate from any server, including an attacker's. Google Play flags such apps.

FAQ

It works in the browser. Why?

Chrome shows a warning for a name mismatch that can be clicked away. Apps usually don't offer that. Often the browser also uses a different name than the app.

Is it enough to put the name in the CN?

No. Android only checks the SANs. The CN is ignored.

Can a Let's Encrypt certificate contain an IP address?

For public IP addresses Let's Encrypt now offers this with short-lived certificates, but not for private addresses like 192.168.x.x. For those you need an internal CA.

Sources and documentation