#der #asn-1 #binary-encoding #data-encoding #data-stream

simple_asn1

A simple DER/ASN.1 encoding/decoding library

15 releases

0.6.2 Jun 3, 2022
0.6.1 Nov 14, 2021
0.6.0 Oct 20, 2021
0.5.4 Jun 25, 2021
0.1.0 Dec 27, 2017

#71 in Encoding

Download history 403944/week @ 2024-07-07 380104/week @ 2024-07-14 404952/week @ 2024-07-21 407601/week @ 2024-07-28 407165/week @ 2024-08-04 478523/week @ 2024-08-11 501243/week @ 2024-08-18 484553/week @ 2024-08-25 448509/week @ 2024-09-01 490878/week @ 2024-09-08 444033/week @ 2024-09-15 507977/week @ 2024-09-22 526733/week @ 2024-09-29 544364/week @ 2024-10-06 540821/week @ 2024-10-13 564814/week @ 2024-10-20

2,199,874 downloads per month
Used in 445 crates (33 directly)

ISC license

59KB
1.5K SLoC

Overview

This is a simple library for parsing DER-encoded data.

In particular, this library automates the process of understanding the DER encoded objects in an ASN.1 data stream. These tokens can then be parsed by your library, based on the ASN.1 description in your format.

For convenience, we create the traits ToASN1 and FromASN to abstract the ability to decode a type from an ASN.1 token stream. If your type implements one of these traits, your program or library can then use the convenience functions der_encode and der_decode to do all the parsing work in one action.

Patches welcome!


lib.rs:

A small ASN.1 parsing library for Rust. In particular, this library is used to translate the binary DER encoding of an ASN.1-formatted document into the core primitives of ASN.1. It is assumed that you can do what you need to from there.

The critical items for this document are the traits ToASN1 and FromASN1. The first takes your data type and encodes it into a Vec of simple ASN.1 structures (ASN1Blocks). The latter inverts the process.

Items that implement ToASN1 can be used with the function der_encode to provide single-step encoding of a data type to binary DER encoding. Similarly, items that are FromASN can be single-step decoded using the helper function der_decode.

You can implement one or both traits, depending on your needs. If you do implement both, the obvious encode/decode quickcheck property is strongly advised.

For decoding schemes that require the actual bytes associated with the binary representation, we also provide FromASN1WithBody. This can be used with the offset information in the primitive ASN1Blocks to, for example, validate signatures in X509 documents.

Finally, this library supports ASN.1 class information. I'm still not sure why it's useful, but there it is.

Please send any bug reports, patches, and curses to the GitHub repository at https://github.com/acw/simple_asn1.

Dependencies

~1.3–2MB
~41K SLoC