ASN1Dump.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:8k
源码类别:

CA认证

开发平台:

Java

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