J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | { |
| 141 | if (isReadOnly()) |
| 142 | throw new SecurityException("attempt to add a Permission to " + |
| 143 | "a readonly PermissionCollection"); |
| 144 | |
| 145 | if (permission != CryptoAllPermission.INSTANCE) |
| 146 | return; |
| 147 | |
| 148 | all_allowed = true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check and see if this set of permissions implies the permissions |
| 153 | * expressed in "permission". |
| 154 | * |
| 155 | * @param p the Permission object to compare |
| 156 | * |
| 157 | * @return true if the given permission is implied by this |
| 158 | * CryptoAllPermissionCollection. |
| 159 | */ |
| 160 | public boolean implies(Permission permission) |
| 161 | { |
| 162 | if (!(permission instanceof CryptoPermission)) { |
| 163 | return false; |
| 164 | } |
| 165 | return all_allowed; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns an enumeration of all the CryptoAllPermission |
| 170 | * objects in the container. |
| 171 | * |
| 172 | * @return an enumeration of all the CryptoAllPermission objects. |
| 173 | */ |
| 174 | public Enumeration elements() { |
| 175 | Vector v = new Vector(1); |
| 176 | if (all_allowed) v.add(CryptoAllPermission.INSTANCE); |
| 177 | return v.elements(); |
| 178 | } |
| 179 | } |