Annex D: Use of Basic Encoding Rules ASN.1
ISO 7816 Annex D.1: BER-TLV data object
Each BER-TLV data object (see ISO/IEC 8825) shall consist of 2 or 3 consecutive fields :
- The tag field T consists of one or more consecutive bytes. It encodes a class, a type and a number.
- The length field consists of one or more consecutive bytes. It encodes an integer L.
- If L is not null, then the value field V consists of L consecutive bytes. If L is null, then the data object is empty: there is no value field.
ISO/IEC 7816 uses neither '00' nor 'FF' as tag value.
NOTE - Before, between or after BER-TLV data objects, '00' or 'FF' bytes without any meaning may occur (e.g. due to erased or modified TLV-coded data objects).
ISO 7816-4 Annex D.2: Tag field
The bits B8 and B7 of the leading byte of tag field shall encode the tag class, i.e. the class of the data object.
- B8-7='00' introduces a tag of universal class
- B8-7='01' introduces a tag of application class
- B8-7='10' introduces a tag of context-specific clas
- B8-7='11' introduces a tag of private class
The bit B6 of the leading byte of the tag field shall encode the tag type, i.e. the type of the data object.
- B6=0 introduces a primitive data object
- B6=1 introduces a constructed data object
If the bits B5-B1 of the leading byte are not all set to 1, then may they shall encode an integer equal to the tag number which therefore lies in the range from 0 to 30. Then the tag field consists of a single byte.
Otherwise (B5-B1 set to 1 in the leading byte), the tag field shall continue on one or more subsequent bytes.
- The bit B8 of each subsequent byte shall be set to 1, unless it is the last subsequent byte
- The bits B7-B1 of the first subsequent byte shall not be all set to 0
- The bits B7-B1 of the first subsequent byte, folowed by the bits B7 to B1 of each further subsequent byte, up to and including the bits B7-B1 of the last subsequent byte, shall encode an integer equal to the tag number (thus strictly positive).
ISO 7816-4 Annex D.3: Length field
In short form, the length field consists of a single byte where the bit B8 shall be set to 0 and the bits B7-B1 shall encode an integer equal to the number of bytes in the value field. Any length from 0-127 can thus be encoded by 1 byte.
In long form, the length field consists of a leading byte where the bit B8 shall be set to 1 and the B7-B1 shall not be all equal, thus encoding a positive integer equal to the number of subsequent bytes in the length field. Those subsequent bytes shall encode an integer equal to the number of bytes in the value field. Any length within the APDU limit (up to 65535) can thus be encoded by 3 bytes.
NOTE - ISO/IEC 7816 does not use the indefinite lengths specified by the basic encoding rules of ASN.1 (see ISO/IEC 8825).
ISO 7816-4 Annex D.4: Value field
In this part of ISO/IEC 7816, the value field of some primitive BER-TLV data objects consists of zero, one or more SIMPLE-TLV data objects.
The value field of any other primitive BER-TLV data object consists of zero, one or more data elements fixed by the specifications of the data objects.
The value field of each constructed BER-TLV data object consists of zero, one or more BER-TLV data objects.