der-parser
BER/DER Parser
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.
DER parser design
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.
use parse_der;
let bytes = ;
let parsed = parse_der;
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:
use *;
use BerResult;
let bytes = ;
let parsed = localparse_seq;
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.
Notes
- The DER constraints are verified if using
parse_der. BerObjectandDerObjectare the same objects (type alias). The only difference is the verification of constraints during parsing.- DER integers can be of any size, so it is not possible to store them as simple integers (they
are stored as raw bytes). To get a simple value, use
BerObject::as_u32(knowning that this method will return an error if the integer is too large),BerObject::as_u64, or use thebigintfeature of this crate and useBerObject::as_bigint.
References
- [X.680] Abstract Syntax Notation One (ASN.1): Specification of basic notation.
- [X.690] ASN.1 encoding rules: Specification of Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER).
Changes
4.0.0
- The string types
IA5String,NumericString,PrintableStringandUTF8Stringdo now only parse if the characters are valid. as_str()was added toBerObjectContentto obtain a&strfor the types above.as_slice()works as before.- Implement
Errortrait forBerError - Change the api around
Oidto achieve zero-copy. The following changed:- The
Oidstruct now has a lifetime and usesCowinternally. - The procedural macro
oid!was added. Oid::fromreturns aResultnow.- The
Oidstruct now encodes whether the oid is relative or not. - The
Debugimplementation now shows whether the oid is relative and uses the bigint feature if available. - The
Oid::itermethod now returns anOption.Oid::iter_bigintwas added. Hashis now derived forOid.
- The
- Add method to extract raw tag from header
BerObjectHeadernow has a lifetime and araw_tagfieldBerObjectnow has araw_tagfield- Implement
PartialEqmanually forBerObject:raw_tagis compared only if both fields provide it
3.0.3
- Make the pretty-printer function public
- Fix DER datestring sanity check
- CI
- add rusfmt check
- add cargo clippy
3.0.2
- Add
parse_ber_u32andparse_ber_u64functions - Fix typo in description
3.0.1
- Add crate
BerResultandDerResulttypes - Use crate result types, remove uneeded imports
- Crates using
der-parserdo not need to importnomorrusticata-macrosanymore - Result types are aliases, so API is unchanged
- Crates using
3.0.0
- Upgrade to nom 5 (breaks API)
- New error types, now all functions use
BerError
2.1.0
- Handle BER/DER tags that are longer than one byte.
- Set edition to 2018
2.0.2
- Revert 2.0.1 release, breaks API
2.0.1
- Handle BER/DER tags that are longer than one byte.
2.0.0
- Refactor code, split BER and DER, check DER constraints
- Add recursion limit for sequences and sets
- Rustfmt
- Documentation
- Remove unused function
ber_read_element_content
1.1.1
- Fix OID parsing, and add support for relative OIDs
- Add FromStr trait for Oid
1.1.0
- Use num-bigint over num and upgrade to 0.2
1.0.0
- Upgrade to nom 4
0.5.5
- Add functions
parse_der_u32andparse_der_u64to quickly parse integers - Remove
Oid::from_vec,Oid::fromdoes the same - Enforce constraints on DER booleans
0.5.4
- Add
BitStringObjectto wrap BitString objects - Mark constructed BitStrings as unsupported
- Do not try to parse application-specific data in
parse_der
0.5.3
- Add function
DerObject::as_u64 - Add function
DerObject::as_oid_val - Add
parse_der_struct!variant to check tag
0.5.2
- Add functions to test object class and primitive/constructed state
- Add macro
parse_der_application! - Add macro
parse_der_tagged!to parse[x] EXPLICITor[x] IMPLICITtagged values
0.5.1
- Add type GeneralString
- Add macro
parse_der_struct!
0.5.0
- Allow use of crate without extra use statements
- Use constants for u32 errors instead of magical numbers
- Rename
tag_of_der_content()toDerObjectContent::tag - Rename DerElementxxx structs to have a consistent naming scheme
- Add documentation for parsing DER sequences and sets, and fix wrong return type for sets
- Fix a lot of clippy warnings
- QA: add pragma rules (disable unsafe code, unstable features etc.)
- More documentation
- Switch license to MIT + APLv2
0.4.4
- Add macro parse_der_defined_m, to parse a defined sequence or set
This macro differs from
parse_der_definedbecause it allows using macros - Rename
DerObject::new_inttoDerObject::from_int_slice - Rename
Oid::to_hextoOid::to_string - Document more functions
0.4.1
- Add new feature 'bigint' to export DER integers
- OID is now a specific type
- Add new types T61String and BmpString
- Fix wrong expected tag in parse_der_set_of
0.4.0
- Der Integers are now represented as slices (byte arrays) since they can be larger than u64.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
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.