Crate der_parser [] [src]

DER Parser

A DER (X.690) parser, 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_der method. However, it cannot fully parse all objects, especially those containing IMPLICIT, OPTIONAL, or DEFINED BY items.

use der_parser::parse_der;

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:

use der_parser::*;
use nom::{IResult,Err,ErrorKind};

fn localparse_seq(i:&[u8]) -> IResult<&[u8],DerObject> {
    parse_der_sequence_defined!(i,
        parse_der_integer,
        parse_der_integer
    )
}
let bytes = [ 0x30, 0x0a,
              0x02, 0x03, 0x01, 0x00, 0x01,
              0x02, 0x03, 0x01, 0x00, 0x00,
];
let parsed = localparse_seq(&bytes);

All functions return an IResult object from nom: the parsed DerObject, an Incomplete value, or an error.

Notes

  • The DER constraints are not enforced or verified. Because of that, this parser is mostly compatible with BER.
  • 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 DerObject::as_u32 (knowning that this method will return an error if the integer is too large), or use the bigint feature of this crate and use DerObject::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).

Modules

oid

Object ID (OID) representation

Macros

parse_der_application

Parse an application DER element

parse_der_optional

Parse an optional DER element

parse_der_sequence_defined

Parse a sequence of DER elements (folding version)

parse_der_sequence_defined_m

Parse a sequence of DER elements (macro version)

parse_der_sequence_of

Parse a sequence of identical DER elements

parse_der_set_defined

Parse a set of DER elements (folding version)

parse_der_set_defined_m

Parse a set of DER elements (macro version)

parse_der_set_of

Parse a set of identical DER elements

parse_der_struct

Parse a constructed DER element

parse_der_tagged

Parse a tagged DER element

Structs

DerObject

Representation of a DER-encoded (X.690) object

DerObjectHeader
DerObjectIntoIterator
DerObjectRefIterator
PrettyDer

Enums

DerError
DerObjectContent
DerTag

Defined in X.680 section 8.4

PrettyPrinterFlag

Constants

DER_CLASS_ERROR

Unexpected DER class

DER_INVALID_LENGTH

Invalid length for DER object

DER_OBJ_TOOSHORT

Items contained in a structured object do not fill the entire container object

DER_STRUCT_ERROR

Unexpected DER structured flag

DER_TAG_ERROR

Unexpected DER tag

DER_TAG_UNKNOWN

Unknown or unsupported DER tag

Functions

der_read_element_content
der_read_element_content_as

Parse the next bytes as the content of a DER object.

der_read_element_header
parse_der
parse_der_bitstring
parse_der_bmpstring
parse_der_bool

Read a boolean value

parse_der_enum
parse_der_explicit
parse_der_explicit_failed
parse_der_generalizedtime
parse_der_generalstring
parse_der_ia5string
parse_der_implicit

call der content parsing function

parse_der_integer

Read an integer value

parse_der_null
parse_der_numericstring
parse_der_octetstring
parse_der_oid
parse_der_printablestring
parse_der_sequence

Parse a sequence of DER elements

parse_der_set

Parse a set of DER elements

parse_der_t61string
parse_der_utctime
parse_der_utf8string