Skip to main content

Pure Python X.690 implementation

Project description

Pure Python X.690 implementation

Build & Publish Docs

This module contains a pure Python implementation of the “x690” standard for BER encoding/decoding. Other encodings are currently unsupported but pull-requests are welcome.

Supporting New Types

Some applications may need to support types which are not defined in the X.690 standard. This is supported by this library but the types must be defined and registered.

To register a type, simply subclass x690.types.Type. This will take care of the registration. Make sure that your new type is imported before using it.

New types should define the following 3 class-variables:

TYPECLASS

A value from x690.util.TypeClass

NATURE

A value from x690.util.TypeNature

TAG

A numerical identifier for the type

Refer to the x690 standard for more details on these values. As a general rule-of-thumb you can assume that the class is either “context” or “application” (it might be good to keep the “universal” class reserved for x690). The nature should be “primitive” for simple values and “constructed” for composed types. The tag is free to choose as long as you don’t overlap with an existing type.

To convert raw-bytes into a Python object, override x690.Type.decode_raw and conversely also x690.Type.encode_raw. Refer to the docstrings for more details.

Reverse Engineering Bytes

All types defined in the x690 library provide a .pretty() method which returns a prettyfied string.

If you are confronted with a bytes-object encoded using X.690 but don’t have any documentation, you can write the following loop:

from x690 import decode

data = open("mydatafile.bin", "rb").read()

value, nxt = decode(data)
print(value.pretty())

while nxt < len(data):
    value, nxt = decode(data, nxt)
    print(value.pretty())

This should get you started.

If the data contain non-standard types, they will get detected as “UnknownType” and will print out the type-class, nature and tag in the pretty-printed block.

This will allow you to define your own subclass of x690.types.Type using those values. Override decode(...) in that class to handle the unknown type.

Examples

Encoding to bytes

Encoding to bytes can be done by simply calling the Python builting bytes() on instances from x690.types:

Encoding of a single value

>>> import x690.types as t
>>> myvalue = t.Integer(12)
>>> asbytes = bytes(myvalue)
>>> repr(asbytes)
b'\x02\x01\x0c'

Encoding of a composite value using Sequence

>>> import x690.types as t
>>> myvalue = t.Sequence(
...     t.Integer(12),
...     t.Integer(12),
...     t.Integer(12),
... )
>>> asbytes = bytes(myvalue)
>>> repr(asbytes)
b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'

Decoding from bytes

Decode bytes by calling x690.types.decode on your byte data. This will return a tuple where the first value contains the decoded object, and the second one will contain any remaining bytes which were not decoded.

>>> import x690
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'
>>> decoded, nxt = x690.decode(data)
>>> decoded
Sequence(Integer(12), Integer(12), Integer(12))
>>> nxt
11

Type-Hinting & Enforcing

New in 0.3.0

When decoding bytes, it is possible to specify an expcted type which does two things: Firstly, it tells tools like mypy what the return type will be and secondly, it runs an internal type-check which ensures that the returned value is of the expected type. x690.exc.UnexpectedType is raised otherwise.

This does of course only work if you know the type in advance.

>>> import x690
>>> import x690.types as t
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'
>>> decoded, nxt = x690.decode(data, enforce_type=t.Sequence)
>>> decoded
Sequence(Integer(12), Integer(12), Integer(12))
>>> nxt
11

Strict Decoding

New in 0.3.0

When decoding using decode and you don’t expect any remaining bytes, use strict=True which will raise x690.exc.IncompleteDecoding if there’s any remaining data.

>>> import x690
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0cjunk-bytes'
>>> decoded, nxt = x690.decode(data, strict=True)
Traceback (most recent call last):
  ...
x690.exc.IncompleteDecoding: Strict decoding still had 10 remaining bytes!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x690-0.5.0a0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x690-0.5.0a0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file x690-0.5.0a0.tar.gz.

File metadata

  • Download URL: x690-0.5.0a0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for x690-0.5.0a0.tar.gz
Algorithm Hash digest
SHA256 0cdf5e4fab245ce1805f4a0f8ca76b23e2a20a62e88865f4d7cde277db1dbf91
MD5 2147ac4c59b0fba00b84c5f7edfe955d
BLAKE2b-256 baf0868fc8945597e0e51e5ce55a8f4b440622cc9664d837f7e085cf55e7c214

See more details on using hashes here.

File details

Details for the file x690-0.5.0a0-py3-none-any.whl.

File metadata

  • Download URL: x690-0.5.0a0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for x690-0.5.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bb51c1014ac4936bfb69aab923c529102f4a3f25b5c956dec9d2757862e0757
MD5 b66192b605d5c52e5e1341c98053da59
BLAKE2b-256 f26f58954b0d76fe0b6ecc7cfd1552a483b2ce00afebe633c0805b4d3fc4769e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page