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§
- A reader object for BER/DER-encoded ASN.1 data.
- A reader object for a sequence of BER/DER-encoded ASN.1 data.
- A reader object for a set of BER/DER-encoded ASN.1 data.
- A writer object that accepts an ASN.1 value.
- A writer object that accepts ASN.1 values.
- A writer object that accepts ASN.1 values.
- An ASN.1 tag.
Enums§
- Used by
BERReader
to determine whether or not to enforce DER restrictions when parsing. - A value of the ASN.1 primitive/constructed (“P/C”) bit.
- An ASN.1 tag class, used in
Tag
.
Traits§
- Types decodable in BER.
- Types encodable in DER.
Functions§
- Constructs DER-encoded data as
Vec<u8>
. - Constructs DER-encoded sequence of data as
Vec<u8>
. - Reads an ASN.1 value from
&[u8]
. - Decodes DER/BER-encoded data.
- Reads an ASN.1 value from
&[u8]
. - Encodes a value to DER-encoded ASN.1 data.
- Parses BER-encoded data.
- Parses DER/BER-encoded data.
- Parses DER-encoded data.
- Tries to construct DER-encoded data as
Vec<u8>
. - Tries to construct a DER-encoded sequence of data as
Vec<u8>
.