CertificateException - OpenSSLX509CertificateFactory$ParsingException

14,199

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")

Share:
14,199
Navin GV
Author by

Navin GV

Updated on June 05, 2022

Comments

  • Navin GV
    Navin GV almost 2 years

    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)

  • Amitsharma
    Amitsharma over 4 years
    @ Josh how we can add this
  • Josh
    Josh over 4 years
    Hi @Amitsharma , that code is for the TrustManager instance. You can find some ideas, in the answer I posted for this: stackoverflow.com/questions/859111/…