5.2 pisces.asn1 - Encode and decode ASN.1 BER format

This module provides a parser for ASN.1 objects encoded using BER. The parser produces ASN1Object instances that can be converted back to BER encoding using the encode method.

parse (buf)
Parse string buf and return an ASN1Object instance that it encodes. Raises ValueError if invalid data is passed to it. Warning: This code could be more robust; other exceptions may be raised for particular invalid inputs.

ASN1Object (val)
The ASN1Object class is the abstract base class of all the objects generated by parse. The constructor takes a single argument val, a parsed ASN.1 object.

Subclasses of ASN1Object define the following method and attribute:

atomic: True if the object is atomic; false if it is a container.

encode ([io])
Returns a string containing the BER encoding of the ASN.1 object. If a file-like object is passed as io, the encoding will be written to the file instead.

The following subclasses of ASN1Object are defined in this module. Methods defined only for single subclasses are also described here.

Sequence ()
Sequence implements the ASN.1 type SEQUENCE, an ordered collection of one or more types. Instances can are also Python sequence objects.

Set ()
Set implements the ASN.1 type SET, an unorderd collection of one or more types. Instances can are also Python sequence objects, where the order depends on the specific order of elements in the original encoding.

UTCTime ()
UTCTime implements the standard ASN.1 type for time expressed in GMT. The X.509 standards note that UTCTime values shall be expressed Greenwich Mean Time (Zulu) and shall include seconds (i.e., times are YYMMDDHHMMSSZ), even where the number of seconds is zero. Conforming systems shall interpret the year field (YY) as follows:

Where YY is greater than or equal to 50, the year shall be inter- preted as 19YY; and

Where YY is less than 50, the year shall be interpreted as 20YY.

UTCTime instances can be compared to each other and support ordering in the natural way.

Contextual ()
Contextual is a wrapper for ASN.1 types defined using CHOICE. For contextual encoding, it isn't possible to tell what the type of the contained object is without looking at the ASN.1 type declaration. This module is designed for parsing independently of the type declaration, which works for every case exception this one.

In the case of contextual encoding, this object is returned. When the decoded structure is actually used, it should be clear whether this is, e.g., an OPTIONAL integer type, some other tagged, known type, or an encoded CHOICE. Clients should call the decode method when the encoding includes the full DER encoding. Clients should call choose when the value doesn't have the appropriate tag/len info.

Contextual has two extra methods:

decode ()
Return the decoded object. This method should be called when the encoding includes the tag and length.

choose (tag)
Return the decoded object, using tag as the ASN.1 tag. This method should be called when the containing type declaration uses CHOICE and the tag is not included in the encoding.

Boolean ()
Boolean is the ASN.1 Boolean type. Instances of this class implement __nonzero__, so they can be used in Python conditionals.

OID ()
An ASN.1 Object Identifier. Can be compared to other OID objects and used as a dictionary key.

Other ASN.1 types are represented using Python's builtin datatypes. This table summarizes the ASN.1 types supported.

ASN.1 type  Python type 
INTEGER int
BIT_STRING string
OCTET_STRING string
NULL None
PrintableString string
T61String string
IA5String string

display (obj)
Pretty-print an ASN1Object instance.

Displayer ([oids])
The Displayer class creates pretty-printers for ASN.1 objects that will print labels for certain well-known oids. The constructor argument oids is a mapping from OID object to string names.

parseCfg (io)
Reads a configuration file from io, which must be a file-like object ready for reading. Returns a dictionary mapping OIDs to strings that describe the OIDs. This function is provided for compatibility with Peter Gutmann's dumpasn1 program. An example of the configuration file is available at http://www.cs.auckland.ac.nz/~pgut001/dumpasn1.cfg.