I'm having difficulty with Java SSL. The problem is that my key has a password. When I generated the key using the command openssl req -new -newkey rsa:1024 -nodes -out local.csr -keyout local.key, when prompted A challenge password []:, I entered MyKeyPass. Then I received a certificate from a certification authority. Then I imported the certificate into KeyStore and TrustStore.
One of my applications is based on Jetty and works with Maven, the other uses raw sockets, and I do not use Maven for it.
When I use a certificate with Jetty using the following configuration, everything works fine:
<connector implementation="org.mortbay.jetty.security.SslSelectChannelConnector">
<port>443</port>
<maxIdleTime>30000</maxIdleTime>
<keystore>keys/domain.jks</keystore>
<password>KeyStorePass</password>
<keyPassword>MyKeyPass</keyPassword>
<truststore>keys/truststore_domain.jks</truststore>
<trustPassword>TrustStorePass</trustPassword>
</connector>
However, for an application not using Jetty / Maven, the following configuration does not work:
-Djavax.net.ssl.keyStore=./keys/domain.jks \
-Djavax.net.ssl.keyStorePassword=KeyStorePass \
-Djavax.net.ssl.trustStore=./keys/truststore_domain.jks \
-Djavax.net.ssl.trustStorePassword=TrustStorePass \
I get the following error:
Caused by: java.security.UnrecoverableKeyException: Cannot recover key
at sun.security.provider.KeyProtector.recover(KeyProtector.java:328)
at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:138)
at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:55)
at java.security.KeyStore.getKey(KeyStore.java:792)
at sun.security.ssl.SunX509KeyManagerImpl.<init>(SunX509KeyManagerImpl.java:131)
at sun.security.ssl.KeyManagerFactoryImpl$SunX509.engineInit(KeyManagerFactoryImpl.java:68)
at javax.net.ssl.KeyManagerFactory.init(KeyManagerFactory.java:259)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager(SSLContextImpl.java:621)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.<init>(SSLContextImpl.java:486)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at java.security.Provider$Service.newInstance(Provider.java:1238)
... 12 more
, KeyPassword (MyKeyPass). , , -Djavax.net.ssl, . ?