ASN.1 Types and Tags
Table of contents
Definition
To ensure that encodings are not ambiguous, every ASN.1 type is associated with a tag. A tag consists of two parts: the class and the number as we saw at 3_Tag Length Value chapter. TLV encoding may be recursive (netest construction, constructed format). One value field can consist of one or more other TLVs, which are encoded. No maximum level of nesting is defined, so complex structures can be created. The porperty of Tag is unique. However, the same tag to two (or more) can be assigned to different types, but should avoid ambiguity. The context in which an instance of the type occurs must be sufficient to allow unique identification.
Reference
Please refer to the listed reference below. That help you understand what is ASN.1.
Deep Dive
- Tyeps and Tags
Let’s take a look at the table below. We cna figure out which tag corresponds to a type, and they are defined by ASN.1.
Type Tag (dec) Type Tag (dec) Type Tag (dec) Bit String 03 GraphicString 25 SET 17 BMPString 30 IA5String 22 SET OF 17 BOOLEAN 01 INTEGER 02 TeletexString 20 CHARACTER STRING 29 NULL 05 T61String 20 CHOICE NumericString 18 TIME 14 DATE 31 ObjectDescriptor 07 TIME OF DAY 32 DATE-TIME 33 OBJECT IDENTIFIER 06 UniversalString 28 DURATION 34 OCTET STRING 04 UTCTime 23 EMBEDDED PDV 11 PrintableString 19 UTF8String 12 ENUMERATED 10 REAL 09 VideotexString 21 EXTERNAL 08 RELATIVE-OID 13 VisibleString 26 GeneralString 27 SEQUENCE 16 GeneralizedTime 24 SEQUENCE OF 16 - As an example, if we use Universal Class, Primitive TLV, and Tag 03, then the tag field value will be 00h + 0h + 03h = 0x03.
- Let’s take a look at a couple of tag. The details and more information can be found at ASN1 Tyep and Tag
- Bit String (Tag: 0x03)
- The corresponding Value field to ASN.1 BIT STRING type is arbitrary length strings of bits.
- The Value field contains a leading byte that specifies the number of bits left unused in the final byte of content.
- For example, In [Pic.2], the last 5 bits are not used.
[Pic.2] Bit String Example - Octet String (Tag: 0x04)
- Octet String is similar to Bit String data types. But one difference is Octet String cannot have unused bits.
- The leading bytes must not be added to the contents.
- Example is shown in [Pic.3].
[Pic.3] Octet String Example - Boolean (Tag: 0x01)
- The value of type Boolean can be TRUE(0x00) or FALSE(0xFF).
- The example is shown in [Pic.4].
[Pic.4] Boolean Example - Integer (Tag: 0x02)
- The value of integer contains the encoded integer if it is positive, or its two’s complement if it is negative.
- If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative.
- The example is shown in [Pic.5].
[Pic.5] Integer Example - Object Identifier (Tag: 0x06)
- Object Identifier is composed of the combination of number and dot.
- The type of Object Identifier is listed in Object Identifier of ASN1.
- As an example, Object Identification will be written with 1.2.840.113549.1.7.1
- We will see the Encoding/Decoding rule of Object Identifier in 5_ASN1_ObjectIdentifier chapter in detail, because they are not attached to certificate as the plan form.
[Pic.6] Object Identifier Example