jdk/src/share/classes/javax/crypto/JceSecurityManager.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 13558 3fb57310d83b
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) 1999, 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: 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The JCE security manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>The JCE security manager is responsible for determining the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * allowable cryptographic strength for a given applet/application, for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * algorithm, by consulting the configured jurisdiction policy files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the cryptographic permissions bundled with the applet/application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>Note that this security manager is never installed, only instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
final class JceSecurityManager extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final CryptoPermissions defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final CryptoPermissions exemptPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final CryptoAllPermission allPerm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    52
    private static final Vector<Class<?>> TrustedCallersCache =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    53
            new Vector<>(2);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    54
    private static final Map<URL, CryptoPermissions> exemptCache =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    55
            new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // singleton instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final JceSecurityManager INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        defaultPolicy = JceSecurity.getDefaultPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        exemptPolicy = JceSecurity.getExemptPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        allPerm = CryptoAllPermission.INSTANCE;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    64
        INSTANCE = AccessController.doPrivileged(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    65
                new PrivilegedAction<JceSecurityManager>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    66
                    public JceSecurityManager run() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    67
                        return new JceSecurityManager();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    68
                    }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    69
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private JceSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Returns the maximum allowable crypto strength for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * applet/application, for the given algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    CryptoPermission getCryptoPermission(String alg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Need to convert to uppercase since the crypto perm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // lookup is case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        alg = alg.toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // If CryptoAllPermission is granted by default, we return that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Otherwise, this will be the permission we return if anything goes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        CryptoPermission defaultPerm = getDefaultPermission(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (defaultPerm == CryptoAllPermission.INSTANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Determine the codebase of the caller of the JCE API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // This is the codebase of the first class which is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // javax.crypto.* packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // NOTE: javax.crypto.* package maybe subject to package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // 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
    98
        Class<?>[] context = getClassContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        URL callerCodeBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        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
   102
            Class<?> cls = context[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            callerCodeBase = JceSecurity.getCodeBase(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (callerCodeBase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (cls.getName().startsWith("javax.crypto.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    // skip jce classes since they aren't the callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // use default permission when the caller is system classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (i == context.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        CryptoPermissions appPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized (this.getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (exemptCache.containsKey(callerCodeBase)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   123
                appPerms = exemptCache.get(callerCodeBase);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                appPerms = getAppPermissions(callerCodeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                exemptCache.put(callerCodeBase, appPerms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (appPerms == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // If the app was granted the special CryptoAllPermission, return that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (appPerms.implies(allPerm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return allPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // Check if the crypto permissions granted to the app contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // crypto permission for the requested algorithm that does not require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // any exemption mechanism to be enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Return that permission, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        PermissionCollection appPc = appPerms.getPermissionCollection(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (appPc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   147
        Enumeration<Permission> enum_ = appPc.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            CryptoPermission cp = (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (cp.getExemptionMechanism() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // Check if the jurisdiction file for exempt applications contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // any entries for the requested algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // If not, return the default permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        PermissionCollection exemptPc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            exemptPolicy.getPermissionCollection(alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (exemptPc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // In the jurisdiction file for exempt applications, go through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // list of CryptoPermission entries for the requested algorithm, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // stop at the first entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        //  - that is implied by the collection of crypto permissions granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //    to the app, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //  - whose exemption mechanism is available from one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        //    registered CSPs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        enum_ = exemptPc.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            CryptoPermission cp = (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                ExemptionMechanism.getInstance(cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (cp.getAlgorithm().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                      CryptoPermission.ALG_NAME_WILDCARD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    CryptoPermission newCp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    if (cp.getCheckParam()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        newCp = new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                alg, cp.getMaxKeySize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                cp.getAlgorithmParameterSpec(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        newCp = new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                alg, cp.getMaxKeySize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                cp.getExemptionMechanism());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    if (appPerms.implies(newCp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        return newCp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (appPerms.implies(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    return cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return defaultPerm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static CryptoPermissions getAppPermissions(URL callerCodeBase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Check if app is exempt, and retrieve the permissions bundled with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return JceSecurity.verifyExemptJar(callerCodeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // Jar verification fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return null;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns the default permission for the given algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private CryptoPermission getDefaultPermission(String alg) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   219
        Enumeration<Permission> enum_ =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            defaultPolicy.getPermissionCollection(alg).elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // See  bug 4341369 & 4334690 for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    boolean isCallerTrusted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // Get the caller and its codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Class[] context = getClassContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        URL callerCodeBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (i=0; i<context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            callerCodeBase = JceSecurity.getCodeBase(context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (callerCodeBase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // The caller is in the JCE framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (i == context.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        //The caller has been verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (TrustedCallersCache.contains(context[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // Check whether the caller is a trusted provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            JceSecurity.verifyProviderJar(callerCodeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        TrustedCallersCache.addElement(context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}