Package: asn1ate
asn1ate – ASN.1 translation library.
Copyright 2013-2017 Schneider Electric Buildings ABIntroductionasn1ateis a Python library for translating ASN.1 into other forms.
It is intended for code generation from formal ASN.1 definitions, and a
code generator forpyasn1is included.asn1ateis released under a 3-clause BSD license. For details, see
LICENSE.txt.CaveatThis is very much an alpha-quality prototype. Things that need doing:Regression test suiteHACK/TODO/BUGs need to be fixedASN.1 grammar is very incomplete in some places. Known issues:Constraint syntax is currently limited to simple value range constraints (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12.asn, line 53)Reference syntax is not accepted at all (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12.asn, line 74)Improve parser error handling/reportingAllow for semantic analysis, e.g. validity check, warnings for problematic
constructs, etc.UsageThe immediate use ofasn1ateis to generatepyasn1definitions from
ASN.1 definitions. The command to do this is:$ python .../asn1ate/pyasn1gen.py source.asn1It will print thepyasn1equivalent ofsource.asn1to stdout.Butasn1ateis also designed to be usable as a library, to allow reuse of
the ASN.1 parser for custom code generation.Caveat #2My understanding of ASN.1 syntax and semantics is limited. As I learn and
discover more, the designwillchange. I will always prioritize a better model
before API stability for the foreseeable future.So if you’re usingasn1ateas a library for custom codegen, please be aware
that every release will likely break something for you.The in-treepyasn1code generator will be kept up-to-date, of course, and
may serve as an example of different codegen tasks,DependenciesThe only third-party dependency ispyparsing.Althoughasn1atewas initially developed on Python 3.2, it has been tested
with Python 2.7 and should port to older Python versions easily. Latest release
tested with:Python 3.6.3Python 2.7.14Design notesTheasn1atepackage is designed along the same lines as a compiler with a
driver, a parser, a semantic model and a convention for code generators.parser.py– a tokenizing parser for ASN.1 per X.680. It currently
recognizes a naive sub-set of X.680sema.py– a semantic ASN.1 object model, which can be constructed from
the AST generated byparser.pysupport/pygen.py– a support library for generating Python code.pyasn1gen.py– a code generator to transform a semantic model intopyasn1syntax. This can be used as a script in which case it will dump
output to stdout.The ASN.1 parser is very ad-hoc, I’ve experimented with the grammar until I
found something that accepted our proprietary ASN.1 definition. It’s based onpyparsingbut sets up parse actions to build an annotated AST. Every node of
interest is annotated with a string denoting its type, e.g.Identifier,TypeAssignment, etc. I’ve tried to stay with token types as named in X.680,
but added custom ones or suppressed others, as necessary to get the AST in a
useful shape.Annotated tokens are represented by a simple class containing the type name and
a list of children (calledelements) which may be annotated tokens, lists or
simple values. This gives a very discoverable tree structure, but there are
probably cleaner AST representations we could use. Patches welcome.asn1ate.semais an object model that represents ASN.1 constructs. It
describes everything from type assignments to default values and tags, but still
only the parts of ASN.1 we happen to use here. Most of the logic revolves around
transforming the AST produced byasn1ate.parserinto a more semantic model
with proper Python objects.Codegen is designed to be extensible. In-house we have a set of code generators
to build an entire protocol stack based on an ASN.1 source, butasn1ateonly
includes the generally useful one,asn1ate.pyasn1gen.The most notable members ofasn1ate.supportare probably thePythonWriterandPythonFragmentclasses, which simplify generation of
correctly indented Python code.