java.util.jar.JarFileクラスはJavaのjarファイルを扱うクラスであり,ZipFileクラスを継承している。jarファイルは実体はzipファイルであるので,当然の構成と言える
getManifest()メソッドは,jarファイルのManifestを取得する。entries()メソッドはjarファイルのエントリーをEnumeration<JarEntry>型で返す。また,getJarEntry(String name)メソッドは名前を指定してJarEntryを取得できる。
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util.jar;
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.JavaUtilZipFileAccess;
import sun.security.action.GetPropertyAction;
import sun.security.util.ManifestEntryVerifier;
import sun.security.util.SignatureFileVerifier;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public
class JarFile extends ZipFile {
private final static Runtime.Version BASE_VERSION;
private final static int BASE_VERSION_FEATURE;
private final static Runtime.Version RUNTIME_VERSION;
private final static boolean MULTI_RELEASE_ENABLED;
private final static boolean MULTI_RELEASE_FORCED;
private SoftReference<Manifest> manRef;
private JarEntry manEntry;
private JarVerifier jv;
private boolean jvInitialized;
private boolean verify;
private final Runtime.Version version; // current version
private final int versionFeature; // version.feature()
private boolean isMultiRelease; // is jar multi-release?
// indicates if Class-Path attribute present
private boolean hasClassPathAttribute;
// true if manifest checked for special attributes
private volatile boolean hasCheckedSpecialAttributes;
private static final JavaUtilZipFileAccess JUZFA;
static {
// Set up JavaUtilJarAccess in SharedSecrets
SharedSecrets.setJavaUtilJarAccess(new JavaUtilJarAccessImpl());
// Get JavaUtilZipFileAccess from SharedSecrets
JUZFA = SharedSecrets.getJavaUtilZipFileAccess();
// multi-release jar file versions >= 9
BASE_VERSION = Runtime.Version.parse(Integer.toString(8));
BASE_VERSION_FEATURE = BASE_VERSION.feature();
String jarVersion = GetPropertyAction.privilegedGetProperty("jdk.util.jar.version");
int runtimeVersion = Runtime.version().feature();
if (jarVersion != null) {
int jarVer = Integer.parseInt(jarVersion);
runtimeVersion = (jarVer > runtimeVersion)
? runtimeVersion
: Math.max(jarVer, BASE_VERSION_FEATURE);
}
RUNTIME_VERSION = Runtime.Version.parse(Integer.toString(runtimeVersion));
String enableMultiRelease = GetPropertyAction
.privilegedGetProperty("jdk.util.jar.enableMultiRelease", "true");
switch (enableMultiRelease) {
case "true":
default:
MULTI_RELEASE_ENABLED = true;
MULTI_RELEASE_FORCED = false;
break;
case "false":
MULTI_RELEASE_ENABLED = false;
MULTI_RELEASE_FORCED = false;
break;
case "force":
MULTI_RELEASE_ENABLED = true;
MULTI_RELEASE_FORCED = true;
break;
}
}
private static final String META_INF = "META-INF/";
private static final String META_INF_VERSIONS = META_INF + "versions/";
public static final String MANIFEST_NAME = META_INF + "MANIFEST.MF";
public static Runtime.Version baseVersion() {
return BASE_VERSION;
}
public static Runtime.Version runtimeVersion() {
return RUNTIME_VERSION;
}
public JarFile(String name) throws IOException {
this(new File(name), true, ZipFile.OPEN_READ);
}
public JarFile(String name, boolean verify) throws IOException {
this(new File(name), verify, ZipFile.OPEN_READ);
}
public JarFile(File file) throws IOException {
this(file, true, ZipFile.OPEN_READ);
}
public JarFile(File file, boolean verify) throws IOException {
this(file, verify, ZipFile.OPEN_READ);
}
public JarFile(File file, boolean verify, int mode) throws IOException {
this(file, verify, mode, BASE_VERSION);
}
public JarFile(File file, boolean verify, int mode, Runtime.Version version) throws IOException {
super(file, mode);
this.verify = verify;
Objects.requireNonNull(version);
if (MULTI_RELEASE_FORCED || version.feature() == RUNTIME_VERSION.feature()) {
// This deals with the common case where the value from JarFile.runtimeVersion() is passed
this.version = RUNTIME_VERSION;
} else if (version.feature() <= BASE_VERSION_FEATURE) {
// This also deals with the common case where the value from JarFile.baseVersion() is passed
this.version = BASE_VERSION;
} else {
// Canonicalize
this.version = Runtime.Version.parse(Integer.toString(version.feature()));
}
this.versionFeature = this.version.feature();
}
public final Runtime.Version getVersion() {
return isMultiRelease() ? this.version : BASE_VERSION;
}
public final boolean isMultiRelease() {
if (isMultiRelease) {
return true;
}
if (MULTI_RELEASE_ENABLED) {
try {
checkForSpecialAttributes();
} catch (IOException io) {
isMultiRelease = false;
}
}
return isMultiRelease;
}
public Manifest getManifest() throws IOException {
return getManifestFromReference();
}
private Manifest getManifestFromReference() throws IOException {
Manifest man = manRef != null ? manRef.get() : null;
if (man == null) {
JarEntry manEntry = getManEntry();
// If found then load the manifest
if (manEntry != null) {
if (verify) {
byte[] b = getBytes(manEntry);
if (!jvInitialized) {
jv = new JarVerifier(b);
}
man = new Manifest(jv, new ByteArrayInputStream(b), getName());
} else {
man = new Manifest(super.getInputStream(manEntry), getName());
}
manRef = new SoftReference<>(man);
}
}
return man;
}
private String[] getMetaInfEntryNames() {
return JUZFA.getMetaInfEntryNames((ZipFile)this);
}
public JarEntry getJarEntry(String name) {
return (JarEntry)getEntry(name);
}
public ZipEntry getEntry(String name) {
JarFileEntry je = getEntry0(name);
if (isMultiRelease()) {
return getVersionedEntry(name, je);
}
return je;
}
public Enumeration<JarEntry> entries() {
return JUZFA.entries(this, JarFileEntry::new);
}
public Stream<JarEntry> stream() {
return JUZFA.stream(this, JarFileEntry::new);
}
public Stream<JarEntry> versionedStream() {
if (isMultiRelease()) {
return JUZFA.entryNameStream(this).map(this::getBasename)
.filter(Objects::nonNull)
.distinct()
.map(this::getJarEntry)
.filter(Objects::nonNull);
}
return stream();
}
/*
* Invokes {@ZipFile}'s getEntry to Return a {@code JarFileEntry} for the
* given entry name or {@code null} if not found.
*/
private JarFileEntry getEntry0(String name) {
// Not using a lambda/method reference here to optimize startup time
Function<String, JarEntry> newJarFileEntryFn = new Function<>() {
@Override
public JarEntry apply(String name) {
return new JarFileEntry(name);
}
};
return (JarFileEntry)JUZFA.getEntry(this, name, newJarFileEntryFn);
}
private String getBasename(String name) {
if (name.startsWith(META_INF_VERSIONS)) {
int off = META_INF_VERSIONS.length();
int index = name.indexOf('/', off);
try {
// filter out dir META-INF/versions/ and META-INF/versions/*/
// and any entry with version > 'version'
if (index == -1 || index == (name.length() – 1) ||
Integer.parseInt(name, off, index, 10) > versionFeature) {
return null;
}
} catch (NumberFormatException x) {
return null; // remove malformed entries silently
}
// map to its base name
return name.substring(index + 1);
}
return name;
}
private JarEntry getVersionedEntry(String name, JarEntry je) {
if (BASE_VERSION_FEATURE < versionFeature) {
if (!name.startsWith(META_INF)) {
// search for versioned entry
int v = versionFeature;
while (v > BASE_VERSION_FEATURE) {
JarFileEntry vje = getEntry0(META_INF_VERSIONS + v + "/" + name);
if (vje != null) {
return vje.withBasename(name);
}
v–;
}
}
}
return je;
}
// placeholder for now
String getRealName(JarEntry entry) {
return entry.getRealName();
}
private class JarFileEntry extends JarEntry {
// 省略
}
/*
* Ensures that the JarVerifier has been created if one is
* necessary (i.e., the jar appears to be signed.) This is done as
* a quick check to avoid processing of the manifest for unsigned
* jars.
*/
private void maybeInstantiateVerifier() throws IOException {
if (jv != null) {
return;
}
if (verify) {
String[] names = getMetaInfEntryNames();
if (names != null) {
for (String nameLower : names) {
String name = nameLower.toUpperCase(Locale.ENGLISH);
if (name.endsWith(".DSA") ||
name.endsWith(".RSA") ||
name.endsWith(".EC") ||
name.endsWith(".SF")) {
// Assume since we found a signature-related file
// that the jar is signed and that we therefore
// need a JarVerifier and Manifest
getManifest();
return;
}
}
}
// No signature-related files; don't instantiate a
// verifier
verify = false;
}
}
/*
* Initializes the verifier object by reading all the manifest
* entries and passing them to the verifier.
*/
private void initializeVerifier() {
ManifestEntryVerifier mev = null;
// Verify "META-INF/" entries…
try {
String[] names = getMetaInfEntryNames();
if (names != null) {
for (String name : names) {
String uname = name.toUpperCase(Locale.ENGLISH);
if (MANIFEST_NAME.equals(uname)
|| SignatureFileVerifier.isBlockOrSF(uname)) {
JarEntry e = getJarEntry(name);
if (e == null) {
throw new JarException("corrupted jar file");
}
if (mev == null) {
mev = new ManifestEntryVerifier
(getManifestFromReference());
}
byte[] b = getBytes(e);
if (b != null && b.length > 0) {
jv.beginEntry(e, mev);
jv.update(b.length, b, 0, b.length, mev);
jv.update(-1, null, 0, 0, mev);
}
}
}
}
} catch (IOException ex) {
// if we had an error parsing any blocks, just
// treat the jar file as being unsigned
jv = null;
verify = false;
if (JarVerifier.debug != null) {
JarVerifier.debug.println("jarfile parsing error!");
ex.printStackTrace();
}
}
// if after initializing the verifier we have nothing
// signed, we null it out.
if (jv != null) {
jv.doneWithMeta();
if (JarVerifier.debug != null) {
JarVerifier.debug.println("done with meta!");
}
if (jv.nothingToVerify()) {
if (JarVerifier.debug != null) {
JarVerifier.debug.println("nothing to verify!");
}
jv = null;
verify = false;
}
}
}
/*
* Reads all the bytes for a given entry. Used to process the
* META-INF files.
*/
private byte[] getBytes(ZipEntry ze) throws IOException {
try (InputStream is = super.getInputStream(ze)) {
int len = (int)ze.getSize();
int bytesRead;
byte[] b;
// trust specified entry sizes when reasonably small
if (len != -1 && len <= 65535) {
b = new byte[len];
bytesRead = is.readNBytes(b, 0, len);
} else {
b = is.readAllBytes();
bytesRead = b.length;
}
if (len != -1 && len != bytesRead) {
throw new EOFException("Expected:" + len + ", read:" + bytesRead);
}
return b;
}
}
public synchronized InputStream getInputStream(ZipEntry ze)
throws IOException
{
maybeInstantiateVerifier();
if (jv == null) {
return super.getInputStream(ze);
}
if (!jvInitialized) {
initializeVerifier();
jvInitialized = true;
// could be set to null after a call to
// initializeVerifier if we have nothing to
// verify
if (jv == null)
return super.getInputStream(ze);
}
// wrap a verifier stream around the real stream
return new JarVerifier.VerifierStream(
getManifestFromReference(),
verifiableEntry(ze),
super.getInputStream(ze),
jv);
}
private JarEntry verifiableEntry(ZipEntry ze) {
if (ze instanceof JarFileEntry) {
// assure the name and entry match for verification
return ((JarFileEntry)ze).realEntry();
}
ze = getJarEntry(ze.getName());
if (ze instanceof JarFileEntry) {
return ((JarFileEntry)ze).realEntry();
}
return (JarEntry)ze;
}
// Statics for hand-coded Boyer-Moore search
private static final byte[] CLASSPATH_CHARS =
{'C','L','A','S','S','-','P','A','T','H', ':', ' '};
// The bad character shift for "class-path: "
private static final byte[] CLASSPATH_LASTOCC;
// The good suffix shift for "class-path: "
private static final byte[] CLASSPATH_OPTOSFT;
private static final byte[] MULTIRELEASE_CHARS =
{'M','U','L','T','I','-','R','E','L','E', 'A', 'S', 'E', ':',
' ', 'T', 'R', 'U', 'E'};
// The bad character shift for "multi-release: true"
private static final byte[] MULTIRELEASE_LASTOCC;
// The good suffix shift for "multi-release: true"
private static final byte[] MULTIRELEASE_OPTOSFT;
static {
CLASSPATH_LASTOCC = new byte[65];
CLASSPATH_OPTOSFT = new byte[12];
CLASSPATH_LASTOCC[(int)'C' – 32] = 1;
CLASSPATH_LASTOCC[(int)'L' – 32] = 2;
CLASSPATH_LASTOCC[(int)'S' – 32] = 5;
CLASSPATH_LASTOCC[(int)'-' – 32] = 6;
CLASSPATH_LASTOCC[(int)'P' – 32] = 7;
CLASSPATH_LASTOCC[(int)'A' – 32] = 8;
CLASSPATH_LASTOCC[(int)'T' – 32] = 9;
CLASSPATH_LASTOCC[(int)'H' – 32] = 10;
CLASSPATH_LASTOCC[(int)':' – 32] = 11;
CLASSPATH_LASTOCC[(int)' ' – 32] = 12;
for (int i = 0; i < 11; i++) {
CLASSPATH_OPTOSFT[i] = 12;
}
CLASSPATH_OPTOSFT[11] = 1;
MULTIRELEASE_LASTOCC = new byte[65];
MULTIRELEASE_OPTOSFT = new byte[19];
MULTIRELEASE_LASTOCC[(int)'M' – 32] = 1;
MULTIRELEASE_LASTOCC[(int)'I' – 32] = 5;
MULTIRELEASE_LASTOCC[(int)'-' – 32] = 6;
MULTIRELEASE_LASTOCC[(int)'L' – 32] = 9;
MULTIRELEASE_LASTOCC[(int)'A' – 32] = 11;
MULTIRELEASE_LASTOCC[(int)'S' – 32] = 12;
MULTIRELEASE_LASTOCC[(int)':' – 32] = 14;
MULTIRELEASE_LASTOCC[(int)' ' – 32] = 15;
MULTIRELEASE_LASTOCC[(int)'T' – 32] = 16;
MULTIRELEASE_LASTOCC[(int)'R' – 32] = 17;
MULTIRELEASE_LASTOCC[(int)'U' – 32] = 18;
MULTIRELEASE_LASTOCC[(int)'E' – 32] = 19;
for (int i = 0; i < 17; i++) {
MULTIRELEASE_OPTOSFT[i] = 19;
}
MULTIRELEASE_OPTOSFT[17] = 6;
MULTIRELEASE_OPTOSFT[18] = 1;
}
private JarEntry getManEntry() {
if (manEntry == null) {
// First look up manifest entry using standard name
JarEntry manEntry = getEntry0(MANIFEST_NAME);
if (manEntry == null) {
// If not found, then iterate through all the "META-INF/"
// entries to find a match.
String[] names = getMetaInfEntryNames();
if (names != null) {
for (String name : names) {
if (MANIFEST_NAME.equals(name.toUpperCase(Locale.ENGLISH))) {
manEntry = getEntry0(name);
break;
}
}
}
}
this.manEntry = manEntry;
}
return manEntry;
}
boolean hasClassPathAttribute() throws IOException {
checkForSpecialAttributes();
return hasClassPathAttribute;
}
private int match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft) {
int len = src.length;
int last = b.length – len;
int i = 0;
next:
while (i <= last) {
for (int j = (len – 1); j >= 0; j–) {
byte c = b[i + j];
if (c >= ' ' && c <= 'z') {
if (c >= 'a') c -= 32; // Canonicalize
if (c != src[j]) {
// no match
int badShift = lastOcc[c – 32];
i += Math.max(j + 1 – badShift, optoSft[j]);
continue next;
}
} else {
// no match, character not valid for name
i += len;
continue next;
}
}
return i;
}
return -1;
}
private void checkForSpecialAttributes() throws IOException {
if (hasCheckedSpecialAttributes) {
return;
}
synchronized (this) {
if (hasCheckedSpecialAttributes) {
return;
}
JarEntry manEntry = getManEntry();
if (manEntry != null) {
byte[] b = getBytes(manEntry);
hasClassPathAttribute = match(CLASSPATH_CHARS, b,
CLASSPATH_LASTOCC, CLASSPATH_OPTOSFT) != -1;
// is this a multi-release jar file
if (MULTI_RELEASE_ENABLED) {
int i = match(MULTIRELEASE_CHARS, b, MULTIRELEASE_LASTOCC,
MULTIRELEASE_OPTOSFT);
if (i != -1) {
// Read the main attributes of the manifest
byte[] lbuf = new byte[512];
Attributes attr = new Attributes();
attr.read(new Manifest.FastInputStream(
new ByteArrayInputStream(b)), lbuf);
isMultiRelease = Boolean.parseBoolean(
attr.getValue(Attributes.Name.MULTI_RELEASE));
}
}
}
hasCheckedSpecialAttributes = true;
}
}
synchronized void ensureInitialization() {
try {
maybeInstantiateVerifier();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (jv != null && !jvInitialized) {
initializeVerifier();
jvInitialized = true;
}
}
/*
* Returns a versioned {@code JarFileEntry} for the given entry,
* if there is one. Otherwise returns the original entry. This
* is invoked by the {@code entries2} for verifier.
*/
JarEntry newEntry(JarEntry je) {
if (isMultiRelease()) {
return getVersionedEntry(je.getName(), je);
}
return je;
}
/*
* Returns a versioned {@code JarFileEntry} for the given entry
* name, if there is one. Otherwise returns a {@code JarFileEntry}
* with the given name. It is invoked from JarVerifier's entries2
* for {@code singers}.
*/
JarEntry newEntry(String name) {
if (isMultiRelease()) {
JarEntry vje = getVersionedEntry(name, (JarEntry)null);
if (vje != null) {
return vje;
}
}
return new JarFileEntry(name);
}
Enumeration<String> entryNames(CodeSource[] cs) {
ensureInitialization();
if (jv != null) {
return jv.entryNames(this, cs);
}
/*
* JAR file has no signed content. Is there a non-signing
* code source?
*/
boolean includeUnsigned = false;
for (CodeSource c : cs) {
if (c.getCodeSigners() == null) {
includeUnsigned = true;
break;
}
}
if (includeUnsigned) {
return unsignedEntryNames();
} else {
return Collections.emptyEnumeration();
}
}
Enumeration<JarEntry> entries2() {
ensureInitialization();
if (jv != null) {
return jv.entries2(this, JUZFA.entries(JarFile.this,
JarFileEntry::new));
}
// screen out entries which are never signed
final var unfilteredEntries = JUZFA.entries(JarFile.this, JarFileEntry::new);
return new Enumeration<>() {
JarEntry entry;
public boolean hasMoreElements() {
if (entry != null) {
return true;
}
while (unfilteredEntries.hasMoreElements()) {
JarEntry je = unfilteredEntries.nextElement();
if (JarVerifier.isSigningRelated(je.getName())) {
continue;
}
entry = je;
return true;
}
return false;
}
public JarEntry nextElement() {
if (hasMoreElements()) {
JarEntry je = entry;
entry = null;
return newEntry(je);
}
throw new NoSuchElementException();
}
};
}
CodeSource[] getCodeSources(URL url) {
ensureInitialization();
if (jv != null) {
return jv.getCodeSources(this, url);
}
/*
* JAR file has no signed content. Is there a non-signing
* code source?
*/
Enumeration<String> unsigned = unsignedEntryNames();
if (unsigned.hasMoreElements()) {
return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
} else {
return null;
}
}
private Enumeration<String> unsignedEntryNames() {
final Enumeration<JarEntry> entries = entries();
return new Enumeration<>() {
String name;
/*
* Grab entries from ZIP directory but screen out
* metadata.
*/
public boolean hasMoreElements() {
if (name != null) {
return true;
}
while (entries.hasMoreElements()) {
String value;
ZipEntry e = entries.nextElement();
value = e.getName();
if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
continue;
}
name = value;
return true;
}
return false;
}
public String nextElement() {
if (hasMoreElements()) {
String value = name;
name = null;
return value;
}
throw new NoSuchElementException();
}
};
}
CodeSource getCodeSource(URL url, String name) {
ensureInitialization();
if (jv != null) {
if (jv.eagerValidation) {
CodeSource cs = null;
JarEntry je = getJarEntry(name);
if (je != null) {
cs = jv.getCodeSource(url, this, je);
} else {
cs = jv.getCodeSource(url, name);
}
return cs;
} else {
return jv.getCodeSource(url, name);
}
}
return JarVerifier.getUnsignedCS(url);
}
void setEagerValidation(boolean eager) {
try {
maybeInstantiateVerifier();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (jv != null) {
jv.setEagerValidation(eager);
}
}
List<Object> getManifestDigests() {
ensureInitialization();
if (jv != null) {
return jv.getManifestDigests();
}
return new ArrayList<>();
}
}