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§

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

Structs§

ASN1Error
BERReader
A reader object for BER/DER-encoded ASN.1 data.
BERReaderSeq
A reader object for a sequence of BER/DER-encoded ASN.1 data.
BERReaderSet
A reader object for a set of BER/DER-encoded ASN.1 data.
DERWriter
A writer object that accepts an ASN.1 value.
DERWriterSeq
A writer object that accepts ASN.1 values.
DERWriterSet
A writer object that accepts ASN.1 values.
Tag
An ASN.1 tag.

Enums§

ASN1ErrorKind
BERMode
Used by BERReader to determine whether or not to enforce DER restrictions when parsing.
PCBit
A value of the ASN.1 primitive/constructed (“P/C”) bit.
TagClass
An ASN.1 tag class, used in Tag.

Traits§

BERDecodable
Types decodable in BER.
DEREncodable
Types encodable in DER.

Functions§

construct_der
Constructs DER-encoded data as Vec<u8>.
construct_der_seq
Constructs DER-encoded sequence of data as Vec<u8>.
decode_ber
Reads an ASN.1 value from &[u8].
decode_ber_general
Decodes DER/BER-encoded data.
decode_der
Reads an ASN.1 value from &[u8].
encode_der
Encodes a value to DER-encoded ASN.1 data.
parse_ber
Parses BER-encoded data.
parse_ber_general
Parses DER/BER-encoded data.
parse_der
Parses DER-encoded data.
try_construct_der
Tries to construct DER-encoded data as Vec<u8>.
try_construct_der_seq
Tries to construct a DER-encoded sequence of data as Vec<u8>.

Type Aliases§

ASN1Result