Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/CryptoPermission.java
38829 views
/*1* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto;2627import java.security.*;28import java.security.spec.AlgorithmParameterSpec;29import java.io.Serializable;30import java.util.Enumeration;31import java.util.Vector;3233import javax.crypto.spec.*;3435/**36* The CryptoPermission class extends the37* java.security.Permission class. A38* CryptoPermission object is used to represent39* the ability of an application/applet to use certain40* algorithms with certain key sizes and other41* restrictions in certain environments. <p>42*43* @see java.security.Permission44*45* @author Jan Luehe46* @author Sharon Liu47* @since 1.448*/49class CryptoPermission extends java.security.Permission {5051private static final long serialVersionUID = 8987399626114087514L;5253private String alg;54private int maxKeySize = Integer.MAX_VALUE; // no restriction on maxKeySize55private String exemptionMechanism = null;56private AlgorithmParameterSpec algParamSpec = null;57private boolean checkParam = false; // no restriction on param5859static final String ALG_NAME_WILDCARD = "*";6061/**62* Constructor that takes an algorithm name.63*64* This constructor implies that the given algorithm can be65* used without any restrictions.66*67* @param alg the algorithm name.68*/69CryptoPermission(String alg) {70super(null);71this.alg = alg;72}7374/**75* Constructor that takes an algorithm name and a maximum76* key size.77*78* This constructor implies that the given algorithm can be79* used with a key size up to <code>maxKeySize</code>.80*81* @param alg the algorithm name.82*83* @param maxKeySize the maximum allowable key size,84* specified in number of bits.85*/86CryptoPermission(String alg, int maxKeySize) {87super(null);88this.alg = alg;89this.maxKeySize = maxKeySize;90}9192/**93* Constructor that takes an algorithm name, a maximum94* key size, and an AlgorithmParameterSpec object.95*96* This constructor implies that the given algorithm can be97* used with a key size up to <code>maxKeySize</code>, and98* algorithm99* parameters up to the limits set in <code>algParamSpec</code>.100*101* @param alg the algorithm name.102*103* @param maxKeySize the maximum allowable key size,104* specified in number of bits.105*106* @param algParamSpec the limits for allowable algorithm107* parameters.108*/109CryptoPermission(String alg,110int maxKeySize,111AlgorithmParameterSpec algParamSpec) {112super(null);113this.alg = alg;114this.maxKeySize = maxKeySize;115this.checkParam = true;116this.algParamSpec = algParamSpec;117}118119/**120* Constructor that takes an algorithm name and the name of121* an exemption mechanism.122*123* This constructor implies that the given algorithm can be124* used without any key size or algorithm parameter restrictions125* provided that the specified exemption mechanism is enforced.126*127* @param alg the algorithm name.128*129* @param exemptionMechanism the name of the exemption mechanism.130*/131CryptoPermission(String alg,132String exemptionMechanism) {133super(null);134this.alg = alg;135this.exemptionMechanism = exemptionMechanism;136}137138/**139* Constructor that takes an algorithm name, a maximum key140* size, and the name of an exemption mechanism.141*142* This constructor implies that the given algorithm can be143* used with a key size up to <code>maxKeySize</code>144* provided that the145* specified exemption mechanism is enforced.146*147* @param alg the algorithm name.148* @param maxKeySize the maximum allowable key size,149* specified in number of bits.150* @param exemptionMechanism the name of the exemption151* mechanism.152*/153CryptoPermission(String alg,154int maxKeySize,155String exemptionMechanism) {156super(null);157this.alg = alg;158this.exemptionMechanism = exemptionMechanism;159this.maxKeySize = maxKeySize;160}161162/**163* Constructor that takes an algorithm name, a maximum key164* size, the name of an exemption mechanism, and an165* AlgorithmParameterSpec object.166*167* This constructor implies that the given algorithm can be168* used with a key size up to <code>maxKeySize</code>169* and algorithm170* parameters up to the limits set in <code>algParamSpec</code>171* provided that172* the specified exemption mechanism is enforced.173*174* @param alg the algorithm name.175* @param maxKeySize the maximum allowable key size,176* specified in number of bits.177* @param algParamSpec the limit for allowable algorithm178* parameter spec.179* @param exemptionMechanism the name of the exemption180* mechanism.181*/182CryptoPermission(String alg,183int maxKeySize,184AlgorithmParameterSpec algParamSpec,185String exemptionMechanism) {186super(null);187this.alg = alg;188this.exemptionMechanism = exemptionMechanism;189this.maxKeySize = maxKeySize;190this.checkParam = true;191this.algParamSpec = algParamSpec;192}193194/**195* Checks if the specified permission is "implied" by196* this object.197* <p>198* More specifically, this method returns true if:<p>199* <ul>200* <li> <i>p</i> is an instance of CryptoPermission, and<p>201* <li> <i>p</i>'s algorithm name equals or (in the case of wildcards)202* is implied by this permission's algorithm name, and<p>203* <li> <i>p</i>'s maximum allowable key size is less or204* equal to this permission's maximum allowable key size, and<p>205* <li> <i>p</i>'s algorithm parameter spec equals or is206* implied by this permission's algorithm parameter spec, and<p>207* <li> <i>p</i>'s exemptionMechanism equals or208* is implied by this permission's209* exemptionMechanism (a <code>null</code> exemption mechanism210* implies any other exemption mechanism).211* </ul>212*213* @param p the permission to check against.214*215* @return true if the specified permission is equal to or216* implied by this permission, false otherwise.217*/218public boolean implies(Permission p) {219if (!(p instanceof CryptoPermission))220return false;221222CryptoPermission cp = (CryptoPermission)p;223224if ((!alg.equalsIgnoreCase(cp.alg)) &&225(!alg.equalsIgnoreCase(ALG_NAME_WILDCARD))) {226return false;227}228229// alg is the same as cp's alg or230// alg is a wildcard.231if (cp.maxKeySize <= this.maxKeySize) {232// check algParamSpec.233if (!impliesParameterSpec(cp.checkParam, cp.algParamSpec)) {234return false;235}236237// check exemptionMechanism.238if (impliesExemptionMechanism(cp.exemptionMechanism)) {239return true;240}241}242243return false;244}245246/**247* Checks two CryptoPermission objects for equality. Checks that248* <code>obj</code> is a CryptoPermission, and has the same249* algorithm name,250* exemption mechanism name, maximum allowable key size and251* algorithm parameter spec252* as this object.253* <P>254* @param obj the object to test for equality with this object.255* @return true if <code>obj</code> is equal to this object.256*/257public boolean equals(Object obj) {258if (obj == this)259return true;260261if (!(obj instanceof CryptoPermission))262return false;263264CryptoPermission that = (CryptoPermission) obj;265266if (!(alg.equalsIgnoreCase(that.alg)) ||267(maxKeySize != that.maxKeySize)) {268return false;269}270if (this.checkParam != that.checkParam) {271return false;272}273return (equalObjects(this.exemptionMechanism,274that.exemptionMechanism) &&275equalObjects(this.algParamSpec,276that.algParamSpec));277}278279/**280* Returns the hash code value for this object.281*282* @return a hash code value for this object.283*/284285public int hashCode() {286int retval = alg.hashCode();287retval ^= maxKeySize;288if (exemptionMechanism != null) {289retval ^= exemptionMechanism.hashCode();290}291if (checkParam) retval ^= 100;292if (algParamSpec != null) {293retval ^= algParamSpec.hashCode();294}295return retval;296}297298/**299* There is no action defined for a CryptoPermission300* onject.301*/302public String getActions()303{304return null;305}306307/**308* Returns a new PermissionCollection object for storing309* CryptoPermission objects.310*311* @return a new PermissionCollection object suitable for storing312* CryptoPermissions.313*/314315public PermissionCollection newPermissionCollection() {316return new CryptoPermissionCollection();317}318319/**320* Returns the algorithm name associated with321* this CryptoPermission object.322*/323final String getAlgorithm() {324return alg;325}326327/**328* Returns the exemption mechanism name329* associated with this CryptoPermission330* object.331*/332final String getExemptionMechanism() {333return exemptionMechanism;334}335336/**337* Returns the maximum allowable key size associated338* with this CryptoPermission object.339*/340final int getMaxKeySize() {341return maxKeySize;342}343344/**345* Returns true if there is a limitation on the346* AlgorithmParameterSpec associated with this347* CryptoPermission object and false if otherwise.348*/349final boolean getCheckParam() {350return checkParam;351}352353/**354* Returns the AlgorithmParameterSpec355* associated with this CryptoPermission356* object.357*/358final AlgorithmParameterSpec getAlgorithmParameterSpec() {359return algParamSpec;360}361362/**363* Returns a string describing this CryptoPermission. The convention is to364* specify the class name, the algorithm name, the maximum allowable365* key size, and the name of the exemption mechanism, in the following366* format: '("ClassName" "algorithm" "keysize" "exemption_mechanism")'.367*368* @return information about this CryptoPermission.369*/370public String toString() {371StringBuilder buf = new StringBuilder(100);372buf.append("(CryptoPermission " + alg + " " + maxKeySize);373if (algParamSpec != null) {374if (algParamSpec instanceof RC2ParameterSpec) {375buf.append(" , effective " +376((RC2ParameterSpec)algParamSpec).getEffectiveKeyBits());377} else if (algParamSpec instanceof RC5ParameterSpec) {378buf.append(" , rounds " +379((RC5ParameterSpec)algParamSpec).getRounds());380}381}382if (exemptionMechanism != null) { // OPTIONAL383buf.append(" " + exemptionMechanism);384}385buf.append(")");386return buf.toString();387}388389private boolean impliesExemptionMechanism(String exemptionMechanism) {390if (this.exemptionMechanism == null) {391return true;392}393394if (exemptionMechanism == null) {395return false;396}397398if (this.exemptionMechanism.equals(exemptionMechanism)) {399return true;400}401402return false;403}404405private boolean impliesParameterSpec(boolean checkParam,406AlgorithmParameterSpec algParamSpec) {407if ((this.checkParam) && checkParam) {408if (algParamSpec == null) {409return true;410} else if (this.algParamSpec == null) {411return false;412}413414if (this.algParamSpec.getClass() != algParamSpec.getClass()) {415return false;416}417418if (algParamSpec instanceof RC2ParameterSpec) {419if (((RC2ParameterSpec)algParamSpec).getEffectiveKeyBits() <=420((RC2ParameterSpec)421(this.algParamSpec)).getEffectiveKeyBits()) {422return true;423}424}425426if (algParamSpec instanceof RC5ParameterSpec) {427if (((RC5ParameterSpec)algParamSpec).getRounds() <=428((RC5ParameterSpec)this.algParamSpec).getRounds()) {429return true;430}431}432433if (algParamSpec instanceof PBEParameterSpec) {434if (((PBEParameterSpec)algParamSpec).getIterationCount() <=435((PBEParameterSpec)this.algParamSpec).getIterationCount()) {436return true;437}438}439440// For classes we don't know, the following441// may be the best try.442if (this.algParamSpec.equals(algParamSpec)) {443return true;444}445return false;446} else if (this.checkParam) {447return false;448} else {449return true;450}451}452453private boolean equalObjects(Object obj1, Object obj2) {454if (obj1 == null) {455return (obj2 == null ? true : false);456}457458return obj1.equals(obj2);459}460}461462/**463* A CryptoPermissionCollection stores a set of CryptoPermission464* permissions.465*466* @see java.security.Permission467* @see java.security.Permissions468* @see java.security.PermissionCollection469*470* @author Sharon Liu471*/472final class CryptoPermissionCollection extends PermissionCollection473implements Serializable474{475private static final long serialVersionUID = -511215555898802763L;476477private Vector<Permission> permissions;478479/**480* Creates an empty CryptoPermissionCollection481* object.482*/483CryptoPermissionCollection() {484permissions = new Vector<Permission>(3);485}486487/**488* Adds a permission to the CryptoPermissionCollection.489*490* @param permission the Permission object to add.491*492* @exception SecurityException - if this CryptoPermissionCollection493* object has been marked <i>readOnly</i>.494*/495public void add(Permission permission) {496if (isReadOnly())497throw new SecurityException("attempt to add a Permission " +498"to a readonly PermissionCollection");499500if (!(permission instanceof CryptoPermission))501return;502503permissions.addElement(permission);504}505506/**507* Check and see if this CryptoPermission object implies508* the given Permission object.509*510* @param permission the Permission object to compare511*512* @return true if the given permission is implied by this513* CryptoPermissionCollection, false if not.514*/515public boolean implies(Permission permission) {516if (!(permission instanceof CryptoPermission))517return false;518519CryptoPermission cp = (CryptoPermission)permission;520521Enumeration<Permission> e = permissions.elements();522523while (e.hasMoreElements()) {524CryptoPermission x = (CryptoPermission) e.nextElement();525if (x.implies(cp)) {526return true;527}528}529return false;530}531532/**533* Returns an enumeration of all the CryptoPermission objects534* in the container.535*536* @return an enumeration of all the CryptoPermission objects.537*/538539public Enumeration<Permission> elements() {540return permissions.elements();541}542}543544545