I have a code like this:
// configure the SSLContext with a TrustManager SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); SSLContext.setDefault(ctx); URL url = new URL(urlString); // https://abc.myhost.com HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { System.out.println("verify:" + arg0); return true; } }); System.out.println("HTTP status: " + conn.getResponseCode()); Certificate[] certs = conn.getServerCertificates(); int c=0; for (Certificate cert : certs){ String t = cert.getType(); System.out.println(String.format("\ncert[%d]: %s",c,t)); c++; if (pi.verbose) { System.out.println(cert); } else if (cert instanceof X509Certificate) { X509Certificate x509cert = (X509Certificate) cert; System.out.println(x509cert.getSubjectDN().getName()); } }
By running this code on a specific website, in Java 6, I get a different certificate than I get for Java 7. Say the host name is abc.myhost.com.
on Java6 I get:
cert[0]: X.509 CN=example.com,OU=Secure Link SSL Pro,O=Company Name Here, STREET=2001 Space Odyssey Dr,L=Weirton,ST=Wv,2.5.4.17=
on Java7 I get:
cert[0]: X.509 CN=abc.myhost.com,OU=Secure Link SSL Pro,O=Company Name Here, STREET=2001 Space Odyssey Dr,L=Weirton,ST=Wv,2.5.4.17=
If I print out valid dates, they are also different. Like serial numbers. These are different certificates.
In Java 7, this looks right; In Java 6, I have a disagreement between the host name and CN. The certificate does not look right.
It is possible that this server is located behind the proxy server. It is also possible that the owner of this server (the partner in the project I'm working on) has recently changed the certificate. There may be two certificates: one on the proxy server and one on the server behind the proxy server. I study them.
I have a question: why don't I get the same results in Java7 as in Java6? Has Java replaced anything in HttpsURLConnection.getServerCertificates() ?
For the curious, this is just a diagnostic job. The real mistake:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching abc.myhost.com found.
The problem in this case is usually the disagreement between the host name and CN in the certificate. I checked the odds, but only in Java 6. I would like to understand why Java6 and Java7 are different.
EDIT: Python 2.7.1 script returns the same certificate as Java6. SSLConnection.get_peer_cert() shows me a certificate with an inconsistent CN.