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 | 3D | CHARACTER STRING (X.680 44.2); always constructed, because X.690 8.24.1 encodes it as the SEQUENCE of X.680 44.5 |
| 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 INSTANCE OF),
EMBEDDED PDV and CHARACTER STRING are always constructed (8.9–8.12, 8.16, 8.17, 8.18,
8.24.1), so their bytes are 30, 31, 28,
2B and 3D; the others are shown primitive, which DER
requires for strings (10.2). ObjectDescriptor is encoded as if it were
[UNIVERSAL 7] IMPLICIT GraphicString (X.690 8.25, X.680 48.3).
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. |
| REAL | Always primitive (8.5.1). Plus zero has no contents octets (09 00, 8.5.2). Otherwise bit 8 of the first contents octet chooses the form (8.5.6): 1 binary; 00 decimal; 01 a special value. Binary (8.5.7): bit 7 is the sign S (1 = negative); bits 6–5 the base (00 2, 01 8, 10 16, 11 reserved); bits 4–3 the binary scaling factor F, 0 to 3; bits 2–1 the exponent format: 00 exponent in the next 1 octet, 01 next 2, 10 next 3, all two’s complement, 11 the next octet gives the exponent’s length X (at least 1) and the exponent follows in X octets. The remaining octets are the unsigned integer N, and the value is S × N × 2F × baseexponent. Decimal (8.5.8): bits 6–1 of the first octet are 000001 ISO 6093 NR1, 000010 NR2 or 000011 NR3 (other values reserved); the rest is the number as ISO 6093 text, with no scaling factor. Special (8.5.9): a single octet, 40 PLUS-INFINITY, 41 MINUS-INFINITY, 42 NOT-A-NUMBER, 43 minus zero (others with bits 8–7 = 01 reserved). DER (11.3): a base-2 value uses binary encoding in base 2 with F = 0, the mantissa N zero or odd and M and E each in the fewest octets; a base-10 value uses NR3 with no spaces, a leading - only for negatives, no zero as first or last mantissa digit, a full stop then E after the mantissa, and the exponent +0 if zero, otherwise without a leading zero or plus sign. |
| RELATIVE-OID | Primitive. The same sub-identifier encoding as OBJECT IDENTIFIER, but every arc is its own sub-identifier, with no 40 × first + second packing (8.20). {8571 3 2} is 0D 04 C2 7B 03 02. |
| OID-IRI, RELATIVE-OID-IRI | Primitive. The UTF-8 text of the lexical items of the XML value notation (X.680 34.3, 35.3), with no white space between them, no announcers or escape sequences, each character in the fewest octets (8.21, 8.22). |
| TIME and the useful time types | Primitive, UTF-8 text of the value notation without the surrounding quotation marks (8.26). DATE also drops every hyphen-minus (8.26.2); TIME-OF-DAY drops every colon (8.26.3); DATE-TIME drops hyphens, colons and the letter T (8.26.4); DURATION drops the letter P (8.26.5). In DER and CER (11.9) the notation is first put in canonical form: decimal commas become full stops; a time-difference component of a whole number of hours loses its minutes; in an interval whose end point has the same time difference as the start point, the end point’s is removed; and in a duration, zero components are removed except the least significant one present. |
| Tagged (prefixed) types | Explicit tagging: constructed, and the contents are the complete encoding of the base type (8.14.3). Implicit tagging: the base encoding with its identifier octets replaced, constructed if the base encoding is, and the contents unchanged (8.14.4). With Type1 ::= VisibleString and the value “Jones”: Type1 is 1A 05 4A 6F 6E 65 73; [APPLICATION 3] IMPLICIT Type1 is 43 05 4A 6F 6E 65 73; [2] Type2 (explicit) is A2 07 43 05 4A 6F 6E 65 73; [2] IMPLICIT Type2 is 82 05 4A 6F 6E 65 73 (X.690 8.14.4 example). |
| CHOICE | The encoding of the chosen value alone, using the chosen type’s tag; there is no extra wrapper (8.13). Whether it is primitive or constructed depends on the chosen type. |
| SEQUENCE, SEQUENCE OF, SET, SET OF | Constructed; the contents are the complete encodings of the components in order (8.9–8.12). A SEQUENCE OF keeps its order (8.10.3); a SET OF’s order is not preserved by BER (8.12.3) but DER sorts it (11.6). A component absent through OPTIONAL or DEFAULT is simply not present (8.9.3), and DER leaves out any equal to its default (11.5). |
| EXTERNAL, INSTANCE OF | Constructed. The BER encoding of [UNIVERSAL 8] IMPLICIT SEQUENCE { direct-reference OBJECT IDENTIFIER OPTIONAL, indirect-reference INTEGER OPTIONAL, data-value-descriptor ObjectDescriptor OPTIONAL, encoding CHOICE { single-ASN1-type [0] ABSTRACT-SYNTAX.&Type, octet-aligned [1] IMPLICIT OCTET STRING, arbitrary [2] IMPLICIT BIT STRING } } in an explicit-tagging environment (8.18.1). The identification alternatives map to direct and indirect references by X.690 Table 2: syntax is direct-reference only; presentation-context-id is indirect-reference only; context-negotiation sets both (its transfer-syntax as direct-reference); syntaxes, transfer-syntax and fixed cannot occur (8.18.4). A single-ASN.1-type value may use any of the three encoding choices; a value whose encoding is a whole number of octets may use octet-aligned or arbitrary; otherwise arbitrary (8.18.6–8.18.8). INSTANCE OF is [UNIVERSAL 8] IMPLICIT SEQUENCE { type-id <class>.&id, value [0] EXPLICIT <class>.&Type } (8.16.1). |
| EMBEDDED PDV, CHARACTER STRING | Constructed. The BER encoding of the associated SEQUENCE type of X.680 36.5 (EMBEDDED PDV: identification CHOICE of syntaxes { abstract, transfer }, syntax, presentation-context-id, context-negotiation { presentation-context-id, transfer-syntax }, transfer-syntax, fixed NULL; then optional data-value-descriptor ObjectDescriptor and data-value OCTET STRING) and 44.5 (CHARACTER STRING: the same identification, the optional descriptor and string-value OCTET STRING) (8.17, 8.24). The OCTET STRING holds the value in the identified transfer syntax. |
| ObjectDescriptor | Encoded as a GraphicString with tag 7: octets of the registered character sets (8.25, 8.23.5). |
| 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.