As far as I understand in the majority of cases this exception states, that the certificate owner CN (common name) does not match the host name in the url. However in my case they do match, but the exception still raises..
The remote server certificate hierachy is:
- a self signed certificate with
CN=sms.main.ru
- a certificate signed with the first one and
CN=client.sms.main.ru
My java client is launched under apache-tomcat 6 and tries to connect to https://client.sms.main.ru/
and the following exception is thrown:
No name matching client.sms.main.ru found
Both certificates are added to $JAVA_HOME/jre/lib/security/cacerts
via $JAVA_HOME/bin/keytool
as shown in How do you configure Apache/Tomcat to trust internal Certificate Authorities for server-to-server https requests in the answer by unixtippse.
The Java code is quite trivial:
URL url = new URL(strurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Connection", "close");
con.setDoOutput(true);
con.connect();
What am I missing?
Interesting thing is that when I try to access this url with a browser on a Windows PC, it says that the certificate is not trusted, I add it to the browser exception list and it works fine. So it looks like I added these certificates to cacerts
incorrectly, so that java cannot use them. But I can easily find them by alias or by CN with:
$JAVA_HOME/bin/keytool -list -keystore $JAVA_HOME/jre/lib/security/cacerts | less