KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > asn1 > Name


1
2 package com.ca.commons.security.asn1;
3
4 import java.util.StringTokenizer JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 /**
8  * The definition of a X.500 directory name.
9  * For more information, please refer to :
10  * CCITT. Recommendation X.500: The Directory - Overview of Concepts,
11  * Models and Services. 1988.
12  *
13  * @author who
14  */

15 public class Name
16 {
17     private String JavaDoc country = null;
18     private String JavaDoc state = null;
19     private String JavaDoc locality = null;
20     private String JavaDoc org = null;
21     // support multiple OU
22
private Vector JavaDoc orgUnit = new Vector JavaDoc();
23     private String JavaDoc commonName = null;
24     private String JavaDoc emailAdd = null;
25     private String JavaDoc uniqueID = null;
26     private String JavaDoc addressIP = null;
27     
28     /* ASN1Object representation of the Name */
29     private ASN1Object name = null;
30     
31     /**
32      * Constructs an empty Name.
33      */

34     public Name()
35     {}
36     
37     /**
38      * Constructs a name given a string with the following format:
39      * "C=country, S=state, L=locality, ..."
40      */

41     public Name(String JavaDoc input)
42     {
43         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(input, ",");
44         while (tok.hasMoreTokens())
45         {
46             String JavaDoc nextToken = tok.nextToken().trim();
47             StringTokenizer JavaDoc fieldTok = new StringTokenizer JavaDoc(nextToken, "=");
48             if (fieldTok.countTokens() == 2)
49             {
50                 String JavaDoc name = fieldTok.nextToken().trim();
51                 String JavaDoc 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 JavaDoc("invalid parameters " +
109                                                "for Name object.");
110         }
111     }
112     
113     /**
114      * Constructs a Name with the given attributes.
115      * @param c country code
116      * @param s state or province
117      * @param l locality
118      * @param o organization
119      * @param ou organizational unit
120      * @param cn common name
121      * @param em email address
122      */

123     public Name(String JavaDoc c, String JavaDoc s, String JavaDoc l, String JavaDoc o, String JavaDoc ou,
124                 String JavaDoc cn, String JavaDoc 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 JavaDoc("invalid parameters " +
137                                                "for Name object.");
138         }
139     }
140     
141     /**
142      * Constructs a Name from an ASN1Object.
143      */

144     public Name(ASN1Object o)
145     {
146         name = o;
147         init();
148         if (name == null)
149         {
150             throw new IllegalArgumentException JavaDoc("cannot construct a " +
151                                                "Name from input ASN1Object");
152         }
153     }
154     
155     /**
156      * Constructs a Name from a byte array.
157      */

158     public Name(byte [] input)
159     {
160         name = ASN1Object.fromBytes(input);
161         init();
162         if (name == null)
163         {
164             throw new IllegalArgumentException JavaDoc("cannot construct a " +
165                                                "Name from input byte array");
166         }
167     }
168     
169     /**
170      * Initializes the Name from the ASN1Object. If error, the ASN1Object
171      * is reset to null.
172      */

173     private void init()
174     {
175         ASN1Object o = name;
176         ASN1Object rdn, ava;
177         String JavaDoc 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             /* accepted attributes */
195             for (int j = 0; j < rdn.size(); j++)
196             {
197                 ava = rdn.getComponent(j);
198                 type = (String JavaDoc) (ava.getComponent(0).getValue());
199                 value = (String JavaDoc) (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                 { // ignore other attribute
238
// System.out.println("Unknown attribute");
239
// System.out.println("type " + type + " value " + value);
240
}
241             }
242         }
243         
244         /* the essential attributes cannot all be null */
245         if (!valid())
246         {
247             name = null;
248         }
249     }
250     
251     /**
252      * Creates the ASN1Object with respective attibutes. If error,
253      * the name object is set to null.
254      */

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 JavaDoc myou = (String JavaDoc) 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     /**
317      * Creates a relative distinguished name while the value is an
318      * ASN.1 PrintableString.
319      */

320     private ASN1Object createRDN(String JavaDoc t, String JavaDoc 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     /**
336      * Creates a relative distinguished name while the value is an
337      * ASN.1 IA5String.
338      */

339     private ASN1Object createEmail(String JavaDoc t, String JavaDoc 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     /**
355      * Converts the Name to an ASN1Object.
356      */

357     public ASN1Object toASN1Object()
358     {
359         if (name == null)
360         {
361             createName();
362         }
363         return name;
364     }
365     
366     /**
367      * Gets the DER encoded byte array of the Name.
368      */

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     /* the following methods retrieve each attribute */
383     
384     /**
385      * Gets the country code.
386      */

387     public String JavaDoc country()
388     {
389         return country;
390     }
391     
392     /**
393      * Gets the state or province name.
394      */

395     public String JavaDoc stateOrProvince()
396     {
397         return state;
398     }
399     
400     /**
401      * Gets the locality.
402      */

403     public String JavaDoc locality()
404     {
405         return locality;
406     }
407     
408     /**
409      * Gets the organization.
410      */

411     public String JavaDoc organization()
412     {
413         return org;
414     }
415     
416     /**
417      * Gets the organizational unit.
418      */

419     public String JavaDoc organizationalUnit()
420     {
421         if (orgUnit.size() >= 1)
422             return (String JavaDoc) orgUnit.elementAt(0);
423         else
424             return null;
425     }
426     
427     /**
428      * Gets the organizational units.
429      */

430     public Vector JavaDoc organizationalUnits()
431     {
432         return orgUnit;
433     }
434     
435     /**
436      * Gets the common name.
437      */

438     public String JavaDoc commonName()
439     {
440         if (commonName != null && commonName.trim().length() != 0)
441             return commonName;
442         else
443             return toString();
444     }
445     
446     /**
447      * Gets the email address.
448      */

449     public String JavaDoc emailAddress()
450     {
451         return emailAdd;
452     }
453     
454     /**
455      * Gets the unique identifier.
456      */

457     public String JavaDoc uniqueID()
458     {
459         return uniqueID;
460     }
461     
462     /**
463      * Gets the IP address.
464      */

465     public String JavaDoc addressIP()
466     {
467         return addressIP;
468     }
469     
470     /**
471      * Sets the name to be the same as 'o'.
472      */

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     /**
488      * Tests whether this Name is the same as the given object.
489      */

490     public boolean equals(Object JavaDoc 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     /**
501      * Checks the validity of the Name. The essential attributes cannot
502      * be all nulls.
503      */

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     /**
520      * A string description of the Name.
521      */

522     public String JavaDoc toString()
523     {
524         String JavaDoc 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 JavaDoc anOrgUnit = (String JavaDoc) 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