0N/A/*
5297N/A * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.crypto;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.util.jar.*;
5297N/Aimport java.util.concurrent.ConcurrentHashMap;
5297N/Aimport java.util.concurrent.ConcurrentMap;
0N/A
0N/A/**
0N/A * The JCE security manager.
0N/A *
0N/A * <p>The JCE security manager is responsible for determining the maximum
0N/A * allowable cryptographic strength for a given applet/application, for a given
0N/A * algorithm, by consulting the configured jurisdiction policy files and
0N/A * the cryptographic permissions bundled with the applet/application.
0N/A *
0N/A * <p>Note that this security manager is never installed, only instantiated.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Afinal class JceSecurityManager extends SecurityManager {
0N/A
0N/A private static final CryptoPermissions defaultPolicy;
0N/A private static final CryptoPermissions exemptPolicy;
0N/A private static final CryptoAllPermission allPerm;
0N/A private static final Vector TrustedCallersCache = new Vector(2);
5297N/A private static final ConcurrentMap<URL,CryptoPermissions> exemptCache =
5297N/A new ConcurrentHashMap<>();
5297N/A private static final CryptoPermissions CACHE_NULL_MARK =
5297N/A new CryptoPermissions();
0N/A
0N/A // singleton instance
0N/A static final JceSecurityManager INSTANCE;
0N/A
0N/A static {
0N/A defaultPolicy = JceSecurity.getDefaultPolicy();
0N/A exemptPolicy = JceSecurity.getExemptPolicy();
0N/A allPerm = CryptoAllPermission.INSTANCE;
0N/A INSTANCE = (JceSecurityManager)
0N/A AccessController.doPrivileged(new PrivilegedAction() {
0N/A public Object run() {
0N/A return new JceSecurityManager();
0N/A }
0N/A });
0N/A }
0N/A
0N/A private JceSecurityManager() {
0N/A // empty
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum allowable crypto strength for the given
0N/A * applet/application, for the given algorithm.
0N/A */
0N/A CryptoPermission getCryptoPermission(String alg) {
0N/A // Need to convert to uppercase since the crypto perm
0N/A // lookup is case sensitive.
0N/A alg = alg.toUpperCase(Locale.ENGLISH);
0N/A
0N/A // If CryptoAllPermission is granted by default, we return that.
0N/A // Otherwise, this will be the permission we return if anything goes
0N/A // wrong.
0N/A CryptoPermission defaultPerm = getDefaultPermission(alg);
0N/A if (defaultPerm == CryptoAllPermission.INSTANCE) {
0N/A return defaultPerm;
0N/A }
0N/A
0N/A // Determine the codebase of the caller of the JCE API.
0N/A // This is the codebase of the first class which is not in
0N/A // javax.crypto.* packages.
0N/A // NOTE: javax.crypto.* package maybe subject to package
0N/A // insertion, so need to check its classloader as well.
0N/A Class[] context = getClassContext();
0N/A URL callerCodeBase = null;
0N/A int i;
0N/A for (i=0; i<context.length; i++) {
0N/A Class cls = context[i];
0N/A callerCodeBase = JceSecurity.getCodeBase(cls);
0N/A if (callerCodeBase != null) {
0N/A break;
0N/A } else {
0N/A if (cls.getName().startsWith("javax.crypto.")) {
0N/A // skip jce classes since they aren't the callers
0N/A continue;
0N/A }
0N/A // use default permission when the caller is system classes
0N/A return defaultPerm;
0N/A }
0N/A }
0N/A
0N/A if (i == context.length) {
0N/A return defaultPerm;
0N/A }
0N/A
5297N/A CryptoPermissions appPerms = exemptCache.get(callerCodeBase);
5297N/A if (appPerms == null) {
5297N/A // no match found in cache
5297N/A synchronized (this.getClass()) {
5297N/A appPerms = exemptCache.get(callerCodeBase);
5297N/A if (appPerms == null) {
5297N/A appPerms = getAppPermissions(callerCodeBase);
5297N/A exemptCache.putIfAbsent(callerCodeBase,
5297N/A (appPerms == null? CACHE_NULL_MARK:appPerms));
5297N/A }
0N/A }
0N/A }
5297N/A if (appPerms == null || appPerms == CACHE_NULL_MARK) {
0N/A return defaultPerm;
0N/A }
0N/A
0N/A // If the app was granted the special CryptoAllPermission, return that.
0N/A if (appPerms.implies(allPerm)) {
0N/A return allPerm;
0N/A }
0N/A
0N/A // Check if the crypto permissions granted to the app contain a
0N/A // crypto permission for the requested algorithm that does not require
0N/A // any exemption mechanism to be enforced.
0N/A // Return that permission, if present.
0N/A PermissionCollection appPc = appPerms.getPermissionCollection(alg);
0N/A if (appPc == null) {
0N/A return defaultPerm;
0N/A }
0N/A Enumeration enum_ = appPc.elements();
0N/A while (enum_.hasMoreElements()) {
0N/A CryptoPermission cp = (CryptoPermission)enum_.nextElement();
0N/A if (cp.getExemptionMechanism() == null) {
0N/A return cp;
0N/A }
0N/A }
0N/A
0N/A // Check if the jurisdiction file for exempt applications contains
0N/A // any entries for the requested algorithm.
0N/A // If not, return the default permission.
0N/A PermissionCollection exemptPc =
0N/A exemptPolicy.getPermissionCollection(alg);
0N/A if (exemptPc == null) {
0N/A return defaultPerm;
0N/A }
0N/A
0N/A // In the jurisdiction file for exempt applications, go through the
0N/A // list of CryptoPermission entries for the requested algorithm, and
0N/A // stop at the first entry:
0N/A // - that is implied by the collection of crypto permissions granted
0N/A // to the app, and
0N/A // - whose exemption mechanism is available from one of the
0N/A // registered CSPs
0N/A enum_ = exemptPc.elements();
0N/A while (enum_.hasMoreElements()) {
0N/A CryptoPermission cp = (CryptoPermission)enum_.nextElement();
0N/A try {
0N/A ExemptionMechanism.getInstance(cp.getExemptionMechanism());
0N/A if (cp.getAlgorithm().equals(
0N/A CryptoPermission.ALG_NAME_WILDCARD)) {
0N/A CryptoPermission newCp;
0N/A if (cp.getCheckParam()) {
0N/A newCp = new CryptoPermission(
0N/A alg, cp.getMaxKeySize(),
0N/A cp.getAlgorithmParameterSpec(),
0N/A cp.getExemptionMechanism());
0N/A } else {
0N/A newCp = new CryptoPermission(
0N/A alg, cp.getMaxKeySize(),
0N/A cp.getExemptionMechanism());
0N/A }
0N/A if (appPerms.implies(newCp)) {
0N/A return newCp;
0N/A }
0N/A }
0N/A
0N/A if (appPerms.implies(cp)) {
0N/A return cp;
0N/A }
0N/A } catch (Exception e) {
0N/A continue;
0N/A }
0N/A }
0N/A return defaultPerm;
0N/A }
0N/A
0N/A private static CryptoPermissions getAppPermissions(URL callerCodeBase) {
0N/A // Check if app is exempt, and retrieve the permissions bundled with it
0N/A try {
0N/A return JceSecurity.verifyExemptJar(callerCodeBase);
0N/A } catch (Exception e) {
0N/A // Jar verification fails
0N/A return null;
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the default permission for the given algorithm.
0N/A */
0N/A private CryptoPermission getDefaultPermission(String alg) {
0N/A Enumeration enum_ =
0N/A defaultPolicy.getPermissionCollection(alg).elements();
0N/A return (CryptoPermission)enum_.nextElement();
0N/A }
0N/A
0N/A // See bug 4341369 & 4334690 for more info.
0N/A boolean isCallerTrusted() {
0N/A // Get the caller and its codebase.
0N/A Class[] context = getClassContext();
0N/A URL callerCodeBase = null;
0N/A int i;
0N/A for (i=0; i<context.length; i++) {
0N/A callerCodeBase = JceSecurity.getCodeBase(context[i]);
0N/A if (callerCodeBase != null) {
0N/A break;
0N/A }
0N/A }
0N/A // The caller is in the JCE framework.
0N/A if (i == context.length) {
0N/A return true;
0N/A }
0N/A //The caller has been verified.
0N/A if (TrustedCallersCache.contains(context[i])) {
0N/A return true;
0N/A }
0N/A // Check whether the caller is a trusted provider.
0N/A try {
0N/A JceSecurity.verifyProviderJar(callerCodeBase);
0N/A } catch (Exception e2) {
0N/A return false;
0N/A }
0N/A TrustedCallersCache.addElement(context[i]);
0N/A return true;
0N/A }
0N/A}