Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Android Certificate

I have a short question: What does this call return exactely?

context.getPackageManager().getPackageInfo(context.getPackageName(), GET_SIGNATURES).signatures[0].toByteArray();

I know it returns the first app certificate for the app which is the CERT.RSA in the META-INF folder, but what exately does it return? Just a byte-array which represents the whole certificate as the file or some other byte-array? I don't really know much about the structure of certificates and the data they contain so I really don't have any clue.

The best answer would be an instruction for openssl with that I get the returned value from the above code line.

like image 460
SkryptX Avatar asked May 11 '16 11:05

SkryptX


People also ask

How do I view my certificates?

Android (v.Click the padlock icon next to the URL. Then click the "Details" link. 2. From here you can see some more information about the certificate and encrypted connection, including the issuing CA and some of the cipher, protocol, and algorithm information.

What are Android security certificates?

These security certificates tell a user if a website or app is trusted by Android and if your information is safe on that platform. When your Android detects a security certificate, it downloads it to your device. If you need to delete these stored certificates, you can.


1 Answers

I finally tested it myself on an android simulator and got the final answer. It's actually not hard to understand once I realized that PKCS7 is just a storage-form or rather a container for various signature-types.

Within the app

The call returns the first signature within the CERT.RSA file. It's a PKCS7 file which embeds the X.509-certificate and from what I've read it's always just 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 generate a working X.509-certificate like 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);

      //do stuff with your certificate
 } catch(Exception ex) {
      //handle exception
 }

Anywhere else

If you have the certificate outside of your own android app and want the same byte-stream, that is 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 the CertificateFactory and then the important step it to isolate the first certificate in the PKCS7-container. And then c.getEncoded() finally gives you the exact same representation 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

It will give you a pretty overview of the information contained and at the end the

 -----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 exact same byte array as in the upper two examples.

like image 124
SkryptX Avatar answered Oct 12 '22 00:10

SkryptX