| 1 7 8 package java.security.cert; 9 10 import java.io.IOException ; 11 import java.math.BigInteger ; 12 import java.security.PublicKey ; 13 import java.util.*; 14 import javax.security.auth.x500.X500Principal ; 15 16 import sun.misc.HexDumpEncoder; 17 import sun.security.util.Debug; 18 import sun.security.util.DerInputStream; 19 import sun.security.util.DerValue; 20 import sun.security.util.ObjectIdentifier; 21 import sun.security.x509.*; 22 23 68 public class X509CertSelector implements CertSelector { 69 70 private static final Debug debug = Debug.getInstance("certpath"); 71 72 private final static ObjectIdentifier ANY_EXTENDED_KEY_USAGE = 73 ObjectIdentifier.newInternal(new int[] {2, 5, 29, 37, 0}); 74 75 static { 76 CertPathHelperImpl.initialize(); 77 } 78 79 private BigInteger serialNumber; 80 private X500Principal issuer; 81 private X500Principal subject; 82 private byte[] subjectKeyID; 83 private byte[] authorityKeyID; 84 private Date certificateValid; 85 private Date privateKeyValid; 86 private ObjectIdentifier subjectPublicKeyAlgID; 87 private PublicKey subjectPublicKey; 88 private byte[] subjectPublicKeyBytes; 89 private boolean[] keyUsage; 90 private Set<String > keyPurposeSet; 91 private Set<ObjectIdentifier> keyPurposeOIDSet; 92 private Set<List<?>> subjectAlternativeNames; 93 private Set<GeneralNameInterface> subjectAlternativeGeneralNames; 94 private CertificatePolicySet policy; 95 private Set<String > policySet; 96 private Set<List<?>> pathToNames; 97 private Set<GeneralNameInterface> pathToGeneralNames; 98 private NameConstraintsExtension nc; 99 private byte[] ncBytes; 100 private int basicConstraints = -1; 101 private X509Certificate x509Cert; 102 private boolean matchAllSubjectAltNames = true; 103 104 private static final Boolean FALSE = Boolean.FALSE; 105 106 private static final int PRIVATE_KEY_USAGE_ID = 0; 107 private static final int SUBJECT_ALT_NAME_ID = 1; 108 private static final int NAME_CONSTRAINTS_ID = 2; 109 private static final int CERT_POLICIES_ID = 3; 110 private static final int EXTENDED_KEY_USAGE_ID = 4; 111 private static final int NUM_OF_EXTENSIONS = 5; 112 private static final String [] EXTENSION_OIDS = new String [NUM_OF_EXTENSIONS]; 113 114 static { 115 EXTENSION_OIDS[PRIVATE_KEY_USAGE_ID] = "2.5.29.16"; 116 EXTENSION_OIDS[SUBJECT_ALT_NAME_ID] = "2.5.29.17"; 117 EXTENSION_OIDS[NAME_CONSTRAINTS_ID] = "2.5.29.30"; 118 EXTENSION_OIDS[CERT_POLICIES_ID] = "2.5.29.32"; 119 EXTENSION_OIDS[EXTENDED_KEY_USAGE_ID] = "2.5.29.37"; 120 }; 121 122 123 static final int NAME_ANY = 0; 124 static final int NAME_RFC822 = 1; 125 static final int NAME_DNS = 2; 126 static final int NAME_X400 = 3; 127 static final int NAME_DIRECTORY = 4; 128 static final int NAME_EDI = 5; 129 static final int NAME_URI = 6; 130 static final int NAME_IP = 7; 131 static final int NAME_OID = 8; 132 133 137 public X509CertSelector() { 138 } 140 141 156 public void setCertificate(X509Certificate cert) { 157 x509Cert = cert; 158 } 159 160 170 public void setSerialNumber(BigInteger serial) { 171 serialNumber = serial; 172 } 173 174 184 public void setIssuer(X500Principal issuer) { 185 this.issuer = issuer; 186 } 187 188 207 public void setIssuer(String issuerDN) throws IOException { 208 if (issuerDN == null) { 209 issuer = null; 210 } else { 211 issuer = new X500Name(issuerDN).asX500Principal(); 212 } 213 } 214 215 257 public void setIssuer(byte[] issuerDN) throws IOException { 258 try { 259 issuer = (issuerDN == null ? null : new X500Principal (issuerDN)); 260 } catch (IllegalArgumentException e) { 261 throw (IOException )new IOException ("Invalid name").initCause(e); 262 } 263 } 264 265 275 public void setSubject(X500Principal subject) { 276 this.subject = subject; 277 } 278 279 298 public void setSubject(String subjectDN) throws IOException { 299 if (subjectDN == null) { 300 subject = null; 301 } else { 302 subject = new X500Name(subjectDN).asX500Principal(); 303 } 304 } 305 306 321 public void setSubject(byte[] subjectDN) throws IOException { 322 try { 323 subject = (subjectDN == null ? null : new X500Principal (subjectDN)); 324 } catch (IllegalArgumentException e) { 325 throw (IOException )new IOException ("Invalid name").initCause(e); 326 } 327 } 328 329 361 public void setSubjectKeyIdentifier(byte[] subjectKeyID) { 362 if (subjectKeyID == null) { 363 this.subjectKeyID = null; 364 } else { 365 this.subjectKeyID = (byte[])subjectKeyID.clone(); 366 } 367 } 368 369 422 public void setAuthorityKeyIdentifier(byte[] authorityKeyID) { 423 if (authorityKeyID == null) { 424 this.authorityKeyID = null; 425 } else { 426 this.authorityKeyID = (byte[])authorityKeyID.clone(); 427 } 428 } 429 430 442 public void setCertificateValid(Date certValid) { 443 if (certValid == null) { 444 certificateValid = null; 445 } else { 446 certificateValid = (Date)certValid.clone(); 447 } 448 } 449 450 463 public void setPrivateKeyValid(Date privateKeyValid) { 464 if (privateKeyValid == null) { 465 this.privateKeyValid = null; 466 } else { 467 this.privateKeyValid = (Date)privateKeyValid.clone(); 468 } 469 } 470 471 486 public void setSubjectPublicKeyAlgID(String oid) throws IOException { 487 if (oid == null) { 488 subjectPublicKeyAlgID = null; 489 } else { 490 subjectPublicKeyAlgID = new ObjectIdentifier(oid); 491 } 492 } 493 494 502 public void setSubjectPublicKey(PublicKey key) { 503 if (key == null) { 504 subjectPublicKey = null; 505 subjectPublicKeyBytes = null; 506 } else { 507 subjectPublicKey = key; 508 subjectPublicKeyBytes = key.getEncoded(); 509 } 510 } 511 512 545 public void setSubjectPublicKey(byte[] key) throws IOException { 546 if (key == null) { 547 subjectPublicKey = null; 548 subjectPublicKeyBytes = null; 549 } else { 550 subjectPublicKeyBytes = (byte[])key.clone(); 551 subjectPublicKey = X509Key.parse(new DerValue(subjectPublicKeyBytes)); 552 } 553 } 554 555 570 public void setKeyUsage(boolean[] keyUsage) { 571 if (keyUsage == null) { 572 this.keyUsage = null; 573 } else { 574 this.keyUsage = (boolean[])keyUsage.clone(); 575 } 576 } 577 578 597 public void setExtendedKeyUsage(Set<String > keyPurposeSet) throws IOException { 598 if ((keyPurposeSet == null) || keyPurposeSet.isEmpty()) { 599 this.keyPurposeSet = null; 600 keyPurposeOIDSet = null; 601 } else { 602 this.keyPurposeSet = 603 Collections.unmodifiableSet(new HashSet<String >(keyPurposeSet)); 604 keyPurposeOIDSet = new HashSet<ObjectIdentifier>(); 605 for (String s : this.keyPurposeSet) { 606 keyPurposeOIDSet.add(new ObjectIdentifier(s)); 607 } 608 } 609 } 610 611 627 public void setMatchAllSubjectAltNames(boolean matchAllNames) { 628 this.matchAllSubjectAltNames = matchAllNames; 629 } 630 631 678 public void setSubjectAlternativeNames(Collection<List<?>> names) 679 throws IOException { 680 if (names == null) { 681 subjectAlternativeNames = null; 682 subjectAlternativeGeneralNames = null; 683 } else { 684 if (names.isEmpty()) { 685 subjectAlternativeNames = null; 686 subjectAlternativeGeneralNames = null; 687 return; 688 } 689 Set<List<?>> tempNames = cloneAndCheckNames(names); 690 subjectAlternativeGeneralNames = parseNames(tempNames); 692 subjectAlternativeNames = tempNames; 693 } 694 } 695 696 733 public void addSubjectAlternativeName(int type, String name) 734 throws IOException { 735 addSubjectAlternativeNameInternal(type, name); 736 } 737 738 777 public void addSubjectAlternativeName(int type, byte[] name) 778 throws IOException { 779 addSubjectAlternativeNameInternal(type, name.clone()); 781 } 782 783 793 private void addSubjectAlternativeNameInternal(int type, Object name) 794 throws IOException { 795 GeneralNameInterface tempName = makeGeneralNameInterface(type, name); 797 if (subjectAlternativeNames == null) { 798 subjectAlternativeNames = new HashSet<List<?>>(); 799 } 800 if (subjectAlternativeGeneralNames == null) { 801 subjectAlternativeGeneralNames = new HashSet<GeneralNameInterface>(); 802 } 803 List<Object > list = new ArrayList<Object >(2); 804 list.add(Integer.valueOf(type)); 805 list.add(name); 806 subjectAlternativeNames.add(list); 807 subjectAlternativeGeneralNames.add(tempName); 808 } 809 810 827 private static Set<GeneralNameInterface> parseNames(Collection<List<?>> names) throws IOException { 828 Set<GeneralNameInterface> genNames = new HashSet<GeneralNameInterface>(); 829 Iterator<List<?>> i = names.iterator(); 830 while (i.hasNext()) { 831 Object o = i.next(); 832 if (!(o instanceof List)) { 833 throw new IOException ("expected List"); 834 } 835 List<Object > nameList = (List<Object >)o; 836 if (nameList.size() != 2) { 837 throw new IOException ("name list size not 2"); 838 } 839 o = nameList.get(0); 840 if (!(o instanceof Integer )) { 841 throw new IOException ("expected an Integer"); 842 } 843 int nameType = ((Integer )o).intValue(); 844 o = nameList.get(1); 845 genNames.add(makeGeneralNameInterface(nameType, o)); 846 } 847 return genNames; 848 } 849 850 860 static boolean equalNames(Collection object1, Collection object2) { 861 if ((object1 == null) || (object2 == null)) { 862 return object1 == object2; 863 } 864 return object1.equals(object2); 865 } 866 867 882 static GeneralNameInterface makeGeneralNameInterface(int type, Object name) 883 throws IOException { 884 GeneralNameInterface result; 885 if (debug != null) { 886 debug.println("X509CertSelector.makeGeneralNameInterface(" 887 + type + ")..."); 888 } 889 890 if (name instanceof String ) { 891 if (debug != null) { 892 debug.println("X509CertSelector.makeGeneralNameInterface() " 893 + "name is String: " + name); 894 } 895 switch (type) { 896 case NAME_RFC822: 897 result = new RFC822Name((String )name); 898 break; 899 case NAME_DNS: 900 result = new DNSName((String )name); 901 break; 902 case NAME_DIRECTORY: 903 result = new X500Name((String )name); 904 break; 905 case NAME_URI: 906 result = new URIName((String )name); 907 break; 908 case NAME_IP: 909 result = new IPAddressName((String )name); 910 break; 911 case NAME_OID: 912 result = new OIDName((String )name); 913 break; 914 default: 915 throw new IOException ("unable to parse String names of type " 916 + type); 917 } 918 if (debug != null) { 919 debug.println("X509CertSelector.makeGeneralNameInterface() " 920 + "result: " + result.toString()); 921 } 922 } else if (name instanceof byte[]) { 923 DerValue val = new DerValue((byte[]) name); 924 if (debug != null) { 925 debug.println 926 ("X509CertSelector.makeGeneralNameInterface() is byte[]"); 927 } 928 929 switch (type) { 930 case NAME_ANY: 931 result = new OtherName(val); 932 break; 933 case NAME_RFC822: 934 result = new RFC822Name(val); 935
|