jdk/src/share/classes/javax/crypto/JceSecurity.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 24501 767c30e88a61
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class instantiates implementations of JCE engine classes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * providers registered with the java.security.Security object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Sharon Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class JceSecurity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final SecureRandom RANDOM = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // The defaultPolicy and exemptPolicy will be set up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // in the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static CryptoPermissions defaultPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static CryptoPermissions exemptPolicy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Map<Provider,?> of the providers we already have verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // value == PROVIDER_VERIFIED is successfully verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // value is failure cause Exception in error case
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    60
    private final static Map<Provider, Object> verificationResults =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    61
            new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Map<Provider,?> of the providers currently being verified
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    64
    private final static Map<Provider, Object> verifyingProviders =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    65
            new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Set the default value. May be changed in the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static boolean isRestricted = true;
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(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    79
                new PrivilegedExceptionAction<Object>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    80
                    public Object run() throws Exception {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    81
                        setupJurisdictionPolicies();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    82
                        return null;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    83
                    }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    84
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            isRestricted = defaultPolicy.implies(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                CryptoAllPermission.INSTANCE) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    89
            throw new SecurityException(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    90
                    "Can not initialize cryptographic mechanism", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    94
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Exception ve = getVerificationResult(s.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            String msg = "JCE cannot authenticate the provider " + provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw (NoSuchProviderException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                new NoSuchProviderException(msg).initCause(ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   107
    static Instance getInstance(String type, Class<?> clazz, String algorithm,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Service s = GetInstance.getService(type, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Exception ve = JceSecurity.getVerificationResult(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (ve != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            String msg = "JCE cannot authenticate the provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                + provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new SecurityException(msg, ve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   119
    static Instance getInstance(String type, Class<?> clazz, String algorithm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   121
        List<Service> services = GetInstance.getServices(type, algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        NoSuchAlgorithmException failure = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   123
        for (Service s : services) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // allow only signed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                Instance instance = GetInstance.getInstance(s, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throw new NoSuchAlgorithmException("Algorithm " + algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                + " not available", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Verify if the JAR at URL codeBase is a signed exempt application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * JAR file and returns the permissions bundled with the JAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static CryptoPermissions verifyExemptJar(URL codeBase) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        JarVerifier jv = new JarVerifier(codeBase, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        jv.verify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return jv.getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Verify if the JAR at URL codeBase is a signed provider JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static void verifyProviderJar(URL codeBase) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Verify the provider JAR file and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // supporting JAR files if there are any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JarVerifier jv = new JarVerifier(codeBase, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        jv.verify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private final static Object PROVIDER_VERIFIED = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Verify that the provider JAR files are signed properly, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * means the signer's certificate can be traced back to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * JCE trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Return null if ok, failure Exception if verification failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static synchronized Exception getVerificationResult(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Object o = verificationResults.get(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (o == PROVIDER_VERIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return (Exception)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (verifyingProviders.get(p) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // this method is static synchronized, must be recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // return failure now but do not save the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return new NoSuchProviderException("Recursion during verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            verifyingProviders.put(p, Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            URL providerURL = getCodeBase(p.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            verifyProviderJar(providerURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Verified ok, cache result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            verificationResults.put(p, PROVIDER_VERIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            verificationResults.put(p, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            verifyingProviders.remove(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // return whether this provider is properly signed and can be used by JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static boolean canUseProvider(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return getVerificationResult(p) == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // dummy object to represent null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static final URL NULL_URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            NULL_URL = new URL("http://null.sun.com/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // 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
   215
    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
   216
            new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Retuns the CodeBase for the given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   221
    static URL getCodeBase(final Class<?> clazz) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   222
        URL url = codeBaseCacheRef.get(clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (url == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   224
            url = AccessController.doPrivileged(new PrivilegedAction<URL>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   225
                public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    ProtectionDomain pd = clazz.getProtectionDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    if (pd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        CodeSource cs = pd.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            return cs.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    return NULL_URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            codeBaseCacheRef.put(clazz, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return (url == NULL_URL) ? null : url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static void setupJurisdictionPolicies() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String javaHomeDir = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        String sep = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        String pathToPolicyJar = javaHomeDir + sep + "lib" + sep +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            "security" + sep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        File exportJar = new File(pathToPolicyJar, "US_export_policy.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        File importJar = new File(pathToPolicyJar, "local_policy.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        URL jceCipherURL = ClassLoader.getSystemResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ("javax/crypto/Cipher.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if ((jceCipherURL == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                !exportJar.exists() || !importJar.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                ("Cannot locate policy or framework files!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Read jurisdiction policies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        CryptoPermissions defaultExport = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        CryptoPermissions exemptExport = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        loadPolicies(exportJar, defaultExport, exemptExport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        CryptoPermissions defaultImport = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        CryptoPermissions exemptImport = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        loadPolicies(importJar, defaultImport, exemptImport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Merge the export and import policies for default applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (defaultExport.isEmpty() || defaultImport.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new SecurityException("Missing mandatory jurisdiction " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                        "policy files");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        defaultPolicy = defaultExport.getMinimum(defaultImport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // Merge the export and import policies for exempt applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (exemptExport.isEmpty())  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            exemptPolicy = exemptImport.isEmpty() ? null : exemptImport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            exemptPolicy = exemptExport.getMinimum(exemptImport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Load the policies from the specified file. Also checks that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * policies are correctly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static void loadPolicies(File jarPathName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                     CryptoPermissions defaultPolicy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                     CryptoPermissions exemptPolicy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        JarFile jf = new JarFile(jarPathName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   293
        Enumeration<JarEntry> entries = jf.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        while (entries.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   295
            JarEntry je = entries.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (je.getName().startsWith("default_")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    is = jf.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    defaultPolicy.load(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                } else if (je.getName().startsWith("exempt_")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    is = jf.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    exemptPolicy.load(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // Enforce the signer restraint, i.e. signer of JCE framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // jar should also be the signer of the two jurisdiction policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            JarVerifier.verifyPolicySigned(je.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Close and nullify the JarFile reference to help GC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static CryptoPermissions getDefaultPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static CryptoPermissions getExemptPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return exemptPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    static boolean isRestricted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return isRestricted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}