Search in sources :

Example 91 with Signature

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

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 92 with Signature

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

the class SystemConfig method readPermissionsFromXml.

private void readPermissionsFromXml(File permFile, int permissionFlag) {
    FileReader permReader = null;
    try {
        permReader = new FileReader(permFile);
    } catch (FileNotFoundException e) {
        Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
        return;
    }
    final boolean lowRam = ActivityManager.isLowRamDeviceStatic();
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(permReader);
        int type;
        while ((type = parser.next()) != parser.START_TAG && type != parser.END_DOCUMENT) {
            ;
        }
        if (type != parser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }
        if (!parser.getName().equals("permissions") && !parser.getName().equals("config")) {
            throw new XmlPullParserException("Unexpected start tag in " + permFile + ": found " + parser.getName() + ", expected 'permissions' or 'config'");
        }
        boolean allowAll = permissionFlag == ALLOW_ALL;
        boolean allowLibs = (permissionFlag & ALLOW_LIBS) != 0;
        boolean allowFeatures = (permissionFlag & ALLOW_FEATURES) != 0;
        boolean allowPermissions = (permissionFlag & ALLOW_PERMISSIONS) != 0;
        boolean allowAppConfigs = (permissionFlag & ALLOW_APP_CONFIGS) != 0;
        while (true) {
            XmlUtils.nextElement(parser);
            if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
                break;
            }
            String name = parser.getName();
            if ("group".equals(name) && allowAll) {
                String gidStr = parser.getAttributeValue(null, "gid");
                if (gidStr != null) {
                    int gid = android.os.Process.getGidForName(gidStr);
                    mGlobalGids = appendInt(mGlobalGids, gid);
                } else {
                    Slog.w(TAG, "<group> without gid in " + permFile + " at " + parser.getPositionDescription());
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("allow-permission".equals(name)) {
                String perm = parser.getAttributeValue(null, "name");
                if (perm == null) {
                    Slog.w(TAG, "<allow-permission> without name at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                String signature = parser.getAttributeValue(null, "signature");
                if (signature == null) {
                    Slog.w(TAG, "<allow-permission> without signature at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                Signature sig = null;
                try {
                    sig = new Signature(signature);
                } catch (IllegalArgumentException e) {
                // sig will be null so we will log it below
                }
                if (sig != null) {
                    ArraySet<String> perms = mSignatureAllowances.get(sig);
                    if (perms == null) {
                        perms = new ArraySet<String>();
                        mSignatureAllowances.put(sig, perms);
                    }
                    perms.add(perm);
                } else {
                    Slog.w(TAG, "<allow-permission> with bad signature at " + parser.getPositionDescription());
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("permission".equals(name) && allowPermissions) {
                String perm = parser.getAttributeValue(null, "name");
                if (perm == null) {
                    Slog.w(TAG, "<permission> without name in " + permFile + " at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                perm = perm.intern();
                readPermission(parser, perm);
            } else if ("assign-permission".equals(name) && allowPermissions) {
                String perm = parser.getAttributeValue(null, "name");
                if (perm == null) {
                    Slog.w(TAG, "<assign-permission> without name in " + permFile + " at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                String uidStr = parser.getAttributeValue(null, "uid");
                if (uidStr == null) {
                    Slog.w(TAG, "<assign-permission> without uid in " + permFile + " at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                int uid = Process.getUidForName(uidStr);
                if (uid < 0) {
                    Slog.w(TAG, "<assign-permission> with unknown uid \"" + uidStr + "  in " + permFile + " at " + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                perm = perm.intern();
                ArraySet<String> perms = mSystemPermissions.get(uid);
                if (perms == null) {
                    perms = new ArraySet<String>();
                    mSystemPermissions.put(uid, perms);
                }
                perms.add(perm);
                XmlUtils.skipCurrentTag(parser);
            } else if ("library".equals(name) && allowLibs) {
                String lname = parser.getAttributeValue(null, "name");
                String lfile = parser.getAttributeValue(null, "file");
                if (lname == null) {
                    Slog.w(TAG, "<library> without name in " + permFile + " at " + parser.getPositionDescription());
                } else if (lfile == null) {
                    Slog.w(TAG, "<library> without file in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    //Log.i(TAG, "Got library " + lname + " in " + lfile);
                    mSharedLibraries.put(lname, lfile);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("feature".equals(name) && allowFeatures) {
                String fname = parser.getAttributeValue(null, "name");
                int fversion = XmlUtils.readIntAttribute(parser, "version", 0);
                boolean allowed;
                if (!lowRam) {
                    allowed = true;
                } else {
                    String notLowRam = parser.getAttributeValue(null, "notLowRam");
                    allowed = !"true".equals(notLowRam);
                }
                if (fname == null) {
                    Slog.w(TAG, "<feature> without name in " + permFile + " at " + parser.getPositionDescription());
                } else if (allowed) {
                    addFeature(fname, fversion);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("unavailable-feature".equals(name) && allowFeatures) {
                String fname = parser.getAttributeValue(null, "name");
                if (fname == null) {
                    Slog.w(TAG, "<unavailable-feature> without name in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mUnavailableFeatures.add(fname);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<allow-in-power-save-except-idle> without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mAllowInPowerSaveExceptIdle.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("allow-in-power-save".equals(name) && allowAll) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<allow-in-power-save> without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mAllowInPowerSave.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("allow-in-data-usage-save".equals(name) && allowAll) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<allow-in-data-usage-save> without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mAllowInDataUsageSave.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else if ("app-link".equals(name) && allowAppConfigs) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<app-link> without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mLinkedApps.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("system-user-whitelisted-app".equals(name) && allowAppConfigs) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<system-user-whitelisted-app> without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mSystemUserWhitelistedApps.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("system-user-blacklisted-app".equals(name) && allowAppConfigs) {
                String pkgname = parser.getAttributeValue(null, "package");
                if (pkgname == null) {
                    Slog.w(TAG, "<system-user-blacklisted-app without package in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mSystemUserBlacklistedApps.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("default-enabled-vr-app".equals(name) && allowAppConfigs) {
                String pkgname = parser.getAttributeValue(null, "package");
                String clsname = parser.getAttributeValue(null, "class");
                if (pkgname == null) {
                    Slog.w(TAG, "<default-enabled-vr-app without package in " + permFile + " at " + parser.getPositionDescription());
                } else if (clsname == null) {
                    Slog.w(TAG, "<default-enabled-vr-app without class in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    mDefaultVrComponents.add(new ComponentName(pkgname, clsname));
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("backup-transport-whitelisted-service".equals(name) && allowFeatures) {
                String serviceName = parser.getAttributeValue(null, "service");
                if (serviceName == null) {
                    Slog.w(TAG, "<backup-transport-whitelisted-service> without service in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    ComponentName cn = ComponentName.unflattenFromString(serviceName);
                    if (cn == null) {
                        Slog.w(TAG, "<backup-transport-whitelisted-service> with invalid service name " + serviceName + " in " + permFile + " at " + parser.getPositionDescription());
                    } else {
                        mBackupTransportWhitelist.add(cn);
                    }
                }
                XmlUtils.skipCurrentTag(parser);
            } else if ("disabled-until-used-preinstalled-carrier-associated-app".equals(name) && allowAppConfigs) {
                String pkgname = parser.getAttributeValue(null, "package");
                String carrierPkgname = parser.getAttributeValue(null, "carrierAppPackage");
                if (pkgname == null || carrierPkgname == null) {
                    Slog.w(TAG, "<disabled-until-used-preinstalled-carrier-associated-app" + " without package or carrierAppPackage in " + permFile + " at " + parser.getPositionDescription());
                } else {
                    List<String> associatedPkgs = mDisabledUntilUsedPreinstalledCarrierAssociatedApps.get(carrierPkgname);
                    if (associatedPkgs == null) {
                        associatedPkgs = new ArrayList<>();
                        mDisabledUntilUsedPreinstalledCarrierAssociatedApps.put(carrierPkgname, associatedPkgs);
                    }
                    associatedPkgs.add(pkgname);
                }
                XmlUtils.skipCurrentTag(parser);
            } else {
                XmlUtils.skipCurrentTag(parser);
                continue;
            }
        }
    } catch (XmlPullParserException e) {
        Slog.w(TAG, "Got exception parsing permissions.", e);
    } catch (IOException e) {
        Slog.w(TAG, "Got exception parsing permissions.", e);
    } finally {
        IoUtils.closeQuietly(permReader);
    }
    // those features if not already defined by the static config
    if (StorageManager.isFileEncryptedNativeOnly()) {
        addFeature(PackageManager.FEATURE_FILE_BASED_ENCRYPTION, 0);
        addFeature(PackageManager.FEATURE_SECURELY_REMOVES_USERS, 0);
    }
    for (String featureName : mUnavailableFeatures) {
        removeFeature(featureName);
    }
}
Also used : ArraySet(android.util.ArraySet) FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Signature(android.content.pm.Signature) FileReader(java.io.FileReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ComponentName(android.content.ComponentName) ArrayList(java.util.ArrayList) List(java.util.List)

Example 93 with Signature

use of android.content.pm.Signature in project dropbox-sdk-java by dropbox.

the class DbxOfficialAppConnector method getDropboxAppPackage.

/**
 * Verify that intent will be processed by Dropbox App
 *
 * @return PackageInfo of DropboxApp if Dropbox App can process intent, else null
 */
static PackageInfo getDropboxAppPackage(Context context, Intent intent) {
    PackageManager manager = context.getPackageManager();
    List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
    if (null == infos || 1 != infos.size()) {
        // is available, or multiple activities are confusing us.
        return null;
    } else {
        // The official app exists. Make sure it's the correct one by
        // checking signing keys.
        ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
        if (resolveInfo == null) {
            return null;
        }
        final PackageInfo packageInfo;
        try {
            packageInfo = manager.getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_SIGNATURES);
        } catch (NameNotFoundException e) {
            return null;
        }
        for (Signature signature : packageInfo.signatures) {
            for (String dbSignature : DROPBOX_APP_SIGNATURES) {
                if (dbSignature.equals(signature.toCharsString())) {
                    return packageInfo;
                }
            }
        }
    }
    return null;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature)

Example 94 with Signature

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

the class WebViewUpdateServiceTest method testWithSignatures.

// Ensure that package with valid signatures is chosen rather than package with invalid
// signatures.
public void testWithSignatures() {
    String validPackage = "valid package";
    String invalidPackage = "invalid package";
    Signature validSignature = new Signature("11");
    Signature invalidExpectedSignature = new Signature("22");
    Signature invalidPackageSignature = new Signature("33");
    WebViewProviderInfo[] packages = new WebViewProviderInfo[] { new WebViewProviderInfo(invalidPackage, "", true, false, new String[] { Base64.encodeToString(invalidExpectedSignature.toByteArray(), Base64.DEFAULT) }), new WebViewProviderInfo(validPackage, "", true, false, new String[] { Base64.encodeToString(validSignature.toByteArray(), Base64.DEFAULT) }) };
    setupWithPackages(packages, true, /* fallback logic enabled */
    1, /* numRelros */
    false);
    mTestSystemImpl.setPackageInfo(createPackageInfo(invalidPackage, true, /* enabled */
    true, /* valid */
    true, /* installed */
    new Signature[] { invalidPackageSignature }, 0));
    mTestSystemImpl.setPackageInfo(createPackageInfo(validPackage, true, /* enabled */
    true, /* valid */
    true, /* installed */
    new Signature[] { validSignature }, 0));
    mWebViewUpdateServiceImpl.prepareWebViewInSystemServer();
    checkPreparationPhasesForPackage(validPackage, 1);
    WebViewProviderInfo[] validPackages = mWebViewUpdateServiceImpl.getValidWebViewPackages();
    assertEquals(1, validPackages.length);
    assertEquals(validPackage, validPackages[0].packageName);
}
Also used : Signature(android.content.pm.Signature) WebViewProviderInfo(android.webkit.WebViewProviderInfo)

Example 95 with Signature

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

the class KeySetManagerServiceTest method testPublicKeyCertReprEquiv.

/* test equivalence of PackageManager cert encoding and PackageParser manifest keys */
public void testPublicKeyCertReprEquiv() throws CertificateException {
    PublicKey keyA = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyA);
    PublicKey keyB = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyB);
    PublicKey keyC = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyC);
    Signature sigA = new Signature(KeySetStrings.ctsKeySetCertA);
    Signature sigB = new Signature(KeySetStrings.ctsKeySetCertB);
    Signature sigC = new Signature(KeySetStrings.ctsKeySetCertC);
    assertNotNull(keyA);
    assertNotNull(keyB);
    assertNotNull(keyC);
    assertEquals(keyA, sigA.getPublicKey());
    assertEquals(keyB, sigB.getPublicKey());
    assertEquals(keyC, sigC.getPublicKey());
    byte[] bArrayPk = keyA.getEncoded();
    byte[] bArrayCert = sigA.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
    bArrayPk = keyB.getEncoded();
    bArrayCert = sigB.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
    bArrayPk = keyC.getEncoded();
    bArrayCert = sigC.getPublicKey().getEncoded();
    assertEquals(bArrayPk.length, bArrayCert.length);
    assertEquals(true, ArrayUtils.equals(bArrayPk, bArrayCert, bArrayPk.length));
}
Also used : PublicKey(java.security.PublicKey) 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