【发布时间】:2015-06-21 23:19:16
【问题描述】:
我正在为客户端-服务器应用程序开发 Android 客户端。我需要与具有相互身份验证的服务器的 TLS 会话。我在服务器上使用 Netty。我的客户代码:
// private key
File client_tls_key = new File("/sdcard/GreatParents/tls/client_key.pkcs8");
KeyManagerFactory kmf = null;
KeyStore ks;
try {
ks = KeyStore.getInstance("BKS");
ks.load(new FileInputStream("/sdcard/GreatParents/tls/client_ks.bks"), "changeit".toCharArray());
String kmf_type = KeyManagerFactory.getDefaultAlgorithm();
kmf = KeyManagerFactory.getInstance(kmf_type);
kmf.init(ks, "changeit".toCharArray());
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | UnrecoverableKeyException e) {
e.printStackTrace();
}
TrustManagerFactory tmf = null;
KeyStore ts;
try {
ts = KeyStore.getInstance("BKS");
ts.load(new FileInputStream("/sdcard/GreatParents/tls/client_ts.bks"), "changeit".toCharArray());
String tmf_type = TrustManagerFactory.getDefaultAlgorithm();
tmf = TrustManagerFactory.getInstance(tmf_type);
tmf.init(ts);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
e.printStackTrace();
}
if (tmf != null) {
SslContext sslCtx = null;
try {
sslCtx = SslContext.newClientContext(SslProvider.JDK,null,tmf,null,client_tls_key,keypass,kmf,null,IdentityCipherSuiteFilter.INSTANCE,(ApplicationProtocolConfig) null,0,0);
} catch (SSLException e) {
e.printStackTrace();
logger.error("TLS Session not initialized");
return;
}
}
服务器的证书存储在文件 client_ts.bks 中。客户端的证书存储在文件 client_ks.bks 中。
我遇到了异常:
java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
我在stacktrace方法中找到:
io.netty.handler.ssl.JdkSslContext.buildKeyManagerFactory
带代码:
KeyStore ks = KeyStore.getInstance("JKS");
Netty 强制创建 JKS-keystores,而不是使用我的 BKS-keystores,对吧?!如果我是对的,Netty 在 TLS 部分与 Android 不兼容。
【问题讨论】: