ssl-certificate - 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,没有来自同一个文件的异常。
解决方案
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;
}
}
}
推荐阅读
- ssh - 为 confd netconf ssh 连接设置 ipv4 和 ipv6
- javascript - 停止离子变化激发,直到从存储中加载值
- html - 如何使用 hr 获取他父 div 的全宽?
- c# - 做项目学习
- vb.net - 如何保持增加相同的值?
- neo4j - 如何解决 neo4j 身份验证错误?
- xamarin - 使用 IOS 设备中的 ScrollView 控件滚动不流畅
- django - 我可以在 Heroku 的生产环境中使用 sqlite3 进行简单的 django 博客应用程序吗?
- ios - 如何以编程方式更改名称以区分 Xcode 可视化调试器中的 UIView?
- android - Firebase中以下代码的功能或用处是什么?