summaryrefslogtreecommitdiff
path: root/README.md
blob: 8916a2eb0f25cdf36d6d80c14fa138ae32e495ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

ASN.1 library for Python
------------------------
[![GitHub license](https://img.shields.io/badge/license-BSD-blue.svg)](https://raw.githubusercontent.com/etingof/pyasn1/master/LICENSE.txt)
[![Downloads](https://img.shields.io/pypi/dm/pyasn1.svg)](https://pypi.python.org/pypi/pyasn1)
[![Build status](https://travis-ci.org/etingof/pyasn1.svg?branch=master)](https://secure.travis-ci.org/etingof/pyasn1)
[![Coverage Status](https://img.shields.io/codecov/c/github/etingof/pyasn1.svg)](https://codecov.io/github/etingof/pyasn1/coverage.svg?branch=master)

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](https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-X.208-198811-W!!PDF-E&type=items).

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

Misfeatures
-----------

* No ASN.1 compiler shipped with pyasn1, so by-hand ASN.1 spec compilation 
  into Python code would be needed. But there is a project, called
  [Asn1ate](https://github.com/kimgr/asn1ate) that compiles ASN.1 documents
  into pyasn1 code. 
* Codecs are not restartable

Download
--------

The pyasn1 package is distributed under terms and conditions of 2-clause
BSD [license](http://pyasn1.sourceforge.net/license.html). Source code is freely
available as a Github [repo](https://github.com/etingof/pyasn1).

Installation
------------

Download pyasn1 from [PyPI](https://pypi.python.org/pypi/pyasn1) 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.

Detailed information on pyasn1 APIs can be found in the
[documentation](http://pyasn1.sourceforge.net),
working examples are in the pyasn1-modules
[repo](https://github.com/etingof/pyasn1-modules).

Feedback
--------

If something does not work as expected, try browsing pyasn1
[mailing list archives](https://sourceforge.net/p/pyasn1/mailman/pyasn1-users/)
or post your question
[to Stack Overflow](http://stackoverflow.com/questions/ask).  
Copyright (c) 2005-2016, [Ilya Etingof](http://ilya@glas.net).
All rights reserved.