jdk/src/java.base/share/classes/javax/crypto/JceSecurity.java
author wetmore
Fri, 26 Aug 2016 13:44:20 -0700
changeset 40565 3ac0ba151e70
parent 36511 9d0388c6b336
permissions -rw-r--r--
8061842: Package jurisdiction policy files as something other than JAR Reviewed-by: xuelei, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36119
75308cd1bf2c 8149417: Use final restricted flag
xuelei
parents: 32649
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
    32
import java.nio.file.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class instantiates implementations of JCE engine classes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * providers registered with the java.security.Security object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Sharon Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
final class JceSecurity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final SecureRandom RANDOM = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // The defaultPolicy and exemptPolicy will be set up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // in the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static CryptoPermissions defaultPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static CryptoPermissions exemptPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Map<Provider,?> of the providers we already have verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // value == PROVIDER_VERIFIED is successfully verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // value is failure cause Exception in error case
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
    61
    private static final Map<Provider, Object> verificationResults =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    62
            new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Map<Provider,?> of the providers currently being verified
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
    65
    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
    66
            new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
36119
75308cd1bf2c 8149417: Use final restricted flag
xuelei
parents: 32649
diff changeset
    68
    private static final boolean isRestricted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Don't let anyone instantiate this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private JceSecurity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    78
            AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26861
diff changeset
    79
                new PrivilegedExceptionAction<> () {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    80
                    @Override
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    81
                    public Void run() throws Exception {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    82
                        setupJurisdictionPolicies();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    83
                        return null;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    84
                    }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    85
                }
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    86
            );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            isRestricted = defaultPolicy.implies(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                CryptoAllPermission.INSTANCE) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    91
            throw new SecurityException(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    92
                    "Can not initialize cryptographic mechanism", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Exception ve = getVerificationResult(s.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            String msg = "JCE cannot authenticate the provider " + provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw (NoSuchProviderException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                new NoSuchProviderException(msg).initCause(ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   109
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Exception ve = JceSecurity.getVerificationResult(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            String msg = "JCE cannot authenticate the provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                + provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new SecurityException(msg, ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   121
    static Instance getInstance(String type, Class<?> clazz, String algorithm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   123
        List<Service> services = GetInstance.getServices(type, algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        NoSuchAlgorithmException failure = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   125
        for (Service s : services) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // allow only signed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                Instance instance = GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throw new NoSuchAlgorithmException("Algorithm " + algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                + " not available", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Verify if the JAR at URL codeBase is a signed exempt application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * JAR file and returns the permissions bundled with the JAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static CryptoPermissions verifyExemptJar(URL codeBase) throws Exception {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   148
        ProviderVerifier pv = new ProviderVerifier(codeBase, true);
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   149
        pv.verify();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   150
        return pv.getPermissions();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Verify if the JAR at URL codeBase is a signed provider JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   158
    static void verifyProvider(URL codeBase, Provider p) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Verify the provider JAR file and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // supporting JAR files if there are any.
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   161
        ProviderVerifier pv = new ProviderVerifier(codeBase, p, false);
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   162
        pv.verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
   165
    private static final Object PROVIDER_VERIFIED = Boolean.TRUE;
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
     * Verify that the provider JAR files are signed properly, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * means the signer's certificate can be traced back to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * JCE trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Return null if ok, failure Exception if verification failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static synchronized Exception getVerificationResult(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Object o = verificationResults.get(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (o == PROVIDER_VERIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return (Exception)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (verifyingProviders.get(p) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // this method is static synchronized, must be recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // return failure now but do not save the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return new NoSuchProviderException("Recursion during verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            verifyingProviders.put(p, Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            URL providerURL = getCodeBase(p.getClass());
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   188
            verifyProvider(providerURL, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Verified ok, cache result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            verificationResults.put(p, PROVIDER_VERIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            verificationResults.put(p, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            verifyingProviders.remove(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // return whether this provider is properly signed and can be used by JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static boolean canUseProvider(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return getVerificationResult(p) == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // dummy object to represent null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static final URL NULL_URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        try {
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   210
            NULL_URL = new URL("http://null.oracle.com/");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // 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
   217
    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
   218
            new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
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
   221
     * Returns the CodeBase for the given class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   223
    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
   224
        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
   225
            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
   226
            if (url == null) {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   227
                url = AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 26861
diff changeset
   228
                    new PrivilegedAction<>() {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   229
                        @Override
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   230
                        public URL run() {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   231
                            ProtectionDomain pd = clazz.getProtectionDomain();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   232
                            if (pd != null) {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   233
                                CodeSource cs = pd.getCodeSource();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   234
                                if (cs != null) {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   235
                                    return cs.getLocation();
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   236
                                }
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
   237
                            }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   238
                            return NULL_URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        }
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   240
                    });
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
   241
                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
   242
            }
767c30e88a61 8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents: 10336
diff changeset
   243
            return (url == NULL_URL) ? null : url;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   247
    // This is called from within an doPrivileged block.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static void setupJurisdictionPolicies() throws Exception {
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   249
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   250
        // Sanity check the crypto.policy Security property.  Single
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   251
        // directory entry, no pseudo-directories (".", "..", leading/trailing
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   252
        // path separators). normalize()/getParent() will help later.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   253
        String cryptoPolicyProperty = Security.getProperty("crypto.policy");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   254
        Path cpPath = Paths.get(cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   255
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   256
        if ((cryptoPolicyProperty == null) ||
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   257
                (cpPath.getNameCount() != 1) ||
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   258
                (cpPath.compareTo(cpPath.getFileName()) != 0)) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   259
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   260
                "Invalid policy directory name format: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   261
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   262
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   264
        // 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
   265
        // case an extra "." or ".." snuck in somehow.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   266
        String javaHomeProperty = System.getProperty("java.home");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   267
        Path javaHomePolicyPath = Paths.get(javaHomeProperty, "conf",
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   268
                "security", "policy").normalize();
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   269
        Path cryptoPolicyPath = Paths.get(javaHomeProperty, "conf", "security",
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   270
                "policy", cryptoPolicyProperty).normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   272
        if (cryptoPolicyPath.getParent().compareTo(javaHomePolicyPath) != 0) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   273
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   274
                "Invalid cryptographic jurisdiction policy directory path: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   275
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   276
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   277
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   278
        if (!Files.isDirectory(cryptoPolicyPath)
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   279
                || !Files.isReadable(cryptoPolicyPath)) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   280
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   281
                "Can't read cryptographic policy directory: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   282
                cryptoPolicyProperty);
2
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
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   286
                cryptoPolicyPath, "{default,exempt}_*.policy")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   287
            for (Path entry : stream) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   288
                try (InputStream is = new BufferedInputStream(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   289
                        Files.newInputStream(entry))) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   290
                    String filename = entry.getFileName().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   292
                    CryptoPermissions tmpPerms = new CryptoPermissions();
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   293
                    tmpPerms.load(is);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   295
                    if (filename.startsWith("default_")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   296
                        // Did we find a default perms?
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   297
                        defaultPolicy = ((defaultPolicy == null) ? tmpPerms :
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   298
                                defaultPolicy.getMinimum(tmpPerms));
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   299
                    } else if (filename.startsWith("exempt_")) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   300
                        // Did we find a exempt perms?
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   301
                        exemptPolicy = ((exemptPolicy == null) ? tmpPerms :
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   302
                                exemptPolicy.getMinimum(tmpPerms));
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   303
                    } else {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   304
                        // This should never happen.  newDirectoryStream
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   305
                        // should only throw return "{default,exempt}_*.policy"
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   306
                        throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   307
                            "Unexpected jurisdiction policy files in : " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   308
                            cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   309
                    }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   310
                } catch (Exception e) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   311
                    throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   312
                        "Couldn't parse jurisdiction policy files in: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   313
                        cryptoPolicyProperty);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   316
        } catch (DirectoryIteratorException ex) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   317
            // I/O error encountered during the iteration,
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   318
            // the cause is an IOException
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   319
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   320
                "Couldn't iterate through the jurisdiction policy files: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   321
                cryptoPolicyProperty);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   322
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   324
        // Must have a default policy
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   325
        if ((defaultPolicy == null) || defaultPolicy.isEmpty()) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   326
            throw new SecurityException(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   327
                "Missing mandatory jurisdiction policy files: " +
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   328
                cryptoPolicyProperty);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   330
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   331
        // 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
   332
        if ((exemptPolicy != null) && exemptPolicy.isEmpty()) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   333
            exemptPolicy = null;
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents: 36511
diff changeset
   334
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    static CryptoPermissions getDefaultPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    static CryptoPermissions getExemptPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return exemptPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static boolean isRestricted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return isRestricted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}