jdk/src/share/classes/sun/security/util/SignatureFileVerifier.java
author igor
Sun, 01 May 2011 09:14:36 -0700
changeset 9365 469cd39a25de
parent 9248 44d129a2adfa
child 10336 0bb1999251f8
permissions -rw-r--r--
7040803: regression: bugster fail to start Reviewed-by: mullan, weijun, ngthomas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 5462
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: 5462
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: 5462
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
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.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.timestamp.TimestampToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.BASE64Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.jca.Providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class SignatureFileVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* Are we debugging ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final Debug debug = Debug.getInstance("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* cache of CodeSigner objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ArrayList<CodeSigner[]> signerCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String ATTR_DIGEST =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ("-DIGEST-" + ManifestDigester.MF_MAIN_ATTRS).toUpperCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        (Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
    55
    /** the PKCS7 block for this .DSA/.RSA/.EC file */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private PKCS7 block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
    58
    /** the raw bytes of the .SF file */
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
    59
    private byte sfBytes[];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** the name of the signature block file, uppercased and without
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
    62
     *  the extension (.DSA/.RSA/.EC)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** the ManifestDigester */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private ManifestDigester md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** cache of created MessageDigest objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private HashMap<String, MessageDigest> createdDigests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /* workaround for parsing Netscape jars  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean workaround = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /* for generating certpath objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private CertificateFactory certificateFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Create the named SignatureFileVerifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 2
diff changeset
    81
     * @param name the name of the signature block file (.DSA/.RSA/.EC)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param rawBytes the raw bytes of the signature block file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public SignatureFileVerifier(ArrayList<CodeSigner[]> signerCache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                 ManifestDigester md,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                 String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                 byte rawBytes[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws IOException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // new PKCS7() calls CertificateFactory.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // need to use local providers here, see Providers class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Object obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            obj = Providers.startJarVerification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            block = new PKCS7(rawBytes);
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
    97
            sfBytes = block.getContentInfo().getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            certificateFactory = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Providers.stopJarVerification(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.name = name.substring(0, name.lastIndexOf("."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                   .toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.md = md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.signerCache = signerCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * returns true if we need the .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   111
    public boolean needSignatureFileBytes()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   113
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   114
        return sfBytes == null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   117
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   118
    /**
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   119
     * returns true if we need this .SF file.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   120
     *
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   121
     * @param name the name of the .SF file without the extension
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   122
     *
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   123
     */
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   124
    public boolean needSignatureFile(String name)
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   125
    {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   126
        return this.name.equalsIgnoreCase(name);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   127
    }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   128
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   129
    /**
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   130
     * used to set the raw bytes of the .SF file when it
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   131
     * is external to the signature block file.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   132
     */
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   133
    public void setSignatureFile(byte sfBytes[])
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   134
    {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   135
        this.sfBytes = sfBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Utility method used by JarVerifier and JarSigner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * to determine the signature file names and PKCS7 block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * files names that are supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param s file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return true if the input file name is a supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          Signature File or PKCS7 block file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static boolean isBlockOrSF(String s) {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   148
        // we currently only support DSA and RSA PKCS7 blocks
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   149
        if (s.endsWith(".SF") || s.endsWith(".DSA") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   150
                s.endsWith(".RSA") || s.endsWith(".EC")) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   151
            return true;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   152
        }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   153
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** get digest from cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private MessageDigest getDigest(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (createdDigests == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            createdDigests = new HashMap<String, MessageDigest>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        MessageDigest digest = createdDigests.get(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (digest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                digest = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                createdDigests.put(algorithm, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * process the signature block file. Goes through the .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * and adds code signers for each section where the .SF section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * hash was verified against the Manifest section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   183
    public void process(Hashtable<String, CodeSigner[]> signers,
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   184
            List manifestDigests)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throws IOException, SignatureException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            JarException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // calls Signature.getInstance() and MessageDigest.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // need to use local providers here, see Providers class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Object obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            obj = Providers.startJarVerification();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   193
            processImpl(signers, manifestDigests);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            Providers.stopJarVerification(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   200
    private void processImpl(Hashtable<String, CodeSigner[]> signers,
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   201
            List manifestDigests)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws IOException, SignatureException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            JarException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   205
        Manifest sf = new Manifest();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   206
        sf.read(new ByteArrayInputStream(sfBytes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   208
        String version =
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   209
            sf.getMainAttributes().getValue(Attributes.Name.SIGNATURE_VERSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   211
        if ((version == null) || !(version.equalsIgnoreCase("1.0"))) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   212
            // XXX: should this be an exception?
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   213
            // for now we just ignore this signature file
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   214
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   217
        SignerInfo[] infos = block.verify(sfBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   219
        if (infos == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new SecurityException("cannot verify signature block file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                        name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   224
        BASE64Decoder decoder = new BASE64Decoder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        CodeSigner[] newSigners = getSigners(infos, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // make sure we have something to do all this work for...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (newSigners == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   232
        Iterator<Map.Entry<String,Attributes>> entries =
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   233
                                sf.getEntries().entrySet().iterator();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   234
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   235
        // see if we can verify the whole manifest first
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   236
        boolean manifestSigned = verifyManifestHash(sf, md, decoder, manifestDigests);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   237
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // verify manifest main attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!manifestSigned && !verifyManifestMainAttrs(sf, md, decoder)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                ("Invalid signature file digest for Manifest main attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   244
        // go through each section in the signature file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        while(entries.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Map.Entry<String,Attributes> e = entries.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            String name = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (manifestSigned ||
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   251
                (verifySection(e.getValue(), name, md, decoder))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (name.startsWith("./"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    name = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (name.startsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                updateSigners(newSigners, signers, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    debug.println("processSignature signed name = "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                debug.println("processSignature unsigned name = "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   269
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   270
        // MANIFEST.MF is always regarded as signed
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   271
        updateSigners(newSigners, signers, JarFile.MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * See if the whole manifest was signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private boolean verifyManifestHash(Manifest sf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                       ManifestDigester md,
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   279
                                       BASE64Decoder decoder,
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   280
                                       List manifestDigests)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Attributes mattr = sf.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean manifestSigned = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // go through all the attributes and process *-Digest-Manifest entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (Map.Entry<Object,Object> se : mattr.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            String key = se.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (key.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST-MANIFEST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                // 16 is length of "-Digest-Manifest"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                String algorithm = key.substring(0, key.length()-16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   295
                manifestDigests.add(key);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 7524
diff changeset
   296
                manifestDigests.add(se.getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                MessageDigest digest = getDigest(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (digest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    byte[] computedHash = md.manifestDigest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    byte[] expectedHash =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        decoder.decodeBuffer((String)se.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                     debug.println("Signature File: Manifest digest " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                          digest.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     debug.println( "  sigfile  " + toHex(expectedHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                     debug.println( "  computed " + toHex(computedHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                     debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (MessageDigest.isEqual(computedHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                              expectedHash)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        manifestSigned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        //XXX: we will continue and verify each section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return manifestSigned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private boolean verifyManifestMainAttrs(Manifest sf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                        ManifestDigester md,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                        BASE64Decoder decoder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Attributes mattr = sf.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        boolean attrsVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // go through all the attributes and process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // digest entries for the manifest main attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (Map.Entry<Object,Object> se : mattr.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            String key = se.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (key.toUpperCase(Locale.ENGLISH).endsWith(ATTR_DIGEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                String algorithm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        key.substring(0, key.length() - ATTR_DIGEST.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                MessageDigest digest = getDigest(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (digest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    ManifestDigester.Entry mde =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        md.get(ManifestDigester.MF_MAIN_ATTRS, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    byte[] computedHash = mde.digest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    byte[] expectedHash =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        decoder.decodeBuffer((String)se.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                     debug.println("Signature File: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                        "Manifest Main Attributes digest " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                        digest.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                     debug.println( "  sigfile  " + toHex(expectedHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                     debug.println( "  computed " + toHex(computedHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                     debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    if (MessageDigest.isEqual(computedHash,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                              expectedHash)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        // we will *not* continue and verify each section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        attrsVerified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            debug.println("Verification of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                        "Manifest main attributes failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // this method returns 'true' if either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        //      . manifest main attributes were not signed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        //      . manifest main attributes were signed and verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return attrsVerified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * given the .SF digest header, and the data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * section in the manifest, see if the hashes match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * if not, throw a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return true if all the -Digest headers verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception SecurityException if the hash was not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private boolean verifySection(Attributes sfAttr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                  String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                  ManifestDigester md,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                  BASE64Decoder decoder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        boolean oneDigestVerified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        ManifestDigester.Entry mde = md.get(name,block.isOldStyle());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (mde == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                  "no manifiest section for signature file entry "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (sfAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            //sun.misc.HexDumpEncoder hex = new sun.misc.HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            //hex.encodeBuffer(data, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // go through all the attributes and process *-Digest entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            for (Map.Entry<Object,Object> se : sfAttr.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                String key = se.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                if (key.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    // 7 is length of "-Digest"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    String algorithm = key.substring(0, key.length()-7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    MessageDigest digest = getDigest(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    if (digest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        byte[] expected =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            decoder.decodeBuffer((String)se.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        byte[] computed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (workaround) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            computed = mde.digestWorkaround(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            computed = mde.digest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                          debug.println("Signature Block File: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                   name + " digest=" + digest.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                          debug.println("  expected " + toHex(expected));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                          debug.println("  computed " + toHex(computed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                          debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        if (MessageDigest.isEqual(computed, expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            oneDigestVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            // attempt to fallback to the workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            if (!workaround) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                               computed = mde.digestWorkaround(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                               if (MessageDigest.isEqual(computed, expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                   if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                       debug.println("  re-computed " + toHex(computed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                       debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                   workaround = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                   oneDigestVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                   ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        if (!ok){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            throw new SecurityException("invalid " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                       digest.getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                       " signature file digest for " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return oneDigestVerified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Given the PKCS7 block and SignerInfo[], create an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * CodeSigner objects. We do this only *once* for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * signature block file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private CodeSigner[] getSigners(SignerInfo infos[], PKCS7 block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        throws IOException, NoSuchAlgorithmException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        ArrayList<CodeSigner> signers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        for (int i = 0; i < infos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            SignerInfo info = infos[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            ArrayList<X509Certificate> chain = info.getCertificateChain(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            CertPath certChain = certificateFactory.generateCertPath(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (signers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                signers = new ArrayList<CodeSigner>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            // Append the new code signer
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 8387
diff changeset
   488
            signers.add(new CodeSigner(certChain, getTimestamp(info)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                debug.println("Signature Block Certificate: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    chain.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return signers.toArray(new CodeSigner[signers.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Examines a signature timestamp token to generate a timestamp object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Examines the signer's unsigned attributes for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <tt>signatureTimestampToken</tt> attribute. If present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * then it is parsed to extract the date and time at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * timestamp was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param info A signer information element of a PKCS 7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return A timestamp token or null if none is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @throws IOException if an error is encountered while parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *         PKCS7 data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @throws NoSuchAlgorithmException if an error is encountered while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *         verifying the PKCS7 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @throws SignatureException if an error is encountered while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *         verifying the PKCS7 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @throws CertificateException if an error is encountered while generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *         the TSA's certpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private Timestamp getTimestamp(SignerInfo info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        throws IOException, NoSuchAlgorithmException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        Timestamp timestamp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // Extract the signer's unsigned attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        PKCS9Attributes unsignedAttrs = info.getUnauthenticatedAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (unsignedAttrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            PKCS9Attribute timestampTokenAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                unsignedAttrs.getAttribute("signatureTimestampToken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (timestampTokenAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                PKCS7 timestampToken =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    new PKCS7((byte[])timestampTokenAttr.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                // Extract the content (an encoded timestamp token info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                byte[] encodedTimestampTokenInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    timestampToken.getContentInfo().getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // Extract the signer (the Timestamping Authority)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // while verifying the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                SignerInfo[] tsa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    timestampToken.verify(encodedTimestampTokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // Expect only one signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                ArrayList<X509Certificate> chain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                tsa[0].getCertificateChain(timestampToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                CertPath tsaChain = certificateFactory.generateCertPath(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                // Create a timestamp token info object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                TimestampToken timestampTokenInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    new TimestampToken(encodedTimestampTokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                // Create a timestamp object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                timestamp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    new Timestamp(timestampTokenInfo.getDate(), tsaChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    // for the toHex function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private static final char[] hexc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * convert a byte array to a hex string for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param data the binary data to be converted to a hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return an ASCII hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    static String toHex(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        StringBuffer sb = new StringBuffer(data.length*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        for (int i=0; i<data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            sb.append(hexc[(data[i] >>4) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            sb.append(hexc[data[i] & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    // returns true if set contains signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    static boolean contains(CodeSigner[] set, CodeSigner signer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        for (int i = 0; i < set.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            if (set[i].equals(signer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // returns true if subset is a subset of set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    static boolean isSubSet(CodeSigner[] subset, CodeSigner[] set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // check for the same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (set == subset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   596
        boolean match;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        for (int i = 0; i < subset.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (!contains(set, subset[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * returns true if signer contains exactly the same code signers as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * oldSigner and newSigner, false otherwise. oldSigner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * is allowed to be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    static boolean matches(CodeSigner[] signers, CodeSigner[] oldSigners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        CodeSigner[] newSigners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if ((oldSigners == null) && (signers == newSigners))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   616
        boolean match;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   617
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // make sure all oldSigners are in signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if ((oldSigners != null) && !isSubSet(oldSigners, signers))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // make sure all newSigners are in signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (!isSubSet(newSigners, signers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // now make sure all the code signers in signers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // also in oldSigners or newSigners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            boolean found =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                ((oldSigners != null) && contains(oldSigners, signers[i])) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                contains(newSigners, signers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (!found)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    void updateSigners(CodeSigner[] newSigners,
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   641
        Hashtable<String, CodeSigner[]> signers, String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        CodeSigner[] oldSigners = signers.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        // search through the cache for a match, go in reverse order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // as we are more likely to find a match with the last one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // added to the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        CodeSigner[] cachedSigners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        for (int i = signerCache.size() - 1; i != -1; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            cachedSigners = signerCache.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if (matches(cachedSigners, oldSigners, newSigners)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                signers.put(name, cachedSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (oldSigners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            cachedSigners = newSigners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            cachedSigners =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                new CodeSigner[oldSigners.length + newSigners.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            System.arraycopy(oldSigners, 0, cachedSigners, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                oldSigners.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            System.arraycopy(newSigners, 0, cachedSigners, oldSigners.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                newSigners.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        signerCache.add(cachedSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        signers.put(name, cachedSigners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
}