1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
//! 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);
//! }
//! ```
use std::fmt::{self, Display};
mod der;
mod writer;
pub use der::*;
pub use writer::*;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Error {
BadBooleanValue,
LeadingZero,
LessThanMinimum,
LongLengthNotSupported,
HighTagNumberForm,
Io,
NegativeValue,
NonCanonical,
NonZeroUnusedBits,
Read,
UnexpectedEnd,
UnknownTag,
WrongTag,
WrongValue,
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
Error::BadBooleanValue => "bad boolean value",
Error::LeadingZero => "leading zero",
Error::LessThanMinimum => "less than minimum",
Error::LongLengthNotSupported => "long length not supported",
Error::HighTagNumberForm => "high tag number form",
Error::Io => "I/O",
Error::NegativeValue => "negative value",
Error::NonCanonical => "non-canonical",
Error::NonZeroUnusedBits => "non-zero unused bits",
Error::Read => "read",
Error::UnexpectedEnd => "unexpected end",
Error::UnknownTag => "unknown tag",
Error::WrongTag => "wrong tag",
Error::WrongValue => "wrong value",
};
s.fmt(f)
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::BadBooleanValue => "bad boolean value",
Error::LeadingZero => "leading zero",
Error::LessThanMinimum => "less than minimum",
Error::LongLengthNotSupported => "long length not supported",
Error::HighTagNumberForm => "high tag number form",
Error::Io => "I/O",
Error::NegativeValue => "negative value",
Error::NonCanonical => "non-canonical",
Error::NonZeroUnusedBits => "non-zero unused bits",
Error::Read => "read",
Error::UnexpectedEnd => "unexpected end",
Error::UnknownTag => "unknown tag",
Error::WrongTag => "wrong tag",
Error::WrongValue => "wrong value",
}
}
}
impl From<untrusted::EndOfInput> for Error {
fn from(_: untrusted::EndOfInput) -> Error {
Error::UnexpectedEnd
}
}
impl From<::std::io::Error> for Error {
fn from(_: ::std::io::Error) -> Error {
Error::Io
}
}
/// Alias for `Result<T, Error>`
pub type Result<T> = ::std::result::Result<T, Error>;