ASN.1 Made Simple — Encoding Rules

ASN.1 has sets of rules precisely specifying how messages must be "encoded" for communication with other machines. Each set of "encoding rules" has specific characteristics, such as compactness or decoding speed, which make it best suited for particular environments. Note that all of the encoding rules are able to represent any messages you would like to exchange.


The oldest encoding rules, the Basic Encoding Rules (BER), use a Tag-Length-Value (TLV) format for encoding all information. BER always sends a tag to indicate what kind of data follows, then a length indicating the length of the data that follows, then the value which is the actual data. Two related encoding rules, the Distinguished Encoding Rules (DER) and the Canonical Encoding Rules (CER) are subsets of BER which eliminate some of the extra flexibility provided by BER. Note that DER is commonly used in security-related applications such as X.509 digital certificates.


The fastest ASN.1 encoding rules are the Octet Encoding Rules (OER). The Octet Encoding Rules (OER), like the Packed Encoding Rules (PER), produce compact encodings by taking advantage of information present in the ASN.1 schema to limit the amount of information included in each encoded message. However, in contrast to PER, OER favors encoding/decoding speed and ease of implementation over compactness of the encodings. Unlike PER Aligned, OER is completely octet-oriented. Whereas in PER Aligned a fixed-size encoding occupying less than 8 bits may begin at any bit position within an octet and may end at any bit position within the same or another octet, in OER the encoding of every possible value of every ASN.1 type occupies a whole number of octets, and all the fields of the encoding are octet-aligned. OER is used in Intelligent Transportation protocols and is highly suited for any protocol for which the speed of encoding/decoding is essential.


The most compact encoding rules are the Packed Encoding Rules (PER). PER differs from BER in that PER does not send the Tag of the TLV since the order in which components of the message occur is known. PER also does not send the Length of the TLV if the Value has a fixed length. PER also uses additional information from the ASN.1 message description to eliminate redundant information from the Value portion of the TLV, thus making PER messages quite compact and suitable for environments in which bandwidth conservation is important. There are two ways to encode messages in PER-Aligned and Unaligned. With the Aligned PER, a field can be aligned to 8-bit octet boundaries by inserting padding bits. With the Unaligned PER, pad bits are never inserted between fields. Unaligned PER is commonly used in 3GPP cellular technologies such as UMTS (3G) or LTE (4G) for protocols like RANAP, NBAP or RRC.


The least compact of the encoding rules, the XML Encoding Rules (XER), uses a textual XML format for its encodings of the form <start-tag> value <end-tag>. The XER and E-XER (Extended XER) differ in that the default encodings produced by E-XER are more suitable for exchanging information with XML Schema Definition (XSD) engines. Note that the same ASN.1 specification can be used with multiple encoding rules, which means that a PER message from a cell phone could be converted to E-XER for display and manipulation in a web browser.


The JSON encoding rules (JER), compared to XER or E-XER, provide a more compact and easy-to-use encoding method. The encoding of ASN.1 types using JER is based on the JSON specification (ECMA-404/RFC-8259). JER combines the advantages of the popular JSON data format and the benefits of ASN.1: ASN.1 schemas are easier to read and can be used to validate regular JSON messages, ASN.1 endpoints can communicate with JSON endpoints (the encoding of primitive types matches the encoding of JSON messages), etc. JER enables ASN.1 users to easily debug and troubleshoot ASN.1 protocols. The following example shows a JSON message with two variants of an ASN.1 schema:

JSON message ASN.1 Schema (auto-generated by JSON2ASN) ASN.1 schema
"itemCode": 10234,
"color": "Brown",
GeneratedType ::= SEQUENCE {
 itemCode     INTEGER,   
 color        UTF8String,
 power        INTEGER,   
 deliveryTime INTEGER,   
 quantity     INTEGER,   
 unitPrice    REAL,      
 isTaxable    BOOLEAN    
Item ::= SEQUENCE {                                     
 itemCode     INTEGER (1..99999),                     
 color        UTF8String ("Black" | "Blue" 
              | "Brown"),
 power        INTEGER (110 | 220),                    
 deliveryTime INTEGER (8..12 | 14..19),               
 quantity     INTEGER (1..1000),                      
 unitPrice    REAL (1.00 .. 9999.00),                 

For more information about the ASN.1 encoding rules, see the ASN.1 Quick Reference section.