jdk/src/share/classes/java/util/jar/JarVerifier.java
author weijun
Thu, 06 May 2010 13:42:52 +0800
changeset 5462 cb614e59f7f9
parent 4152 bc36a9f01ac6
child 5506 202f599c92aa
child 5606 63aa4b61c0a8
permissions -rw-r--r--
6890876: jarsigner can add CRL info into signed jar Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 4152
diff changeset
     2
 * Copyright 1997-2010 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 java.util.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
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.ManifestDigester;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.ManifestEntryVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.SignatureFileVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author      Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class JarVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /* Are we debugging ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final Debug debug = Debug.getInstance("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* a table mapping names to code signers, for jar entries that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
       had their actual hashes verified */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Hashtable verifiedSigners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* a table mapping names to code signers, for jar entries that have
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 4049
diff changeset
    52
       passed the .SF/.DSA/.EC -> MANIFEST check */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Hashtable sigFileSigners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* a hash table to hold .SF bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Hashtable sigFileData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** "queue" of pending PKCS7 blocks that we couldn't parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *  until we parsed the .SF file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private ArrayList pendingBlocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* cache of CodeSigner objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ArrayList signerCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* Are we parsing a block? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean parsingBlockOrSF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Are we done parsing META-INF entries? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean parsingMeta = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* Are there are files to verify? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean anyToVerify = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* The output stream to use when keeping track of files we are interested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       in */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private ByteArrayOutputStream baos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** The ManifestDigester object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private ManifestDigester manDig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** the bytes for the manDig object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    byte manifestRawBytes[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public JarVerifier(byte rawBytes[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        manifestRawBytes = rawBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        sigFileSigners = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        verifiedSigners = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sigFileData = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        pendingBlocks = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * This method scans to see which entry we're parsing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * keeps various state information depending on what type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * file is being parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void beginEntry(JarEntry je, ManifestEntryVerifier mev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (je == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            debug.println("beginEntry "+je.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String name = je.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * Assumptions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * 1. The manifest should be the first entry in the META-INF directory.
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 4049
diff changeset
   113
         * 2. The .SF/.DSA/.EC files follow the manifest, before any normal entries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * 3. Any of the following will throw a SecurityException:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         *    a. digest mismatch between a manifest section and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         *       the SF section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         *    b. digest mismatch between the actual jar entry and the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (parsingMeta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            String uname = name.toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if ((uname.startsWith("META-INF/") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                 uname.startsWith("/META-INF/"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (je.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    mev.setEntry(null, je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (SignatureFileVerifier.isBlockOrSF(uname)) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 4049
diff changeset
   131
                    /* We parse only DSA, RSA or EC PKCS7 blocks. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    parsingBlockOrSF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    mev.setEntry(null, je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (parsingMeta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            doneWithMeta();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (je.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            mev.setEntry(null, je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // be liberal in what you accept. If the name starts with ./, remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // it as we internally canonicalize it with out the ./.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (name.startsWith("./"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            name = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // be liberal in what you accept. If the name starts with /, remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // it as we internally canonicalize it with out the /.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (name.startsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // only set the jev object for entries that have a signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (sigFileSigners.get(name) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            mev.setEntry(name, je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // don't compute the digest for this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        mev.setEntry(null, je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * update a single byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void update(int b, ManifestEntryVerifier mev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (b != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (parsingBlockOrSF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                baos.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                mev.update((byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            processEntry(mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * update an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public void update(int n, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                       ManifestEntryVerifier mev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (n != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (parsingBlockOrSF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                baos.write(b, off, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                mev.update(b, off, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            processEntry(mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * called when we reach the end of entry in one of the read() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private void processEntry(ManifestEntryVerifier mev)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (!parsingBlockOrSF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            JarEntry je = mev.getEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if ((je != null) && (je.signers == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                je.signers = mev.verify(verifiedSigners, sigFileSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                je.certs = mapSignersToCertArray(je.signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                parsingBlockOrSF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    debug.println("processEntry: processing block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                String uname = mev.getEntry().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                             .toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (uname.endsWith(".SF")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    String key = uname.substring(0, uname.length()-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    byte bytes[] = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    // add to sigFileData in case future blocks need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    sigFileData.put(key, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    // check pending blocks, we can now process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    // anyone waiting for this .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    Iterator it = pendingBlocks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        SignatureFileVerifier sfv =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            (SignatureFileVerifier) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        if (sfv.needSignatureFile(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                debug.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                 "processEntry: processing pending block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            sfv.setSignatureFile(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            sfv.process(sigFileSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                // now we are parsing a signature block file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                String key = uname.substring(0, uname.lastIndexOf("."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (signerCache == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    signerCache = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (manDig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    synchronized(manifestRawBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        if (manDig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            manDig = new ManifestDigester(manifestRawBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            manifestRawBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                SignatureFileVerifier sfv =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                  new SignatureFileVerifier(signerCache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                            manDig, uname, baos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (sfv.needSignatureFileBytes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    // see if we have already parsed an external .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    byte[] bytes = (byte[]) sigFileData.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        // put this block on queue for later processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        // since we don't have the .SF bytes yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        // (uname, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                            debug.println("adding pending block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        pendingBlocks.add(sfv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        sfv.setSignatureFile(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                sfv.process(sigFileSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } catch (IOException ioe) {
4049
f02d7c56d8f8 6883983: JarVerifier dependency on sun.security.pkcs should be removed
alanb
parents: 2
diff changeset
   296
                // e.g. sun.security.pkcs.ParsingException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (debug != null) debug.println("processEntry caught: "+ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                // ignore and treat as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } catch (SignatureException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (debug != null) debug.println("processEntry caught: "+se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // ignore and treat as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (debug != null) debug.println("processEntry caught: "+nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // ignore and treat as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (debug != null) debug.println("processEntry caught: "+ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // ignore and treat as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Return an array of java.security.cert.Certificate objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * the given file in the jar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public java.security.cert.Certificate[] getCerts(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return mapSignersToCertArray(getCodeSigners(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * return an array of CodeSigner objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the given file in the jar. this array is not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public CodeSigner[] getCodeSigners(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return (CodeSigner[])verifiedSigners.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Convert an array of signers into an array of concatenated certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static java.security.cert.Certificate[] mapSignersToCertArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        CodeSigner[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            ArrayList certChains = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                certChains.addAll(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    signers[i].getSignerCertPath().getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // Convert into a Certificate[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return (java.security.cert.Certificate[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                certChains.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    new java.security.cert.Certificate[certChains.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * returns true if there no files to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * should only be called after all the META-INF entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * have been processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    boolean nothingToVerify()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return (anyToVerify == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * called to let us know we have processed all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * META-INF entries, and if we re-read one of them, don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * re-process it. Also gets rid of any data structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * we needed when parsing META-INF entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    void doneWithMeta()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        parsingMeta = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        anyToVerify = !sigFileSigners.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        baos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        sigFileData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        pendingBlocks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        signerCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        manDig = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    static class VerifierStream extends java.io.InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        private InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        private JarVerifier jv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        private ManifestEntryVerifier mev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        private long numLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        VerifierStream(Manifest man,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                       JarEntry je,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                       InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                       JarVerifier jv) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            this.jv = jv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            this.mev = new ManifestEntryVerifier(man);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            this.jv.beginEntry(je, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            this.numLeft = je.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (this.numLeft == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                this.jv.update(-1, this.mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public int read() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (numLeft > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                int b = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                jv.update(b, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                numLeft--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (numLeft == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    jv.update(-1, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if ((numLeft > 0) && (numLeft < len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                len = (int)numLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (numLeft > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                int n = is.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                jv.update(n, b, off, len, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                numLeft -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                if (numLeft == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    jv.update(-1, b, off, len, mev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (is != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            mev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return is.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}