Compare commits
6 Commits
fix/13
...
fix/code-c
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f98c8cd3b | |||
| d8e32f63a5 | |||
| 9dae809afa | |||
| 5d07dc7cc3 | |||
| 5bb01ae0e4 | |||
| ce2bf1407c |
@@ -1,12 +1,13 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "simple_asn1"
|
name = "simple_asn1"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
authors = ["Adam Wick <awick@uhsure.com>"]
|
authors = ["Adam Wick <awick@uhsure.com>"]
|
||||||
description = "A simple DER/ASN.1 encoding/decoding library."
|
description = "A simple DER/ASN.1 encoding/decoding library."
|
||||||
categories = ["encoding"]
|
categories = ["encoding"]
|
||||||
keywords = ["ASN1","encoding","DER"]
|
keywords = ["ASN1","encoding","DER"]
|
||||||
license-file = "LICENSE"
|
license-file = "LICENSE"
|
||||||
repository = "https://github.com/acw/simple_asn1"
|
repository = "https://github.com/acw/simple_asn1"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "^0.4.0"
|
chrono = "^0.4.0"
|
||||||
|
|||||||
7
src/ber.rs
Normal file
7
src/ber.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
pub(crate) mod tag;
|
||||||
|
pub(crate) mod length;
|
||||||
|
pub(crate) mod value;
|
||||||
|
|
||||||
|
pub use crate::ber::tag::*;
|
||||||
|
pub use crate::ber::length::*;
|
||||||
|
pub use crate::ber::value::*;
|
||||||
200
src/ber/length.rs
Normal file
200
src/ber/length.rs
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::convert::TryFrom;
|
||||||
|
use crate::ber::tag::Tag;
|
||||||
|
use crate::ber::value::ValueReaderError;
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::ber::tag::{TagClass, TagForm, BasicTagType};
|
||||||
|
use crate::number::Number;
|
||||||
|
use crate::util::BufferReader;
|
||||||
|
#[cfg(test)]
|
||||||
|
use quickcheck::{quickcheck, Arbitrary, Gen};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum Length {
|
||||||
|
Short(usize),
|
||||||
|
Long(Number),
|
||||||
|
Indefinite,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum ConversionError {
|
||||||
|
ValueTooLarge,
|
||||||
|
Unconvertable,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ConversionError> for ValueReaderError {
|
||||||
|
fn from(x: ConversionError) -> ValueReaderError {
|
||||||
|
match x {
|
||||||
|
ConversionError::ValueTooLarge => ValueReaderError::LengthTooBig,
|
||||||
|
ConversionError::Unconvertable => ValueReaderError::LengthIncompatible,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a Length> for usize {
|
||||||
|
type Error = ConversionError;
|
||||||
|
|
||||||
|
fn try_from(x: &Length) -> Result<usize, Self::Error> {
|
||||||
|
match x {
|
||||||
|
Length::Short(x) => Ok(*x),
|
||||||
|
Length::Long(ref v) => usize::try_from(v),
|
||||||
|
Length::Indefinite => Err(ConversionError::Unconvertable),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<usize> for Length {
|
||||||
|
fn from(x: usize) -> Self {
|
||||||
|
Length::Short(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for Length {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> Length {
|
||||||
|
match g.next_u32() % 3 {
|
||||||
|
0 => Length::Short(usize::arbitrary(g) % 128),
|
||||||
|
1 => Length::Long(Number::arbitrary(g)),
|
||||||
|
2 => Length::Indefinite,
|
||||||
|
_ => panic!("Mathematics broke."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum LengthReaderError {
|
||||||
|
NotEnoughData,
|
||||||
|
IllegalConstructedFound,
|
||||||
|
IllegalLong,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum LengthWriterError {
|
||||||
|
SizeTooLarge,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Length {
|
||||||
|
/// Read the next length value from the provided iterator, in the context of the provided tag.
|
||||||
|
/// (In some cases, the tag will allow or disallow certain forms of length field, hence the
|
||||||
|
/// need for the context.)
|
||||||
|
pub fn read<I: Iterator<Item = u8>>(tag: &Tag, it: &mut I) -> Result<Length, LengthReaderError> {
|
||||||
|
let constructed_form_allowed = !tag.has_primitive_form();
|
||||||
|
|
||||||
|
match it.next() {
|
||||||
|
None =>
|
||||||
|
Err(LengthReaderError::NotEnoughData),
|
||||||
|
Some(l) if l < 128 =>
|
||||||
|
Ok(Length::Short(l as usize)),
|
||||||
|
Some(l) if l == 0b1000_0000 && constructed_form_allowed =>
|
||||||
|
Ok(Length::Indefinite),
|
||||||
|
Some(l) if l == 0b1111_1111 =>
|
||||||
|
Err(LengthReaderError::IllegalLong),
|
||||||
|
Some(l) => {
|
||||||
|
let bytelen = (l & 0b0111_1111) as usize;
|
||||||
|
match bytelen.read_buffer(it) {
|
||||||
|
None => Err(LengthReaderError::NotEnoughData),
|
||||||
|
Some(bytes) => {
|
||||||
|
let num = Number::from_bytes(&bytes);
|
||||||
|
Ok(Length::Long(num))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write the start of a length value to the data stream. Unfortunately, for lengths, you may
|
||||||
|
/// also need to write something after the value, as well; for that, use `write_postfix` to
|
||||||
|
/// ensure you frame the length appropriately.
|
||||||
|
pub fn write(&self, buffer: &mut Vec<u8>) -> Result<(), LengthWriterError> {
|
||||||
|
match self {
|
||||||
|
Length::Short(s) if *s > 127 =>
|
||||||
|
Err(LengthWriterError::SizeTooLarge),
|
||||||
|
Length::Short(s) => {
|
||||||
|
buffer.push(*s as u8);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Length::Long(n) => {
|
||||||
|
let bytes = n.serialize();
|
||||||
|
|
||||||
|
if bytes.len() > 127 {
|
||||||
|
return Err(LengthWriterError::SizeTooLarge);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.push((bytes.len() as u8) | 0b1000_0000);
|
||||||
|
for x in bytes.iter() {
|
||||||
|
buffer.push(*x);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Length::Indefinite => {
|
||||||
|
buffer.push(0b1000_0000);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write, if required, the postfix bytes to the object. This is only relevant for a very
|
||||||
|
/// narrow set of types, but I suggest calling it no matter what; it will never do harm to call
|
||||||
|
/// it.
|
||||||
|
pub fn write_postfix(&self, buffer: &mut Vec<u8>) {
|
||||||
|
match self {
|
||||||
|
Length::Indefinite => {
|
||||||
|
buffer.push(0b0000_0000);
|
||||||
|
buffer.push(0b0000_0000);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BufferReader for Length {
|
||||||
|
fn read_buffer<I: Iterator<Item=u8>>(&self, it: &mut I) -> Option<Vec<u8>> {
|
||||||
|
match self {
|
||||||
|
Length::Indefinite => {
|
||||||
|
let mut res = Vec::new();
|
||||||
|
let mut successive_zeros = 0;
|
||||||
|
|
||||||
|
while successive_zeros < 2 {
|
||||||
|
let next = it.next()?;
|
||||||
|
|
||||||
|
if next == 0 {
|
||||||
|
successive_zeros += 1;
|
||||||
|
} else {
|
||||||
|
successive_zeros = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.push(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.truncate(res.len() - 2);
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
Length::Long(x) => match usize::try_from(x) {
|
||||||
|
Err(_) => None,
|
||||||
|
Ok(x) => x.read_buffer(it),
|
||||||
|
}
|
||||||
|
Length::Short(x) => x.read_buffer(it),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
quickcheck! {
|
||||||
|
fn length_bytes_length(l: Length) -> bool {
|
||||||
|
let form = if l == Length::Indefinite { TagForm::Constructed } else { TagForm::Primitive };
|
||||||
|
let tag = Tag::Simple(TagClass::Universal, form, BasicTagType::Boolean);
|
||||||
|
let mut output = Vec::new();
|
||||||
|
l.write(&mut output).unwrap();
|
||||||
|
let mut outiter = output.iter().map(|x| *x);
|
||||||
|
match Length::read(&tag, &mut outiter) {
|
||||||
|
Err(e) => {
|
||||||
|
println!("Error found: {:?}", e);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Ok(l2) => {
|
||||||
|
println!("Result: {:?}", l2);
|
||||||
|
l == l2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
456
src/ber/tag.rs
Normal file
456
src/ber/tag.rs
Normal file
@@ -0,0 +1,456 @@
|
|||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::convert::TryFrom;
|
||||||
|
use core::fmt;
|
||||||
|
use crate::bitstring::BitString;
|
||||||
|
use crate::lift_error;
|
||||||
|
#[cfg(test)]
|
||||||
|
use quickcheck::{quickcheck, Arbitrary, Gen};
|
||||||
|
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum BasicTagType {
|
||||||
|
Boolean = 1,
|
||||||
|
Integer = 2,
|
||||||
|
BitString = 3,
|
||||||
|
OctetString = 4,
|
||||||
|
Null = 5,
|
||||||
|
ObjectIdentifier = 6,
|
||||||
|
ObjectDescriptor = 7,
|
||||||
|
External = 8,
|
||||||
|
Real = 9,
|
||||||
|
Enumerated = 10,
|
||||||
|
EmbeddedBDV = 11,
|
||||||
|
UTF8String = 12,
|
||||||
|
RelativeOID = 13,
|
||||||
|
Sequence = 16,
|
||||||
|
Set = 17,
|
||||||
|
NumericString = 18,
|
||||||
|
PrintableString = 19,
|
||||||
|
TeletexString = 20,
|
||||||
|
VideotexString = 21,
|
||||||
|
IA5String = 22,
|
||||||
|
UTCTime = 23,
|
||||||
|
GeneralizedTime = 24,
|
||||||
|
GraphicString = 25,
|
||||||
|
VisibleString = 26,
|
||||||
|
GeneralString = 27,
|
||||||
|
UniversalString = 28,
|
||||||
|
CharacterString = 29,
|
||||||
|
BMPString = 30,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for BasicTagType {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> BasicTagType {
|
||||||
|
let options = vec![
|
||||||
|
BasicTagType::Boolean,
|
||||||
|
BasicTagType::Integer,
|
||||||
|
BasicTagType::BitString,
|
||||||
|
BasicTagType::OctetString,
|
||||||
|
BasicTagType::Null,
|
||||||
|
BasicTagType::ObjectIdentifier,
|
||||||
|
BasicTagType::ObjectDescriptor,
|
||||||
|
BasicTagType::External,
|
||||||
|
BasicTagType::Real,
|
||||||
|
BasicTagType::Enumerated,
|
||||||
|
BasicTagType::EmbeddedBDV,
|
||||||
|
BasicTagType::UTF8String,
|
||||||
|
BasicTagType::RelativeOID,
|
||||||
|
BasicTagType::Sequence,
|
||||||
|
BasicTagType::Set,
|
||||||
|
BasicTagType::NumericString,
|
||||||
|
BasicTagType::PrintableString,
|
||||||
|
BasicTagType::TeletexString,
|
||||||
|
BasicTagType::VideotexString,
|
||||||
|
BasicTagType::IA5String,
|
||||||
|
BasicTagType::UTCTime,
|
||||||
|
BasicTagType::GeneralizedTime,
|
||||||
|
BasicTagType::GraphicString,
|
||||||
|
BasicTagType::VisibleString,
|
||||||
|
BasicTagType::GeneralString,
|
||||||
|
BasicTagType::UniversalString,
|
||||||
|
BasicTagType::CharacterString,
|
||||||
|
BasicTagType::BMPString,
|
||||||
|
];
|
||||||
|
let index = usize::arbitrary(g) % options.len();
|
||||||
|
options[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TagTypeParseError {
|
||||||
|
UsedReservedSlot,
|
||||||
|
UsedSignalSlot,
|
||||||
|
ValueTooLarge,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for TagTypeParseError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
TagTypeParseError::UsedReservedSlot => write!(
|
||||||
|
f,
|
||||||
|
"Tag type value was one marked reserved in our specification."
|
||||||
|
),
|
||||||
|
TagTypeParseError::UsedSignalSlot => write!(
|
||||||
|
f,
|
||||||
|
"Tag type value was the one that signals a multi-byte tag."
|
||||||
|
),
|
||||||
|
TagTypeParseError::ValueTooLarge => {
|
||||||
|
write!(f, "Tag type value was much too large for us.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u8> for BasicTagType {
|
||||||
|
type Error = TagTypeParseError;
|
||||||
|
|
||||||
|
fn try_from(x: u8) -> Result<BasicTagType, TagTypeParseError> {
|
||||||
|
match x {
|
||||||
|
0 => Err(TagTypeParseError::UsedReservedSlot),
|
||||||
|
1 => Ok(BasicTagType::Boolean),
|
||||||
|
2 => Ok(BasicTagType::Integer),
|
||||||
|
3 => Ok(BasicTagType::BitString),
|
||||||
|
4 => Ok(BasicTagType::OctetString),
|
||||||
|
5 => Ok(BasicTagType::Null),
|
||||||
|
6 => Ok(BasicTagType::ObjectIdentifier),
|
||||||
|
7 => Ok(BasicTagType::ObjectDescriptor),
|
||||||
|
8 => Ok(BasicTagType::External),
|
||||||
|
9 => Ok(BasicTagType::Real),
|
||||||
|
10 => Ok(BasicTagType::Enumerated),
|
||||||
|
11 => Ok(BasicTagType::EmbeddedBDV),
|
||||||
|
12 => Ok(BasicTagType::UTF8String),
|
||||||
|
13 => Ok(BasicTagType::RelativeOID),
|
||||||
|
14 => Err(TagTypeParseError::UsedReservedSlot),
|
||||||
|
15 => Err(TagTypeParseError::UsedReservedSlot),
|
||||||
|
16 => Ok(BasicTagType::Sequence),
|
||||||
|
17 => Ok(BasicTagType::Set),
|
||||||
|
18 => Ok(BasicTagType::NumericString),
|
||||||
|
19 => Ok(BasicTagType::PrintableString),
|
||||||
|
20 => Ok(BasicTagType::TeletexString),
|
||||||
|
21 => Ok(BasicTagType::VideotexString),
|
||||||
|
22 => Ok(BasicTagType::IA5String),
|
||||||
|
23 => Ok(BasicTagType::UTCTime),
|
||||||
|
24 => Ok(BasicTagType::GeneralizedTime),
|
||||||
|
25 => Ok(BasicTagType::GraphicString),
|
||||||
|
26 => Ok(BasicTagType::VisibleString),
|
||||||
|
27 => Ok(BasicTagType::GeneralString),
|
||||||
|
28 => Ok(BasicTagType::UniversalString),
|
||||||
|
29 => Ok(BasicTagType::CharacterString),
|
||||||
|
30 => Ok(BasicTagType::BMPString),
|
||||||
|
31 => Err(TagTypeParseError::UsedSignalSlot),
|
||||||
|
_ => Err(TagTypeParseError::ValueTooLarge),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum TagClass {
|
||||||
|
Universal = 0b00,
|
||||||
|
Application = 0b01,
|
||||||
|
ContextSpecific = 0b10,
|
||||||
|
Private = 0b11,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for TagClass {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> TagClass {
|
||||||
|
let options = vec![
|
||||||
|
TagClass::Universal,
|
||||||
|
TagClass::Application,
|
||||||
|
TagClass::ContextSpecific,
|
||||||
|
TagClass::Private,
|
||||||
|
];
|
||||||
|
let index = usize::arbitrary(g) % options.len();
|
||||||
|
options[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TagClassParseError {
|
||||||
|
TagClassTooLarge,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for TagClassParseError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
TagClassParseError::TagClassTooLarge => write!(f, "Tag class value is too big"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u8> for TagClass {
|
||||||
|
type Error = TagClassParseError;
|
||||||
|
|
||||||
|
fn try_from(x: u8) -> Result<TagClass, TagClassParseError> {
|
||||||
|
match x {
|
||||||
|
0 => Ok(TagClass::Universal),
|
||||||
|
1 => Ok(TagClass::Application),
|
||||||
|
2 => Ok(TagClass::ContextSpecific),
|
||||||
|
3 => Ok(TagClass::Private),
|
||||||
|
_ => Err(TagClassParseError::TagClassTooLarge),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum TagForm {
|
||||||
|
Primitive = 0,
|
||||||
|
Constructed = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for TagForm {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> TagForm {
|
||||||
|
let options = vec![TagForm::Primitive, TagForm::Constructed];
|
||||||
|
let index = usize::arbitrary(g) % options.len();
|
||||||
|
options[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TagFormParseError {
|
||||||
|
TagFormTooLarge,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for TagFormParseError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
TagFormParseError::TagFormTooLarge => write!(f, "Tag form value is more than one bit"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u8> for TagForm {
|
||||||
|
type Error = TagFormParseError;
|
||||||
|
|
||||||
|
fn try_from(x: u8) -> Result<TagForm, TagFormParseError> {
|
||||||
|
match x {
|
||||||
|
0 => Ok(TagForm::Primitive),
|
||||||
|
1 => Ok(TagForm::Constructed),
|
||||||
|
_ => Err(TagFormParseError::TagFormTooLarge),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum Tag {
|
||||||
|
Simple(TagClass, TagForm, BasicTagType),
|
||||||
|
Extended(TagClass, TagForm, Vec<u8>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for Tag {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> Tag {
|
||||||
|
if g.next_u32() & 1 == 0 {
|
||||||
|
Tag::Simple(
|
||||||
|
TagClass::arbitrary(g),
|
||||||
|
TagForm::arbitrary(g),
|
||||||
|
BasicTagType::arbitrary(g),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let mut basic_vec = Vec::<u8>::arbitrary(g);
|
||||||
|
basic_vec.push(u8::arbitrary(g)); // just to ensure there's at least one
|
||||||
|
Tag::Extended(TagClass::arbitrary(g), TagForm::arbitrary(g), basic_vec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TagReaderError {
|
||||||
|
NotEnoughData,
|
||||||
|
InappropriateExtendedLength,
|
||||||
|
TagClassProblem(TagClassParseError),
|
||||||
|
TagFormProblem(TagFormParseError),
|
||||||
|
TagTypeProblem(TagTypeParseError),
|
||||||
|
}
|
||||||
|
|
||||||
|
lift_error!(TagClassParseError, TagClassProblem, TagReaderError);
|
||||||
|
lift_error!(TagFormParseError, TagFormProblem, TagReaderError);
|
||||||
|
lift_error!(TagTypeParseError, TagTypeProblem, TagReaderError);
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TagSerializationError {
|
||||||
|
NoExtendedTag,
|
||||||
|
ExtendedTagTooSmall,
|
||||||
|
InternalError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tag {
|
||||||
|
pub fn has_primitive_form(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Tag::Simple(_, TagForm::Primitive, _) => true,
|
||||||
|
Tag::Extended(_, TagForm::Primitive, _) => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read<I: Iterator<Item = u8>>(it: &mut I) -> Result<Tag, TagReaderError> {
|
||||||
|
match it.next() {
|
||||||
|
None => Err(TagReaderError::NotEnoughData),
|
||||||
|
Some(b) => {
|
||||||
|
let class = TagClass::try_from(b >> 6)?;
|
||||||
|
let form = TagForm::try_from((b >> 5) & 1)?;
|
||||||
|
let tag = b & 0b11111;
|
||||||
|
|
||||||
|
if tag == 31 {
|
||||||
|
let mut bitstr = BitString::new();
|
||||||
|
|
||||||
|
// OK, here's an example of what we have to do here.
|
||||||
|
// Imagine that this tag was four bytes [67,33,30,42]:
|
||||||
|
//
|
||||||
|
// 01000011_00100001_00011110_00101010
|
||||||
|
//
|
||||||
|
// To encode them, we're going to pad the front, and then
|
||||||
|
// group them into sevens:
|
||||||
|
//
|
||||||
|
// 0000100_0011001_0000100_0111100_0101010
|
||||||
|
// 4 25 4 60 42
|
||||||
|
//
|
||||||
|
// We'll then set the high bits on the first 4, giving us
|
||||||
|
// an input to this function of:
|
||||||
|
// 132 153 132 188 42
|
||||||
|
//
|
||||||
|
// On the flip side, to parse, we need to first turn these
|
||||||
|
// back into 8 bit quantities:
|
||||||
|
// 00001000_01100100_00100011_11000101_010
|
||||||
|
let mut ended_clean = false;
|
||||||
|
|
||||||
|
while let Some(b) = it.next() {
|
||||||
|
bitstr.push_bits(7, b);
|
||||||
|
if b & 0b1000_0000 == 0 {
|
||||||
|
ended_clean = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ended_clean {
|
||||||
|
return Err(TagReaderError::NotEnoughData);
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// which is off by three.
|
||||||
|
let padding = bitstr.len() % 8;
|
||||||
|
//
|
||||||
|
// So if we pull three bits off the front we get back to:
|
||||||
|
// 01000011_00100001_00011110_00101010
|
||||||
|
//
|
||||||
|
let mut bititer = bitstr.bits().skip(padding);
|
||||||
|
let mut res = Vec::new();
|
||||||
|
let mut work_byte = 0;
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
while let Some(x) = bititer.next() {
|
||||||
|
work_byte = (work_byte << 1) | (x & 1);
|
||||||
|
count += 1;
|
||||||
|
if count == 8 {
|
||||||
|
res.push(work_byte);
|
||||||
|
count = 0;
|
||||||
|
work_byte = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if count != 0 {
|
||||||
|
return Err(TagReaderError::InappropriateExtendedLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(Tag::Extended(class, form, res));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Tag::Simple(class, form, BasicTagType::try_from(tag)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&self, buffer: &mut Vec<u8>) -> Result<(), TagSerializationError> {
|
||||||
|
match self {
|
||||||
|
Tag::Simple(class, form, basic) => {
|
||||||
|
let class_val = (*class as u8) << 6;
|
||||||
|
let form_val = (*form as u8) << 5;
|
||||||
|
let basic_val = *basic as u8;
|
||||||
|
|
||||||
|
buffer.push(class_val | form_val | basic_val);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Tag::Extended(class, form, value) => {
|
||||||
|
let class_val = (*class as u8) << 6;
|
||||||
|
let form_val = (*form as u8) << 5;
|
||||||
|
let basic_val = 0b00011111;
|
||||||
|
|
||||||
|
if value.len() == 0 {
|
||||||
|
return Err(TagSerializationError::NoExtendedTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.push(class_val | form_val | basic_val);
|
||||||
|
let original_length = value.len() * 8;
|
||||||
|
let mut work_byte = 0;
|
||||||
|
let mut bits_added = if original_length % 7 == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
7 - (original_length % 7)
|
||||||
|
};
|
||||||
|
let mut bitstream = BitString::from(value.iter().map(|x| *x)).bits().peekable();
|
||||||
|
|
||||||
|
while bitstream.peek().is_some() {
|
||||||
|
while bits_added < 7 {
|
||||||
|
match bitstream.next() {
|
||||||
|
None => return Err(TagSerializationError::InternalError),
|
||||||
|
Some(b) => {
|
||||||
|
bits_added += 1;
|
||||||
|
work_byte = (work_byte << 1) | b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.push(0b1000_0000 | work_byte);
|
||||||
|
bits_added = 0;
|
||||||
|
work_byte = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let last_idx = buffer.len() - 1;
|
||||||
|
buffer[last_idx] &= 0b0111_1111;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! item_u8_item {
|
||||||
|
($name: ident, $type: ident) => {
|
||||||
|
#[cfg(test)]
|
||||||
|
quickcheck! {
|
||||||
|
fn $name(t: $type) -> bool {
|
||||||
|
let t8 = t as u8;
|
||||||
|
match $type::try_from(t8) {
|
||||||
|
Err(_) => false,
|
||||||
|
Ok(t2) => t == t2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
item_u8_item!(tag_u8_tag, BasicTagType);
|
||||||
|
item_u8_item!(form_u8_form, TagForm);
|
||||||
|
item_u8_item!(class_u8_class, TagClass);
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
quickcheck! {
|
||||||
|
fn tag_bytes_tag(t: Tag) -> bool {
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
let () = t.write(&mut bytes).unwrap();
|
||||||
|
let mut byteiter = bytes.iter().map(|x| *x);
|
||||||
|
match Tag::read(&mut byteiter) {
|
||||||
|
Err(e) => {
|
||||||
|
// println!("Error result: {:?}", e);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Ok(t2) => {
|
||||||
|
// println!("Result: {:?}", t2);
|
||||||
|
t == t2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
168
src/ber/value.rs
Normal file
168
src/ber/value.rs
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
use crate::ber::length::{Length, LengthReaderError, LengthWriterError};
|
||||||
|
use crate::ber::tag::{Tag, TagClass, TagForm, TagReaderError, TagSerializationError, BasicTagType};
|
||||||
|
use crate::lift_error;
|
||||||
|
use crate::number::Number;
|
||||||
|
use crate::real::Real;
|
||||||
|
use crate::util::BufferReader;
|
||||||
|
|
||||||
|
pub enum Value {
|
||||||
|
Boolean(TagClass, TagForm, bool),
|
||||||
|
Integer(TagClass, TagForm, Number),
|
||||||
|
Null(TagClass, TagForm),
|
||||||
|
Real(TagClass, TagForm, Real),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ValueReaderError {
|
||||||
|
LengthIncompatible,
|
||||||
|
LengthTooBig,
|
||||||
|
NotEnoughData,
|
||||||
|
InvalidFormat(BasicTagType),
|
||||||
|
TagReaderProblem(TagReaderError),
|
||||||
|
LengthReaderError(LengthReaderError),
|
||||||
|
}
|
||||||
|
|
||||||
|
lift_error!(TagReaderError, TagReaderProblem, ValueReaderError);
|
||||||
|
lift_error!(LengthReaderError, ValueReaderError);
|
||||||
|
|
||||||
|
pub enum ValueWriterError {
|
||||||
|
Length(LengthWriterError),
|
||||||
|
Tag(TagSerializationError),
|
||||||
|
}
|
||||||
|
|
||||||
|
lift_error!(LengthWriterError, Length, ValueWriterError);
|
||||||
|
lift_error!(TagSerializationError, Tag, ValueWriterError);
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
/// Read a value from the provided iterator.
|
||||||
|
pub fn read<I: Iterator<Item = u8>>(it: &mut I) -> Result<Value, ValueReaderError> {
|
||||||
|
let tag = Tag::read(it)?;
|
||||||
|
let length = Length::read(&tag, it)?;
|
||||||
|
let mut bytes: Vec<u8> = match length.read_buffer(it) {
|
||||||
|
None => return Err(ValueReaderError::NotEnoughData),
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
|
|
||||||
|
match tag {
|
||||||
|
Tag::Simple(c, f, BasicTagType::Boolean) => {
|
||||||
|
match it.next() {
|
||||||
|
None => Err(ValueReaderError::NotEnoughData),
|
||||||
|
Some(0) => Ok(Value::Boolean(c, f, false)),
|
||||||
|
Some(_) => Ok(Value::Boolean(c, f, true)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Tag::Simple(c, f, BasicTagType::Null) =>
|
||||||
|
Ok(Value::Null(c, f)),
|
||||||
|
|
||||||
|
Tag::Simple(c, f, BasicTagType::Integer) => {
|
||||||
|
let res = Number::from_bytes(&bytes);
|
||||||
|
Ok(Value::Integer(c, f, res))
|
||||||
|
}
|
||||||
|
|
||||||
|
Tag::Simple(c, f, BasicTagType::Real) => {
|
||||||
|
if bytes.len() == 0 {
|
||||||
|
return Err(ValueReaderError::InvalidFormat(BasicTagType::Real));
|
||||||
|
}
|
||||||
|
|
||||||
|
let leader = bytes.remove(0); // has the handy side-effect of making bytes by the
|
||||||
|
// actual value.
|
||||||
|
|
||||||
|
if leader == 0b01000000 {
|
||||||
|
return Ok(Value::Real(c, f, Real::PositiveInfinity));
|
||||||
|
}
|
||||||
|
|
||||||
|
if leader == 0b01000001 {
|
||||||
|
return Ok(Value::Real(c, f, Real::NegativeInfinity));
|
||||||
|
}
|
||||||
|
|
||||||
|
if leader >> 6 == 0b00 {
|
||||||
|
match String::from_utf8(bytes) {
|
||||||
|
Err(_) => return Err(ValueReaderError::InvalidFormat(BasicTagType::Real)),
|
||||||
|
Ok(v) => {
|
||||||
|
let has_e = v.chars().any(|c| (c == 'e') || (c == 'E'));
|
||||||
|
let has_p = v.chars().any(|c| (c == '.'));
|
||||||
|
let nr = leader & 0b00111111;
|
||||||
|
|
||||||
|
match nr {
|
||||||
|
0b01 if !has_e && !has_p => return Ok(Value::Real(c, f, Real::ISO6093(v))),
|
||||||
|
0b10 if !has_e && has_p => return Ok(Value::Real(c, f, Real::ISO6093(v))),
|
||||||
|
0b11 if has_e && has_p => return Ok(Value::Real(c, f, Real::ISO6093(v))),
|
||||||
|
_ => return Err(ValueReaderError::InvalidFormat(BasicTagType::Real)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leader >> 7) == 0 {
|
||||||
|
return Err(ValueReaderError::InvalidFormat(BasicTagType::Real));
|
||||||
|
}
|
||||||
|
|
||||||
|
let positive = (leader >> 6) & 1 == 0;
|
||||||
|
let mant_shift = ((leader >> 2) & 0b11) as usize;
|
||||||
|
let exp_shift = match (leader >> 4) & 0b11 {
|
||||||
|
0b00 => 0,
|
||||||
|
0b01 => 2,
|
||||||
|
0b10 => 3,
|
||||||
|
_ => return Err(ValueReaderError::InvalidFormat(BasicTagType::Real)),
|
||||||
|
} as usize;
|
||||||
|
let explen = match leader & 0b11 {
|
||||||
|
0 => 1,
|
||||||
|
1 => 2,
|
||||||
|
2 => 3,
|
||||||
|
3 => bytes.remove(0),
|
||||||
|
_ => panic!("Mathematics has failed us.")
|
||||||
|
} as usize;
|
||||||
|
|
||||||
|
let mut exponent = Number::from_bytes(&bytes[0..explen]);
|
||||||
|
let mut mantissa = Number::from_bytes(&bytes[explen..]);
|
||||||
|
|
||||||
|
exponent <<= exp_shift;
|
||||||
|
mantissa <<= mant_shift;
|
||||||
|
|
||||||
|
if !positive {
|
||||||
|
mantissa = -mantissa;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Value::Real(c, f, Real::new(exponent, mantissa)))
|
||||||
|
}
|
||||||
|
|
||||||
|
_ =>
|
||||||
|
unimplemented!("Cannot parse tag {:?}", tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serialize the value to the given buffer. Note that this writes the full definiton of the
|
||||||
|
/// value: it's type, it's length, and the value itself.
|
||||||
|
pub fn write(&self, buffer: &mut Vec<u8>) -> Result<(), ValueWriterError> {
|
||||||
|
match self {
|
||||||
|
Value::Boolean(cl, form, v) => {
|
||||||
|
Length::from(1).write(buffer)?;
|
||||||
|
Tag::Simple(*cl, *form, BasicTagType::Boolean).write(buffer)?;
|
||||||
|
if *v {
|
||||||
|
buffer.push(0b10101010);
|
||||||
|
} else {
|
||||||
|
buffer.push(0b00000000);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Integer(c, f, n) => {
|
||||||
|
let mut bytes = n.serialize();
|
||||||
|
Length::from(bytes.len()).write(buffer)?;
|
||||||
|
Tag::Simple(*c, *f, BasicTagType::Integer).write(buffer)?;
|
||||||
|
buffer.append(&mut bytes);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Null(c, f) => {
|
||||||
|
Length::from(0).write(buffer)?;
|
||||||
|
Tag::Simple(*c, *f, BasicTagType::Null).write(buffer)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Real(c, f, r) => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
139
src/bitstring.rs
Normal file
139
src/bitstring.rs
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
pub struct BitString {
|
||||||
|
current_bit: usize,
|
||||||
|
work_byte: u8,
|
||||||
|
contents: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BitIter {
|
||||||
|
current_bit: usize,
|
||||||
|
contents: BitString,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BitString {
|
||||||
|
/// Create a new, empty bit string
|
||||||
|
pub fn new() -> BitString {
|
||||||
|
BitString {
|
||||||
|
current_bit: 7,
|
||||||
|
work_byte: 0,
|
||||||
|
contents: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an iterator over the bits in the BitString
|
||||||
|
pub fn bits(self) -> BitIter {
|
||||||
|
BitIter {
|
||||||
|
current_bit: 0,
|
||||||
|
contents: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add a bit to the end of the bitstring
|
||||||
|
pub fn push_bit(&mut self, x: bool) {
|
||||||
|
let bitval = if x { 1 } else { 0 };
|
||||||
|
self.work_byte |= bitval << self.current_bit;
|
||||||
|
|
||||||
|
if self.current_bit == 0 {
|
||||||
|
self.contents.push(self.work_byte);
|
||||||
|
self.work_byte = 0;
|
||||||
|
self.current_bit = 7;
|
||||||
|
} else {
|
||||||
|
self.current_bit -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add the low `n` bits of the provided byte to the BitString
|
||||||
|
pub fn push_bits(&mut self, mut n: usize, x: u8) {
|
||||||
|
while n > 0 {
|
||||||
|
let bit = (x >> (n - 1)) & 1 == 1;
|
||||||
|
self.push_bit(bit);
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the length of this bitstring, in bits
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
(self.contents.len() * 8) + (7 - self.current_bit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: Iterator<Item = u8>> From<I> for BitString {
|
||||||
|
fn from(x: I) -> BitString {
|
||||||
|
let contents: Vec<u8> = x.collect();
|
||||||
|
BitString {
|
||||||
|
current_bit: contents.len() * 8,
|
||||||
|
work_byte: 0,
|
||||||
|
contents,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for BitIter {
|
||||||
|
type Item = u8;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<u8> {
|
||||||
|
let byte_idx = self.current_bit / 8;
|
||||||
|
let bit_idx = self.current_bit % 8;
|
||||||
|
let shift_amt = 7 - bit_idx;
|
||||||
|
|
||||||
|
// if we're still in the main body of the thing, then we just compute
|
||||||
|
// the offset and shift and be done with it.
|
||||||
|
if byte_idx < self.contents.contents.len() {
|
||||||
|
let byte = self.contents.contents[byte_idx];
|
||||||
|
let retval = byte >> shift_amt;
|
||||||
|
self.current_bit += 1;
|
||||||
|
return Some(retval & 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// just a sanity check; this should reallly never happen.
|
||||||
|
if byte_idx > self.contents.contents.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// in this case, we're processing in the work_byte area of our parent
|
||||||
|
// BitString.
|
||||||
|
if shift_amt <= self.contents.current_bit {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.current_bit += 1;
|
||||||
|
return Some((self.contents.work_byte >> shift_amt) & 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nullary_test() {
|
||||||
|
let bitstr = BitString::new();
|
||||||
|
let bits: Vec<u8> = bitstr.bits().collect();
|
||||||
|
assert_eq!(bits.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_bit() {
|
||||||
|
let mut bitstr = BitString::new();
|
||||||
|
|
||||||
|
bitstr.push_bit(false);
|
||||||
|
bitstr.push_bit(true);
|
||||||
|
bitstr.push_bit(false);
|
||||||
|
bitstr.push_bit(false);
|
||||||
|
bitstr.push_bit(true);
|
||||||
|
bitstr.push_bit(true);
|
||||||
|
bitstr.push_bit(true);
|
||||||
|
bitstr.push_bit(false);
|
||||||
|
bitstr.push_bit(false);
|
||||||
|
let bits: Vec<u8> = bitstr.bits().collect();
|
||||||
|
assert_eq!(bits, vec![0, 1, 0, 0, 1, 1, 1, 0, 0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_bits() {
|
||||||
|
let mut bitstr = BitString::new();
|
||||||
|
|
||||||
|
bitstr.push_bits(5, 0b11111111);
|
||||||
|
let bits: Vec<u8> = bitstr.bits().collect();
|
||||||
|
assert_eq!(bits, vec![1, 1, 1, 1, 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
415
src/lib.rs
415
src/lib.rs
@@ -26,19 +26,18 @@
|
|||||||
//!
|
//!
|
||||||
//! Please send any bug reports, patches, and curses to the GitHub repository
|
//! Please send any bug reports, patches, and curses to the GitHub repository
|
||||||
//! at <code>https://github.com/acw/simple_asn1</code>.
|
//! at <code>https://github.com/acw/simple_asn1</code>.
|
||||||
extern crate chrono;
|
extern crate alloc;
|
||||||
extern crate num_bigint;
|
mod ber;
|
||||||
extern crate num_traits;
|
mod bitstring;
|
||||||
#[cfg(test)]
|
mod number;
|
||||||
#[macro_use]
|
mod real;
|
||||||
extern crate quickcheck;
|
mod util;
|
||||||
#[cfg(test)]
|
|
||||||
extern crate rand;
|
|
||||||
|
|
||||||
use chrono::{DateTime, TimeZone, Utc};
|
use chrono::{DateTime, TimeZone, Utc};
|
||||||
pub use num_bigint::{BigInt, BigUint};
|
pub use num_bigint::{BigInt, BigUint};
|
||||||
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
|
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
|
||||||
use std::error::Error;
|
#[cfg(test)]
|
||||||
|
use quickcheck::quickcheck;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
@@ -160,62 +159,43 @@ impl ASN1Block {
|
|||||||
impl PartialEq for ASN1Block {
|
impl PartialEq for ASN1Block {
|
||||||
fn eq(&self, other: &ASN1Block) -> bool {
|
fn eq(&self, other: &ASN1Block) -> bool {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&ASN1Block::Boolean(_,a1),
|
(&ASN1Block::Boolean(_, a1), &ASN1Block::Boolean(_, a2)) => (a1 == a2),
|
||||||
&ASN1Block::Boolean(_,a2)) =>
|
(&ASN1Block::Integer(_, ref a1), &ASN1Block::Integer(_, ref a2)) => (a1 == a2),
|
||||||
(a1 == a2),
|
(&ASN1Block::BitString(_, a1, ref b1), &ASN1Block::BitString(_, a2, ref b2)) => {
|
||||||
(&ASN1Block::Integer(_,ref a1),
|
(a1 == a2) && (b1 == b2)
|
||||||
&ASN1Block::Integer(_,ref a2)) =>
|
}
|
||||||
(a1 == a2),
|
(&ASN1Block::OctetString(_, ref a1), &ASN1Block::OctetString(_, ref a2)) => (a1 == a2),
|
||||||
(&ASN1Block::BitString(_,a1,ref b1),
|
(&ASN1Block::Null(_), &ASN1Block::Null(_)) => true,
|
||||||
&ASN1Block::BitString(_,a2,ref b2)) =>
|
(&ASN1Block::ObjectIdentifier(_, ref a1), &ASN1Block::ObjectIdentifier(_, ref a2)) => {
|
||||||
(a1 == a2) && (b1 == b2),
|
a1 == a2
|
||||||
(&ASN1Block::OctetString(_,ref a1),
|
}
|
||||||
&ASN1Block::OctetString(_,ref a2)) =>
|
(&ASN1Block::UTF8String(_, ref a1), &ASN1Block::UTF8String(_, ref a2)) => (a1 == a2),
|
||||||
(a1 == a2),
|
(&ASN1Block::PrintableString(_, ref a1), &ASN1Block::PrintableString(_, ref a2)) => {
|
||||||
(&ASN1Block::Null(_),
|
a1 == a2
|
||||||
&ASN1Block::Null(_)) =>
|
}
|
||||||
true,
|
(&ASN1Block::TeletexString(_, ref a1), &ASN1Block::TeletexString(_, ref a2)) => {
|
||||||
(&ASN1Block::ObjectIdentifier(_,ref a1),
|
a1 == a2
|
||||||
&ASN1Block::ObjectIdentifier(_,ref a2)) =>
|
}
|
||||||
(a1 == a2),
|
(&ASN1Block::IA5String(_, ref a1), &ASN1Block::IA5String(_, ref a2)) => (a1 == a2),
|
||||||
(&ASN1Block::UTF8String(_,ref a1),
|
(&ASN1Block::UTCTime(_, ref a1), &ASN1Block::UTCTime(_, ref a2)) => (a1 == a2),
|
||||||
&ASN1Block::UTF8String(_,ref a2)) =>
|
(&ASN1Block::GeneralizedTime(_, ref a1), &ASN1Block::GeneralizedTime(_, ref a2)) => {
|
||||||
(a1 == a2),
|
a1 == a2
|
||||||
(&ASN1Block::PrintableString(_,ref a1),
|
}
|
||||||
&ASN1Block::PrintableString(_,ref a2)) =>
|
(&ASN1Block::UniversalString(_, ref a1), &ASN1Block::UniversalString(_, ref a2)) => {
|
||||||
(a1 == a2),
|
a1 == a2
|
||||||
(&ASN1Block::TeletexString(_,ref a1),
|
}
|
||||||
&ASN1Block::TeletexString(_,ref a2)) =>
|
(&ASN1Block::BMPString(_, ref a1), &ASN1Block::BMPString(_, ref a2)) => (a1 == a2),
|
||||||
(a1 == a2),
|
(&ASN1Block::Sequence(_, ref a1), &ASN1Block::Sequence(_, ref a2)) => (a1 == a2),
|
||||||
(&ASN1Block::IA5String(_,ref a1),
|
(&ASN1Block::Set(_, ref a1), &ASN1Block::Set(_, ref a2)) => (a1 == a2),
|
||||||
&ASN1Block::IA5String(_,ref a2)) =>
|
(
|
||||||
(a1 == a2),
|
&ASN1Block::Explicit(a1, _, ref b1, ref c1),
|
||||||
(&ASN1Block::UTCTime(_,ref a1),
|
&ASN1Block::Explicit(a2, _, ref b2, ref c2),
|
||||||
&ASN1Block::UTCTime(_,ref a2)) =>
|
) => (a1 == a2) && (b1 == b2) && (c1 == c2),
|
||||||
(a1 == a2),
|
(
|
||||||
(&ASN1Block::GeneralizedTime(_,ref a1),
|
&ASN1Block::Unknown(a1, b1, _, ref c1, ref d1),
|
||||||
&ASN1Block::GeneralizedTime(_,ref a2)) =>
|
&ASN1Block::Unknown(a2, b2, _, ref c2, ref d2),
|
||||||
(a1 == a2),
|
) => (a1 == a2) && (b1 == b2) && (c1 == c2) && (d1 == d2),
|
||||||
(&ASN1Block::UniversalString(_,ref a1),
|
_ => false,
|
||||||
&ASN1Block::UniversalString(_,ref a2)) =>
|
|
||||||
(a1 == a2),
|
|
||||||
(&ASN1Block::BMPString(_,ref a1),
|
|
||||||
&ASN1Block::BMPString(_,ref a2)) =>
|
|
||||||
(a1 == a2),
|
|
||||||
(&ASN1Block::Sequence(_,ref a1),
|
|
||||||
&ASN1Block::Sequence(_,ref a2)) =>
|
|
||||||
(a1 == a2),
|
|
||||||
(&ASN1Block::Set(_,ref a1),
|
|
||||||
&ASN1Block::Set(_,ref a2)) =>
|
|
||||||
(a1 == a2),
|
|
||||||
(&ASN1Block::Explicit(a1,_,ref b1,ref c1),
|
|
||||||
&ASN1Block::Explicit(a2,_,ref b2,ref c2)) =>
|
|
||||||
(a1 == a2) && (b1 == b2) && (c1 == c2),
|
|
||||||
(&ASN1Block::Unknown(a1,b1,_,ref c1,ref d1),
|
|
||||||
&ASN1Block::Unknown(a2,b2,_,ref c2,ref d2)) =>
|
|
||||||
(a1 == a2) && (b1 == b2) && (c1 == c2) && (d1 == d2),
|
|
||||||
_ =>
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,70 +304,33 @@ pub enum ASN1DecodeErr {
|
|||||||
///
|
///
|
||||||
/// Invalid ASN.1 input can lead to this error.
|
/// Invalid ASN.1 input can lead to this error.
|
||||||
Incomplete,
|
Incomplete,
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
__Nonexhaustive,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for ASN1DecodeErr {
|
impl fmt::Display for ASN1DecodeErr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ASN1DecodeErr::EmptyBuffer =>
|
ASN1DecodeErr::EmptyBuffer => {
|
||||||
write!(f, "Encountered an empty buffer decoding ASN1 block."),
|
write!(f, "Encountered an empty buffer decoding ASN1 block.")
|
||||||
ASN1DecodeErr::BadBooleanLength(x) =>
|
|
||||||
write!(f, "Bad length field in boolean block: {}", x),
|
|
||||||
ASN1DecodeErr::LengthTooLarge(x) =>
|
|
||||||
write!(f, "Length field too large for object type: {}", x),
|
|
||||||
ASN1DecodeErr::UTF8DecodeFailure(x) =>
|
|
||||||
write!(f, "UTF8 string failed to properly decode: {}", x),
|
|
||||||
ASN1DecodeErr::PrintableStringDecodeFailure =>
|
|
||||||
write!(f, "Printable string failed to properly decode."),
|
|
||||||
ASN1DecodeErr::InvalidDateValue(x) =>
|
|
||||||
write!(f, "Invalid date value: {}", x),
|
|
||||||
ASN1DecodeErr::InvalidBitStringLength(i) =>
|
|
||||||
write!(f, "Invalid length of bit string: {}", i),
|
|
||||||
ASN1DecodeErr::InvalidClass(i) =>
|
|
||||||
write!(f, "Invalid class value: {}", i),
|
|
||||||
ASN1DecodeErr::Incomplete =>
|
|
||||||
write!(f, "Incomplete data or invalid ASN1"),
|
|
||||||
ASN1DecodeErr::__Nonexhaustive =>
|
|
||||||
panic!("A non exhaustive error should not be constructed"),
|
|
||||||
}
|
}
|
||||||
|
ASN1DecodeErr::BadBooleanLength(x) => {
|
||||||
|
write!(f, "Bad length field in boolean block: {}", x)
|
||||||
}
|
}
|
||||||
|
ASN1DecodeErr::LengthTooLarge(x) => {
|
||||||
|
write!(f, "Length field too large for object type: {}", x)
|
||||||
}
|
}
|
||||||
|
ASN1DecodeErr::UTF8DecodeFailure(x) => {
|
||||||
impl Error for ASN1DecodeErr {
|
write!(f, "UTF8 string failed to properly decode: {}", x)
|
||||||
fn description(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
ASN1DecodeErr::EmptyBuffer =>
|
|
||||||
"Encountered an empty buffer decoding ASN1 block.",
|
|
||||||
ASN1DecodeErr::BadBooleanLength(_) =>
|
|
||||||
"Bad length field in boolean block.",
|
|
||||||
ASN1DecodeErr::LengthTooLarge(_) =>
|
|
||||||
"Length field too large for object type.",
|
|
||||||
ASN1DecodeErr::UTF8DecodeFailure(_) =>
|
|
||||||
"UTF8 string failed to properly decode.",
|
|
||||||
ASN1DecodeErr::PrintableStringDecodeFailure =>
|
|
||||||
"Printable string failed to properly decode.",
|
|
||||||
ASN1DecodeErr::InvalidDateValue(_) =>
|
|
||||||
"Invalid date value.",
|
|
||||||
ASN1DecodeErr::InvalidClass(_) =>
|
|
||||||
"Invalid class value",
|
|
||||||
ASN1DecodeErr::InvalidBitStringLength(_) =>
|
|
||||||
"Invalid length of bit string",
|
|
||||||
ASN1DecodeErr::Incomplete =>
|
|
||||||
"Incomplete data or invalid ASN1",
|
|
||||||
ASN1DecodeErr::__Nonexhaustive =>
|
|
||||||
panic!("A non exhaustive error should not be constructed"),
|
|
||||||
}
|
}
|
||||||
|
ASN1DecodeErr::PrintableStringDecodeFailure => {
|
||||||
|
write!(f, "Printable string failed to properly decode.")
|
||||||
}
|
}
|
||||||
|
ASN1DecodeErr::InvalidDateValue(x) => write!(f, "Invalid date value: {}", x),
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
ASN1DecodeErr::InvalidBitStringLength(i) => {
|
||||||
None
|
write!(f, "Invalid length of bit string: {}", i)
|
||||||
|
}
|
||||||
|
ASN1DecodeErr::InvalidClass(i) => write!(f, "Invalid class value: {}", i),
|
||||||
|
ASN1DecodeErr::Incomplete => write!(f, "Incomplete data or invalid ASN1"),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,28 +344,17 @@ pub enum ASN1EncodeErr {
|
|||||||
|
|
||||||
impl fmt::Display for ASN1EncodeErr {
|
impl fmt::Display for ASN1EncodeErr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.write_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for ASN1EncodeErr {
|
|
||||||
fn description(&self) -> &str {
|
|
||||||
match self {
|
match self {
|
||||||
ASN1EncodeErr::ObjectIdentHasTooFewFields =>
|
ASN1EncodeErr::ObjectIdentHasTooFewFields => {
|
||||||
"ASN1 object identifier has too few fields.",
|
write!(f, "ASN1 object identifier has too few fields.")
|
||||||
ASN1EncodeErr::ObjectIdentVal1TooLarge =>
|
}
|
||||||
"First value in ASN1 OID is too big.",
|
ASN1EncodeErr::ObjectIdentVal1TooLarge => {
|
||||||
ASN1EncodeErr::ObjectIdentVal2TooLarge =>
|
write!(f, "First value in ASN1 OID is too big.")
|
||||||
"Second value in ASN1 OID is too big."
|
}
|
||||||
|
ASN1EncodeErr::ObjectIdentVal2TooLarge => {
|
||||||
|
write!(f, "Second value in ASN1 OID is too big.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,9 +426,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
result.push(ASN1Block::Integer(soff, res));
|
result.push(ASN1Block::Integer(soff, res));
|
||||||
}
|
}
|
||||||
// BIT STRING
|
// BIT STRING
|
||||||
Some(0x03) if body.len() == 0 => {
|
Some(0x03) if body.len() == 0 => result.push(ASN1Block::BitString(soff, 0, Vec::new())),
|
||||||
result.push(ASN1Block::BitString(soff, 0, Vec::new()))
|
|
||||||
}
|
|
||||||
Some(0x03) => {
|
Some(0x03) => {
|
||||||
let bits = (&body[1..]).to_vec();
|
let bits = (&body[1..]).to_vec();
|
||||||
let bitcount = bits.len() * 8;
|
let bitcount = bits.len() * 8;
|
||||||
@@ -511,9 +441,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
result.push(ASN1Block::BitString(soff, nbits, bits))
|
result.push(ASN1Block::BitString(soff, nbits, bits))
|
||||||
}
|
}
|
||||||
// OCTET STRING
|
// OCTET STRING
|
||||||
Some(0x04) => {
|
Some(0x04) => result.push(ASN1Block::OctetString(soff, body.to_vec())),
|
||||||
result.push(ASN1Block::OctetString(soff, body.to_vec()))
|
|
||||||
}
|
|
||||||
// NULL
|
// NULL
|
||||||
Some(0x05) => {
|
Some(0x05) => {
|
||||||
result.push(ASN1Block::Null(soff));
|
result.push(ASN1Block::Null(soff));
|
||||||
@@ -548,32 +476,20 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
result.push(ASN1Block::ObjectIdentifier(soff, res))
|
result.push(ASN1Block::ObjectIdentifier(soff, res))
|
||||||
}
|
}
|
||||||
// UTF8STRING
|
// UTF8STRING
|
||||||
Some(0x0C) => {
|
Some(0x0C) => match String::from_utf8(body.to_vec()) {
|
||||||
match String::from_utf8(body.to_vec()) {
|
Ok(v) => result.push(ASN1Block::UTF8String(soff, v)),
|
||||||
Ok(v) =>
|
Err(e) => return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error())),
|
||||||
result.push(ASN1Block::UTF8String(soff, v)),
|
},
|
||||||
Err(e) =>
|
|
||||||
return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// SEQUENCE
|
// SEQUENCE
|
||||||
Some(0x10) => {
|
Some(0x10) => match from_der_(body, start_offset + index) {
|
||||||
match from_der_(body, start_offset + index) {
|
Ok(items) => result.push(ASN1Block::Sequence(soff, items)),
|
||||||
Ok(items) =>
|
Err(e) => return Err(e),
|
||||||
result.push(ASN1Block::Sequence(soff, items)),
|
},
|
||||||
Err(e) =>
|
|
||||||
return Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// SET
|
// SET
|
||||||
Some(0x11) => {
|
Some(0x11) => match from_der_(body, start_offset + index) {
|
||||||
match from_der_(body, start_offset + index) {
|
Ok(items) => result.push(ASN1Block::Set(soff, items)),
|
||||||
Ok(items) =>
|
Err(e) => return Err(e),
|
||||||
result.push(ASN1Block::Set(soff, items)),
|
},
|
||||||
Err(e) =>
|
|
||||||
return Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// PRINTABLE STRING
|
// PRINTABLE STRING
|
||||||
Some(0x13) => {
|
Some(0x13) => {
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
@@ -589,14 +505,10 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
result.push(ASN1Block::PrintableString(soff, res));
|
result.push(ASN1Block::PrintableString(soff, res));
|
||||||
}
|
}
|
||||||
// TELETEX STRINGS
|
// TELETEX STRINGS
|
||||||
Some(0x14) => {
|
Some(0x14) => match String::from_utf8(body.to_vec()) {
|
||||||
match String::from_utf8(body.to_vec()) {
|
Ok(v) => result.push(ASN1Block::TeletexString(soff, v)),
|
||||||
Ok(v) =>
|
Err(e) => return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error())),
|
||||||
result.push(ASN1Block::TeletexString(soff, v)),
|
},
|
||||||
Err(e) =>
|
|
||||||
return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// IA5 (ASCII) STRING
|
// IA5 (ASCII) STRING
|
||||||
Some(0x16) => {
|
Some(0x16) => {
|
||||||
let val = body.iter().map(|x| *x as char);
|
let val = body.iter().map(|x| *x as char);
|
||||||
@@ -611,11 +523,8 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
|
|
||||||
let v = String::from_iter(body.iter().map(|x| *x as char));
|
let v = String::from_iter(body.iter().map(|x| *x as char));
|
||||||
match Utc.datetime_from_str(&v, "%y%m%d%H%M%SZ") {
|
match Utc.datetime_from_str(&v, "%y%m%d%H%M%SZ") {
|
||||||
Err(_) =>
|
Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)),
|
||||||
return Err(ASN1DecodeErr::InvalidDateValue(v)),
|
Ok(t) => result.push(ASN1Block::UTCTime(soff, t)),
|
||||||
Ok(t) => {
|
|
||||||
result.push(ASN1Block::UTCTime(soff, t))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// GeneralizedTime
|
// GeneralizedTime
|
||||||
@@ -641,34 +550,29 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
|
|||||||
v.insert(idx, '0');
|
v.insert(idx, '0');
|
||||||
}
|
}
|
||||||
match Utc.datetime_from_str(&v, "%Y%m%d%H%M%S.%fZ") {
|
match Utc.datetime_from_str(&v, "%Y%m%d%H%M%S.%fZ") {
|
||||||
Err(_) =>
|
Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)),
|
||||||
return Err(ASN1DecodeErr::InvalidDateValue(v)),
|
Ok(t) => result.push(ASN1Block::GeneralizedTime(soff, t)),
|
||||||
Ok(t) => {
|
|
||||||
result.push(ASN1Block::GeneralizedTime(soff, t))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// UNIVERSAL STRINGS
|
|
||||||
Some(0x1C) => {
|
|
||||||
match String::from_utf8(body.to_vec()) {
|
|
||||||
Ok(v) =>
|
|
||||||
result.push(ASN1Block::UniversalString(soff, v)),
|
|
||||||
Err(e) =>
|
|
||||||
return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// UNIVERSAL STRINGS
|
// UNIVERSAL STRINGS
|
||||||
Some(0x1E) => {
|
Some(0x1C) => match String::from_utf8(body.to_vec()) {
|
||||||
match String::from_utf8(body.to_vec()) {
|
Ok(v) => result.push(ASN1Block::UniversalString(soff, v)),
|
||||||
Ok(v) =>
|
Err(e) => return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error())),
|
||||||
result.push(ASN1Block::BMPString(soff, v)),
|
},
|
||||||
Err(e) =>
|
// UNIVERSAL STRINGS
|
||||||
return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error()))
|
Some(0x1E) => match String::from_utf8(body.to_vec()) {
|
||||||
}
|
Ok(v) => result.push(ASN1Block::BMPString(soff, v)),
|
||||||
}
|
Err(e) => return Err(ASN1DecodeErr::UTF8DecodeFailure(e.utf8_error())),
|
||||||
|
},
|
||||||
// Dunno.
|
// Dunno.
|
||||||
_ => {
|
_ => {
|
||||||
result.push(ASN1Block::Unknown(class, constructed, soff, tag, body.to_vec()));
|
result.push(ASN1Block::Unknown(
|
||||||
|
class,
|
||||||
|
constructed,
|
||||||
|
soff,
|
||||||
|
tag,
|
||||||
|
body.to_vec(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index += len;
|
index += len;
|
||||||
@@ -837,7 +741,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
|
|||||||
|
|
||||||
// first, validate that the first two items meet spec
|
// first, validate that the first two items meet spec
|
||||||
if v1 > &two {
|
if v1 > &two {
|
||||||
return Err(ASN1EncodeErr::ObjectIdentVal1TooLarge)
|
return Err(ASN1EncodeErr::ObjectIdentVal1TooLarge);
|
||||||
}
|
}
|
||||||
|
|
||||||
let u175 = BigUint::from_u8(175).unwrap();
|
let u175 = BigUint::from_u8(175).unwrap();
|
||||||
@@ -871,9 +775,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
|
|||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => Err(ASN1EncodeErr::ObjectIdentHasTooFewFields),
|
||||||
Err(ASN1EncodeErr::ObjectIdentHasTooFewFields)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// SEQUENCE
|
// SEQUENCE
|
||||||
@@ -948,18 +850,24 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
|
|||||||
res.append(&mut body);
|
res.append(&mut body);
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
&ASN1Block::UTF8String(_, ref str) =>
|
&ASN1Block::UTF8String(_, ref str) => {
|
||||||
encode_asn1_string(0x0c, false, ASN1Class::Universal, str),
|
encode_asn1_string(0x0c, false, ASN1Class::Universal, str)
|
||||||
&ASN1Block::PrintableString(_, ref str) =>
|
}
|
||||||
encode_asn1_string(0x13, true, ASN1Class::Universal, str),
|
&ASN1Block::PrintableString(_, ref str) => {
|
||||||
&ASN1Block::TeletexString(_, ref str) =>
|
encode_asn1_string(0x13, true, ASN1Class::Universal, str)
|
||||||
encode_asn1_string(0x14, false, ASN1Class::Universal, str),
|
}
|
||||||
&ASN1Block::UniversalString(_, ref str) =>
|
&ASN1Block::TeletexString(_, ref str) => {
|
||||||
encode_asn1_string(0x1c, false, ASN1Class::Universal, str),
|
encode_asn1_string(0x14, false, ASN1Class::Universal, str)
|
||||||
&ASN1Block::IA5String(_, ref str) =>
|
}
|
||||||
encode_asn1_string(0x16, true, ASN1Class::Universal, str),
|
&ASN1Block::UniversalString(_, ref str) => {
|
||||||
&ASN1Block::BMPString(_, ref str) =>
|
encode_asn1_string(0x1c, false, ASN1Class::Universal, str)
|
||||||
encode_asn1_string(0x1e, false, ASN1Class::Universal, str),
|
}
|
||||||
|
&ASN1Block::IA5String(_, ref str) => {
|
||||||
|
encode_asn1_string(0x16, true, ASN1Class::Universal, str)
|
||||||
|
}
|
||||||
|
&ASN1Block::BMPString(_, ref str) => {
|
||||||
|
encode_asn1_string(0x1e, false, ASN1Class::Universal, str)
|
||||||
|
}
|
||||||
&ASN1Block::Explicit(class, _, ref tag, ref item) => {
|
&ASN1Block::Explicit(class, _, ref tag, ref item) => {
|
||||||
let mut tagbytes = encode_tag(class, true, tag);
|
let mut tagbytes = encode_tag(class, true, tag);
|
||||||
let mut bytes = to_der(item)?;
|
let mut bytes = to_der(item)?;
|
||||||
@@ -1053,12 +961,9 @@ fn encode_base127(v: &BigUint) -> Vec<u8> {
|
|||||||
acc = acc >> 7;
|
acc = acc >> 7;
|
||||||
|
|
||||||
match digit.to_u8() {
|
match digit.to_u8() {
|
||||||
None =>
|
None => panic!("7 bits don't fit into 8, cause ..."),
|
||||||
panic!("7 bits don't fit into 8, cause ..."),
|
Some(x) if res.is_empty() => res.push(x),
|
||||||
Some(x) if res.is_empty() =>
|
Some(x) => res.push(x | 0x80),
|
||||||
res.push(x),
|
|
||||||
Some(x) =>
|
|
||||||
res.push(x | 0x80)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1075,7 +980,6 @@ fn encode_class(c: ASN1Class) -> u8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn encode_len(x: usize) -> Vec<u8> {
|
fn encode_len(x: usize) -> Vec<u8> {
|
||||||
if x < 128 {
|
if x < 128 {
|
||||||
vec![x as u8]
|
vec![x as u8]
|
||||||
@@ -1642,63 +1546,4 @@ mod tests {
|
|||||||
assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]);
|
assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn openssl_ec_key_test() {
|
|
||||||
use ASN1Block::{Sequence, Explicit, Integer, ObjectIdentifier,
|
|
||||||
OctetString};
|
|
||||||
|
|
||||||
// Create openssl ec key
|
|
||||||
let private_key = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
|
||||||
let der = to_der(
|
|
||||||
&ASN1Block::Sequence(0, vec![
|
|
||||||
// Version
|
|
||||||
ASN1Block::Integer(0, 1.into()),
|
|
||||||
// Private key
|
|
||||||
ASN1Block::OctetString(0, private_key.clone()),
|
|
||||||
// Parameters
|
|
||||||
// Explicitely tagged oid
|
|
||||||
// Oid: 1, 2, 840, 10045, 3, 1, 7,
|
|
||||||
ASN1Block::Explicit(ASN1Class::ContextSpecific, 0, 0u8.into(),
|
|
||||||
Box::new(ASN1Block::ObjectIdentifier(0,
|
|
||||||
OID::new(vec![
|
|
||||||
1u8.into(), 2u8.into(), 840u16.into(),
|
|
||||||
10045u16.into(), 3u8.into(), 1u8.into(), 7u8.into(),
|
|
||||||
])
|
|
||||||
)))
|
|
||||||
])
|
|
||||||
).unwrap();
|
|
||||||
let der_data = vec![48, 25, 2, 1, 1, 4, 8, 0, 1, 2, 3, 4, 5, 6, 7, 160,
|
|
||||||
10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7];
|
|
||||||
|
|
||||||
assert_eq!(der, der_data);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
from_der(&der_data),
|
|
||||||
Ok(vec![Sequence(0, vec![Integer(2, 1u8.into()),
|
|
||||||
OctetString(5, private_key),
|
|
||||||
Explicit(ASN1Class::ContextSpecific, 15, 0u8.into(),
|
|
||||||
Box::new(ObjectIdentifier(17, OID(vec![1u8.into(),
|
|
||||||
2u8.into(), 840u16.into(), 10045u16.into(), 3u8.into(),
|
|
||||||
1u8.into(), 7u8.into(),
|
|
||||||
]))))])]),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn encode_unknowns() {
|
|
||||||
fn encode_structure(bufs: &[Vec<u8>]) -> Vec<u8> {
|
|
||||||
let mut body = Vec::new();
|
|
||||||
for (i, buf) in bufs.iter().enumerate() {
|
|
||||||
let mut der = to_der(&ASN1Block::Unknown(ASN1Class::ContextSpecific, false, 0, BigUint::from_usize(i).unwrap(), buf.to_vec())).unwrap();
|
|
||||||
body.append(&mut der);
|
|
||||||
}
|
|
||||||
let block = ASN1Block::Unknown(ASN1Class::ContextSpecific, true, 0, BigUint::from_u8(0).unwrap(), body);
|
|
||||||
to_der(&block).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
let decoded = from_der(&encode_structure(&vec![vec![0]])).unwrap();
|
|
||||||
let expected = [ASN1Block::Unknown(ASN1Class::ContextSpecific, true, 0, BigUint::from_u8(0).unwrap(), vec![128, 1, 0])];
|
|
||||||
assert_eq!(decoded, expected);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
201
src/number.rs
Normal file
201
src/number.rs
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
use core::convert::TryFrom;
|
||||||
|
use core::ops::{Neg, ShlAssign};
|
||||||
|
use crate::ber::length::ConversionError;
|
||||||
|
#[cfg(test)]
|
||||||
|
use quickcheck::{quickcheck, Arbitrary, Gen};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct Number {
|
||||||
|
value: Vec<u64>,
|
||||||
|
bits: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for Number {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> Number {
|
||||||
|
let bytes = u8::arbitrary(g) as usize;
|
||||||
|
let digits = (bytes + 7) / 8;
|
||||||
|
let bits = bytes * 8;
|
||||||
|
|
||||||
|
let mut value = Vec::with_capacity(digits);
|
||||||
|
for _ in 0..digits {
|
||||||
|
value.push(g.next_u64());
|
||||||
|
}
|
||||||
|
|
||||||
|
if digits > 0 {
|
||||||
|
let spare_bits = (digits * 64) - bits;
|
||||||
|
let mask = 0xFFFFFFFFFFFFFFFFu64 >> spare_bits;
|
||||||
|
value[digits - 1] &= mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
Number {
|
||||||
|
value,
|
||||||
|
bits,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Number {
|
||||||
|
pub fn new() -> Number {
|
||||||
|
Number {
|
||||||
|
value: Vec::new(),
|
||||||
|
bits: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialize(&self) -> Vec<u8> {
|
||||||
|
let serialized_bytes = (self.bits + 7) / 8;
|
||||||
|
let mut res = Vec::with_capacity(serialized_bytes);
|
||||||
|
|
||||||
|
for idx in 0..serialized_bytes {
|
||||||
|
let byte_off = serialized_bytes - idx - 1;
|
||||||
|
let val64_off = byte_off / 8;
|
||||||
|
let internal_bit_off = (byte_off % 8) * 8;
|
||||||
|
let val = (self.value[val64_off] >> internal_bit_off) & 0xff;
|
||||||
|
|
||||||
|
res.push(val as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_bytes(bytes: &[u8]) -> Number {
|
||||||
|
let bits = bytes.len() * 8;
|
||||||
|
let digit_len = (bytes.len() + 7) / 8;
|
||||||
|
let mut value = Vec::with_capacity(digit_len);
|
||||||
|
let mut bytes_added = 0;
|
||||||
|
let mut next = 0u64;
|
||||||
|
|
||||||
|
for x in bytes.iter().rev() {
|
||||||
|
next += (*x as u64) << (bytes_added * 8);
|
||||||
|
bytes_added += 1;
|
||||||
|
if bytes_added == 8 {
|
||||||
|
value.push(next);
|
||||||
|
next = 0;
|
||||||
|
bytes_added = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes_added != 0 {
|
||||||
|
value.push(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
Number { value, bits }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[test]
|
||||||
|
fn basic_serialization() {
|
||||||
|
assert_eq!(Number::new().serialize(), vec![]);
|
||||||
|
//
|
||||||
|
let one = Number {
|
||||||
|
value: vec![1],
|
||||||
|
bits: 8,
|
||||||
|
};
|
||||||
|
let onevec = vec![1];
|
||||||
|
assert_eq!(one.serialize(), onevec);
|
||||||
|
assert_eq!(Number::from_bytes(&onevec), one);
|
||||||
|
//
|
||||||
|
let one_oh_oh_one = Number {
|
||||||
|
value: vec![0x1001],
|
||||||
|
bits: 16,
|
||||||
|
};
|
||||||
|
let one_oh_oh_one_vec = vec![0x10,0x01];
|
||||||
|
assert_eq!(one_oh_oh_one.serialize(), one_oh_oh_one_vec);
|
||||||
|
assert_eq!(Number::from_bytes(&one_oh_oh_one_vec), one_oh_oh_one);
|
||||||
|
//
|
||||||
|
let one_to_nine = Number {
|
||||||
|
value: vec![0x0807060504030201, 0x09],
|
||||||
|
bits: 72,
|
||||||
|
};
|
||||||
|
let one_to_nine_vec = vec![0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01];
|
||||||
|
assert_eq!(one_to_nine.serialize(), one_to_nine_vec);
|
||||||
|
assert_eq!(Number::from_bytes(&one_to_nine_vec), one_to_nine);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct SmallByteArray {
|
||||||
|
a: Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Arbitrary for SmallByteArray {
|
||||||
|
fn arbitrary<G: Gen>(g: &mut G) -> SmallByteArray {
|
||||||
|
let len = u8::arbitrary(g);
|
||||||
|
let mut a = Vec::with_capacity(len as usize);
|
||||||
|
for _ in 0..len {
|
||||||
|
a.push(u8::arbitrary(g));
|
||||||
|
}
|
||||||
|
SmallByteArray{ a }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
quickcheck! {
|
||||||
|
fn bytes_num_bytes(x: SmallByteArray) -> bool {
|
||||||
|
let num = Number::from_bytes(&x.a);
|
||||||
|
let y = num.serialize();
|
||||||
|
println!("x.a: {:?}", x.a);
|
||||||
|
println!("y: {:?}", y);
|
||||||
|
&x.a == &y
|
||||||
|
}
|
||||||
|
|
||||||
|
fn num_bytes_num(x: Number) -> bool {
|
||||||
|
let bytes = x.serialize();
|
||||||
|
let y = Number::from_bytes(&bytes);
|
||||||
|
println!("x: {:?}", x);
|
||||||
|
println!("b: {:?}", bytes);
|
||||||
|
println!("y: {:?}", y);
|
||||||
|
x == y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for Number {
|
||||||
|
fn from(x: u8) -> Number {
|
||||||
|
Number {
|
||||||
|
value: vec![x as u64],
|
||||||
|
bits: 8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a Number> for usize {
|
||||||
|
type Error = ConversionError;
|
||||||
|
|
||||||
|
fn try_from(x: &Number) -> Result<Self, Self::Error> {
|
||||||
|
if x.value.iter().skip(1).all(|v| *v == 0) {
|
||||||
|
if x.value.len() == 0 {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut value = x.value[0];
|
||||||
|
|
||||||
|
if x.bits < 64 {
|
||||||
|
value &= 0xFFFFFFFFFFFFFFFFu64 >> (64 - x.bits);
|
||||||
|
}
|
||||||
|
|
||||||
|
match usize::try_from(value) {
|
||||||
|
Err(_) => Err(ConversionError::ValueTooLarge),
|
||||||
|
Ok(v) => Ok(v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(ConversionError::ValueTooLarge)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShlAssign<usize> for Number {
|
||||||
|
fn shl_assign(&mut self, amt: usize) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Neg for Number {
|
||||||
|
type Output = Number;
|
||||||
|
|
||||||
|
fn neg(self) -> Number {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/real.rs
Normal file
23
src/real.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::number::Number;
|
||||||
|
|
||||||
|
pub enum Real {
|
||||||
|
PositiveInfinity,
|
||||||
|
NegativeInfinity,
|
||||||
|
ISO6093(String),
|
||||||
|
Binary(RealNumber),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Real {
|
||||||
|
pub fn new(exponent: Number, mantissa: Number) -> Real {
|
||||||
|
Real::Binary(RealNumber{
|
||||||
|
exponent,
|
||||||
|
mantissa,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RealNumber {
|
||||||
|
exponent: Number,
|
||||||
|
mantissa: Number,
|
||||||
|
}
|
||||||
|
|
||||||
33
src/util.rs
Normal file
33
src/util.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
pub trait BufferReader {
|
||||||
|
fn read_buffer<I: Iterator<Item=u8>>(&self, it: &mut I) -> Option<Vec<u8>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BufferReader for usize {
|
||||||
|
fn read_buffer<I: Iterator<Item=u8>>(&self, it: &mut I) -> Option<Vec<u8>> {
|
||||||
|
let me = *self;
|
||||||
|
let mut res = Vec::with_capacity(me);
|
||||||
|
|
||||||
|
while res.len() < me {
|
||||||
|
let n = it.next()?;
|
||||||
|
res.push(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! lift_error {
|
||||||
|
($fromt: ident, $tot: ident) => {
|
||||||
|
lift_error!($fromt, $fromt, $tot);
|
||||||
|
};
|
||||||
|
($fromt: ident, $const: ident, $tot: ident) => {
|
||||||
|
impl From<$fromt> for $tot {
|
||||||
|
fn from(x: $fromt) -> $tot {
|
||||||
|
$tot::$const(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user