How to check if another application in the system is genuine?

I would like to send intentions containing confidential information to another application in the system that I also wrote. Before doing this, I need to verify that the application to which I am sending them is signed by me, and not just the rogue version with the same package name and classes. How can I do this programmatically?

I want to do this using explicit intentions sent through startService () and startActivity (), so there is no problem around the translation. However, I do not want to send anything until I confirm that the name of the package that I specify is installed and signed by the author (I myself in this particular case).

+5
source share
1

.

final PackageManager packageManager = context.getPackageManager();
final List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);
CertificateFactory certFactory = null;
try {
    certFactory = CertificateFactory.getInstance("X509");
}
catch (CertificateException e) {
    // e.printStackTrace();
}

for (PackageInfo p : packageList) {
    String strName = p.applicationInfo.loadLabel(packageManager).toString();
    String strVendor = p.packageName;

    sb.append("<br>" + strName + " / " + strVendor + "<br>");

    Signature[] arrSignatures = p.signatures;
    for (Signature sig : arrSignatures) {
        /*
        * Get the X.509 certificate.
        */
        byte[] rawCert = sig.toByteArray();
        InputStream certStream = new ByteArrayInputStream(rawCert);

        X509Certificate x509Cert = null;
        try {
            x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);
        }
        catch (CertificateException e) {
            // e.printStackTrace();
        }

        sb.append("Certificate subject: " + x509Cert.getSubjectDN() + "<br>");
        sb.append("Certificate issuer: " + x509Cert.getIssuerDN() + "<br>");
        sb.append("Certificate serial number: " + x509Cert.getSerialNumber() + "<br>");
        sb.append("<br>");
    }
}
+2

All Articles