Chapter 7. Generated BER/DER Decode Methods

Table of Contents

Run-time and Generated Python Decode Methods
Procedure for Calling Python BER Decode Methods

For Python, BER decode methods are generated that support decoding BER data in either definite or indefinite length from. Data may be read from any valid streaming data source supported by Python including memory, files, and sockets. Decoding data in DER and CER format is also supported as these are subsets of BER, however, checks are not done to ensure this data in in proper canonical format as specified by these rules.

For each ASN.1 production defined in an ASN.1 source file, a Python decode method may be generated. This method will read data from the stream specified when the decode buffer object was created and decode into variables in memory.

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

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

   X ::= INTEGER

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

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

   X ::= [APPLICATION 1] INTEGER

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

Some types will always cause Python classes with decode 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 decode methods.