Search in sources :

Example 21 with Signature

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

the class NetworkPolicyManagerServiceTest method _setUp.

public void _setUp() throws Exception {
    super.setUp();
    setCurrentTimeMillis(TEST_START);
    // intercept various broadcasts, and pretend that uids have packages
    mServiceContext = new BroadcastInterceptingContext(getContext()) {

        @Override
        public PackageManager getPackageManager() {
            return new MockPackageManager() {

                @Override
                public String[] getPackagesForUid(int uid) {
                    return new String[] { "com.example" };
                }

                @Override
                public PackageInfo getPackageInfo(String packageName, int flags) {
                    final PackageInfo info = new PackageInfo();
                    final Signature signature;
                    if ("android".equals(packageName)) {
                        signature = new Signature("F00D");
                    } else {
                        signature = new Signature("DEAD");
                    }
                    info.signatures = new Signature[] { signature };
                    return info;
                }
            };
        }

        @Override
        public void startActivity(Intent intent) {
        // ignored
        }
    };
    mPolicyDir = getContext().getFilesDir();
    if (mPolicyDir.exists()) {
        IoUtils.deleteContents(mPolicyDir);
    }
    mActivityManager = createMock(IActivityManager.class);
    mStatsService = createMock(INetworkStatsService.class);
    mNetworkManager = createMock(INetworkManagementService.class);
    mPolicyListener = createMock(INetworkPolicyListener.class);
    mTime = createMock(TrustedTime.class);
    mConnManager = createMock(IConnectivityManager.class);
    mNotifManager = createMock(INotificationManager.class);
    mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
    mService.bindConnectivityManager(mConnManager);
    mService.bindNotificationManager(mNotifManager);
    // RemoteCallbackList needs a binder to use as key
    expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
    replay();
    mService.registerListener(mPolicyListener);
    verifyAndReset();
    // catch IProcessObserver during systemReady()
    final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
    mActivityManager.registerProcessObserver(capture(processObserver));
    expectLastCall().atLeastOnce();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetworkManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
    expectCurrentTime();
    replay();
    mService.systemReady();
    verifyAndReset();
    mProcessObserver = processObserver.getValue();
    mNetworkObserver = networkObserver.getValue();
}
Also used : PackageInfo(android.content.pm.PackageInfo) TrustedTime(android.util.TrustedTime) INetworkManagementService(android.os.INetworkManagementService) MockPackageManager(android.test.mock.MockPackageManager) Intent(android.content.Intent) INetworkPolicyListener(android.net.INetworkPolicyListener) Capture(org.easymock.Capture) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) MockPackageManager(android.test.mock.MockPackageManager) INotificationManager(android.app.INotificationManager) Signature(android.content.pm.Signature) INetworkStatsService(android.net.INetworkStatsService) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) IConnectivityManager(android.net.IConnectivityManager) IProcessObserver(android.app.IProcessObserver) IActivityManager(android.app.IActivityManager)

Example 22 with Signature

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

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

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

the class BackupUtilsTest method testHashSignature.

public void testHashSignature() {
    final byte[] sig1 = "abc".getBytes();
    final byte[] sig2 = "def".getBytes();
    final byte[] hash1a = BackupUtils.hashSignature(sig1);
    final byte[] hash1b = BackupUtils.hashSignature(new Signature(sig1));
    final byte[] hash2a = BackupUtils.hashSignature(sig2);
    final byte[] hash2b = BackupUtils.hashSignature(new Signature(sig2));
    assertEquals(32, hash1a.length);
    MoreAsserts.assertEquals(hash1a, hash1b);
    assertEquals(32, hash2a.length);
    MoreAsserts.assertEquals(hash2a, hash2b);
    assertFalse(Arrays.equals(hash1a, hash2a));
    final ArrayList<byte[]> listA = BackupUtils.hashSignatureArray(Arrays.asList("abc".getBytes(), "def".getBytes()));
    final ArrayList<byte[]> listB = BackupUtils.hashSignatureArray(new Signature[] { new Signature("abc".getBytes()), new Signature("def".getBytes()) });
    assertEquals(2, listA.size());
    assertEquals(2, listB.size());
    MoreAsserts.assertEquals(hash1a, listA.get(0));
    MoreAsserts.assertEquals(hash1a, listB.get(0));
    MoreAsserts.assertEquals(hash2a, listA.get(1));
    MoreAsserts.assertEquals(hash2a, listB.get(1));
}
Also used : Signature(android.content.pm.Signature)

Example 24 with Signature

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

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

use of android.content.pm.Signature in project DroidPlugin by DroidPluginTeam.

the class IPluginManagerImpl method loadAllPlugin.

private void loadAllPlugin(Context context) {
    long b = System.currentTimeMillis();
    ArrayList<File> apkfiles = null;
    try {
        apkfiles = new ArrayList<File>();
        File baseDir = new File(PluginDirHelper.getBaseDir(context));
        File[] dirs = baseDir.listFiles();
        for (File dir : dirs) {
            if (dir.isDirectory()) {
                File file = new File(dir, "apk/base-1.apk");
                if (file.exists()) {
                    apkfiles.add(file);
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "scan a apk file error", e);
    }
    Log.i(TAG, "Search apk cost %s ms", (System.currentTimeMillis() - b));
    b = System.currentTimeMillis();
    if (apkfiles != null && apkfiles.size() > 0) {
        for (File pluginFile : apkfiles) {
            long b1 = System.currentTimeMillis();
            try {
                PluginPackageParser pluginPackageParser = new PluginPackageParser(mContext, pluginFile);
                Signature[] signatures = readSignatures(pluginPackageParser.getPackageName());
                if (signatures == null || signatures.length <= 0) {
                    pluginPackageParser.collectCertificates(0);
                    PackageInfo info = pluginPackageParser.getPackageInfo(PackageManager.GET_SIGNATURES);
                    saveSignatures(info);
                } else {
                    mSignatureCache.put(pluginPackageParser.getPackageName(), signatures);
                    pluginPackageParser.writeSignature(signatures);
                }
                if (!mPluginCache.containsKey(pluginPackageParser.getPackageName())) {
                    mPluginCache.put(pluginPackageParser.getPackageName(), pluginPackageParser);
                }
            } catch (Throwable e) {
                Log.e(TAG, "parse a apk file error %s", e, pluginFile.getPath());
            } finally {
                Log.i(TAG, "Parse %s apk cost %s ms", pluginFile.getPath(), (System.currentTimeMillis() - b1));
            }
        }
    }
    Log.i(TAG, "Parse all apk cost %s ms", (System.currentTimeMillis() - b));
    b = System.currentTimeMillis();
    try {
        mActivityManagerService.onCreate(IPluginManagerImpl.this);
    } catch (Throwable e) {
        Log.e(TAG, "mActivityManagerService.onCreate", e);
    }
    Log.i(TAG, "ActivityManagerService.onCreate %s ms", (System.currentTimeMillis() - b));
}
Also used : PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature) PluginPackageParser(com.morgoo.droidplugin.pm.parser.PluginPackageParser) File(java.io.File) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException)

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