jdk/src/jdk.jartool/share/classes/sun/tools/jar/SignatureFile.java
author prr
Tue, 09 Aug 2016 16:20:02 -0700
changeset 40435 553eb1a50733
parent 29369 8f9f8aa1e8e9
permissions -rw-r--r--
8163238: Upgrade to harfbuzz 1.3.0 in JDK 9 Reviewed-by: vadim, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16020
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.www.MessageHeader;
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14342
diff changeset
    33
import java.util.Base64;
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14342
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.pkcs.*;
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
    37
import sun.security.x509.AlgorithmId;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>A signature file as defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * href="manifest.html">Manifest and Signature Format</a>. It has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * essentially the same structure as a Manifest file in that it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * set of RFC 822 headers (sections). The first section contains meta
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * data relevant to the entire file (i.e "Signature-Version:1.0") and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * each subsequent section contains data relevant to specific entries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * entry sections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Each entry section contains the name of an entry (which must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * have a counterpart in the manifest). Like the manifest it contains
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
    50
 * a hash, the hash of the manifest section corresponding to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * name. Since the manifest entry contains the hash of the data, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is equivalent to a signature of the data, plus the attributes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the manifest entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>This signature file format deal with PKCS7 encoded DSA signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * block. It should be straightforward to extent to support other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author      David Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author      Benjamin Renaud */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class SignatureFile {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Are we debugging? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* list of headers that all pertain to a particular file in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * archive */
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
    69
    private Vector<MessageHeader> entries = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* Right now we only support SHA hashes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final String[] hashes = {"SHA"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final void debug(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println("sig> " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The manifest we're working with.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Manifest manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The file name for the file. This is the raw name, i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * extention-less 8 character name (such as MYSIGN) which wil be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * used to build the signature filename (MYSIGN.SF) and the block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * filename (MYSIGN.DSA) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private String rawName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* The digital signature block corresponding to this signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * file.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private PKCS7 signatureBlock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Private constructor which takes a name a given signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * file. The name must be extension-less and less or equal to 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * character in length.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private SignatureFile(String name) throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   101
        entries = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (name.length() > 8 || name.indexOf('.') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new JarException("invalid file name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
   107
            rawName = name.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Private constructor which takes a name a given signature file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * and a new file predicate. If it is a new file, a main header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * will be added. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private SignatureFile(String name, boolean newFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (newFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            MessageHeader globals = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            globals.set("Signature-Version", "1.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            entries.addElement(globals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Constructs a new Signature file corresponding to a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Manifest. All entries in the manifest are signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param manifest the manifest to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param name for this signature file. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * be less than 8 characters, and without a suffix (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * without a period in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception JarException if an invalid name is passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public SignatureFile(Manifest manifest, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.manifest = manifest;
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   145
        Enumeration<MessageHeader> enum_ = manifest.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        while (enum_.hasMoreElements()) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   147
            MessageHeader mh = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            String entryName = mh.findValue("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (entryName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                add(entryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Constructs a new Signature file corresponding to a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Manifest. Specific entries in the manifest are signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param manifest the manifest to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param entries the entries to sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param filename for this signature file. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * be less than 8 characters, and without a suffix (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * without a period in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @exception JarException if an invalid name is passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public SignatureFile(Manifest manifest, String[] entries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                         String filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this(filename, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.manifest = manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        add(entries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Construct a Signature file from an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception IOException if an invalid name is passed in or if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * stream exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public SignatureFile(InputStream is, String filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        while (is.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            MessageHeader m = new MessageHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            entries.addElement(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Construct a Signature file from an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception IOException if an invalid name is passed in or if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * stream exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public SignatureFile(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this(is, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public SignatureFile(byte[] bytes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this(new ByteArrayInputStream(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the name of the signature file, ending with a ".SF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * suffix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return "META-INF/" + rawName + ".SF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns the name of the block file, ending with a block suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * such as ".DSA". */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public String getBlockName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String suffix = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (signatureBlock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            SignerInfo info = signatureBlock.getSignerInfos()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            suffix = info.getDigestEncryptionAlgorithmId().getName();
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
   221
            String temp = AlgorithmId.getEncAlgFromSigAlg(suffix);
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
   222
            if (temp != null) suffix = temp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return "META-INF/" + rawName + "." + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the signature block associated with this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public PKCS7 getBlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return signatureBlock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Sets the signature block associated with this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void setBlock(PKCS7 block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.signatureBlock = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Add a set of entries from the current manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void add(String[] entries) throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (int i = 0; i < entries.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            add (entries[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Add a specific entry from the current manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void add(String entry) throws JarException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        MessageHeader mh = manifest.getEntry(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (mh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new JarException("entry " + entry + " not in manifest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        MessageHeader smh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            smh = computeEntry(mh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new JarException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        entries.addElement(smh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Get the entry corresponding to a given name. Returns null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *the entry does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public MessageHeader getEntry(String name) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   272
        Enumeration<MessageHeader> enum_ = entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        while(enum_.hasMoreElements()) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   274
            MessageHeader mh = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (name.equals(mh.findValue("Name"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return mh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Returns the n-th entry. The global header is a entry 0.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public MessageHeader entryAt(int n) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   285
        return entries.elementAt(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns an enumeration of the entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   291
    public Enumeration<MessageHeader> entries() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return entries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Given a manifest entry, computes the signature entry for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * manifest entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private MessageHeader computeEntry(MessageHeader mh) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        MessageHeader smh = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String name = mh.findValue("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        smh.set("Name", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            for (int i = 0; i < hashes.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                MessageDigest dig = getDigest(hashes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                PrintStream ps = new PrintStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                mh.print(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                byte[] headerBytes = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                byte[] digest = dig.digest(headerBytes);
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14342
diff changeset
   316
                smh.set(hashes[i] + "-Digest", Base64.getMimeEncoder().encodeToString(digest));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return smh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new JarException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   324
    private Hashtable<String, MessageDigest> digests = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private MessageDigest getDigest(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    throws NoSuchAlgorithmException {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   328
        MessageDigest dig = digests.get(algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (dig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            dig = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            digests.put(algorithm, dig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        dig.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return dig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Add a signature file at current position in a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void stream(OutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /* the first header in the file should be the global one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * It should say "SignatureFile-Version: x.x"; barf if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   346
        MessageHeader globals = entries.elementAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (globals.findValue("Signature-Version") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new JarException("Signature file requires " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            "Signature-Version: 1.0 in 1st header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        PrintStream ps = new PrintStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        globals.print(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (int i = 1; i < entries.size(); ++i) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 5506
diff changeset
   356
            MessageHeader mh = entries.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            mh.print(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}