DER, BER and PEM: encoding rules and the 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.
| Part | Meaning | Example |
|---|---|---|
| Tag | 2 class bits, 1 constructed bit, 5 tag-number bits | 0x30 = SEQUENCE |
| Length | Number of value bytes, short or long form | 0x03, 0x82 0x02 0x35 |
| Value | The content, or a run of child TLVs | 02 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
- Short form: if the first byte is 0x7F or less, it is the length. 0x03 means three bytes.
- Long form: 0x81 to 0x84 means the next one to four bytes hold the length. 0x82 0x02 0x35 is 565 bytes.
- Indefinite: 0x80 omits the length and the children run until an end-of-contents marker (00 00). Only BER allows this.
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.
- Lengths are always definite. DER has no indefinite length and no EOC.
- Length bytes use the minimum count: 0x81 0x03, never 0x82 0x00 0x03.
- BOOLEAN true is fixed at 0xFF. BER accepts any non-zero byte.
- INTEGER uses the shortest two's-complement form, with no redundant leading 0x00 or 0xFF.
- BIT STRING sets the unused bits of the final byte to zero.
- SET OF sorts its members by their encoded bytes.
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.
CERTIFICATE: one X.509 certificate.PUBLIC KEY: a SubjectPublicKeyInfo (SPKI).PRIVATE KEY: an unencrypted PKCS#8 private key.ENCRYPTED PRIVATE KEY: an encrypted PKCS#8 key.RSA PRIVATE KEY: PKCS#1;EC PRIVATE KEY: SEC1 (RFC 5915).
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
- A length larger than the bytes that remain: a truncated download or a lossy copy.
- Unparsed bytes at the end: several PEM blocks concatenated, or explanatory text mixed in.
- Child lengths that do not add up to the parent: a length field edited by hand.
- Indefinite length with no EOC: the BER stream stopped early.
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 toolThis 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.