resolved conflicts for merge of 0afa8b36 to master
Change-Id: I09a453ab5dbad58e3dc0858972e222a34d53b282
This commit is contained in:
@@ -18,10 +18,17 @@ package android.content.pm;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.Base64;
|
||||
import android.util.Slog;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.jar.Attributes;
|
||||
|
||||
import libcore.io.IoUtils;
|
||||
|
||||
/**
|
||||
* Represents the manifest digest for a package. This is suitable for comparison
|
||||
@@ -30,17 +37,17 @@ import java.util.jar.Attributes;
|
||||
* @hide
|
||||
*/
|
||||
public class ManifestDigest implements Parcelable {
|
||||
private static final String TAG = "ManifestDigest";
|
||||
|
||||
/** The digest of the manifest in our preferred order. */
|
||||
private final byte[] mDigest;
|
||||
|
||||
/** Digest field names to look for in preferred order. */
|
||||
private static final String[] DIGEST_TYPES = {
|
||||
"SHA1-Digest", "SHA-Digest", "MD5-Digest",
|
||||
};
|
||||
|
||||
/** What we print out first when toString() is called. */
|
||||
private static final String TO_STRING_PREFIX = "ManifestDigest {mDigest=";
|
||||
|
||||
/** Digest algorithm to use. */
|
||||
private static final String DIGEST_ALGORITHM = "SHA-256";
|
||||
|
||||
ManifestDigest(byte[] digest) {
|
||||
mDigest = digest;
|
||||
}
|
||||
@@ -49,26 +56,32 @@ public class ManifestDigest implements Parcelable {
|
||||
mDigest = source.createByteArray();
|
||||
}
|
||||
|
||||
static ManifestDigest fromAttributes(Attributes attributes) {
|
||||
if (attributes == null) {
|
||||
static ManifestDigest fromInputStream(InputStream fileIs) {
|
||||
if (fileIs == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String encodedDigest = null;
|
||||
final MessageDigest md;
|
||||
try {
|
||||
md = MessageDigest.getInstance(DIGEST_ALGORITHM);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(DIGEST_ALGORITHM + " must be available", e);
|
||||
}
|
||||
|
||||
for (int i = 0; i < DIGEST_TYPES.length; i++) {
|
||||
final String value = attributes.getValue(DIGEST_TYPES[i]);
|
||||
if (value != null) {
|
||||
encodedDigest = value;
|
||||
break;
|
||||
final DigestInputStream dis = new DigestInputStream(new BufferedInputStream(fileIs), md);
|
||||
try {
|
||||
byte[] readBuffer = new byte[8192];
|
||||
while (dis.read(readBuffer, 0, readBuffer.length) != -1) {
|
||||
// not using
|
||||
}
|
||||
}
|
||||
|
||||
if (encodedDigest == null) {
|
||||
} catch (IOException e) {
|
||||
Slog.w(TAG, "Could not read manifest");
|
||||
return null;
|
||||
} finally {
|
||||
IoUtils.closeQuietly(dis);
|
||||
}
|
||||
|
||||
final byte[] digest = Base64.decode(encodedDigest, Base64.DEFAULT);
|
||||
final byte[] digest = md.digest();
|
||||
return new ManifestDigest(digest);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.PatternMatcher;
|
||||
@@ -60,10 +59,9 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import com.android.internal.util.XmlUtils;
|
||||
|
||||
@@ -573,6 +571,28 @@ public class PackageParser {
|
||||
return pkg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gathers the {@link ManifestDigest} for {@code pkg} if it exists in the
|
||||
* APK. If it successfully scanned the package and found the
|
||||
* {@code AndroidManifest.xml}, {@code true} is returned.
|
||||
*/
|
||||
public boolean collectManifestDigest(Package pkg) {
|
||||
try {
|
||||
final JarFile jarFile = new JarFile(mArchiveSourcePath);
|
||||
try {
|
||||
final ZipEntry je = jarFile.getEntry(ANDROID_MANIFEST_FILENAME);
|
||||
if (je != null) {
|
||||
pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
|
||||
}
|
||||
} finally {
|
||||
jarFile.close();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean collectCertificates(Package pkg, int flags) {
|
||||
pkg.mSignatures = null;
|
||||
|
||||
@@ -624,7 +644,6 @@ public class PackageParser {
|
||||
}
|
||||
} else {
|
||||
Enumeration<JarEntry> entries = jarFile.entries();
|
||||
final Manifest manifest = jarFile.getManifest();
|
||||
while (entries.hasMoreElements()) {
|
||||
final JarEntry je = entries.nextElement();
|
||||
if (je.isDirectory()) continue;
|
||||
@@ -635,8 +654,8 @@ public class PackageParser {
|
||||
continue;
|
||||
|
||||
if (ANDROID_MANIFEST_FILENAME.equals(name)) {
|
||||
final Attributes attributes = manifest.getAttributes(name);
|
||||
pkg.manifestDigest = ManifestDigest.fromAttributes(attributes);
|
||||
pkg.manifestDigest =
|
||||
ManifestDigest.fromInputStream(jarFile.getInputStream(je));
|
||||
}
|
||||
|
||||
final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
|
||||
|
||||
Reference in New Issue
Block a user