BCStyle.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.asn1.x500.style; 2 3import java.io.IOException; 4import java.util.Enumeration; 5import java.util.Hashtable; 6 7import org.bouncycastle.asn1.ASN1Encodable; 8import org.bouncycastle.asn1.ASN1GeneralizedTime; 9import org.bouncycastle.asn1.ASN1ObjectIdentifier; 10import org.bouncycastle.asn1.DERIA5String; 11import org.bouncycastle.asn1.DERPrintableString; 12import org.bouncycastle.asn1.DERUTF8String; 13import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 14import org.bouncycastle.asn1.x500.AttributeTypeAndValue; 15import org.bouncycastle.asn1.x500.RDN; 16import org.bouncycastle.asn1.x500.X500Name; 17import org.bouncycastle.asn1.x500.X500NameStyle; 18import org.bouncycastle.asn1.x509.X509ObjectIdentifiers; 19 20public class BCStyle 21 implements X500NameStyle 22{ 23 /** 24 * country code - StringType(SIZE(2)) 25 */ 26 public static final ASN1ObjectIdentifier C = new ASN1ObjectIdentifier("2.5.4.6"); 27 28 /** 29 * organization - StringType(SIZE(1..64)) 30 */ 31 public static final ASN1ObjectIdentifier O = new ASN1ObjectIdentifier("2.5.4.10"); 32 33 /** 34 * organizational unit name - StringType(SIZE(1..64)) 35 */ 36 public static final ASN1ObjectIdentifier OU = new ASN1ObjectIdentifier("2.5.4.11"); 37 38 /** 39 * Title 40 */ 41 public static final ASN1ObjectIdentifier T = new ASN1ObjectIdentifier("2.5.4.12"); 42 43 /** 44 * common name - StringType(SIZE(1..64)) 45 */ 46 public static final ASN1ObjectIdentifier CN = new ASN1ObjectIdentifier("2.5.4.3"); 47 48 /** 49 * device serial number name - StringType(SIZE(1..64)) 50 */ 51 public static final ASN1ObjectIdentifier SN = new ASN1ObjectIdentifier("2.5.4.5"); 52 53 /** 54 * street - StringType(SIZE(1..64)) 55 */ 56 public static final ASN1ObjectIdentifier STREET = new ASN1ObjectIdentifier("2.5.4.9"); 57 58 /** 59 * device serial number name - StringType(SIZE(1..64)) 60 */ 61 public static final ASN1ObjectIdentifier SERIALNUMBER = SN; 62 63 /** 64 * locality name - StringType(SIZE(1..64)) 65 */ 66 public static final ASN1ObjectIdentifier L = new ASN1ObjectIdentifier("2.5.4.7"); 67 68 /** 69 * state, or province name - StringType(SIZE(1..64)) 70 */ 71 public static final ASN1ObjectIdentifier ST = new ASN1ObjectIdentifier("2.5.4.8"); 72 73 /** 74 * Naming attributes of type X520name 75 */ 76 public static final ASN1ObjectIdentifier SURNAME = new ASN1ObjectIdentifier("2.5.4.4"); 77 public static final ASN1ObjectIdentifier GIVENNAME = new ASN1ObjectIdentifier("2.5.4.42"); 78 public static final ASN1ObjectIdentifier INITIALS = new ASN1ObjectIdentifier("2.5.4.43"); 79 public static final ASN1ObjectIdentifier GENERATION = new ASN1ObjectIdentifier("2.5.4.44"); 80 public static final ASN1ObjectIdentifier UNIQUE_IDENTIFIER = new ASN1ObjectIdentifier("2.5.4.45"); 81 82 /** 83 * businessCategory - DirectoryString(SIZE(1..128) 84 */ 85 public static final ASN1ObjectIdentifier BUSINESS_CATEGORY = new ASN1ObjectIdentifier( 86 "2.5.4.15"); 87 88 /** 89 * postalCode - DirectoryString(SIZE(1..40) 90 */ 91 public static final ASN1ObjectIdentifier POSTAL_CODE = new ASN1ObjectIdentifier( 92 "2.5.4.17"); 93 94 /** 95 * dnQualifier - DirectoryString(SIZE(1..64) 96 */ 97 public static final ASN1ObjectIdentifier DN_QUALIFIER = new ASN1ObjectIdentifier( 98 "2.5.4.46"); 99 100 /** 101 * RFC 3039 Pseudonym - DirectoryString(SIZE(1..64) 102 */ 103 public static final ASN1ObjectIdentifier PSEUDONYM = new ASN1ObjectIdentifier( 104 "2.5.4.65"); 105 106 107 /** 108 * RFC 3039 DateOfBirth - GeneralizedTime - YYYYMMDD000000Z 109 */ 110 public static final ASN1ObjectIdentifier DATE_OF_BIRTH = new ASN1ObjectIdentifier( 111 "1.3.6.1.5.5.7.9.1"); 112 113 /** 114 * RFC 3039 PlaceOfBirth - DirectoryString(SIZE(1..128) 115 */ 116 public static final ASN1ObjectIdentifier PLACE_OF_BIRTH = new ASN1ObjectIdentifier( 117 "1.3.6.1.5.5.7.9.2"); 118 119 /** 120 * RFC 3039 Gender - PrintableString (SIZE(1)) -- "M", "F", "m" or "f" 121 */ 122 public static final ASN1ObjectIdentifier GENDER = new ASN1ObjectIdentifier( 123 "1.3.6.1.5.5.7.9.3"); 124 125 /** 126 * RFC 3039 CountryOfCitizenship - PrintableString (SIZE (2)) -- ISO 3166 127 * codes only 128 */ 129 public static final ASN1ObjectIdentifier COUNTRY_OF_CITIZENSHIP = new ASN1ObjectIdentifier( 130 "1.3.6.1.5.5.7.9.4"); 131 132 /** 133 * RFC 3039 CountryOfResidence - PrintableString (SIZE (2)) -- ISO 3166 134 * codes only 135 */ 136 public static final ASN1ObjectIdentifier COUNTRY_OF_RESIDENCE = new ASN1ObjectIdentifier( 137 "1.3.6.1.5.5.7.9.5"); 138 139 140 /** 141 * ISIS-MTT NameAtBirth - DirectoryString(SIZE(1..64) 142 */ 143 public static final ASN1ObjectIdentifier NAME_AT_BIRTH = new ASN1ObjectIdentifier("1.3.36.8.3.14"); 144 145 /** 146 * RFC 3039 PostalAddress - SEQUENCE SIZE (1..6) OF 147 * DirectoryString(SIZE(1..30)) 148 */ 149 public static final ASN1ObjectIdentifier POSTAL_ADDRESS = new ASN1ObjectIdentifier("2.5.4.16"); 150 151 /** 152 * RFC 2256 dmdName 153 */ 154 public static final ASN1ObjectIdentifier DMD_NAME = new ASN1ObjectIdentifier("2.5.4.54"); 155 156 /** 157 * id-at-telephoneNumber 158 */ 159 public static final ASN1ObjectIdentifier TELEPHONE_NUMBER = X509ObjectIdentifiers.id_at_telephoneNumber; 160 161 /** 162 * id-at-name 163 */ 164 public static final ASN1ObjectIdentifier NAME = X509ObjectIdentifiers.id_at_name; 165 166 /** 167 * Email address (RSA PKCS#9 extension) - IA5String. 168 * <p>Note: if you're trying to be ultra orthodox, don't use this! It shouldn't be in here. 169 */ 170 public static final ASN1ObjectIdentifier EmailAddress = PKCSObjectIdentifiers.pkcs_9_at_emailAddress; 171 172 /** 173 * more from PKCS#9 174 */ 175 public static final ASN1ObjectIdentifier UnstructuredName = PKCSObjectIdentifiers.pkcs_9_at_unstructuredName; 176 public static final ASN1ObjectIdentifier UnstructuredAddress = PKCSObjectIdentifiers.pkcs_9_at_unstructuredAddress; 177 178 /** 179 * email address in Verisign certificates 180 */ 181 public static final ASN1ObjectIdentifier E = EmailAddress; 182 183 /* 184 * others... 185 */ 186 public static final ASN1ObjectIdentifier DC = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.25"); 187 188 /** 189 * LDAP User id. 190 */ 191 public static final ASN1ObjectIdentifier UID = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.1"); 192 193 /** 194 * default look up table translating OID values into their common symbols following 195 * the convention in RFC 2253 with a few extras 196 */ 197 private static final Hashtable DefaultSymbols = new Hashtable(); 198 199 /** 200 * look up table translating common symbols into their OIDS. 201 */ 202 private static final Hashtable DefaultLookUp = new Hashtable(); 203 204 static 205 { 206 DefaultSymbols.put(C, "C"); 207 DefaultSymbols.put(O, "O"); 208 DefaultSymbols.put(T, "T"); 209 DefaultSymbols.put(OU, "OU"); 210 DefaultSymbols.put(CN, "CN"); 211 DefaultSymbols.put(L, "L"); 212 DefaultSymbols.put(ST, "ST"); 213 DefaultSymbols.put(SN, "SERIALNUMBER"); 214 DefaultSymbols.put(EmailAddress, "E"); 215 DefaultSymbols.put(DC, "DC"); 216 DefaultSymbols.put(UID, "UID"); 217 DefaultSymbols.put(STREET, "STREET"); 218 DefaultSymbols.put(SURNAME, "SURNAME"); 219 DefaultSymbols.put(GIVENNAME, "GIVENNAME"); 220 DefaultSymbols.put(INITIALS, "INITIALS"); 221 DefaultSymbols.put(GENERATION, "GENERATION"); 222 DefaultSymbols.put(UnstructuredAddress, "unstructuredAddress"); 223 DefaultSymbols.put(UnstructuredName, "unstructuredName"); 224 DefaultSymbols.put(UNIQUE_IDENTIFIER, "UniqueIdentifier"); 225 DefaultSymbols.put(DN_QUALIFIER, "DN"); 226 DefaultSymbols.put(PSEUDONYM, "Pseudonym"); 227 DefaultSymbols.put(POSTAL_ADDRESS, "PostalAddress"); 228 DefaultSymbols.put(NAME_AT_BIRTH, "NameAtBirth"); 229 DefaultSymbols.put(COUNTRY_OF_CITIZENSHIP, "CountryOfCitizenship"); 230 DefaultSymbols.put(COUNTRY_OF_RESIDENCE, "CountryOfResidence"); 231 DefaultSymbols.put(GENDER, "Gender"); 232 DefaultSymbols.put(PLACE_OF_BIRTH, "PlaceOfBirth"); 233 DefaultSymbols.put(DATE_OF_BIRTH, "DateOfBirth"); 234 DefaultSymbols.put(POSTAL_CODE, "PostalCode"); 235 DefaultSymbols.put(BUSINESS_CATEGORY, "BusinessCategory"); 236 DefaultSymbols.put(TELEPHONE_NUMBER, "TelephoneNumber"); 237 DefaultSymbols.put(NAME, "Name"); 238 239 DefaultLookUp.put("c", C); 240 DefaultLookUp.put("o", O); 241 DefaultLookUp.put("t", T); 242 DefaultLookUp.put("ou", OU); 243 DefaultLookUp.put("cn", CN); 244 DefaultLookUp.put("l", L); 245 DefaultLookUp.put("st", ST); 246 DefaultLookUp.put("sn", SN); 247 DefaultLookUp.put("serialnumber", SN); 248 DefaultLookUp.put("street", STREET); 249 DefaultLookUp.put("emailaddress", E); 250 DefaultLookUp.put("dc", DC); 251 DefaultLookUp.put("e", E); 252 DefaultLookUp.put("uid", UID); 253 DefaultLookUp.put("surname", SURNAME); 254 DefaultLookUp.put("givenname", GIVENNAME); 255 DefaultLookUp.put("initials", INITIALS); 256 DefaultLookUp.put("generation", GENERATION); 257 DefaultLookUp.put("unstructuredaddress", UnstructuredAddress); 258 DefaultLookUp.put("unstructuredname", UnstructuredName); 259 DefaultLookUp.put("uniqueidentifier", UNIQUE_IDENTIFIER); 260 DefaultLookUp.put("dn", DN_QUALIFIER); 261 DefaultLookUp.put("pseudonym", PSEUDONYM); 262 DefaultLookUp.put("postaladdress", POSTAL_ADDRESS); 263 DefaultLookUp.put("nameofbirth", NAME_AT_BIRTH); 264 DefaultLookUp.put("countryofcitizenship", COUNTRY_OF_CITIZENSHIP); 265 DefaultLookUp.put("countryofresidence", COUNTRY_OF_RESIDENCE); 266 DefaultLookUp.put("gender", GENDER); 267 DefaultLookUp.put("placeofbirth", PLACE_OF_BIRTH); 268 DefaultLookUp.put("dateofbirth", DATE_OF_BIRTH); 269 DefaultLookUp.put("postalcode", POSTAL_CODE); 270 DefaultLookUp.put("businesscategory", BUSINESS_CATEGORY); 271 DefaultLookUp.put("telephonenumber", TELEPHONE_NUMBER); 272 DefaultLookUp.put("name", NAME); 273 } 274 275 /** 276 * Singleton instance. 277 */ 278 public static final X500NameStyle INSTANCE = new BCStyle(); 279 280 protected final Hashtable defaultLookUp; 281 protected final Hashtable defaultSymbols; 282 283 protected BCStyle() 284 { 285 defaultSymbols = copyHashTable(DefaultSymbols); 286 defaultLookUp = copyHashTable(DefaultLookUp); 287 } 288 289 public ASN1Encodable stringToValue(ASN1ObjectIdentifier oid, String value) 290 { 291 if (value.length() != 0 && value.charAt(0) == '#') 292 { 293 try 294 { 295 return IETFUtils.valueFromHexString(value, 1); 296 } 297 catch (IOException e) 298 { 299 throw new RuntimeException("can't recode value for oid " + oid.getId()); 300 } 301 } 302 else 303 { 304 if (value.length() != 0 && value.charAt(0) == '\\') 305 { 306 value = value.substring(1); 307 } 308 if (oid.equals(EmailAddress) || oid.equals(DC)) 309 { 310 return new DERIA5String(value); 311 } 312 else if (oid.equals(DATE_OF_BIRTH)) // accept time string as well as # (for compatibility) 313 { 314 return new ASN1GeneralizedTime(value); 315 } 316 else if (oid.equals(C) || oid.equals(SN) || oid.equals(DN_QUALIFIER) 317 || oid.equals(TELEPHONE_NUMBER)) 318 { 319 return new DERPrintableString(value); 320 } 321 } 322 323 return new DERUTF8String(value); 324 } 325 326 public String oidToDisplayName(ASN1ObjectIdentifier oid) 327 { 328 return (String)DefaultSymbols.get(oid); 329 } 330 331 public String[] oidToAttrNames(ASN1ObjectIdentifier oid) 332 { 333 return IETFUtils.findAttrNamesForOID(oid, defaultLookUp); 334 } 335 336 public ASN1ObjectIdentifier attrNameToOID(String attrName) 337 { 338 return IETFUtils.decodeAttrName(attrName, defaultLookUp); 339 } 340 341 public boolean areEqual(X500Name name1, X500Name name2) 342 { 343 RDN[] rdns1 = name1.getRDNs(); 344 RDN[] rdns2 = name2.getRDNs(); 345 346 if (rdns1.length != rdns2.length) 347 { 348 return false; 349 } 350 351 boolean reverse = false; 352 353 if (rdns1[0].getFirst() != null && rdns2[0].getFirst() != null) 354 { 355 reverse = !rdns1[0].getFirst().getType().equals(rdns2[0].getFirst().getType()); // guess forward 356 } 357 358 for (int i = 0; i != rdns1.length; i++) 359 { 360 if (!foundMatch(reverse, rdns1[i], rdns2)) 361 { 362 return false; 363 } 364 } 365 366 return true; 367 } 368 369 private boolean foundMatch(boolean reverse, RDN rdn, RDN[] possRDNs) 370 { 371 if (reverse) 372 { 373 for (int i = possRDNs.length - 1; i >= 0; i--) 374 { 375 if (possRDNs[i] != null && rdnAreEqual(rdn, possRDNs[i])) 376 { 377 possRDNs[i] = null; 378 return true; 379 } 380 } 381 } 382 else 383 { 384 for (int i = 0; i != possRDNs.length; i++) 385 { 386 if (possRDNs[i] != null && rdnAreEqual(rdn, possRDNs[i])) 387 { 388 possRDNs[i] = null; 389 return true; 390 } 391 } 392 } 393 394 return false; 395 } 396 397 protected boolean rdnAreEqual(RDN rdn1, RDN rdn2) 398 { 399 return IETFUtils.rDNAreEqual(rdn1, rdn2); 400 } 401 402 public RDN[] fromString(String dirName) 403 { 404 return IETFUtils.rDNsFromString(dirName, this); 405 } 406 407 public int calculateHashCode(X500Name name) 408 { 409 int hashCodeValue = 0; 410 RDN[] rdns = name.getRDNs(); 411 412 // this needs to be order independent, like equals 413 for (int i = 0; i != rdns.length; i++) 414 { 415 if (rdns[i].isMultiValued()) 416 { 417 AttributeTypeAndValue[] atv = rdns[i].getTypesAndValues(); 418 419 for (int j = 0; j != atv.length; j++) 420 { 421 hashCodeValue ^= atv[j].getType().hashCode(); 422 hashCodeValue ^= calcHashCode(atv[j].getValue()); 423 } 424 } 425 else 426 { 427 hashCodeValue ^= rdns[i].getFirst().getType().hashCode(); 428 hashCodeValue ^= calcHashCode(rdns[i].getFirst().getValue()); 429 } 430 } 431 432 return hashCodeValue; 433 } 434 435 private int calcHashCode(ASN1Encodable enc) 436 { 437 String value = IETFUtils.valueToString(enc); 438 439 value = IETFUtils.canonicalize(value); 440 441 return value.hashCode(); 442 } 443 444 public String toString(X500Name name) 445 { 446 StringBuffer buf = new StringBuffer(); 447 boolean first = true; 448 449 RDN[] rdns = name.getRDNs(); 450 451 for (int i = 0; i < rdns.length; i++) 452 { 453 if (first) 454 { 455 first = false; 456 } 457 else 458 { 459 buf.append(','); 460 } 461 462 IETFUtils.appendRDN(buf, rdns[i], defaultSymbols); 463 } 464 465 return buf.toString(); 466 } 467 468 private static Hashtable copyHashTable(Hashtable paramsMap) 469 { 470 Hashtable newTable = new Hashtable(); 471 472 Enumeration keys = paramsMap.keys(); 473 while (keys.hasMoreElements()) 474 { 475 Object key = keys.nextElement(); 476 newTable.put(key, paramsMap.get(key)); 477 } 478 479 return newTable; 480 } 481} 482