首页 > 解决方案 > java api从同一个pem文件中读取证书和CRL

问题描述

我的证书和 CRL 在同一个 PEM 文件中,如下所示

-----开始证书-----

-----结束证书-----

-----开始 X509 CRL-----

-----END X509 CRL-----

我正在尝试读取如下数据

 try
  { 
    ByteArrayInputStream temp= new ByteArrayInputStream( cacert_file_data );

        while(temp.available()>0 )
        {
            x509_cacert = (java.security.cert.X509Certificate)cf.generateCertificate(temp);

            if (x509_cacert == null) { return ; }   // paranoid

            cert_list.add( x509_cacert );
        }

    }
    catch (CertificateException e)
    {some code}

如果是 CRL,它会抛出 CertificateException ,有没有办法可以避免这个异常并优雅地读取 CRL,没有来自同一个文件的异常。

标签: ssl-certificatex509certificate

解决方案


    ByteArrayInputStream bis= new ByteArrayInputStream( cacert_file_data );
    int count = bis.available();
    int pos = 0;
    String cacert_str;

    try
    {
        while(bis.available()>0 )
        {

            x509_cacert = (java.security.cert.X509Certificate)cf.generateCertificate(bis);

            if (x509_cacert == null) { return ; }   // paranoid

            cert_list.add( x509_cacert );
            pos = count - bis.available();
            byte[] bytes = new byte[bis.available()];
            bis.read(bytes, 0, bis.available());
            bis.reset();
            bis.skip(pos);
            cacert_str = new String( bytes );
            if(!cacert_str.contains("-----BEGIN CERTIFICATE-----"))
            {
               break;
            }
        }
    }

推荐阅读