I have a java server that is trying to connect to an external Ldap server through SSL (as a client in order to perform queries).
I'm having trouble connecting since the certificate they send me upon connecting is trusted only in my local windows Truststore but is not present in java truststore (cacerts).
Is there a way to tell Java to trust any certificate that windows would have trust?
Or, alternatively, is there a way to import all trusted certificates from windows truststore to Java's cacerts?
Any idea would be appreciated.
Solution
On Windows, set the following JVM properties:
I’ve successfully tested this with Java 7, which runs on a 64-bit Windows installation which trusts a self-signed CA.
Configuring the security provider
If the above solution works for you (it should), you may skip this section. Otherwise, check the setup of your Java Cryptography Extension (JCE), which is bundled with modern JDKs. Your JDK installation should have a property file which contains a list of security providers. The location of that file may vary with Java versions; mine is located at
"%JAVA_HOME%\jre\lib\security\java.security"
. Inside that file, locate a set of properties whose names begin withsecurity.provider
. One of those entries should be set tosun.security.mscapi.SunMSCAPI
.Example
To set the properties at runtime, use the following Java code:
Explanation
javax.net.ssl.trustStoreType
On Windows, Java ships with SunMSCAPI, a security provider which is actually a wrapper around the Windows CAPI.
Setting the
javax.net.ssl.trustStoreType
property toWindows-ROOT
instructs Java to refer to the native Windows ROOT keystore for trusted certificates, which includes root CAs. (Similarly, settingjavax.net.ssl.keyStoreType
toWindows-MY
tells Java to refer to the native Windows MY keystore for user-specific certificates and their corresponding keys).javax.net.ssl.trustStore
If the
javax.net.ssl.trustStoreType
property is set toWindows-ROOT
, one would expect that the value ofjavax.net.ssl.trustStore
is ignored, and that it can be set to e. g.NONE
. Some users report that this approach doesn’t work for them though.One common workaround for this issue is to set
javax.net.ssl.trustStore
toNONE
, and then creating a dummy file whose file name isNONE
. If you find yourself affected by this quirk, try settingjavax.net.ssl.trustStore
toNUL
so you won’t have to create any dummy files.No, you have to use the JVM default at
jre/lib/security/cacerts
or set your own truststore:There is no any automatic process, but you could build a program to extract trusted authorities from windows certificate store and import into a truststore configured to use in your application (modifying cacerts is not recommended)