gpt4 book ai didi

java - 在输入证书之前检查 CA 的证书?

转载 作者:行者123 更新时间:2023-12-04 05:48:29 26 4
gpt4 key购买 nike

我正在使用以下代码将客户端证书插入到我的 servertruststore 中

  FileInputStream fileInputStream = new FileInputStream( "c:/server.jks" );
keyStore.load( fileInputStream, "keystore".toCharArray() );
fileInputStream.close();
keyStore.setCertificateEntry( alias, new X509Certificate( trustedCertificate ) );

FileOutputStream fileOutputStream = new FileOutputStream("c:/server.jks" );
keyStore.store( fileOutputStream, "keystore".toCharArray() );
fileOutputStream.close();

现在我看到该证书已输入到我的信任库中,但是我的信任库中不存在签署客户端证书的 CA 证书。所以我想知道在将证书输入 keystore 之前,有什么方法可以检查CA的证书是否可用?

最佳答案

我猜您需要做的是验证证书是由根授权机构颁发的还是自签名的。我假设您使用的是默认的 java keystore ,即 cacerts。
我还没有测试过代码,但我认为这可能是您问题的解决方案:

  • 从以下链接中获取和修改的代码:

  • How can I get a list of trusted root certificates in Java?
            String filename = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar);
    Set<X509Certificate> additionalCerts = new HashSet<X509Certificate>();
    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
    Iterator it = params.getTrustAnchors().iterator();
    while( it.hasNext() ) {
    TrustAnchor ta = (TrustAnchor)it.next();
    // Get certificate
    X509Certificate cert = ta.getTrustedCert();
    additionalCerts.add(cert);
    }
  • 然后您可以使用以下代码将客户端证书和包含所有根 CA 的 Set 传递给以下代码的 verifyCertificate(X509Certificate cert, Set additionalCerts) 方法:

  • http://www.nakov.com/blog/2009/12/01/x509-certificate-validation-in-java-build-and-verify-chain-and-verify-clr-with-bouncy-castle/

    关于java - 在输入证书之前检查 CA 的证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384669/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com