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