Java Code Examples for android.content.pm.PackageManager#checkSignatures()

The following examples show how to use android.content.pm.PackageManager#checkSignatures() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XProvider.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
private static void enforcePermission(Context context) throws SecurityException {
    int cuid = Util.getAppId(Binder.getCallingUid());

    // Access package manager as system user
    long ident = Binder.clearCallingIdentity();
    try {
        // Allow system
        if (cuid == Process.SYSTEM_UID)
            return;

        // Allow same signature
        PackageManager pm = context.getPackageManager();
        int uid = pm.getApplicationInfo(BuildConfig.APPLICATION_ID, 0).uid;
        if (pm.checkSignatures(cuid, uid) == PackageManager.SIGNATURE_MATCH)
            return;

        // Allow specific signature
        String[] cpkg = pm.getPackagesForUid(cuid);
        if (cpkg.length > 0) {
            byte[] bytes = Util.getSha1Fingerprint(context, cpkg[0]);
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes)
                sb.append(Integer.toString(b & 0xff, 16).toLowerCase());

            Resources resources = pm.getResourcesForApplication(BuildConfig.APPLICATION_ID);
            if (sb.toString().equals(resources.getString(R.string.pro_fingerprint)))
                return;
        }
        throw new SecurityException("Signature error cuid=" + cuid);
    } catch (Throwable ex) {
        throw new SecurityException(ex);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example 2
Source File: MainActivity.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
private boolean freeVersionInstalled() {
    PackageManager pm = getPackageManager();
    try {
        PackageInfo pInfo = pm.getPackageInfo(BuildConfig.BASE_APPLICATION_ID, 0);
        return pInfo.versionCode >= 68
                && pm.checkSignatures(BuildConfig.BASE_APPLICATION_ID, getPackageName())
                == PackageManager.SIGNATURE_MATCH;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 3
Source File: U.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
public static boolean hasSupportLibrary(Context context, int minVersion) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pInfo = pm.getPackageInfo(BuildConfig.SUPPORT_APPLICATION_ID, 0);
        return pInfo.versionCode >= minVersion
                && pm.checkSignatures(BuildConfig.SUPPORT_APPLICATION_ID, context.getPackageName()) == PackageManager.SIGNATURE_MATCH
                && context.getPackageName().equals(BuildConfig.BASE_APPLICATION_ID)
                && isSystemApp(context);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 4
Source File: NevoDecoratorService.java    From sdk with Apache License 2.0 5 votes vote down vote up
@Override public int onConnected(final INevoController controller, final Bundle options) {
	RemoteImplementation.initializeIfNotYet(NevoDecoratorService.this);

	final PackageManager pm = getPackageManager();
	final int caller_uid = Binder.getCallingUid(), my_uid = Process.myUid();
	if (caller_uid != my_uid && pm.checkSignatures(caller_uid, my_uid) != SIGNATURE_MATCH) {
		final String[] caller_pkgs = pm.getPackagesForUid(caller_uid);
		if (caller_pkgs == null || caller_pkgs.length == 0) throw new SecurityException();
		try { @SuppressLint("PackageManagerGetSignatures")
		final PackageInfo caller_info = pm.getPackageInfo(caller_pkgs[0], GET_SIGNATURES);
			if (caller_info == null) throw new SecurityException();
			for (final Signature signature : caller_info.signatures)
				if (signature.hashCode() != SIGNATURE_HASH) throw new SecurityException("Caller signature mismatch");
		} catch (final PackageManager.NameNotFoundException e) { throw new SecurityException(); }	// Should not happen
	}
	mCallerUid = caller_uid;

	mController = controller;
	if (options != null) mSupportedApiVersion = options.getInt(KEY_SUPPORTED_API_VERSION);
	try {
		Log.v(TAG, "onConnected");
		NevoDecoratorService.this.onConnected();
	} catch (final Throwable t) {
		Log.e(TAG, "Error running onConnected()", t);
		throw asParcelableException(t);
	}
	return mFlags;
}
 
Example 5
Source File: U.java    From SecondScreen with Apache License 2.0 5 votes vote down vote up
public static boolean hasSupportLibrary(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(BuildConfig.SUPPORT_APPLICATION_ID, 0);
        return pm.checkSignatures(BuildConfig.SUPPORT_APPLICATION_ID, BuildConfig.APPLICATION_ID)
                == PackageManager.SIGNATURE_MATCH;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 6
Source File: LocationManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void ensureFallbackFusedProviderPresentLocked(ArrayList<String> pkgs) {
    PackageManager pm = mContext.getPackageManager();
    String systemPackageName = mContext.getPackageName();
    ArrayList<HashSet<Signature>> sigSets = ServiceWatcher.getSignatureSets(mContext, pkgs);

    List<ResolveInfo> rInfos = pm.queryIntentServicesAsUser(
            new Intent(FUSED_LOCATION_SERVICE_ACTION),
            PackageManager.GET_META_DATA, mCurrentUserId);
    for (ResolveInfo rInfo : rInfos) {
        String packageName = rInfo.serviceInfo.packageName;

        // Check that the signature is in the list of supported sigs. If it's not in
        // this list the standard provider binding logic won't bind to it.
        try {
            PackageInfo pInfo;
            pInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            if (!ServiceWatcher.isSignatureMatch(pInfo.signatures, sigSets)) {
                Log.w(TAG, packageName + " resolves service " + FUSED_LOCATION_SERVICE_ACTION +
                        ", but has wrong signature, ignoring");
                continue;
            }
        } catch (NameNotFoundException e) {
            Log.e(TAG, "missing package: " + packageName);
            continue;
        }

        // Get the version info
        if (rInfo.serviceInfo.metaData == null) {
            Log.w(TAG, "Found fused provider without metadata: " + packageName);
            continue;
        }

        int version = rInfo.serviceInfo.metaData.getInt(
                ServiceWatcher.EXTRA_SERVICE_VERSION, -1);
        if (version == 0) {
            // This should be the fallback fused location provider.

            // Make sure it's in the system partition.
            if ((rInfo.serviceInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                if (D) Log.d(TAG, "Fallback candidate not in /system: " + packageName);
                continue;
            }

            // Check that the fallback is signed the same as the OS
            // as a proxy for coreApp="true"
            if (pm.checkSignatures(systemPackageName, packageName)
                    != PackageManager.SIGNATURE_MATCH) {
                if (D) {
                    Log.d(TAG, "Fallback candidate not signed the same as system: "
                            + packageName);
                }
                continue;
            }

            // Found a valid fallback.
            if (D) Log.d(TAG, "Found fallback provider: " + packageName);
            return;
        } else {
            if (D) Log.d(TAG, "Fallback candidate not version 0: " + packageName);
        }
    }

    throw new IllegalStateException("Unable to find a fused location provider that is in the "
            + "system partition with version 0 and signed with the platform certificate. "
            + "Such a package is needed to provide a default fused location provider in the "
            + "event that no other fused location provider has been installed or is currently "
            + "available. For example, coreOnly boot mode when decrypting the data "
            + "partition. The fallback must also be marked coreApp=\"true\" in the manifest");
}
 
Example 7
Source File: MainUI.java    From AnLinux-App with Apache License 2.0 4 votes vote down vote up
private boolean donationInstalled() {
    PackageManager packageManager = context.getPackageManager();
    return packageManager.checkSignatures(context.getPackageName(), "exa.lnx.d") == PackageManager.SIGNATURE_MATCH;
}
 
Example 8
Source File: MainUI.java    From AnLinux-Adfree with Apache License 2.0 4 votes vote down vote up
private boolean donationInstalled() {
    PackageManager packageManager = context.getPackageManager();
    return packageManager.checkSignatures(context.getPackageName(), "exa.lnx.d") == PackageManager.SIGNATURE_MATCH;
}
 
Example 9
Source File: PackageUtils.java    From prevent with Do What The F*ck You Want To Public License 4 votes vote down vote up
public static boolean isSystemSignaturePackage(PackageManager pm, String packageName) {
    return pm.checkSignatures("android", packageName) != PackageManager.SIGNATURE_NO_MATCH;
}
 
Example 10
Source File: StubProvider.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    int uid = Binder.getCallingUid();

    PackageManager pm = getContext().getPackageManager();
    String callerPackage = pm.getPackagesForUid(uid)[0];

    Log.d("AptoideDebug", "Someone is trying to update preferences");

    int result = pm.checkSignatures(callerPackage, getContext().getPackageName());

    if(result == PackageManager.SIGNATURE_MATCH) {
        switch (uriMatcher.match(uri)) {
            case CHANGE_PREFERENCE:

                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
                SharedPreferences.Editor edit = preferences.edit();
                int changed = 0;
                for (final Map.Entry<String, Object> entry : values.valueSet()) {
                    Object value = entry.getValue();
                    if (value instanceof String) {
                        edit.putString(entry.getKey(), (String) value);
                    } else if (value instanceof Integer) {
                        edit.putInt(entry.getKey(), (Integer) value);
                    } else if (value instanceof Long) {
                        edit.putLong(entry.getKey(), (Long) value);
                    } else if (value instanceof Boolean) {

                        if(entry.getKey().equals("debugmode")){
                            Aptoide.DEBUG_MODE = (Boolean) entry.getValue();
                        }

                        edit.putBoolean(entry.getKey(), (Boolean) value);
                    } else if (value instanceof Float) {
                        edit.putFloat(entry.getKey(), (Float) value);
                    }
                    changed++;
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(Aptoide.getContext(), "Preference set: " + entry.getKey() + "=" + entry.getValue(), Toast.LENGTH_LONG).show();
                        }
                    });
                }



                Log.d("AptoideDebug", "Commited");

                edit.commit();
                return changed;
            default:
                return 0;
        }

    }
    return 0;
}