Skip to content
ASN.1 / DER Encoding
step 1/5

Reading — step 1 of 5

Read

~1 min readX.509 & PKI Foundations

ASN.1 / DER Encoding

ASN.1 is the schema language; DER is the binary encoding (Distinguished Encoding Rules).

DER encodes things as TLV — Type, Length, Value:

[TAG (1+ bytes)] [LENGTH (1+ bytes)] [VALUE (length bytes)]

Tags (single byte for simple types):

  • 02 INTEGER
  • 03 BIT STRING
  • 04 OCTET STRING
  • 05 NULL
  • 06 OBJECT IDENTIFIER (OID)
  • 0C UTF8String
  • 13 PrintableString
  • 17 UTCTime
  • 18 GeneralizedTime
  • 30 SEQUENCE (combined: 30 = constructed | tag 16)
  • 31 SET (constructed | 17)

Length encoding:

  • 0..127: single byte.
  • 128+: first byte = 0x80 | num_length_bytes; following bytes = length big-endian.
INTEGER 100   = 02 01 64
INTEGER 1000  = 02 02 03 E8
SEQUENCE { INTEGER 1, INTEGER 2 } = 30 06 02 01 01 02 01 02

OIDs are dot-notation identifiers encoded as bytes:

  • 1.2.840.113549.1.1.11 = sha256WithRSAEncryption
  • 1.2.840.10045.2.1 = ecPublicKey
  • 2.5.29.17 = subjectAltName

OID encoding:

  • First two arcs combined: 40*X + Y.
  • Subsequent: base-128 with high bit set on continuation bytes.
  • 1.2.840.113549.1.1.11 → 2A 86 48 86 F7 0D 01 01 0B

DER quirks:

  • INTEGER must be minimum bytes (no leading 0x00 unless to keep sign positive).
  • BIT STRING includes "unused bits" count as first byte.
  • SET OF is sorted lexicographically.

Parse with care: x.509 has been a wellspring of CVEs. Use battle-tested libraries.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…