I finally tested it myself on an Android simulator and got the final answer. Itβs actually not difficult to understand, as soon as I realized that PKCS7 is just a form of storage or, rather, a container for different types of signatures.
In the application
The call returns the first signature in the CERT.RSA file. This is a PKCS7 file that includes an X.509 certificate, and from what I read, it was always only one signature for Android apps.
Signature sig = context.getPackageManager().getPackageInfo(context.getPackageName(), GET_SIGNATURES).signatures[0];
This Signature , obtained from above, can be directly used to create an X.509 working certificate similar to this (taken from here ):
byte[] rawCert = sig.toByteArray(); InputStream certStream = new ByteArrayInputStream(rawCert); CertificateFactory certFactory; X509Certificate x509Cert; try { certFactory = CertificateFactory.getInstance("X509"); x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);
Anywhere
If you have a certificate outside of your own Android application and require the same byte stream provided by the function above, you can do the same with a simple Java program like this:
FileInputStream is = new FileInputStream("CERT.RSA"); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate c = (X509Certificate) cf.generateCertificates(is).toArray()[0]; byte[] rawCert = c.getEncoded();
This code first reads the file, creates a CertificateFactory , and then an important step to isolate the first certificate in the PKCS7 container. And then c.getEncoded() finally gives you the same view as the method above.
Openssl
And last but not least, the openssl command for it (taken from here ):
openssl pkcs7 -inform DER -in CERT.RSA -print_certs -text
This will give you a brief overview of the information contained at the end.
-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----
block. It contains the same data as above. If you parse the contents of this block and decode it with base64, it will give you the same byte array as in the top two examples.