Path: blob/master/src/java.base/share/classes/javax/crypto/CryptoAllPermission.java
41152 views
/*1* Copyright (c) 1999, 2019, 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.util.Enumeration;29import java.util.Vector;3031/**32* The CryptoAllPermission is a permission that implies33* any other crypto permissions.34*35* @see java.security.Permission36* @see java.security.AllPermission37*38* @author Sharon Liu39* @since 1.440*/4142final class CryptoAllPermission extends CryptoPermission {4344@java.io.Serial45private static final long serialVersionUID = -5066513634293192112L;4647// This class is similar to java.security.AllPermission.48static final String ALG_NAME = "CryptoAllPermission";49static final CryptoAllPermission INSTANCE =50new CryptoAllPermission();5152private CryptoAllPermission() {53super(ALG_NAME);54}5556/**57* Checks if the specified permission is implied by58* this object.59*60* @param p the permission to check against.61*62* @return true if the specified permission is an63* instance of CryptoPermission.64*/65public boolean implies(Permission p) {66return (p instanceof CryptoPermission);67}6869/**70* Checks two CryptoAllPermission objects for equality.71* Two CryptoAllPermission objects are always equal.72*73* @param obj the object to test for equality with this object.74*75* @return true if <i>obj</i> is a CryptoAllPermission object.76*/77public boolean equals(Object obj) {78return (obj == INSTANCE);79}8081/**82*83* Returns the hash code value for this object.84*85* @return a hash code value for this object.86*/87public int hashCode() {88return 1;89}9091/**92* Returns a new PermissionCollection object for storing93* CryptoAllPermission objects.94* <p>95*96* @return a new PermissionCollection object suitable for97* storing CryptoAllPermissions.98*/99public PermissionCollection newPermissionCollection() {100return new CryptoAllPermissionCollection();101}102}103104/**105* A CryptoAllPermissionCollection stores a collection106* of CryptoAllPermission permissions.107*108* @see java.security.Permission109* @see java.security.Permissions110* @see javax.crypto.CryptoPermission111*112* @author Sharon Liu113*/114final class CryptoAllPermissionCollection extends PermissionCollection115implements java.io.Serializable116{117118@java.io.Serial119private static final long serialVersionUID = 7450076868380144072L;120121// true if a CryptoAllPermission has been added122private boolean all_allowed;123124/**125* Create an empty CryptoAllPermissions object.126*/127CryptoAllPermissionCollection() {128all_allowed = false;129}130131/**132* Adds a permission to the CryptoAllPermissions.133*134* @param permission the Permission object to add.135*136* @exception SecurityException - if this CryptoAllPermissionCollection137* object has been marked readonly138*/139public void add(Permission permission) {140if (isReadOnly())141throw new SecurityException("attempt to add a Permission to " +142"a readonly PermissionCollection");143144if (permission != CryptoAllPermission.INSTANCE)145return;146147all_allowed = true;148}149150/**151* Check and see if this set of permissions implies the permissions152* expressed in "permission".153*154* @param permission the Permission object to compare155*156* @return true if the given permission is implied by this157* CryptoAllPermissionCollection.158*/159public boolean implies(Permission permission) {160if (!(permission instanceof CryptoPermission)) {161return false;162}163return all_allowed;164}165166/**167* Returns an enumeration of all the CryptoAllPermission168* objects in the container.169*170* @return an enumeration of all the CryptoAllPermission objects.171*/172public Enumeration<Permission> elements() {173Vector<Permission> v = new Vector<>(1);174if (all_allowed) v.add(CryptoAllPermission.INSTANCE);175return v.elements();176}177}178179180