Chapter 6. Generated BER/DER Encode Methods

Table of Contents

Run-time and Generated Python Encode Methods
Populating Generated Variables for Encoding
Procedure for Calling Python BER Encode Methods

For Python, the generation of memory-based, definite length BER (Basic Encoding Rules) encode methods is the only encoding method supported at ths time. This also covers the case of DER (Distinguished Encoding Rules) which also uses definite lengths. The extra canonical checks such as sorting elements in a SET are also done if DER is selected as the encoding rules type.

For each ASN.1 production defined in an ASN.1 source file, a Python encode method may be generated. This method will convert a populated variable of the given type into an encoded ASN.1 message.

For primitive types, an encode method is only generated if it is required to alter the encoding of the BER run-time function for that type. The Python BER run-time library contains a set of common run-time base methods in the Asn1BerEncodeBuffer class. These functions include support for encoding content as well as adding the universal tags associated with the types as defined in the X.680 standard.

So for simple assignments, the generation of an encode method is not necessary. For example, the following production will not result in the generation of an encode method or even a Python class:

   X ::= INTEGER

In this case, the user must use the standard run-time function in the BER library to encode a a value of the type. In this case, this would be the encode_integer function.

However, if the type is altered to contain a tag or constraint, then a Python class with a static custom encode method would be generated:

   X ::= [APPLICATION 1] INTEGER

In this case, special logic is necessary to apply the tag value.

Some types will always cause Python classes with encode methods to be generated. At the primitive level, this is true for the ENUMERATED type. This type will always contain a custom set of enumerated values. All constructed types (SEQUENCE, SET, SEQUENCE/SET OF, and CHOICE) will cause Python classes to be generated that include encode methods.