thomascannon.net

Get Certificate from Android Application

This is some PoC source code for an Android Application which gets the certificate from an application package (apk) and prints out the details to the debug log. It is setup to get the certificate from itself, but could easily be adjusted to get the certificate from another installed application. It also only prints out details of the first certificate, and an application can be signed with multiple certificates, so adjust if required.

Example output:

DEBUG/Example(468): Certificate for: CN=Android Debug, O=Android, C=US
DEBUG/Example(468): Certificate issued by: CN=Android Debug, O=Android, C=US
DEBUG/Example(468): The certificate is valid from Wed Aug 03 19:43:44 GMT+00:00 2011 to Fri Jul 26 19:43:44 GMT+00:00 2041
DEBUG/Example(468): Certificate SN# 1312400624
DEBUG/Example(468): Generated with SHA1withRSA

Source:

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class CertActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                PackageManager pm = this.getPackageManager();
                String packageName = this.getPackageName();
                int flags = PackageManager.GET_SIGNATURES;
                PackageInfo packageInfo = null;

                try {
                        packageInfo = pm.getPackageInfo(packageName, flags);
                } catch (NameNotFoundException e) {
                        // TODO some error checking
                        e.printStackTrace();
                }
                Signature[] signatures = packageInfo.signatures;

                // cert = DER encoded X.509 certificate:
                byte[] cert = signatures[0].toByteArray();
                InputStream input = new ByteArrayInputStream(cert);

                CertificateFactory cf = null;
                try {
                        cf = CertificateFactory.getInstance("X509");
                } catch (CertificateException e) {
                        // TODO some error checking
                        e.printStackTrace();
                }
                X509Certificate c = null;
                try {
                        c = (X509Certificate) cf.generateCertificate(input);
                } catch (CertificateException e) {
                        // TODO some error checking
                        e.printStackTrace();
                }
                Log.d("Example", "Certificate for: " + c.getSubjectDN());
                Log.d("Example", "Certificate issued by: " + c.getIssuerDN());
                Log.d("Example", "The certificate is valid from " + c.getNotBefore() + " to " + c.getNotAfter());
                Log.d("Example", "Certificate SN# " + c.getSerialNumber());
                Log.d("Example", "Generated with " + c.getSigAlgName());

        }
}