[OFFICIAL] ASN.1 library for Python
Python
Pull request Compare This branch is 7 commits ahead, 134 commits behind etingof:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
doc
pyasn1
test
.gitignore
.travis.yml
CHANGES.txt
LICENSE.txt
MANIFEST.in
README.md
THANKS.txt
TODO.txt
requirements.txt
setup.cfg
setup.py

README.md

ASN.1 library for Python

Downloads Build status Coverage Status GitHub license

This is a free and open source implementation of ASN.1 types and codecs as a Python package. It has been first written to support particular protocol (SNMP) but then generalized to be suitable for a wide range of protocols based on ASN.1 specification.

Features

  • Generic implementation of ASN.1 types (X.208)
  • Fully standard compliant BER/CER/DER codecs
  • 100% Python, works with Python 2.4 up to Python 3.5
  • MT-safe
  • Contributed ASN.1 compiler Asn1ate

Download

The pyasn1 package is distributed under terms and conditions of 2-clause BSD license. Source code is freely available as a Github repo.

Installation

Download pyasn1 from PyPI or just run:

$ pip install pyasn1

How to use pyasn1

With pyasn1 you can build ASN.1 structures from Python objects. For example, the following ASN.1 structure:

Record ::= SEQUENCE {
  id        INTEGER,
  room  [0] INTEGER OPTIONAL,
  house [1] INTEGER DEFAULT 0
}

Could be expressed in pyasn1 like this:

class Record(Sequence):
    componentType = NamedTypes(
        NamedType('id', Integer()),
        OptionalNamedType(
            'room',
            Integer().subtype(
                implicitTag=Tag(tagClassContext, tagFormatSimple, 0)
            )
        ),
        DefaultedNamedType(
            'house', 
            Integer(0).subtype(
                implicitTag=Tag(tagClassContext, tagFormatSimple, 1)
            )
        )
    )

Once you have your ASN.1 data structure defined, you can use it as a conventional data structure:

>>> record = Record()
>>> record['id'] = 123
>>> record[1] = 321
>>> print(record.prettyPrint())
Record:
 id=123
 room=321
>>>

The power of ASN.1 comes with its serialization features. You can serialize your initialized data structure and send it over the network. Conversely, you can turn serialized ASN.1 content, as received from network or read from a file, into a fully-fledged, initialized data structure.

>>> encode(record)
b'0\x07\x02\x01{\x80\x02\x01A'

Many high-profile Internet protocols and file formats utilize ASN.1 serialization. Quite a number of books cover the topic of ASN.1. Communication between heterogeneous systems by Olivier Dubuisson is one of high quality books freely available on the Internet.

Detailed information on pyasn1 APIs can be found in the documentation, working examples are in the pyasn1-modules repo.

Feedback

If something does not work as expected, try browsing pyasn1 mailing list archives or post your question to Stack Overflow.

Copyright (c) 2005-2016, Ilya Etingof. All rights reserved.