1 7 8 package javax.crypto; 9 10 import java.security.*; 11 import java.util.Enumeration; 12 import java.util.Hashtable; 13 import java.util.Vector; 14 import java.util.NoSuchElementException; 15 import java.io.Serializable; 16 import java.io.InputStream; 17 import java.io.InputStreamReader; 18 import java.io.BufferedReader; 19 import java.io.IOException; 20 21 42 final class CryptoPermissions extends PermissionCollection 43 implements Serializable { 44 45 private static final long serialVersionUID = 4946547168093391015L; 46 47 private Hashtable perms; 49 50 54 CryptoPermissions() { 55 perms = new Hashtable(7); 56 } 57 58 67 void load(InputStream in) 68 throws IOException, CryptoPolicyParser.ParsingException { 69 CryptoPolicyParser parser = new CryptoPolicyParser(); 70 parser.read(new BufferedReader(new InputStreamReader(in, "UTF-8"))); 71 72 CryptoPermission[] parsingResult = parser.getPermissions(); 73 for (int i = 0; i < parsingResult.length; i++) { 74 this.add(parsingResult[i]); 75 } 76 } 77 78 83 boolean isEmpty() { 84 return perms.isEmpty(); 85 } 86 87 103 public void add(Permission permission) { 104 105 if (isReadOnly()) 106 throw new SecurityException("Attempt to add a Permission " + 107 "to a readonly CryptoPermissions " + 108 "object"); 109 110 if (!(permission instanceof CryptoPermission)) 111 return; 112 113 CryptoPermission cryptoPerm = (CryptoPermission)permission; 114 PermissionCollection pc = 115 getPermissionCollection(cryptoPerm); 116 pc.add(cryptoPerm); 117 String alg = cryptoPerm.getAlgorithm(); 118 if (!perms.containsKey(alg)) { 119 perms.put(alg, pc); 120 } 121 } 122 123 134 public boolean implies(Permission permission) { 135 if (!(permission instanceof CryptoPermission)) { 136 return false; 137 } 138 139 CryptoPermission cryptoPerm = (CryptoPermission)permission; 140 141 PermissionCollection pc = 142 getPermissionCollection(cryptoPerm.getAlgorithm()); 143 return pc.implies(cryptoPerm); 144 } 145 146 152 public Enumeration elements() { 153 return new PermissionsEnumerator(perms.elements()); 156 } 157 158 167 CryptoPermissions getMinimum(CryptoPermissions other) { 168 if (other == null) { 169 return null; 170 } 171 172 if (this.perms.containsKey(CryptoAllPermission.ALG_NAME)) { 173 return other; 174 } 175 176 if (other.perms.containsKey(CryptoAllPermission.ALG_NAME)) { 177 return this; 178 } 179 180 CryptoPermissions ret = new CryptoPermissions(); 181 182 183 PermissionCollection thatWildcard = 184 (PermissionCollection)other.perms.get( 185 CryptoPermission.ALG_NAME_WILDCARD); 186 int maxKeySize = 0; 187 if (thatWildcard != null) { 188 maxKeySize = ((CryptoPermission) 189 thatWildcard.elements().nextElement()).getMaxKeySize(); 190 } 191 Enumeration thisKeys = this.perms.keys(); 195 while (thisKeys.hasMoreElements()) { 196 String alg = (String)thisKeys.nextElement(); 197 198 PermissionCollection thisPc = 199 (PermissionCollection)this.perms.get(alg); 200 PermissionCollection thatPc = 201 (PermissionCollection)other.perms.get(alg); 202 203 CryptoPermission[] partialResult; 204 205 if (thatPc == null) { 206 if (thatWildcard == null) { 207 continue; 212 } 213 partialResult = getMinimum(maxKeySize, thisPc); 214 } else { 215 partialResult = getMinimum(thisPc, thatPc); 216 } 217 218 for (int i = 0; i < partialResult.length; i++) { 219 ret.add(partialResult[i]); 220 } 221 } 222 223 PermissionCollection thisWildcard = 224 (PermissionCollection)this.perms.get( 225 CryptoPermission.ALG_NAME_WILDCARD); 226 227 if (thisWildcard == null) { 230 return ret; 231 } 232 233 maxKeySize = 236 ((CryptoPermission) 237 thisWildcard.elements().nextElement()).getMaxKeySize(); 238 Enumeration thatKeys = other.perms.keys(); 239 while (thatKeys.hasMoreElements()) { 240 String alg = (String)thatKeys.nextElement(); 241 242 if (this.perms.containsKey(alg)) { 243 continue; 244 } 245 246 PermissionCollection thatPc = 247 (PermissionCollection)other.perms.get(alg); 248 249 CryptoPermission[] partialResult; 250 251 partialResult = getMinimum(maxKeySize, thatPc); 252 253 for (int i = 0; i < partialResult.length; i++) { 254 ret.add(partialResult[i]); 255 } 256 } 257 return ret; 258 } 259 260 270 private CryptoPermission[] getMinimum(PermissionCollection thisPc, 271 PermissionCollection thatPc) { 272 Vector permVector = new Vector(2); 273 274 Enumeration thisPcPermissions = thisPc.elements(); 275 276 while (thisPcPermissions.hasMoreElements()) { 290 CryptoPermission thisCp = 291 (CryptoPermission)thisPcPermissions.nextElement(); 292 293 Enumeration thatPcPermissions = thatPc.elements(); 294 while (thatPcPermissions.hasMoreElements()) { 295 CryptoPermission thatCp = 296 (CryptoPermission)thatPcPermissions.nextElement(); 297 298 if (thatCp.implies(thisCp)) { 299 permVector.addElement(thisCp); 300 break; 301 } 302 if (thisCp.implies(thatCp)) { 303 permVector.addElement(thatCp); 304 } 305 } 306 } 307 308 CryptoPermission[] ret = new CryptoPermission[permVector.size()]; 309 permVector.copyInto(ret); 310 return ret; 311 } 312 313 326 private CryptoPermission[] getMinimum(int maxKeySize, 327 PermissionCollection pc) { 328 Vector permVector = new Vector(1); 329 330 Enumeration enum_ = pc.elements(); 331 332 while (enum_.hasMoreElements()) { 333 CryptoPermission cp = 334 (CryptoPermission)enum_.nextElement(); 335 if (cp.getMaxKeySize() <= maxKeySize) { 336 permVector.addElement(cp); 337 } else { 338 if (cp.getCheckParam()) { 339 permVector.addElement( 340 new CryptoPermission(cp.getAlgorithm(), 341 maxKeySize, 342 cp.getAlgorithmParameterSpec(), 343 cp.getExemptionMechanism())); 344 } else { 345 permVector.addElement( 346 new CryptoPermission(cp.getAlgorithm(), 347 maxKeySize, 348 cp.getExemptionMechanism())); 349 } 350 } 351 } 352 353 CryptoPermission[] ret = new CryptoPermission[permVector.size()]; 354 permVector.copyInto(ret); 355 return ret; 356 } 357 358 365 PermissionCollection getPermissionCollection(String alg) { 366 if (perms.containsKey(CryptoAllPermission.ALG_NAME)) { 369 return 370 (PermissionCollection)(perms.get(CryptoAllPermission.ALG_NAME)); 371 } 372 373 PermissionCollection pc = (PermissionCollection)perms.get(alg); 374 375 if (pc == null) { 380 pc = (PermissionCollection)perms.get( 381 CryptoPermission.ALG_NAME_WILDCARD); 382 } 383 return pc; 384 } 385 386 395 private PermissionCollection getPermissionCollection( 396 CryptoPermission cryptoPerm) { 397 398 String alg = cryptoPerm.getAlgorithm(); 399 400 PermissionCollection pc = (PermissionCollection)perms.get(alg); 401 402 if (pc == null) { 403 pc = cryptoPerm.newPermissionCollection(); 404 } 405 return pc; 406 } 407 } 408 409 final class PermissionsEnumerator implements Enumeration { 410 411 private Enumeration perms; 413 private Enumeration permset; 415 416 PermissionsEnumerator(Enumeration e) { 417 perms = e; 418 permset = getNextEnumWithMore(); 419 } 420 421 public synchronized boolean hasMoreElements() { 422 425 if (permset == null) 426 return false; 427 428 430 if (permset.hasMoreElements()) 431 return true; 432 433 permset = getNextEnumWithMore(); 435 436 return (permset != null); 438 } 439 440 public synchronized Object nextElement() { 441 444 if (hasMoreElements()) { 445 return permset.nextElement(); 446 } else { 447 throw new NoSuchElementException("PermissionsEnumerator"); 448 } 449 450 } 451 452 private Enumeration getNextEnumWithMore() { 453 while (perms.hasMoreElements()) { 454 PermissionCollection pc = 455 (PermissionCollection) perms.nextElement(); 456 Enumeration next = pc.elements(); 457 if (next.hasMoreElements()) 458 return next; 459 } 460 return null; 461 } 462 } 463 | Popular Tags |