1 package org.bouncycastle.asn1.x500.style; 2 3 import java.util.Hashtable; 4 5 import org.bouncycastle.asn1.ASN1Encodable; 6 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 7 import org.bouncycastle.asn1.DERIA5String; 8 import org.bouncycastle.asn1.DERPrintableString; 9 import org.bouncycastle.asn1.x500.RDN; 10 import org.bouncycastle.asn1.x500.X500Name; 11 import org.bouncycastle.asn1.x500.X500NameStyle; 12 13 public class RFC4519Style 14 extends AbstractX500NameStyle 15 { 16 public static final ASN1ObjectIdentifier businessCategory = new ASN1ObjectIdentifier("2.5.4.15").intern(); 17 public static final ASN1ObjectIdentifier c = new ASN1ObjectIdentifier("2.5.4.6").intern(); 18 public static final ASN1ObjectIdentifier cn = new ASN1ObjectIdentifier("2.5.4.3").intern(); 19 public static final ASN1ObjectIdentifier dc = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.25").intern(); 20 public static final ASN1ObjectIdentifier description = new ASN1ObjectIdentifier("2.5.4.13").intern(); 21 public static final ASN1ObjectIdentifier destinationIndicator = new ASN1ObjectIdentifier("2.5.4.27").intern(); 22 public static final ASN1ObjectIdentifier distinguishedName = new ASN1ObjectIdentifier("2.5.4.49").intern(); 23 public static final ASN1ObjectIdentifier dnQualifier = new ASN1ObjectIdentifier("2.5.4.46").intern(); 24 public static final ASN1ObjectIdentifier enhancedSearchGuide = new ASN1ObjectIdentifier("2.5.4.47").intern(); 25 public static final ASN1ObjectIdentifier facsimileTelephoneNumber = new ASN1ObjectIdentifier("2.5.4.23").intern(); 26 public static final ASN1ObjectIdentifier generationQualifier = new ASN1ObjectIdentifier("2.5.4.44").intern(); 27 public static final ASN1ObjectIdentifier givenName = new ASN1ObjectIdentifier("2.5.4.42").intern(); 28 public static final ASN1ObjectIdentifier houseIdentifier = new ASN1ObjectIdentifier("2.5.4.51").intern(); 29 public static final ASN1ObjectIdentifier initials = new ASN1ObjectIdentifier("2.5.4.43").intern(); 30 public static final ASN1ObjectIdentifier internationalISDNNumber = new ASN1ObjectIdentifier("2.5.4.25").intern(); 31 public static final ASN1ObjectIdentifier l = new ASN1ObjectIdentifier("2.5.4.7").intern(); 32 public static final ASN1ObjectIdentifier member = new ASN1ObjectIdentifier("2.5.4.31").intern(); 33 public static final ASN1ObjectIdentifier name = new ASN1ObjectIdentifier("2.5.4.41").intern(); 34 public static final ASN1ObjectIdentifier o = new ASN1ObjectIdentifier("2.5.4.10").intern(); 35 public static final ASN1ObjectIdentifier ou = new ASN1ObjectIdentifier("2.5.4.11").intern(); 36 public static final ASN1ObjectIdentifier owner = new ASN1ObjectIdentifier("2.5.4.32").intern(); 37 public static final ASN1ObjectIdentifier physicalDeliveryOfficeName = new ASN1ObjectIdentifier("2.5.4.19").intern(); 38 public static final ASN1ObjectIdentifier postalAddress = new ASN1ObjectIdentifier("2.5.4.16").intern(); 39 public static final ASN1ObjectIdentifier postalCode = new ASN1ObjectIdentifier("2.5.4.17").intern(); 40 public static final ASN1ObjectIdentifier postOfficeBox = new ASN1ObjectIdentifier("2.5.4.18").intern(); 41 public static final ASN1ObjectIdentifier preferredDeliveryMethod = new ASN1ObjectIdentifier("2.5.4.28").intern(); 42 public static final ASN1ObjectIdentifier registeredAddress = new ASN1ObjectIdentifier("2.5.4.26").intern(); 43 public static final ASN1ObjectIdentifier roleOccupant = new ASN1ObjectIdentifier("2.5.4.33").intern(); 44 public static final ASN1ObjectIdentifier searchGuide = new ASN1ObjectIdentifier("2.5.4.14").intern(); 45 public static final ASN1ObjectIdentifier seeAlso = new ASN1ObjectIdentifier("2.5.4.34").intern(); 46 public static final ASN1ObjectIdentifier serialNumber = new ASN1ObjectIdentifier("2.5.4.5").intern(); 47 public static final ASN1ObjectIdentifier sn = new ASN1ObjectIdentifier("2.5.4.4").intern(); 48 public static final ASN1ObjectIdentifier st = new ASN1ObjectIdentifier("2.5.4.8").intern(); 49 public static final ASN1ObjectIdentifier street = new ASN1ObjectIdentifier("2.5.4.9").intern(); 50 public static final ASN1ObjectIdentifier telephoneNumber = new ASN1ObjectIdentifier("2.5.4.20").intern(); 51 public static final ASN1ObjectIdentifier teletexTerminalIdentifier = new ASN1ObjectIdentifier("2.5.4.22").intern(); 52 public static final ASN1ObjectIdentifier telexNumber = new ASN1ObjectIdentifier("2.5.4.21").intern(); 53 public static final ASN1ObjectIdentifier title = new ASN1ObjectIdentifier("2.5.4.12").intern(); 54 public static final ASN1ObjectIdentifier uid = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.1").intern(); 55 public static final ASN1ObjectIdentifier uniqueMember = new ASN1ObjectIdentifier("2.5.4.50").intern(); 56 public static final ASN1ObjectIdentifier userPassword = new ASN1ObjectIdentifier("2.5.4.35").intern(); 57 public static final ASN1ObjectIdentifier x121Address = new ASN1ObjectIdentifier("2.5.4.24").intern(); 58 public static final ASN1ObjectIdentifier x500UniqueIdentifier = new ASN1ObjectIdentifier("2.5.4.45").intern(); 59 60 /** 61 * default look up table translating OID values into their common symbols following 62 * the convention in RFC 2253 with a few extras 63 */ 64 private static final Hashtable DefaultSymbols = new Hashtable(); 65 66 /** 67 * look up table translating common symbols into their OIDS. 68 */ 69 private static final Hashtable DefaultLookUp = new Hashtable(); 70 71 static 72 { 73 DefaultSymbols.put(businessCategory, "businessCategory"); 74 DefaultSymbols.put(c, "c"); 75 DefaultSymbols.put(cn, "cn"); 76 DefaultSymbols.put(dc, "dc"); 77 DefaultSymbols.put(description, "description"); 78 DefaultSymbols.put(destinationIndicator, "destinationIndicator"); 79 DefaultSymbols.put(distinguishedName, "distinguishedName"); 80 DefaultSymbols.put(dnQualifier, "dnQualifier"); 81 DefaultSymbols.put(enhancedSearchGuide, "enhancedSearchGuide"); 82 DefaultSymbols.put(facsimileTelephoneNumber, "facsimileTelephoneNumber"); 83 DefaultSymbols.put(generationQualifier, "generationQualifier"); 84 DefaultSymbols.put(givenName, "givenName"); 85 DefaultSymbols.put(houseIdentifier, "houseIdentifier"); 86 DefaultSymbols.put(initials, "initials"); 87 DefaultSymbols.put(internationalISDNNumber, "internationalISDNNumber"); 88 DefaultSymbols.put(l, "l"); 89 DefaultSymbols.put(member, "member"); 90 DefaultSymbols.put(name, "name"); 91 DefaultSymbols.put(o, "o"); 92 DefaultSymbols.put(ou, "ou"); 93 DefaultSymbols.put(owner, "owner"); 94 DefaultSymbols.put(physicalDeliveryOfficeName, "physicalDeliveryOfficeName"); 95 DefaultSymbols.put(postalAddress, "postalAddress"); 96 DefaultSymbols.put(postalCode, "postalCode"); 97 DefaultSymbols.put(postOfficeBox, "postOfficeBox"); 98 DefaultSymbols.put(preferredDeliveryMethod, "preferredDeliveryMethod"); 99 DefaultSymbols.put(registeredAddress, "registeredAddress"); 100 DefaultSymbols.put(roleOccupant, "roleOccupant"); 101 DefaultSymbols.put(searchGuide, "searchGuide"); 102 DefaultSymbols.put(seeAlso, "seeAlso"); 103 DefaultSymbols.put(serialNumber, "serialNumber"); 104 DefaultSymbols.put(sn, "sn"); 105 DefaultSymbols.put(st, "st"); 106 DefaultSymbols.put(street, "street"); 107 DefaultSymbols.put(telephoneNumber, "telephoneNumber"); 108 DefaultSymbols.put(teletexTerminalIdentifier, "teletexTerminalIdentifier"); 109 DefaultSymbols.put(telexNumber, "telexNumber"); 110 DefaultSymbols.put(title, "title"); 111 DefaultSymbols.put(uid, "uid"); 112 DefaultSymbols.put(uniqueMember, "uniqueMember"); 113 DefaultSymbols.put(userPassword, "userPassword"); 114 DefaultSymbols.put(x121Address, "x121Address"); 115 DefaultSymbols.put(x500UniqueIdentifier, "x500UniqueIdentifier"); 116 117 DefaultLookUp.put("businesscategory", businessCategory); 118 DefaultLookUp.put("c", c); 119 DefaultLookUp.put("cn", cn); 120 DefaultLookUp.put("dc", dc); 121 DefaultLookUp.put("description", description); 122 DefaultLookUp.put("destinationindicator", destinationIndicator); 123 DefaultLookUp.put("distinguishedname", distinguishedName); 124 DefaultLookUp.put("dnqualifier", dnQualifier); 125 DefaultLookUp.put("enhancedsearchguide", enhancedSearchGuide); 126 DefaultLookUp.put("facsimiletelephonenumber", facsimileTelephoneNumber); 127 DefaultLookUp.put("generationqualifier", generationQualifier); 128 DefaultLookUp.put("givenname", givenName); 129 DefaultLookUp.put("houseidentifier", houseIdentifier); 130 DefaultLookUp.put("initials", initials); 131 DefaultLookUp.put("internationalisdnnumber", internationalISDNNumber); 132 DefaultLookUp.put("l", l); 133 DefaultLookUp.put("member", member); 134 DefaultLookUp.put("name", name); 135 DefaultLookUp.put("o", o); 136 DefaultLookUp.put("ou", ou); 137 DefaultLookUp.put("owner", owner); 138 DefaultLookUp.put("physicaldeliveryofficename", physicalDeliveryOfficeName); 139 DefaultLookUp.put("postaladdress", postalAddress); 140 DefaultLookUp.put("postalcode", postalCode); 141 DefaultLookUp.put("postofficebox", postOfficeBox); 142 DefaultLookUp.put("preferreddeliverymethod", preferredDeliveryMethod); 143 DefaultLookUp.put("registeredaddress", registeredAddress); 144 DefaultLookUp.put("roleoccupant", roleOccupant); 145 DefaultLookUp.put("searchguide", searchGuide); 146 DefaultLookUp.put("seealso", seeAlso); 147 DefaultLookUp.put("serialnumber", serialNumber); 148 DefaultLookUp.put("sn", sn); 149 DefaultLookUp.put("st", st); 150 DefaultLookUp.put("street", street); 151 DefaultLookUp.put("telephonenumber", telephoneNumber); 152 DefaultLookUp.put("teletexterminalidentifier", teletexTerminalIdentifier); 153 DefaultLookUp.put("telexnumber", telexNumber); 154 DefaultLookUp.put("title", title); 155 DefaultLookUp.put("uid", uid); 156 DefaultLookUp.put("uniquemember", uniqueMember); 157 DefaultLookUp.put("userpassword", userPassword); 158 DefaultLookUp.put("x121address", x121Address); 159 DefaultLookUp.put("x500uniqueidentifier", x500UniqueIdentifier); 160 161 // TODO: need to add correct matching for equality comparisons. 162 } 163 164 /** 165 * Singleton instance. 166 */ 167 public static final X500NameStyle INSTANCE = new RFC4519Style(); 168 169 protected final Hashtable defaultLookUp; 170 protected final Hashtable defaultSymbols; 171 172 protected RFC4519Style() 173 { 174 defaultSymbols = copyHashTable(DefaultSymbols); 175 defaultLookUp = copyHashTable(DefaultLookUp); 176 } 177 178 protected ASN1Encodable encodeStringValue(ASN1ObjectIdentifier oid, 179 String value) { 180 if (oid.equals(dc)) 181 { 182 return new DERIA5String(value); 183 } 184 else if (oid.equals(c) || oid.equals(serialNumber) || oid.equals(dnQualifier) 185 || oid.equals(telephoneNumber)) 186 { 187 return new DERPrintableString(value); 188 } 189 190 return super.encodeStringValue(oid, value); 191 } 192 193 public String oidToDisplayName(ASN1ObjectIdentifier oid) 194 { 195 return (String)DefaultSymbols.get(oid); 196 } 197 198 public String[] oidToAttrNames(ASN1ObjectIdentifier oid) 199 { 200 return IETFUtils.findAttrNamesForOID(oid, defaultLookUp); 201 } 202 203 public ASN1ObjectIdentifier attrNameToOID(String attrName) 204 { 205 return IETFUtils.decodeAttrName(attrName, defaultLookUp); 206 } 207 208 // parse backwards 209 public RDN[] fromString(String dirName) 210 { 211 RDN[] tmp = IETFUtils.rDNsFromString(dirName, this); 212 RDN[] res = new RDN[tmp.length]; 213 214 for (int i = 0; i != tmp.length; i++) 215 { 216 res[res.length - i - 1] = tmp[i]; 217 } 218 219 return res; 220 } 221 222 // convert in reverse 223 public String toString(X500Name name) 224 { 225 StringBuffer buf = new StringBuffer(); 226 boolean first = true; 227 228 RDN[] rdns = name.getRDNs(); 229 230 for (int i = rdns.length - 1; i >= 0; i--) 231 { 232 if (first) 233 { 234 first = false; 235 } 236 else 237 { 238 buf.append(','); 239 } 240 241 IETFUtils.appendRDN(buf, rdns[i], defaultSymbols); 242 } 243 244 return buf.toString(); 245 } 246 247 248 } 249