1 19 20 package com.maverick.crypto.asn1; 21 22 import java.util.Enumeration ; 23 24 import com.maverick.crypto.encoders.Hex; 25 26 public class ASN1Dump 27 { 28 private static String TAB = " "; 29 30 35 public static String _dumpAsString( 36 String indent, 37 DERObject obj) 38 { 39 if (obj instanceof ASN1Sequence) 40 { 41 StringBuffer buf = new StringBuffer (); 42 Enumeration e = ((ASN1Sequence)obj).getObjects(); 43 String 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 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 buf = new StringBuffer (); 93 String 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 buf = new StringBuffer (); 133 Enumeration e = ((ASN1Set)obj).getObjects(); 134 String 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 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 buf = new StringBuffer (); 164 Enumeration e = ((ASN1Set)obj).getObjects(); 165 String 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 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 buf = new StringBuffer (); 195 Enumeration e = ((ASN1Set)obj).getObjects(); 196 String 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 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 (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 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 | Popular Tags |