1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/* * @(#)CardPermission.java 1.3 06/07/20 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.smartcardio; import java.io.*; import java.security.Permission; /** * A permission for Smart Card operations. A CardPermission consists of the * name of the card terminal the permission applies to and a set of actions * that are valid for that terminal. * * <p>A CardPermission with a name of <code>*</code> applies to all * card terminals. The actions string is a comma separated list of the actions * listed below, or <code>*</code> to signify "all actions." * * <p>Individual actions are: * <dl> * <dt>connect * <dd>connect to a card using * {@linkplain CardTerminal#connect CardTerminal.connect()} * * <dt>reset * <dd>reset the card using {@linkplain Card#disconnect Card.disconnect(true)} * * <dt>exclusive * <dd>establish exclusive access to a card using * {@linkplain Card#beginExclusive} and {@linkplain Card#endExclusive * endExclusive()} * * <dt>transmitControl * <dd>transmit a control command using * {@linkplain Card#transmitControlCommand Card.transmitControlCommand()} * * <dt>getBasicChannel * <dd>obtain the basic logical channel using * {@linkplain Card#getBasicChannel} * * <dt>openLogicalChannel * <dd>open a new logical channel using * {@linkplain Card#openLogicalChannel} * * </dl> * * @version 1.3, 07/20/06 * @since 1.6 * @author Andreas Sterbenz * @author JSR 268 Expert Group */ public class CardPermission extends Permission { private static final long serialVersionUID = 7146787880530705613L; private final static int A_CONNECT = 0x01; private final static int A_EXCLUSIVE = 0x02; private final static int A_GET_BASIC_CHANNEL = 0x04; private final static int A_OPEN_LOGICAL_CHANNEL = 0x08; private final static int A_RESET = 0x10; private final static int A_TRANSMIT_CONTROL = 0x20; // sum of all the actions above private final static int A_ALL = 0x3f; private final static int[] ARRAY_MASKS = { A_ALL, A_CONNECT, A_EXCLUSIVE, A_GET_BASIC_CHANNEL, A_OPEN_LOGICAL_CHANNEL, A_RESET, A_TRANSMIT_CONTROL, }; private final static String S_CONNECT = "connect"; private final static String S_EXCLUSIVE = "exclusive"; private final static String S_GET_BASIC_CHANNEL = "getBasicChannel"; private final static String S_OPEN_LOGICAL_CHANNEL = "openLogicalChannel"; private final static String S_RESET = "reset"; private final static String S_TRANSMIT_CONTROL = "transmitControl"; private final static String S_ALL = "*"; private final static String[] ARRAY_STRINGS = { S_ALL, S_CONNECT, S_EXCLUSIVE, S_GET_BASIC_CHANNEL, S_OPEN_LOGICAL_CHANNEL, S_RESET, S_TRANSMIT_CONTROL, }; private transient int mask; /** * @serial */ private volatile String actions; /** * Constructs a new CardPermission with the specified actions. * <code>terminalName</code> is the name of a CardTerminal or <code>*</code> * if this permission applies to all terminals. <code>actions</code> * contains a comma-separated list of the individual actions * or <code>*</code> to signify all actions. For more information, * see the documentation at the top of this {@linkplain CardPermission * class}. * * @param terminalName the name of the card terminal, or <code>*</code> * @param actions the action string (or null if the set of permitted * actions is empty) * * @throws NullPointerException if terminalName is null * @throws IllegalArgumentException if actions is an invalid actions * specification */ public CardPermission(String terminalName, String actions) { super(terminalName); if (terminalName == null) { throw new NullPointerException(); } mask = getMask(actions); } private static int getMask(String actions) { if ((actions == null) || (actions.length() == 0)) { throw new IllegalArgumentException("actions must not be empty"); } // try exact matches for simple actions first for (int i = 0; i < ARRAY_STRINGS.length; i++) { if (actions == ARRAY_STRINGS[i]) { return ARRAY_MASKS[i]; } } if (actions.endsWith(",")) { throw new IllegalArgumentException("Invalid actions: '" + actions + "'"); } int mask = 0; String[] split = actions.split(","); outer: for (String s : split) { for (int i = 0; i < ARRAY_STRINGS.length; i++) { if (ARRAY_STRINGS[i].equalsIgnoreCase(s)) { mask |= ARRAY_MASKS[i]; continue outer; } } throw new IllegalArgumentException("Invalid action: '" + s + "'"); } return mask; } private static String getActions(int mask) { if (mask == A_ALL) { return S_ALL; } boolean first = true; StringBuilder sb = new StringBuilder(); for (int i = 0; i < ARRAY_MASKS.length; i++) { int action = ARRAY_MASKS[i]; if ((mask & action) == action) { if (first == false) { sb.append(","); } else { first = false; } sb.append(ARRAY_STRINGS[i]); } } return sb.toString(); } /** * Returns the canonical string representation of the actions. * It is <code>*</code> to signify all actions defined by this class or * the string concatenation of the comma-separated, * lexicographically sorted list of individual actions. * * @return the canonical string representation of the actions. */ public String getActions() { if (actions == null) { actions = getActions(mask); } return actions; } /** * Checks if this CardPermission object implies the specified permission. * That is the case, if and only if * <ul> * <li><p><code>permission</code> is an instance of CardPermission,</p> * <li><p><code>permission</code>'s actions are a proper subset of this * object's actions, and</p> * <li><p>this object's <code>getName()</code> method is either * <code>*</code> or equal to <code>permission</code>'s <code>name</code>. * </p> * </ul> * * @param permission the permission to check against * @return true if and only if this CardPermission object implies the * specified permission. */ public boolean implies(Permission permission) { if (permission instanceof CardPermission == false) { return false; } CardPermission other = (CardPermission)permission; if ((this.mask & other.mask) != other.mask) { return false; } String thisName = getName(); if (thisName.equals("*")) { return true; } if (thisName.equals(other.getName())) { return true; } return false; } /** * Compares the specified object with this CardPermission for equality. * This CardPermission is equal to another Object <code>object</code>, if * and only if * <ul> * <li><p><code>object</code> is an instance of CardPermission,</p> * <li><p><code>this.getName()</code> is equal to * <code>((CardPermission)object).getName()</code>, and</p> * <li><p><code>this.getActions()</code> is equal to * <code>((CardPermission)object).getActions()</code>.</p> * </ul> * * @param obj the object to be compared for equality with this CardPermission * @return true if and only if the specified object is equal to this * CardPermission */ public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof CardPermission == false) { return false; } CardPermission other = (CardPermission)obj; return this.getName().equals(other.getName()) && (this.mask == other.mask); } /** * Returns the hash code value for this CardPermission object. * * @return the hash code value for this CardPermission object. */ public int hashCode() { return getName().hashCode() + 31 * mask; } private void writeObject(ObjectOutputStream s) throws IOException { // Write out the actions. The superclass takes care of the name. // Call getActions to make sure actions field is initialized if (actions == null) { getActions(); } s.defaultWriteObject(); } private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the actions, then restore the mask. s.defaultReadObject(); mask = getMask(actions); } }