summaryrefslogtreecommitdiff
path: root/README.md
blob: fc5877e0318a9f43eaf77fc273c1a8e55c6d1e91 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

ASN.1 library for Python
------------------------
[![PyPI](https://img.shields.io/pypi/v/pyasn1.svg?maxAge=2592000)](https://pypi.python.org/pypi/pyasn1)
[![Python Versions](https://img.shields.io/pypi/pyversions/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)
[![GitHub license](https://img.shields.io/badge/license-BSD-blue.svg)](https://raw.githubusercontent.com/etingof/pyasn1/master/LICENSE.txt)

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.6
* MT-safe
* Contributed ASN.1 compiler [Asn1ate](https://github.com/kimgr/asn1ate)

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:

```bash
$ pip install pyasn1
```

Why using pyasn1
----------------

ASN.1 solves the data serialization problem. This solution was
designed long ago by the wise Ancients. Back then, they did not
have the luxury of wasting bits. That is why ASN.1 is designed
to serialize data structures of unbounded complexity into
something compact and robust.

That probably explains why many network protocols and file formats
still rely upon the 30+ years old technology.

The pyasn1 tool is designed to help Python programmers tackling
network protocols and file formats at the comfort of their Python
prompt. The tool struggles to capture all aspects of a rather
complicated ASN.1 system and represent it on Python terms.

How to use pyasn1
-----------------

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

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

Could be expressed in pyasn1 like this:

```python
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 along
the lines of corresponding Python type:

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

Part of the power of ASN.1 comes from its serialization features. You
can serialize your data structure and send it over the network.

```python
>>> substrate = encode(record)
>>> hexdump(substrate)
00000: 30 07 02 01 7B 80 02 01 41
```

Conversely, you can turn serialized ASN.1 content, as received from
network or read from a file, into a Python object which you can
introspect, modify, encode and send back.

```python
>>> received_record, rest_of_substrate = decode(substrate, asn1Spec=Record())
>>>
>>> print(received_record.prettyPrint())
Record:
 id=123
 room=321
 house=0
>>>
>>> record == received_record
True
>>> received_record['room'] = 123
>>> substrate = encode(received_record)
>>> hexdump(substrate)
00000: 30 06 02 01 7B 80 01 7B
```

In ASN.1 design, serialization codecs are decoupled from data objects,
so you could turn your ASN.1 object into many different serialized forms.
As of this moment, pyasn1 supports BER, DER and CER formats. PER
support is expected to introduced in the upcoming release.

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](http://www.oss.com/asn1/dubuisson.html)
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](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-2017, [Ilya Etingof](http://etingof@gmail.com).
All rights reserved.