1 7 8 package javax.crypto; 9 10 import java.security.*; 11 import java.security.spec.AlgorithmParameterSpec; 12 import java.io.Serializable; 13 import java.util.Enumeration; 14 import java.util.Vector; 15 16 import javax.crypto.spec.*; 17 18 33 class CryptoPermission extends java.security.Permission { 34 35 private static final long serialVersionUID = 8987399626114087514L; 36 37 private String alg; 38 private int maxKeySize = Integer.MAX_VALUE; private String exemptionMechanism = null; 40 private AlgorithmParameterSpec algParamSpec = null; 41 private boolean checkParam = false; 43 static final String ALG_NAME_WILDCARD = "*"; 44 45 53 CryptoPermission(String alg) { 54 super(null); 55 this.alg = alg; 56 } 57 58 70 CryptoPermission(String alg, int maxKeySize) { 71 super(null); 72 this.alg = alg; 73 this.maxKeySize = maxKeySize; 74 } 75 76 93 CryptoPermission(String alg, 94 int maxKeySize, 95 AlgorithmParameterSpec algParamSpec) { 96 super(null); 97 this.alg = alg; 98 this.maxKeySize = maxKeySize; 99 this.checkParam = true; 100 this.algParamSpec = algParamSpec; 101 } 102 103 115 CryptoPermission(String alg, 116 String exemptionMechanism) { 117 super(null); 118 this.alg = alg; 119 this.exemptionMechanism = exemptionMechanism; 120 } 121 122 137 CryptoPermission(String alg, 138 int maxKeySize, 139 String exemptionMechanism) { 140 super(null); 141 this.alg = alg; 142 this.exemptionMechanism = exemptionMechanism; 143 this.maxKeySize = maxKeySize; 144 } 145 146 166 CryptoPermission(String alg, 167 int maxKeySize, 168 AlgorithmParameterSpec algParamSpec, 169 String exemptionMechanism) { 170 super(null); 171 this.alg = alg; 172 this.exemptionMechanism = exemptionMechanism; 173 this.maxKeySize = maxKeySize; 174 this.checkParam = true; 175 this.algParamSpec = algParamSpec; 176 } 177 178 202 public boolean implies(Permission p) { 203 if (!(p instanceof CryptoPermission)) 204 return false; 205 206 CryptoPermission cp = (CryptoPermission)p; 207 208 if ((!alg.equalsIgnoreCase(cp.alg)) && 209 (!alg.equalsIgnoreCase(ALG_NAME_WILDCARD))) { 210 return false; 211 } 212 213 if (cp.maxKeySize <= this.maxKeySize) { 216 if (!impliesParameterSpec(cp.checkParam, cp.algParamSpec)) { 218 return false; 219 } 220 221 if (impliesExemptionMechanism(cp.exemptionMechanism)) { 223 return true; 224 } 225 } 226 227 return false; 228 } 229 230 241 public boolean equals(Object obj) { 242 if (obj == this) 243 return true; 244 245 if (!(obj instanceof CryptoPermission)) 246 return false; 247 248 CryptoPermission that = (CryptoPermission) obj; 249 250 if (!(alg.equalsIgnoreCase(that.alg)) || 251 (maxKeySize != that.maxKeySize)) { 252 return false; 253 } 254 if (this.checkParam != that.checkParam) { 255 return false; 256 } 257 return (equalObjects(this.exemptionMechanism, 258 that.exemptionMechanism) && 259 equalObjects(this.algParamSpec, 260 that.algParamSpec)); 261 } 262 263 268 269 public int hashCode() { 270 int retval = alg.hashCode(); 271 retval ^= maxKeySize; 272 if (exemptionMechanism != null) { 273 retval ^= exemptionMechanism.hashCode(); 274 } 275 if (checkParam) retval ^= 100; 276 if (algParamSpec != null) { 277 retval ^= algParamSpec.hashCode(); 278 } 279 return retval; 280 } 281 282 286 public String getActions() 287 { 288 return null; 289 } 290 291 298 299 public PermissionCollection newPermissionCollection() { 300 return new CryptoPermissionCollection(); 301 } 302 303 307 final String getAlgorithm() { 308 return alg; 309 } 310 311 316 final String getExemptionMechanism() { 317 return exemptionMechanism; 318 } 319 320 324 final int getMaxKeySize() { 325 return maxKeySize; 326 } 327 328 333 final boolean getCheckParam() { 334 return checkParam; 335 } 336 337 342 final AlgorithmParameterSpec getAlgorithmParameterSpec() { 343 return algParamSpec; 344 } 345 346 354 public String toString() { 355 StringBuilder buf = new StringBuilder(100); 356 buf.append("(CryptoPermission " + alg + " " + maxKeySize); 357 if (algParamSpec != null) { 358 if (algParamSpec instanceof RC2ParameterSpec) { 359 buf.append(" , effective " + 360 ((RC2ParameterSpec)algParamSpec).getEffectiveKeyBits()); 361 } else if (algParamSpec instanceof RC5ParameterSpec) { 362 buf.append(" , rounds " + 363 ((RC5ParameterSpec)algParamSpec).getRounds()); 364 } 365 } 366 if (exemptionMechanism != null) { buf.append(" " + exemptionMechanism); 368 } 369 buf.append(")"); 370 return buf.toString(); 371 } 372 373 private boolean impliesExemptionMechanism(String exemptionMechanism) { 374 if (this.exemptionMechanism == null) { 375 return true; 376 } 377 378 if (exemptionMechanism == null) { 379 return false; 380 } 381 382 if (this.exemptionMechanism.equals(exemptionMechanism)) { 383 return true; 384 } 385 386 return false; 387 } 388 389 private boolean impliesParameterSpec(boolean checkParam, 390 AlgorithmParameterSpec algParamSpec) { 391 if ((this.checkParam) && checkParam) { 392 if (algParamSpec == null) { 393 return true; 394 } else if (this.algParamSpec == null) { 395 return false; 396 } 397 398 if (this.algParamSpec.getClass() != algParamSpec.getClass()) { 399 return false; 400 } 401 402 if (algParamSpec instanceof RC2ParameterSpec) { 403 if (((RC2ParameterSpec)algParamSpec).getEffectiveKeyBits() <= 404 ((RC2ParameterSpec) 405 (this.algParamSpec)).getEffectiveKeyBits()) { 406 return true; 407 } 408 } 409 410 if (algParamSpec instanceof RC5ParameterSpec) { 411 if (((RC5ParameterSpec)algParamSpec).getRounds() <= 412 ((RC5ParameterSpec)this.algParamSpec).getRounds()) { 413 return true; 414 } 415 } 416 417 if (algParamSpec instanceof PBEParameterSpec) { 418 if (((PBEParameterSpec)algParamSpec).getIterationCount() <= 419 ((PBEParameterSpec)this.algParamSpec).getIterationCount()) { 420 return true; 421 } 422 } 423 424 if (this.algParamSpec.equals(algParamSpec)) { 427 return true; 428 } 429 return false; 430 } else if (this.checkParam) { 431 return false; 432 } else { 433 return true; 434 } 435 } 436 437 private boolean equalObjects(Object obj1, Object obj2) { 438 if (obj1 == null) { 439 return (obj2 == null ? true : false); 440 } 441 442 return obj1.equals(obj2); 443 } 444 } 445 446 458 final class CryptoPermissionCollection extends PermissionCollection 459 implements Serializable { 460 461 private static final long serialVersionUID = -511215555898802763L; 462 463 private Vector permissions; 464 465 469 CryptoPermissionCollection() { 470 permissions = new Vector(3); 471 } 472 473 481 public void add(Permission permission) 482 { 483 if (isReadOnly()) 484 throw new SecurityException("attempt to add a Permission " + 485 "to a readonly PermissionCollection"); 486 487 if (!(permission instanceof CryptoPermission)) 488 return; 489 490 permissions.addElement(permission); 491 } 492 493 502 public boolean implies(Permission permission) { 503 if (!(permission instanceof CryptoPermission)) 504 return false; 505 506 CryptoPermission cp = (CryptoPermission)permission; 507 508 Enumeration e = permissions.elements(); 509 510 while (e.hasMoreElements()) { 511 CryptoPermission x = (CryptoPermission) e.nextElement(); 512 if (x.implies(cp)) { 513 return true; 514 } 515 } 516 return false; 517 } 518 519 525 526 public Enumeration elements() 527 { 528 return permissions.elements(); 529 } 530 } 531 | Popular Tags |