I ran into this problem while writing a standalone java tool that programatically opens a HttpsUrlConnection to a server running an ISAPI component. The development environment server had an expired ssl certificate due to which my code would throw an InvalidCertificateException exception while making the connection.
The following 2 steps helped me get around the problem.
1. Import the expired certificate to JDK keystore. This great post explains how to achieve this.
I have copied the whole post below just in case the above url is moved
2. Extend the javax.net.ssl.HostNameVerifier class and override the verify() method to always return true.
next force your HttpsUrlConnection to use your verifier by connection.setHostNameVerifier(myverifier)
Sample code:
HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection();
/** The following is a patch is to bypass ssl authentication
* issues due to expired certificates on the server
* Also ensure u have imported the expired server certificate
* into your keystore using the keytool
*/
urlConn.setHostnameVerifier(new Verifier());
urlConn.setUseCaches(false);
urlConn.setDoInput(true);
urlConn.setAllowUserInteraction(true);
//Read the response
BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
And you are ready to go!!
Bypass invalid ssl cert by Dirk Mccormik
For posterity, here's how to ignore/bypass an expired/invalid
certificate without changing your java code.
In order to add a certificate from a website to your JVM do the
following:
1. In Internet Explorer, go to the website. It will give you a Security
Alert dialog and say what the problem with the certificate is. Click
"View Certificate". Click the Details tab. Click copy to file and
follow the Certificate Export Wizard. Save using the format "DER
encoded binary X.509 (.CER)".
2. Repeat the process, and this time after clicking "View Certificate"
click the "Certification Path" tab. If there are any other certificates
in the certification path, export these to file as well.
3. Go to the directory where you saved the certificate, and run keytool
to import it:
C:\tmp>"C:\j2sdk1.4.2_10\bin\keytool.exe" -import -keystore
"C:\j2sdk1.4.2_10\jre\lib\security\cacerts" -storepass changeit -alias
verisignTestCert -file verisignTest.cer
where verisignTest.cer is the name of the certificate file you exported
from IE.
4. Repeat this process for each certificate you exported in IE.