1 13 14 package org.ejbca.util.dn; 15 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 20 import org.apache.log4j.Logger; 21 import org.ejbca.util.CertTools; 22 import org.ietf.ldap.LDAPDN; 23 24 25 32 public class DNFieldExtractor implements java.io.Serializable { 33 private static final Logger log = Logger.getLogger(DNFieldExtractor.class); 34 public static final int TYPE_SUBJECTDN = 0; 36 public static final int TYPE_SUBJECTALTNAME = 1; 37 public static final int TYPE_SUBJECTDIRATTR = 2; 38 39 public static final int E = 0; 41 public static final int UID = 1; 42 public static final int CN = 2; 43 public static final int SN = 3; 44 public static final int GIVENNAME = 4; 45 public static final int INITIALS = 5; 46 public static final int SURNAME = 6; 47 public static final int T = 7; 48 public static final int OU = 8; 49 public static final int O = 9; 50 public static final int L = 10; 51 public static final int ST = 11; 52 public static final int DC = 12; 53 public static final int C = 13; 54 public static final int UNSTRUCTUREDADDRESS = 14; 55 public static final int UNSTRUCTUREDNAME = 15; 56 57 public static final int OTHERNAME = 16; 59 public static final int RFC822NAME = 17; 60 public static final int DNSNAME = 18; 61 public static final int IPADDRESS = 19; 62 public static final int X400ADDRESS = 20; 63 public static final int DIRECTORYNAME = 21; 64 public static final int EDIPARTNAME = 22; 65 public static final int URI = 23; 66 public static final int REGISTEREDID = 24; 67 public static final int UPN = 25; 68 public static final int GUID = 26; 69 70 public static final int DATEOFBIRTH = 27; 72 public static final int PLACEOFBIRTH = 28; 73 public static final int GENDER = 29; 74 public static final int COUNTRYOFCITIZENSHIP = 30; 75 public static final int COUNTRYOFRESIDENCE = 31; 76 77 83 public DNFieldExtractor(String dn, int type) { 84 dnfields = new HashMap (); 85 setDN(dn, type); 86 } 87 88 90 public static Integer [] getUseFields(int type) { 91 if (type == DNFieldExtractor.TYPE_SUBJECTDN) { 92 return (Integer [])DnComponents.getDnDnIds().toArray(new Integer [0]); 93 } else if (type == DNFieldExtractor.TYPE_SUBJECTALTNAME) { 94 return (Integer [])DnComponents.getAltNameDnIds().toArray(new Integer [0]); 95 } else if (type == DNFieldExtractor.TYPE_SUBJECTDIRATTR) { 96 return (Integer [])DnComponents.getDirAttrDnIds().toArray(new Integer [0]); 97 } else { 98 return new Integer [0]; 99 } 100 } 101 102 public static String getFieldComponent(int field, int type) { 103 if (type == DNFieldExtractor.TYPE_SUBJECTDN) { 104 String ret = DnComponents.getDnExtractorFieldFromDnId(field); 105 return ret; 106 } else if (type == DNFieldExtractor.TYPE_SUBJECTALTNAME) { 107 String ret = DnComponents.getAltNameExtractorFieldFromDnId(field); 108 return ret; 109 } else { 110 String ret = DnComponents.getDirAttrExtractorFieldFromDnId(field); 111 return ret; 112 } 113 } 114 115 121 public void setDN(String dn, int type) { 122 this.type = type; 123 ArrayList ids; 124 if (type == TYPE_SUBJECTDN) { 125 ids = DnComponents.getDnDnIds(); 126 } else if (type == TYPE_SUBJECTALTNAME){ 127 ids = DnComponents.getAltNameDnIds(); 128 } else if (type == TYPE_SUBJECTDIRATTR){ 129 ids = DnComponents.getDirAttrDnIds(); 130 } else { 131 ids = new ArrayList (); 132 } 133 fieldnumbers = new HashMap (); 134 Iterator it = ids.iterator(); 135 while (it.hasNext()) { 136 Integer id = (Integer )it.next(); 137 fieldnumbers.put(id, new Integer (0)); 138 } 139 140 if ((dn != null) && !dn.equalsIgnoreCase("null")) { 141 dnfields = new HashMap (); 142 143 try { 144 String [] dnexploded = LDAPDN.explodeDN(dn, false); 145 146 for (int i = 0; i < dnexploded.length; i++) { 147 boolean exists = false; 148 Iterator iter = ids.iterator(); 149 while (iter.hasNext()) { 150 Integer id = (Integer )iter.next(); 151 Integer number = (Integer )fieldnumbers.get(id); 152 String field; 153 if (type == TYPE_SUBJECTDN) { 154 field = DnComponents.getDnExtractorFieldFromDnId(id.intValue()); 155 } else if (type == TYPE_SUBJECTALTNAME){ 156 field = DnComponents.getAltNameExtractorFieldFromDnId(id.intValue()); 157 } else { 158 field = DnComponents.getDirAttrExtractorFieldFromDnId(id.intValue()); 159 } 160 String dnex = dnexploded[i].toUpperCase(); 161 if (id.intValue() == DNFieldExtractor.URI) { 162 if (dnex.indexOf(CertTools.URI.toUpperCase()+"=") > -1) { 164 field = CertTools.URI.toUpperCase()+"="; 165 } 166 if (dnex.indexOf(CertTools.URI1.toUpperCase()+"=") > -1) { 167 field = CertTools.URI1.toUpperCase()+"="; 168 } 169 } 170 if (dnex.startsWith(field)) { 171 exists = true; 172 String rdn = LDAPDN.unescapeRDN(dnexploded[i]); 173 if (rdn.toUpperCase().startsWith(field)) { 175 rdn = rdn.substring(field.length(),rdn.length()); 176 } 177 178 if (type == TYPE_SUBJECTDN) { 179 dnfields.put(new Integer ((id.intValue() * BOUNDRARY) + number.intValue()), rdn); 180 } else if (type == TYPE_SUBJECTALTNAME) { 181 dnfields.put(new Integer ((id.intValue() * BOUNDRARY) + 182 number.intValue()), rdn); 183 } else if (type == TYPE_SUBJECTDIRATTR) { 184 dnfields.put(new Integer ((id.intValue() * BOUNDRARY) + 185 number.intValue()), rdn); 186 } 187 number = new Integer (number.intValue()+1); 188 fieldnumbers.put(id, number); 189 } 190 } 191 if (!exists) { 192 existsother = true; 193 } 194 } 195 } catch (Exception e) { 196 log.error("setDN: ", e); 197 illegal = true; 198 if (type == TYPE_SUBJECTDN) { 199 dnfields.put(new Integer ((CN * BOUNDRARY)), "Illegal DN : " + dn); 200 } else if (type == TYPE_SUBJECTALTNAME){ 201 dnfields.put(new Integer ((RFC822NAME * BOUNDRARY)), 202 "Illegal Subjectaltname : " + dn); 203 } else if (type == TYPE_SUBJECTDIRATTR){ 204 dnfields.put(new Integer ((PLACEOFBIRTH * BOUNDRARY)), 205 "Illegal Subjectdirectory attribute : " + dn); 206 } 207 } 208 } 209 } 210 211 221 public String getField(int field, int number) { 222 String returnval; 223 returnval = (String ) dnfields.get(new Integer ((field * BOUNDRARY) + number)); 224 225 if (returnval == null) { 226 returnval = ""; 227 } 228 229 return returnval; 230 } 231 232 237 public String getFieldString(int field){ 238 String retval = ""; 239 String fieldname = DnComponents.getDnExtractorFieldFromDnId(field); 240 if(type != TYPE_SUBJECTDN){ 241 fieldname = DnComponents.getAltNameExtractorFieldFromDnId(field); 242 } 243 int num = getNumberOfFields(field); 244 for(int i=0;i<num;i++){ 245 if(retval.length() == 0) 246 retval += fieldname + getField(field,i); 247 else 248 retval += "," + fieldname + getField(field,i); 249 } 250 return retval; 251 } 252 253 254 259 public boolean existsOther() { 260 return existsother; 261 } 262 263 270 public int getNumberOfFields(int field) { 271 Integer ret = (Integer )fieldnumbers.get(new Integer (field)); 272 if (ret == null) { 273 log.error("Not finding fieldnumber value for "+field); 274 } 275 return ret.intValue(); 276 } 277 278 284 public HashMap getNumberOfFields() { 285 return fieldnumbers; 286 } 287 288 public boolean isIllegal(){ 289 return illegal; 290 } 291 292 private static final int BOUNDRARY = 100; 293 private HashMap fieldnumbers; 295 private HashMap dnfields; 296 private boolean existsother = false; 297 private boolean illegal = false; 298 private int type; 299 } 300 | Popular Tags |