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");
}
}
}
}
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));
}
}
}
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();
}
}
}
}
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) {
}
}
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;
}
Aggregations