Quick DER -- Abstract Base Classes for generated ASN.1 handlers
Project description
Embedding Quick DER into Python
This description explains how Quick DER is mapped into Python. The design is made to facilitate similar structural traversals in Python as in C, albeit through a different mechanism. Much like the C headers, the structures are generated and placed into modules that are delivered along with Quick DER for Python.
General Description
The implementation of Quick DER in Python centers around classes that
encapsulate the logic of various ASN.1 objects. Constructed types such
as CHOICE
, SEQUENCE
and SET
have named fields, which map to attributes
that can be addressed directly by adding .fielfname
to the instance.
Although in C we need to explicitly traverse SEQUENCE OF
and SET OF
on account of their variable-sized structures, this has been encapsulated
into the Python API, and such structures show up as either a list or a
set that can be manipulated as is normal under Python.
ASN.1 objects typed by ANY
are left as they are, and provided as a
(binary) Python string, holding the header and contents. You can use
it in any way you like, but if you know the class, you can instantiate
it as though you received the data over a protocol.
It is possible to create custom classes, by setting up the right internal
variables in a subclass or instance of ASN1Object
, but this is not for
the faint of heart; there are ways of crashing the program in the current
system. The same risk does not occur with generated include files. So,
the best way to create handlers for custom structures is by mapping a
custom ASN.1 specification through the asn2quickder
compiler.
Building DER is the reverse process, and it can follow the same process.
Using the Generated Classes
The package for Quick DER is quick_der
. It provides a function
der_unpack()
which expects a class (that must be a subclass of ASN1Object
)
and a Python string holding the byte sequence to decode. It will return
an instance of the given class, with all the pleasantries of using it.
Given such an instance, entries in it can be manipulated as expected. It
should be noted that the data in each output of der_unpack()
is shared,
meaning that you could traverse to an object within the parsed structure,
change it, and then repack the overall structure, to find the changes made
in the embedded object. If this is not what you need, you should clone()
the respective object.
Any ASN1Object
may be turned into DER bytes through its _der_pack()
method (not an ASN.1 name) or the packages der_pack()
function. This
uses the information stored in the object to find the format for packing.
In general, using Quick DER under Python means that you are using classes,
not packer descriptions such as in C. It marks the differences between
the languages.
In case you are wondering why the package quick_der
explicitly mentions
DER again in der_pack()
and der_unpack()
functions: we can see ways
of expanding this approach with encodings for BER, XER, one of the JER
encodings, PER, CER and so on. This might work through multiple inheritance
of the objects, that could incorporate a possible future Quick XER module,
and so on. Factories may do this appropriately for your platform. One day,
who knows!
Example Code
Kerberos is completely defined in terms of ASN.1, so it serves as a good example. Instead of the binary transmission format that defies manipulation and perhaps even reading of parts, we can turn it into a Python object, work on it and generate the binary transmission format when the need arises.
The asn2quickder
compiler produces Python packages for many specifications,
ready to be loaded as modules. For example, a Kerberos Ticket is defined
by the name Ticket
in RFC 4120, so it can be reached under
quickder.rfc.rfc4120.Ticket
and used just like a custom class that would
implement a Kerberos Ticket:
from quickder.rfc.rfc4120 import Ticket
def show_ticket (der):
"""Access individual parts of the Ticket, and print them.
Then compose the owner's name from its constituent parts.
"""
tkt = Ticket (der)
print 'Ticket for Realm', tkt.realm
print ' has name-type', tkt.sname.name_type
for nm in tkt.sname.name_string:
print ' has name-string component', nm
owner = '/'.join (tkt.sname.name_string) + '@' + tkt.realm
print 'In short, it is for', owner
def rebase_ticket (der, newrealm):
"""This violates RFC 4120, but is still a nice demo of modifying
DER data in Python. The violation is caused by the mismatch
of the realm with the encrypted copy in tkt.enc_part
"""
tkt = Ticket (der)
tkt.realm = newrealm
return tkt._der_pack ()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file arpa2.quickder-1.4.0.tar.gz
.
File metadata
- Download URL: arpa2.quickder-1.4.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79ce2d2ea693909087f803e8b61de73f6e128f309b4a564e7c7e016340e55227 |
|
MD5 | abab31b3f2f749806cf29a362a3d2796 |
|
BLAKE2b-256 | 846ed941724923db1413ad0687e78c73864d975a8393444e68ddc51d5d72945d |
File details
Details for the file arpa2.quickder-1.4.0-cp38-cp38-manylinux2010_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp38-cp38-manylinux2010_x86_64.whl
- Upload date:
- Size: 45.3 kB
- Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e62b1ff584ebf467621fb7f0334305c18cf4a59722490cc842f9bbcd6a7ef8c |
|
MD5 | d94cabf1c7e9a5843ed8d1f9946cd6d1 |
|
BLAKE2b-256 | 0ed076427f3dceb418706cf21a674360c5f7d93d51eb6a5ff13072c6f96252d1 |
File details
Details for the file arpa2.quickder-1.4.0-cp38-cp38-manylinux1_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp38-cp38-manylinux1_x86_64.whl
- Upload date:
- Size: 45.3 kB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdcd594e02b0bb9e053152afbc1fceed9a22bcf4dc2afbe8db1da01829cea778 |
|
MD5 | e641ec3e4fe1a58eed946532cc86a4c0 |
|
BLAKE2b-256 | 36b95b78537cf88163adab9489716c2bcc4421bf739964a8e4b9e9a701550a76 |
File details
Details for the file arpa2.quickder-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl
- Upload date:
- Size: 46.1 kB
- Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83715492b88881b21e22b04288e63583d0be4c310d459d3e0ef878a260232750 |
|
MD5 | 085cf7902ee00b9887da7f9caa8142bc |
|
BLAKE2b-256 | 44c7151787ac3c221643fcab0e31d450be1c929d76deb83571ba641a17350f40 |
File details
Details for the file arpa2.quickder-1.4.0-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 46.1 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68267609eafd3565f09e75a8122ca2c27d2c7c576061b611229d8acb961f506e |
|
MD5 | d5c2859bd8445bfde39fc4c01ba870df |
|
BLAKE2b-256 | 8ccd0968ae5b1e061a1e311bd6725f0b5cc03fd667148afecc04d5626476a1f8 |
File details
Details for the file arpa2.quickder-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl
- Upload date:
- Size: 45.2 kB
- Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cdfa58c7b267f6deb6f68511530a1f6860816e66d2ca5ea20580b463d94ffb7 |
|
MD5 | a4e2e1f6ffa3e8d3a13beb9004b9c383 |
|
BLAKE2b-256 | 093f0608f4d6310a83b68893d136350f1f15170718cd455c52a71e0f09a721e8 |
File details
Details for the file arpa2.quickder-1.4.0-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: arpa2.quickder-1.4.0-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 45.2 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfadcb585f6e87a7d8e846319492605e51b55b4ab8dbf05980a91d6e32069c6a |
|
MD5 | 27a9ffa207da1d65c6639f78b1351caf |
|
BLAKE2b-256 | e851fce24943cd5259e195b0089a8f968e025eb30dfb4244aeb720a5f8f71799 |