title: Load SSL certificate from file in Android
body: <p>I'm trying to load a self signed SSL certificate from a file.</p>
  
  <p>According to this
  <a href="https://developer.android.com/reference/java/security/cert/CertificateFactory" rel="nofollow noreferrer">https://developer.android.com/reference/java/security/cert/CertificateFactory</a>
  it should work like this:</p>
  
  <pre><code>private void LoadCert()
  {
      try
      {
          AssetFileDescriptor assetFileDescriptor = this.getAssets().openFd("certbase64.cer");
          FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();
  
          FileInputStream fis = new FileInputStream(fileDescriptor);
          BufferedInputStream bis = new BufferedInputStream(fis);
  
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
  
          while (bis.available() &gt; 0)
          {
              Certificate cert = cf.generateCertificate(bis);
              System.out.println(cert.toString());
          }
      }
      catch (Exception ex)
      {
          Log.d("Error", ex.getMessage());
      }
  }
  </code></pre>
  
  <p>However I get this error:</p>
  
  <blockquote>
    <p>D/Error: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG</p>
  </blockquote>
  
  <p>The file:</p>
  
  <pre><code>-----BEGIN CERTIFICATE-----
  MIICzjCCAbagAwIBAgIQRYyJrMpTfrlNFpAM8oS1VDANBgkqhkiG9w0BAQsFADAQ
  MQ4wDAYDVQQDEwVTZWZmYTAeFw0xOTA2MjUyMDM5MTdaFw0yMDA2MjUwMDAwMDBa
  MBAxDjAMBgNVBAMTBVNlZmZhMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
  AQEA+WFZChTqIIqamM0v+EeaZGhxopmkbmbCfL73dhBKZOOPq2ALwGWO9+ABjVDZ
  Z5igcmhuYa29BymB9oMeyOWiyD1p/Wo1RUENCf5zTxOXdCCSF/up/ahj3q3M4afV
  RNg8/4ld3r3m2u6XbOr2+y3p//9zgmilS5uswefM+p058uxBX4eoeDj7JhCNUuy5
  xqWmvJAdaPL3/W/CjDva8c0HQ8GenMmi/JYwtjuZTYcKCk2Oxha5aCn0zu0DiwaY
  s5/+x0/iyhg9kMXKjEDDTwu41wL1G90uKN7H+81uf+eEf0qHjwb+SMzugDiWbwcX
  NZw4cL2fbi9z3QEy2k9Yov6NGQIDAQABoyQwIjALBgNVHQ8EBAMCBDAwEwYDVR0l
  BAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQELBQADggEBABX/uVlO1dtFXj/bRDEz
  S+gAc+5Bl4GggzRA5sxR1+FqS37AxkiHhsBweuxK5bx0wZBykzHhjmq24vnBRmt1
  ucfffpJ2rJjlsoya6GEL/qTvj3gxvAH023M+V/Sdvg8K+reX/NGrRsVb19McDjbw
  JqGyIDO4f5uOzMmky5zRoo8SbYTFZwPnRTL4oI99C7YWMGWOGPF/HvjqC2D+xRUy
  cb8RWtj1ms6uwu96CQgU4i26a7xwVDP4mUAnOc7gdpdtQoDHROvz2LO04unEtfRd
  jWO79/7nICA0miHo6QBVVxq5s6JOIyd1J5a2d8p+HZN4fDcEV6vB9VUU67WvjYXy
  EIU=
  -----END CERTIFICATE-----
  </code></pre>
  
  <p>Any idea what the problem might be?</p>