Android fails converting p12 file's certificates to x509; converts properly using java
<p>I am running the below code to open a p12 file and convert it's certificates to x509.  For the linked p12 file, I get the below exception (have tried on multiple android OS builds).  This conversion works properly when I run similar Java code.  Also, on desktop applications like Internet explorer, and I can convert it's certificates to x509.  </p>

<p>Is due to java.security implementation on Android, or is my code incorrect?</p>

<pre><code>java.security.cert.CertificateException: org.apache.harmony.security.asn1.ASN1Exception: ASN.1 boolean: wrong content at [4]. DER allows only 0x00 or 0xFF values
</code></pre>

<p><a href="https://www.dropbox.com/s/al1415o4kdycmqs/test.p12" rel="nofollow">https://www.dropbox.com/s/al1415o4kdycmqs/test.p12</a></p>

<pre><code>import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class Temp extends Activity 
{
    protected void onCreate(Bundle state)
    {
        super.onCreate(state);

        try
        {
            openAndConvert(new File("/sdcard/test.p12"), "11111111", "mglobetrotter's cryptovision id");
        }
        catch (Exception e)
        {
            Log.e("", Log.getStackTraceString(e));
        }

    }

    private void openAndConvert(File file, String password, String alias) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException
    {
        KeyStore keyStore = openKeyStore(file, new String(password));

        Certificate[] certs = keyStore.getCertificateChain(alias);
        X509Certificate[] certChain = convertCertificates(certs);

    }

    private X509Certificate[] convertCertificates(Certificate[] certsIn) throws CertificateException
    {
        X509Certificate[] certsOut = new X509Certificate[certsIn.length];

        for (int iCnt = 0; iCnt &lt; certsIn.length; iCnt++)
        {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream bais = new ByteArrayInputStream(certsIn[iCnt].getEncoded());
            certsOut[iCnt] =  (X509Certificate) cf.generateCertificate(bais);
        }

        return certsOut;
    }

    private KeyStore openKeyStore(File file, String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException
    {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        FileInputStream ksStream = new FileInputStream(file);
        keyStore.load(ksStream, password.toCharArray());
        ksStream.close();

        return keyStore;
    }
}
</code></pre>
