KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > ASN1Dump


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.util.Enumeration JavaDoc;
23
24 import com.maverick.crypto.encoders.Hex;
25
26 public class ASN1Dump
27 {
28     private static String JavaDoc TAB = " ";
29
30     /**
31      * dump a DER object as a formatted string with indentation
32      *
33      * @param obj the DERObject to be dumped out.
34      */

35     public static String JavaDoc _dumpAsString(
36         String JavaDoc indent,
37         DERObject obj)
38     {
39         if (obj instanceof ASN1Sequence)
40         {
41             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
42             Enumeration JavaDoc e = ((ASN1Sequence)obj).getObjects();
43             String JavaDoc tab = indent + TAB;
44
45             buf.append(indent);
46             if (obj instanceof BERConstructedSequence)
47             {
48                 buf.append("BER ConstructedSequence");
49             }
50             else if (obj instanceof DERConstructedSequence)
51             {
52                 buf.append("DER ConstructedSequence");
53             }
54             else if (obj instanceof DERSequence)
55             {
56                 buf.append("DER Sequence");
57             }
58             else if (obj instanceof BERSequence)
59             {
60                 buf.append("BER Sequence");
61             }
62             else
63             {
64                 buf.append("Sequence");
65             }
66
67             buf.append(System.getProperty("line.separator"));
68
69             while (e.hasMoreElements())
70             {
71                 Object JavaDoc o = e.nextElement();
72
73                 if (o == null || o.equals(new DERNull()))
74                 {
75                     buf.append(tab);
76                     buf.append("NULL");
77                     buf.append(System.getProperty("line.separator"));
78                 }
79                 else if (o instanceof DERObject)
80                 {
81                     buf.append(_dumpAsString(tab, (DERObject)o));
82                 }
83                 else
84                 {
85                     buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
86                 }
87             }
88             return buf.toString();
89         }
90         else if (obj instanceof DERTaggedObject)
91         {
92             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93             String JavaDoc tab = indent + TAB;
94
95             buf.append(indent);
96             if (obj instanceof BERTaggedObject)
97             {
98                 buf.append("BER Tagged [");
99             }
100             else
101             {
102                 buf.append("Tagged [");
103             }
104
105             DERTaggedObject o = (DERTaggedObject)obj;
106
107             buf.append(Integer.toString(o.getTagNo()));
108             buf.append("]");
109
110             if (!o.isExplicit())
111             {
112                 buf.append(" IMPLICIT ");
113             }
114
115             buf.append(System.getProperty("line.separator"));
116
117             if (o.isEmpty())
118             {
119                 buf.append(tab);
120                 buf.append("EMPTY");
121                 buf.append(System.getProperty("line.separator"));
122             }
123             else
124             {
125                 buf.append(_dumpAsString(tab, o.getObject()));
126             }
127
128             return buf.toString();
129         }
130         else if (obj instanceof DERConstructedSet)
131         {
132             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
133             Enumeration JavaDoc e = ((ASN1Set)obj).getObjects();
134             String JavaDoc tab = indent + TAB;
135
136             buf.append(indent);
137             buf.append("ConstructedSet");
138             buf.append(System.getProperty("line.separator"));
139
140             while (e.hasMoreElements())
141             {
142                 Object JavaDoc o = e.nextElement();
143
144                 if (o == null)
145                 {
146                     buf.append(tab);
147                     buf.append("NULL");
148                     buf.append(System.getProperty("line.separator"));
149                 }
150                 else if (o instanceof DERObject)
151                 {
152                     buf.append(_dumpAsString(tab, (DERObject)o));
153                 }
154                 else
155                 {
156                     buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
157                 }
158             }
159             return buf.toString();
160         }
161         else if (obj instanceof BERSet)
162         {
163             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
164             Enumeration JavaDoc e = ((ASN1Set)obj).getObjects();
165             String JavaDoc tab = indent + TAB;
166
167             buf.append(indent);
168             buf.append("BER Set");
169             buf.append(System.getProperty("line.separator"));
170
171             while (e.hasMoreElements())
172             {
173                 Object JavaDoc o = e.nextElement();
174
175                 if (o == null)
176                 {
177                     buf.append(tab);
178                     buf.append("NULL");
179                     buf.append(System.getProperty("line.separator"));
180                 }
181                 else if (o instanceof DERObject)
182                 {
183                     buf.append(_dumpAsString(tab, (DERObject)o));
184                 }
185                 else
186                 {
187                     buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
188                 }
189             }
190             return buf.toString();
191         }
192         else if (obj instanceof DERSet)
193         {
194             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
195             Enumeration JavaDoc e = ((ASN1Set)obj).getObjects();
196             String JavaDoc tab = indent + TAB;
197
198             buf.append(indent);
199             buf.append("DER Set");
200             buf.append(System.getProperty("line.separator"));
201
202             while (e.hasMoreElements())
203             {
204                 Object JavaDoc o = e.nextElement();
205
206                 if (o == null)
207                 {
208                     buf.append(tab);
209                     buf.append("NULL");
210                     buf.append(System.getProperty("line.separator"));
211                 }
212                 else if (o instanceof DERObject)
213                 {
214                     buf.append(_dumpAsString(tab, (DERObject)o));
215                 }
216                 else
217                 {
218                     buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
219                 }
220             }
221             return buf.toString();
222         }
223         else if (obj instanceof DERObjectIdentifier)
224         {
225             return indent + "ObjectIdentifier(" + ((DERObjectIdentifier)obj).getId() + ")" + System.getProperty("line.separator");
226         }
227         else if (obj instanceof DERBoolean)
228         {
229             return indent + "Boolean(" + ((DERBoolean)obj).isTrue() + ")" + System.getProperty("line.separator");
230         }
231         else if (obj instanceof DERInteger)
232         {
233             return indent + "Integer(" + ((DERInteger)obj).getValue() + ")" + System.getProperty("line.separator");
234         }
235         else if (obj instanceof DEROctetString)
236         {
237             return indent + obj.toString() + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator");
238         }
239         else if (obj instanceof DERIA5String)
240         {
241             return indent + "IA5String(" + ((DERIA5String)obj).getString() + ") " + System.getProperty("line.separator");
242         }
243         else if (obj instanceof DERPrintableString)
244         {
245             return indent + "PrintableString(" + ((DERPrintableString)obj).getString() + ") " + System.getProperty("line.separator");
246         }
247         else if (obj instanceof DERVisibleString)
248         {
249             return indent + "VisibleString(" + ((DERVisibleString)obj).getString() + ") " + System.getProperty("line.separator");
250         }
251         else if (obj instanceof DERBMPString)
252         {
253             return indent + "BMPString(" + ((DERBMPString)obj).getString() + ") " + System.getProperty("line.separator");
254         }
255         else if (obj instanceof DERT61String)
256         {
257             return indent + "T61String(" + ((DERT61String)obj).getString() + ") " + System.getProperty("line.separator");
258         }
259         else if (obj instanceof DERUTCTime)
260         {
261             return indent + "UTCTime(" + ((DERUTCTime)obj).getTime() + ") " + System.getProperty("line.separator");
262         }
263         else if (obj instanceof DERUnknownTag)
264         {
265             return indent + "Unknown " + Integer.toString(((DERUnknownTag)obj).getTag(), 16) + " " + new String JavaDoc(Hex.encode(((DERUnknownTag)obj).getData())) + System.getProperty("line.separator");
266         }
267         else
268         {
269             return indent + obj.toString() + System.getProperty("line.separator");
270         }
271     }
272
273     /**
274      * dump out a DER object as a formatted string
275      *
276      * @param obj the DERObject to be dumped out.
277      */

278     public static String JavaDoc dumpAsString(
279         Object JavaDoc obj)
280     {
281         if (obj instanceof DERObject)
282         {
283             return _dumpAsString("", (DERObject)obj);
284         }
285         else if (obj instanceof DEREncodable)
286         {
287             return _dumpAsString("", ((DEREncodable)obj).getDERObject());
288         }
289
290         return "unknown object type " + obj.toString();
291     }
292 }
293
Popular Tags