001: package org.bouncycastle.asn1.util;
002:
003: import org.bouncycastle.asn1.ASN1OctetString;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.ASN1Set;
006: import org.bouncycastle.asn1.BERConstructedOctetString;
007: import org.bouncycastle.asn1.BERConstructedSequence;
008: import org.bouncycastle.asn1.BERSequence;
009: import org.bouncycastle.asn1.BERSet;
010: import org.bouncycastle.asn1.BERTaggedObject;
011: import org.bouncycastle.asn1.DERBMPString;
012: import org.bouncycastle.asn1.DERBitString;
013: import org.bouncycastle.asn1.DERBoolean;
014: import org.bouncycastle.asn1.DERConstructedSequence;
015: import org.bouncycastle.asn1.DERConstructedSet;
016: import org.bouncycastle.asn1.DEREncodable;
017: import org.bouncycastle.asn1.DERGeneralizedTime;
018: import org.bouncycastle.asn1.DERIA5String;
019: import org.bouncycastle.asn1.DERInteger;
020: import org.bouncycastle.asn1.DERNull;
021: import org.bouncycastle.asn1.DERObject;
022: import org.bouncycastle.asn1.DERObjectIdentifier;
023: import org.bouncycastle.asn1.DEROctetString;
024: import org.bouncycastle.asn1.DERPrintableString;
025: import org.bouncycastle.asn1.DERSequence;
026: import org.bouncycastle.asn1.DERSet;
027: import org.bouncycastle.asn1.DERT61String;
028: import org.bouncycastle.asn1.DERTaggedObject;
029: import org.bouncycastle.asn1.DERUTCTime;
030: import org.bouncycastle.asn1.DERUTF8String;
031: import org.bouncycastle.asn1.DERUnknownTag;
032: import org.bouncycastle.asn1.DERVisibleString;
033: import org.bouncycastle.util.encoders.Hex;
034:
035: import java.util.Enumeration;
036:
037: public class ASN1Dump {
038: private static final String TAB = " ";
039:
040: /**
041: * dump a DER object as a formatted string with indentation
042: *
043: * @param obj the DERObject to be dumped out.
044: */
045: static String _dumpAsString(String indent, DERObject obj) {
046: if (obj instanceof ASN1Sequence) {
047: StringBuffer buf = new StringBuffer();
048: Enumeration e = ((ASN1Sequence) obj).getObjects();
049: String tab = indent + TAB;
050:
051: buf.append(indent);
052: if (obj instanceof BERConstructedSequence) {
053: buf.append("BER ConstructedSequence");
054: } else if (obj instanceof DERConstructedSequence) {
055: buf.append("DER ConstructedSequence");
056: } else if (obj instanceof BERSequence) {
057: buf.append("BER Sequence");
058: } else if (obj instanceof DERSequence) {
059: buf.append("DER Sequence");
060: } else {
061: buf.append("Sequence");
062: }
063:
064: buf.append(System.getProperty("line.separator"));
065:
066: while (e.hasMoreElements()) {
067: Object o = e.nextElement();
068:
069: if (o == null || o.equals(new DERNull())) {
070: buf.append(tab);
071: buf.append("NULL");
072: buf.append(System.getProperty("line.separator"));
073: } else if (o instanceof DERObject) {
074: buf.append(_dumpAsString(tab, (DERObject) o));
075: } else {
076: buf.append(_dumpAsString(tab, ((DEREncodable) o)
077: .getDERObject()));
078: }
079: }
080: return buf.toString();
081: } else if (obj instanceof DERTaggedObject) {
082: StringBuffer buf = new StringBuffer();
083: String tab = indent + TAB;
084:
085: buf.append(indent);
086: if (obj instanceof BERTaggedObject) {
087: buf.append("BER Tagged [");
088: } else {
089: buf.append("Tagged [");
090: }
091:
092: DERTaggedObject o = (DERTaggedObject) obj;
093:
094: buf.append(Integer.toString(o.getTagNo()));
095: buf.append(']');
096:
097: if (!o.isExplicit()) {
098: buf.append(" IMPLICIT ");
099: }
100:
101: buf.append(System.getProperty("line.separator"));
102:
103: if (o.isEmpty()) {
104: buf.append(tab);
105: buf.append("EMPTY");
106: buf.append(System.getProperty("line.separator"));
107: } else {
108: buf.append(_dumpAsString(tab, o.getObject()));
109: }
110:
111: return buf.toString();
112: } else if (obj instanceof DERConstructedSet) {
113: StringBuffer buf = new StringBuffer();
114: Enumeration e = ((ASN1Set) obj).getObjects();
115: String tab = indent + TAB;
116:
117: buf.append(indent);
118: buf.append("ConstructedSet");
119: buf.append(System.getProperty("line.separator"));
120:
121: while (e.hasMoreElements()) {
122: Object o = e.nextElement();
123:
124: if (o == null) {
125: buf.append(tab);
126: buf.append("NULL");
127: buf.append(System.getProperty("line.separator"));
128: } else if (o instanceof DERObject) {
129: buf.append(_dumpAsString(tab, (DERObject) o));
130: } else {
131: buf.append(_dumpAsString(tab, ((DEREncodable) o)
132: .getDERObject()));
133: }
134: }
135: return buf.toString();
136: } else if (obj instanceof BERSet) {
137: StringBuffer buf = new StringBuffer();
138: Enumeration e = ((ASN1Set) obj).getObjects();
139: String tab = indent + TAB;
140:
141: buf.append(indent);
142: buf.append("BER Set");
143: buf.append(System.getProperty("line.separator"));
144:
145: while (e.hasMoreElements()) {
146: Object o = e.nextElement();
147:
148: if (o == null) {
149: buf.append(tab);
150: buf.append("NULL");
151: buf.append(System.getProperty("line.separator"));
152: } else if (o instanceof DERObject) {
153: buf.append(_dumpAsString(tab, (DERObject) o));
154: } else {
155: buf.append(_dumpAsString(tab, ((DEREncodable) o)
156: .getDERObject()));
157: }
158: }
159: return buf.toString();
160: } else if (obj instanceof DERSet) {
161: StringBuffer buf = new StringBuffer();
162: Enumeration e = ((ASN1Set) obj).getObjects();
163: String tab = indent + TAB;
164:
165: buf.append(indent);
166: buf.append("DER Set");
167: buf.append(System.getProperty("line.separator"));
168:
169: while (e.hasMoreElements()) {
170: Object o = e.nextElement();
171:
172: if (o == null) {
173: buf.append(tab);
174: buf.append("NULL");
175: buf.append(System.getProperty("line.separator"));
176: } else if (o instanceof DERObject) {
177: buf.append(_dumpAsString(tab, (DERObject) o));
178: } else {
179: buf.append(_dumpAsString(tab, ((DEREncodable) o)
180: .getDERObject()));
181: }
182: }
183: return buf.toString();
184: } else if (obj instanceof DERObjectIdentifier) {
185: return indent + "ObjectIdentifier("
186: + ((DERObjectIdentifier) obj).getId() + ")"
187: + System.getProperty("line.separator");
188: } else if (obj instanceof DERBoolean) {
189: return indent + "Boolean(" + ((DERBoolean) obj).isTrue()
190: + ")" + System.getProperty("line.separator");
191: } else if (obj instanceof DERInteger) {
192: return indent + "Integer(" + ((DERInteger) obj).getValue()
193: + ")" + System.getProperty("line.separator");
194: } else if (obj instanceof BERConstructedOctetString) {
195: return indent + "BER Constructed Octet String" + "["
196: + ((ASN1OctetString) obj).getOctets().length + "] "
197: + System.getProperty("line.separator");
198: } else if (obj instanceof DEROctetString) {
199: return indent + "DER Octet String" + "["
200: + ((ASN1OctetString) obj).getOctets().length + "] "
201: + System.getProperty("line.separator");
202: } else if (obj instanceof DERBitString) {
203: return indent + "DER Bit String" + "["
204: + ((DERBitString) obj).getBytes().length + ", "
205: + ((DERBitString) obj).getPadBits() + "] "
206: + System.getProperty("line.separator");
207: } else if (obj instanceof DERIA5String) {
208: return indent + "IA5String("
209: + ((DERIA5String) obj).getString() + ") "
210: + System.getProperty("line.separator");
211: } else if (obj instanceof DERUTF8String) {
212: return indent + "UTF8String("
213: + ((DERUTF8String) obj).getString() + ") "
214: + System.getProperty("line.separator");
215: } else if (obj instanceof DERPrintableString) {
216: return indent + "PrintableString("
217: + ((DERPrintableString) obj).getString() + ") "
218: + System.getProperty("line.separator");
219: } else if (obj instanceof DERVisibleString) {
220: return indent + "VisibleString("
221: + ((DERVisibleString) obj).getString() + ") "
222: + System.getProperty("line.separator");
223: } else if (obj instanceof DERBMPString) {
224: return indent + "BMPString("
225: + ((DERBMPString) obj).getString() + ") "
226: + System.getProperty("line.separator");
227: } else if (obj instanceof DERT61String) {
228: return indent + "T61String("
229: + ((DERT61String) obj).getString() + ") "
230: + System.getProperty("line.separator");
231: } else if (obj instanceof DERUTCTime) {
232: return indent + "UTCTime(" + ((DERUTCTime) obj).getTime()
233: + ") " + System.getProperty("line.separator");
234: } else if (obj instanceof DERGeneralizedTime) {
235: return indent + "GeneralizedTime("
236: + ((DERGeneralizedTime) obj).getTime() + ") "
237: + System.getProperty("line.separator");
238: } else if (obj instanceof DERUnknownTag) {
239: return indent
240: + "Unknown "
241: + Integer.toString(((DERUnknownTag) obj).getTag(),
242: 16)
243: + " "
244: + new String(Hex.encode(((DERUnknownTag) obj)
245: .getData()))
246: + System.getProperty("line.separator");
247: } else {
248: return indent + obj.toString()
249: + System.getProperty("line.separator");
250: }
251: }
252:
253: /**
254: * dump out a DER object as a formatted string
255: *
256: * @param obj the DERObject to be dumped out.
257: */
258: public static String dumpAsString(Object obj) {
259: if (obj instanceof DERObject) {
260: return _dumpAsString("", (DERObject) obj);
261: } else if (obj instanceof DEREncodable) {
262: return _dumpAsString("", ((DEREncodable) obj)
263: .getDERObject());
264: }
265:
266: return "unknown object type " + obj.toString();
267: }
268: }
|