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 56 use crate::ber::{BerClass, BerObject, BerObjectContent, BerObjectHeader, BerTag}; 57 58 mod multi; 59 mod parser; 60 mod tagged; 61 pub use crate::der::multi::*; 62 pub use crate::der::parser::*; 63 pub use crate::der::tagged::*; 64 65 use alloc::boxed::Box; 66 use alloc::vec::Vec; 67 use core::convert::{Into, TryFrom}; 68 69 /// DER Object class of tag (same as `BerClass`) 70 pub type DerClass = BerClass; 71 72 /// DER tag (same as BER tag) 73 pub type DerTag = BerTag; 74 75 /// Representation of a DER-encoded (X.690) object 76 /// 77 /// Note that a DER object is just a BER object, with additional constraints. 78 pub type DerObject<'a> = BerObject<'a>; 79 80 /// DER object header (identifier and length) 81 /// 82 /// This is the same object as `BerObjectHeader`. 83 pub type DerObjectHeader<'a> = BerObjectHeader<'a>; 84 85 /// BER object content 86 /// 87 /// This is the same object as `BerObjectContent`. 88 pub type DerObjectContent<'a> = BerObjectContent<'a>; 89