ASN.1 tools

buildstatus coverage

About

A Python package for ASN.1 parsing, encoding and decoding.

This project is under development and does only support a subset of the ASN.1 specification syntax and BER codec.

Project homepage: https://github.com/eerimoq/asn1tools

Documentation: http://asn1tools.readthedocs.org/en/latest

Installation

pip install asn1tools

Example Usage

This is an example ASN.1 specification defining the messages of a fictitious Foo protocol (based on the FooProtocol on Wikipedia).

Foo DEFINITIONS ::= BEGIN

    Question ::= SEQUENCE {
        id        INTEGER,
        question  IA5String
    }

    Answer ::= SEQUENCE {
        id        INTEGER,
        answer    BOOLEAN
    }

END

Compile the ASN.1 specification, and encode and decode a question using the default codec (BER).

>>> import asn1tools
>>> foo = asn1tools.compile_file('tests/files/foo.asn')
>>> encoded = foo.encode('Question', {'id': 1, 'question': 'Is 1+1=3?'})
>>> encoded
b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?'
>>> foo.decode('Question', encoded)
{'id': 1, 'question': 'Is 1+1=3?'}

See the examples folder for additional examples

Basic Usage

asn1tools.compile_file(filename, codec='ber')[source]

Compile given ASN.1 specification file and return a Specification object that can be used to encode and decode data structures.

>>> foo = asn1tools.compile_file('foo.asn')
class asn1tools.compiler.Specification(modules)[source]

This class is used to encode and decode ASN.1 types found in an ASN.1 specification.

Instances of this class are created by the factory functions compile_file(), compile_string() and compile_json().

types

A dictionary of all types in the specification, or None if a type name was found in two or more modules.

modules

A dictionary of all modules in the specification.

encode(name, data)[source]

Encode given dictionary data as given type name and return the encoded data as a bytes object.

>>> foo.encode('Question', {'id': 1, 'question': 'Is 1+1=3?'})
b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?'
decode(name, data)[source]

Decode given bytes object data as given type name and return the decoded data as a dictionary.

>>> foo.decode('Question', b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?')
{'id': 1, 'question': 'Is 1+1=3?'}

Advanced Usage

asn1tools.compile_string(string, codec='ber')[source]

Compile given ASN.1 specification string and return a Specification object that can be used to encode and decode data structures.

>>> with open('foo.asn') as fin:
...     foo = asn1tools.compile_string(fin.read())
asn1tools.compile_json(specification, codec='ber')[source]

Compile given ASN.1 specification JSON dictionary and return a Specification object that can be used to encode and decode data structures.

>>> foo = asn1tools.compile_json(asn1tools.parse_file('foo.asn'))
asn1tools.parse_file(filename)[source]

Parse given ASN.1 specification file and return a JSON dictionary of its contents.

>>> foo = asn1tools.parse_file('foo.asn')
asn1tools.parse_string(string)[source]

Parse given ASN.1 specification string and return a JSON dictionary of its contents.

>>> with open('foo.asn') as fin:
...     foo = asn1tools.parse_string(fin.read())