Crate yasna

source ·
Expand description

A library for reading and writing ASN.1 data.

Examples

Encoding/decoding simple data

A type implementing DEREncodable can be easily encoded:

fn main() {
    let der = yasna::encode_der(&(10, true));
    println!("(10, true) = {:?}", der);
}

Similarly, a type implementing BERDecodable can be easily decoded:

fn main() {
    let asn: (i64, bool) = yasna::decode_der(
        &[48, 6, 2, 1, 10, 1, 1, 255]).unwrap();
    println!("{:?} = [48, 6, 2, 1, 10, 1, 1, 255]", asn);
}

Encoding/decoding by hand

The default DEREncodable/BERDecodable implementations can’t handle all ASN.1 types. In many cases you have to write your reader/writer by hand.

To serialize ASN.1 data, you can use construct_der.

fn main() {
    let der = yasna::construct_der(|writer| {
        writer.write_sequence(|writer| {
            writer.next().write_i64(10);
            writer.next().write_bool(true);
        })
    });
    println!("(10, true) = {:?}", der);
}

To deserialize ASN.1 data, you can use parse_ber or parse_der.

fn main() {
    let asn = yasna::parse_der(&[48, 6, 2, 1, 10, 1, 1, 255], |reader| {
        reader.read_sequence(|reader| {
            let i = reader.next().read_i64()?;
            let b = reader.next().read_bool()?;
            return Ok((i, b));
        })
    }).unwrap();
    println!("{:?} = [48, 6, 2, 1, 10, 1, 1, 255]", asn);
}

Modules

  • Provides datatypes which correspond to ASN.1 types.
  • Provides universal tag constants.

Structs

Enums

Traits

Functions

Type Definitions