src/java.base/share/classes/javax/crypto/CryptoAllPermission.java
changeset 47216 71c04702a3d5
parent 25859 3317bb8137f4
child 57950 4612a3cfb927
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package javax.crypto;
       
    27 
       
    28 import java.security.*;
       
    29 import java.util.Enumeration;
       
    30 import java.util.Vector;
       
    31 
       
    32 /**
       
    33  * The CryptoAllPermission is a permission that implies
       
    34  * any other crypto permissions.
       
    35  * <p>
       
    36  *
       
    37  * @see java.security.Permission
       
    38  * @see java.security.AllPermission
       
    39  *
       
    40  * @author Sharon Liu
       
    41  * @since 1.4
       
    42  */
       
    43 
       
    44 final class CryptoAllPermission extends CryptoPermission {
       
    45 
       
    46     private static final long serialVersionUID = -5066513634293192112L;
       
    47 
       
    48     // This class is similar to java.security.AllPermission.
       
    49     static final String ALG_NAME = "CryptoAllPermission";
       
    50     static final CryptoAllPermission INSTANCE =
       
    51         new CryptoAllPermission();
       
    52 
       
    53     private CryptoAllPermission() {
       
    54         super(ALG_NAME);
       
    55     }
       
    56 
       
    57     /**
       
    58      * Checks if the specified permission is implied by
       
    59      * this object.
       
    60      *
       
    61      * @param p the permission to check against.
       
    62      *
       
    63      * @return true if the specified permission is an
       
    64      * instance of CryptoPermission.
       
    65      */
       
    66     public boolean implies(Permission p) {
       
    67          return (p instanceof CryptoPermission);
       
    68     }
       
    69 
       
    70     /**
       
    71      * Checks two CryptoAllPermission objects for equality.
       
    72      * Two CryptoAllPermission objects are always equal.
       
    73      *
       
    74      * @param obj the object to test for equality with this object.
       
    75      *
       
    76      * @return true if <i>obj</i> is a CryptoAllPermission object.
       
    77      */
       
    78     public boolean equals(Object obj) {
       
    79         return (obj == INSTANCE);
       
    80     }
       
    81 
       
    82     /**
       
    83      *
       
    84      * Returns the hash code value for this object.
       
    85      *
       
    86      * @return a hash code value for this object.
       
    87      */
       
    88     public int hashCode() {
       
    89         return 1;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Returns a new PermissionCollection object for storing
       
    94      * CryptoAllPermission objects.
       
    95      * <p>
       
    96      *
       
    97      * @return a new PermissionCollection object suitable for
       
    98      * storing CryptoAllPermissions.
       
    99      */
       
   100     public PermissionCollection newPermissionCollection() {
       
   101         return new CryptoAllPermissionCollection();
       
   102     }
       
   103 }
       
   104 
       
   105 /**
       
   106  * A CryptoAllPermissionCollection stores a collection
       
   107  * of CryptoAllPermission permissions.
       
   108  *
       
   109  * @see java.security.Permission
       
   110  * @see java.security.Permissions
       
   111  * @see javax.crypto.CryptoPermission
       
   112  *
       
   113  * @author Sharon Liu
       
   114  */
       
   115 final class CryptoAllPermissionCollection extends PermissionCollection
       
   116     implements java.io.Serializable
       
   117 {
       
   118 
       
   119     private static final long serialVersionUID = 7450076868380144072L;
       
   120 
       
   121     // true if a CryptoAllPermission has been added
       
   122     private boolean all_allowed;
       
   123 
       
   124     /**
       
   125      * Create an empty CryptoAllPermissions object.
       
   126      */
       
   127     CryptoAllPermissionCollection() {
       
   128         all_allowed = false;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Adds a permission to the CryptoAllPermissions.
       
   133      *
       
   134      * @param permission the Permission object to add.
       
   135      *
       
   136      * @exception SecurityException - if this CryptoAllPermissionCollection
       
   137      * object has been marked readonly
       
   138      */
       
   139     public void add(Permission permission) {
       
   140         if (isReadOnly())
       
   141             throw new SecurityException("attempt to add a Permission to " +
       
   142                                         "a readonly PermissionCollection");
       
   143 
       
   144         if (permission != CryptoAllPermission.INSTANCE)
       
   145             return;
       
   146 
       
   147         all_allowed = true;
       
   148     }
       
   149 
       
   150     /**
       
   151      * Check and see if this set of permissions implies the permissions
       
   152      * expressed in "permission".
       
   153      *
       
   154      * @param permission the Permission object to compare
       
   155      *
       
   156      * @return true if the given permission is implied by this
       
   157      * CryptoAllPermissionCollection.
       
   158      */
       
   159     public boolean implies(Permission permission) {
       
   160         if (!(permission instanceof CryptoPermission)) {
       
   161             return false;
       
   162         }
       
   163         return all_allowed;
       
   164     }
       
   165 
       
   166     /**
       
   167      * Returns an enumeration of all the CryptoAllPermission
       
   168      * objects in the  container.
       
   169      *
       
   170      * @return an enumeration of all the CryptoAllPermission objects.
       
   171      */
       
   172     public Enumeration<Permission> elements() {
       
   173         Vector<Permission> v = new Vector<>(1);
       
   174         if (all_allowed) v.add(CryptoAllPermission.INSTANCE);
       
   175         return v.elements();
       
   176     }
       
   177 }