use of android.content.pm.Signature in project XobotOS by xamarin.
the class NetworkPolicyManager method isUidValidForPolicy.
/**
* Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
* usually to protect critical system services.
*/
public static boolean isUidValidForPolicy(Context context, int uid) {
// first, quick-reject non-applications
if (uid < android.os.Process.FIRST_APPLICATION_UID || uid > android.os.Process.LAST_APPLICATION_UID) {
return false;
}
if (!ALLOW_PLATFORM_APP_POLICY) {
final PackageManager pm = context.getPackageManager();
final HashSet<Signature> systemSignature;
try {
systemSignature = Sets.newHashSet(pm.getPackageInfo("android", GET_SIGNATURES).signatures);
} catch (NameNotFoundException e) {
throw new RuntimeException("problem finding system signature", e);
}
try {
// reject apps signed with platform cert
for (String packageName : pm.getPackagesForUid(uid)) {
final HashSet<Signature> packageSignature = Sets.newHashSet(pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
if (packageSignature.containsAll(systemSignature)) {
return false;
}
}
} catch (NameNotFoundException e) {
}
}
// nothing found above; we can apply policy to UID
return true;
}
use of android.content.pm.Signature in project platform_frameworks_base by android.
the class WebViewFactory method signaturesEquals.
/**
* Returns true if the signatures match, false otherwise
*/
private static boolean signaturesEquals(Signature[] s1, Signature[] s2) {
if (s1 == null) {
return s2 == null;
}
if (s2 == null)
return false;
ArraySet<Signature> set1 = new ArraySet<>();
for (Signature signature : s1) {
set1.add(signature);
}
ArraySet<Signature> set2 = new ArraySet<>();
for (Signature signature : s2) {
set2.add(signature);
}
return set1.equals(set2);
}
use of android.content.pm.Signature in project carat by amplab.
the class SamplingLibrary method getSignatures.
public static List<String> getSignatures(PackageInfo pak) {
List<String> sigList = new LinkedList<String>();
String[] pmInfos = pak.requestedPermissions;
if (pmInfos != null) {
byte[] bytes = getPermissionBytes(pmInfos);
String hexB = convertToHex(bytes);
sigList.add(hexB);
}
Signature[] sigs = pak.signatures;
for (Signature s : sigs) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(s.toByteArray());
byte[] dig = md.digest();
// Add SHA-1
sigList.add(convertToHex(dig));
CertificateFactory fac = CertificateFactory.getInstance("X.509");
if (fac == null)
continue;
X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
if (cert == null)
continue;
PublicKey pkPublic = cert.getPublicKey();
if (pkPublic == null)
continue;
String al = pkPublic.getAlgorithm();
if (al.equals("RSA")) {
md = MessageDigest.getInstance("SHA-256");
RSAPublicKey rsa = (RSAPublicKey) pkPublic;
byte[] data = rsa.getModulus().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of modulus
sigList.add(convertToHex(dig));
} else if (al.equals("DSA")) {
DSAPublicKey dsa = (DSAPublicKey) pkPublic;
md = MessageDigest.getInstance("SHA-256");
byte[] data = dsa.getY().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of public key (DSA)
sigList.add(convertToHex(dig));
} else {
Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
}
} catch (NoSuchAlgorithmException e) {
// Do nothing
} catch (CertificateException e) {
// Do nothing
}
}
return sigList;
}
use of android.content.pm.Signature in project SeriesGuide by UweTrottmann.
the class Utils method hasUnlockKeyInstalled.
/**
* Returns if the user has a valid copy of X Pass installed.
*/
private static boolean hasUnlockKeyInstalled(Context context) {
try {
// Get our signing key
PackageManager manager = context.getPackageManager();
@SuppressLint("PackageManagerGetSignatures") PackageInfo appInfoSeriesGuide = manager.getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
// Try to find the X signing key
@SuppressLint("PackageManagerGetSignatures") PackageInfo appInfoSeriesGuideX = manager.getPackageInfo("com.battlelancer.seriesguide.x", PackageManager.GET_SIGNATURES);
Signature[] sgSignatures = appInfoSeriesGuide.signatures;
Signature[] xSignatures = appInfoSeriesGuideX.signatures;
if (sgSignatures.length == xSignatures.length) {
for (int i = 0; i < sgSignatures.length; i++) {
if (!sgSignatures[i].toCharsString().equals(xSignatures[i].toCharsString())) {
// a signature does not match
return false;
}
}
return true;
}
} catch (NameNotFoundException e) {
// Expected exception that occurs if the package is not present.
}
return false;
}
use of android.content.pm.Signature in project android_frameworks_base by ParanoidAndroid.
the class PackageManagerBackupAgent method writeSignatureArray.
private static void writeSignatureArray(DataOutputStream out, Signature[] sigs) throws IOException {
// write the number of signatures in the array
out.writeInt(sigs.length);
// write the signatures themselves, length + flattened buffer
for (Signature sig : sigs) {
byte[] flat = sig.toByteArray();
out.writeInt(flat.length);
out.write(flat);
}
}
Aggregations