Search code examples
decodingasn.1ber

ASN.1 BER Handling Tag Number 31 or Higher


I'm working on decoding ASN.1 BER encoded data, and I'm wondering what the correct handling of Tag 31 (specifically) and greater should be. I feel the X.690 spec is a little ambiguous and I don't have an example of any data which does have a tag of 31 or greater.

The section in question is 8.1.2.4 which deals with tags greater than or equal to 31; specifically 8.1.2.4.1 c) which says:

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

Then 8.1.2.4.2 which says:

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.

Based on that, how do you encode tag 31? I think it means that, other than indicating that tag 31 or greater is being represented, bits 5 to 1 of the first octet should be ignored as far as indicating the actual tag number are concerned, and that the actual tag number is encoded in octet 2 onwards following the rules from section 8.1.2.4.2. In this scheme tag 31 would be:

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

So; in the first octet, bits 5 to 1 are set to 1's but they don't form a part of the number of the tag. Bits 5 to 1 of the second octet are also 1's, but this time they do represent the tag number being encoded.

Can anyone confirm if my interpretation is correct, or should bits 5 to 1 of the first octet be considered as part of the tag number when the tag is >= 31?


Solution

  • Section 8.1.2.4.2 is pretty straightforward. If a tag does not fit in the 5 bits of Identifier octet you set them to 11111 to state that tag number is greater and will be encoded in the next octets.

    Your interpretation is IMHO correct. Specification ITU-T X.690 (08/2015) has a nice illustration of tag greater than 30 in section 8.1.2.4.3. Tag number is computed from (in the illustration) the 2nd octet to Last octet.