A parser for Basic Encoding Rules (BER [[X.690]]) and Distinguished Encoding Rules(DER [[X.690]]), implemented with the nom parser combinator framework.
The code is available on Github and is part of the Rusticata project.
There are two different approaches for parsing DER objects: reading the objects recursively as long as the tags are known, or specifying a description of the expected objects (generally from the ASN.1 description).
The first parsing method can be done using the parse_ber and
parse_der methods.
However, it cannot fully parse all objects, especially those containing IMPLICIT, OPTIONAL, or
DEFINED BY items.
```rust use derparser::parseder;
let bytes = [ 0x30, 0x0a, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x03, 0x01, 0x00, 0x00, ];
let parsed = parse_der(&bytes); ```
The second (and preferred) parsing method is to specify the expected objects recursively. The
following macros can be used:
parse_der_sequence_defined and similar functions,
parse_der_struct, etc.
For example, to read a sequence containing two integers:
```rust use derparser::ber::*; use derparser::error::BerResult;
fn localparseseq(i:&[u8]) -> BerResult { parsedersequencedefined!(i, parseberinteger >> parseberinteger ) }
let bytes = [ 0x30, 0x0a, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x03, 0x01, 0x00, 0x00, ]; let parsed = localparse_seq(&bytes); ```
All functions return a BerResult object: the parsed
BerObject, an Incomplete value, or an error.
Note that this type is also a Result, so usual functions (map, unwrap etc.) are available.
parse_der.BerObject and DerObject are the same objects (type alias). The only difference is the
verification of constraints during parsing.BerObject::as_u32 (knowning that this method will
return an error if the integer is too large), BerObject::as_u64,
or use the bigint feature of this crate and use
BerObject::as_bigint.[[X.690]] ASN.1 encoding rules: Specification of Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER).
Specification of basic notation." Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER)."
IA5String, NumericString, PrintableString and UTF8String
do now only parse if the characters are valid.as_str() was added to BerObjectContent to obtain a &str for the types above.
as_slice() works as before.Error trait for BerErrorOid to achieve zero-copy. The following changed:
Oid struct now has a lifetime and uses Cow internally.oid! was added.Oid::from returns a Result now.Oid struct now encodes whether the oid is relative or not.Debug implementation now shows whether the oid is relative
and uses the bigint feature if available.Oid::iter method now returns an Option. Oid::iter_bigint was
added.Hash is now derived for Oid.BerObjectHeader now has a lifetime and a raw_tag fieldBerObject now has a raw_tag fieldPartialEq manually for BerObject: raw_tag is compared only if both fields provide itparse_ber_u32 and parse_ber_u64 functionsBerResult and DerResult typesder-parser do not need to import nom or rusticata-macros anymoreBerErrorber_read_element_contentparse_der_u32 and parse_der_u64 to quickly parse integersOid::from_vec, Oid::from does the sameBitStringObject to wrap BitString objectsparse_derDerObject::as_u64DerObject::as_oid_valparse_der_struct! variant to check tagparse_der_application!parse_der_tagged! to parse [x] EXPLICIT or [x] IMPLICIT tagged valuesparse_der_struct!tag_of_der_content() to DerObjectContent::tagparse_der_defined because it allows using macrosDerObject::new_int to DerObject::from_int_sliceOid::to_hex to Oid::to_stringLicensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.