ASN.1 BER 处理标签号 31 或更高

ASN.1 BER Handling Tag Number 31 or Higher

我正在解码 ASN.1 BER 编码数据,我想知道如何正确处理 Tag 31(特别是)和更高版本。我觉得 X.690 规范有点模棱两可,而且我没有任何数据的示例确实具有 31 或更大的标签。

有问题的部分是 8.1.2.4,它处理大于或等于 31 的标签;特别是 8.1.2.4.1 c) 上面写着:

c) bits 5 to 1 shall be encoded as 11111₂

然后 8.1.2.4.2 表示:

The subsequent octets shall encode the number of the tag as follows:

a) bit 8 of each octet shall be set to one unless it is the last octet of the identifier octets;

b) bits 7 to 1 of the first subsequent octet, followed by bits 7 to 1 of the second subsequent octet, followed in turn by bits 7 to 1 of each further octet, up to and including the last subsequent octet in the identifier octets shall be the encoding of an unsigned binary integer equal to the tag number, with bit 7 of the first subsequent octet as the most significant bit;

c) bits 7 to 1 of the first subsequent octet shall not all be zero.

基于此,您如何对标签 31 进行编码?我认为这意味着,除了表示正在表示标签 31 或更大之外,就指示实际标签号而言,第一个八位组的第 5 到 1 位应该被忽略,并且实际标签号以八位组编码2 从第 8.1.2.4.2 节开始遵循规则。在此方案中,标签 31 将是:

. . . 1 1 1 1 1   0 0 0 1 1 1 1 1

所以;在第一个八位字节中,位 5 到 1 设置为 1,但它们不构成标签编号的一部分。第二个八位字节的第 5 位到第 1 位也是 1,但这次它们确实代表正在编码的标签号。

任何人都可以确认我的解释是否正确,或者当标签 >= 31 时,第一个八位字节的第 5 位到第 1 位是否应该被视为标签号的一部分?

第 8.1.2.4.2 节非常简单。如果标签不适合标识符八位组的 5 位,则将它们设置为 11111 以声明标签编号更大,并将在下一个八位组中编码。

恕我直言,您的解释是正确的。规范 ITU-T X.690 (08/2015) 在第 8.1.2.4.3 节中很好地说明了大于 30 的标签。标签号是从(图中)2nd octetLast octet.

计算得出的

您可以尝试不同的 ASN.1 编码 here

这是我的标签编码 implementation,显然可以与许多其他代码一起使用。