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