我在使用 Java SSL 时遇到了困难。问题是我的钥匙有密码。当我使用命令生成密钥openssl req -new -newkey rsa:1024 -nodes -out local.csr -keyout local.key
时,当被要求时A challenge password []:
,我输入了MyKeyPass
。然后我从证书颁发机构获得了证书。然后我将证书导入 KeyStore 和 TrustStore。
我的一个应用程序基于 Jetty 并使用 Maven 运行,另一个使用原始套接字,我没有使用 Maven。
当我将证书与 Jetty 一起使用时,使用以下配置,一切正常:
<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>
但是,对于不使用 Jetty/Maven 的 App,以下配置不起作用:
-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 \
我收到以下错误:
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
语法来指定它,但似乎找不到它。如何指定密钥密码?