jdk/src/java.base/share/classes/javax/crypto/JceSecurityManager.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43326 bbbdabc3f81b
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43326
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
13558
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    31
import java.util.concurrent.ConcurrentHashMap;
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    32
import java.util.concurrent.ConcurrentMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The JCE security manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>The JCE security manager is responsible for determining the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * allowable cryptographic strength for a given applet/application, for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * algorithm, by consulting the configured jurisdiction policy files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the cryptographic permissions bundled with the applet/application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>Note that this security manager is never installed, only instantiated.
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
 *
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 JceSecurityManager extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final CryptoPermissions defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final CryptoPermissions exemptPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final CryptoAllPermission allPerm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    54
    private static final Vector<Class<?>> TrustedCallersCache =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    55
            new Vector<>(2);
13558
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    56
    private static final ConcurrentMap<URL,CryptoPermissions> exemptCache =
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    57
            new ConcurrentHashMap<>();
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    58
    private static final CryptoPermissions CACHE_NULL_MARK =
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
    59
            new CryptoPermissions();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // singleton instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final JceSecurityManager INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        defaultPolicy = JceSecurity.getDefaultPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        exemptPolicy = JceSecurity.getExemptPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        allPerm = CryptoAllPermission.INSTANCE;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    68
        INSTANCE = AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
    69
                new PrivilegedAction<>() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    70
                    public JceSecurityManager run() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    71
                        return new JceSecurityManager();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    72
                    }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    73
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private JceSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns the maximum allowable crypto strength for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * applet/application, for the given algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    CryptoPermission getCryptoPermission(String alg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // Need to convert to uppercase since the crypto perm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // lookup is case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        alg = alg.toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // If CryptoAllPermission is granted by default, we return that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // Otherwise, this will be the permission we return if anything goes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        CryptoPermission defaultPerm = getDefaultPermission(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (defaultPerm == CryptoAllPermission.INSTANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Determine the codebase of the caller of the JCE API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // This is the codebase of the first class which is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // javax.crypto.* packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // NOTE: javax.crypto.* package maybe subject to package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // insertion, so need to check its classloader as well.
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   102
        Class<?>[] context = getClassContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        URL callerCodeBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (i=0; i<context.length; i++) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   106
            Class<?> cls = context[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            callerCodeBase = JceSecurity.getCodeBase(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (callerCodeBase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (cls.getName().startsWith("javax.crypto.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    // skip jce classes since they aren't the callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // use default permission when the caller is system classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (i == context.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
13558
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   124
        CryptoPermissions appPerms = exemptCache.get(callerCodeBase);
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   125
        if (appPerms == null) {
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   126
            // no match found in cache
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   127
            synchronized (this.getClass()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   128
                appPerms = exemptCache.get(callerCodeBase);
13558
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   129
                if (appPerms == null) {
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   130
                    appPerms = getAppPermissions(callerCodeBase);
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   131
                    exemptCache.putIfAbsent(callerCodeBase,
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   132
                        (appPerms == null? CACHE_NULL_MARK:appPerms));
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   133
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
13558
3fb57310d83b 7107616: scalability bloker in javax.crypto.JceSecurityManager
valeriep
parents: 10336
diff changeset
   136
        if (appPerms == null || appPerms == CACHE_NULL_MARK) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // If the app was granted the special CryptoAllPermission, return that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (appPerms.implies(allPerm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return allPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // Check if the crypto permissions granted to the app contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // crypto permission for the requested algorithm that does not require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // any exemption mechanism to be enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Return that permission, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        PermissionCollection appPc = appPerms.getPermissionCollection(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (appPc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   153
        Enumeration<Permission> enum_ = appPc.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            CryptoPermission cp = (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (cp.getExemptionMechanism() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Check if the jurisdiction file for exempt applications contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // any entries for the requested algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // If not, return the default permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        PermissionCollection exemptPc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            exemptPolicy.getPermissionCollection(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (exemptPc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // In the jurisdiction file for exempt applications, go through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // list of CryptoPermission entries for the requested algorithm, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // stop at the first entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //  - that is implied by the collection of crypto permissions granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        //    to the app, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //  - whose exemption mechanism is available from one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        //    registered CSPs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        enum_ = exemptPc.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            CryptoPermission cp = (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                ExemptionMechanism.getInstance(cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (cp.getAlgorithm().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                      CryptoPermission.ALG_NAME_WILDCARD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    CryptoPermission newCp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    if (cp.getCheckParam()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        newCp = new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                alg, cp.getMaxKeySize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                cp.getAlgorithmParameterSpec(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        newCp = new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                alg, cp.getMaxKeySize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    if (appPerms.implies(newCp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        return newCp;
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
                if (appPerms.implies(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    return cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static CryptoPermissions getAppPermissions(URL callerCodeBase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // Check if app is exempt, and retrieve the permissions bundled with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return JceSecurity.verifyExemptJar(callerCodeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // Jar verification fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the default permission for the given algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private CryptoPermission getDefaultPermission(String alg) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   225
        Enumeration<Permission> enum_ =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            defaultPolicy.getPermissionCollection(alg).elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
43326
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   230
    // Only used by javax.crypto.Cipher constructor to disallow Cipher
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   231
    // objects being constructed by untrusted code (See bug 4341369 &
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   232
    // 4334690 for more info).
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   233
    boolean isCallerTrusted(Provider provider) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Get the caller and its codebase.
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 13558
diff changeset
   235
        Class<?>[] context = getClassContext();
43326
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   236
        if (context.length >= 3) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   237
            // context[0]: class javax.crypto.JceSecurityManager
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   238
            // context[1]: class javax.crypto.Cipher (or other JCE API class)
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   239
            // context[2]: this is what we are gonna check
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   240
            Class<?> caller = context[2];
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   241
            URL callerCodeBase = JceSecurity.getCodeBase(caller);
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   242
            if (callerCodeBase == null) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   243
                return true;
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   244
            }
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   245
            // The caller has been verified.
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   246
            if (TrustedCallersCache.contains(caller)) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   247
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
43326
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   249
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   250
            // Check the association between caller and provider
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   251
            Class<?> pCls = provider.getClass();
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   252
            Module pMod = pCls.getModule();
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   253
            // If they are in the same named module or share
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   254
            // the same codebase, then they are associated
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   255
            boolean sameOrigin = (pMod.isNamed()?
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   256
                caller.getModule().equals(pMod) :
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   257
                callerCodeBase.equals(JceSecurity.getCodeBase(pCls)));
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   258
            if (sameOrigin) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   259
                // The caller is from trusted provider
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   260
                if (ProviderVerifier.isTrustedCryptoProvider(provider)) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   261
                    TrustedCallersCache.addElement(caller);
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   262
                    return true;
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   263
                }
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   264
            } else {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   265
                // Don't include the provider in the subsequent
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   266
                // JceSecurity.verifyProvider(...) call
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   267
                provider = null;
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   268
            }
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   269
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   270
            // Check whether the caller is a trusted provider.
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   271
            try {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   272
                JceSecurity.verifyProvider(callerCodeBase, provider);
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   273
            } catch (Exception e2) {
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   274
                return false;
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   275
            }
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   276
            TrustedCallersCache.addElement(caller);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
43326
bbbdabc3f81b 8062731: Cipher object can be created without calling Cipher.getInstance
valeriep
parents: 30033
diff changeset
   279
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}