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