Skip to main content

Embedding Quick DER into Python

Quick DER logo

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 ()

Metadata

Release files for arpa2.quickder 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for arpa2.quickder 1.4.0
File Size Uploaded
arpa2.quickder-1.4.0.tar.gz 15.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for arpa2.quickder 1.4.0
File
arpa2.quickder-1.4.0-cp38-cp38-manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details
arpa2.quickder-1.4.0-cp38-cp38-manylinux1_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.5+ x86-64 Details
arpa2.quickder-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details
arpa2.quickder-1.4.0-cp37-cp37m-manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details
arpa2.quickder-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Details
arpa2.quickder-1.4.0-cp36-cp36m-manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details

Total release size: 288.4 kB

Release files / arpa2.quickder-1.4.0.tar.gz

Download URL arpa2.quickder-1.4.0.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
79ce2d2ea693909087f803e8b61de73f6e128f309b4a564e7c7e016340e55227
BLAKE2b-256 checksum
How to use checksums
846ed941724923db1413ad0687e78c73864d975a8393444e68ddc51d5d72945d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp38-cp38-manylinux2010_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp38-cp38-manylinux2010_x86_64.whl
Size 45.3 kB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
5e62b1ff584ebf467621fb7f0334305c18cf4a59722490cc842f9bbcd6a7ef8c
BLAKE2b-256 checksum
How to use checksums
0ed076427f3dceb418706cf21a674360c5f7d93d51eb6a5ff13072c6f96252d1
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp38-cp38-manylinux1_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp38-cp38-manylinux1_x86_64.whl
Size 45.3 kB
Tags CPython 3.8 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
fdcd594e02b0bb9e053152afbc1fceed9a22bcf4dc2afbe8db1da01829cea778
BLAKE2b-256 checksum
How to use checksums
36b95b78537cf88163adab9489716c2bcc4421bf739964a8e4b9e9a701550a76
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl
Size 46.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
83715492b88881b21e22b04288e63583d0be4c310d459d3e0ef878a260232750
BLAKE2b-256 checksum
How to use checksums
44c7151787ac3c221643fcab0e31d450be1c929d76deb83571ba641a17350f40
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp37-cp37m-manylinux1_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp37-cp37m-manylinux1_x86_64.whl
Size 46.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
68267609eafd3565f09e75a8122ca2c27d2c7c576061b611229d8acb961f506e
BLAKE2b-256 checksum
How to use checksums
8ccd0968ae5b1e061a1e311bd6725f0b5cc03fd667148afecc04d5626476a1f8
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl
Size 45.2 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
4cdfa58c7b267f6deb6f68511530a1f6860816e66d2ca5ea20580b463d94ffb7
BLAKE2b-256 checksum
How to use checksums
093f0608f4d6310a83b68893d136350f1f15170718cd455c52a71e0f09a721e8
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / arpa2.quickder-1.4.0-cp36-cp36m-manylinux1_x86_64.whl

Download URL arpa2.quickder-1.4.0-cp36-cp36m-manylinux1_x86_64.whl
Size 45.2 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
dfadcb585f6e87a7d8e846319492605e51b55b4ab8dbf05980a91d6e32069c6a
BLAKE2b-256 checksum
How to use checksums
e851fce24943cd5259e195b0089a8f968e025eb30dfb4244aeb720a5f8f71799
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.4.0 This release

7 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page