Search in sources :

Example 86 with Signature

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

the class PackageSignatures method readXml.

void readXml(XmlPullParser parser, ArrayList<Signature> pastSignatures) throws IOException, XmlPullParserException {
    String countStr = parser.getAttributeValue(null, "count");
    if (countStr == null) {
        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <signatures> has" + " no count at " + parser.getPositionDescription());
        XmlUtils.skipCurrentTag(parser);
    }
    final int count = Integer.parseInt(countStr);
    mSignatures = new Signature[count];
    int pos = 0;
    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }
        String tagName = parser.getName();
        if (tagName.equals("cert")) {
            if (pos < count) {
                String index = parser.getAttributeValue(null, "index");
                if (index != null) {
                    try {
                        int idx = Integer.parseInt(index);
                        String key = parser.getAttributeValue(null, "key");
                        if (key == null) {
                            if (idx >= 0 && idx < pastSignatures.size()) {
                                Signature sig = pastSignatures.get(idx);
                                if (sig != null) {
                                    mSignatures[pos] = pastSignatures.get(idx);
                                    pos++;
                                } else {
                                    PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is not defined at " + parser.getPositionDescription());
                                }
                            } else {
                                PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is out of bounds at " + parser.getPositionDescription());
                            }
                        } else {
                            while (pastSignatures.size() <= idx) {
                                pastSignatures.add(null);
                            }
                            Signature sig = new Signature(key);
                            pastSignatures.set(idx, sig);
                            mSignatures[pos] = sig;
                            pos++;
                        }
                    } catch (NumberFormatException e) {
                        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is not a number at " + parser.getPositionDescription());
                    } catch (IllegalArgumentException e) {
                        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " has an invalid signature at " + parser.getPositionDescription() + ": " + e.getMessage());
                    }
                } else {
                    PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> has" + " no index at " + parser.getPositionDescription());
                }
            } else {
                PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: too " + "many <cert> tags, expected " + count + " at " + parser.getPositionDescription());
            }
        } else {
            PackageManagerService.reportSettingsProblem(Log.WARN, "Unknown element under <cert>: " + parser.getName());
        }
        XmlUtils.skipCurrentTag(parser);
    }
    if (pos < count) {
        // Should never happen -- there is an error in the written
        // settings -- but if it does we don't want to generate
        // a bad array.
        Signature[] newSigs = new Signature[pos];
        System.arraycopy(mSignatures, 0, newSigs, 0, pos);
        mSignatures = newSigs;
    }
}
Also used : Signature(android.content.pm.Signature)

Example 87 with Signature

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

the class ServiceWatcher method getSignatureSets.

public static ArrayList<HashSet<Signature>> getSignatureSets(Context context, List<String> initialPackageNames) {
    PackageManager pm = context.getPackageManager();
    ArrayList<HashSet<Signature>> sigSets = new ArrayList<HashSet<Signature>>();
    for (int i = 0, size = initialPackageNames.size(); i < size; i++) {
        String pkg = initialPackageNames.get(i);
        try {
            HashSet<Signature> set = new HashSet<Signature>();
            Signature[] sigs = pm.getPackageInfo(pkg, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_SIGNATURES).signatures;
            set.addAll(Arrays.asList(sigs));
            sigSets.add(set);
        } catch (NameNotFoundException e) {
            Log.w("ServiceWatcher", pkg + " not found");
        }
    }
    return sigSets;
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Signature(android.content.pm.Signature) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 88 with Signature

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

the class Utils method getCertFingerprintsFromPackageManager.

/**
     * Returns the normalized sha-256 fingerprints of a given package according to the Android
     * package manager.
     */
public static List<String> getCertFingerprintsFromPackageManager(String packageName, Context context) throws NameNotFoundException {
    Signature[] signatures = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures;
    ArrayList<String> result = new ArrayList<String>(signatures.length);
    for (Signature sig : signatures) {
        result.add(computeNormalizedSha256Fingerprint(sig.toByteArray()));
    }
    return result;
}
Also used : Signature(android.content.pm.Signature) ArrayList(java.util.ArrayList)

Example 89 with Signature

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

the class AccountManagerService method calculatePackageSignatureDigest.

private byte[] calculatePackageSignatureDigest(String callerPkg) {
    MessageDigest digester;
    try {
        digester = MessageDigest.getInstance("SHA-256");
        PackageInfo pkgInfo = mPackageManager.getPackageInfo(callerPkg, PackageManager.GET_SIGNATURES);
        for (Signature sig : pkgInfo.signatures) {
            digester.update(sig.toByteArray());
        }
    } catch (NoSuchAlgorithmException x) {
        Log.wtf(TAG, "SHA-256 should be available", x);
        digester = null;
    } catch (NameNotFoundException e) {
        Log.w(TAG, "Could not find packageinfo for: " + callerPkg);
        digester = null;
    }
    return (digester == null) ? null : digester.digest();
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 90 with Signature

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

the class PackageSignatures method readXml.

void readXml(XmlPullParser parser, ArrayList<Signature> pastSignatures) throws IOException, XmlPullParserException {
    String countStr = parser.getAttributeValue(null, "count");
    if (countStr == null) {
        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <signatures> has" + " no count at " + parser.getPositionDescription());
        XmlUtils.skipCurrentTag(parser);
    }
    final int count = Integer.parseInt(countStr);
    mSignatures = new Signature[count];
    int pos = 0;
    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }
        String tagName = parser.getName();
        if (tagName.equals("cert")) {
            if (pos < count) {
                String index = parser.getAttributeValue(null, "index");
                if (index != null) {
                    try {
                        int idx = Integer.parseInt(index);
                        String key = parser.getAttributeValue(null, "key");
                        if (key == null) {
                            if (idx >= 0 && idx < pastSignatures.size()) {
                                Signature sig = pastSignatures.get(idx);
                                if (sig != null) {
                                    mSignatures[pos] = pastSignatures.get(idx);
                                    pos++;
                                } else {
                                    PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is not defined at " + parser.getPositionDescription());
                                }
                            } else {
                                PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is out of bounds at " + parser.getPositionDescription());
                            }
                        } else {
                            while (pastSignatures.size() <= idx) {
                                pastSignatures.add(null);
                            }
                            Signature sig = new Signature(key);
                            pastSignatures.set(idx, sig);
                            mSignatures[pos] = sig;
                            pos++;
                        }
                    } catch (NumberFormatException e) {
                        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " is not a number at " + parser.getPositionDescription());
                    } catch (IllegalArgumentException e) {
                        PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> " + "index " + index + " has an invalid signature at " + parser.getPositionDescription() + ": " + e.getMessage());
                    }
                } else {
                    PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: <cert> has" + " no index at " + parser.getPositionDescription());
                }
            } else {
                PackageManagerService.reportSettingsProblem(Log.WARN, "Error in package manager settings: too " + "many <cert> tags, expected " + count + " at " + parser.getPositionDescription());
            }
        } else {
            PackageManagerService.reportSettingsProblem(Log.WARN, "Unknown element under <cert>: " + parser.getName());
        }
        XmlUtils.skipCurrentTag(parser);
    }
    if (pos < count) {
        // Should never happen -- there is an error in the written
        // settings -- but if it does we don't want to generate
        // a bad array.
        Signature[] newSigs = new Signature[pos];
        System.arraycopy(mSignatures, 0, newSigs, 0, pos);
        mSignatures = newSigs;
    }
}
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