Search in sources :

Example 41 with JarFile

use of java.util.jar.JarFile in project jdk8u_jdk by JetBrains.

the class TimestampCheck method checkTimestamp.

static void checkTimestamp(String file, String policyId, String digestAlg) throws Exception {
    try (JarFile jf = new JarFile(file)) {
        JarEntry je = jf.getJarEntry("META-INF/OLD.RSA");
        try (InputStream is = jf.getInputStream(je)) {
            byte[] content = IOUtils.readFully(is, -1, true);
            PKCS7 p7 = new PKCS7(content);
            SignerInfo[] si = p7.getSignerInfos();
            if (si == null || si.length == 0) {
                throw new Exception("Not signed");
            }
            PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes().getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
            PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
            TimestampToken tt = new TimestampToken(tsToken.getContentInfo().getData());
            if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
                throw new Exception("Digest alg different");
            }
            if (!tt.getPolicyID().equals(policyId)) {
                throw new Exception("policyId different");
            }
        }
    }
}
Also used : SignerInfo(sun.security.pkcs.SignerInfo) PKCS9Attribute(sun.security.pkcs.PKCS9Attribute) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PKCS7(sun.security.pkcs.PKCS7) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException) TimestampToken(sun.security.timestamp.TimestampToken)

Example 42 with JarFile

use of java.util.jar.JarFile in project jdk8u_jdk by JetBrains.

the class TestNormal method main.

public static void main(String[] args) throws Exception {
    Properties p = System.getProperties();
    String java_home = p.getProperty("test.jdk");
    String dtjar = java_home + File.separator + "lib" + File.separator + "dt.jar";
    File folder = new File("dt");
    if (folder.exists()) {
        delete(folder);
    }
    folder.mkdir();
    try {
        extractJar(new JarFile(dtjar), folder);
        execJavaCommand(java_home, "jar cnf normalized.jar -C dt .");
        execJavaCommand(java_home, "jar cf original.jar -C dt .");
        execJavaCommand(java_home, "pack200 -r repacked.jar original.jar");
        compareJars(new JarFile("normalized.jar"), new JarFile("repacked.jar"));
    } finally {
        String[] cleanupList = { "dt", "normalized.jar", "original.jar", "repacked.jar" };
        for (String s : cleanupList) {
            delete(new File(s));
        }
    }
}
Also used : Properties(java.util.Properties) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile)

Example 43 with JarFile

use of java.util.jar.JarFile in project jdk8u_jdk by JetBrains.

the class TestNormal method extractJar.

public static void extractJar(JarFile jf, File where) throws Exception {
    for (JarEntry file : Collections.list(jf.entries())) {
        File out = new File(where, file.getName());
        if (file.isDirectory()) {
            out.mkdirs();
            continue;
        }
        File parent = out.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = jf.getInputStream(file);
            os = new FileOutputStream(out);
            while (is.available() > 0) {
                os.write(is.read());
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
    }
}
Also used : JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile)

Example 44 with JarFile

use of java.util.jar.JarFile in project jdk8u_jdk by JetBrains.

the class SorryClosed method main.

public static void main(String[] args) throws IOException {
    File file = new File(System.getProperty("test.src", "."), "test.jar");
    String testEntryName = "test.class";
    try {
        JarFile f = new JarFile(file);
        ZipEntry e = f.getEntry(testEntryName);
        f.close();
        f.getInputStream(e);
    }// OK
     catch (IllegalStateException e) {
    }
    try {
        JarFile f = new JarFile(file);
        f.close();
        f.getEntry(testEntryName);
    }// OK
     catch (IllegalStateException e) {
    }
    try {
        JarFile f = new JarFile(file);
        f.close();
        f.getJarEntry(testEntryName);
    }// OK
     catch (IllegalStateException e) {
    }
    try {
        JarFile f = new JarFile(file);
        f.close();
        f.getManifest();
    }// OK
     catch (IllegalStateException e) {
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 45 with JarFile

use of java.util.jar.JarFile 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;
}
Also used : Enumeration(java.util.Enumeration) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) PackageManager(android.content.pm.PackageManager) ZipFile(java.util.zip.ZipFile) Signature(android.content.pm.Signature) RandomAccessFile(java.io.RandomAccessFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Certificate(java.security.cert.Certificate)

Aggregations

JarFile (java.util.jar.JarFile)1366 File (java.io.File)720 JarEntry (java.util.jar.JarEntry)616 IOException (java.io.IOException)587 URL (java.net.URL)272 InputStream (java.io.InputStream)271 ZipEntry (java.util.zip.ZipEntry)203 Manifest (java.util.jar.Manifest)186 ArrayList (java.util.ArrayList)158 Test (org.junit.Test)131 JarURLConnection (java.net.JarURLConnection)123 FileOutputStream (java.io.FileOutputStream)122 ZipFile (java.util.zip.ZipFile)121 FileInputStream (java.io.FileInputStream)111 Attributes (java.util.jar.Attributes)110 MalformedURLException (java.net.MalformedURLException)94 Enumeration (java.util.Enumeration)67 JarOutputStream (java.util.jar.JarOutputStream)65 HashSet (java.util.HashSet)58 URLClassLoader (java.net.URLClassLoader)55