![](/img/trans.png)
[英]SSL client (Java) is not sending a certificate back to the server in two-way SSL handshake
[英]My SSL client (Java) isn't sending a certificate back to the server in two-way SSL handshake
在 Windows 7 上运行的 Java 1.7 应用程序中,我正在尝试使用服务器执行 2 向 SSL(智能卡令牌通过 openSC 提供我的客户端证书)。 服务器的证书得到客户端的验证就好了,但客户端不响应服务器的证书请求。 我相信这是因为客户端无法从我的证书到服务器请求的证书之一建立链(即使存在这样的链)。
这是服务器证书请求和客户端空响应的 SSL 调试:
*** CertificateRequest
Cert Types: RSA, DSS, ECDSA
Cert Authorities:
<CN=c4isuite-SDNI-DC02-CA, DC=c4isuite, DC=local>
<CN=DoD Root CA 2, OU=PKI, OU=DoD, O=U.S. Government, C=US>
...
*** ServerHelloDone
*** Certificate chain
***
我的客户证书如下:
found key for : Certificate for PIV Authentication
chain [0] = [
[
Version: V3
Subject: CN=<...>, OU=CONTRACTOR, OU=PKI, OU=DoD, O=U.S. Government, C=US
Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5
Key: Sun RSA public key, 2048 bits
Issuer: CN=DOD CA-30, OU=PKI, OU=DoD, O=U.S. Government, C=US
SerialNumber: [ 05bf13]
通过密钥工具,我还安装在信任库(java cacerts 文件)中,我的证书颁发者 DOD CA-30 与服务器请求的内容之间的链接应该是什么,DoD Root CA 2。
从 SSL 调试:
adding as trusted cert:
Subject: CN=DOD CA-30, OU=PKI, OU=DoD, O=U.S. Government, C=US
Issuer: CN=DoD Root CA 2, OU=PKI, OU=DoD, O=U.S. Government, C=US
Algorithm: RSA; Serial number: 0x1b5
Valid from Thu Sep 08 10:59:24 CDT 2011 until Fri Sep 08 10:59:24 CDT 2017
adding as trusted cert:
Subject: CN=DoD Root CA 2, OU=PKI, OU=DoD, O=U.S. Government, C=US
Issuer: CN=DoD Root CA 2, OU=PKI, OU=DoD, O=U.S. Government, C=US
Algorithm: RSA; Serial number: 0x5
Valid from Mon Dec 13 09:00:10 CST 2004 until Wed Dec 05 09:00:10 CST 2029
那么问题来了,为什么客户端不能为响应制作证书链呢? 这是相关的代码:
// Create the keyStore from the SmartCard certs
Provider provider = new sun.security.pkcs11.SunPKCS11(configName);
Security.addProvider(provider);
keyStore = KeyStore.getInstance("PKCS11", "SunPKCS11-SCR3310test");
char[] pin = PIN.toCharArray();
keyStore.load(null, pin);
// Init the trustmanager
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
// Create the client key manager
LOG.info("Installing keystore with pin");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(clientKeyStore, clientKeyPassword.toCharArray());
sslContext.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);
// Init SSL context
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
if (connection instanceof HttpsURLConnection) {
LOG.info("Connection is HTTPS");
((HttpsURLConnection) connection).setSSLSocketFactory(socketFactory);
}
// Send the request.
connection.connect();
InputStreamReader in = new InputStreamReader((InputStream) connection.getContent());
...
我得到的错误是服务器返回 403。很可能是因为客户端没有向它发送客户端证书。
即使看起来您只是将服务器发送的 CA 列表的一部分复制到这个问题中,我还是假设CN=DOD CA-30, OU=PKI, OU=DoD, O=US Government, C=US
不在此列表中。
链中似乎缺少的是此证书(您稍后会提到):
Subject: CN=DOD CA-30, OU=PKI, OU=DoD, O=U.S. Government, C=US
Issuer: CN=DoD Root CA 2, OU=PKI, OU=DoD, O=U.S. Government, C=US
Algorithm: RSA; Serial number: 0x1b5
Valid from Thu Sep 08 10:59:24 CDT 2011 until Fri Sep 08 10:59:24 CDT 2017
将证书导入客户端的信任库对客户端发送的证书绝对没有影响。 客户端证书(及其私钥)需要在客户端密钥库中设置。 此外,如果您想发送客户端证书链(如果服务器未在其列表中提供此中间 CA 证书,则此处是必需的),您需要将完整链与该证书条目相关联。 将其他证书放入密钥库是不够的。
要解决此问题,您应该使用客户端证书链配置您的密钥库条目。 这可以按照这个答案中的描述来完成。 但是,这是一个通过 PKCS#11 访问的硬件令牌这一事实可能会使这变得更加复杂(也许该卡提供了另一个证书管理工具,可能独立于 Java)。
由于我知道我需要使用哪个证书来对服务器进行身份验证,因此我可以通过扩展 X509ExtendedKeyManager 并覆盖chooseClientAlias() 方法来强制客户端发送该特定证书以始终返回该证书的别名。 代码:
public class MyX509KeyManager extends X509ExtendedKeyManager
{
X509KeyManager defaultKeyManager;
public MyX509KeyManager(X509KeyManager inKeyManager) {
defaultKeyManager = inKeyManager;
}
public String chooseEngineClientAlias(String[] keyType,
Principal[] issuers, SSLEngine engine) {
return "<Alias of my cert>";
}
@Override
public String chooseClientAlias(String[] strings, Principal[] prncpls, Socket socket) {
return "<Alias of my cert>";
}
@Override
public String[] getClientAliases(String string, Principal[] prncpls) {
return defaultKeyManager.getClientAliases(string, prncpls);
}
@Override
public String[] getServerAliases(String string, Principal[] prncpls) {
return defaultKeyManager.getServerAliases(string, prncpls);
}
...
正如你所看到的,我采用了一个 defaultKeyManager,除了我想要覆盖的内容之外,我对任何事情都遵循它。 然后,要在您的 sslContext 中使用它,请执行以下操作:
// clientKeyStore is initialized elsewhere from the SmartCard
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(clientKeyStore, clientKeyPassword.toCharArray());
MyX509KeyManager customKeyManager = new MyX509KeyManager((X509KeyManager) keyManagerFactory.getKeyManagers()[0]);
sslContext.init(new KeyManager[] {customKeyManager}, tmf.getTrustManagers(), null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.