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