如何获取网站的根CA?

时间:2018-01-01 05:01:20

标签: java ssl truststore

使用以下代码时,不会列出根CA证书

URL destinationURL = new URL("https://google.com");    
HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
        conn.connect();    
Certificate[] certs = conn.getServerCertificates();

如何获取根CA(此处为GeoTrust Global CA)。我应该使用CertPathBuilder吗?

enter image description here

这是我为构建认证路径而找到的示例代码

// 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);

但是如何获得trustedRootCertsintermediateCerts?还是有完全不同的方式?

修改

This问题解答了如何获取受信任的根CA,我认为intermediateCertsconn.getServerCertificates();。应在选择器selector.setCertificate(cert);中设置哪个证书?

2 个答案:

答案 0 :(得分:0)

constructor(props) { super(props); const { params } = props.navigation.state; this.state = { passKey: params.postKey, passUserID: params.userID, passContent: params.postContent, firebaseItems: '', passID: params.expoID, passNameID: params.passNameID, fontLoaded: true }; } 证书应该已经在您的GeoTrust Global CA文件中,用于Java安装,除非它是一个新的,并且您使用的是旧的Java版本。

您的屏幕截图可能是显示证书路径的Web浏览器,如果您信任,可以从那里保存证书。

请注意,Web服务器不应该发送根证书,因此服务器正在做正确的事情。

答案 1 :(得分:-1)

Certificate[] certs = conn.getServerCertificates();

定义了此数组的顺序。您信任的服务器证书现在位于certs[certs.length-1。请注意,它不一定是根证书。如果您需要,可能必须使用CertPathBuilder

相关问题