title: How to parse the ASN.1 object and get the data using bouncycastle in java
body: <p>I have an ASN.1 data which contain some URL info and certificates data. Now I am trying to parse the data. I am using the dump method which is shown below.</p>
  
  <pre><code>  private static void dump(ASN1Object obj, StringBuffer buf, int depth)  throws Exception
    {
      indent(buf, depth);
      if (obj == null)
      {
        buf.append("Null").append(NL);
  
      }
      else if (obj instanceof DERBitString)
      {
            try {
  
                DERBitString dbr =  (DERBitString)obj;
                Log.e("Testing", "DERBitString length:"+dbr.getBytes().length);
  
          } catch (Exception e) {
              // TODO: handle exception
              Log.e("Testing", "Exception :"+e);
          }
  
       // buf.append("DERBitString : ").append(((DERBitString)obj).getBytes().length * 8).append(" bits").append(NL);
        buf.append("DERBitString content111 : ").append(new String(((DERBitString)obj).getBytes())).append(NL);
  
  
  
        cert.write(obj.getEncoded());
      }
      else if (obj instanceof ASN1String)
      {
          Log.i("Testing", "Hashcode:"+obj.hashCode());
          buf.append("ASN1String : ").append(((ASN1String)obj).getString()).append(NL);
          if(numOctString == 1 &amp;&amp; sURL == null )
          {
              sURL = ((ASN1String)obj).getString();
          }
          else
          {
              cert.write(obj.getEncoded());
          }
      }
      else if (obj instanceof ASN1UTCTime)
      {
        buf.append("ASN1UTCTime : ").append(((ASN1UTCTime)obj).getTime()).append(NL);
        cert.write(obj.getEncoded());
      }
      else if (obj instanceof ASN1GeneralizedTime)
      {
        buf.append("ASN1GeneralizedTime : ").append(((ASN1GeneralizedTime)obj).getTime()).append(NL);
        cert.write(obj.getEncoded());
      }
      else if (obj instanceof ASN1ObjectIdentifier)
      {
          buf.append("ASN1ObjectIdentifier : ").append((ASN1ObjectIdentifier)obj).append(NL);
  
          ASN1ObjectIdentifier identifier =  (ASN1ObjectIdentifier)obj;
  
          cert.write(obj.getEncoded());
          //buf.append("ASN1ObjectIdentifier : ").append(OidMap.get(((ASN1ObjectIdentifier)obj).getId())).append(NL); 
      }
      else if (obj instanceof ASN1Integer)
      {
        buf.append("ASN1Integer : ").append(((ASN1Integer)obj).getValue().toString()).append(NL);
        cert.write(obj.getEncoded());
      }
      else if (obj instanceof ASN1Boolean)
      {
        buf.append("ASN1Boolean : ").append(((ASN1Boolean)obj).isTrue() ? "true" : "false").append(NL);
        cert.write(obj.getEncoded());
      }
      else if (obj instanceof ASN1OctetString)
      {
         numOctString ++;
        buf.append("ASN1OctetString").append(NL);
        ASN1OctetString octObj = (ASN1OctetString)obj;
        try
        {
          ASN1InputStream aIn = new ASN1InputStream(octObj.getOctetStream());
          ASN1Primitive dobj = aIn.readObject();
          dump(dobj, buf, depth + 1);
        }
        catch(Exception e)
        {
          indent(buf, depth + 1);
          //buf.append(ByteArrayUtil.toHexString(octObj.getOctets(), ":")).append(NL);
        }
      }
      else if (obj instanceof ASN1TaggedObject)
      {
        buf.append("ASN1TaggedObject").append(NL);
        dump(((ASN1TaggedObject)obj).getObject(), buf, depth + 1);
      }
      else if (obj instanceof ASN1Sequence)
      {
        buf.append("ASN1Sequence").append(NL);
  
        Enumeration objs = ((ASN1Sequence)obj).getObjects();
        while (objs.hasMoreElements())
        {
          Object o = objs.nextElement();
          if (o == null || o instanceof DERNull)
          {
            indent(buf, depth + 1);
            //ASN1VectorImpl impl= new ASN1VectorImpl((ASN1Sequence)obj);
            buf.append("DERNull").append(NL);
          }
          else if (o instanceof ASN1Primitive)
          {
            dump((ASN1Primitive)o, buf, depth + 1);
          }
          else
          {
            dump(((ASN1Encodable)o).toASN1Primitive(), buf, depth + 1);
          }
        }
      }
      else if (obj instanceof ASN1Set)
      {
        buf.append("ASN1Set").append(NL);
  
        Enumeration objs = ((ASN1Set)obj).getObjects();
        while (objs.hasMoreElements())
        {
          Object o = objs.nextElement();
          if (o == null || o instanceof DERNull)
          {
            indent(buf, depth + 1);
            buf.append(NL);
          }
          else if (o instanceof ASN1Primitive)
          {
            dump((ASN1Primitive)o, buf, depth + 1);
          }
          else
          {
            dump(((ASN1Encodable)o).toASN1Primitive(), buf, depth + 1);
          }
        }
      }
      else
      {
          Log.e("Testing", "==================Else case=============");
          Log.e("Testing", "==================obj============="+obj.getEncoded().length);
        buf.append(obj.getClass().getName()).append(NL);
      }      
    }
  </code></pre>
  
  <p>When I print the StringBuffer i am getting the output as below.</p>
  
  <pre><code> ASN1Sequence
     ASN1Sequence
       ASN1String : 1
       ASN1OctetString
         ASN1Sequence
           ASN1String : https://website url(Some StringDta )
           ASN1String : StringDta
           ASN1Sequence
             ASN1OctetString
               ASN1Sequence
                 ASN1Sequence
                   ASN1TaggedObject
                     ASN1Integer : 2
                   ASN1Integer : 288799720653653968683086
                   ASN1Sequence
                     ASN1ObjectIdentifier : 1.2.840.113549.1.1.5
                     DERNull
                   ASN1Sequence
                     ASN1Set
                       ASN1Sequence
                         ASN1ObjectIdentifier : 2.5.4.3
                         ASN1String : StringDta
                   ASN1Sequence
                     ASN1UTCTime : 130417071944GMT+00:00
                     ASN1UTCTime : 140417072944GMT+00:00
                   ASN1Sequence
                     ASN1Set
                       ASN1Sequence
                         ASN1ObjectIdentifier : 2.5.4.10
                         ASN1String : StringDta
                     ASN1Set
                       ASN1Sequence
                         ASN1ObjectIdentifier : 2.5.4.3
                         ASN1String : StringDta
                     ASN1Set
                       ASN1Sequence
                         ASN1ObjectIdentifier : 1.2.840.113549.1.9.1
                         ASN1String : StringDta
                   ASN1Sequence
                     ASN1Sequence
                       ASN1ObjectIdentifier : 1.2.840.113549.1.1.1
                       DERNull
                     DERBitString content111 : 0?
   ?
  </code></pre>
  
  <p>Now how can parse the above data to get the required data like URL and certificates data?</p>
  
  <p>How can we identify the URL and certificates data from above output? Can some one help on this?</p>