KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package com.ca.commons.security.asn1;
3
4 import java.util.Hashtable JavaDoc;
5
6 public class GeneralName
7 {
8
9     private int generalNameID = -1;
10     private String JavaDoc rfc822Name = null; // GeneralName[1]
11
private String JavaDoc dNSName = null; // GeneralName[2]
12
private Name directoryName = null; // GeneralName[4]
13
private String JavaDoc uRID = null; // GeneralName[6]
14
private String JavaDoc ipAddress = null; // GeneralName[7]
15
private String JavaDoc registeredOID = null; // GeneralName[8]
16

17     private static Hashtable JavaDoc idName = new Hashtable JavaDoc();
18     
19     static
20     {
21         idName.put(new Integer JavaDoc(1), "rfc822Name");
22         idName.put(new Integer JavaDoc(2), "dNSName");
23         idName.put(new Integer JavaDoc(4), "directoryName");
24         idName.put(new Integer JavaDoc(6), "uRID");
25         idName.put(new Integer JavaDoc(7), "ipAddress");
26         idName.put(new Integer JavaDoc(8), "registeredOID");
27     }
28     
29     public static String JavaDoc lookUpName(int id)
30     {
31         return (String JavaDoc) idName.get(new Integer JavaDoc(id));
32     }
33     
34     public GeneralName( Object JavaDoc obj, int type) throws ASN1Exception
35     {
36     
37         if ( (type < 0) || ( type > 8))
38             throw new ASN1Exception("Wrong type");
39             
40         if ( (type == 1) && ( obj instanceof String JavaDoc))
41         {
42             rfc822Name = (String JavaDoc) obj;
43             generalNameID = 1;
44         }
45         else if ( (type ==2) && ( obj instanceof String JavaDoc))
46         {
47             dNSName = (String JavaDoc) obj;
48             generalNameID = 2;
49         }
50         else if ( (type ==4) && ( obj instanceof Name))
51         {
52             directoryName = ( Name) obj;
53             generalNameID = 4;
54         }
55         else if ( (type == 6) && ( obj instanceof String JavaDoc))
56         {
57             uRID = (String JavaDoc) obj;
58             generalNameID = 6;
59         }
60         else if ( (type == 7) && ( obj instanceof String JavaDoc))
61         {
62             ipAddress = (String JavaDoc) obj;
63             generalNameID = 7;
64         }
65         else if ( (type == 8) && ( obj instanceof String JavaDoc))
66         {
67             registeredOID = (String JavaDoc) obj;
68             generalNameID = 8;
69         }
70         else
71         {
72             throw new ASN1Exception("Wrong Type");
73         }
74     }
75     
76     /**
77      * Creates an GeneralName from DER bytes.
78      */

79     public GeneralName(byte [] data)
80     {
81         ASN1Object o = ASN1Object.fromBytes(data);
82         // System.out.println(o.toString());
83
init(o);
84     }
85     
86     /**
87      * Creates an GeneralName from an ASN1object.
88      */

89     public GeneralName(ASN1Object o)
90     {
91         init(o);
92     }
93     
94     private void init(ASN1Object ss)
95     {
96         if (!ss.isASN1Type(ASN1Type.ContextSpecific))
97         {
98             // && ss.implicit()) {
99
//throw Exception();
100
return;
101         }
102         
103         ASN1Object mm = (ASN1Object) ss.getValue();
104         int tag = ss.getTag();
105         
106         if (tag >= 0 && tag <= 8)
107             generalNameID = tag;
108         else
109             generalNameID = tag - 32; // For constructed Types
110

111         if (tag == 0)
112         {
113             System.out.println("not Implemented");
114             
115         }
116         else if (tag == 0x01)
117         {
118         
119             ASN1Object tmp1 = (ASN1Object)ss.getValue();
120             if (tmp1.isASN1Type(ASN1Type.OCTET_STRING) == false)
121                 return;
122                 
123             byte[] bytearray= (byte[])tmp1.getValue();
124             rfc822Name = new String JavaDoc(bytearray);
125             
126         }
127         else if (tag == 0x2)
128         {
129         
130             ASN1Object tmp1 = (ASN1Object)ss.getValue();
131             if (tmp1.isASN1Type(ASN1Type.OCTET_STRING) == false)
132                 return;
133                 
134             byte[] bytearray= (byte[])tmp1.getValue();
135             dNSName = new String JavaDoc(bytearray);
136             
137         }
138         else if (tag == 0x3)
139         {
140             System.out.println("not Implemented");
141             
142         }
143         else if (tag == ( 0x04 + 0x20))
144         { // Constructed Type!!
145
directoryName = new Name( (ASN1Object) ss.getValue());
146             
147         }
148         else if (tag == 0x5)
149         {
150             System.out.println("not Implemented");
151             
152         }
153         else if (tag == 0x6)
154         {
155         
156             ASN1Object tmp1 = (ASN1Object)ss.getValue();
157             if (tmp1.isASN1Type(ASN1Type.OCTET_STRING) == false)
158                 return;
159                 
160             byte[] bytearray= (byte[])tmp1.getValue();
161             uRID = (String JavaDoc) new String JavaDoc(bytearray);
162             
163         }
164         else if (tag == 0x7)
165         {
166         
167             ASN1Object tmp1 = (ASN1Object)ss.getValue();
168             if (tmp1.isASN1Type(ASN1Type.OCTET_STRING) == false)
169                 return;
170                 
171             byte[] bytearray= (byte[])tmp1.getValue();
172             ipAddress = new String JavaDoc(bytearray);
173             
174             
175         }
176         else if (tag == 0x08)
177         {
178         
179             ASN1Object tmp1 = (ASN1Object)ss.getValue();
180             //System.out.println(" OBJECT TYPE:" + tmp1.getASN1Type());
181

182             if (tmp1.isASN1Type(ASN1Type.OCTET_STRING) == false)
183                 return;
184                 
185             try
186             {
187                 byte[] data = (byte[] ) tmp1.getValue();
188                 ASN1Object obj = ASN1Object.create(ASN1Type.OBJECT_ID);
189                 decodeOBJECTID( obj, data, 0, data.length);
190                 registeredOID = (String JavaDoc) obj.getValue();
191             }
192             catch ( Exception JavaDoc e)
193             {
194                 System.out.println(e);
195                 registeredOID = null;
196             }
197             
198         }
199         else
200         {
201             //throw ASN1Exception("Wrong Type");
202
return;
203         }
204     }
205     
206     public Object JavaDoc getValue(int type)
207     {
208     
209         if ( type < 0 || type > 8)
210             return null;
211             
212         if ( type ==1)
213             return rfc822Name;
214         else if ( type ==2)
215             return dNSName;
216         else if ( type ==4)
217             return directoryName;
218         else if ( type ==6)
219             return uRID;
220         else if ( type ==7)
221             return ipAddress;
222         else if ( type ==8)
223             return registeredOID;
224             
225         return null;
226     }
227     
228     public int getType()
229     {
230     
231         return generalNameID;
232     }
233     
234     
235     /**
236      * Converts the GeneralName to an ASN1Object.
237      */

238     public ASN1Object toASN1Object()
239     {
240         // try {
241

242         if (generalNameID < 0)
243         {
244             return null;
245         }
246         
247         ASN1Object mm = null;
248         
249         if (generalNameID == 1 && rfc822Name != null)
250         {
251         
252             try
253             {
254                 mm = new Context(0x01, true,
255                                  ASN1Object.create( ASN1Type.IA5String, rfc822Name));
256                 mm.initByteArray();
257             }
258             catch ( Exception JavaDoc e)
259             {
260                 System.out.println(e);
261             }
262             
263             return mm;
264         }
265         else if ( generalNameID == 2 && dNSName != null)
266         {
267         
268             try
269             {
270                 mm = new Context(0x02, true,
271                                  ASN1Object.create( ASN1Type.IA5String, dNSName));
272                 mm.initByteArray();
273             }
274             catch ( Exception JavaDoc e)
275             {
276                 System.out.println(e);
277             }
278             return mm;
279         }
280         else if ( generalNameID == 4 && directoryName != null)
281         {
282         
283             try
284             {
285                 mm = new Context(0x04, false,
286                                  directoryName.toASN1Object());
287                 mm.initByteArray();
288             }
289             catch ( Exception JavaDoc e)
290             {
291                 System.out.println(e);
292             }
293             return mm;
294         }
295         else if ( generalNameID == 6 && uRID != null)
296         {
297         
298             try
299             {
300                 mm = new Context(0x06, true,
301                                  ASN1Object.create( ASN1Type.IA5String, uRID));
302                 mm.initByteArray();
303             }
304             catch ( Exception JavaDoc e)
305             {
306                 System.out.println(e);
307             }
308             return mm;
309         }
310         else if ( generalNameID == 7 && ipAddress != null)
311         {
312         
313             try
314             {
315                 mm = new Context(0x07, true,
316                                  ASN1Object.create( ASN1Type.OCTET_STRING,
317                                                     ipAddress.getBytes()));
318                 mm.initByteArray();
319             }
320             catch ( Exception JavaDoc e)
321             {
322                 System.out.println(e);
323             }
324             return mm;
325         }
326         else if ( generalNameID == 8 && registeredOID != null)
327         {
328         
329             try
330             {
331                 mm = new Context(0x08, true,
332                                  ASN1Object.create( ASN1Type.OBJECT_ID,
333                                                     registeredOID));
334                 mm.initByteArray();
335             }
336             catch ( Exception JavaDoc e)
337             {
338                 System.out.println(e);
339             }
340             return mm;
341         }
342         
343         return null;
344     }
345     
346     private static void decodeOBJECTID(ASN1Object o, byte [] stream, int offset,
347                                        int length) throws ASN1Exception
348     {
349         if (length < 1)
350         { // at least two components
351
throw new ASN1Exception("Wrong data (OBJECT ID) length");
352         }
353         int end = offset + length;
354         int v = stream[offset++];
355         String JavaDoc content = Integer.toString(v/40) + " ";
356         content += Integer.toString(v%40) + " ";
357         
358         while (offset < end)
359         {
360             long l = 0;
361             while ((stream[offset] & 0x80) != 0)
362             {
363                 l |= 0x7f & stream[offset++];
364                 l <<= 7;
365             }
366             l |= 0x7f & stream[offset++];
367             content += Long.toString(l) + " ";
368         }
369         if (offset != end)
370         {
371             throw new ASN1Exception("Wrong data (OBJECT ID) length");
372         }
373         o.setValue(content.trim());
374     }
375     
376     
377 }
378
Popular Tags