src/java.base/share/classes/javax/crypto/JceSecurity.java.template
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 54996 3081932e7efd
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    26
/*
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    27
 * README README README README README README README README README
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    28
 *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    29
 * This file is the template for generating the JceSecurity.java source
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    30
 * file.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    31
 *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    32
 * In the current jdk builds, this file is first preprocessed to replace
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    33
 * @@JCE_DEFAULT_POLICY@ [sic] with "limited" or "unlimited" which is
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    34
 * determined by the $(UNLIMTED_CRYPTO) make variable.  This variable is
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    35
 * set by top-level configure script, using either
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    36
 * --disable-unlimited-crypto or --enable-unlimited-crypto [default].
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    37
 *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    38
 * Since this file is a generated source, incremental changes to
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    39
 * this file require regenerating the source.  Compilation options:
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    40
 *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    41
 *     (fewer dependencies/"faster" ones first)
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    42
 *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    43
 * 1.  make JDK_FILTER=javax/crypto java.base-gensrc-only java.base-java-only
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    44
 * 2.  make java.base-gensrc-only java.base-java-only
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    45
 * 3.  make java.base-gensrc-only java.base-only
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    46
 * 4.  make java.base-only
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    47
 * 5.  make
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    48
 */
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    53
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.net.URL;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
    56
import java.nio.file.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47216
diff changeset
    61
import jdk.internal.util.StaticProperty;
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47216
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.security.jca.GetInstance.Instance;
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    65
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * This class instantiates implementations of JCE engine classes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * providers registered with the java.security.Security object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author Sharon Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
final class JceSecurity {
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47216
diff changeset
    77
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47216
diff changeset
    78
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    79
    private static final Debug debug = Debug.getInstance("jca");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final SecureRandom RANDOM = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // The defaultPolicy and exemptPolicy will be set up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // in the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static CryptoPermissions defaultPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static CryptoPermissions exemptPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    88
    // Map of the providers we already have verified.
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    89
    // If verified ok, value == PROVIDER_VERIFIED, otherwise
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    90
    // the cause of verification failure is stored as value.
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    91
    private static final Map<IdentityWrapper, Object>
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
    92
        verificationResults = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Map<Provider,?> of the providers currently being verified
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
    95
    private static final Map<Provider, Object> verifyingProviders =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
            new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
36119
75308cd1bf2c 8149417: Use final restricted flag
xuelei
parents: 32649
diff changeset
    98
    private static final boolean isRestricted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Don't let anyone instantiate this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private JceSecurity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   108
            AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26861
diff changeset
   109
                new PrivilegedExceptionAction<> () {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   110
                    @Override
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   111
                    public Void run() throws Exception {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   112
                        setupJurisdictionPolicies();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   113
                        return null;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   114
                    }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   115
                }
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   116
            );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            isRestricted = defaultPolicy.implies(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                CryptoAllPermission.INSTANCE) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   121
            throw new SecurityException(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   122
                    "Can not initialize cryptographic mechanism", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   126
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Exception ve = getVerificationResult(s.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            String msg = "JCE cannot authenticate the provider " + provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw (NoSuchProviderException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                new NoSuchProviderException(msg).initCause(ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   139
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Exception ve = JceSecurity.getVerificationResult(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            String msg = "JCE cannot authenticate the provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                + provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new SecurityException(msg, ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   151
    static Instance getInstance(String type, Class<?> clazz, String algorithm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   153
        List<Service> services = GetInstance.getServices(type, algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        NoSuchAlgorithmException failure = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   155
        for (Service s : services) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // allow only signed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Instance instance = GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throw new NoSuchAlgorithmException("Algorithm " + algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                + " not available", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Verify if the JAR at URL codeBase is a signed exempt application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * JAR file and returns the permissions bundled with the JAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static CryptoPermissions verifyExemptJar(URL codeBase) throws Exception {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   178
        ProviderVerifier pv = new ProviderVerifier(codeBase, true);
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   179
        pv.verify();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   180
        return pv.getPermissions();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Verify if the JAR at URL codeBase is a signed provider JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   188
    static void verifyProvider(URL codeBase, Provider p) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Verify the provider JAR file and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // supporting JAR files if there are any.
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   191
        ProviderVerifier pv = new ProviderVerifier(codeBase, p, false);
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   192
        pv.verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
   195
    private static final Object PROVIDER_VERIFIED = Boolean.TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Verify that the provider JAR files are signed properly, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * means the signer's certificate can be traced back to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * JCE trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Return null if ok, failure Exception if verification failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   203
    static Exception getVerificationResult(Provider p) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   204
        IdentityWrapper pKey = new IdentityWrapper(p);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   205
        Object o = verificationResults.get(pKey);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   206
        // no mapping found
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   207
        if (o == null) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   208
            synchronized (JceSecurity.class) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   209
                // check cache again in case the result is now available
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   210
                o = verificationResults.get(pKey);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   211
                if (o == null) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   212
                    if (verifyingProviders.get(p) != null) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   213
                        // recursion; return failure now
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   214
                        return new NoSuchProviderException
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   215
                                ("Recursion during verification");
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   216
                    }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   217
                    try {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   218
                        verifyingProviders.put(p, Boolean.FALSE);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   219
                        URL providerURL = getCodeBase(p.getClass());
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   220
                        verifyProvider(providerURL, p);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   221
                        o = PROVIDER_VERIFIED;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   222
                    } catch (Exception e) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   223
                        o = e;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   224
                    } finally {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   225
                        verifyingProviders.remove(p);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   226
                    }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   227
                    verificationResults.put(pKey, o);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   228
                    if (debug != null) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   229
                        debug.println("Provider " + p.getName() +
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   230
                                " verification result: " + o);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   231
                    }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   232
                }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   233
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   235
        return (o == PROVIDER_VERIFIED? null : (Exception) o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // return whether this provider is properly signed and can be used by JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static boolean canUseProvider(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return getVerificationResult(p) == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // dummy object to represent null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static final URL NULL_URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   248
            NULL_URL = new URL("http://null.oracle.com/");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    // reference to a Map we use as a cache for codebases
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   255
    private static final Map<Class<?>, URL> codeBaseCacheRef =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   256
            new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
24501
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   259
     * Returns the CodeBase for the given class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   261
    static URL getCodeBase(final Class<?> clazz) {
24501
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   262
        synchronized (codeBaseCacheRef) {
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   263
            URL url = codeBaseCacheRef.get(clazz);
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   264
            if (url == null) {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   265
                url = AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26861
diff changeset
   266
                    new PrivilegedAction<>() {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   267
                        @Override
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   268
                        public URL run() {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   269
                            ProtectionDomain pd = clazz.getProtectionDomain();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   270
                            if (pd != null) {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   271
                                CodeSource cs = pd.getCodeSource();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   272
                                if (cs != null) {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   273
                                    return cs.getLocation();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   274
                                }
24501
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   275
                            }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   276
                            return NULL_URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   278
                    });
24501
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   279
                codeBaseCacheRef.put(clazz, url);
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   280
            }
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   281
            return (url == NULL_URL) ? null : url;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   285
    // This is called from within an doPrivileged block.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static void setupJurisdictionPolicies() throws Exception {
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   287
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   288
        // Sanity check the crypto.policy Security property.  Single
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   289
        // directory entry, no pseudo-directories (".", "..", leading/trailing
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   290
        // path separators). normalize()/getParent() will help later.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   291
        String cryptoPolicyProperty = Security.getProperty("crypto.policy");
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   292
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   293
        /*
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   294
         * In case no property is present, rather than fail catastrophically,
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   295
         * we at least try for a "sane" value, which is what we were
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   296
         * built with.  We first preprocess this file to plug in that
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   297
         * value, then compile the result gensrc.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   298
         *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   299
         * Log the warning first.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   300
         */
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   301
        if (cryptoPolicyProperty == null) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   302
            cryptoPolicyProperty = "@@JCE_DEFAULT_POLICY@@";
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   303
            if (debug != null) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   304
                debug.println(
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   305
                    "Security Property 'crypto.policy' not found: "
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   306
                    + "using '" + cryptoPolicyProperty + "' as fallback");
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   307
            }
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   308
        }
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   309
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   310
        Path cpPath = Paths.get(cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   311
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   312
        if ((cpPath.getNameCount() != 1) ||
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   313
                (cpPath.compareTo(cpPath.getFileName()) != 0)) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   314
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   315
                "Invalid policy directory name format: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   316
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   319
        // Prepend java.home to get the full path.  normalize() in
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   320
        // case an extra "." or ".." snuck in somehow.
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47216
diff changeset
   321
        String javaHomeProperty = StaticProperty.javaHome();
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   322
        Path javaHomePolicyPath = Paths.get(javaHomeProperty, "conf",
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   323
                "security", "policy").normalize();
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   324
        Path cryptoPolicyPath = Paths.get(javaHomeProperty, "conf", "security",
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   325
                "policy", cryptoPolicyProperty).normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   327
        if (cryptoPolicyPath.getParent().compareTo(javaHomePolicyPath) != 0) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   328
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   329
                "Invalid cryptographic jurisdiction policy directory path: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   330
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   331
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   332
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   333
        if (!Files.isDirectory(cryptoPolicyPath)
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   334
                || !Files.isReadable(cryptoPolicyPath)) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   335
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   336
                "Can't read cryptographic policy directory: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   337
                cryptoPolicyProperty);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   340
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   341
                cryptoPolicyPath, "{default,exempt}_*.policy")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   342
            for (Path entry : stream) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   343
                try (InputStream is = new BufferedInputStream(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   344
                        Files.newInputStream(entry))) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   345
                    String filename = entry.getFileName().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   347
                    CryptoPermissions tmpPerms = new CryptoPermissions();
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   348
                    tmpPerms.load(is);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   350
                    if (filename.startsWith("default_")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   351
                        // Did we find a default perms?
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   352
                        defaultPolicy = ((defaultPolicy == null) ? tmpPerms :
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   353
                                defaultPolicy.getMinimum(tmpPerms));
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   354
                    } else if (filename.startsWith("exempt_")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   355
                        // Did we find a exempt perms?
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   356
                        exemptPolicy = ((exemptPolicy == null) ? tmpPerms :
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   357
                                exemptPolicy.getMinimum(tmpPerms));
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   358
                    } else {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   359
                        // This should never happen.  newDirectoryStream
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   360
                        // should only throw return "{default,exempt}_*.policy"
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   361
                        throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   362
                            "Unexpected jurisdiction policy files in : " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   363
                            cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   364
                    }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   365
                } catch (Exception e) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   366
                    throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   367
                        "Couldn't parse jurisdiction policy files in: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   368
                        cryptoPolicyProperty);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   371
        } catch (DirectoryIteratorException ex) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   372
            // I/O error encountered during the iteration,
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   373
            // the cause is an IOException
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   374
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   375
                "Couldn't iterate through the jurisdiction policy files: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   376
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   377
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   379
        // Must have a default policy
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   380
        if ((defaultPolicy == null) || defaultPolicy.isEmpty()) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   381
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   382
                "Missing mandatory jurisdiction policy files: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   383
                cryptoPolicyProperty);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   385
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   386
        // If there was an empty exempt policy file, ignore it.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   387
        if ((exemptPolicy != null) && exemptPolicy.isEmpty()) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   388
            exemptPolicy = null;
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   389
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    static CryptoPermissions getDefaultPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    static CryptoPermissions getExemptPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return exemptPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    static boolean isRestricted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return isRestricted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
54996
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   403
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   404
    private static final class IdentityWrapper {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   405
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   406
        final Provider obj;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   407
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   408
        IdentityWrapper(Provider obj) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   409
            this.obj = obj;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   410
        }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   411
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   412
        @Override
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   413
        public boolean equals(Object o) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   414
            if (this == o) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   415
                return true;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   416
            }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   417
            if (!(o instanceof IdentityWrapper)) {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   418
                return false;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   419
            }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   420
            return this.obj == ((IdentityWrapper)o).obj;
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   421
        }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   422
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   423
        @Override
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   424
        public int hashCode() {
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   425
            return System.identityHashCode(obj);
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   426
        }
3081932e7efd 7107615: scalability bloker in javax.crypto.JceSecurity
valeriep
parents: 50817
diff changeset
   427
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
}