1 package org.bouncycastle.asn1.util; 2 3 import java.util.Enumeration; 4 5 import org.bouncycastle.asn1.*; 6 import org.bouncycastle.util.encoders.Hex; 7 8 public class ASN1Dump 9 { 10 private static String TAB = " "; 11 12 /** 13 * dump a DER object as a formatted string with indentation 14 * 15 * @param obj the DERObject to be dumped out. 16 */ 17 static String _dumpAsString( 18 String indent, 19 DERObject obj) 20 { 21 if (obj instanceof ASN1Sequence) 22 { 23 StringBuffer buf = new StringBuffer(); 24 Enumeration e = ((ASN1Sequence)obj).getObjects(); 25 String tab = indent + TAB; 26 27 buf.append(indent); 28 if (obj instanceof BERConstructedSequence) 29 { 30 buf.append("BER ConstructedSequence"); 31 } 32 else if (obj instanceof DERConstructedSequence) 33 { 34 buf.append("DER ConstructedSequence"); 35 } 36 else if (obj instanceof BERSequence) 37 { 38 buf.append("BER Sequence"); 39 } 40 else if (obj instanceof DERSequence) 41 { 42 buf.append("DER Sequence"); 43 } 44 else 45 { 46 buf.append("Sequence"); 47 } 48 49 buf.append(System.getProperty("line.separator")); 50 51 while (e.hasMoreElements()) 52 { 53 Object o = e.nextElement(); 54 55 // BEGIN android-changed 56 if (o == null || o.equals(DERNull.THE_ONE)) 57 { 58 buf.append(tab); 59 buf.append("NULL"); 60 buf.append(System.getProperty("line.separator")); 61 } 62 else if (o instanceof DERObject) 63 { 64 buf.append(_dumpAsString(tab, (DERObject)o)); 65 } 66 else 67 { 68 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject())); 69 } 70 // END android-changed 71 } 72 return buf.toString(); 73 } 74 else if (obj instanceof DERTaggedObject) 75 { 76 StringBuffer buf = new StringBuffer(); 77 String tab = indent + TAB; 78 79 buf.append(indent); 80 if (obj instanceof BERTaggedObject) 81 { 82 buf.append("BER Tagged ["); 83 } 84 else 85 { 86 buf.append("Tagged ["); 87 } 88 89 DERTaggedObject o = (DERTaggedObject)obj; 90 91 buf.append(Integer.toString(o.getTagNo())); 92 buf.append(']'); 93 94 if (!o.isExplicit()) 95 { 96 buf.append(" IMPLICIT "); 97 } 98 99 buf.append(System.getProperty("line.separator")); 100 101 if (o.isEmpty()) 102 { 103 buf.append(tab); 104 buf.append("EMPTY"); 105 buf.append(System.getProperty("line.separator")); 106 } 107 else 108 { 109 buf.append(_dumpAsString(tab, o.getObject())); 110 } 111 112 return buf.toString(); 113 } 114 else if (obj instanceof DERConstructedSet) 115 { 116 StringBuffer buf = new StringBuffer(); 117 Enumeration e = ((ASN1Set)obj).getObjects(); 118 String tab = indent + TAB; 119 120 buf.append(indent); 121 buf.append("ConstructedSet"); 122 buf.append(System.getProperty("line.separator")); 123 124 while (e.hasMoreElements()) 125 { 126 Object o = e.nextElement(); 127 128 if (o == null) 129 { 130 buf.append(tab); 131 buf.append("NULL"); 132 buf.append(System.getProperty("line.separator")); 133 } 134 else if (o instanceof DERObject) 135 { 136 buf.append(_dumpAsString(tab, (DERObject)o)); 137 } 138 else 139 { 140 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject())); 141 } 142 } 143 return buf.toString(); 144 } 145 else if (obj instanceof BERSet) 146 { 147 StringBuffer buf = new StringBuffer(); 148 Enumeration e = ((ASN1Set)obj).getObjects(); 149 String tab = indent + TAB; 150 151 buf.append(indent); 152 buf.append("BER Set"); 153 buf.append(System.getProperty("line.separator")); 154 155 while (e.hasMoreElements()) 156 { 157 Object o = e.nextElement(); 158 159 if (o == null) 160 { 161 buf.append(tab); 162 buf.append("NULL"); 163 buf.append(System.getProperty("line.separator")); 164 } 165 else if (o instanceof DERObject) 166 { 167 buf.append(_dumpAsString(tab, (DERObject)o)); 168 } 169 else 170 { 171 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject())); 172 } 173 } 174 return buf.toString(); 175 } 176 else if (obj instanceof DERSet) 177 { 178 StringBuffer buf = new StringBuffer(); 179 Enumeration e = ((ASN1Set)obj).getObjects(); 180 String tab = indent + TAB; 181 182 buf.append(indent); 183 buf.append("DER Set"); 184 buf.append(System.getProperty("line.separator")); 185 186 while (e.hasMoreElements()) 187 { 188 Object o = e.nextElement(); 189 190 if (o == null) 191 { 192 buf.append(tab); 193 buf.append("NULL"); 194 buf.append(System.getProperty("line.separator")); 195 } 196 else if (o instanceof DERObject) 197 { 198 buf.append(_dumpAsString(tab, (DERObject)o)); 199 } 200 else 201 { 202 buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject())); 203 } 204 } 205 return buf.toString(); 206 } 207 else if (obj instanceof DERObjectIdentifier) 208 { 209 return indent + "ObjectIdentifier(" + ((DERObjectIdentifier)obj).getId() + ")" + System.getProperty("line.separator"); 210 } 211 else if (obj instanceof DERBoolean) 212 { 213 return indent + "Boolean(" + ((DERBoolean)obj).isTrue() + ")" + System.getProperty("line.separator"); 214 } 215 else if (obj instanceof DERInteger) 216 { 217 return indent + "Integer(" + ((DERInteger)obj).getValue() + ")" + System.getProperty("line.separator"); 218 } 219 else if (obj instanceof BERConstructedOctetString) 220 { 221 return indent + "BER Constructed Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator"); 222 } 223 else if (obj instanceof DEROctetString) 224 { 225 return indent + "DER Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator"); 226 } 227 else if (obj instanceof DERBitString) 228 { 229 return indent + "DER Bit String" + "[" + ((DERBitString)obj).getBytes().length + ", " + ((DERBitString)obj).getPadBits() + "] " + System.getProperty("line.separator"); 230 } 231 else if (obj instanceof DERIA5String) 232 { 233 return indent + "IA5String(" + ((DERIA5String)obj).getString() + ") " + System.getProperty("line.separator"); 234 } 235 else if (obj instanceof DERUTF8String) 236 { 237 return indent + "UTF8String(" + ((DERUTF8String)obj).getString() + ") " + System.getProperty("line.separator"); 238 } 239 else if (obj instanceof DERPrintableString) 240 { 241 return indent + "PrintableString(" + ((DERPrintableString)obj).getString() + ") " + System.getProperty("line.separator"); 242 } 243 else if (obj instanceof DERVisibleString) 244 { 245 return indent + "VisibleString(" + ((DERVisibleString)obj).getString() + ") " + System.getProperty("line.separator"); 246 } 247 else if (obj instanceof DERBMPString) 248 { 249 return indent + "BMPString(" + ((DERBMPString)obj).getString() + ") " + System.getProperty("line.separator"); 250 } 251 else if (obj instanceof DERT61String) 252 { 253 return indent + "T61String(" + ((DERT61String)obj).getString() + ") " + System.getProperty("line.separator"); 254 } 255 else if (obj instanceof DERUTCTime) 256 { 257 return indent + "UTCTime(" + ((DERUTCTime)obj).getTime() + ") " + System.getProperty("line.separator"); 258 } 259 else if (obj instanceof DERGeneralizedTime) 260 { 261 return indent + "GeneralizedTime(" + ((DERGeneralizedTime)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(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 dumpAsString( 279 Object 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