Home → ASN.1

ASN.1 Basics

ASN.1 stands for Abstract Syntax Notation One.

ASN.1 allows to describe complex data structures independently of any particular programming language. The ASN.1 compiler would take these ASN.1 specifications and produce a set of target language (C, C++, Java) files which would contain the native type definitions for these abstractly specified structures, and also generate a code which would perform the conversions of these structures into/from a series of bytes (serialization/deserialization), (presumably, these routines would be useful if the structure is going to be transferred over the network or written to an external media).

There are multiple data encodings developed for ASN.1. The most widely used ones are BER (Basic Encoding Rules), CER (Canonical-), DER (Distinguished Encoding Rules), PER (Packed Encoding Rules) and XER (XML Encoding Rules).

Let's say you want to transmit a certain structure between computers. That structure could include a string tag, a couple of integer values (one of them optional), and a set of floating-point values. The structure can be described using ASN.1 notation as follows:

        CertainStructure ::= SEQUENCE {
                tag     VisibleString,
                val1    INTEGER,
                val2    INTEGER   OPTIONAL,
                reals   SET OF REAL
This specification can be fed into the asn1c compiler (), which would produce the C language files with headers containing something like that:
        typedef struct CertainStructure {
                VisibleString_t  tag;
                long             val1;
                long            *val2;        /* OPTIONAL */
                A_SET_OF(double) reals;
        } CertainStructure_t;
The contents of a variable of this type may be directly manipulated. But the most important thing, the serialization and deserialization process to convert this complex structure into (from) a series of bytes, becomes a snap.

Here is how such structure may be received from the network, edited, and then converted back into the compact binary encoding:

        CertainStructure_t *cs = 0;
        ber_decode(0, &asn_DEF_CertainStructure, &cs, buffer, buffer_length);
        cs->val1 = 123;        /* Modify the contents */
        der_encode(&asn_DEF_CertainStructure, cs, write_handle, 0);
(The meaning of write_handle() is described in the asn1c compiler's usage guide, check the Documentation).

And here's how easy the contents of this structure can be printed out in XML format:

        xer_fprint(stdout, &asn_DEF_CertainStructure, cs);
Here's an example of what xer_fprint() might generate:
                <tag>This is a random tag</tag>
The ASN.1 notation is extensively used in telecom industry (examples include cell networks' MMS (multimedia services) and telecom equipment CDR (Call Data Records), TAP3 (Transferred Account Procedure)), security protocols and data formats (PKIX/X.509) and other high-profile applications. If you are using SSL (HTTPS) to access a bank or email account, be sure the ASN.1 is involved too; it describes the structure of the server's identity certificate for verification by your Internet browser.

The ASN.1 Compiler Copyright © 2003—2017 Lev Walkin <vlm@lionet.info>