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.ASN1GeneralizedTime; 7 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 8 import org.bouncycastle.asn1.DERIA5String; 9 import org.bouncycastle.asn1.DERPrintableString; 10 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 11 import org.bouncycastle.asn1.x500.RDN; 12 import org.bouncycastle.asn1.x500.X500Name; 13 import org.bouncycastle.asn1.x500.X500NameStyle; 14 import org.bouncycastle.asn1.x509.X509ObjectIdentifiers; 15 16 public class BCStyle 17 extends AbstractX500NameStyle 18 { 19 /** 20 * country code - StringType(SIZE(2)) 21 */ 22 public static final ASN1ObjectIdentifier C = new ASN1ObjectIdentifier("2.5.4.6").intern(); 23 24 /** 25 * organization - StringType(SIZE(1..64)) 26 */ 27 public static final ASN1ObjectIdentifier O = new ASN1ObjectIdentifier("2.5.4.10").intern(); 28 29 /** 30 * organizational unit name - StringType(SIZE(1..64)) 31 */ 32 public static final ASN1ObjectIdentifier OU = new ASN1ObjectIdentifier("2.5.4.11").intern(); 33 34 /** 35 * Title 36 */ 37 public static final ASN1ObjectIdentifier T = new ASN1ObjectIdentifier("2.5.4.12").intern(); 38 39 /** 40 * common name - StringType(SIZE(1..64)) 41 */ 42 public static final ASN1ObjectIdentifier CN = new ASN1ObjectIdentifier("2.5.4.3").intern(); 43 44 /** 45 * device serial number name - StringType(SIZE(1..64)) 46 */ 47 public static final ASN1ObjectIdentifier SN = new ASN1ObjectIdentifier("2.5.4.5").intern(); 48 49 /** 50 * street - StringType(SIZE(1..64)) 51 */ 52 public static final ASN1ObjectIdentifier STREET = new ASN1ObjectIdentifier("2.5.4.9").intern(); 53 54 /** 55 * device serial number name - StringType(SIZE(1..64)) 56 */ 57 public static final ASN1ObjectIdentifier SERIALNUMBER = SN; 58 59 /** 60 * locality name - StringType(SIZE(1..64)) 61 */ 62 public static final ASN1ObjectIdentifier L = new ASN1ObjectIdentifier("2.5.4.7").intern(); 63 64 /** 65 * state, or province name - StringType(SIZE(1..64)) 66 */ 67 public static final ASN1ObjectIdentifier ST = new ASN1ObjectIdentifier("2.5.4.8").intern(); 68 69 /** 70 * Naming attributes of type X520name 71 */ 72 public static final ASN1ObjectIdentifier SURNAME = new ASN1ObjectIdentifier("2.5.4.4").intern(); 73 public static final ASN1ObjectIdentifier GIVENNAME = new ASN1ObjectIdentifier("2.5.4.42").intern(); 74 public static final ASN1ObjectIdentifier INITIALS = new ASN1ObjectIdentifier("2.5.4.43").intern(); 75 public static final ASN1ObjectIdentifier GENERATION = new ASN1ObjectIdentifier("2.5.4.44").intern(); 76 public static final ASN1ObjectIdentifier UNIQUE_IDENTIFIER = new ASN1ObjectIdentifier("2.5.4.45").intern(); 77 78 /** 79 * businessCategory - DirectoryString(SIZE(1..128) 80 */ 81 public static final ASN1ObjectIdentifier BUSINESS_CATEGORY = new ASN1ObjectIdentifier("2.5.4.15").intern(); 82 83 /** 84 * postalCode - DirectoryString(SIZE(1..40) 85 */ 86 public static final ASN1ObjectIdentifier POSTAL_CODE = new ASN1ObjectIdentifier("2.5.4.17").intern(); 87 88 /** 89 * dnQualifier - DirectoryString(SIZE(1..64) 90 */ 91 public static final ASN1ObjectIdentifier DN_QUALIFIER = new ASN1ObjectIdentifier("2.5.4.46").intern(); 92 93 /** 94 * RFC 3039 Pseudonym - DirectoryString(SIZE(1..64) 95 */ 96 public static final ASN1ObjectIdentifier PSEUDONYM = new ASN1ObjectIdentifier("2.5.4.65").intern(); 97 98 99 /** 100 * RFC 3039 DateOfBirth - GeneralizedTime - YYYYMMDD000000Z 101 */ 102 public static final ASN1ObjectIdentifier DATE_OF_BIRTH = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.9.1").intern(); 103 104 /** 105 * RFC 3039 PlaceOfBirth - DirectoryString(SIZE(1..128) 106 */ 107 public static final ASN1ObjectIdentifier PLACE_OF_BIRTH = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.9.2").intern(); 108 109 /** 110 * RFC 3039 Gender - PrintableString (SIZE(1)) -- "M", "F", "m" or "f" 111 */ 112 public static final ASN1ObjectIdentifier GENDER = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.9.3").intern(); 113 114 /** 115 * RFC 3039 CountryOfCitizenship - PrintableString (SIZE (2)) -- ISO 3166 116 * codes only 117 */ 118 public static final ASN1ObjectIdentifier COUNTRY_OF_CITIZENSHIP = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.9.4").intern(); 119 120 /** 121 * RFC 3039 CountryOfResidence - PrintableString (SIZE (2)) -- ISO 3166 122 * codes only 123 */ 124 public static final ASN1ObjectIdentifier COUNTRY_OF_RESIDENCE = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.9.5").intern(); 125 126 127 /** 128 * ISIS-MTT NameAtBirth - DirectoryString(SIZE(1..64) 129 */ 130 public static final ASN1ObjectIdentifier NAME_AT_BIRTH = new ASN1ObjectIdentifier("1.3.36.8.3.14").intern(); 131 132 /** 133 * RFC 3039 PostalAddress - SEQUENCE SIZE (1..6) OF 134 * DirectoryString(SIZE(1..30)) 135 */ 136 public static final ASN1ObjectIdentifier POSTAL_ADDRESS = new ASN1ObjectIdentifier("2.5.4.16").intern(); 137 138 /** 139 * RFC 2256 dmdName 140 */ 141 public static final ASN1ObjectIdentifier DMD_NAME = new ASN1ObjectIdentifier("2.5.4.54").intern(); 142 143 /** 144 * id-at-telephoneNumber 145 */ 146 public static final ASN1ObjectIdentifier TELEPHONE_NUMBER = X509ObjectIdentifiers.id_at_telephoneNumber; 147 148 /** 149 * id-at-name 150 */ 151 public static final ASN1ObjectIdentifier NAME = X509ObjectIdentifiers.id_at_name; 152 153 /** 154 * Email address (RSA PKCS#9 extension) - IA5String. 155 * <p>Note: if you're trying to be ultra orthodox, don't use this! It shouldn't be in here. 156 */ 157 public static final ASN1ObjectIdentifier EmailAddress = PKCSObjectIdentifiers.pkcs_9_at_emailAddress; 158 159 /** 160 * more from PKCS#9 161 */ 162 public static final ASN1ObjectIdentifier UnstructuredName = PKCSObjectIdentifiers.pkcs_9_at_unstructuredName; 163 public static final ASN1ObjectIdentifier UnstructuredAddress = PKCSObjectIdentifiers.pkcs_9_at_unstructuredAddress; 164 165 /** 166 * email address in Verisign certificates 167 */ 168 public static final ASN1ObjectIdentifier E = EmailAddress; 169 170 /* 171 * others... 172 */ 173 public static final ASN1ObjectIdentifier DC = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.25"); 174 175 /** 176 * LDAP User id. 177 */ 178 public static final ASN1ObjectIdentifier UID = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.1"); 179 180 /** 181 * default look up table translating OID values into their common symbols following 182 * the convention in RFC 2253 with a few extras 183 */ 184 private static final Hashtable DefaultSymbols = new Hashtable(); 185 186 /** 187 * look up table translating common symbols into their OIDS. 188 */ 189 private static final Hashtable DefaultLookUp = new Hashtable(); 190 191 static 192 { 193 DefaultSymbols.put(C, "C"); 194 DefaultSymbols.put(O, "O"); 195 DefaultSymbols.put(T, "T"); 196 DefaultSymbols.put(OU, "OU"); 197 DefaultSymbols.put(CN, "CN"); 198 DefaultSymbols.put(L, "L"); 199 DefaultSymbols.put(ST, "ST"); 200 DefaultSymbols.put(SN, "SERIALNUMBER"); 201 DefaultSymbols.put(EmailAddress, "E"); 202 DefaultSymbols.put(DC, "DC"); 203 DefaultSymbols.put(UID, "UID"); 204 DefaultSymbols.put(STREET, "STREET"); 205 DefaultSymbols.put(SURNAME, "SURNAME"); 206 DefaultSymbols.put(GIVENNAME, "GIVENNAME"); 207 DefaultSymbols.put(INITIALS, "INITIALS"); 208 DefaultSymbols.put(GENERATION, "GENERATION"); 209 DefaultSymbols.put(UnstructuredAddress, "unstructuredAddress"); 210 DefaultSymbols.put(UnstructuredName, "unstructuredName"); 211 DefaultSymbols.put(UNIQUE_IDENTIFIER, "UniqueIdentifier"); 212 DefaultSymbols.put(DN_QUALIFIER, "DN"); 213 DefaultSymbols.put(PSEUDONYM, "Pseudonym"); 214 DefaultSymbols.put(POSTAL_ADDRESS, "PostalAddress"); 215 DefaultSymbols.put(NAME_AT_BIRTH, "NameAtBirth"); 216 DefaultSymbols.put(COUNTRY_OF_CITIZENSHIP, "CountryOfCitizenship"); 217 DefaultSymbols.put(COUNTRY_OF_RESIDENCE, "CountryOfResidence"); 218 DefaultSymbols.put(GENDER, "Gender"); 219 DefaultSymbols.put(PLACE_OF_BIRTH, "PlaceOfBirth"); 220 DefaultSymbols.put(DATE_OF_BIRTH, "DateOfBirth"); 221 DefaultSymbols.put(POSTAL_CODE, "PostalCode"); 222 DefaultSymbols.put(BUSINESS_CATEGORY, "BusinessCategory"); 223 DefaultSymbols.put(TELEPHONE_NUMBER, "TelephoneNumber"); 224 DefaultSymbols.put(NAME, "Name"); 225 226 DefaultLookUp.put("c", C); 227 DefaultLookUp.put("o", O); 228 DefaultLookUp.put("t", T); 229 DefaultLookUp.put("ou", OU); 230 DefaultLookUp.put("cn", CN); 231 DefaultLookUp.put("l", L); 232 DefaultLookUp.put("st", ST); 233 DefaultLookUp.put("sn", SN); 234 DefaultLookUp.put("serialnumber", SN); 235 DefaultLookUp.put("street", STREET); 236 DefaultLookUp.put("emailaddress", E); 237 DefaultLookUp.put("dc", DC); 238 DefaultLookUp.put("e", E); 239 DefaultLookUp.put("uid", UID); 240 DefaultLookUp.put("surname", SURNAME); 241 DefaultLookUp.put("givenname", GIVENNAME); 242 DefaultLookUp.put("initials", INITIALS); 243 DefaultLookUp.put("generation", GENERATION); 244 DefaultLookUp.put("unstructuredaddress", UnstructuredAddress); 245 DefaultLookUp.put("unstructuredname", UnstructuredName); 246 DefaultLookUp.put("uniqueidentifier", UNIQUE_IDENTIFIER); 247 DefaultLookUp.put("dn", DN_QUALIFIER); 248 DefaultLookUp.put("pseudonym", PSEUDONYM); 249 DefaultLookUp.put("postaladdress", POSTAL_ADDRESS); 250 DefaultLookUp.put("nameofbirth", NAME_AT_BIRTH); 251 DefaultLookUp.put("countryofcitizenship", COUNTRY_OF_CITIZENSHIP); 252 DefaultLookUp.put("countryofresidence", COUNTRY_OF_RESIDENCE); 253 DefaultLookUp.put("gender", GENDER); 254 DefaultLookUp.put("placeofbirth", PLACE_OF_BIRTH); 255 DefaultLookUp.put("dateofbirth", DATE_OF_BIRTH); 256 DefaultLookUp.put("postalcode", POSTAL_CODE); 257 DefaultLookUp.put("businesscategory", BUSINESS_CATEGORY); 258 DefaultLookUp.put("telephonenumber", TELEPHONE_NUMBER); 259 DefaultLookUp.put("name", NAME); 260 } 261 262 /** 263 * Singleton instance. 264 */ 265 public static final X500NameStyle INSTANCE = new BCStyle(); 266 267 protected final Hashtable defaultLookUp; 268 protected final Hashtable defaultSymbols; 269 270 protected BCStyle() 271 { 272 defaultSymbols = copyHashTable(DefaultSymbols); 273 defaultLookUp = copyHashTable(DefaultLookUp); 274 } 275 276 protected ASN1Encodable encodeStringValue(ASN1ObjectIdentifier oid, 277 String value) { 278 if (oid.equals(EmailAddress) || oid.equals(DC)) 279 { 280 return new DERIA5String(value); 281 } 282 else if (oid.equals(DATE_OF_BIRTH)) // accept time string as well as # (for compatibility) 283 { 284 return new ASN1GeneralizedTime(value); 285 } 286 else if (oid.equals(C) || oid.equals(SN) || oid.equals(DN_QUALIFIER) 287 || oid.equals(TELEPHONE_NUMBER)) 288 { 289 return new DERPrintableString(value); 290 } 291 292 return super.encodeStringValue(oid, value); 293 } 294 295 public String oidToDisplayName(ASN1ObjectIdentifier oid) 296 { 297 return (String)DefaultSymbols.get(oid); 298 } 299 300 public String[] oidToAttrNames(ASN1ObjectIdentifier oid) 301 { 302 return IETFUtils.findAttrNamesForOID(oid, defaultLookUp); 303 } 304 305 public ASN1ObjectIdentifier attrNameToOID(String attrName) 306 { 307 return IETFUtils.decodeAttrName(attrName, defaultLookUp); 308 } 309 310 public RDN[] fromString(String dirName) 311 { 312 return IETFUtils.rDNsFromString(dirName, this); 313 } 314 315 public String toString(X500Name name) 316 { 317 StringBuffer buf = new StringBuffer(); 318 boolean first = true; 319 320 RDN[] rdns = name.getRDNs(); 321 322 for (int i = 0; i < rdns.length; i++) 323 { 324 if (first) 325 { 326 first = false; 327 } 328 else 329 { 330 buf.append(','); 331 } 332 333 IETFUtils.appendRDN(buf, rdns[i], defaultSymbols); 334 } 335 336 return buf.toString(); 337 } 338 339 340 } 341