home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.5 KB | 300 lines |
- /*
- * @(#)JarVerifier.java 1.15 98/03/18
- *
- * Copyright 1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.util.jar;
-
- import java.io.*;
- import java.util.*;
- import java.util.zip.*;
- import java.security.*;
-
- import sun.security.tools.ManifestDigester;
- import sun.security.tools.ManifestEntryVerifier;
- import sun.security.tools.SignatureFileVerifier;
-
- /**
- *
- * @version 1.15 98/03/18
- * @author Roland Schemers
- */
- class JarVerifier {
-
- /* Are we debugging ? */
- private static final boolean debug = false;
-
- /* The current JAR entry */
- private JarEntry currentEntry;
-
- /* a table mapping names to identities for entries that have
- had their actual hashes verified */
- private Hashtable verifiedIdentities;
-
- /* a table mapping names to identities for entries that have
- passed the .SF/.DSA -> MANIFEST check */
- private Hashtable sigFileIdentities;
-
- /* a hash table to hold .SF bytes */
- private Hashtable sigFileData;
-
- /** "queue" of pending PKCS7 blocks that we couldn't parse
- * until we parsed the .SF file */
- private ArrayList pendingBlocks;
-
- /* Are we parsing a block? */
- private boolean parsingBlockOrSF = false;
-
- /* Are we done parsing META-INF entries? */
- private boolean parsingMeta = true;
-
- /* Has this entry been processed yet? */
- private boolean processed = false;
-
- /* The manifest file */
- private Manifest manifest;
-
- /* The output stream to use when keeping track of files we are interested
- in */
- private ByteArrayOutputStream baos;
-
- /* The JarEntryVerifier for the current entry */
- ManifestEntryVerifier mev;
-
- /* the uppercased version of the current entry's name */
- String uname;
-
- /** The ManifestDigester object */
- ManifestDigester manDig;
-
- /**
- */
- public JarVerifier(Manifest manifest, byte rawBytes[]) {
- manDig = new ManifestDigester(rawBytes);
- mev = new ManifestEntryVerifier(manifest);
- sigFileIdentities = new Hashtable();
- verifiedIdentities = new Hashtable();
- sigFileData = new Hashtable();
- pendingBlocks = new ArrayList();
- baos = new ByteArrayOutputStream();
- this.manifest = manifest;
- }
-
- /**
- * This method scans to see which entry we're parsing and
- * keeps various state information depending on what type of
- * file is being parsed.
- */
- public void beginEntry(JarEntry je) throws IOException {
- currentEntry = je;
-
- if (currentEntry == null)
- return;
-
- if (debug) {
- System.out.println("beginEntry "+je.getName());
- }
-
- processed = false;
- String name = currentEntry.getName();
- uname = name.toUpperCase();
-
- /*
- * Assumptions:
- * 1. The manifest should be the first entry in the META-INF directory.
- * 2. The .SF/.DSA files follow the manifest, before any normal entries
- * 3. Any of the following will throw a SecurityException:
- * a. digest mismatch between a manifest section and
- * the SF section.
- * b. digest mismatch between the actual jar entry and the manifest
- */
-
- if (parsingMeta &&
- (uname.startsWith("META-INF/") || uname.startsWith("/META-INF/"))) {
-
- if (currentEntry.isDirectory()) {
- mev.setEntry(null);
- return;
- }
-
- if (uname.endsWith(".DSA") || uname.endsWith(".RSA") ||
- uname.endsWith(".SF")) {
- /* We parse only DSA or RSA PKCS7 blocks. */
- parsingBlockOrSF = true;
- baos.reset();
- mev.setEntry(null);
- }
- return;
- }
-
- if (parsingMeta)
- parsingMeta = false;
-
- if (currentEntry.isDirectory()) {
- mev.setEntry(null);
- return;
-
- }
-
- // be liberal in what you accept. If the name starts with ./, remove
- // it as we internally canonicalize it with out the ./.
- if (name.startsWith("./"))
- name = name.substring(2);
-
- // be liberal in what you accept. If the name starts with /, remove
- // it as we internally canonicalize it with out the /.
- if (name.startsWith("/"))
- name = name.substring(1);
-
- // only set the jev object for entries that have a signature
- if (sigFileIdentities.get(name) != null) {
- mev.setEntry(name);
- return;
- }
-
- // don't compute the digest for this entry
- mev.setEntry(null);
-
- return;
- }
-
- /**
- * update a single byte.
- */
-
- public void update(int b) throws IOException {
- if (b != -1) {
- if (parsingBlockOrSF) {
- baos.write(b);
- } else {
- mev.update((byte)b);
- }
- } else {
- processEntry();
- }
- }
-
- /**
- * update an array of bytes.
- */
-
- public void update(int n, byte[] b, int off, int len) throws IOException {
- if (n != -1) {
- if (parsingBlockOrSF) {
- baos.write(b, off, n);
- } else {
- mev.update(b, off, n);
- }
- } else {
- processEntry();
- }
- }
-
- /**
- * called when we reach the end of entry in one of the read() methods.
- */
- private void processEntry()
- throws IOException
- {
- if (processed) return;
-
- processed = true;
-
- if (!parsingBlockOrSF) {
- currentEntry.ids =
- mev.verify(verifiedIdentities, sigFileIdentities);
- } else {
-
- try {
- parsingBlockOrSF = false;
-
- if (debug) {
- System.out.println("processEntry: processing block");
- }
-
- if (uname.endsWith(".SF")) {
- String key = uname.substring(0, uname.length()-3);
- byte bytes[] = baos.toByteArray();
- // add to sigFileData in case future blocks need it
- sigFileData.put(key, bytes);
- // check pending blocks, we can now process
- // anyone waiting for this .SF file
- Iterator it = pendingBlocks.iterator();
- while (it.hasNext()) {
- SignatureFileVerifier sfv =
- (SignatureFileVerifier) it.next();
- if (sfv.needSignatureFile(key)) {
- if (debug) {
- System.out.println(
- "processEntry: processing pending block");
- }
-
- sfv.setSignatureFile(bytes);
- sfv.process(sigFileIdentities);
- }
- }
- return;
- }
-
- // now we are parsing a signature block file
-
- String key = uname.substring(0, uname.lastIndexOf("."));
-
- SignatureFileVerifier sfv =
- new SignatureFileVerifier(manDig, uname, baos.toByteArray());
-
- if (sfv.needSignatureFileBytes()) {
- // see if we have already parsed an external .SF file
- byte[] bytes = (byte[]) sigFileData.get(key);
-
- if (bytes == null) {
- // put this block on queue for later processing
- // since we don't have the .SF bytes yet
- // (uname, block);
- if (debug) {
- System.out.println("adding pending block");
- }
- pendingBlocks.add(sfv);
- return;
- } else {
- sfv.setSignatureFile(bytes);
- }
- }
- sfv.process(sigFileIdentities);
-
- } catch (sun.security.pkcs.ParsingException pe) {
- if (debug) System.err.println("processEntry caught: "+pe);
- // ignore and treat as unsigned
- } catch (IOException ioe) {
- if (debug) System.err.println("processEntry caught: "+ioe);
- // ignore and treat as unsigned
- } catch (SignatureException se) {
- if (debug) System.err.println("processEntry caught: "+se);
- // ignore and treat as unsigned
- } catch (NoSuchAlgorithmException nsae) {
- if (debug) System.err.println("processEntry caught: "+nsae);
- // ignore and treat as unsigned
- }
- }
- }
-
- /**
- * return an array of Identity objects for the given file in the jar.
- * this array is not cloned.
- *
- */
- public Identity[] getIdentities(String name)
- {
- return (Identity[])verifiedIdentities.get(name);
- }
- }
-