0N/A/*
2362N/A * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.jar;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.security.*;
0N/A
0N/Aimport sun.net.www.MessageHeader;
0N/Aimport sun.misc.BASE64Encoder;
0N/Aimport sun.misc.BASE64Decoder;
0N/A
0N/Aimport sun.security.pkcs.*;
1786N/Aimport sun.security.x509.AlgorithmId;
0N/A
0N/A/**
0N/A * <p>A signature file as defined in the <a
0N/A * href="manifest.html">Manifest and Signature Format</a>. It has
0N/A * essentially the same structure as a Manifest file in that it is a
0N/A * set of RFC 822 headers (sections). The first section contains meta
0N/A * data relevant to the entire file (i.e "Signature-Version:1.0") and
0N/A * each subsequent section contains data relevant to specific entries:
0N/A * entry sections.
0N/A *
0N/A * <p>Each entry section contains the name of an entry (which must
0N/A * have a counterpart in the manifest). Like the manifest it contains
0N/A * a hash, the hash of the manifest section correspondind to the
0N/A * name. Since the manifest entry contains the hash of the data, this
0N/A * is equivalent to a signature of the data, plus the attributes of
0N/A * the manifest entry.
0N/A *
0N/A * <p>This signature file format deal with PKCS7 encoded DSA signature
0N/A * block. It should be straightforward to extent to support other
0N/A * algorithms.
0N/A *
0N/A * @author David Brown
0N/A * @author Benjamin Renaud */
0N/A
0N/Apublic class SignatureFile {
0N/A
0N/A /* Are we debugging? */
0N/A static final boolean debug = false;
0N/A
0N/A /* list of headers that all pertain to a particular file in the
0N/A * archive */
0N/A private Vector entries = new Vector();
0N/A
0N/A /* Right now we only support SHA hashes */
0N/A static final String[] hashes = {"SHA"};
0N/A
0N/A static final void debug(String s) {
0N/A if (debug)
0N/A System.out.println("sig> " + s);
0N/A }
0N/A
0N/A /*
0N/A * The manifest we're working with. */
0N/A private Manifest manifest;
0N/A
0N/A /*
0N/A * The file name for the file. This is the raw name, i.e. the
0N/A * extention-less 8 character name (such as MYSIGN) which wil be
0N/A * used to build the signature filename (MYSIGN.SF) and the block
0N/A * filename (MYSIGN.DSA) */
0N/A private String rawName;
0N/A
0N/A /* The digital signature block corresponding to this signature
0N/A * file. */
0N/A private PKCS7 signatureBlock;
0N/A
0N/A
0N/A /**
0N/A * Private constructor which takes a name a given signature
0N/A * file. The name must be extension-less and less or equal to 8
0N/A * character in length. */
0N/A private SignatureFile(String name) throws JarException {
0N/A
0N/A entries = new Vector();
0N/A
0N/A if (name != null) {
0N/A if (name.length() > 8 || name.indexOf('.') != -1) {
0N/A throw new JarException("invalid file name");
0N/A }
1786N/A rawName = name.toUpperCase(Locale.ENGLISH);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Private constructor which takes a name a given signature file
0N/A * and a new file predicate. If it is a new file, a main header
0N/A * will be added. */
0N/A private SignatureFile(String name, boolean newFile)
0N/A throws JarException {
0N/A
0N/A this(name);
0N/A
0N/A if (newFile) {
0N/A MessageHeader globals = new MessageHeader();
0N/A globals.set("Signature-Version", "1.0");
0N/A entries.addElement(globals);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new Signature file corresponding to a given
0N/A * Manifest. All entries in the manifest are signed.
0N/A *
0N/A * @param manifest the manifest to use.
0N/A *
0N/A * @param name for this signature file. This should
0N/A * be less than 8 characters, and without a suffix (i.e.
0N/A * without a period in it.
0N/A *
0N/A * @exception JarException if an invalid name is passed in.
0N/A */
0N/A public SignatureFile(Manifest manifest, String name)
0N/A throws JarException {
0N/A
0N/A this(name, true);
0N/A
0N/A this.manifest = manifest;
0N/A Enumeration enum_ = manifest.entries();
0N/A while (enum_.hasMoreElements()) {
0N/A MessageHeader mh = (MessageHeader)enum_.nextElement();
0N/A String entryName = mh.findValue("Name");
0N/A if (entryName != null) {
0N/A add(entryName);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new Signature file corresponding to a given
0N/A * Manifest. Specific entries in the manifest are signed.
0N/A *
0N/A * @param manifest the manifest to use.
0N/A *
0N/A * @param entries the entries to sign.
0N/A *
0N/A * @param filename for this signature file. This should
0N/A * be less than 8 characters, and without a suffix (i.e.
0N/A * without a period in it.
0N/A *
0N/A * @exception JarException if an invalid name is passed in.
0N/A */
0N/A public SignatureFile(Manifest manifest, String[] entries,
0N/A String filename)
0N/A throws JarException {
0N/A this(filename, true);
0N/A this.manifest = manifest;
0N/A add(entries);
0N/A }
0N/A
0N/A /**
0N/A * Construct a Signature file from an input stream.
0N/A *
0N/A * @exception IOException if an invalid name is passed in or if a
0N/A * stream exception occurs.
0N/A */
0N/A public SignatureFile(InputStream is, String filename)
0N/A throws IOException {
0N/A this(filename);
0N/A while (is.available() > 0) {
0N/A MessageHeader m = new MessageHeader(is);
0N/A entries.addElement(m);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Construct a Signature file from an input stream.
0N/A *
0N/A * @exception IOException if an invalid name is passed in or if a
0N/A * stream exception occurs.
0N/A */
0N/A public SignatureFile(InputStream is) throws IOException {
0N/A this(is, null);
0N/A }
0N/A
0N/A public SignatureFile(byte[] bytes) throws IOException {
0N/A this(new ByteArrayInputStream(bytes));
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the signature file, ending with a ".SF"
0N/A * suffix */
0N/A public String getName() {
0N/A return "META-INF/" + rawName + ".SF";
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the block file, ending with a block suffix
0N/A * such as ".DSA". */
0N/A public String getBlockName() {
0N/A String suffix = "DSA";
0N/A if (signatureBlock != null) {
0N/A SignerInfo info = signatureBlock.getSignerInfos()[0];
0N/A suffix = info.getDigestEncryptionAlgorithmId().getName();
1786N/A String temp = AlgorithmId.getEncAlgFromSigAlg(suffix);
1786N/A if (temp != null) suffix = temp;
0N/A }
0N/A return "META-INF/" + rawName + "." + suffix;
0N/A }
0N/A
0N/A /**
0N/A * Returns the signature block associated with this file.
0N/A */
0N/A public PKCS7 getBlock() {
0N/A return signatureBlock;
0N/A }
0N/A
0N/A /**
0N/A * Sets the signature block associated with this file.
0N/A */
0N/A public void setBlock(PKCS7 block) {
0N/A this.signatureBlock = block;
0N/A }
0N/A
0N/A /**
0N/A * Add a set of entries from the current manifest.
0N/A */
0N/A public void add(String[] entries) throws JarException {
0N/A for (int i = 0; i < entries.length; i++) {
0N/A add (entries[i]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add a specific entry from the current manifest.
0N/A */
0N/A public void add(String entry) throws JarException {
0N/A MessageHeader mh = manifest.getEntry(entry);
0N/A if (mh == null) {
0N/A throw new JarException("entry " + entry + " not in manifest");
0N/A }
0N/A MessageHeader smh;
0N/A try {
0N/A smh = computeEntry(mh);
0N/A } catch (IOException e) {
0N/A throw new JarException(e.getMessage());
0N/A }
0N/A entries.addElement(smh);
0N/A }
0N/A
0N/A /**
0N/A * Get the entry corresponding to a given name. Returns null if
0N/A *the entry does not exist.
0N/A */
0N/A public MessageHeader getEntry(String name) {
0N/A Enumeration enum_ = entries();
0N/A while(enum_.hasMoreElements()) {
0N/A MessageHeader mh = (MessageHeader)enum_.nextElement();
0N/A if (name.equals(mh.findValue("Name"))) {
0N/A return mh;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the n-th entry. The global header is a entry 0. */
0N/A public MessageHeader entryAt(int n) {
0N/A return (MessageHeader) entries.elementAt(n);
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of the entries.
0N/A */
0N/A public Enumeration entries() {
0N/A return entries.elements();
0N/A }
0N/A
0N/A /**
0N/A * Given a manifest entry, computes the signature entry for this
0N/A * manifest entry.
0N/A */
0N/A private MessageHeader computeEntry(MessageHeader mh) throws IOException {
0N/A MessageHeader smh = new MessageHeader();
0N/A
0N/A String name = mh.findValue("Name");
0N/A if (name == null) {
0N/A return null;
0N/A }
0N/A smh.set("Name", name);
0N/A
0N/A BASE64Encoder encoder = new BASE64Encoder();
0N/A try {
0N/A for (int i = 0; i < hashes.length; ++i) {
0N/A MessageDigest dig = getDigest(hashes[i]);
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A PrintStream ps = new PrintStream(baos);
0N/A mh.print(ps);
0N/A byte[] headerBytes = baos.toByteArray();
0N/A byte[] digest = dig.digest(headerBytes);
0N/A smh.set(hashes[i] + "-Digest", encoder.encode(digest));
0N/A }
0N/A return smh;
0N/A } catch (NoSuchAlgorithmException e) {
0N/A throw new JarException(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A private Hashtable digests = new Hashtable();
0N/A
0N/A private MessageDigest getDigest(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A MessageDigest dig = (MessageDigest)digests.get(algorithm);
0N/A if (dig == null) {
0N/A dig = MessageDigest.getInstance(algorithm);
0N/A digests.put(algorithm, dig);
0N/A }
0N/A dig.reset();
0N/A return dig;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Add a signature file at current position in a stream
0N/A */
0N/A public void stream(OutputStream os) throws IOException {
0N/A
0N/A /* the first header in the file should be the global one.
0N/A * It should say "SignatureFile-Version: x.x"; barf if not
0N/A */
0N/A MessageHeader globals = (MessageHeader) entries.elementAt(0);
0N/A if (globals.findValue("Signature-Version") == null) {
0N/A throw new JarException("Signature file requires " +
0N/A "Signature-Version: 1.0 in 1st header");
0N/A }
0N/A
0N/A PrintStream ps = new PrintStream(os);
0N/A globals.print(ps);
0N/A
0N/A for (int i = 1; i < entries.size(); ++i) {
0N/A MessageHeader mh = (MessageHeader) entries.elementAt(i);
0N/A mh.print(ps);
0N/A }
0N/A }
0N/A}