Skip to content

Commit

Permalink
Verify package signature
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Mar 5, 2024
1 parent 07c9829 commit b399e09
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,102 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.os.IBinder;
import android.util.Log;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.microg.gms.common.Constants.GMS_PACKAGE_NAME;
import static org.microg.gms.common.Constants.GMS_MINUS_PACKAGE_NAME;
import static org.microg.gms.common.Constants.GMS_PACKAGE_SIGNATURE_SHA1;
import static org.microg.gms.common.Constants.GMS_SECONDARY_PACKAGE_SIGNATURE_SHA1;
import static org.microg.gms.common.Constants.MICROG_PACKAGE_SIGNATURE_SHA1;

public class MultiConnectionKeeper {
private static final String TAG = "GmsMultiConKeeper";
private static final String PREF_MASTER = "GmsMultiConKeeper";
private static final String PREF_TARGET = "GmsMultiConKeeper_target";
private static final String GOOGLE_APP_KEY = "24bb24c05e47e0aefa68a58a766179d9b613a600";
private static final String[] GOOGLE_PRIMARY_KEYS = {GMS_PACKAGE_SIGNATURE_SHA1, GMS_SECONDARY_PACKAGE_SIGNATURE_SHA1, GOOGLE_APP_KEY};

private static final String[] MICROG_PRIMARY_KEYS = {MICROG_PACKAGE_SIGNATURE_SHA1};
private static MultiConnectionKeeper INSTANCE;

private final Context context;

private final String gmsPackage;
private final Map<String, Connection> connections = new HashMap<String, Connection>();

private String getApplicationName(String packageId) throws PackageManager.NameNotFoundException {
ApplicationInfo ai;
if (SDK_INT >= TIRAMISU) {
ai = context.getPackageManager().getApplicationInfo(
packageId,
PackageManager.ApplicationInfoFlags.of(
PackageManager.GET_META_DATA
)
);
} else {
ai = context.getPackageManager().getApplicationInfo(packageId, 0);
private Boolean isGoogleOrMicrogSig(PackageManager pm, String packageId) throws PackageManager.NameNotFoundException {
List<String> signatures = Arrays.asList(GOOGLE_PRIMARY_KEYS);
signatures.addAll(Arrays.asList(MICROG_PRIMARY_KEYS));
return signatureIsIn(pm, packageId, signatures);
}

private Boolean isMicrogSig(PackageManager pm, String packageId) throws PackageManager.NameNotFoundException {
List<String> signatures = Arrays.asList(MICROG_PRIMARY_KEYS);
return signatureIsIn(pm, packageId, signatures);
}

private Boolean signatureIsIn(PackageManager pm, String packageId, List<String> signatures) throws PackageManager.NameNotFoundException {
Signature[] appSignatures = pm.getPackageInfo(packageId, PackageManager.GET_SIGNATURES).signatures;
for (Signature sig : appSignatures) {
if (sig != null && signatures.contains(sha1sum(sig.toByteArray())))
return true;
}
return false;
}

private String sha1sum(byte[] bytes) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA1");
} catch (final NoSuchAlgorithmException e) {
return null;
}
return (String) context.getPackageManager().getApplicationLabel(ai);
if (md != null) {
bytes = md.digest(bytes);
if (bytes != null) {
StringBuilder sb = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
return null;
}

private String getTargetPackageWithoutPref() {
// Pref: gms > microG > self
PackageManager pm = context.getPackageManager();
try {
// We check the application has an activity and its name to avoid any fraudulent
// app impersonating microG, "Cooking app" with com.google.android.gms id
String name = getApplicationName(GMS_PACKAGE_NAME);
if (name.contains("Google Play") || name.contains("microG")) {
if (isGoogleOrMicrogSig(pm, GMS_PACKAGE_NAME)) {
Log.d(TAG, GMS_PACKAGE_NAME + " found !");
return GMS_PACKAGE_NAME;
} else {
Log.w(TAG, GMS_MINUS_PACKAGE_NAME + " found with another name: " + name);
Log.w(TAG, GMS_PACKAGE_NAME + " found with another signature");
}
} catch (PackageManager.NameNotFoundException e) {
Log.d(TAG, GMS_PACKAGE_NAME + " not found");
}
try {
String name = getApplicationName(GMS_MINUS_PACKAGE_NAME);
if (name.contains("microG")) {
if (isMicrogSig(pm, GMS_MINUS_PACKAGE_NAME)) {
Log.d(TAG, GMS_MINUS_PACKAGE_NAME + " found !");
return GMS_MINUS_PACKAGE_NAME;
} else {
Log.w(TAG, GMS_MINUS_PACKAGE_NAME + " found with another name: " + name);
Log.w(TAG, GMS_MINUS_PACKAGE_NAME + " found with another signature");
}
} catch (PackageManager.NameNotFoundException e) {
Log.d(TAG, GMS_MINUS_PACKAGE_NAME + " not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Constants {
public static final String GSF_PACKAGE_NAME = "com.google.android.gsf";
public static final String GMS_PACKAGE_SIGNATURE_SHA1 = "38918a453d07199354f8b19af05ec6562ced5788";
public static final String GMS_SECONDARY_PACKAGE_SIGNATURE_SHA1 = "bd32424203e0fb25f36b57e5aa356f9bdd1da998";
public static final String MICROG_PACKAGE_SIGNATURE_SHA1 = "10321bd893f69af97f7573aafe9de1dc0901f3a1";
@Deprecated
public static final int MAX_REFERENCE_VERSION = GMS_VERSION_CODE;
}

0 comments on commit b399e09

Please sign in to comment.