Search in sources :

Example 11 with Signature

use of android.content.pm.Signature in project XobotOS by xamarin.

the class NetworkPolicyManager method isUidValidForPolicy.

/**
     * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
     * usually to protect critical system services.
     */
public static boolean isUidValidForPolicy(Context context, int uid) {
    // first, quick-reject non-applications
    if (uid < android.os.Process.FIRST_APPLICATION_UID || uid > android.os.Process.LAST_APPLICATION_UID) {
        return false;
    }
    if (!ALLOW_PLATFORM_APP_POLICY) {
        final PackageManager pm = context.getPackageManager();
        final HashSet<Signature> systemSignature;
        try {
            systemSignature = Sets.newHashSet(pm.getPackageInfo("android", GET_SIGNATURES).signatures);
        } catch (NameNotFoundException e) {
            throw new RuntimeException("problem finding system signature", e);
        }
        try {
            // reject apps signed with platform cert
            for (String packageName : pm.getPackagesForUid(uid)) {
                final HashSet<Signature> packageSignature = Sets.newHashSet(pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
                if (packageSignature.containsAll(systemSignature)) {
                    return false;
                }
            }
        } catch (NameNotFoundException e) {
        }
    }
    // nothing found above; we can apply policy to UID
    return true;
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Signature(android.content.pm.Signature)

Example 12 with Signature

use of android.content.pm.Signature in project platform_frameworks_base by android.

the class WebViewFactory method signaturesEquals.

/**
     * Returns true if the signatures match, false otherwise
     */
private static boolean signaturesEquals(Signature[] s1, Signature[] s2) {
    if (s1 == null) {
        return s2 == null;
    }
    if (s2 == null)
        return false;
    ArraySet<Signature> set1 = new ArraySet<>();
    for (Signature signature : s1) {
        set1.add(signature);
    }
    ArraySet<Signature> set2 = new ArraySet<>();
    for (Signature signature : s2) {
        set2.add(signature);
    }
    return set1.equals(set2);
}
Also used : ArraySet(android.util.ArraySet) Signature(android.content.pm.Signature)

Example 13 with Signature

use of android.content.pm.Signature in project carat by amplab.

the class SamplingLibrary method getSignatures.

public static List<String> getSignatures(PackageInfo pak) {
    List<String> sigList = new LinkedList<String>();
    String[] pmInfos = pak.requestedPermissions;
    if (pmInfos != null) {
        byte[] bytes = getPermissionBytes(pmInfos);
        String hexB = convertToHex(bytes);
        sigList.add(hexB);
    }
    Signature[] sigs = pak.signatures;
    for (Signature s : sigs) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(s.toByteArray());
            byte[] dig = md.digest();
            // Add SHA-1
            sigList.add(convertToHex(dig));
            CertificateFactory fac = CertificateFactory.getInstance("X.509");
            if (fac == null)
                continue;
            X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
            if (cert == null)
                continue;
            PublicKey pkPublic = cert.getPublicKey();
            if (pkPublic == null)
                continue;
            String al = pkPublic.getAlgorithm();
            if (al.equals("RSA")) {
                md = MessageDigest.getInstance("SHA-256");
                RSAPublicKey rsa = (RSAPublicKey) pkPublic;
                byte[] data = rsa.getModulus().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of modulus
                sigList.add(convertToHex(dig));
            } else if (al.equals("DSA")) {
                DSAPublicKey dsa = (DSAPublicKey) pkPublic;
                md = MessageDigest.getInstance("SHA-256");
                byte[] data = dsa.getY().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of public key (DSA)
                sigList.add(convertToHex(dig));
            } else {
                Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
            }
        } catch (NoSuchAlgorithmException e) {
        // Do nothing
        } catch (CertificateException e) {
        // Do nothing
        }
    }
    return sigList;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) MessageDigest(java.security.MessageDigest)

Example 14 with Signature

use of android.content.pm.Signature in project SeriesGuide by UweTrottmann.

the class Utils method hasUnlockKeyInstalled.

/**
     * Returns if the user has a valid copy of X Pass installed.
     */
private static boolean hasUnlockKeyInstalled(Context context) {
    try {
        // Get our signing key
        PackageManager manager = context.getPackageManager();
        @SuppressLint("PackageManagerGetSignatures") PackageInfo appInfoSeriesGuide = manager.getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        // Try to find the X signing key
        @SuppressLint("PackageManagerGetSignatures") PackageInfo appInfoSeriesGuideX = manager.getPackageInfo("com.battlelancer.seriesguide.x", PackageManager.GET_SIGNATURES);
        Signature[] sgSignatures = appInfoSeriesGuide.signatures;
        Signature[] xSignatures = appInfoSeriesGuideX.signatures;
        if (sgSignatures.length == xSignatures.length) {
            for (int i = 0; i < sgSignatures.length; i++) {
                if (!sgSignatures[i].toCharsString().equals(xSignatures[i].toCharsString())) {
                    // a signature does not match
                    return false;
                }
            }
            return true;
        }
    } catch (NameNotFoundException e) {
    // Expected exception that occurs if the package is not present.
    }
    return false;
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 15 with Signature

use of android.content.pm.Signature in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerBackupAgent method writeSignatureArray.

private static void writeSignatureArray(DataOutputStream out, Signature[] sigs) throws IOException {
    // write the number of signatures in the array
    out.writeInt(sigs.length);
    // write the signatures themselves, length + flattened buffer
    for (Signature sig : sigs) {
        byte[] flat = sig.toByteArray();
        out.writeInt(flat.length);
        out.write(flat);
    }
}
Also used : Signature(android.content.pm.Signature)

Aggregations

Signature (android.content.pm.Signature)97 PackageManager (android.content.pm.PackageManager)34 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)31 PackageInfo (android.content.pm.PackageInfo)26 ArrayList (java.util.ArrayList)16 MessageDigest (java.security.MessageDigest)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 ArraySet (android.util.ArraySet)11 IOException (java.io.IOException)8 PublicKey (java.security.PublicKey)8 File (java.io.File)7 Intent (android.content.Intent)6 CertificateException (java.security.cert.CertificateException)6 HashSet (java.util.HashSet)6 ResolveInfo (android.content.pm.ResolveInfo)5 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)5 IActivityManager (android.app.IActivityManager)4 INotificationManager (android.app.INotificationManager)4 IProcessObserver (android.app.IProcessObserver)4 PackageParser (android.content.pm.PackageParser)4