【发布时间】:2016-08-05 06:03:19
【问题描述】:
前提:我有一个证书,我想验证系统“信任”这个证书(由 Java/操作系统的受信任根 CA 签名)
我找到了一些不同的解决方案来解决这个问题。
选项 1:
使用 SSL 类来获得信任。
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init((KeyStore) null);
for (TrustManager trustManager : tmfactory.getTrustManagers()) {
if (trustManager instanceof X509TrustManager) {
try {
((X509TrustManager) trustManager).checkClientTrusted(new X509Certificate[] {new JcaX509CertificateConverter().getCertificate(holder)}, "RSA");
System.out.println("This certificate is trusted by a Root CA");
} catch (CertificateException e) {
e.printStackTrace();
}
}
}
由于这种方法严重依赖 SSL 类(当前项目不需要),我们正在寻找替代方法。
选项 2:
将 Java 的 cacertsfile 加载到密钥库中,并根据我的证书检查每个“最受信任”证书是否相等。
String filename = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar);
FileInputStream is = new FileInputStream(filename);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "changeit";
keystore.load(is, password.toCharArray());
// This class retrieves the most-trusted CAs from the keystore
PKIXParameters params = new PKIXParameters(keystore);
// Get the set of trust anchors, which contain the most-trusted CA certificates
Set<X509Certificate> rootCertificates = params.getTrustAnchors().parallelStream().map(TrustAnchor::getTrustedCert).collect(Collectors.toSet());
return rootCertificates.contains(holderX509);
这种方法的问题在于它需要密码来验证 JKS 编码文件的完整性。虽然 SSL 似乎没有(或者更确切地说使用System.getProperty("javax.net.ssl.trustStorePassword"),这又与 SSL 密切相关。
问题:是否存在介于手动从文件加载证书和纯 SSL 之间的解决方案?我觉得好像应该有一些类我可以调用来简单地验证证书的系统信任,而不必跳过几个环节。
【问题讨论】:
-
这里使用 TrustManager 没有任何问题。这就是它的用途。克服你的厌恶。
标签: java security x509certificate pki truststore