1 7 8 package java.security.cert; 9 10 import java.security.InvalidAlgorithmParameterException ; 11 import java.security.KeyStore ; 12 import java.security.KeyStoreException ; 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Date ; 16 import java.util.Enumeration ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 69 public class PKIXParameters implements CertPathParameters { 70 71 private Set <TrustAnchor > unmodTrustAnchors; 72 private Date date; 73 private List <PKIXCertPathChecker > certPathCheckers; 74 private String sigProvider; 75 private boolean revocationEnabled = true; 76 private Set <String > unmodInitialPolicies; 77 private boolean explicitPolicyRequired = false; 78 private boolean policyMappingInhibited = false; 79 private boolean anyPolicyInhibited = false; 80 private boolean policyQualifiersRejected = true; 81 private List <CertStore > certStores; 82 private CertSelector certSelector; 83 84 100 public PKIXParameters(Set <TrustAnchor > trustAnchors) 101 throws InvalidAlgorithmParameterException 102 { 103 setTrustAnchors(trustAnchors); 104 105 this.unmodInitialPolicies = Collections.<String >emptySet(); 106 this.certPathCheckers = new ArrayList <PKIXCertPathChecker >(); 107 this.certStores = new ArrayList <CertStore >(); 108 } 109 110 124 public PKIXParameters(KeyStore keystore) 125 throws KeyStoreException , InvalidAlgorithmParameterException 126 { 127 if (keystore == null) 128 throw new NullPointerException ("the keystore parameter must be " + 129 "non-null"); 130 Set <TrustAnchor > hashSet = new HashSet <TrustAnchor >(); 131 Enumeration aliases = keystore.aliases(); 132 while (aliases.hasMoreElements()) { 133 String alias = (String ) aliases.nextElement(); 134 if (keystore.isCertificateEntry(alias)) { 135 Certificate cert = keystore.getCertificate(alias); 136 if (cert instanceof X509Certificate ) 137 hashSet.add(new TrustAnchor ((X509Certificate )cert, null)); 138 } 139 } 140 setTrustAnchors(hashSet); 141 this.unmodInitialPolicies = Collections.<String >emptySet(); 142 this.certPathCheckers = new ArrayList <PKIXCertPathChecker >(); 143 this.certStores = new ArrayList <CertStore >(); 144 } 145 146 155 public Set <TrustAnchor > getTrustAnchors() { 156 return this.unmodTrustAnchors; 157 } 158 159 175 public void setTrustAnchors(Set <TrustAnchor > trustAnchors) 176 throws InvalidAlgorithmParameterException 177 { 178 if (trustAnchors == null) { 179 throw new NullPointerException ("the trustAnchors parameters must" + 180 " be non-null"); 181 } 182 if (trustAnchors.isEmpty()) { 183 throw new InvalidAlgorithmParameterException ("the trustAnchors " + 184 "parameter must be non-empty"); 185 } 186 for (Iterator i = trustAnchors.iterator(); i.hasNext(); ) { 187 if (!(i.next() instanceof TrustAnchor )) { 188 throw new ClassCastException ("all elements of set must be " 189 + "of type java.security.cert.TrustAnchor"); 190 } 191 } 192 this.unmodTrustAnchors = Collections.unmodifiableSet 193 (new HashSet <TrustAnchor >(trustAnchors)); 194 } 195 196 210 public Set <String > getInitialPolicies() { 211 return this.unmodInitialPolicies; 212 } 213 214 233 public void setInitialPolicies(Set <String > initialPolicies) { 234 if (initialPolicies != null) { 235 for (Iterator i = initialPolicies.iterator(); i.hasNext();) { 236 if (!(i.next() instanceof String )) 237 throw new ClassCastException ("all elements of set must be " 238 + "of type java.lang.String"); 239 } 240 this.unmodInitialPolicies = 241 Collections.unmodifiableSet(new HashSet <String >(initialPolicies)); 242 } else 243 this.unmodInitialPolicies = Collections.<String >emptySet(); 244 } 245 246 263 public void setCertStores(List <CertStore > stores) { 264 if (stores == null) { 265 this.certStores = new ArrayList <CertStore >(); 266 } else { 267 for (Iterator i = stores.iterator(); i.hasNext();) { 268 if (!(i.next() instanceof CertStore )) { 269 throw new ClassCastException ("all elements of list must be " 270 + "of type java.security.cert.CertStore"); 271 } 272 } 273 this.certStores = new ArrayList <CertStore >(stores); 274 } 275 } 276 277 284 public void addCertStore(CertStore store) { 285 if (store != null) { 286 this.certStores.add(store); 287 } 288 } 289 290 299 public List <CertStore > getCertStores() { 300 return Collections.unmodifiableList 301 (new ArrayList <CertStore >(this.certStores)); 302 } 303 304 322 public void setRevocationEnabled(boolean val) { 323 revocationEnabled = val; 324 } 325 326 336 public boolean isRevocationEnabled() { 337 return revocationEnabled; 338 } 339 340 348 public void setExplicitPolicyRequired(boolean val) { 349 explicitPolicyRequired = val; 350 } 351 352 360 public boolean isExplicitPolicyRequired() { 361 return explicitPolicyRequired; 362 } 363 364 372 public void setPolicyMappingInhibited(boolean val) { 373 policyMappingInhibited = val; 374 } 375 376 383 public boolean isPolicyMappingInhibited() { 384 return policyMappingInhibited; 385 } 386 387 396 public void setAnyPolicyInhibited(boolean val) { 397 anyPolicyInhibited = val; 398 } 399 400 407 public boolean isAnyPolicyInhibited() { 408 return anyPolicyInhibited; 409 } 410 411 434 public void setPolicyQualifiersRejected(boolean qualifiersRejected) { 435 policyQualifiersRejected = qualifiersRejected; 436 } 437 438 452 public boolean getPolicyQualifiersRejected() { 453 return policyQualifiersRejected; 454 } 455 456 466 public Date getDate() { 467 if (date == null) 468 return null; 469 else 470 return (Date ) this.date.clone(); 471 } 472 473 484 public void setDate(Date date) { 485 if (date != null) 486 this.date = (Date ) date.clone(); 487 else 488 date = null; 489 } 490 491 527 public void setCertPathCheckers(List <PKIXCertPathChecker > checkers) { 528 if (checkers != null) { 529 List <PKIXCertPathChecker > tmpList = 530 new ArrayList <PKIXCertPathChecker >(); 531 for (PKIXCertPathChecker checker : checkers) { 532 tmpList.add((PKIXCertPathChecker )checker.clone()); 533 } 534 this.certPathCheckers = tmpList; 535 } else { 536 this.certPathCheckers = new ArrayList <PKIXCertPathChecker >(); 537 } 538 } 539 540 551 public List <PKIXCertPathChecker > getCertPathCheckers() { 552 List <PKIXCertPathChecker > tmpList = new ArrayList <PKIXCertPathChecker >(); 553 for (PKIXCertPathChecker ck : certPathCheckers) { 554 tmpList.add((PKIXCertPathChecker )ck.clone()); 555 } 556 return Collections.unmodifiableList(tmpList); 557 } 558 559 570 public void addCertPathChecker(PKIXCertPathChecker checker) { 571 if (checker != null) { 572 certPathCheckers.add((PKIXCertPathChecker )checker.clone()); 573 } 574 } 575 576 583 public String getSigProvider() { 584 return this.sigProvider; 585 } 586 587 596 public void setSigProvider(String sigProvider) { 597 this.sigProvider = sigProvider; 598 } 599 600 612 public CertSelector getTargetCertConstraints() { 613 if (certSelector != null) { 614 return (CertSelector ) certSelector.clone(); 615 } else { 616 return null; 617 } 618 } 619 620 633 public void setTargetCertConstraints(CertSelector selector) { 634 if (selector != null) 635 certSelector = (CertSelector ) selector.clone(); 636 else 637 certSelector = null; 638 } 639 640 646 public Object clone() { 647 try { 648 Object copy = super.clone(); 649 if (certStores != null) { 651 certStores = new ArrayList <CertStore >(certStores); 652 } 653 if (certPathCheckers != null) { 654 certPathCheckers = 655 new ArrayList <PKIXCertPathChecker >(certPathCheckers); 656 } 657 return copy; 658 } catch (CloneNotSupportedException e) { 659 660 throw new InternalError (e.toString()); 661 } 662 } 663 664 669 public String toString() { 670 StringBuffer sb = new StringBuffer (); 671 sb.append("[\n"); 672 673 674 if (unmodTrustAnchors != null) { 675 sb.append(" Trust Anchors: " + unmodTrustAnchors.toString() 676 + "\n"); 677 } 678 679 680 if (unmodInitialPolicies != null) { 681 if (unmodInitialPolicies.isEmpty()) { 682 sb.append(" Initial Policy OIDs: any\n"); 683 } else { 684 sb.append(" Initial Policy OIDs: [" 685 + unmodInitialPolicies.toString() + "]\n"); 686 } 687 } 688 689 690 sb.append(" Validity Date: " + String.valueOf(date) + "\n"); 691 sb.append(" Signature Provider: " + String.valueOf(sigProvider) + "\n"); 692 sb.append(" Default Revocation Enabled: " + revocationEnabled + "\n"); 693 sb.append(" Explicit Policy Required: " + explicitPolicyRequired + "\n"); 694 sb.append(" Policy Mapping Inhibited: " + policyMappingInhibited + "\n"); 695 sb.append(" Any Policy Inhibited: " + anyPolicyInhibited + "\n"); 696 sb.append(" Policy Qualifiers Rejected: " + policyQualifiersRejected + "\n"); 697 698 699 sb.append(" Target Cert Constraints: " + String.valueOf(certSelector) + "\n"); 700 701 702 if (certPathCheckers != null) 703 sb.append(" Certification Path Checkers: [" 704 + certPathCheckers.toString() + "]\n"); 705 if (certStores != null) 706 sb.append(" CertStores: [" + certStores.toString() + "]\n"); 707 sb.append("]"); 708 return sb.toString(); 709 } 710 } 711 | Popular Tags |