001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.crypto.asn1.util;
019
020 import java.util.Enumeration;
021
022 import org.apache.geronimo.crypto.asn1.*;
023 import org.apache.geronimo.crypto.encoders.Hex;
024
025 public class ASN1Dump
026 {
027 private static String TAB = " ";
028
029 /**
030 * dump a DER object as a formatted string with indentation
031 *
032 * @param obj the DERObject to be dumped out.
033 */
034 static String _dumpAsString(
035 String indent,
036 DERObject obj)
037 {
038 if (obj instanceof ASN1Sequence)
039 {
040 StringBuffer buf = new StringBuffer();
041 Enumeration e = ((ASN1Sequence)obj).getObjects();
042 String tab = indent + TAB;
043
044 buf.append(indent);
045 if (obj instanceof BERConstructedSequence)
046 {
047 buf.append("BER ConstructedSequence");
048 }
049 else if (obj instanceof DERConstructedSequence)
050 {
051 buf.append("DER ConstructedSequence");
052 }
053 else if (obj instanceof BERSequence)
054 {
055 buf.append("BER Sequence");
056 }
057 else if (obj instanceof DERSequence)
058 {
059 buf.append("DER Sequence");
060 }
061 else
062 {
063 buf.append("Sequence");
064 }
065
066 buf.append(System.getProperty("line.separator"));
067
068 while (e.hasMoreElements())
069 {
070 Object o = e.nextElement();
071
072 if (o == null || o.equals(new DERNull()))
073 {
074 buf.append(tab);
075 buf.append("NULL");
076 buf.append(System.getProperty("line.separator"));
077 }
078 else if (o instanceof DERObject)
079 {
080 buf.append(_dumpAsString(tab, (DERObject)o));
081 }
082 else
083 {
084 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
085 }
086 }
087 return buf.toString();
088 }
089 else if (obj instanceof DERTaggedObject)
090 {
091 StringBuffer buf = new StringBuffer();
092 String tab = indent + TAB;
093
094 buf.append(indent);
095 if (obj instanceof BERTaggedObject)
096 {
097 buf.append("BER Tagged [");
098 }
099 else
100 {
101 buf.append("Tagged [");
102 }
103
104 DERTaggedObject o = (DERTaggedObject)obj;
105
106 buf.append(Integer.toString(o.getTagNo()));
107 buf.append("]");
108
109 if (!o.isExplicit())
110 {
111 buf.append(" IMPLICIT ");
112 }
113
114 buf.append(System.getProperty("line.separator"));
115
116 if (o.isEmpty())
117 {
118 buf.append(tab);
119 buf.append("EMPTY");
120 buf.append(System.getProperty("line.separator"));
121 }
122 else
123 {
124 buf.append(_dumpAsString(tab, o.getObject()));
125 }
126
127 return buf.toString();
128 }
129 else if (obj instanceof DERConstructedSet)
130 {
131 StringBuffer buf = new StringBuffer();
132 Enumeration e = ((ASN1Set)obj).getObjects();
133 String tab = indent + TAB;
134
135 buf.append(indent);
136 buf.append("ConstructedSet");
137 buf.append(System.getProperty("line.separator"));
138
139 while (e.hasMoreElements())
140 {
141 Object o = e.nextElement();
142
143 if (o == null)
144 {
145 buf.append(tab);
146 buf.append("NULL");
147 buf.append(System.getProperty("line.separator"));
148 }
149 else if (o instanceof DERObject)
150 {
151 buf.append(_dumpAsString(tab, (DERObject)o));
152 }
153 else
154 {
155 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
156 }
157 }
158 return buf.toString();
159 }
160 else if (obj instanceof BERSet)
161 {
162 StringBuffer buf = new StringBuffer();
163 Enumeration e = ((ASN1Set)obj).getObjects();
164 String tab = indent + TAB;
165
166 buf.append(indent);
167 buf.append("BER Set");
168 buf.append(System.getProperty("line.separator"));
169
170 while (e.hasMoreElements())
171 {
172 Object o = e.nextElement();
173
174 if (o == null)
175 {
176 buf.append(tab);
177 buf.append("NULL");
178 buf.append(System.getProperty("line.separator"));
179 }
180 else if (o instanceof DERObject)
181 {
182 buf.append(_dumpAsString(tab, (DERObject)o));
183 }
184 else
185 {
186 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
187 }
188 }
189 return buf.toString();
190 }
191 else if (obj instanceof DERSet)
192 {
193 StringBuffer buf = new StringBuffer();
194 Enumeration e = ((ASN1Set)obj).getObjects();
195 String tab = indent + TAB;
196
197 buf.append(indent);
198 buf.append("DER Set");
199 buf.append(System.getProperty("line.separator"));
200
201 while (e.hasMoreElements())
202 {
203 Object o = e.nextElement();
204
205 if (o == null)
206 {
207 buf.append(tab);
208 buf.append("NULL");
209 buf.append(System.getProperty("line.separator"));
210 }
211 else if (o instanceof DERObject)
212 {
213 buf.append(_dumpAsString(tab, (DERObject)o));
214 }
215 else
216 {
217 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
218 }
219 }
220 return buf.toString();
221 }
222 else if (obj instanceof DERObjectIdentifier)
223 {
224 return indent + "ObjectIdentifier(" + ((DERObjectIdentifier)obj).getId() + ")" + System.getProperty("line.separator");
225 }
226 else if (obj instanceof DERBoolean)
227 {
228 return indent + "Boolean(" + ((DERBoolean)obj).isTrue() + ")" + System.getProperty("line.separator");
229 }
230 else if (obj instanceof DERInteger)
231 {
232 return indent + "Integer(" + ((DERInteger)obj).getValue() + ")" + System.getProperty("line.separator");
233 }
234 else if (obj instanceof BERConstructedOctetString)
235 {
236 return indent + "BER Constructed Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator");
237 }
238 else if (obj instanceof DEROctetString)
239 {
240 return indent + "DER Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator");
241 }
242 else if (obj instanceof DERBitString)
243 {
244 return indent + "DER Bit String" + "[" + ((DERBitString)obj).getBytes().length + ", " + ((DERBitString)obj).getPadBits() + "] " + System.getProperty("line.separator");
245 }
246 else if (obj instanceof DERIA5String)
247 {
248 return indent + "IA5String(" + ((DERIA5String)obj).getString() + ") " + System.getProperty("line.separator");
249 }
250 else if (obj instanceof DERUTF8String)
251 {
252 return indent + "UTF8String(" + ((DERUTF8String)obj).getString() + ") " + System.getProperty("line.separator");
253 }
254 else if (obj instanceof DERPrintableString)
255 {
256 return indent + "PrintableString(" + ((DERPrintableString)obj).getString() + ") " + System.getProperty("line.separator");
257 }
258 else if (obj instanceof DERVisibleString)
259 {
260 return indent + "VisibleString(" + ((DERVisibleString)obj).getString() + ") " + System.getProperty("line.separator");
261 }
262 else if (obj instanceof DERBMPString)
263 {
264 return indent + "BMPString(" + ((DERBMPString)obj).getString() + ") " + System.getProperty("line.separator");
265 }
266 else if (obj instanceof DERT61String)
267 {
268 return indent + "T61String(" + ((DERT61String)obj).getString() + ") " + System.getProperty("line.separator");
269 }
270 else if (obj instanceof DERUTCTime)
271 {
272 return indent + "UTCTime(" + ((DERUTCTime)obj).getTime() + ") " + System.getProperty("line.separator");
273 }
274 else if (obj instanceof DERGeneralizedTime)
275 {
276 return indent + "GeneralizedTime(" + ((DERGeneralizedTime)obj).getTime() + ") " + System.getProperty("line.separator");
277 }
278 else if (obj instanceof DERUnknownTag)
279 {
280 return indent + "Unknown " + Integer.toString(((DERUnknownTag)obj).getTag(), 16) + " " + new String(Hex.encode(((DERUnknownTag)obj).getData())) + System.getProperty("line.separator");
281 }
282 else
283 {
284 return indent + obj.toString() + System.getProperty("line.separator");
285 }
286 }
287
288 /**
289 * dump out a DER object as a formatted string
290 *
291 * @param obj the DERObject to be dumped out.
292 */
293 public static String dumpAsString(
294 Object obj)
295 {
296 if (obj instanceof DERObject)
297 {
298 return _dumpAsString("", (DERObject)obj);
299 }
300 else if (obj instanceof DEREncodable)
301 {
302 return _dumpAsString("", ((DEREncodable)obj).getDERObject());
303 }
304
305 return "unknown object type " + obj.toString();
306 }
307 }