DER, the Distinguished Encoding Rules, is the strict form of BER, the Basic Encoding Rules, both defined in ITU-T X.690 for data described in ASN.1 (X.680). Every value is encoded as identifier, length and contents octets (“TLV”); a constructed value’s contents are more TLVs. DER allows exactly one encoding for each value, which is why signatures are computed over it.
This page follows Rec. ITU-T X.690 (02/2021), the free ITU-T edition of ISO/IEC 8825-1, clauses 8 (BER), 10 (DER) and 11 (restrictions shared by CER and DER), and takes the universal tag numbers from Rec. ITU-T X.680 (02/2021), Table 1 and Table 8.
Identifier octets
| Bits | Name | Meaning |
|---|---|---|
| 8–7 | Class | 00 universal (types defined by ASN.1 itself), 01 application, 10 context-specific (the [0], [1] … tags of a structure), 11 private. |
| 6 | Constructed | 1 if the contents are further TLVs (SEQUENCE, SET, explicit tags); 0 for a primitive value. |
| 5–1 | Tag number | 0 to 30. The value 31 (11111) means the tag number follows in further octets, 7 bits per octet, most significant first, with bit 8 set on every octet but the last; bits 7–1 of the first further octet must not all be zero (X.690 8.1.2.4). |
So 30 is a universal constructed tag 16 (SEQUENCE), A0 a
context-specific constructed tag 0 ([0]), and 80 a
context-specific primitive tag 0.
Universal tags
| Number | Byte (primitive / constructed) | Type |
|---|---|---|
| 0 | 00 | Reserved for the encoding rules; BER uses 00 00 as end-of-contents after an indefinite length (X.690 8.1.5) |
| 1 | 01 | BOOLEAN |
| 2 | 02 | INTEGER |
| 3 | 03 | BIT STRING |
| 4 | 04 | OCTET STRING |
| 5 | 05 | NULL |
| 6 | 06 | OBJECT IDENTIFIER |
| 7 | 07 | ObjectDescriptor |
| 8 | 28 | EXTERNAL, INSTANCE OF |
| 9 | 09 | REAL |
| 10 | 0A | ENUMERATED |
| 11 | 2B | EMBEDDED PDV |
| 12 | 0C | UTF8String |
| 13 | 0D | RELATIVE-OID |
| 14 | 0E | TIME |
| 15 | – | Reserved for future editions |
| 16 | 30 | SEQUENCE, SEQUENCE OF |
| 17 | 31 | SET, SET OF |
| 18 | 12 | NumericString |
| 19 | 13 | PrintableString |
| 20 | 14 | TeletexString (T61String) |
| 21 | 15 | VideotexString |
| 22 | 16 | IA5String (ASCII) |
| 23 | 17 | UTCTime |
| 24 | 18 | GeneralizedTime |
| 25 | 19 | GraphicString |
| 26 | 1A | VisibleString (ISO646String) |
| 27 | 1B | GeneralString |
| 28 | 1C | UniversalString (4-octet ISO/IEC 10646 form) |
| 29 | 1D | CHARACTER STRING (X.680 44.2) |
| 30 | 1E | BMPString (2-octet ISO/IEC 10646 BMP form) |
| 31 | 1F 1F | DATE |
| 32 | 1F 20 | TIME-OF-DAY |
| 33 | 1F 21 | DATE-TIME |
| 34 | 1F 22 | DURATION |
| 35 | 1F 23 | OID-IRI |
| 36 | 1F 24 | RELATIVE-OID-IRI |
| 37 on | – | Reserved for addenda |
The bytes are worked out from X.690 8.1.2: SEQUENCE, SET, EXTERNAL and EMBEDDED PDV are always constructed (8.9–8.12, 8.17, 8.18); the others are shown primitive, which DER requires for strings (10.2).
Length octets
| First octet | Form | Meaning |
|---|---|---|
00–7F | Short | The length itself, 0 to 127. |
81–FE | Long | The low 7 bits say how many octets of length follow, big-endian: 81 C8 is 200, 82 03 5A is 858. |
80 | Indefinite | BER only, and only for constructed encodings: the contents run until an end-of-contents 00 00. Not allowed in DER. |
FF | – | Shall not be used (kept for future extension, 8.1.3.5). |
Contents of common types
| Type | Contents |
|---|---|
| BOOLEAN | One octet: 00 false; BER allows any non-zero octet for true (8.2), DER and CER require FF (11.1). |
| INTEGER, ENUMERATED | Two’s complement, big-endian (8.3, 8.4). If there is more than one octet, the first octet and bit 8 of the second must not be all ones or all zeros, so the fewest octets are used: 128 is 02 02 00 80. |
| BIT STRING | An octet giving the number of unused bits in the last octet (0–7), then the bits, the leading bit in bit 8 of the first octet (8.6.2). An empty bit string is the single octet 00. |
| OCTET STRING | The octets. |
| NULL | Nothing: 05 00 (8.8). |
| OBJECT IDENTIFIER | The first two arcs as 40 × first + second, then each further arc, each in base 128 in the fewest octets, bit 8 set on all but the last octet of each (8.19). 1.2.840.113549 is 06 06 2A 86 48 86 F7 0D; X.690’s own example, 2.999.3, is 06 03 88 37 03. |
| UTCTime | ASCII; in DER it ends with Z and the seconds are always present (11.8): YYMMDDHHMMSSZ. |
| GeneralizedTime | ASCII; in DER it ends with Z, the seconds are always present, fractions of a second drop trailing zeros (and the point, if nothing is left), and the decimal point is . (11.7). Midnight is YYYYMMDD000000Z of the following day. |
| Strings | UTF8String in UTF-8, UniversalString in the 4-octet and BMPString in the 2-octet ISO/IEC 10646 forms (8.23.7–8.23.10); the other string types by the registered character sets X.680 Table 8 lists for them. |
DER restrictions
- Lengths are always definite and in the fewest octets (no
81 05for 5) (10.1). - Bit strings, octet strings and restricted character strings are always primitive, never split into constructed pieces (10.2).
- The components of a SET appear in X.680’s canonical tag order: universal, then application, then context-specific, then private, each by ascending tag number (10.3; X.680 8.6).
- A SET or SEQUENCE component equal to its DEFAULT is not encoded (11.5).
- The elements of a SET OF are in ascending order of their encodings, compared as octet strings with the shorter padded with zero octets for the comparison (11.6).
- Unused bits of a BIT STRING are zero, and where X.680 22.7 applies (named-bit lists) trailing zero bits are removed (11.2).
Example
00000000 30 82 03 5A 30 82 02 42 A0 03 02 01 02 02 14 … 0..Z0..B.......
Reading it: a SEQUENCE of 858 bytes (the certificate); inside it a SEQUENCE of 578
bytes (the to-be-signed part); a context tag [0] of 3 bytes holding the
INTEGER 2, which is version 3; then an INTEGER of 20 bytes, the serial number. See
certificates for the rest.