Example usage for android.util.jar StrictJarFile StrictJarFile

List of usage examples for android.util.jar StrictJarFile StrictJarFile

Introduction

In this page you can find the example usage for android.util.jar StrictJarFile StrictJarFile.

Prototype

public StrictJarFile(FileDescriptor fd) throws IOException, SecurityException 

Source Link

Usage

From source file:android.content.pm.PackageParser.java

/**
 * 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.
 *//*w w  w . j  a  v a 2  s  .c o  m*/
public void collectManifestDigest(Package pkg) throws PackageParserException {
    pkg.manifestDigest = null;

    // TODO: extend to gather digest for split APKs
    try {
        final StrictJarFile jarFile = new StrictJarFile(pkg.baseCodePath);
        try {
            final ZipEntry je = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
            if (je != null) {
                pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
            }
        } finally {
            jarFile.close();
        }
    } catch (IOException | RuntimeException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
                "Failed to collect manifest digest");
    }
}

From source file:android.content.pm.PackageParser.java

private static void collectCertificates(Package pkg, File apkFile, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();

    StrictJarFile jarFile = null;/*from  w  w  w.  j  a  v  a  2  s. c om*/
    try {
        jarFile = new StrictJarFile(apkPath);

        // Always verify manifest, regardless of source
        final ZipEntry manifestEntry = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
        if (manifestEntry == null) {
            throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
                    "Package " + apkPath + " has no manifest");
        }

        final List<ZipEntry> toVerify = new ArrayList<>();
        toVerify.add(manifestEntry);

        // If we're parsing an untrusted package, verify all contents
        if ((flags & PARSE_IS_SYSTEM) == 0) {
            final Iterator<ZipEntry> i = jarFile.iterator();
            while (i.hasNext()) {
                final ZipEntry entry = i.next();

                if (entry.isDirectory())
                    continue;
                if (entry.getName().startsWith("META-INF/"))
                    continue;
                if (entry.getName().equals(ANDROID_MANIFEST_FILENAME))
                    continue;

                toVerify.add(entry);
            }
        }

        // Verify that entries are signed consistently with the first entry
        // we encountered. Note that for splits, certificates may have
        // already been populated during an earlier parse of a base APK.
        for (ZipEntry entry : toVerify) {
            final Certificate[][] entryCerts = loadCertificates(jarFile, entry);
            if (ArrayUtils.isEmpty(entryCerts)) {
                throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
                        "Package " + apkPath + " has no certificates at entry " + entry.getName());
            }
            final Signature[] entrySignatures = convertToSignatures(entryCerts);

            if (pkg.mCertificates == null) {
                pkg.mCertificates = entryCerts;
                pkg.mSignatures = entrySignatures;
                pkg.mSigningKeys = new ArraySet<PublicKey>();
                for (int i = 0; i < entryCerts.length; i++) {
                    pkg.mSigningKeys.add(entryCerts[i][0].getPublicKey());
                }
            } else {
                if (!Signature.areExactMatch(pkg.mSignatures, entrySignatures)) {
                    throw new PackageParserException(INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES,
                            "Package " + apkPath + " has mismatched certificates at entry " + entry.getName());
                }
            }
        }
    } catch (GeneralSecurityException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING,
                "Failed to collect certificates from " + apkPath, e);
    } catch (IOException | RuntimeException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
                "Failed to collect certificates from " + apkPath, e);
    } finally {
        closeQuietly(jarFile);
    }
}