der_parser/der/mod.rs
1//! Distinguished Encoding Rules (DER) objects and parser
2//!
3//! All functions in this crate use BER parsing functions (see the `ber` module)
4//! internally, adding constraints verification where needed.
5//!
6//! The objects [`BerObject`] and [`DerObject`] are the same (type alias): all BER functions,
7//! combinators and macros can be used, and provide additional tools for DER parsing.
8//! However, DER parsing functions enforce DER constraints in addition of their BER counterparts.
9//!
10//! # DER Objects
11//!
12//! The main object of this crate is [`DerObject`]. It contains a header (ber tag, class, and size)
13//! and content.
14//!
15//! To parse primitive objects (for ex. integers or strings), use the `parse_der_` set of
16//! functions.
17//!
18//! Constructed objects (like sequences, sets or tagged objects) require to use a combinator. This
19//! combinator takes a function or closure as input, and returns a new, specialized parser.
20//! See the [nom](https://github.com/geal/nom) parser combinator library for more details on
21//! combinators.
22//!
23//! # Examples
24//!
25//! Parse two DER integers:
26//!
27//! ```rust
28//! use der_parser::der::parse_der_integer;
29//!
30//! let bytes = [ 0x02, 0x03, 0x01, 0x00, 0x01,
31//! 0x02, 0x03, 0x01, 0x00, 0x00,
32//! ];
33//!
34//! let (rem, obj1) = parse_der_integer(&bytes).expect("parsing failed");
35//! let (rem, obj2) = parse_der_integer(&bytes).expect("parsing failed");
36//! ```
37//!
38//! Parse a BER sequence containing one integer and an octetstring:
39//!
40//! ```rust
41//! use der_parser::der::*;
42//!
43//! let bytes = [ 0x30, 0x0a,
44//! 0x02, 0x03, 0x01, 0x00, 0x01,
45//! 0x04, 0x03, 0x62, 0x61, 0x64,
46//! ];
47//!
48//! let (rem, seq) = parse_der_sequence_defined(|content| {
49//! let (rem, obj1) = parse_der_integer(content)?;
50//! let (rem, obj2) = parse_der_octetstring(rem)?;
51//! Ok((rem, vec![obj1, obj2]))
52//! })(&bytes)
53//! .expect("parsing failed");
54//! ```
55
56use crate::ber::{BerObject, BerObjectContent};
57pub use crate::ber::{Class, Header};
58pub use asn1_rs::Tag;
59
60mod multi;
61mod parser;
62mod tagged;
63pub use crate::der::multi::*;
64pub use crate::der::parser::*;
65pub use crate::der::tagged::*;
66
67use alloc::boxed::Box;
68use alloc::vec::Vec;
69use core::convert::Into;
70
71/// DER Object class of tag (same as `BerClass`)
72#[deprecated(since = "7.0.0", note = "Use `Class` instead")]
73pub type DerClass = Class;
74
75/// DER tag (same as BER tag)
76#[deprecated(since = "7.0.0", note = "Use `Tag` instead")]
77pub type DerTag = Tag;
78
79/// Representation of a DER-encoded (X.690) object
80///
81/// Note that a DER object is just a BER object, with additional constraints.
82pub type DerObject<'a> = BerObject<'a>;
83
84/// DER object header (identifier and length)
85///
86/// This is the same object as `BerObjectHeader`.
87#[deprecated(since = "7.0.0", note = "Use `Tag` instead")]
88pub type DerObjectHeader<'a> = Header<'a>;
89
90/// BER object content
91///
92/// This is the same object as `BerObjectContent`.
93pub type DerObjectContent<'a> = BerObjectContent<'a>;