hvgotcodes
hvgotcodes

Reputation: 120138

How to use an SSL cert in a Java program

I'm trying to query LDAP over an SSL connection. I was sent a certificate and ran:

keytool -import -trustcacerts -alias www.the-domain.com -file the-cert.der -keystore store.jks

I then pointed my Java program at the cert by adding the following to the run configuration in Intellij:

-Djavax.net.ssl.trustStore=/path/to/store.jks

I get a socket closed exception when I try to connect. Did I miss a step?

Here is the standard out, slightly modified to remove IP info:

javax.naming.ServiceUnavailableException: <ip:port>; socket closed
at com.sun.jndi.ldap.Connection.readReply(Connection.java:419)
at com.sun.jndi.ldap.LdapClient.ldapBind(LdapClient.java:340)
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:192)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2694)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:82)
at LDAPTool.main(LDAPTool.java:35)

Upvotes: 1

Views: 2929

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120138

I found out what my issue was. I didn't set up my environment correctly.

env.put(Context.SECURITY_PROTOCOL, "ssl");

when using ssl evidently you need to specify the security protocol, which makes sense....

Upvotes: 1

Related Questions