use of android.content.pm.Signature in project android_frameworks_base by DirtyUnicorns.
the class PackageManagerService method getUidForVerifier.
private int getUidForVerifier(VerifierInfo verifierInfo) {
synchronized (mPackages) {
final PackageParser.Package pkg = mPackages.get(verifierInfo.packageName);
if (pkg == null) {
return -1;
} else if (pkg.mSignatures.length != 1) {
Slog.i(TAG, "Verifier package " + verifierInfo.packageName + " has more than one signature; ignoring");
return -1;
}
/*
* If the public key of the package's signature does not match
* our expected public key, then this is a different package and
* we should skip.
*/
final byte[] expectedPublicKey;
try {
final Signature verifierSig = pkg.mSignatures[0];
final PublicKey publicKey = verifierSig.getPublicKey();
expectedPublicKey = publicKey.getEncoded();
} catch (CertificateException e) {
return -1;
}
final byte[] actualPublicKey = verifierInfo.publicKey.getEncoded();
if (!Arrays.equals(actualPublicKey, expectedPublicKey)) {
Slog.i(TAG, "Verifier package " + verifierInfo.packageName + " does not have the expected public key; ignoring");
return -1;
}
return pkg.applicationInfo.uid;
}
}
use of android.content.pm.Signature in project android_frameworks_base by DirtyUnicorns.
the class ServiceWatcher method getSignatureSets.
public static ArrayList<HashSet<Signature>> getSignatureSets(Context context, List<String> initialPackageNames) {
PackageManager pm = context.getPackageManager();
ArrayList<HashSet<Signature>> sigSets = new ArrayList<HashSet<Signature>>();
for (int i = 0, size = initialPackageNames.size(); i < size; i++) {
String pkg = initialPackageNames.get(i);
try {
HashSet<Signature> set = new HashSet<Signature>();
Signature[] sigs = pm.getPackageInfo(pkg, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_SIGNATURES).signatures;
set.addAll(Arrays.asList(sigs));
sigSets.add(set);
} catch (NameNotFoundException e) {
Log.w("ServiceWatcher", pkg + " not found");
}
}
return sigSets;
}
use of android.content.pm.Signature in project android_frameworks_base by ParanoidAndroid.
the class PluginManager method containsPluginPermissionAndSignatures.
private static boolean containsPluginPermissionAndSignatures(PackageInfo pkgInfo) {
// check if the plugin has the required permissions
String[] permissions = pkgInfo.requestedPermissions;
if (permissions == null) {
return false;
}
boolean permissionOk = false;
for (String permit : permissions) {
if (PLUGIN_PERMISSION.equals(permit)) {
permissionOk = true;
break;
}
}
if (!permissionOk) {
return false;
}
// check to ensure the plugin is properly signed
Signature[] signatures = pkgInfo.signatures;
if (signatures == null) {
return false;
}
if (SystemProperties.getBoolean("ro.secure", false)) {
boolean signatureMatch = false;
for (Signature signature : signatures) {
for (int i = 0; i < SIGNATURES.length; i++) {
if (SIGNATURES[i].equals(signature)) {
signatureMatch = true;
break;
}
}
}
if (!signatureMatch) {
return false;
}
}
return true;
}
use of android.content.pm.Signature in project YourAppIdea by Michenux.
the class SecurityUtils method logHashKey.
public static String logHashKey(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
return Base64.encodeToString(md.digest(), Base64.DEFAULT);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(MCXApplication.LOG_TAG, "logHashKey error", e);
} catch (NoSuchAlgorithmException e) {
Log.e(MCXApplication.LOG_TAG, "logHashKey error", e);
}
return null;
}
use of android.content.pm.Signature in project Small by wequick.
the class BundleParser method verifyAndExtract.
public boolean verifyAndExtract(Bundle bundle, BundleExtractor extractor) {
WeakReference<byte[]> readBufferRef;
byte[] readBuffer = null;
synchronized (this.getClass()) {
readBufferRef = mReadBuffer;
if (readBufferRef != null) {
mReadBuffer = null;
readBuffer = readBufferRef.get();
}
if (readBuffer == null) {
readBuffer = new byte[8192];
readBufferRef = new WeakReference<byte[]>(readBuffer);
}
}
if (sHostCerts == null) {
// Collect host certificates
PackageManager pm = mContext.getPackageManager();
try {
Signature[] ss = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
if (ss != null) {
int N = ss.length;
sHostCerts = new byte[N][];
for (int i = 0; i < N; i++) {
sHostCerts[i] = ss[i].toByteArray();
}
}
} catch (PackageManager.NameNotFoundException ignored) {
}
}
byte[][] hostCerts = sHostCerts;
CrcVerifier crcVerifier = new CrcVerifier(mContext, bundle.getPackageName(), hostCerts);
try {
JarFile jarFile = new JarFile(mArchiveSourcePath);
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry je = (JarEntry) entries.nextElement();
if (je.isDirectory())
continue;
String name = je.getName();
if (name.startsWith("META-INF/"))
continue;
if (mLibDir != null && name.startsWith("lib/") && !name.startsWith(mLibDir)) {
// Ignore unused ABIs
continue;
}
// Verify CRC first
int hash = name.hashCode();
int crc = crcVerifier.getObscuredCrc(je.getCrc());
if (crcVerifier.verifyCrc(hash, crc)) {
continue;
}
// Verify certificates
Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
if (localCerts == null) {
Log.e(TAG, "Package " + mPackageName + " has no certificates at entry " + name + "; ignoring!");
crcVerifier.close();
jarFile.close();
return false;
} else {
// Ensure all certificates match.
for (int i = 0; i < hostCerts.length; i++) {
boolean found = false;
for (int j = 0; j < localCerts.length; j++) {
if (hostCerts[i] != null && Arrays.equals(hostCerts[i], localCerts[j].getEncoded())) {
found = true;
break;
}
}
if (!found || hostCerts.length != localCerts.length) {
Log.e(TAG, "Package " + mPackageName + " has mismatched certificates at entry " + name + "; ignoring!");
crcVerifier.close();
jarFile.close();
return false;
}
}
}
// Extract file if needed
File extractFile = extractor.getExtractFile(bundle, name);
if (extractFile != null) {
if (mZipFile == null) {
mZipFile = new ZipFile(mArchiveSourcePath);
}
postExtractFile(mZipFile, je, extractFile);
}
// Record the new crc
crcVerifier.recordCrc(hash, crc);
}
postSaveCrcs(crcVerifier);
jarFile.close();
synchronized (this.getClass()) {
mReadBuffer = readBufferRef;
}
} catch (CertificateEncodingException e) {
Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
return false;
} catch (IOException e) {
Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
return false;
} catch (RuntimeException e) {
Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
return false;
}
return true;
}
Aggregations