1 13 14 package org.ejbca.util.dn; 15 16 import java.io.BufferedReader ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.InputStreamReader ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.LinkedHashMap ; 23 import java.util.Set ; 24 25 import org.apache.commons.lang.ArrayUtils; 26 import org.apache.commons.lang.BooleanUtils; 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.log4j.Logger; 29 import org.bouncycastle.asn1.DERObjectIdentifier; 30 import org.bouncycastle.asn1.x509.X509Name; 31 32 42 public class DnComponents { 43 private static Logger log = Logger.getLogger(DnComponents.class); 44 45 46 private static DnComponents obj = new DnComponents(); 47 48 52 private static HashMap oids = new HashMap (); 53 static { 55 oids.put("c", X509Name.C); 56 oids.put("dc", X509Name.DC); 57 oids.put("st", X509Name.ST); 58 oids.put("l", X509Name.L); 59 oids.put("o", X509Name.O); 60 oids.put("ou", X509Name.OU); 61 oids.put("t", X509Name.T); 62 oids.put("surname", X509Name.SURNAME); 63 oids.put("initials", X509Name.INITIALS); 64 oids.put("givenname", X509Name.GIVENNAME); 65 oids.put("gn", X509Name.GIVENNAME); 66 oids.put("sn", X509Name.SN); 67 oids.put("serialnumber", X509Name.SN); 68 oids.put("cn", X509Name.CN); 69 oids.put("uid", X509Name.UID); 70 oids.put("dn", X509Name.DN_QUALIFIER); 71 oids.put("emailaddress", X509Name.EmailAddress); 72 oids.put("e", X509Name.EmailAddress); 73 oids.put("email", X509Name.EmailAddress); 74 oids.put("unstructuredname", X509Name.UnstructuredName); oids.put("unstructuredaddress", X509Name.UnstructuredAddress); } 77 80 private static String [] dNObjectsForward = { 81 "unstructuredaddress", "unstructuredname", "emailaddress", "e", "email", "dn", "uid", "cn", "sn", "serialnumber", "gn", "givenname", 82 "initials", "surname", "t", "ou", "o", "l", "st", "dc", "c" 83 }; 84 private static String [] dNObjectsReverse = null; 86 87 90 91 94 public static final String DNEMAIL = "DNEMAIL"; 96 public static final String DNQUALIFIER = "DN"; 97 public static final String UID = "UID"; 98 public static final String COMMONNAME = "COMMONNAME"; 99 public static final String SN = "SN"; 100 public static final String GIVENNAME = "GIVENNAME"; 101 public static final String INITIALS = "INITIALS"; 102 public static final String SURNAME = "SURNAME"; 103 public static final String TITLE = "TITLE"; 104 public static final String ORGANIZATIONUNIT = "ORGANIZATIONUNIT"; 105 public static final String ORGANIZATION = "ORGANIZATION"; 106 public static final String LOCALE = "LOCALE"; 107 public static final String STATE = "STATE"; 108 public static final String DOMAINCOMPONENT = "DOMAINCOMPONENT"; 109 public static final String COUNTRY = "COUNTRY"; 110 public static final String UNSTRUCTUREDADDRESS = "UNSTRUCTUREDADDRESS"; 111 public static final String UNSTRUCTUREDNAME = "UNSTRUCTUREDNAME"; 112 113 public static final String RFC822NAME = "RFC822NAME"; 115 public static final String DNSNAME = "DNSNAME"; 116 public static final String IPADDRESS = "IPADDRESS"; 117 public static final String UNIFORMRESOURCEID = "UNIFORMRESOURCEID"; 118 public static final String DIRECTORYNAME = "DIRECTORYNAME"; 119 public static final String UPN = "UPN"; 120 public static final String GUID = "GUID"; 121 public static final String OTHERNAME = "OTHERNAME"; 123 public static final String X400ADDRESS = "X400ADDRESS"; 124 public static final String EDIPARTNAME = "EDIPARTNAME"; 125 public static final String REGISTEREDID = "REGISTEREDID"; 126 127 public static final String DATEOFBIRTH = "DATEOFBIRTH"; 129 public static final String PLACEOFBIRTH = "PLACEOFBIRTH"; 130 public static final String GENDER = "GENDER"; 131 public static final String COUNTRYOFCITIZENSHIP = "COUNTRYOFCITIZENSHIP"; 132 public static final String COUNTRYOFRESIDENCE = "COUNTRYOFRESIDENCE"; 133 134 private static HashMap dnNameIdMap = new HashMap (); 135 private static HashMap profileNameIdMap = new HashMap (); 136 private static HashMap dnIdToProfileNameMap = new HashMap (); 137 private static HashMap dnIdToProfileIdMap = new HashMap (); 138 private static HashMap profileIdToDnIdMap = new HashMap (); 139 private static HashMap dnErrorTextMap = new HashMap (); 140 private static HashMap profileNameLanguageMap = new HashMap (); 141 private static HashMap profileIdLanguageMap = new HashMap (); 142 private static HashMap dnIdErrorMap = new HashMap (); 143 private static HashMap dnIdToExtractorFieldMap = new HashMap (); 144 private static HashMap altNameIdToExtractorFieldMap = new HashMap (); 145 private static HashMap dirAttrIdToExtractorFieldMap = new HashMap (); 146 private static ArrayList dnProfileFields = new ArrayList (); 147 private static ArrayList dnLanguageTexts = new ArrayList (); 148 private static ArrayList dnDnIds = new ArrayList (); 149 private static ArrayList altNameFields = new ArrayList (); 150 private static ArrayList altNameLanguageTexts = new ArrayList (); 151 private static ArrayList altNameDnIds = new ArrayList (); 152 private static ArrayList dirAttrFields = new ArrayList (); 153 private static ArrayList dirAttrLanguageTexts = new ArrayList (); 154 private static ArrayList dirAttrDnIds = new ArrayList (); 155 private static ArrayList dnExtractorFields = new ArrayList (); 156 private static ArrayList altNameExtractorFields = new ArrayList (); 157 private static ArrayList dirAttrExtractorFields = new ArrayList (); 158 159 160 static { 162 DnComponents.load(); 163 } 164 167 private static final boolean reverseOrder = BooleanUtils.toBoolean("@certtools.dnorderreverse@"); 168 169 170 public static DERObjectIdentifier getOid(String o) { 171 return (DERObjectIdentifier) oids.get(o.toLowerCase()); 172 } 174 public static ArrayList getDnProfileFields() { 175 return dnProfileFields; 176 } 177 public static ArrayList getDnLanguageTexts() { 178 return dnLanguageTexts; 179 } 180 public static ArrayList getAltNameFields() { 181 return altNameFields; 182 } 183 public static ArrayList getAltNameLanguageTexts() { 184 return altNameLanguageTexts; 185 } 186 public static ArrayList getDirAttrFields() { 187 return dirAttrFields; 188 } 189 public static ArrayList getDirAttrDnIds() { 191 return dirAttrDnIds; 192 } 193 public static ArrayList getAltNameDnIds() { 195 return altNameDnIds; 196 } 197 public static ArrayList getDnDnIds() { 199 return dnDnIds; 200 } 201 protected static ArrayList getDnExtractorFields() { 203 return dnExtractorFields; 204 } 205 protected static String getDnExtractorFieldFromDnId(int field) { 206 String val = (String )dnIdToExtractorFieldMap.get(new Integer (field)); 207 return val; 208 } 209 protected static ArrayList getAltNameExtractorFields() { 211 return altNameExtractorFields; 212 } 213 protected static String getAltNameExtractorFieldFromDnId(int field) { 214 String val = (String )altNameIdToExtractorFieldMap.get(new Integer (field)); 215 return val; 216 } 217 protected static ArrayList getDirAttrExtractorFields() { 219 return dirAttrExtractorFields; 220 } 221 protected static String getDirAttrExtractorFieldFromDnId(int field) { 222 String val = (String )dirAttrIdToExtractorFieldMap.get(new Integer (field)); 223 return val; 224 } 225 226 public static String dnIdToProfileName(int dnid) { 227 String val = (String )dnIdToProfileNameMap.get(new Integer (dnid)); 228 return val; 229 } 230 public static int dnIdToProfileId(int dnid) { 231 Integer val = (Integer )dnIdToProfileIdMap.get(new Integer (dnid)); 232 return val.intValue(); 233 } 234 237 public static String getLanguageConstantFromProfileName(String name) { 238 String ret = (String )profileNameLanguageMap.get(name); 239 return ret; 240 } 241 244 public static String getLanguageConstantFromProfileId(int id) { 245 String ret = (String )profileIdLanguageMap.get(new Integer (id)); 246 return ret; 247 } 248 251 public static String getErrTextFromDnId(int id) { 252 String ret = (String )dnIdErrorMap.get(new Integer (id)); 253 return ret; 254 } 255 256 257 261 public static HashMap getProfilenameIdMap() { 262 return profileNameIdMap; 263 264 } 265 270 public static int profileIdToDnId(int profileid) { 271 Integer val = (Integer )profileIdToDnIdMap.get(new Integer (profileid)); 272 if (val == null) { 273 log.error("No dn id mapping from profile id "+profileid); 274 } 276 return val.intValue(); 277 } 278 281 public static String []getDnObjects() { 282 if (!reverseOrder) { 283 return dNObjectsForward; 284 } 285 return getDnObjectsReverse(); 286 } 287 288 292 protected static String [] getDnObjectsReverse() { 293 if (dNObjectsReverse == null) { 295 dNObjectsReverse = (String [])dNObjectsForward.clone(); 297 ArrayUtils.reverse(dNObjectsReverse); 298 } 299 return dNObjectsReverse; 300 } 301 302 private static void load() { 303 loadOrdering(); 304 loadMappings(); 305 } 306 314 private static void loadMappings() { 315 String line; 317 318 BufferedReader in = null; 319 InputStreamReader inf = null; 320 try 321 { 322 InputStream is = obj.getClass().getResourceAsStream("/profilemappings.properties"); 323 if (is != null) { 325 inf = new InputStreamReader (is); 326 in = new BufferedReader (inf); 328 if (!in.ready()) 329 throw new IOException (); 330 String [] splits = null; 331 int lines = 0; 332 while ((line = in.readLine()) != null) { 333 if (!line.startsWith("#")) { splits = StringUtils.split(line, ';'); 335 if ( (splits != null) && (splits.length > 5) ) { 336 String type = splits[0]; 337 String dnname = splits[1]; 338 Integer dnid = new Integer (splits[2]); 339 String profilename = splits[3]; 340 Integer profileid = new Integer (splits[4]); 341 String errstr = splits[5]; 342 String langstr = splits[6]; 343 dnNameIdMap.put(dnname, dnid); 345 profileNameIdMap.put(profilename, profileid); 346 dnIdToProfileNameMap.put(dnid, profilename); 347 dnIdToProfileIdMap.put(dnid, profileid); 348 dnIdErrorMap.put(dnid, errstr); 349 profileIdToDnIdMap.put(profileid, dnid); 350 dnErrorTextMap.put(dnid, errstr); 351 profileNameLanguageMap.put(profilename, langstr); 352 profileIdLanguageMap.put(profileid, langstr); 353 if (type.equals("DN")) { 354 dnProfileFields.add(profilename); 355 dnLanguageTexts.add(langstr); 356 dnDnIds.add(dnid); 357 dnExtractorFields.add(dnname+"="); 358 dnIdToExtractorFieldMap.put(dnid, dnname+"="); 359 } 360 if (type.equals("ALTNAME")) { 361 altNameFields.add(dnname); 362 altNameLanguageTexts.add(langstr); 363 altNameDnIds.add(dnid); 364 altNameExtractorFields.add(dnname+"="); 365 altNameIdToExtractorFieldMap.put(dnid, dnname+"="); 366 } 367 if (type.equals("DIRATTR")) { 368 dirAttrFields.add(dnname); 369 dirAttrLanguageTexts.add(langstr); 370 dirAttrDnIds.add(dnid); 371 dirAttrExtractorFields.add(dnname+"="); 372 dirAttrIdToExtractorFieldMap.put(dnid, dnname+"="); 373 } 374 lines++; 375 } 376 } 377 } 378 in.close(); 379 log.debug("Read profile maps with "+lines+" lines."); 380 } else { 381 throw new IOException ("Input stream for /profilemappings.properties is null"); 382 } 383 } 384 catch (IOException e) { 385 log.error("Can not load profile mappings: ", e); 386 } finally { 387 try { 388 if (inf != null) inf.close(); 389 if (in != null) in.close(); 390 } catch (IOException e) {} 391 } 392 393 } 394 399 private static void loadOrdering() { 400 String line; 402 LinkedHashMap map = new LinkedHashMap (); 403 BufferedReader in = null; 404 InputStreamReader inf = null; 405 try 406 { 407 InputStream is = obj.getClass().getResourceAsStream("/dncomponents.properties"); 408 if (is != null) { 410 inf = new InputStreamReader (is); 411 in = new BufferedReader (inf); 413 if (!in.ready()) 414 throw new IOException (); 415 String [] splits = null; 416 while ((line = in.readLine()) != null) { 417 if (!line.startsWith("#")) { splits = StringUtils.split(line, '='); 419 if ( (splits != null) && (splits.length > 1) ) { 420 String name = splits[0]; 421 DERObjectIdentifier oid = new DERObjectIdentifier(splits[1]); 422 map.put(name, oid); 423 } 424 } 425 } 426 in.close(); 427 log.info("Using DN components from properties file"); 429 oids.clear(); 430 oids.putAll(map); 431 Set keys = map.keySet(); 432 dNObjectsForward = (String [])keys.toArray(new String [0]); 434 } else { 435 log.debug("Using default values for DN components"); 436 } 437 } 438 catch (IOException e) { 439 log.debug("Using default values for DN components"); 440 } finally { 441 try { 442 if (inf != null) inf.close(); 443 if (in != null) in.close(); 444 } catch (IOException e) {} 445 } 446 447 } 448 449 } 450 | Popular Tags |