Check a certificate validity against a custom trust list in Java

我与影子孤独终老i 提交于 2019-12-05 11:49:45

Extract from the CAdES signature for each signer the signer's certificate and also the intermediate certificates as a X509Certificate list. Build also a set with all root CA certificates

Then you can use this (slightly adapted) example code to verify and build the certification chain using Java and BouncyCastle. It will return the certification chain if verification is successful

public PKIXCertPathBuilderResult verifyCertificateChain(
     X509Certificate cert, 
     Set<X509Certificate> trustedRootCerts,
     Set<X509Certificate> intermediateCerts) throws GeneralSecurityException {

    // 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
    // certificate itself has to be added to the list 
    intermediateCerts.add(cert); 
    CertStore intermediateCertStore = CertStore.getInstance("Collection",
        new CollectionCertStoreParameters(intermediateCerts), "BC");
    pkixParams.addCertStore(intermediateCertStore);

    // Build and verify the certification chain
    CertPathBuilder builder = CertPathBuilder.getInstance("PKIX", "BC");
    PKIXCertPathBuilderResult result = 
        (PKIXCertPathBuilderResult) builder.build(pkixParams);
    return result;
}

If you do not want to deal with CAdES complexity, I suggest to use SD-DSS open-source project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!