这个问题在这里已有答案:
使用以下代码时,将不会列出根CA证书
URL destinationURL = new URL("https://google.com");
HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
conn.connect();
Certificate[] certs = conn.getServerCertificates();
如何获得根CA(GeoTrust Global CA)。我应该使用CertPathBuilder
吗?
这是我为构建认证路径而找到的示例代码
// Create the selector that specifies the starting certificate
X509CertSelector selector = new X509CertSelector();
selector.setCertificate(cert);
// Create the trust anchors (set of root CA certificates)
Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
for (X509Certificate trustedRootCert : trustedRootCerts) {
trustAnchors.add(new TrustAnchor(trustedRootCert, null));
}
// Configure the PKIX certificate builder algorithm parameters
PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(
trustAnchors, selector);
// Disable CRL checks (this is done manually as additional step)
pkixParams.setRevocationEnabled(false);
// Specify a list of intermediate certificates
CertStore intermediateCertStore = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(intermediateCerts));
pkixParams.addCertStore(intermediateCertStore);
// Build and verify the certification chain
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
PKIXCertPathBuilderResult result = (PKIXCertPathBuilderResult) builder
.build(pkixParams);
但如何获得trustedRootCerts
和intermediateCerts
?还是有完全不同的方式?
编辑
This问题回答如何获得受信任的根CA,我想intermediateCerts
是conn.getServerCertificates();
。应该在选择器selector.setCertificate(cert);
中设置什么证书?
GeoTrust Global CA
证书应该已经在您的cacerts
文件中用于Java安装,除非它是一个新的并且您使用的是旧的Java版本。
您的屏幕截图看起来可能是显示证书路径的Web浏览器,如果您信任它,您可以从那里保存证书。
请注意,Web服务器不应该发送根证书,因此服务器正在做正确的事情。
Certificate[] certs = conn.getServerCertificates();
定义了此数组的顺序。您信任的服务器证书现在位于certs[certs.length-1
。请注意,它不一定是根证书。如果你想要,你可能必须使用CertPathBuilder
。