'How to parse nested TLV structures

This is TLV code:

30 3e 31 0b 30 09 06 03 55 04 06 13 02 50 4c 31 1b 30 19 06 03 55 04 0a 13 12 
55 6e 69 7a 65 74 6f 20 53 70 2e 20 7a 20 6f 2e 6f 2e 31 12 30 10 06 03 55 04
03 13 09 43 65 72 74 75 6d 20 43 41  

enter image description here

From this figure, we can know how to determine whether a TLV structure is nested。 so,How to parse nested TLV structures。



Solution 1:[1]

If you just want to display the value in your question, just copy and paste it to https://asn1.io/asn1playground/ (Decode Tab)

OSS Nokalva TLV Print Utility  Version 8.6.1
Copyright (C) 1997-2021 OSS Nokalva, Inc.  All rights reserved.


30 3E
  31 0B
    30 09
      06 03 550406
      13 02 504C
  31 1B
    30 19
      06 03 55040A
      13 12 556E697A65746F2053702E207A206F2E6F2E
  31 12
    30 10
      06 03 550403
      13 09 43657274756D204341

If you want to write a program that does this, you need to know how to read a tag and how to read a length (which is not straight forward) ... you can read the x690 spec (https://www.itu.int/rec/T-REC-X.690-202102-I/en)

Then you will write a re entering function that reads your TLV and re enters if T is constructed (meaning that V is itself a TLV)

Java example here: https://github.com/yafred/asn1-tool/blob/master/runtime/src/main/java/com/yafred/asn1/runtime/BERDumper.java

I'm sure you can easily do the same in python (no doubt you can find it already done)

Solution 2:[2]

You would need to follow next specifications:

  1. ASN.1, Basic Encoding Rules (BER) for general SEQUENCE, SET and TLV parsing.
  2. Object Identifier (OID) for the Objects.

The example data parsing:

---
# Cheef's parser.
# Copyright (C) 2008-2022 Aleksandr Shevelev. https://iso8583.info/
# lib   : "/lib/ISO/8825/1/BER/" # Specification of Basic Encoding Rules
# tool  : "TLVs"

# stat  : 53 nodes, 3 lookup tables, 100.00% passed (3/3)

TLVs:#"303E310B3009*************2504C311B3019*****5040A1312556E697A..4341" # ASN.1, Basic Encoding Rules (BER), Tag + Length + Value (TLV) series
- x30:#"303E310B3009*************2504C311B3019*****5040A1312556E697A..4341" # SEQUENCE
  - tag: "30"
  - len: "3E" #  // 62
  - val:#"310B3009*************2504C311B3019*****5040A1312556E697A6574..4341"
    - x31:#"310B3009*************2504C" # SET
      - tag: "31"
      - len: "0B" #  // 11
      - val:#"3009*************2504C"
        - x30:#"3009*************2504C" # SEQUENCE
          - tag: "30"
          - len: "09" #  // 9
          - val:#"0603*********2504C"
            - x06:#"0603550406" # Object Identifier (OID)
              - tag: "06"
              - len: "03" #  // 3
              - val: "550406" #  // 2 5 4 6 - joint-iso-itu-t.ds.attributeType.countryName
            - x13:#"1302504C" # PrintableString
              - tag: "13"
              - len: "02" #  // 2
              - val: "504C" #  // PL
    - x31:#"311B3019*****5040A1312556E697A65746F2053702E207A206F2E6F2E" # SET
      - tag: "31"
      - len: "1B" #  // 27
      - val:#"3019*****5040A1312556E697A65746F2053702E207A206F2E6F2E"
        - x30:#"3019*****5040A1312556E697A65746F2053702E207A206F2E6F2E" # SEQUENCE
          - tag: "30"
          - len: "19" #  // 25
          - val:#"060355040A1312556E697A65746F2053702E207A206F2E6F2E"
            - x06:#"060355040A" # Object Identifier (OID)
              - tag: "06"
              - len: "03" #  // 3
              - val: "55040A" #  // 2 5 4 10 - joint-iso-itu-t.ds.attributeType.organizationName
            - x13:#"1312556E697A65746F2053702E207A206F2E6F2E" # PrintableString
              - tag: "13"
              - len: "12" #  // 18
              - val: "556E697A65746F2053702E207A206F2E6F2E" #  // Unizeto Sp. z o.o.
    - x31:#"3112*************************4756D******" # SET
      - tag: "31"
      - len: "12" #  // 18
      - val:#"3010*********************4756D******"
        - x30:#"3010*********************4756D******" # SEQUENCE
          - tag: "30"
          - len: "10" #  // 16
          - val:#"0603*****************4756D******"
            - x06:#"0603550403" # Object Identifier (OID)
              - tag: "06"
              - len: "03" #  // 3
              - val: "550403" #  // 2 5 4 3 - joint-iso-itu-t.ds.attributeType.commonName
            - x13:#"1309*******4756D******" # PrintableString
              - tag: "13"
              - len: "09" #  // 9
              - val: "43657274756D******" #  // Certum CA

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YaFred
Solution 2 iso8583.info support