Crate derp [] [src]

DER Parser (and Writer)

extern crate derp;
extern crate untrusted;

use derp::{Tag, Der};
use untrusted::Input;

const MY_DATA: &'static [u8] = &[
    0x30, 0x18,                                             // sequence
        0x05, 0x00,                                         // null
        0x30, 0x0e,                                         // sequence
            0x02, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // x
            0x02, 0x04, 0x0a, 0x0b, 0x0c, 0x0d,             // y
        0x03, 0x04, 0x00, 0xff, 0xff, 0xff,                 // bits
];

fn main() {
    let input = Input::from(MY_DATA);
    let (x, y, bits) = input.read_all(derp::Error::Read, |input| {
        derp::nested(input, Tag::Sequence, |input| {
            derp::read_null(input)?;
            let (x, y) = derp::nested(input, Tag::Sequence, |input| {
                let x = derp::positive_integer(input)?;
                let y = derp::positive_integer(input)?;
                Ok((x.as_slice_less_safe(), y.as_slice_less_safe()))
            })?;
            let bits = derp::bit_string_with_no_unused_bits(input)?;
            Ok((x, y, bits.as_slice_less_safe()))
        })
    }).unwrap();

    assert_eq!(x, &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
    assert_eq!(y, &[0x0a, 0x0b, 0x0c, 0x0d]);
    assert_eq!(bits, &[0xff, 0xff, 0xff]);

    let mut buf = Vec::new();
    {
        let mut der = Der::new(&mut buf);
        der.sequence(|der| {
            der.null()?;
            der.sequence(|der| {
                der.integer(x)?;
                der.integer(y)
            })?;
            der.bit_string(0, bits)
        }).unwrap();
    }

    assert_eq!(buf.as_slice(), MY_DATA);
}

Structs

Der

Helper for writing DER that automattically encoes tags and content lengths.

Enums

Error
Tag

ASN.1 Tags

Functions

bit_string_with_no_unused_bits

Read a BIT STRING with leading byte 0x00 signifying no unused bits.

boolean

Parse a boolean value.

expect_tag_and_get_value

Read a tag and return it's value. Errors when the expect and actual tag do not match.

length_of_length

For a given length, how many bytes are required to represent it in DER form.

nested

Return the value of the given tag and apply a decoding function to it.

nonnegative_integer

Return a non-negative integer.

positive_integer

Parses a positive DER integer, returning the big-endian-encoded value, sans any leading zero byte.

read_null
read_tag_and_get_value

Read the next tag, and return it and its value.

small_nonnegative_integer

Parse as integer with a value in the in the range [0, 255], returning its numeric value. This is typically used for parsing version numbers.

Type Definitions

Result

Alias for Result<T, Error>