Submitted by gunnar on
When your Java program attempts to connect to a server that has an invalid or self signed certificate, such as an application server in a development environment, you may get the following exception:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
To make your Java runtime environment trust the certificate, you need to import it into the JRE certificate store.
Step 1 - Get the certificate into your browser store
Browse to your application server using SSL. Your browser will tell you that the certificate isn't trusted and allow you to trust it, thereby placing it in the browser certificate store.
Step 2 - Export the certificate to a binary file
Your browser will have some kind of certificate manager that allows you to export or back up specific certificates to binary files. In Firefox that would be under Preferences / Advanced / Encryption / Servers. Find the certificate presented by the server and export it as a binary DER file.
Step 3 - Import the certificate into the Java Store
Make sure you have write access to your JRE and use the keytool utility to import it:
keytool -import -alias alias -keystore path-to-jre/lib/security/cacerts -file path-to-certificate-file
Example:
keytool -import -alias sunas -keystore /opt/jdk1.6/jre/lib/security/cacerts -file /home/gugrim/tmp/sunas.der
You will be prompted for the keystore password, which is by default changeit.
Also, when you connect to the server make sure you use the same name as the one set as the Subject in the certificate. You may need to add it to your host file if the server isn't reachable using this name, which may be the case for a developer server.
That's it!
Comments
Anonymous replied on Permalink
Nice post really helpful
thanks for your post it really helpful
Anonymous replied on Permalink
great great great
Really great post and thanks .you almost save my life.
Anonymous replied on Permalink
Thanks
Thanks a lot Boss.........
A kedar replied on Permalink
Nice precise steps
I would like to add that in windows the folder permissions of the path-to-jre/lib/security needs to have read/write permissions. Apparently just changing permissions of cacert file is not sufficient
Mark replied on Permalink
Very helpful
Thanks for the helpful information - was stuck on where to place the trusted certificate but following these steps it worked :)
Justin.caseof replied on Permalink
THANKS
Your covered all important steps nice and clearly in a brief way! Thanks a lot, it helped me a lot :)
Arun replied on Permalink
Thanks
Hey Its so simple, Thanks..:-)
kieutiencuong replied on Permalink
Very helpful and clear
Thanks a lot. It is really helpful and very clear.
Add new comment