Speed

Tag, Length, Value Nest

A DER element is a tag byte, a length and that many bytes of value; a length of 128 or more is written in extra bytes after a byte that says how many, and a constructed value is itself a run of elements.

The question

A certificate looks like a block of random bytes, but it follows one rule everywhere. Each piece starts with a tag that says what it is, then a length that says how many bytes follow, then the value itself. How are those lengths written, and how does a program find where the next piece starts?

This lesson walks the top level of the leaf test certificate by hand and with a program. The certificate is test material with a 512-bit RSA key, a toy size far too small to use. What follows is measured on these certificates; the DER standard itself is not quoted here.

Work it by hand

The leaf certificate starts 30 82 01 f0. The tag 30 is a SEQUENCE, a value that holds other elements. The length byte 82 is at least 128, so it does not give the length: it says that 2 length bytes follow, 01 f0. That is 1 x 256 + 240 = 496. The header is 4 bytes, tag, 82 and two length bytes, and 496 bytes of value follow it.

The first child starts right after the header, at offset 4: 30 82 01 9a. Again 2 length bytes: 0x019a = 410, so it takes 4 + 410 = 414 bytes and ends at 418.

At 418 the bytes are 30 0d: the length byte 0d is below 128, so it is the length, 13, and the header is 2 bytes. That element ends at 418 + 2 + 13 = 433. At 433 the bytes are 03 41: a BIT STRING of 65 bytes, ending at 500, the end of the certificate.

Run it

The demo reads the hex of a certificate and writes one row for each child of the top SEQUENCE: its offset, tag, header length and content length. Line 3 skips the header of the top element. The loop of lines 5 to 18 is bounded at 12 children; line 7 reads the length byte, line 8 the header length and lines 11 to 13 build the length from the length bytes. Line 19 returns the number of rows.

The picture shows the leaf: three rows, 4, 418 and 433. Measured with the OpenSSL command asn1parse on the same certificate, its depth-1 lines read 4:d=1 hl=4 l= 410, 418:d=1 hl=2 l= 13 and 433:d=1 hl=2 l= 65; the command is not part of this page.

A table of three rows with the columns offset, tag (hex), header and length for the children of the top SEQUENCE of the leaf test certificate: 4, 30, 4, 410; 418, 30, 2, 13; 433, 03, 2, 65. The program returns the number of rows, 3.
Watch the steps (recorded video)

Public test material, generated for this lesson. Never use it for anything real.

let n = hex_len(der);
let top = hex_u8(der, 1);
let at = 2 + bits_of(top, 0, 7) * (top / 128);
let rows = str_list();
for (k in range(0, 12)) {
  if (at < n) {
    let first = hex_u8(der, at + 1);
    let header = 2 + bits_of(first, 0, 7) * (first / 128);
    let length = first;
    if (first > 127) { length = 0; }
    for (j in range(0, header - 2)) {
      length = length * 256 + hex_u8(der, at + 2 + j);
    }
    let tag = u8_hex(hex_u8(der, at));
    rows.add(str(at) + "|" + tag + "|" + str(header) + "|" + str(length));
    at = at + header + length;
  }
}
let answer = rows.len();
lmcode_return_put("rows", rows);
{
  "answer": 3,
  "rows": [
    "4|30|4|410",
    "418|30|2|13",
    "433|03|2|65"
  ]
}

You can replace der by the hex of another certificate or by any element that follows the rule. Measured: a text shorter than two bytes stops line 2 with byte 1 is past the end of the data; more than 12 children show 12 rows; a length byte of 80 hex, which is not DER, is read as zero: for 30 07 30 80 02 01 01 00 00, whose top element has one child, the demo shows three rows (offsets 2, 4 and 7).

Your turn

The program below should read one element at a given place. der is hex, at is the offset of the element in the bytes. It returns a list of three numbers: the tag byte as a decimal number, the length of the header in bytes and the length of the value. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on six others.

The first 8 bytes of a constructed element, 30 82 01 00 followed by data bytes, the offset 0, and the list of three numbers that the shown program returns.

Public test material, generated for this lesson. Never use it for anything real.

let first = hex_u8(der, at + 1);
let header = 2;
let length = first;
if (first == 129) { header = 3; length = hex_u8(der, at + 2); }
let answer = int_list();
answer.add(hex_u8(der, at));
answer.add(header);
answer.add(length);
{
  "answer": [
    48,
    2,
    130
  ]
}

der is hex and at is the offset of a DER element in its bytes. Return the list [tag, header length, content length]: the tag byte as a decimal number, the number of header bytes (the tag byte and the length bytes: 2, 3 or 4 in all), and the length of the content. A length below 128 is written in one byte; 81 nn gives the length in one more byte and 82 nn nn in two more.

Read the result carefully

The walkers handle a length written in one byte, 81 nn and 82 nn nn; longer lengths are not handled, nor a tag number of 31 or more (more than one tag byte), nor the indefinite length 80, which DER does not allow. The page does not check that the value really has that many bytes. This is a reading of the bytes of these test certificates, not a DER parser.

The numbers 4, 410, 418, 433 and 500 belong to the leaf certificate; another certificate has other offsets. A tag is shown in hex in the picture and in decimal in the answer: 48 is the byte 30.

Recap

  • A DER element is a tag, a length and that many bytes of value.
  • A length below 128 is the length; 81 nn and 82 nn nn say that 1 or 2 length bytes follow, so the header is 2, 3 or 4 bytes.
  • A constructed element holds other elements, and the next one starts at offset plus header plus length.
  • The leaf certificate is 500 bytes: a SEQUENCE of 496 with children at 4, 418 and 433.