#encoding-rules #asn-1 #macro-derive #bindings-generator #per #asn1-der

bin+lib asn1-compiler

Compiler for compiling ASN.1 specs to Rust, with PER Codec support and derive macros for PER Codecs

22 releases

0.7.0 Jun 27, 2024
0.6.1 Nov 10, 2023
0.5.9 Aug 8, 2023
0.5.8 Mar 3, 2023
0.1.2 Nov 13, 2021

#46 in Procedural macros

Download history 826/week @ 2024-07-19 839/week @ 2024-07-26 896/week @ 2024-08-02 558/week @ 2024-08-09 678/week @ 2024-08-16 441/week @ 2024-08-23 206/week @ 2024-08-30 251/week @ 2024-09-06 583/week @ 2024-09-13 508/week @ 2024-09-20 366/week @ 2024-09-27 558/week @ 2024-10-04 523/week @ 2024-10-11 468/week @ 2024-10-18 599/week @ 2024-10-25 592/week @ 2024-11-01

2,313 downloads per month

Apache-2.0 OR MIT

360KB
9K SLoC

Hampi - Rust ASN.1 Toolkit

GitHub Workflow Status (branch) Crates.io Crates.io Crates.io

The Goal of this project is to implement an ASN.1 Compiler in Rust which can generate Rust bindings for different ASN.1 specifications, which can be used in various protocol implementations in Rust. Also, support for different ASN.1 encoding rules like DER, BER, PER is planned to be implemented as ASN.1 codecs.

While it is certainly desirable to have a fully compliant ASN.1 Compiler, initial focus of the project is to be able to generate code that can be used in other software. First goal is to work with 3GPP specifications for protocols like Radio Network Access Protocl (RANAP), S1 Application protocl (S1AP) and NG Application Protocol (NGAP). Consequently current implementation also focuses on codecs used by these specifications viz. Aligned Packed Encoding Rules (APER).

The use cases targeted by this implementation are similar to the use cases targeted by asn1c compiler, which generates C bindings from the ASN.1 Specifications.

This project is divided into three crates -

  1. asn1-compiler docs.rs: This crate provides the actual ASN.1 Compiler. Typically a utility (hampi-rs-asn1c) will generate Rust structures starting with ASN.1 Specifications. Basic working features required to work with the 3GPP specifications is provided by the compiler, this includes Parameterized Types, Information Object Classes and Type Constraints.

  2. asn1-codecs docs.rs: Support for different encodings supported by individual ASN.1 specifications is provided in this crate. Currently basic APER/UPER Codecs are supported. Every Codec is supported as a trait implementing respective 'encode' and 'decode' functions. Support for different 'encoding rules' will be implemented in this crate and then the derive macros will utilize this code to actually generate the encoding support for Rust Structures generated by the compiler above.

  3. asn1_codecs_derive docs.rs: This crate provides the derive macros for the codecs in asn-codecs. The code generated using asn-compiler can be directed to derive appropriate codecs by passing the flags during compilation.

Supported Codecs

  • APER
  • UPER

Supported Protocols

Following protocols are currently supported.

  1. RANAP - Code generation and RANAP PDU encode/decode.
  2. S1AP - Code generation and S1AP PDU decode.
  3. NGAP - Code generation and NGAP PDU encode/decode.
  4. E2AP - Code generation
  5. SUPL - Code generation
  6. RRC - Code generation and RRC PDU decode.
  7. E2SM - Code generation and encode/decode functionality.

Getting Started

build.rs Support

Typically the compiler can be invoked also using build.rs mechanism. An example build.rs is provided in the examples/ sub project. And the code generated through this build.rs can be integrated into your project. Examples of that is provided in examples/tests/ directory.

Using CLI tool

A tool hampi-rs-asn1c can be installed using cargo install asn1-compiler and then following the CLI usage.

cargo install asn1-compiler
hampi-rs-asn1c --help

Running Test Cases

  1. Test cases can be run through cargo test.

Generating ASN.1 spec files

Currently there are two ways of generating the 'ASN.1' spec files from the '.docx' specifications (This mainly applies to 3GPP specifications).

  1. Using the script inside examples/specs/parse_specs.py. This is recommended way. (note: this requires installation of docx or python-docx in the case of Python3).
  2. Using the rust binary extract-asn-spec. Note: this is not the recommended way and your mileage may vary. Eventual goal is to make this the default way - so as to also have a build.rs integration. But this is for now a work in progress. This is not available by default and is enabled only with a feature flag rs-specs-gen.

License

Licensed under either of

Dependencies

~5–8.5MB
~149K SLoC