读BERTLV时,什么时候停止?

When reading BERTLV, when do you stop?

我有以下 BERTLV: 61394F0BA00000030800001001234579074F05A000012345500E49442D4F6E65205049562042494F5F50107777772E6F626572746875722E636F6D7F66080202800002028000

我正在尝试以递归方式解析它,因此我将第一部分视为 TLV。

标记:0x61,长度:0x39,值:4F0BA00000030800001001234579074F05A000012345500E49442D4F6E65205049562042494F5F50107777772E6F626572746875722E636F6D

然后我进一步分解,得到

标签:0x4F,长度:0x0B,值: A000000308000010012345

现在,我该如何停止?在这一点上,我知道这个值是这个 TLV 的最后一段,而不是另一个嵌套的 TLV。

问:现在,我该如何停止?

A: 以十六进制表示的标记长度 (0x0B) 并显示值中的字节数。每个字节在您的示例中表示为 2(两个)十六进制字符。所以 0x0B 是十进制的 11 显示 11*2 = 22 个字符的值要使用。

这是 https://iso8583.info/lib/EMV/TLVs 工具解析的 BER-TLV 缓冲区的解析示例:

---
# Cheef's parser.
# Copyright (C) 2008-2017 Alexander Shevelev. https://iso8583.info/
# lib   : "/lib/EMV/" - Integrated Circuit Card Specifications for Payment Systems
# tool  : "TLVs"
# stat  : 40 nodes, 4 lookup tables, 25.00% passed (1/4)

TLVs:#"61394F0BA00000030800001001234579074F05A000012345500E49442D4F..8000" # EMV, Tag + Length + Value (TLV) series
- x61:#"61394F0BA00000030800001001234579074F05A000012345500E49442D4F..6F6D" # ISO 7816, Template, Application
  - tag: "61"
  - len: "39" #  // 57
  - val:#"4F0BA00000030800001001234579074F05A000012345500E49442D4F6E65..6F6D" # Template, Application.
    - x4F:#"4F0BA000000308000010012345" # ISO 7816, Application Identifier (AID), Card
      - tag: "4F"
      - len: "0B" #  // 11
      - val:#"A000000308000010012345" # AID, Card.
        - RID: "A000000308" # Registered Application Provider Identifier (RID) // National Institute of Standards and Technology
        - PIX: "000010012345" # Proprietary Application Identifier Extension (PIX)
    - x79:#"79074F05A000012345" # ISO 7816, Coexistent Tag Allocation Authority
      - tag: "79"
      - len: "07" #  // 7
      - val:#"4F05A000012345"
        - x4F:#"4F05A000012345" # ISO 7816, Application Identifier (AID), Card
          - tag: "4F"
          - len: "05" #  // 5
          - val:#"A000012345" # AID, Card.
            - RID: "A000012345" # Registered Application Provider Identifier (RID)
    - x50:#"500E49442D4F6E65205049562042494F" # ISO 7816, Application Label
      - tag: "50"
      - len: "0E" #  // 14
      - val: "49442D4F6E65205049562042494F" # Application Label. // ID-One PIV BIO
    - x5F50:#"5F50107777772E6F626572746875722E636F6D" # ISO 7816, Uniform resource locator (URL)
      - tag: "5F50"
      - len: "10" #  // 16
      - val: "7777772E6F626572746875722E636F6D" # URL. // www.oberthur.com
- x7F66:#"7F66080202800002028000"
  - tag: "7F66"
  - len: "08" #  // 8
  - val:#"0202800002028000"
    - x02:#"02028000"
      - tag: "02"
      - len: "02" #  // 2
      - val: "8000"
    - x02:#"02028000"
      - tag: "02"
      - len: "02" #  // 2
      - val: "8000"

好吧,经过大量挖掘,我发现简单的 TAG 实际上不仅仅是一个数字,TAG 本身实际上在其中编码了一大堆信息。我为了找出 TLV 是否包含嵌套的 TLV 以便我的代码知道何时继续处理以及何时停止,我必须解码 TAG.

所以TAG包含8位,每一位都有特殊的含义:

我需要的位是 B5 以确定当前的 TLV 是否为 Constructed...什么时候 Constructed 表示当前TLV 由多个 TLV 组成,因此对于我的代码,我使用此信息递归挖掘嵌套的 TLV.

TLV 不是 而非 Constructed 时,这是我停止冒泡的情况。

这是我通过解析数据并检查 TAG 中的 B5 位的递归函数输出:

11:20:38.428 Parsing: 61394F0BA00000030800001000010079074F05A000000308500E49442D4F6E65205049562042494F5F50107777772E6F626572746875722E636F6D7F66080202800002028000
11:20:38.436 Constructed Data Object
11:20:38.437 Parsing: 4F0BA00000030800001000010079074F05A000000308500E49442D4F6E65205049562042494F5F50107777772E6F626572746875722E636F6D
11:20:38.437 Primitive Data Object
11:20:38.437 tag: 79, len: 11, value: A000000308000010000100
11:20:38.437 Constructed Data Object
11:20:38.437 Parsing: 4F05A000000308
11:20:38.437 Primitive Data Object
11:20:38.437 tag: 79, len: 5, value: A000000308
11:20:38.437 Primitive Data Object
11:20:38.437 tag: 80, len: 14, value: 49442D4F6E65205049562042494F
11:20:38.437 MultiByte tag
11:20:38.437 Primitive Data Object
11:20:38.437 tag: 24400, len: 16, value: 7777772E6F626572746875722E636F6D
11:20:38.438 MultiByte tag
11:20:38.438 Constructed Data Object
11:20:38.438 Parsing: 0202800002028000
11:20:38.438 Primitive Data Object
11:20:38.438 tag: 2, len: 2, value: 8000
11:20:38.438 Primitive Data Object
11:20:38.438 tag: 2, len: 2, value: 8000