CertificateException - OpenSSLX509CertificateFactory$ParsingException

邮差的信 提交于 2020-04-10 07:14:53

问题


I have the following code in my android project where i am connecting to secured server. I am getting the parse error. [pasted below] .if anyone knows about this exception, please let me know. Thanks in advance.

Code Snippet

CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(new FileInputStream("/storage/emulated/0/cert.p12"));
Certificate ca;
try {
    ca = cf.generateCertificate(caInput);   // error at this line
    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
    caInput.close();
}    

Error

01-15 17:01:00.107: W/System.err(14932): java.security.cert.CertificateException:   com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
01-15 17:01:00.107: W/System.err(14932):    at   com.android.org.conscrypt.OpenSSLX509CertificateFactory.engineGenerateCertificate(OpenSSLX509CertificateFactory.java:272)

01-15 17:01:00.107: W/System.err(14932): at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:195)


回答1:


I found the solution to this same problem. Include the "BC" provider to this line of code, and the error could disappear: getInstance("X.509", "BC")




回答2:


Have two issues 1. Seems to be a wrong file format. For openssl threre is DER and PEM type. Try to convert your cert.p12 to another format ($ x509 -in cert.p12 -inform PEM –out output.crt -outform DER). 2. Perhaps cert.p12 is not a X509 format but pkcs12 is. Read more about those formats and check your files.



来源:https://stackoverflow.com/questions/21138485/certificateexception-opensslx509certificatefactoryparsingexception

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