| 1 2 package com.ca.commons.security.asn1; 3 4 import java.util.StringTokenizer ; 5 import java.util.Vector ; 6 7 15 public class Name 16 { 17 private String country = null; 18 private String state = null; 19 private String locality = null; 20 private String org = null; 21 private Vector orgUnit = new Vector (); 23 private String commonName = null; 24 private String emailAdd = null; 25 private String uniqueID = null; 26 private String addressIP = null; 27 28 29 private ASN1Object name = null; 30 31 34 public Name() 35 {} 36 37 41 public Name(String input) 42 { 43 StringTokenizer tok = new StringTokenizer (input, ","); 44 while (tok.hasMoreTokens()) 45 { 46 String nextToken = tok.nextToken().trim(); 47 StringTokenizer fieldTok = new StringTokenizer (nextToken, "="); 48 if (fieldTok.countTokens() == 2) 49 { 50 String name = fieldTok.nextToken().trim(); 51 String value = fieldTok.nextToken().trim(); 52 53 if (name.equalsIgnoreCase("C")) 54 { 55 if (!value.equalsIgnoreCase("null")) 56 country = value; 57 } 58 else if (name.equalsIgnoreCase("S")) 59 { 60 if (!value.equalsIgnoreCase("null")) 61 state = value; 62 } 63 else if (name.equalsIgnoreCase("L")) 64 { 65 if (!value.equalsIgnoreCase("null")) 66 locality = value; 67 } 68 else if (name.equalsIgnoreCase("O")) 69 { 70 if (!value.equalsIgnoreCase("null")) 71 org = value; 72 } 73 else if (name.equalsIgnoreCase("OU")) 74 { 75 if (!value.equalsIgnoreCase("null")) 76 orgUnit.add(value); 77 } 78 else if (name.equalsIgnoreCase("CN")) 79 { 80 if (!value.equalsIgnoreCase("null")) 81 commonName = value; 82 } 83 else if (name.equalsIgnoreCase("E")) 84 { 85 if (!value.equalsIgnoreCase("null")) 86 emailAdd = value; 87 } 88 else if (name.equalsIgnoreCase("EM")) 89 { 90 if (!value.equalsIgnoreCase("null")) 91 emailAdd = value; 92 } 93 else if (name.equalsIgnoreCase("Email")) 94 { 95 if (!value.equalsIgnoreCase("null")) 96 emailAdd = value; 97 } 98 else if (name.equalsIgnoreCase("EmailAddress")) 99 { 100 if (!value.equalsIgnoreCase("null")) 101 emailAdd = value; 102 } 103 } 104 } 105 106 if (!valid()) 107 { 108 throw new IllegalArgumentException ("invalid parameters " + 109 "for Name object."); 110 } 111 } 112 113 123 public Name(String c, String s, String l, String o, String ou, 124 String cn, String em) 125 { 126 country = c; 127 state = s; 128 locality = l; 129 org = o; 130 orgUnit.add(ou); 131 commonName = cn; 132 emailAdd = em; 133 134 if (!valid()) 135 { 136 throw new IllegalArgumentException ("invalid parameters " + 137 "for Name object."); 138 } 139 } 140 141 144 public Name(ASN1Object o) 145 { 146 name = o; 147 init(); 148 if (name == null) 149 { 150 throw new IllegalArgumentException ("cannot construct a " + 151 "Name from input ASN1Object"); 152 } 153 } 154 155 158 public Name(byte [] input) 159 { 160 name = ASN1Object.fromBytes(input); 161 init(); 162 if (name == null) 163 { 164 throw new IllegalArgumentException ("cannot construct a " + 165 "Name from input byte array"); 166 } 167 } 168 169 173 private void init() 174 { 175 ASN1Object o = name; 176 ASN1Object rdn, ava; 177 String type, value; 178 179 if (o == null || !o.isASN1Type(ASN1Type.SEQUENCE)) 180 { 181 name = null; 182 return; 183 } 184 185 for (int i = 0; i < o.size(); i++) 186 { 187 rdn = o.getComponent(i); 188 if (!rdn.isASN1Type(ASN1Type.SET)) 189 { 190 name = null; 191 return; 192 } 193 194 195 for (int j = 0; j < rdn.size(); j++) 196 { 197 ava = rdn.getComponent(j); 198 type = (String ) (ava.getComponent(0).getValue()); 199 value = (String ) (ava.getComponent(1).getValue()); 200 if (type.equals(ASN1OID.commonName)) 201 { 202 commonName = value; 203 } 204 else if (type.equals(ASN1OID.country)) 205 { 206 country = value; 207 } 208 else if (type.equals(ASN1OID.stateOrProvince)) 209 { 210 state = value; 211 } 212 else if (type.equals(ASN1OID.locality)) 213 { 214 locality = value; 215 } 216 else if (type.equals(ASN1OID.organization)) 217 { 218 org = value; 219 } 220 else if (type.equals(ASN1OID.organizationalUnit)) 221 { 222 orgUnit.add(value); 223 } 224 else if (type.equals(ASN1OID.emailAddress)) 225 { 226 emailAdd = value; 227 } 228 else if (type.equals(ASN1OID.unstructuredName)) 229 { 230 uniqueID = value; 231 } 232 else if (type.equals(ASN1OID.unstructuredAddress)) 233 { 234 addressIP = value; 235 } 236 else 237 { } 241 } 242 } 243 244 245 if (!valid()) 246 { 247 name = null; 248 } 249 } 250 251 255 private void createName() 256 { 257 if (!valid()) 258 { 259 name = null; 260 return; 261 } 262 263 try 264 { 265 name = ASN1Object.create(ASN1Type.SEQUENCE); 266 if (country != null) 267 { 268 name.addComponent(createRDN(ASN1OID.country, country)); 269 } 270 if (state != null) 271 { 272 name.addComponent(createRDN(ASN1OID.stateOrProvince, state)); 273 } 274 if (locality != null) 275 { 276 name.addComponent(createRDN(ASN1OID.locality, locality)); 277 } 278 if (org != null) 279 { 280 name.addComponent(createRDN(ASN1OID.organization, org)); 281 } 282 for (int i = 0; i < orgUnit.size(); i++) 283 { 284 String myou = (String ) orgUnit.elementAt(i); 285 if (myou != null && myou.trim().length() > 0) 286 name.addComponent(createRDN( 287 ASN1OID.organizationalUnit, myou)); 288 } 289 if (emailAdd != null) 290 { 291 name.addComponent(createEmail(ASN1OID.emailAddress, emailAdd)); 292 } 293 if (commonName != null) 294 { 295 name.addComponent(createRDN(ASN1OID.commonName, commonName)); 296 } 297 if (uniqueID != null) 298 { 299 name.addComponent(createEmail( 300 ASN1OID.unstructuredName, uniqueID)); 301 } 302 if (addressIP != null) 303 { 304 name.addComponent(createRDN( 305 ASN1OID.unstructuredAddress, addressIP)); 306 } 307 } 308 catch(ASN1Exception asn1e) 309 { 310 name = null; 311 return; 312 } 313 name.initByteArray(); 314 } 315 316 320 private ASN1Object createRDN(String t, String v) 321 throws ASN1Exception 322 { 323 ASN1Object rdn, ava, type, value; 324 325 rdn = ASN1Object.create(ASN1Type.SET); 326 ava = ASN1Object.create(ASN1Type.SEQUENCE); 327 type = ASN1Object.create(ASN1Type.OBJECT_ID, t); 328 value = ASN1Object.create(ASN1Type.PrintableString, v); 329 ava.addComponent(type); 330 ava.addComponent(value); 331 rdn.addComponent(ava); 332 return rdn; 333 } 334 335 339 private ASN1Object createEmail(String t, String v) 340 throws ASN1Exception 341 { 342 ASN1Object rdn, ava, type, value; 343 344 rdn = ASN1Object.create(ASN1Type.SET); 345 ava = ASN1Object.create(ASN1Type.SEQUENCE); 346 type = ASN1Object.create(ASN1Type.OBJECT_ID, t); 347 value = ASN1Object.create(ASN1Type.IA5String, v); 348 ava.addComponent(type); 349 ava.addComponent(value); 350 rdn.addComponent(ava); 351 return rdn; 352 } 353 354 357 public ASN1Object toASN1Object() 358 { 359 if (name == null) 360 { 361 createName(); 362 } 363 return name; 364 } 365 366 369 public byte [] toByteArrayDER() 370 { 371 if (name == null) 372 { 373 createName(); 374 } 375 if (name == null) 376 { 377 return null; 378 } 379 return name.toDERBytes(); 380 } 381 382 383 384 387 public String country() 388 { 389 return country; 390 } 391 392 395 public String stateOrProvince() 396 { 397 return state; 398 } 399 400 403 public String locality() 404 { 405 return locality; 406 } 407 408 411 public String organization() 412 { 413 return org; 414 } 415 416 419 public String organizationalUnit() 420 { 421 if (orgUnit.size() >= 1) 422 return (String ) orgUnit.elementAt(0); 423 else 424 return null; 425 } 426 427 430 public Vector organizationalUnits() 431 { 432 return orgUnit; 433 } 434 435 438 public String commonName() 439 { 440 if (commonName != null && commonName.trim().length() != 0) 441 return commonName; 442 else 443 return toString(); 444 } 445 446 449 public String emailAddress() 450 { 451 return emailAdd; 452 } 453 454 457 public String uniqueID() 458 { 459 return uniqueID; 460 } 461 462 465 public String addressIP() 466 { 467 return addressIP; 468 } 469 470 473 public void set(Name o) 474 { 475 this.country = o.country; 476 this.state = o.state; 477 this.locality = o.locality; 478 this.org = o.org; 479 this.orgUnit = o.orgUnit; 480 this.commonName = o.commonName; 481 this.emailAdd = o.emailAdd; 482 this.uniqueID = o.uniqueID; 483 this.addressIP = o.addressIP; 484 this.name = o.name; 485 } 486 487 490 public boolean equals(Object n) 491 { 492 if (!(n instanceof Name)) 493 { 494 return false; 495 } 496 Name o = (Name) n; 497 return this.toASN1Object().equals(o.toASN1Object()); 498 } 499 500 504 public boolean valid() 505 { 506 if ((country == null || country.length() == 0) 507 && (state == null || state.length() == 0) 508 && (locality == null || locality.length() == 0) 509 && (org == null || org.length() == 0) 510 && (orgUnit == null || orgUnit.size() == 0) 511 && (commonName == null || commonName.length() == 0) 512 && (emailAdd == null || emailAdd.length() == 0)) 513 { 514 return false; 515 } 516 return true; 517 } 518 519 522 public String toString() 523 { 524 String result = null; 525 526 if (country != null && !country.equals("null") && country.trim().length() > 0) 527 result = "C = " + country; 528 if (state != null && !state.equals("null") && state.trim().length() > 0) 529 result += ", ST = " + state; 530 if (locality != null && !locality.equals("null") && locality.trim().length() > 0) 531 result += ", L = " + locality; 532 if (org != null && !org.equals("null") && org.trim().length() > 0) 533 result += ", O = " + org; 534 for (int i = 0; i < orgUnit.size(); i++) 535 { 536 String anOrgUnit = (String ) orgUnit.elementAt(i); 537 if (anOrgUnit != null && !anOrgUnit.equals("null") && anOrgUnit.trim().length() > 0) 538 result += ", OU = " + anOrgUnit; 539 } 540 if (commonName != null && !commonName.equals("null") && commonName.trim().length() > 0) 541 result += ", CN = " + commonName; 542 if (emailAdd != null && !emailAdd.equals("null") && emailAdd.trim().length() > 0) 543 result += ", EM = " + emailAdd; 544 if (uniqueID != null && !uniqueID.equals("null") && uniqueID.trim().length() > 0) 545 result += ", unstructuredName = " + uniqueID; 546 if (addressIP != null && !addressIP.equals("null") && addressIP.trim().length() > 0) 547 result += ", unstructuredAddress = " + addressIP; 548 549 return result; 550 } 551 } 552 | Popular Tags |