The Asn1 application provides:
ASN.1 (Abstract Syntax Notation One) is a formal language for describing data structures to be exchanged between distributed computer systems. The purpose of ASN.1 is to have a platform and programming language independent notation to express types using a standardized set of rules for the transformation of values of a defined type into a stream of bytes. This stream of bytes can then be sent on any type of communication channel. This way, two applications written in different programming languages running on different computers with different internal representation of data can exchange instances of structured data types.
It is assumed that the reader is familiar with the ASN.1 notation as documented in the standard definition [] which is the primary text. It may also be helpful, but not necessary, to read the standard definitions [] [] [] [] [].
A good book explaining those reference texts is
[], which is free to download at
This application covers all features of ASN.1 up to the 1997 edition of the specification. In the 2002 edition of ASN.1 a number of new features were introduced. The following features of the 2002 edition are fully or partly supported as shown below:
Decimal notation (e.g., "1.5e3") for REAL values. The NR1, NR2 and NR3 formats as explained in ISO6093 are supported.
The RELATIVE-OID type for relative object identifiers is fully supported.
The subtype constraint (CONTAINING/ENCODED BY) to constrain the content of an octet string or a bit string is parsed when compiling, but no further action is taken. This constraint is not a PER-visible constraint.
The subtype constraint by regular expressions (PATTERN) for character string types is parsed when compiling, but no further action is taken. This constraint is not a PER-visible constraint.
Multiple-line comments as in C,
The following example demonstrates the basic functionality used to run the Erlang ASN.1 compiler.
Create a file called
People DEFINITIONS AUTOMATIC TAGS ::= BEGIN Person ::= SEQUENCE { name PrintableString, location INTEGER {home(0),field(1),roving(2)}, age INTEGER OPTIONAL } END
This file (
The generated Erlang files will be placed in the current directory or
in the directory specified with the
1> asn1ct:compile("People", [ber]). ok 2>
The
2> asn1ct:compile("People", [ber,verbose]). Erlang ASN.1 compiling "People.asn" --{generated,"People.asn1db"}-- --{generated,"People.hrl"}-- --{generated,"People.erl"}-- ok 3>
The ASN.1 module
or
Assume there is a network application which receives instances of the ASN.1 defined type Person, modifies and sends them back again:
receive
{Port,{data,Bytes}} ->
case 'People':decode('Person',Bytes) of
{ok,P} ->
{ok,Answer} = 'People':encode('Person',mk_answer(P)),
Port ! {self(),{command,Answer}};
{error,Reason} ->
exit({error,Reason})
end
end,
In the example above, a series of bytes is received from an
external source and the bytes are then decoded into a valid
Erlang term. This was achieved with the call
The encoder and the decoder can also be run from
the shell.
2> Rockstar = {'Person',"Some Name",roving,50}. {'Person',"Some Name",roving,50} 3> {ok,Bin} = 'People':encode('Person',Rockstar). {ok,<<243,17,19,9,83,111,109,101,32,78,97,109,101,2,1,2, 2,1,50>>} 4> {ok,Person} = 'People':decode('Person',Bin). {ok,{'Person',"Some Name",roving,50}} 5>
It is common that asn1 modules import defined types, values and other entities from another asn1 module.
Earlier versions of the asn1 compiler required that modules that were imported from had to be compiled before the module that imported. This caused problems when asn1 modules had circular dependencies.
Now are referenced modules parsed when the compiler finds an entity that is imported. There will not be any code generated for the referenced module. However, the compiled module rely on that the referenced modules also will be compiled.
The Asn1 application provides two separate user interfaces:
The module
The module
The reason for the division of the interface into compile-time
and run-time
is that only run-time modules (
The ASN.1 compiler can be invoked directly from the command-line
by means of the
erlc Person.asn erlc -bper Person.asn erlc -bber ../Example.asn erlc -o ../asnfiles -I ../asnfiles -I /usr/local/standards/asn1 Person.asn
The useful options for the ASN.1 compiler are:
Choice of encoding rules, if omitted
Where to put the generated files, default is the current directory.
Where to search for
DER encoding rule. Only when using
This functionality works together with the flags
A buffer that holds a message, being decoded may
also have some following bytes. Now it is possible to get
those following bytes returned together with the decoded
value. If an asn1 spec is compiled with this option a tuple
You may add any option to the Erlang compiler when compiling the generated Erlang files. Any option unrecognised by the asn1 compiler will be passed to the Erlang compiler.
For a complete description of
The compiler and other compile-time functions can also be invoked from
the Erlang shell. Below follows a brief
description of the primary functions, for a
complete description of each function see
The compiler is invoked by using
asn1ct:compile("H323-MESSAGES.asn1").
which equals:
asn1ct:compile("H323-MESSAGES.asn1",[ber]).
If one wants PER encoding:
asn1ct:compile("H323-MESSAGES.asn1",[per]).
The generic encode and decode functions can be invoked like this:
'H323-MESSAGES':encode('SomeChoiceType',{call,"octetstring"}). 'H323-MESSAGES':decode('SomeChoiceType',Bytes).
When an ASN.1 specification is compiled with the
By invoking the function
Errors detected at compile time appear on the screen together with a line number indicating where in the source file the error was detected. If no errors are found, an Erlang ASN.1 module will be created as default.
The run-time encoders and decoders execute within a catch and
returns
There are various reasons for using a multi file compilation:
You need to specify which asn1 specs you will
compile in a module that must have the extension
File1.asn File2.asn File3.asn
If you compile with:
~> erlc MyModule.set.asn
the result will be one merged module
Tags used to be important for all users of ASN.1, because it was necessary to manually add tags to certain constructs in order for the ASN.1 specification to be valid. Here is an example of an old-style specification:
Tags DEFINITIONS ::= BEGIN Afters ::= CHOICE { cheese [0] IA5String, dessert [1] IA5String } END
Without the tags (the numbers in square brackets) the ASN.1 compiler would refuse to compile the file.
In 1994 the global tagging mode AUTOMATIC TAGS was introduced. By putting AUTOMATIC TAGS in the module header, the ASN.1 compiler will automatically add tags when needed. Here is the same specification in AUTOMATIC TAGS mode:
Tags DEFINITIONS AUTOMATIC TAGS ::= BEGIN Afters ::= CHOICE { cheese IA5String, dessert IA5String } END
Tags will not be mentioned any more in this manual.
This section describes the ASN.1 types including their functionality, purpose and how values are assigned in Erlang.
ASN.1 has both primitive and constructed types:
Values of each ASN.1 type has its own representation in Erlang described in the following subsections. Users shall provide these values for encoding according to the representation, as in the example below.
Operational ::= BOOLEAN --ASN.1 definition
In Erlang code it may look like:
Val = true, {ok,Bytes} = MyModule:encode('Operational', Val),
Below follows a description of how values of each type can be represented in Erlang.
Booleans in ASN.1 express values that can be either
TRUE or FALSE.
The meanings assigned to TRUE or FALSE is beyond the scope
of this text.
In ASN.1 it is possible to have:
Operational ::= BOOLEAN
Assigning a value to the type Operational in Erlang is possible by using the following Erlang code:
Myvar1 = true,
Thus, in Erlang the atoms
ASN.1 itself specifies indefinitely large integers, and the Erlang systems with versions 4.3 and higher, support very large integers, in practice indefinitely large integers.
The concept of sub-typing can be applied to integers as well as to other ASN.1 types. The details of sub-typing are not explained here, for further info see []. A variety of syntaxes are allowed when defining a type as an integer:
T1 ::= INTEGER T2 ::= INTEGER (-2..7) T3 ::= INTEGER (0..MAX) T4 ::= INTEGER (0<..MAX) T5 ::= INTEGER (MIN<..-99) T6 ::= INTEGER {red(0),blue(1),white(2)}
The Erlang representation of an ASN.1 INTEGER is an integer or
an atom if a so called
Below is an example of Erlang code which assigns values for the above types:
T1value = 0, T2value = 6, T6value1 = blue, T6value2 = 0, T6value3 = white
The Erlang variables above are now bound to valid instances of ASN.1 defined types. This style of value can be passed directly to the encoder for transformation into a series of bytes.
The decoder will return an atom if the value corresponds to a symbol in the Named Number List.
In this version reals are not implemented. When they are, the following ASN.1 type is used:
R1 ::= REAL
Can be assigned a value in Erlang as:
R1value1 = 2.14, R1value2 = {256,10,-2},
In the last line note that the tuple {256,10,-2} is the real number
2.56 in a special notation, which will encode faster than simply
stating the number as 2.56. The arity three tuple is
Null is suitable in cases where supply and recognition of a value is important but the actual value is not.
Notype ::= NULL
The NULL type can be assigned in Erlang:
N1 = 'NULL',
The actual value is the quoted atom 'NULL'.
The enumerated type can be used, when the value we wish to describe, may only take one of a set of predefined values.
DaysOfTheWeek ::= ENUMERATED { sunday(1),monday(2),tuesday(3), wednesday(4),thursday(5),friday(6),saturday(7) }
For example to assign a weekday value in Erlang use the same atom
as in the
Day1 = saturday,
The enumerated type is very similar to an integer type, when defined with a set of predefined values. An enumerated type differs from an integer in that it may only have specified values, whereas an integer can also have any other value.
The BIT STRING type can be used to model information which
is made up of arbitrary length series of bits. It is intended
to be used for a selection of flags, not for binary files.
In ASN.1 BIT STRING definitions may look like:
Bits1 ::= BIT STRING Bits2 ::= BIT STRING {foo(0),bar(1),gnu(2),gnome(3),punk(14)}
There are two notations available for representation of BIT STRING values in Erlang and as input to the encode functions.
Example:
Bits1Val1 = <<0:1,1:1,0:1,1:1,1:1>>, Bits2Val1 = [gnu,punk], Bits2Val2 = <<2#1110:4>>, Bits2Val3 = [bar,gnu,gnome],
BIT STRINGS may also be sub-typed with, for example, a SIZE specification:
Bits3 ::= BIT STRING (SIZE(0..31))
This means that no bit higher than 31 can ever be set.
In addition to the representations described above, the
following deprecated representations are available if the
specification has been compiled with the
The OCTET STRING is the simplest of all ASN.1 types. The OCTET STRING only moves or transfers e.g. binary files or other unstructured information complying to two rules. Firstly, the bytes consist of octets and secondly, encoding is not required.
It is possible to have the following ASN.1 type definitions:
O1 ::= OCTET STRING O2 ::= OCTET STRING (SIZE(28))
With the following example assignments in Erlang:
O1Val = <<17,13,19,20,0,0,255,254>>, O2Val = <<"must be exactly 28 chars....">>,
By default, an OCTET STRING is always represented as
an Erlang binary. If the specification has been compiled with
the
ASN.1 supports a wide variety of character sets. The main difference between OCTET STRINGS and the Character strings is that OCTET STRINGS have no imposed semantics on the bytes delivered.
However, when using for instance the IA5String (which closely resembles ASCII) the byte 65 (in decimal notation) means the character 'A'.
For example, if a defined type is to be a VideotexString and an octet is received with the unsigned integer value X, then the octet should be interpreted as specified in the standard ITU-T T.100,T.101.
The ASN.1 to Erlang compiler will not determine the correct interpretation of each BER (Basic Encoding Rules) string octet value with different Character strings. Interpretation of octets is the responsibility of the application. Therefore, from the BER string point of view, octets appear to be very similar to character strings and are compiled in the same way.
It should be noted that when PER (Packed Encoding Rules) is used, there is a significant difference in the encoding scheme between OCTET STRINGS and other strings. The constraints specified for a type are especially important for PER, where they affect the encoding.
Please note that all the Character strings are supported and it is possible to use the following ASN.1 type definitions:
Digs ::= NumericString (SIZE(1..3)) TextFile ::= IA5String (SIZE(0..64000))
and the following Erlang assignments:
DigsVal1 = "456", DigsVal2 = "123", TextFileVal1 = "abc...xyz...", TextFileVal2 = [88,76,55,44,99,121 .......... a lot of characters here ....]
The Erlang representation for "BMPString" and "UniversalString" is either a list of ASCII values or a list of quadruples. The quadruple representation associates to the Unicode standard representation of characters. The ASCII characters are all represented by quadruples beginning with three zeros like {0,0,0,65} for the 'A' character. When decoding a value for these strings the result is a list of quadruples, or integers when the value is an ASCII character.
The following example shows how it works. We have the following
specification in the file
PrimStrings DEFINITIONS AUTOMATIC TAGS ::= BEGIN BMP ::= BMPString END
Encoding and decoding some strings:
1> asn1ct:compile('PrimStrings', [ber]). ok 2> {ok,Bytes1} = 'PrimStrings':encode('BMP', [{0,0,53,53},{0,0,45,56}]). {ok,<<30,4,53,54,45,56>>} 3> 'PrimStrings':decode('BMP', Bytes1). {ok,[{0,0,53,53},{0,0,45,56}]} 4> {ok,Bytes2} = 'PrimStrings':encode('BMP', [{0,0,53,53},{0,0,0,65}]). {ok,<<30,4,53,53,0,65>>} 5> 'PrimStrings':decode('BMP', Bytes2). {ok,[{0,0,53,53},65]} 6> {ok,Bytes3} = 'PrimStrings':encode('BMP', "BMP string"). {ok,<<30,20,0,66,0,77,0,80,0,32,0,115,0,116,0,114,0,105,0,110,0,103>>} 7> 'PrimStrings':decode('BMP', Bytes3). {ok,"BMP string"}
The UTF8String type is represented as a UTF-8 encoded binary in
Erlang. Such binaries can be created directly using the binary syntax
or by converting from a list of Unicode code points using the
Here are some examples showing how UTF-8 encoded binaries can be created and manipulated:
1> Gs = "Мой маленький Гном". [1052,1086,1081,32,1084,1072,1083,1077,1085,1100,1082,1080, 1081,32,1043,1085,1086,1084] 2> Gbin = unicode:characters_to_binary(Gs). <<208,156,208,190,208,185,32,208,188,208,176,208,187,208, 181,208,189,209,140,208,186,208,184,208,185,32,208,147, 208,...>> 3> Gbin = <<"Мой маленький Гном"/utf8>>. <<208,156,208,190,208,185,32,208,188,208,176,208,187,208, 181,208,189,209,140,208,186,208,184,208,185,32,208,147, 208,...>> 4> Gs = unicode:characters_to_list(Gbin). [1052,1086,1081,32,1084,1072,1083,1077,1085,1100,1082,1080, 1081,32,1043,1085,1086,1084]
See the
In the following example we will use this ASN.1 specification:
UTF DEFINITIONS AUTOMATIC TAGS ::= BEGIN UTF ::= UTF8String END
Encoding and decoding a string with Unicode characters:
5> asn1ct:compile('UTF', [ber]). ok 6> {ok,Bytes1} = 'UTF':encode('UTF', <<"Гном"/utf8>>). {ok,<<12,8,208,147,208,189,208,190,208,188>>} 7> {ok,Bin1} = 'UTF':decode('UTF', Bytes1). {ok,<<208,147,208,189,208,190,208,188>>} 8> io:format("~ts\n", [Bin1]). Гном ok 9> unicode:characters_to_list(Bin1). [1043,1085,1086,1084]
The OBJECT IDENTIFIER is used whenever a unique identity is required. An ASN.1 module, a transfer syntax, etc. is identified with an OBJECT IDENTIFIER. Assume the example below:
Oid ::= OBJECT IDENTIFIER
Therefore, the example below is a valid Erlang instance of the type 'Oid'.
OidVal1 = {1,2,55},
The OBJECT IDENTIFIER value is simply a tuple with the consecutive values which must be integers.
The first value is limited to the values 0, 1 or 2 and the second value must be in the range 0..39 when the first value is 0 or 1.
The OBJECT IDENTIFIER is a very important type and it is widely used within different standards to uniquely identify various objects. In [], there is an easy-to-understand description of the usage of OBJECT IDENTIFIER.
Values of this type can be assigned a value as an ordinary string i.e.
"This is the value of an Object descriptor"
Two different time types are defined within ASN.1, Generalized Time and UTC (Universal Time Coordinated), both are assigned a value as an ordinary string within double quotes i.e. "19820102070533.8".
In case of DER encoding the compiler does not check the validity of the time values. The DER requirements upon those strings is regarded as a matter for the application to fulfill.
The structured types of ASN.1 are constructed from other types
in a manner similar to the concepts of array and struct in C.
A SEQUENCE in ASN.1 is
comparable with a struct in C and a record in Erlang.
A SEQUENCE may be defined as:
Pdu ::= SEQUENCE { a INTEGER, b REAL, c OBJECT IDENTIFIER, d NULL }
This is a 4-component structure called 'Pdu'. The major format
for representation of SEQUENCE in Erlang is the record format.
For each SEQUENCE and
-record('Pdu',{a, b, c, d}).
The record declarations for a module
Values can be assigned in Erlang as shown below:
MyPdu = #'Pdu'{a=22,b=77.99,c={0,1,2,3,4},d='NULL'}.
The decode functions will return a record as result when decoding
a
A
An application can use the atom
Depending on the encoding rules, the encoder may also compare the given value to the default value and automatically omit the encoding if they are equal. How much effort the encoder makes to to compare the values depends on the encoding rules. The DER encoding rules forbids encoding a value equal to the default value, so it has a more thorough and time-consuming comparison than the encoders for the other encoding rules.
In the following example we will use this ASN.1 specification:
File DEFINITIONS AUTOMATIC TAGS ::= BEGIN Seq1 ::= SEQUENCE { a INTEGER DEFAULT 1, b Seq2 DEFAULT {aa TRUE, bb 15} } Seq2 ::= SEQUENCE { aa BOOLEAN, bb INTEGER } Seq3 ::= SEQUENCE { bs BIT STRING {a(0), b(1), c(2)} DEFAULT {a, c} } END
Here is an example where the BER encoder is able to omit encoding of the default values:
1> asn1ct:compile('File', [ber]). ok 2> 'File':encode('Seq1', {'Seq1',asn1_DEFAULT,asn1_DEFAULT}). {ok,<<48,0>>} 3> 'File':encode('Seq1', {'Seq1',1,{'Seq2',true,15}}). {ok,<<48,0>>}
And here is an example with a named BIT STRING where the BER encoder will not omit the encoding:
4> 'File':encode('Seq3', {'Seq3',asn1_DEFAULT). {ok,<<48,0>>} 5> 'File':encode('Seq3', {'Seq3',<<16#101:3>>). {ok,<<48,4,128,2,5,160>>}
The DER encoder will omit the encoding for the same BIT STRING:
6> asn1ct:compile('File', [ber,der]). ok 7> 'File':encode('Seq3', {'Seq3',asn1_DEFAULT). {ok,<<48,0>>} 8> 'File':encode('Seq3', {'Seq3',<<16#101:3>>). {ok,<<48,0>>}
In Erlang, the SET type is used exactly as SEQUENCE. Note that if the BER or DER encoding rules are used, decoding a SET is slower than decoding a SEQUENCE because the components must be sorted.
When a SEQUENCE or SET contains an extension marker and extension components like this:
SExt ::= SEQUENCE { a INTEGER, ..., b BOOLEAN }
It means that the type may get more components in newer
versions of the ASN.1 spec. In this case it has got a new
component
The component
During decoding the
The CHOICE type is a space saver and is similar to the concept of a 'union' in the C language.
Assume:
SomeModuleName DEFINITIONS AUTOMATIC TAGS ::= BEGIN T ::= CHOICE { x REAL, y INTEGER, z OBJECT IDENTIFIER } END
It is then possible to assign values:
TVal1 = {y,17}, TVal2 = {z,{0,1,2}},
A CHOICE value is always represented as the tuple
When a CHOICE contains an extension marker and the decoder detects an unknown alternative of the CHOICE the value is represented as:
{asn1_ExtAlt, BytesForOpenType}
Where
The SET OF and SEQUENCE OF types correspond to the concept of an array found in several programming languages. The Erlang syntax for both of these types is straight forward. For example:
Arr1 ::= SET SIZE (5) OF INTEGER (4..9) Arr2 ::= SEQUENCE OF OCTET STRING
We may have the following in Erlang:
Arr1Val = [4,5,6,7,8], Arr2Val = ["abc",[14,34,54],"Octets"],
Please note that the definition of the SET OF type implies that the order of the components is undefined, but in practice there is no difference between SET OF and SEQUENCE OF. The ASN.1 compiler for Erlang does not randomize the order of the SET OF components before encoding.
However, in case of a value of the type
The types
A value of this type is encoded as an
Instead of
See also
These types are used in presentation layer negotiation. They are encoded according to their associated type, see [].
The
The structured types previously described may very well have other named types
as their components. The general syntax to assign a value to the component C
of a named ASN.1 type T in Erlang is the record syntax
For example:
EmbeddedExample DEFINITIONS AUTOMATIC TAGS ::= BEGIN B ::= SEQUENCE { a Arr1, b T } Arr1 ::= SET SIZE (5) OF INTEGER (4..9) T ::= CHOICE { x REAL, y INTEGER, z OBJECT IDENTIFIER } END
The SEQUENCE b can be encoded like this in Erlang:
1> 'EmbeddedExample':encode('B', {'B',[4,5,6,7,8],{x,"7.77"}}). {ok,<<5,56,0,8,3,55,55,55,46,69,45,50>>}
When an asn1 specification is compiled all defined types of type SET or SEQUENCE will result in a corresponding record in the generated hrl file. This is because the values for SET/SEQUENCE as mentioned in sections above are represented as records.
Though there are some special cases of this functionality that are presented below.
It is also possible in ASN.1 to have components that are themselves structured types. For example, it is possible to have:
Emb ::= SEQUENCE { a SEQUENCE OF OCTET STRING, b SET { a INTEGER, b INTEGER DEFAULT 66}, c CHOICE { a INTEGER, b FooType } } FooType ::= [3] VisibleString
The following records are generated because of the type
-record('Emb,{a, b, c}). -record('Emb_b',{a, b = asn1_DEFAULT}). % the embedded SET type
Values of the
V = #'Emb'{a=["qqqq",[1,2,255]],
b = #'Emb_b'{a=99},
c ={b,"Can you see this"}}.
For an embedded type of type SEQUENCE/SET in a SEQUENCE/SET the record name is extended with an underscore and the component name. If the embedded structure is deeper with SEQUENCE, SET or CHOICE types in the line, each component-/alternative-name will be added to the record-name.
For example:
Seq ::= SEQUENCE{ a CHOICE{ b SEQUENCE { c INTEGER } } }
will result in the following record:
-record('Seq_a_b',{c}).
If the structured type has a component with an embedded SEQUENCE OF/SET OF which embedded type in turn is a SEQUENCE/SET it will give a record with the SEQOF/SETOF addition as in the following example:
Seq ::= SEQUENCE { a SEQUENCE OF SEQUENCE { b } c SET OF SEQUENCE { d } }
This results in the records:
-record('Seq_a_SEQOF'{b}). -record('Seq_c_SETOF'{d}).
A parameterized type should be considered as an embedded
type. Each time a such type is referenced an instance of it is
defined. Thus in the following example a record with name
Seq ::= SEQUENCE { b PType{INTEGER} } PType{T} ::= SEQUENCE{ id T }
Types may refer to themselves. Suppose:
Rec ::= CHOICE { nothing NULL, something SEQUENCE { a INTEGER, b OCTET STRING, c Rec }}
This type is recursive; that is, it refers to itself. This is allowed in ASN.1 and the ASN.1-to-Erlang compiler supports this recursive type. A value for this type is assigned in Erlang as shown below:
V = {something,#'Rec_something'{a = 77, b = "some octets here", c = {nothing,'NULL'}}}.
Values can be assigned to ASN.1 type within the ASN.1 code itself, as opposed to the actions taken in the previous chapter where a value was assigned to an ASN.1 type in Erlang. The full value syntax of ASN.1 is supported and [X.680] describes in detail how to assign values in ASN.1. Below is a short example:
TT ::= SEQUENCE { a INTEGER, b SET OF OCTET STRING } tt TT ::= {a 77,b {"kalle","kula"}}
The value defined here could be used in several ways. Firstly, it could be used as the value in some DEFAULT component:
SS ::= SET { s OBJECT IDENTIFIER, val TT DEFAULT tt }
It could also be used from inside an Erlang program. If the above ASN.1
code was defined in ASN.1 module
1> Val = 'Values':tt(). {'TT',77,["kalle","kula"]} 2> {ok,Bytes} = 'Values':encode('TT',Val). {ok,<<48,18,128,1,77,161,13,4,5,107,97,108,108,101,4,4, 107,117,108,97>>} 4> 'Values':decode('TT',Bytes). {ok,{'TT',77,["kalle","kula"]}} 5>
The above example shows that a function is generated by the compiler that returns a valid Erlang representation of the value, even though the value is of a complex type.
Furthermore, there is a macro generated for each value in the .hrl
file. So, the defined value
MACRO is not supported as the the type is no longer part of the ASN.1 standard.
Information Object Classes, Information Objects and Information Object Sets, (in the following called classes, objects and object sets respectively), are defined in the standard definition []. In the following only a brief explanation is given.
These constructs makes it possible to define open types, i.e. values of that type can be of any ASN.1 type. It is also possible to define relationships between different types and values, since classes can hold types, values, objects, object sets and other classes in its fields. An Information Object Class may be defined in ASN.1 as:
GENERAL-PROCEDURE ::= CLASS { &Message, &Reply OPTIONAL, &Error OPTIONAL, &id PrintableString UNIQUE } WITH SYNTAX { NEW MESSAGE &Message [REPLY &Reply] [ERROR &Error] ADDRESS &id }
An object is an instance of a class and an object set is a set containing objects of one specified class. A definition may look like below.
The object
object1 GENERAL-PROCEDURE ::= { NEW MESSAGE PrintableString ADDRESS "home" } object2 GENERAL-PROCEDURE ::= { NEW MESSAGE INTEGER ERROR INTEGER ADDRESS "remote" }
The field ADDRESS is a UNIQUE field. Objects in an object set must have unique values in their UNIQUE field, as in GENERAL-PROCEDURES:
GENERAL-PROCEDURES GENERAL-PROCEDURE ::= { object1 | object2}
One can not encode a class, object or object set, only referring to it when defining other ASN.1 entities. Typically one refers to a class and to object sets by table constraints and component relation constraints [] in ASN.1 types, as in:
StartMessage ::= SEQUENCE { msgId GENERAL-PROCEDURE.&id ({GENERAL-PROCEDURES}), content GENERAL-PROCEDURE.&Message ({GENERAL-PROCEDURES}{@msgId}), }
In the type
So, the value
In practice, object sets are usually declared to be extensible so so that more objects can be added to the set later. Extensibility is indicated like this:
GENERAL-PROCEDURES GENERAL-PROCEDURE ::= { object1 | object2, ...}
When decoding a type that uses an extensible set constraint, there is always the possibility that the value in the UNIQUE field is unknown (i.e. the type has been encoded with a later version of the ASN.1 specification). When that happens, the unencoded data will be returned wrapped in a tuple like this:
{asn1_OPENTYPE,Binary}
where
Parameterization, which is defined in the standard [], can be used when defining types, values, value sets, information object classes, information objects or information object sets. A part of a definition can be supplied as a parameter. For instance, if a Type is used in a definition with certain purpose, one want the type-name to express the intention. This can be done with parameterization.
When many types (or an other ASN.1 entity) only differs in some minor cases, but the structure of the types are similar, only one general type can be defined and the differences may be supplied through parameters.
One example of use of parameterization is:
General{Type} ::= SEQUENCE { number INTEGER, string Type } T1 ::= General{PrintableString} T2 ::= General{BIT STRING}
An example of a value that can be encoded as type T1 is {12,"hello"}.
Observe that the compiler not generates encode/decode functions for parameterized types, only for the instances of the parameterized types. So, if a file contains the types General{}, T1 and T2 above, encode/decode functions will only be generated for T1 and T2.
When the first recommendation on ASN.1 was released 1988 it was accompanied with the Basic Encoding Rules, BER, as the only alternative for encoding. BER is a somewhat verbose protocol. It adopts a so-called TLV (type, length, value) approach to encoding in which every element of the encoding carries some type information, some length information and then the value of that element. Where the element is itself structured, then the Value part of the element is itself a series of embedded TLV components, to whatever depth is necessary. In summary, BER is not a compact encoding but is relatively fast and easy to produce.
The DER (Distinguished Encoding Rule) encoding format was included in
the standard in 1994. It is a specialized form of BER, which gives
the encoder the option to encode some entities differently. For
instance, is the value for TRUE any octet with any bit set to one. But,
DER does not leave any such choices. The value for TRUE in the DER
case is encoded as the octet
A more compact encoding is achieved with the Packed Encoding Rules PER which was introduced together with the revised recommendation in 1994. PER takes a rather different approach from that taken by BER. The first difference is that the tag part in the TLV is omitted from the encodings, and any tags in the notation are not encoded. The potential ambiguities are resolved as follows:
A CHOICE is encoded by first encoding a choice index which identifies the chosen alternative by its position in the notation.
The elements of a SEQUENCE are transmitted in textual order. OPTIONAL or DEFAULT elements are preceded by a bit map to identify which elements are present. After sorting the elements of a SET in the "canonical tag order" as defined in X.680 8.6 they are treated as a SEQUENCE regarding OPTIONAL and DEFAULT elements. A SET is transferred in the sorted order.
A second difference is that PER takes full account of the sub-typing information in that the encoded bytes are affected by the constraints. The BER encoded bytes are unaffected by the constraints. PER uses the sub-typing information to for example omit length fields whenever possible.
The run-time functions, sometimes take the constraints into account both for BER and PER. For instance are SIZE constrained strings checked.
There are two variants of PER, aligned and unaligned. In summary, PER results in compact encodings which require much more computation to produce than BER.