While accessing our own website in Java code, an exception is thrown:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching en.greatfire.org found.
However, when accessing it in a browser or using curl, there’s no problem.
Any idea why this could be? If there’s any problem with our certificates but browsers are somehow more lenient we’d like to fix it.
Not sure if it’s related, we have separate certificates for greatfire.org and en.greatfire.org.
Java code that throws the above exception:
URL url = new URL("https://en.greatfire.org");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
System.out.println("Response code: " + conn.getResponseCode());
for(Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
for(String headerValue : header.getValue()) {
System.out.println(header.getKey() + ": " + headerValue);
}
}
RFC 2818 (the HTTPS specification) says:
Essentially, if there’s in no SAN extension at all, it should use the CN in the Subject DN, otherwise, it should only use the SAN entries. (The more recent RFC 6125 also goes along these lines.)
It sounds like your certificate has one or more SAN entries, but none of them are valid for the host name you’re after (which could be in the CN only).
Java is quite strict on this, including on the type of SAN entry (e.g. for IP addresses), but some browsers are more lenient. I’d suggest fixing the certificate by putting all the host names (or IP addresses) you want to use in Subject Alternative Names.
EDIT:
This being said, you have a couple of related problems.
The certificate you serve for
en.greatfire.orghas SAN entries for bothen.greatfire.organdwww.en.greatfire.org, which is fine and should work.However, if you’re using a client that doesn’t support the SSL/TLS Server Name Indication extension (e.g.
openssl s_client -showcerts -connect en.greatfire.org:443, or Java 6 for that matter), you get a certificate forgreatfire.organdwww.greatfire.org.This is a double problem because:
greatfire.orgdoesn’t even resolve to that same machine.It looks like one of your certificates is fine, but clients that don’t support SNI won’t get to see it.
If you want to avoid this problem, get a certificate valid for all 4 names (4 SAN entries), and point
greatfire.orgto the right IP address too.