title: parsing asn.1 with bouncycastle
body: <p>I have some asn.1 encoded data. I have some success decoding it with bouncycastle but I hit the wall with quite complex example. It must be doable, but can't get further, hope you could help me, here is the example data:</p>
  
  <pre><code>A1 81 A9 02 02 1C 1F 02 01 15 30 81 9F 55 02 01 14 A0 81 98 A4 81 95 6B 10 30 0E 80 04 00 00 01 1F A1 06 30 04 80 02 33 32 63 06 30 04 80 02 33 32 61 0E 30 0C 80 0A 30 32 32 33 37 38 33 36 31 30 62 06 30 04 80 02 33 32 64 02 87 00 6B 13 30 11 80 04 00 00 01 1F A1 09 30 07 80 05 23 23 37 30 30 4E 01 02 0A 01 16 67 12 30 10 A3 0E 81 0C 2B 34 38 32 32 33 37 38 33 36 31 30 68 0F 30 0D A3 0B 82 09 32 32 38 36 35 33 33 39 38 65 09 30 07 80 05 23 23 37 30 30 7E 12 A0 10 18 0E 32 30 31 36 30 33 30 32 32 32 30 31 33 36
  
  CSTA Browser decoded:
  
  rOSE.roiv-apdu
  { -- SEQUENCE -- 
      invokeID = 7199,
      operation-value = 21 (cSTAEventReport),
      argument
      { -- SEQUENCE -- 
          crossRefIdentifier = '01 14'H,
          eventSpecificInfo.callControlEvents.delivered
          { -- SEQUENCE -- 
              connection.both
              { -- SEQUENCE -- 
                  callID = '00 00 01 1F'H,
                  deviceID.staticID
                  { -- SEQUENCE -- 
                      deviceIdentifier.dialingNumber = "32" '33 32'H
                  }
              },
              alertingDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.dialingNumber = "32" '33 32'H
              },
              callingDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.dialingNumber = "0223783610" '30 32 32 33 37 38 33 36 31 30'H
              },
              calledDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.dialingNumber = "32" '33 32'H
              },
              lastRedirectionDevice.notKnown NULL,
              originatingNIDConnection.both
              { -- SEQUENCE -- 
                  callID = '00 00 01 1F'H,
                  deviceID.staticID
                  { -- SEQUENCE -- 
                      deviceIdentifier.dialingNumber = "##700" '23 23 37 30 30'H
                  }
              },
              localConnectionInfo = 2 (alerting),
              cause = 22 (newCall),
              networkCallingDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.explicitPublic.international = "+48223783610" '2B 34 38 32 32 33 37 38 33 36 31 30'H
              },
              networkCalledDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.explicitPublic.national = "228653398" '32 32 38 36 35 33 33 39 38'H
              },
              associatedCallingDevice.deviceIdentifier
              { -- SEQUENCE -- 
                  deviceIdentifier.dialingNumber = "##700" '23 23 37 30 30'H
              },
              extensions
              { -- SEQUENCE -- 
                  security
                  { -- SEQUENCE -- 
                      timestamp = "20160302220136" '32 30 31 36 30 33 30 32 32 32 30 31 33 36'H
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>bouncycastle's dump produces something like that:</p>
  
  <pre><code>00 AC A1 81 A9 02 02 5F B9 02 01 15 30 81 9F 55 02 01 91 A0 81 98 A4 81 95 6B 10 30 0E 80 04 00 00 03 98 A1 06 30 04 80 02 33 32 63 06 30 04 80 02 33 32 61 0E 30 0C 80 0A 30 32 32 33 37 38 33 36 31 30 62 06 30 04 80 02 33 32 64 02 87 00 6B 13 30 11 80 04 00 00 03 98 A1 09 30 07 80 05 23 23 37 30 30 4E 01 02 0A 01 16 67 12 30 10 A3 0E 81 0C 2B 34 38 32 32 33 37 38 33 36 31 30 68 0F 30 0D A3 0B 82 09 32 32 38 36 35 33 33 39 38 65 09 30 07 80 05 23 23 37 30 30 7E 12 A0 10 18 0E 32 30 31 36 30 33 30 34 31 35 32 32 34 30 
  buf:Tagged [1] IMPLICIT 
      Sequence
          Integer(24505)
          Integer(21)
          DER Sequence
              DER ApplicationSpecific[21] (0191)
              Tagged [0]
                  Tagged [4] IMPLICIT 
                      Sequence
                          DER ApplicationSpecific[11]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[4] 
                                  Tagged [1]
                                      DER Sequence
                                          Tagged [0] IMPLICIT 
                                              DER Octet String[2] 
                          DER ApplicationSpecific[3]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[2] 
                          DER ApplicationSpecific[1]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[10] 
                          DER ApplicationSpecific[2]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[2] 
                          DER ApplicationSpecific[4]
                              Tagged [7] IMPLICIT 
                                  DER Octet String[0] 
                          DER ApplicationSpecific[11]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[4] 
                                  Tagged [1]
                                      DER Sequence
                                          Tagged [0] IMPLICIT 
                                              DER Octet String[5] 
                          DER ApplicationSpecific[14] (02)
                          DER Enumerated(22)
                          DER ApplicationSpecific[7]
                              Sequence
                                  Tagged [3]
                                      Tagged [1] IMPLICIT 
                                          DER Octet String[12] 
                          DER ApplicationSpecific[8]
                              Sequence
                                  Tagged [3]
                                      Tagged [2] IMPLICIT 
                                          DER Octet String[9] 
                          DER ApplicationSpecific[5]
                              Sequence
                                  Tagged [0] IMPLICIT 
                                      DER Octet String[5] 
                          DER ApplicationSpecific[30]
                              Tagged [0]
                                  GeneralizedTime(20160304152240GMT+01:00) 
  </code></pre>
  
  <p>and I am trying to parse it: </p>
  
  <pre><code>protected void parse() {
      logger.trace("Executing parse()");
  
      try {
  
          ASN1InputStream input = new ASN1InputStream(asn1Data);
          ASN1Primitive p;
  
          if ((p = input.readObject()) != null) {
              ASN1TaggedObject o1 = ASN1TaggedObject.getInstance(p);
              ASN1Sequence s1 = ASN1Sequence.getInstance(o1.getObject());
              invokeID = Integer.parseInt(s1.getObjectAt(0).toString());
              operationValue = Integer.parseInt(s1.getObjectAt(1).toString());
  
              DERSequence ders = (DERSequence) DERSequence.getInstance(s1.getObjectAt(2));
              DERApplicationSpecific das = (DERApplicationSpecific) ders.getObjectAt(0);
              crossRefIdentifier = das.getContents();
  
  
  
       //here are some experiments, but can't get the right objects I could parse / walk through
  
              ASN1TaggedObject o2 = ASN1TaggedObject.getInstance(ders.getObjectAt(1));
              DERTaggedObject dto = (DERTaggedObject) o2.getObject();
  
              ASN1Sequence s2 = ASN1Sequence.getInstance(dto.getObject());
              DERApplicationSpecific das1 = (DERApplicationSpecific) s2.getObjectAt(0);
              ASN1Sequence s3 = (ASN1Sequence) das1.getObject();
  
              }
      } catch (Exception ex) {
          logger.warn("exception while parsing ASN1 data", ex);
      }
  
  }
  </code></pre>
  
  <p>as you can see I've been able to decode some basic tags (ie. invokeID, operation-value and crossRefIdentifier) but can't get deeper in the tree (callId, callingNumber ...).
  If you have experience in that matter I would appreciate your help.</p>