What Is BER (Basic Encoding Rules)

This section introduces the BER (Basic Encoding Rules) defined in the ASN.1 standard. It uses the TLV (Tag-Length-Value) format to encode a value of a given data type.

What Is BER (Basic Encoding Rules)? BER is a set of encoding/decoding rules provided in the ANS.1 standard that allows you to serialize instance values of ASN.1 data types into byte sequences to transmit them to other systems, or save them to files.

BER uses the TLV (Tag-Length-Value) format to encode a value of a given data type. Each section in TLV format is described below:

Tag Encoding Section - The Tag section encodes 3 pieces of information about the data type, Class, Complexity and Tag:

Here are the main BER encoding rules for the Tag section:

1. The first 2 bits of the first byte is used to encoding the data type class:

00...... - For UNIVERSAL
01...... - For APPLICATION
10...... - For CONTEXT-SPECIFIC
11...... - For PRIVATE

2. The third bit of the first byte is used to encoding the data type complexity:

..0..... - For PRIMITIVE
..1..... - For CONSTRUCTED

3. If the data type tag is <= 30, it will be encoded in the remaining 5 bits of the first byte. For example:

...00001 - For BOOLEAN (1)
...00010 - For INTEGER (2)
...00110 - For OBJECT IDENTIFIER (6)
...01011 - For UTF8String (11)
...10000 - For SEQUENCE (16)

4. If the data type tag is >= 31 and <= 127, it will be encoded in the last 7 bits of the second byte. The remaining 5 bits of the first byte are set to 1, and the first bit of the second byte is set to 0. For example:

...11111 00100010 - For DURATION (34)

5. If the data type tag is >= 128, more bytes will be added. The first bit of additional bytes is set to 1, except the last byte, which has 0 in the first bit to end the Tag section of the encoding. For example:

...11111 1....... 0....... - 14 bits to encode the type tag

For commonly used types in UNIVERSAL class, only one byte is needed for the tag encoding section as shown below:

Tag Byte          Type Tag   Type Name
---------------   --------   ---------
00000001 (0x01)          1   BOOLEAN
00000010 (0x02)          2   INTEGER
00000011 (0x03)          3   BIT STRING
00000100 (0x04)          4   OCTET STRING
00000101 (0x05)          5   NULL
00000110 (0x06)          6   OBJECT IDENTIFIER
00001001 (0x09)          9   REAL
00001010 (0x0a)         10   ENUMERATED
00001011 (0x0b)         11   UTF8String
00001110 (0x0e)         14   TIME
00110000 (0x30)         16   SEQUENCE
00110001 (0x31)         17   SET
00010011 (0x13)         19   PrintableString
00010110 (0x16)         22   IA5String

Length Encoding Section - The Length section uses one or more bytes to encode the number of bytes in the Value encoding section.

Here are the main BER encoding rules for the Length section:

1. If the length <= 127, it can be encoded in the short form or the long form.

2. If the length >= 128, it will be encoded in the long form.

In the short form, the length is encoded in the last 7 bits of the first byte. The first bit of first byte is set to 0. For example:

00100010 - For length of 34 bytes in the Value encoding section.

In the long form, the length is encoded in additional bytes. The number of addtional bytes will be encoded in the last 7 bits of the first byte. The first bit of the first byte is set to 1. For example:

10000010 ........ ........ - 16 bits to encode the value length

Value Encoding Section - The Value section uses zero or more bytes to encode the data value.

Rules to encode values are different for different date types. Some examples are list below:

Conclusion: DER only encode data values and their type class, complexity and tag. It does not encode type names and memeber IDs in constructed values. The next tutorials for encoding examples.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of PKI Certificate

PKI Certificate File Formats

 What Is ASN.1 (Abstract Syntax Notation One)

What Is BER (Basic Encoding Rules)

 BER Encoding Examples

 ASN.1 Type Modifier - Type Tagging

 What Is DER (Distinguished Encoding Rules)

 PKI Certificate Structure in ASN.1 Notations

 PKI Certificate in DER Format

 PKI Certificate in Base64 Format

 PKI Certificate in PEM Format

 PKI Certificate File Viewer and Decoder

 PKI Certificate File ASN.1 Parser

 Certificate Wrapped in PKCS7 Formats

 Certificate Wrapped in PKCS12 Formats

 Certificate File Format Summary

 OpenSSL - Cryptography Toolkit

 "openssl ca" - CA (Certificate Authority) Tool

 Java "keytool" Commands and KeyStore Files

 PKI Certificate Store

 PKCS12 Certificate Bundle File

 PKCS7 Certificate Chain File

 PKI Certificate Related Terminology

 References

 Full Version in PDF/EPUB