Home » geronimo-2.2-source-release » org.apache.geronimo.crypto.asn1.util » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.crypto.asn1.util;
   19   
   20   import java.util.Enumeration;
   21   
   22   import org.apache.geronimo.crypto.asn1;
   23   import org.apache.geronimo.crypto.encoders.Hex;
   24   
   25   public class ASN1Dump
   26   {
   27       private static String  TAB = "    ";
   28   
   29       /**
   30        * dump a DER object as a formatted string with indentation
   31        *
   32        * @param obj the DERObject to be dumped out.
   33        */
   34       static String _dumpAsString(
   35           String      indent,
   36           DERObject   obj)
   37       {
   38           if (obj instanceof ASN1Sequence)
   39           {
   40               StringBuffer    buf = new StringBuffer();
   41               Enumeration     e = ((ASN1Sequence)obj).getObjects();
   42               String          tab = indent + TAB;
   43   
   44               buf.append(indent);
   45               if (obj instanceof BERConstructedSequence)
   46               {
   47                   buf.append("BER ConstructedSequence");
   48               }
   49               else if (obj instanceof DERConstructedSequence)
   50               {
   51                   buf.append("DER ConstructedSequence");
   52               }
   53               else if (obj instanceof BERSequence)
   54               {
   55                   buf.append("BER Sequence");
   56               }
   57               else if (obj instanceof DERSequence)
   58               {
   59                   buf.append("DER Sequence");
   60               }
   61               else
   62               {
   63                   buf.append("Sequence");
   64               }
   65   
   66               buf.append(System.getProperty("line.separator"));
   67   
   68               while (e.hasMoreElements())
   69               {
   70                   Object  o = e.nextElement();
   71   
   72                   if (o == null || o.equals(new DERNull()))
   73                   {
   74                       buf.append(tab);
   75                       buf.append("NULL");
   76                       buf.append(System.getProperty("line.separator"));
   77                   }
   78                   else if (o instanceof DERObject)
   79                   {
   80                       buf.append(_dumpAsString(tab, (DERObject)o));
   81                   }
   82                   else
   83                   {
   84                       buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
   85                   }
   86               }
   87               return buf.toString();
   88           }
   89           else if (obj instanceof DERTaggedObject)
   90           {
   91               StringBuffer    buf = new StringBuffer();
   92               String          tab = indent + TAB;
   93   
   94               buf.append(indent);
   95               if (obj instanceof BERTaggedObject)
   96               {
   97                   buf.append("BER Tagged [");
   98               }
   99               else
  100               {
  101                   buf.append("Tagged [");
  102               }
  103   
  104               DERTaggedObject o = (DERTaggedObject)obj;
  105   
  106               buf.append(Integer.toString(o.getTagNo()));
  107               buf.append("]");
  108   
  109               if (!o.isExplicit())
  110               {
  111                   buf.append(" IMPLICIT ");
  112               }
  113   
  114               buf.append(System.getProperty("line.separator"));
  115   
  116               if (o.isEmpty())
  117               {
  118                   buf.append(tab);
  119                   buf.append("EMPTY");
  120                   buf.append(System.getProperty("line.separator"));
  121               }
  122               else
  123               {
  124                   buf.append(_dumpAsString(tab, o.getObject()));
  125               }
  126   
  127               return buf.toString();
  128           }
  129           else if (obj instanceof DERConstructedSet)
  130           {
  131               StringBuffer    buf = new StringBuffer();
  132               Enumeration     e = ((ASN1Set)obj).getObjects();
  133               String          tab = indent + TAB;
  134   
  135               buf.append(indent);
  136               buf.append("ConstructedSet");
  137               buf.append(System.getProperty("line.separator"));
  138   
  139               while (e.hasMoreElements())
  140               {
  141                   Object  o = e.nextElement();
  142   
  143                   if (o == null)
  144                   {
  145                       buf.append(tab);
  146                       buf.append("NULL");
  147                       buf.append(System.getProperty("line.separator"));
  148                   }
  149                   else if (o instanceof DERObject)
  150                   {
  151                       buf.append(_dumpAsString(tab, (DERObject)o));
  152                   }
  153                   else
  154                   {
  155                       buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
  156                   }
  157               }
  158               return buf.toString();
  159           }
  160           else if (obj instanceof BERSet)
  161           {
  162               StringBuffer    buf = new StringBuffer();
  163               Enumeration     e = ((ASN1Set)obj).getObjects();
  164               String          tab = indent + TAB;
  165   
  166               buf.append(indent);
  167               buf.append("BER Set");
  168               buf.append(System.getProperty("line.separator"));
  169   
  170               while (e.hasMoreElements())
  171               {
  172                   Object  o = e.nextElement();
  173   
  174                   if (o == null)
  175                   {
  176                       buf.append(tab);
  177                       buf.append("NULL");
  178                       buf.append(System.getProperty("line.separator"));
  179                   }
  180                   else if (o instanceof DERObject)
  181                   {
  182                       buf.append(_dumpAsString(tab, (DERObject)o));
  183                   }
  184                   else
  185                   {
  186                       buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
  187                   }
  188               }
  189               return buf.toString();
  190           }
  191           else if (obj instanceof DERSet)
  192           {
  193               StringBuffer    buf = new StringBuffer();
  194               Enumeration     e = ((ASN1Set)obj).getObjects();
  195               String          tab = indent + TAB;
  196   
  197               buf.append(indent);
  198               buf.append("DER Set");
  199               buf.append(System.getProperty("line.separator"));
  200   
  201               while (e.hasMoreElements())
  202               {
  203                   Object  o = e.nextElement();
  204   
  205                   if (o == null)
  206                   {
  207                       buf.append(tab);
  208                       buf.append("NULL");
  209                       buf.append(System.getProperty("line.separator"));
  210                   }
  211                   else if (o instanceof DERObject)
  212                   {
  213                       buf.append(_dumpAsString(tab, (DERObject)o));
  214                   }
  215                   else
  216                   {
  217                       buf.append(_dumpAsString(tab, ((DEREncodable)o).getDERObject()));
  218                   }
  219               }
  220               return buf.toString();
  221           }
  222           else if (obj instanceof DERObjectIdentifier)
  223           {
  224               return indent + "ObjectIdentifier(" + ((DERObjectIdentifier)obj).getId() + ")" + System.getProperty("line.separator");
  225           }
  226           else if (obj instanceof DERBoolean)
  227           {
  228               return indent + "Boolean(" + ((DERBoolean)obj).isTrue() + ")" + System.getProperty("line.separator");
  229           }
  230           else if (obj instanceof DERInteger)
  231           {
  232               return indent + "Integer(" + ((DERInteger)obj).getValue() + ")" + System.getProperty("line.separator");
  233           }
  234           else if (obj instanceof BERConstructedOctetString)
  235           {
  236               return indent + "BER Constructed Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator");
  237           }
  238           else if (obj instanceof DEROctetString)
  239           {
  240               return indent + "DER Octet String" + "[" + ((ASN1OctetString)obj).getOctets().length + "] " + System.getProperty("line.separator");
  241           }
  242           else if (obj instanceof DERBitString)
  243           {
  244               return indent + "DER Bit String" + "[" + ((DERBitString)obj).getBytes().length + ", " + ((DERBitString)obj).getPadBits() + "] " + System.getProperty("line.separator");
  245           }
  246           else if (obj instanceof DERIA5String)
  247           {
  248               return indent + "IA5String(" + ((DERIA5String)obj).getString() + ") " + System.getProperty("line.separator");
  249           }
  250           else if (obj instanceof DERUTF8String)
  251           {
  252               return indent + "UTF8String(" + ((DERUTF8String)obj).getString() + ") " + System.getProperty("line.separator");
  253           }
  254           else if (obj instanceof DERPrintableString)
  255           {
  256               return indent + "PrintableString(" + ((DERPrintableString)obj).getString() + ") " + System.getProperty("line.separator");
  257           }
  258           else if (obj instanceof DERVisibleString)
  259           {
  260               return indent + "VisibleString(" + ((DERVisibleString)obj).getString() + ") " + System.getProperty("line.separator");
  261           }
  262           else if (obj instanceof DERBMPString)
  263           {
  264               return indent + "BMPString(" + ((DERBMPString)obj).getString() + ") " + System.getProperty("line.separator");
  265           }
  266           else if (obj instanceof DERT61String)
  267           {
  268               return indent + "T61String(" + ((DERT61String)obj).getString() + ") " + System.getProperty("line.separator");
  269           }
  270           else if (obj instanceof DERUTCTime)
  271           {
  272               return indent + "UTCTime(" + ((DERUTCTime)obj).getTime() + ") " + System.getProperty("line.separator");
  273           }
  274           else if (obj instanceof DERGeneralizedTime)
  275           {
  276               return indent + "GeneralizedTime(" + ((DERGeneralizedTime)obj).getTime() + ") " + System.getProperty("line.separator");
  277           }
  278           else if (obj instanceof DERUnknownTag)
  279           {
  280               return indent + "Unknown " + Integer.toString(((DERUnknownTag)obj).getTag(), 16) + " " + new String(Hex.encode(((DERUnknownTag)obj).getData())) + System.getProperty("line.separator");
  281           }
  282           else
  283           {
  284               return indent + obj.toString() + System.getProperty("line.separator");
  285           }
  286       }
  287   
  288       /**
  289        * dump out a DER object as a formatted string
  290        *
  291        * @param obj the DERObject to be dumped out.
  292        */
  293       public static String dumpAsString(
  294           Object   obj)
  295       {
  296           if (obj instanceof DERObject)
  297           {
  298               return _dumpAsString("", (DERObject)obj);
  299           }
  300           else if (obj instanceof DEREncodable)
  301           {
  302               return _dumpAsString("", ((DEREncodable)obj).getDERObject());
  303           }
  304   
  305           return "unknown object type " + obj.toString();
  306       }
  307   }

Home » geronimo-2.2-source-release » org.apache.geronimo.crypto.asn1.util » [javadoc | source]