• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..17-Nov-2021-

src/H17-Nov-2021-4,7602,788

tests/H17-Nov-2021-1,4491,303

.cargo-checksum.jsonH A D03-May-202289 11

.travis.ymlH A D17-Nov-20211.8 KiB6766

CHANGELOG.mdH A D17-Nov-20215.2 KiB192131

Cargo.tomlH A D17-Nov-20211.8 KiB6454

LICENSE-APACHEH A D17-Nov-202110.6 KiB202169

LICENSE-MITH A D17-Nov-20211 KiB2622

README.mdH A D17-Nov-20216 KiB149104

UPGRADING.mdH A D17-Nov-20212.8 KiB7151

README.md

1<!-- cargo-sync-readme start -->
2
3[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE-MIT)
4[![Apache License 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE)
5[![docs.rs](https://docs.rs/der-parser/badge.svg)](https://docs.rs/der-parser)
6[![crates.io](https://img.shields.io/crates/v/der-parser.svg)](https://crates.io/crates/der-parser)
7[![Download numbers](https://img.shields.io/crates/d/der-parser.svg)](https://crates.io/crates/der-parser)
8[![Travis CI](https://travis-ci.org/rusticata/der-parser.svg?branch=master)](https://travis-ci.org/rusticata/der-parser)
9[![AppVeyor CI](https://ci.appveyor.com/api/projects/status/github/rusticata/der-parser?svg=true)](https://ci.appveyor.com/project/chifflier/der-parser)
10[![dependency status](https://deps.rs/crate/der-parser/4.0.2/status.svg)](https://deps.rs/crate/der-parser/4.0.2)
11
12# BER/DER Parser
13
14A parser for Basic Encoding Rules (BER [[X.690]]) and Distinguished Encoding Rules(DER
15[[X.690]]), implemented with the [nom](https://github.com/Geal/nom) parser combinator
16framework.
17
18It is written in pure Rust, fast, and makes extensive use of zero-copy. A lot of care is taken
19to ensure security and safety of this crate, including design (recursion limit, defensive
20programming), tests, and fuzzing. It also aims to be panic-free.
21
22The code is available on [Github](https://github.com/rusticata/der-parser)
23and is part of the [Rusticata](https://github.com/rusticata) project.
24
25# DER parser design
26
27There are two different approaches for parsing DER objects: reading the objects recursively as
28long as the tags are known, or specifying a description of the expected objects (generally from
29the [ASN.1][X.680] description).
30
31The first parsing method can be done using the [`parse_ber`](https://docs.rs/der-parser/latest/der_parser/ber/fn.parse_ber.html) and
32[`parse_der`](https://docs.rs/der-parser/latest/der_parser/der/fn.parse_der.html) methods.
33It is useful when decoding an arbitrary DER object.
34However, it cannot fully parse all objects, especially those containing IMPLICIT, OPTIONAL, or
35DEFINED BY items.
36
37```rust
38use der_parser::parse_der;
39
40let bytes = [ 0x30, 0x0a,
41              0x02, 0x03, 0x01, 0x00, 0x01,
42              0x02, 0x03, 0x01, 0x00, 0x00,
43];
44
45let parsed = parse_der(&bytes);
46```
47
48The second (and preferred) parsing method is to specify the expected objects recursively. The
49following macros can be used:
50[`parse_der_sequence_defined`](https://docs.rs/der-parser/latest/der_parser/macro.parse_der_sequence_defined.html) and similar functions,
51[`parse_der_struct`](https://docs.rs/der-parser/latest/der_parser/macro.parse_der_struct.html), etc.
52
53For example, to read a sequence containing two integers:
54
55```rust
56use der_parser::ber::*;
57use der_parser::error::BerResult;
58
59fn localparse_seq(i:&[u8]) -> BerResult {
60    parse_der_sequence_defined!(i,
61        parse_ber_integer >>
62        parse_ber_integer
63    )
64}
65
66let bytes = [ 0x30, 0x0a,
67              0x02, 0x03, 0x01, 0x00, 0x01,
68              0x02, 0x03, 0x01, 0x00, 0x00,
69];
70let parsed = localparse_seq(&bytes);
71```
72
73All functions return a [`BerResult`](https://docs.rs/der-parser/latest/der_parser/error/type.BerResult.html) object: the parsed
74[`BerObject`](https://docs.rs/der-parser/latest/der_parser/ber/struct.BerObject.html), an `Incomplete` value, or an error.
75
76Note that this type is also a `Result`, so usual functions (`map`, `unwrap` etc.) are available.
77
78# Notes
79
80## BER/DER Integers
81
82DER integers can be of any size, so it is not possible to store them as simple integers (they
83are stored as raw bytes).
84
85To get a simple value, use [`BerObject::as_u32`](ber/struct.BerObject.html#method.as_u32)
86(knowning that this method will return an error if the integer is too large),
87[`BerObject::as_u64`](ber/struct.BerObject.html#method.as_u64), or use the `bigint` feature of
88this crate and use [`BerObject::as_bigint`](https://docs.rs/der-parser/latest/der_parser/ber/struct.BerObject.html#method.as_bigint).
89
90```rust
91use der_parser::ber::*;
92use der_parser::error::BerResult;
93
94let data = &[0x02, 0x03, 0x01, 0x00, 0x01];
95
96let (_, object) = parse_ber_integer(data).expect("parsing failed");
97assert_eq!(object.as_u64(), Ok(65537));
98```
99
100Access to the raw value is possible using the `as_slice` method.
101
102## Misc Notes
103
104- The DER constraints are verified if using `parse_der`.
105- `BerObject` and `DerObject` are the same objects (type alias). The only difference is the
106  verification of constraints *during parsing*.
107
108# Serialization
109
110Support for encoding BER/DER objects is currently being tested and can be used by activating the `serialize` feature.
111Note that current status is **experimental**.
112
113See the `ber_encode_*` functions in the [`ber`](https://docs.rs/der-parser/latest/der_parser/ber/index.html) module, and
114[`BerObject::to_vec`](https://docs.rs/der-parser/latest/der_parser/ber/struct.BerObject.html#method.to_vec)
115
116# References
117
118- [[X.680]] Abstract Syntax Notation One (ASN.1): Specification of basic notation.
119- [[X.690]] ASN.1 encoding rules: Specification of Basic Encoding Rules (BER), Canonical
120  Encoding Rules (CER) and Distinguished Encoding Rules (DER).
121
122[X.680]: http://www.itu.int/rec/T-REC-X.680/en "Abstract Syntax Notation One (ASN.1):
123  Specification of basic notation."
124[X.690]: https://www.itu.int/rec/T-REC-X.690/en "ASN.1 encoding rules: Specification of
125  Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules
126  (DER)."
127<!-- cargo-sync-readme end -->
128
129## Changes
130
131See `CHANGELOG.md`, and `UPGRADING.md` for instructions for upgrading major versions.
132
133## License
134
135Licensed under either of
136
137 * Apache License, Version 2.0
138   ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
139 * MIT license
140   ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
141
142at your option.
143
144## Contribution
145
146Unless you explicitly state otherwise, any contribution intentionally submitted
147for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
148dual licensed as above, without any additional terms or conditions.
149