Python with ASN.1 in three steps
ASN.1 is an international standard used by many companies in telecom, avionics, intelligent transportation systems, etc, basically anywhere where robustness and security are important. OSS Nokalva offers a set of ASN.1 tools to help with development of data serialization, communication, and storage.
We’ll use just two of the asn1.io online tools to jumpstart a Python project for a communication protocol using ASN.1 schema and data encodings:
- Json2asn to define a schema based on the JSON representation of our data model.
- ASN1-Python-Compiler to generate a Python encoder/decoder library.
Step 1: Defining the schema
Let's say we’re developing an app that sends and receives messages to control a sensor. For simplicity we have just two kinds of messages: sending a configuration (to sensor) and receiving a reading (from sensor). Let’s define the data in JSON:
Now we can use json2asn to get an ASN.1 schema for each individual message (click one and two), and combine them into a single schema (with minor improvements, highlighted):
Step 2: Generate Python code
Next, we’ll use asn1.io/asn1-python-compiler to generate Python code according to the schema (click here). The generated code includes a sample that looks like:
Every time when the message is encoded or decoded, it can be validated to ensure the required fields (and their values, if constrained) match the schema.
Step 3: Run
Running the code above will encode from a dictionary object value into a byte array (DER) and back to a dictionary object.