opyeb asn1lab

asn1lab · Basics

DER, BER and PEM: encoding rules and the envelope

TLV, short and long lengths, indefinite length, DER strictness, PEM envelope

Open a certificate file and you see numbers such as 30 82 02 35 instead of readable text. They are values defined in ASN.1 and encoded with the BER or DER rules. ASN.1 defines the shape of a value; BER and DER turn that value into bytes; PEM wraps those bytes back into text.

TLV: tag, length, value

Every BER or DER value has three parts: the tag (what kind of value it is), the length (how many value bytes follow), and the value itself. 02 01 05 is INTEGER (tag 0x02), length 1, value 5.

PartMeaningExample
Tag2 class bits, 1 constructed bit, 5 tag-number bits0x30 = SEQUENCE
LengthNumber of value bytes, short or long form0x03, 0x82 0x02 0x35
ValueThe content, or a run of child TLVs02 01 05

The top two bits of the tag give the class: Universal (0) for standard types such as SEQUENCE and INTEGER, Application (1) for application standards, Context-specific (2) for tags a surrounding structure defines, and Private (3). If the fifth bit is 1 the value is constructed (it contains child TLVs); if 0 it is primitive.

Lengths: short, long and indefinite

30 80 … child TLVs … 00 00 ← BER indefinite-length SEQUENCE

Where DER is stricter than BER

DER is a subset of BER. It narrows the rules so that a value has exactly one byte representation, which is what you want when those bytes are signed: change a byte and the signature breaks.

DER can always be read as BER, but not the other way round. Converting BER to DER means re-encoding; cutting or concatenating bytes will not do it.

PEM is the envelope

PEM is DER bytes in Base64 wrapped by -----BEGIN X----- and -----END X-----. Sixty-four characters per line is the convention, and extra headers such as Proc-Type: may appear between the markers.

The label describes the content but does not verify it. A block labelled CERTIFICATE may not hold a certificate structure at all, so decode it and check the first tags and fields.

Signs of a damaged file

asn1lab reports these cases with an error code and an offset. Knowing the byte where the arithmetic broke lets you decide quickly whether to fetch the file again or fix the encoding.

Questions

Which is the real content, PEM or DER?

They are the same content. PEM is DER wrapped in Base64, so decoding the body gives exactly the DER bytes. asn1lab accepts both and draws the same tree.

Why does indefinite length exist?

When a BER stream is produced, the total length is sometimes not known in advance. Old protocols used it; files are usually converted to definite lengths. DER and modern JSON-based formats never use it.

Does every file starting with 0x30 contain a certificate?

No. 0x30 is only SEQUENCE, and certificates, public keys, private keys and signatures all start with one. You have to read the first children to know which.

Paste the bytes and find the tag and length yourself.

Open the tool

This guide follows ITU-T X.690 (DER/BER), RFC 5280 (X.509), RFC 5208 (PKCS#8), RFC 5480 (EC public keys), RFC 5915 (EC private keys) and RFC 3279 (ECDSA signatures). Every example is synthetic.