40 Commits

Author SHA1 Message Date
05507c2199 Add some CI. 2025-01-12 13:43:06 -08:00
6043fc6d6a Clean up some new warnings re parentheses. 2025-01-12 13:41:37 -08:00
74719369dd Bump version to 0.6.2. 2022-06-03 08:25:50 -07:00
4b04d52dae Merge pull request #31 from connec/quickcheck-dev
Set `time/quickcheck` feature only in dev
2022-03-13 20:19:31 -07:00
Chris Connelly
6cca562598 Set time/quickcheck feature only in dev
This prevents `quickcheck` appearing in the regular build dependencies.
2022-03-13 20:09:14 +00:00
e1d5d27db6 Merge pull request #30 from tsoutsman/master
Use `vec!` in `oid!` macro
2022-01-03 14:57:56 -08:00
Klim Tsoutsman
430d907c42 Use vec! in oid! macro
Signed-off-by: Klim Tsoutsman <klimusha@gmail.com>
2022-01-03 22:06:40 +11:00
b0277bb0ad Bump version number to cover security and clippy/fmt patches. 2021-11-14 13:51:47 -08:00
4df1173ecb Fix formatting to rustfmt standards. 2021-11-14 13:50:25 -08:00
f4755e9b37 Merge pull request #29 from nmathewson/clippy_warnings
Fix all clippy warnings in the current code under Rust 1.56.
2021-11-14 13:49:33 -08:00
61533920f6 Merge pull request #28 from nmathewson/fix_string_slice
[security] Fix a panic from an unchecked string slice.
2021-11-14 13:45:34 -08:00
Nick Mathewson
9ab60920d2 Fix all clippy warnings in the current code under Rust 1.56. 2021-11-14 15:40:24 -05:00
Nick Mathewson
c981004290 Fix a panic from an unchecked string slice.
When slicing a string, you get a panic if you do so at any point
other than at a character boundary.  This happened in the
implementation of UTCTime parsing.

This bug was introduced in bc156c36d7,
and appears to affect only version 0.6.0.

I've tried using the clippy::string_slice lint to confirm that there
are not any other string slices in this code.

Fixes bug #27.  Found via fuzzing.
2021-11-14 09:46:36 -05:00
bc156c36d7 Bump version number to reflect API change. 2021-10-20 10:08:40 -07:00
fc5e1e7e97 Merge pull request #26 from Adam-Gleave/remove-chrono
Remove `chrono` dependency in favour of `time` crate
2021-10-20 10:07:18 -07:00
Adam-Gleave
34c0267144 Remove comment 2021-10-20 17:51:30 +01:00
Adam-Gleave
d7d39d7095 Remove chrono dependency in favour of time crate 2021-10-20 17:30:37 +01:00
77a9c2b48f Bump rand version to 0.8.4, and crate version to 0.5.4. 2021-06-24 19:34:14 -07:00
4ea0b30437 Merge pull request #25 from randombit/jack/add-crate
Add $crate:: metavariable to the oid! macro
2021-06-24 19:30:56 -07:00
Jack Lloyd
fcba9cb4be Add $crate:: metavariable to the oid! macro
This allows using the oid macro without having to import BigUint
2021-06-22 12:14:39 -04:00
1568c1bbf9 Merge pull request #24 from randombit/jack/add-eq-for-oid
impl Eq for OID
2021-06-03 17:14:22 -07:00
Jack Lloyd
3bc5c14a19 impl Eq for OID
Closes #23
2021-06-03 16:56:40 -04:00
35cdea3dbe Bump version to 0.5.3 2021-05-18 08:06:33 -07:00
1e141a9393 Merge pull request #22 from zartarn15/master
Implement as_vec for OID
2021-05-18 08:05:09 -07:00
Oleksandr Kravchenko
2d2d9a3bbb Implement as_vec for OID
Change-Id: I29d59b77b541454007343488f8f809c3052ad3e3
2021-05-18 12:46:58 +03:00
19a466bb07 Bump the version to 0.5.2. 2021-04-18 13:20:33 -07:00
c585acc530 Upgrade the QuickCheck/rand dependencies.
This also adds a check in the synthesis of arbitary items of unknown type,
to ensure that we don't accidentally create objects with a known type. I'm
actually not 100% sure how the old tests were passing so reliably, which
is worrying.
2021-04-18 13:17:01 -07:00
483e692109 Merge pull request #21 from stanislav-tkach/patch-1
Update the `num-bigint` crate version
2021-04-17 13:08:54 -07:00
Stanislav Tkach
8305ec24d7 Update the num-bigint crate version 2021-04-14 15:17:23 +03:00
04ee38744f Merge pull request #20 from seinlin/issue-19
Add the missing N character.
2021-02-25 12:02:12 -08:00
Seinlin Li
1903cb6ab5 Add the missing N character. 2021-02-25 01:33:17 -08:00
90d24b6d89 Version number bump (0.5.0 --> 0.5.1) 2021-01-08 09:20:05 -08:00
fcab888856 Merge pull request #18 from rib/wip/rib/thiserror
Use thiserror macro to derive Error + Display traits
2021-01-08 09:16:43 -08:00
Robert Bragg
559db125d3 Use thiserror macro to derive Error + Display traits
This removes the manual implementation of Display + Error for
ASN1EncodeErr and uses the derive macro from `thiserror` to add
std Error and Display implementations for ANS1EncodeErr and
ASN1DecodeErr.
2021-01-08 13:53:53 +00:00
1a246d5970 Merge branch 'master' of github.com:acw/simple_asn1 2020-10-19 20:23:32 -07:00
7f836fc3ed Merge pull request #16 from c410-f3r/master
Update dependencies, clean up some random number generation.
2020-10-19 10:22:12 -07:00
Caio
7db7a487f4 Update dependencies 2020-10-17 22:59:31 -03:00
7b6e48d8fb Bump minor version, to address license change. 2020-07-15 08:20:25 -07:00
c7bc3f9513 Merge pull request #15 from marco-neumann-jdas/fix_license_metadata
use a machine-readable license key
2020-07-15 08:18:46 -07:00
Marco Neumann
d4ac17598a use a machine-readable license key
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-license-and-license-file-fields :

> If a package is using a nonstandard license, then the license-file field may be specified in lieu of the license
> field.

`simple_asn1` however uses a standard license (ISC, as per LICENSE).
Note that this is also a somewhat commonly used way by many projects
(see for example Cargo or serde).

Using the machine-readable SPX specifier instead of a text file helps
automatic tooling like [cargo-lichking](https://github.com/Nemo157/cargo-lichking) to check licenses of projects and
dependencies.
2020-07-13 15:21:05 +02:00
11 changed files with 362 additions and 1484 deletions

33
.github/workflows/rust.yml vendored Normal file
View File

@@ -0,0 +1,33 @@
name: Rust
on:
push:
branches: [ "develop", "ci" ]
pull_request:
branches: [ "develop" ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
default: true
override: true
- name: Format Check
run: cargo fmt --check
- name: Build
run: cargo build
- name: Run tests
run: cargo test

View File

@@ -1,19 +1,21 @@
[package] [package]
name = "simple_asn1" name = "simple_asn1"
version = "0.5.0" version = "0.6.2"
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 = "ISC"
repository = "https://github.com/acw/simple_asn1" repository = "https://github.com/acw/simple_asn1"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
chrono = "^0.4.0" num-bigint = { default-features = false, version = "0.4" }
num-bigint = "^0.2.0" num-traits = { default-features = false, version = "0.2" }
num-traits = "^0.2.0" thiserror = { default-features = false, version = "1" }
time = { default-features = false, version = "0.3", features = ["formatting", "macros", "parsing"] }
[dev-dependencies] [dev-dependencies]
quickcheck = "^0.7.1" quickcheck = "1.0.3"
rand = "0.5.5" rand = "0.8.4"
time = { default-features = false, version = "0.3", features = ["formatting", "macros", "parsing", "quickcheck"] }

View File

@@ -1,7 +0,0 @@
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::*;

View File

@@ -1,200 +0,0 @@
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
}
}
}
}

View File

@@ -1,456 +0,0 @@
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
}
}
}
}

View File

@@ -1,168 +0,0 @@
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!()
}
}
}
}

View File

@@ -1,139 +0,0 @@
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]);
}
}

View File

@@ -26,22 +26,16 @@
//! //!
//! 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 alloc;
mod ber;
mod bitstring;
mod number;
mod real;
mod util;
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};
#[cfg(test)] #[cfg(test)]
use quickcheck::quickcheck; use quickcheck::quickcheck;
use std::fmt; use std::convert::TryFrom;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::mem::size_of; use std::mem::size_of;
use std::str::Utf8Error; use std::str::Utf8Error;
use thiserror::Error;
use time::PrimitiveDateTime;
/// An ASN.1 block class. /// An ASN.1 block class.
/// ///
@@ -84,8 +78,8 @@ pub enum ASN1Block {
PrintableString(usize, String), PrintableString(usize, String),
TeletexString(usize, String), TeletexString(usize, String),
IA5String(usize, String), IA5String(usize, String),
UTCTime(usize, DateTime<Utc>), UTCTime(usize, PrimitiveDateTime),
GeneralizedTime(usize, DateTime<Utc>), GeneralizedTime(usize, PrimitiveDateTime),
UniversalString(usize, String), UniversalString(usize, String),
BMPString(usize, String), BMPString(usize, String),
Sequence(usize, Vec<ASN1Block>), Sequence(usize, Vec<ASN1Block>),
@@ -108,50 +102,49 @@ impl ASN1Block {
/// Get the class associated with the given ASN1Block, regardless of what /// Get the class associated with the given ASN1Block, regardless of what
/// kind of block it is. /// kind of block it is.
pub fn class(&self) -> ASN1Class { pub fn class(&self) -> ASN1Class {
match self { match *self {
&ASN1Block::Boolean(_, _) => ASN1Class::Universal, ASN1Block::Boolean(_, _) => ASN1Class::Universal,
&ASN1Block::Integer(_, _) => ASN1Class::Universal, ASN1Block::Integer(_, _) => ASN1Class::Universal,
&ASN1Block::BitString(_, _, _) => ASN1Class::Universal, ASN1Block::BitString(_, _, _) => ASN1Class::Universal,
&ASN1Block::OctetString(_, _) => ASN1Class::Universal, ASN1Block::OctetString(_, _) => ASN1Class::Universal,
&ASN1Block::Null(_) => ASN1Class::Universal, ASN1Block::Null(_) => ASN1Class::Universal,
&ASN1Block::ObjectIdentifier(_, _) => ASN1Class::Universal, ASN1Block::ObjectIdentifier(_, _) => ASN1Class::Universal,
&ASN1Block::UTF8String(_, _) => ASN1Class::Universal, ASN1Block::UTF8String(_, _) => ASN1Class::Universal,
&ASN1Block::PrintableString(_, _) => ASN1Class::Universal, ASN1Block::PrintableString(_, _) => ASN1Class::Universal,
&ASN1Block::TeletexString(_, _) => ASN1Class::Universal, ASN1Block::TeletexString(_, _) => ASN1Class::Universal,
&ASN1Block::IA5String(_, _) => ASN1Class::Universal, ASN1Block::IA5String(_, _) => ASN1Class::Universal,
&ASN1Block::UTCTime(_, _) => ASN1Class::Universal, ASN1Block::UTCTime(_, _) => ASN1Class::Universal,
&ASN1Block::GeneralizedTime(_, _) => ASN1Class::Universal, ASN1Block::GeneralizedTime(_, _) => ASN1Class::Universal,
&ASN1Block::UniversalString(_, _) => ASN1Class::Universal, ASN1Block::UniversalString(_, _) => ASN1Class::Universal,
&ASN1Block::BMPString(_, _) => ASN1Class::Universal, ASN1Block::BMPString(_, _) => ASN1Class::Universal,
&ASN1Block::Sequence(_, _) => ASN1Class::Universal, ASN1Block::Sequence(_, _) => ASN1Class::Universal,
&ASN1Block::Set(_, _) => ASN1Class::Universal, ASN1Block::Set(_, _) => ASN1Class::Universal,
&ASN1Block::Explicit(c, _, _, _) => c, ASN1Block::Explicit(c, _, _, _) => c,
&ASN1Block::Unknown(c, _, _, _, _) => c, ASN1Block::Unknown(c, _, _, _, _) => c,
} }
} }
/// Get the starting offset associated with the given ASN1Block, regardless /// Get the starting offset associated with the given ASN1Block, regardless
/// of what kind of block it is. /// of what kind of block it is.
pub fn offset(&self) -> usize { pub fn offset(&self) -> usize {
match self { match *self {
&ASN1Block::Boolean(o, _) => o, ASN1Block::Boolean(o, _) => o,
&ASN1Block::Integer(o, _) => o, ASN1Block::Integer(o, _) => o,
&ASN1Block::BitString(o, _, _) => o, ASN1Block::BitString(o, _, _) => o,
&ASN1Block::OctetString(o, _) => o, ASN1Block::OctetString(o, _) => o,
&ASN1Block::Null(o) => o, ASN1Block::Null(o) => o,
&ASN1Block::ObjectIdentifier(o, _) => o, ASN1Block::ObjectIdentifier(o, _) => o,
&ASN1Block::UTF8String(o, _) => o, ASN1Block::UTF8String(o, _) => o,
&ASN1Block::PrintableString(o, _) => o, ASN1Block::PrintableString(o, _) => o,
&ASN1Block::TeletexString(o, _) => o, ASN1Block::TeletexString(o, _) => o,
&ASN1Block::IA5String(o, _) => o, ASN1Block::IA5String(o, _) => o,
&ASN1Block::UTCTime(o, _) => o, ASN1Block::UTCTime(o, _) => o,
&ASN1Block::GeneralizedTime(o, _) => o, ASN1Block::GeneralizedTime(o, _) => o,
&ASN1Block::UniversalString(o, _) => o, ASN1Block::UniversalString(o, _) => o,
&ASN1Block::BMPString(o, _) => o, ASN1Block::BMPString(o, _) => o,
&ASN1Block::Sequence(o, _) => o, ASN1Block::Sequence(o, _) => o,
&ASN1Block::Set(o, _) => o, ASN1Block::Set(o, _) => o,
&ASN1Block::Explicit(_, o, _, _) => o, ASN1Block::Explicit(_, o, _, _) => o,
&ASN1Block::Unknown(_, _, o, _, _) => o, ASN1Block::Unknown(_, _, o, _, _) => o,
} }
} }
} }
@@ -159,34 +152,34 @@ 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(_, a2)) => (a1 == a2), (&ASN1Block::Boolean(_, a1), &ASN1Block::Boolean(_, a2)) => a1 == a2,
(&ASN1Block::Integer(_, ref a1), &ASN1Block::Integer(_, ref a2)) => (a1 == a2), (&ASN1Block::Integer(_, ref a1), &ASN1Block::Integer(_, ref a2)) => a1 == a2,
(&ASN1Block::BitString(_, a1, ref b1), &ASN1Block::BitString(_, a2, ref b2)) => { (&ASN1Block::BitString(_, a1, ref b1), &ASN1Block::BitString(_, a2, ref b2)) => {
(a1 == a2) && (b1 == b2) (a1 == a2) && (b1 == b2)
} }
(&ASN1Block::OctetString(_, ref a1), &ASN1Block::OctetString(_, ref a2)) => (a1 == a2), (&ASN1Block::OctetString(_, ref a1), &ASN1Block::OctetString(_, ref a2)) => a1 == a2,
(&ASN1Block::Null(_), &ASN1Block::Null(_)) => true, (&ASN1Block::Null(_), &ASN1Block::Null(_)) => true,
(&ASN1Block::ObjectIdentifier(_, ref a1), &ASN1Block::ObjectIdentifier(_, ref a2)) => { (&ASN1Block::ObjectIdentifier(_, ref a1), &ASN1Block::ObjectIdentifier(_, ref a2)) => {
a1 == a2 a1 == a2
} }
(&ASN1Block::UTF8String(_, ref a1), &ASN1Block::UTF8String(_, ref a2)) => (a1 == a2), (&ASN1Block::UTF8String(_, ref a1), &ASN1Block::UTF8String(_, ref a2)) => a1 == a2,
(&ASN1Block::PrintableString(_, ref a1), &ASN1Block::PrintableString(_, ref a2)) => { (&ASN1Block::PrintableString(_, ref a1), &ASN1Block::PrintableString(_, ref a2)) => {
a1 == a2 a1 == a2
} }
(&ASN1Block::TeletexString(_, ref a1), &ASN1Block::TeletexString(_, ref a2)) => { (&ASN1Block::TeletexString(_, ref a1), &ASN1Block::TeletexString(_, ref a2)) => {
a1 == a2 a1 == a2
} }
(&ASN1Block::IA5String(_, ref a1), &ASN1Block::IA5String(_, ref a2)) => (a1 == a2), (&ASN1Block::IA5String(_, ref a1), &ASN1Block::IA5String(_, ref a2)) => a1 == a2,
(&ASN1Block::UTCTime(_, ref a1), &ASN1Block::UTCTime(_, ref a2)) => (a1 == a2), (&ASN1Block::UTCTime(_, ref a1), &ASN1Block::UTCTime(_, ref a2)) => a1 == a2,
(&ASN1Block::GeneralizedTime(_, ref a1), &ASN1Block::GeneralizedTime(_, ref a2)) => { (&ASN1Block::GeneralizedTime(_, ref a1), &ASN1Block::GeneralizedTime(_, ref a2)) => {
a1 == a2 a1 == a2
} }
(&ASN1Block::UniversalString(_, ref a1), &ASN1Block::UniversalString(_, ref a2)) => { (&ASN1Block::UniversalString(_, ref a1), &ASN1Block::UniversalString(_, ref a2)) => {
a1 == a2 a1 == a2
} }
(&ASN1Block::BMPString(_, ref a1), &ASN1Block::BMPString(_, 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::Sequence(_, ref a1), &ASN1Block::Sequence(_, ref a2)) => a1 == a2,
(&ASN1Block::Set(_, ref a1), &ASN1Block::Set(_, ref a2)) => (a1 == a2), (&ASN1Block::Set(_, ref a1), &ASN1Block::Set(_, ref a2)) => a1 == a2,
( (
&ASN1Block::Explicit(a1, _, ref b1, ref c1), &ASN1Block::Explicit(a1, _, ref b1, ref c1),
&ASN1Block::Explicit(a2, _, ref b2, ref c2), &ASN1Block::Explicit(a2, _, ref b2, ref c2),
@@ -201,7 +194,7 @@ impl PartialEq for ASN1Block {
} }
/// An ASN.1 OID. /// An ASN.1 OID.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct OID(Vec<BigUint>); pub struct OID(Vec<BigUint>);
impl OID { impl OID {
@@ -239,7 +232,7 @@ impl OID {
// now we can build all the rest of the body // now we can build all the rest of the body
let mut body = vec![byte1]; let mut body = vec![byte1];
for num in self.0.iter().skip(2) { for num in self.0.iter().skip(2) {
let mut local = encode_base127(&num); let mut local = encode_base127(num);
body.append(&mut local); body.append(&mut local);
} }
@@ -248,6 +241,19 @@ impl OID {
_ => Err(ASN1EncodeErr::ObjectIdentHasTooFewFields), _ => Err(ASN1EncodeErr::ObjectIdentHasTooFewFields),
} }
} }
pub fn as_vec<'a, T: TryFrom<&'a BigUint>>(&'a self) -> Result<Vec<T>, ASN1DecodeErr> {
let mut vec = Vec::new();
for val in self.0.iter() {
let ul = match T::try_from(val) {
Ok(a) => a,
Err(_) => return Err(ASN1DecodeErr::Overflow),
};
vec.push(ul);
}
Ok(vec)
}
} }
impl<'a> PartialEq<OID> for &'a OID { impl<'a> PartialEq<OID> for &'a OID {
@@ -276,88 +282,58 @@ impl<'a> PartialEq<OID> for &'a OID {
#[macro_export] #[macro_export]
macro_rules! oid { macro_rules! oid {
( $( $e: expr ),* ) => {{ ( $( $e: expr ),* ) => {{
let mut res = Vec::new(); $crate::OID::new(vec![$($crate::BigUint::from($e as u64)),*])
$(
res.push(BigUint::from($e as u64));
)*
OID::new(res)
}}; }};
} }
const PRINTABLE_CHARS: &'static str = const PRINTABLE_CHARS: &str =
"ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+,-./:=? "; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+,-./:=? ";
#[cfg(test)]
const KNOWN_TAGS: &[u8] = &[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0c, 0x10, 0x11, 0x13, 0x14, 0x16, 0x17, 0x18, 0x1c, 0x1e,
];
/// An error that can arise decoding ASN.1 primitive blocks. /// An error that can arise decoding ASN.1 primitive blocks.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Error, PartialEq)]
pub enum ASN1DecodeErr { pub enum ASN1DecodeErr {
#[error("Encountered an empty buffer decoding ASN1 block.")]
EmptyBuffer, EmptyBuffer,
#[error("Bad length field in boolean block: {0}")]
BadBooleanLength(usize), BadBooleanLength(usize),
#[error("Length field too large for object type: {0}")]
LengthTooLarge(usize), LengthTooLarge(usize),
#[error("UTF8 string failed to properly decode: {0}")]
UTF8DecodeFailure(Utf8Error), UTF8DecodeFailure(Utf8Error),
#[error("Printable string failed to properly decode.")]
PrintableStringDecodeFailure, PrintableStringDecodeFailure,
#[error("Invalid date value: {0}")]
InvalidDateValue(String), InvalidDateValue(String),
#[error("Invalid length of bit string: {0}")]
InvalidBitStringLength(isize), InvalidBitStringLength(isize),
/// Not a valid ASN.1 class /// Not a valid ASN.1 class
#[error("Invalid class value: {0}")]
InvalidClass(u8), InvalidClass(u8),
/// Expected more input /// Expected more input
/// ///
/// Invalid ASN.1 input can lead to this error. /// Invalid ASN.1 input can lead to this error.
#[error("Incomplete data or invalid ASN1")]
Incomplete, Incomplete,
} #[error("Value overflow")]
Overflow,
impl fmt::Display for ASN1DecodeErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ASN1DecodeErr::EmptyBuffer => {
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"),
}
}
} }
/// An error that can arise encoding ASN.1 primitive blocks. /// An error that can arise encoding ASN.1 primitive blocks.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Error, PartialEq)]
pub enum ASN1EncodeErr { pub enum ASN1EncodeErr {
#[error("ASN1 object identifier has too few fields.")]
ObjectIdentHasTooFewFields, ObjectIdentHasTooFewFields,
#[error("First value in ASN1 OID is too big.")]
ObjectIdentVal1TooLarge, ObjectIdentVal1TooLarge,
#[error("Second value in ASN1 OID is too big.")]
ObjectIdentVal2TooLarge, ObjectIdentVal2TooLarge,
} }
impl fmt::Display for ASN1EncodeErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ASN1EncodeErr::ObjectIdentHasTooFewFields => {
write!(f, "ASN1 object identifier has too few fields.")
}
ASN1EncodeErr::ObjectIdentVal1TooLarge => {
write!(f, "First value in ASN1 OID is too big.")
}
ASN1EncodeErr::ObjectIdentVal2TooLarge => {
write!(f, "Second value in ASN1 OID is too big.")
}
}
}
}
/// Translate a binary blob into a series of `ASN1Block`s, or provide an /// Translate a binary blob into a series of `ASN1Block`s, or provide an
/// error if it didn't work. /// error if it didn't work.
pub fn from_der(i: &[u8]) -> Result<Vec<ASN1Block>, ASN1DecodeErr> { pub fn from_der(i: &[u8]) -> Result<Vec<ASN1Block>, ASN1DecodeErr> {
@@ -384,8 +360,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
if class != ASN1Class::Universal { if class != ASN1Class::Universal {
if constructed { if constructed {
// Try to read as explicitly tagged // Try to read as explicitly tagged
match from_der_(body, start_offset + index) { if let Ok(mut items) = from_der_(body, start_offset + index) {
Ok(mut items) => {
if items.len() == 1 { if items.len() == 1 {
result.push(ASN1Block::Explicit( result.push(ASN1Block::Explicit(
class, class,
@@ -397,8 +372,6 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
continue; continue;
} }
} }
Err(_) => {}
}
} }
result.push(ASN1Block::Unknown( result.push(ASN1Block::Unknown(
class, class,
@@ -422,11 +395,11 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
} }
// INTEGER // INTEGER
Some(0x02) => { Some(0x02) => {
let res = BigInt::from_signed_bytes_be(&body); let res = BigInt::from_signed_bytes_be(body);
result.push(ASN1Block::Integer(soff, res)); result.push(ASN1Block::Integer(soff, res));
} }
// BIT STRING // BIT STRING
Some(0x03) if body.len() == 0 => result.push(ASN1Block::BitString(soff, 0, Vec::new())), Some(0x03) if body.is_empty() => 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;
@@ -449,7 +422,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
// OBJECT IDENTIFIER // OBJECT IDENTIFIER
Some(0x06) => { Some(0x06) => {
let mut value1 = BigUint::zero(); let mut value1 = BigUint::zero();
if body.len() == 0 { if body.is_empty() {
return Err(ASN1DecodeErr::Incomplete); return Err(ASN1DecodeErr::Incomplete);
} }
let mut value2 = BigUint::from_u8(body[0]).unwrap(); let mut value2 = BigUint::from_u8(body[0]).unwrap();
@@ -459,10 +432,10 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
if body[0] >= 40 { if body[0] >= 40 {
if body[0] < 80 { if body[0] < 80 {
value1 = BigUint::one(); value1 = BigUint::one();
value2 = value2 - BigUint::from_u8(40).unwrap(); value2 -= BigUint::from_u8(40).unwrap();
} else { } else {
value1 = BigUint::from_u8(2).unwrap(); value1 = BigUint::from_u8(2).unwrap();
value2 = value2 - BigUint::from_u8(80).unwrap(); value2 -= BigUint::from_u8(80).unwrap();
} }
} }
@@ -522,7 +495,34 @@ 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") {
let y = match v.get(0..2) {
Some(yy) => yy,
None => {
// This wasn't a valid character boundrary.
return Err(ASN1DecodeErr::InvalidDateValue(v));
}
};
let y_prefix = match y.parse::<u8>() {
Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)),
Ok(y) => {
if y >= 50 {
"19"
} else {
"20"
}
}
};
let v = format!("{}{}", y_prefix, v);
let format = time::format_description::parse(
"[year][month][day][hour repr:24][minute][second]Z",
)
.unwrap();
match PrimitiveDateTime::parse(&v, &format) {
Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)), Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)),
Ok(t) => result.push(ASN1Block::UTCTime(soff, t)), Ok(t) => result.push(ASN1Block::UTCTime(soff, t)),
} }
@@ -549,7 +549,13 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
let idx = v.len() - 1; let idx = v.len() - 1;
v.insert(idx, '0'); v.insert(idx, '0');
} }
match Utc.datetime_from_str(&v, "%Y%m%d%H%M%S.%fZ") {
let format = time::format_description::parse(
"[year][month][day][hour repr:24][minute][second].[subsecond]Z",
)
.unwrap();
match PrimitiveDateTime::parse(&v, &format) {
Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)), Err(_) => return Err(ASN1DecodeErr::InvalidDateValue(v)),
Ok(t) => result.push(ASN1Block::GeneralizedTime(soff, t)), Ok(t) => result.push(ASN1Block::GeneralizedTime(soff, t)),
} }
@@ -672,9 +678,9 @@ fn decode_length(i: &[u8], index: &mut usize) -> Result<usize, ASN1DecodeErr> {
/// Given an `ASN1Block`, covert it to its DER encoding, or return an error /// Given an `ASN1Block`, covert it to its DER encoding, or return an error
/// if something broke along the way. /// if something broke along the way.
pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> { pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
match i { match *i {
// BOOLEAN // BOOLEAN
&ASN1Block::Boolean(_, val) => { ASN1Block::Boolean(_, val) => {
let inttag = BigUint::one(); let inttag = BigUint::one();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag); let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
tagbytes.push(1); tagbytes.push(1);
@@ -682,7 +688,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(tagbytes) Ok(tagbytes)
} }
// INTEGER // INTEGER
&ASN1Block::Integer(_, ref int) => { ASN1Block::Integer(_, ref int) => {
let mut base = int.to_signed_bytes_be(); let mut base = int.to_signed_bytes_be();
let mut lenbytes = encode_len(base.len()); let mut lenbytes = encode_len(base.len());
let inttag = BigUint::from_u8(0x02).unwrap(); let inttag = BigUint::from_u8(0x02).unwrap();
@@ -695,7 +701,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(result) Ok(result)
} }
// BIT STRING // BIT STRING
&ASN1Block::BitString(_, bits, ref vs) => { ASN1Block::BitString(_, bits, ref vs) => {
let inttag = BigUint::from_u8(0x03).unwrap(); let inttag = BigUint::from_u8(0x03).unwrap();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag); let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
@@ -715,7 +721,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
} }
} }
// OCTET STRING // OCTET STRING
&ASN1Block::OctetString(_, ref bytes) => { ASN1Block::OctetString(_, ref bytes) => {
let inttag = BigUint::from_u8(0x04).unwrap(); let inttag = BigUint::from_u8(0x04).unwrap();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag); let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
let mut lenbytes = encode_len(bytes.len()); let mut lenbytes = encode_len(bytes.len());
@@ -727,14 +733,14 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(result) Ok(result)
} }
// NULL // NULL
&ASN1Block::Null(_) => { ASN1Block::Null(_) => {
let inttag = BigUint::from_u8(0x05).unwrap(); let inttag = BigUint::from_u8(0x05).unwrap();
let mut result = encode_tag(ASN1Class::Universal, false, &inttag); let mut result = encode_tag(ASN1Class::Universal, false, &inttag);
result.push(0); result.push(0);
Ok(result) Ok(result)
} }
// OBJECT IDENTIFIER // OBJECT IDENTIFIER
&ASN1Block::ObjectIdentifier(_, OID(ref nums)) => { ASN1Block::ObjectIdentifier(_, OID(ref nums)) => {
match (nums.get(0), nums.get(1)) { match (nums.get(0), nums.get(1)) {
(Some(v1), Some(v2)) => { (Some(v1), Some(v2)) => {
let two = BigUint::from_u8(2).unwrap(); let two = BigUint::from_u8(2).unwrap();
@@ -761,7 +767,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
// now we can build all the rest of the body // now we can build all the rest of the body
let mut body = vec![byte1]; let mut body = vec![byte1];
for num in nums.iter().skip(2) { for num in nums.iter().skip(2) {
let mut local = encode_base127(&num); let mut local = encode_base127(num);
body.append(&mut local); body.append(&mut local);
} }
@@ -779,7 +785,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
} }
} }
// SEQUENCE // SEQUENCE
&ASN1Block::Sequence(_, ref items) => { ASN1Block::Sequence(_, ref items) => {
let mut body = Vec::new(); let mut body = Vec::new();
// put all the subsequences into a block // put all the subsequences into a block
@@ -801,7 +807,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(res) Ok(res)
} }
// SET // SET
&ASN1Block::Set(_, ref items) => { ASN1Block::Set(_, ref items) => {
let mut body = Vec::new(); let mut body = Vec::new();
// put all the subsequences into a block // put all the subsequences into a block
@@ -822,8 +828,13 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
res.append(&mut body); res.append(&mut body);
Ok(res) Ok(res)
} }
&ASN1Block::UTCTime(_, ref time) => { ASN1Block::UTCTime(_, ref time) => {
let mut body = time.format("%y%m%d%H%M%SZ").to_string().into_bytes(); let format = time::format_description::parse(
"[year][month][day][hour repr:24][minute][second]Z",
)
.unwrap();
let mut body = time.format(&format).unwrap().into_bytes();
body.drain(0..2);
let inttag = BigUint::from_u8(0x17).unwrap(); let inttag = BigUint::from_u8(0x17).unwrap();
let mut lenbytes = encode_len(body.len()); let mut lenbytes = encode_len(body.len());
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag); let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
@@ -834,8 +845,12 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
res.append(&mut body); res.append(&mut body);
Ok(res) Ok(res)
} }
&ASN1Block::GeneralizedTime(_, ref time) => { ASN1Block::GeneralizedTime(_, ref time) => {
let base = time.format("%Y%m%d%H%M%S.%f").to_string(); let format = time::format_description::parse(
"[year][month][day][hour repr:24][minute][second].[subsecond]",
)
.unwrap();
let base = time.format(&format).unwrap();
let zclear = base.trim_end_matches('0'); let zclear = base.trim_end_matches('0');
let dclear = zclear.trim_end_matches('.'); let dclear = zclear.trim_end_matches('.');
let mut body = format!("{}Z", dclear).into_bytes(); let mut body = format!("{}Z", dclear).into_bytes();
@@ -850,25 +865,25 @@ 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) => { ASN1Block::PrintableString(_, ref str) => {
encode_asn1_string(0x13, true, ASN1Class::Universal, str) encode_asn1_string(0x13, true, ASN1Class::Universal, str)
} }
&ASN1Block::TeletexString(_, ref str) => { ASN1Block::TeletexString(_, ref str) => {
encode_asn1_string(0x14, false, ASN1Class::Universal, str) encode_asn1_string(0x14, false, ASN1Class::Universal, str)
} }
&ASN1Block::UniversalString(_, ref str) => { ASN1Block::UniversalString(_, ref str) => {
encode_asn1_string(0x1c, false, ASN1Class::Universal, str) encode_asn1_string(0x1c, false, ASN1Class::Universal, str)
} }
&ASN1Block::IA5String(_, ref str) => { ASN1Block::IA5String(_, ref str) => {
encode_asn1_string(0x16, true, ASN1Class::Universal, str) encode_asn1_string(0x16, true, ASN1Class::Universal, str)
} }
&ASN1Block::BMPString(_, ref str) => { ASN1Block::BMPString(_, ref str) => {
encode_asn1_string(0x1e, false, ASN1Class::Universal, 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)?;
let mut lenbytes = encode_len(bytes.len()); let mut lenbytes = encode_len(bytes.len());
@@ -880,7 +895,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(res) Ok(res)
} }
// Unknown blocks // Unknown blocks
&ASN1Block::Unknown(class, c, _, ref tag, ref bytes) => { ASN1Block::Unknown(class, c, _, ref tag, ref bytes) => {
let mut tagbytes = encode_tag(class, c, tag); let mut tagbytes = encode_tag(class, c, tag);
let mut lenbytes = encode_len(bytes.len()); let mut lenbytes = encode_len(bytes.len());
@@ -897,7 +912,7 @@ fn encode_asn1_string(
tag: u8, tag: u8,
force_chars: bool, force_chars: bool,
c: ASN1Class, c: ASN1Class,
s: &String, s: &str,
) -> Result<Vec<u8>, ASN1EncodeErr> { ) -> Result<Vec<u8>, ASN1EncodeErr> {
let mut body = { let mut body = {
if force_chars { if force_chars {
@@ -908,7 +923,7 @@ fn encode_asn1_string(
} }
out out
} else { } else {
s.clone().into_bytes() s.to_string().into_bytes()
} }
}; };
let inttag = BigUint::from_u8(tag).unwrap(); let inttag = BigUint::from_u8(tag).unwrap();
@@ -958,7 +973,7 @@ fn encode_base127(v: &BigUint) -> Vec<u8> {
while acc > zero { while acc > zero {
// we build this vector backwards // we build this vector backwards
let digit = &acc % &u128; let digit = &acc % &u128;
acc = acc >> 7; acc >>= 7;
match digit.to_u8() { match digit.to_u8() {
None => panic!("7 bits don't fit into 8, cause ..."), None => panic!("7 bits don't fit into 8, cause ..."),
@@ -990,7 +1005,7 @@ fn encode_len(x: usize) -> Vec<u8> {
// convert this into bytes, backwards // convert this into bytes, backwards
while work > 0 { while work > 0 {
bstr.push(work as u8); bstr.push(work as u8);
work = work >> 8; work >>= 8;
} }
// encode the front of the length // encode the front of the length
@@ -1041,7 +1056,7 @@ impl<T: FromASN1> FromASN1WithBody for T {
/// is a member of `FromASN1` or `FromASN1WithBody`. /// is a member of `FromASN1` or `FromASN1WithBody`.
pub fn der_decode<T: FromASN1WithBody>(v: &[u8]) -> Result<T, T::Error> { pub fn der_decode<T: FromASN1WithBody>(v: &[u8]) -> Result<T, T::Error> {
let vs = from_der(v)?; let vs = from_der(v)?;
T::from_asn1_with_body(&vs, v).and_then(|(a, _)| Ok(a)) T::from_asn1_with_body(&vs, v).map(|(a, _)| a)
} }
/// The set of types that can automatically converted into a sequence /// The set of types that can automatically converted into a sequence
@@ -1061,7 +1076,7 @@ pub trait ToASN1 {
/// Automatically encode a type into binary via DER encoding, assuming /// Automatically encode a type into binary via DER encoding, assuming
/// that the type is a member of `ToASN1`. /// that the type is a member of `ToASN1`.
pub fn der_encode<T: ToASN1>(v: &T) -> Result<Vec<u8>, T::Error> { pub fn der_encode<T: ToASN1>(v: &T) -> Result<Vec<u8>, T::Error> {
let blocks = T::to_asn1(&v)?; let blocks = T::to_asn1(v)?;
let mut res = Vec::new(); let mut res = Vec::new();
for block in blocks { for block in blocks {
@@ -1077,15 +1092,14 @@ pub fn der_encode<T: ToASN1>(v: &T) -> Result<Vec<u8>, T::Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use chrono::offset::LocalResult;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use rand::{distributions::Standard, Rng};
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use time::{Date, Month, Time};
impl Arbitrary for ASN1Class { impl Arbitrary for ASN1Class {
fn arbitrary<G: Gen>(g: &mut G) -> ASN1Class { fn arbitrary(g: &mut Gen) -> ASN1Class {
match g.gen::<u8>() % 4 { match u8::arbitrary(g) % 4 {
0 => ASN1Class::Private, 0 => ASN1Class::Private,
1 => ASN1Class::ContextSpecific, 1 => ASN1Class::ContextSpecific,
2 => ASN1Class::Universal, 2 => ASN1Class::Universal,
@@ -1111,8 +1125,8 @@ mod tests {
} }
impl Arbitrary for RandomUint { impl Arbitrary for RandomUint {
fn arbitrary<G: Gen>(g: &mut G) -> RandomUint { fn arbitrary(g: &mut Gen) -> RandomUint {
let v = BigUint::from_u32(g.gen::<u32>()).unwrap(); let v = BigUint::from_u32(u32::arbitrary(g)).unwrap();
RandomUint { x: v } RandomUint { x: v }
} }
} }
@@ -1141,54 +1155,64 @@ mod tests {
} }
impl Arbitrary for RandomInt { impl Arbitrary for RandomInt {
fn arbitrary<G: Gen>(g: &mut G) -> RandomInt { fn arbitrary(g: &mut Gen) -> RandomInt {
let v = BigInt::from_i64(g.gen::<i64>()).unwrap(); let v = BigInt::from_i64(i64::arbitrary(g)).unwrap();
RandomInt { x: v } RandomInt { x: v }
} }
} }
#[allow(type_alias_bounds)] #[allow(type_alias_bounds)]
type ASN1BlockGen<G: Gen> = fn(&mut G, usize) -> ASN1Block; type ASN1BlockGen = fn(&mut Gen, usize) -> ASN1Block;
fn arb_boolean<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_boolean(g: &mut Gen, _d: usize) -> ASN1Block {
let v = g.gen::<bool>(); let v = bool::arbitrary(g);
ASN1Block::Boolean(0, v) ASN1Block::Boolean(0, v)
} }
fn arb_integer<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_integer(g: &mut Gen, _d: usize) -> ASN1Block {
let d = RandomInt::arbitrary(g); let d = RandomInt::arbitrary(g);
ASN1Block::Integer(0, d.x) ASN1Block::Integer(0, d.x)
} }
fn arb_bitstr<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_bitstr(g: &mut Gen, _d: usize) -> ASN1Block {
let size = g.gen::<u16>() as usize % 16; let size = u16::arbitrary(g) as usize % 16;
let maxbits = (size as usize) * 8; let maxbits = (size as usize) * 8;
let modbits = g.gen::<u8>() as usize % 8; let modbits = u8::arbitrary(g) as usize % 8;
let nbits = if modbits > maxbits { let nbits = if modbits > maxbits {
maxbits maxbits
} else { } else {
maxbits - modbits maxbits - modbits
}; };
let bytes = g.sample_iter::<u8, _>(&Standard).take(size).collect();
let mut bytes = Vec::with_capacity(size);
while bytes.len() < size {
bytes.push(u8::arbitrary(g));
}
ASN1Block::BitString(0, nbits, bytes) ASN1Block::BitString(0, nbits, bytes)
} }
fn arb_octstr<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_octstr(g: &mut Gen, _d: usize) -> ASN1Block {
let size = g.gen::<u16>() as usize % 16; let size = usize::arbitrary(g) % 16;
let bytes = g.sample_iter::<u8, _>(&Standard).take(size).collect(); let mut bytes = Vec::with_capacity(size);
while bytes.len() < size {
bytes.push(u8::arbitrary(g));
}
ASN1Block::OctetString(0, bytes) ASN1Block::OctetString(0, bytes)
} }
fn arb_null<G: Gen>(_g: &mut G, _d: usize) -> ASN1Block { fn arb_null(_g: &mut Gen, _d: usize) -> ASN1Block {
ASN1Block::Null(0) ASN1Block::Null(0)
} }
impl Arbitrary for OID { impl Arbitrary for OID {
fn arbitrary<G: Gen>(g: &mut G) -> OID { fn arbitrary(g: &mut Gen) -> OID {
let count = g.gen_range::<usize>(0, 40); let count = usize::arbitrary(g) % 40;
let val1 = g.gen::<u8>() % 3; let val1 = u8::arbitrary(g) % 3;
let v2mod = if val1 == 2 { 176 } else { 40 }; let v2mod = if val1 == 2 { 176 } else { 40 };
let val2 = g.gen::<u8>() % v2mod; let val2 = u8::arbitrary(g) % v2mod;
let v1 = BigUint::from_u8(val1).unwrap(); let v1 = BigUint::from_u8(val1).unwrap();
let v2 = BigUint::from_u8(val2).unwrap(); let v2 = BigUint::from_u8(val2).unwrap();
let mut nums = vec![v1, v2]; let mut nums = vec![v1, v2];
@@ -1202,13 +1226,13 @@ mod tests {
} }
} }
fn arb_objid<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_objid(g: &mut Gen, _d: usize) -> ASN1Block {
let oid = OID::arbitrary(g); let oid = OID::arbitrary(g);
ASN1Block::ObjectIdentifier(0, oid) ASN1Block::ObjectIdentifier(0, oid)
} }
fn arb_seq<G: Gen>(g: &mut G, d: usize) -> ASN1Block { fn arb_seq(g: &mut Gen, d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(1, 64); let count = usize::arbitrary(g) % 63 + 1;
let mut items = Vec::new(); let mut items = Vec::new();
for _ in 0..count { for _ in 0..count {
@@ -1218,8 +1242,8 @@ mod tests {
ASN1Block::Sequence(0, items) ASN1Block::Sequence(0, items)
} }
fn arb_set<G: Gen>(g: &mut G, d: usize) -> ASN1Block { fn arb_set(g: &mut Gen, d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(1, 64); let count = usize::arbitrary(g) % 63 + 1;
let mut items = Vec::new(); let mut items = Vec::new();
for _ in 0..count { for _ in 0..count {
@@ -1229,89 +1253,85 @@ mod tests {
ASN1Block::Set(0, items) ASN1Block::Set(0, items)
} }
fn arb_print<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_print(g: &mut Gen, _d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(0, 384); let count = usize::arbitrary(g) % 384;
let mut items = Vec::new(); let mut items = Vec::new();
for _ in 0..count { for _ in 0..count {
let v = g.choose(PRINTABLE_CHARS.as_bytes()).unwrap(); let v = g.choose(PRINTABLE_CHARS.as_bytes());
items.push(*v as char); items.push(*v.unwrap() as char);
} }
ASN1Block::PrintableString(0, String::from_iter(items.iter())) ASN1Block::PrintableString(0, String::from_iter(items.iter()))
} }
fn arb_ia5<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_ia5(g: &mut Gen, _d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(0, 384); let count = usize::arbitrary(g) % 384;
let mut items = Vec::new(); let mut items = Vec::new();
for _ in 0..count { for _ in 0..count {
items.push(g.gen::<u8>() as char); items.push(u8::arbitrary(g) as char);
} }
ASN1Block::IA5String(0, String::from_iter(items.iter())) ASN1Block::IA5String(0, String::from_iter(items.iter()))
} }
fn arb_utf8<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_utf8(g: &mut Gen, _d: usize) -> ASN1Block {
let val = String::arbitrary(g); let val = String::arbitrary(g);
ASN1Block::UTF8String(0, val) ASN1Block::UTF8String(0, val)
} }
fn arb_tele<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_tele(g: &mut Gen, _d: usize) -> ASN1Block {
let val = String::arbitrary(g); let val = String::arbitrary(g);
ASN1Block::TeletexString(0, val) ASN1Block::TeletexString(0, val)
} }
fn arb_uni<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_uni(g: &mut Gen, _d: usize) -> ASN1Block {
let val = String::arbitrary(g); let val = String::arbitrary(g);
ASN1Block::UniversalString(0, val) ASN1Block::UniversalString(0, val)
} }
fn arb_bmp<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_bmp(g: &mut Gen, _d: usize) -> ASN1Block {
let val = String::arbitrary(g); let val = String::arbitrary(g);
ASN1Block::BMPString(0, val) ASN1Block::BMPString(0, val)
} }
fn arb_utc<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_utc(g: &mut Gen, _d: usize) -> ASN1Block {
loop { let min = Date::from_calendar_date(1950, Month::January, 01)
let y = g.gen_range::<i32>(1970, 2069); .unwrap()
let m = g.gen_range::<u32>(1, 13); .to_julian_day();
let d = g.gen_range::<u32>(1, 32); let max = Date::from_calendar_date(2049, Month::December, 31)
match Utc.ymd_opt(y, m, d) { .unwrap()
LocalResult::None => {} .to_julian_day();
LocalResult::Single(d) => { let date =
let h = g.gen_range::<u32>(0, 24); Date::from_julian_day(i32::arbitrary(g).rem_euclid(max - min + 1) + min).unwrap();
let m = g.gen_range::<u32>(0, 60);
let s = g.gen_range::<u32>(0, 60); let h = u8::arbitrary(g).rem_euclid(24);
let t = d.and_hms(h, m, s); let m = u8::arbitrary(g).rem_euclid(60);
return ASN1Block::UTCTime(0, t); let s = u8::arbitrary(g).rem_euclid(60);
} let time = Time::from_hms(h, m, s).unwrap();
LocalResult::Ambiguous(_, _) => {}
} let t = PrimitiveDateTime::new(date, time);
} ASN1Block::UTCTime(0, t)
} }
fn arb_time<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_time(g: &mut Gen, _d: usize) -> ASN1Block {
loop { let min = Date::from_calendar_date(0, Month::January, 01)
let y = g.gen_range::<i32>(0, 10000); .unwrap()
let m = g.gen_range::<u32>(1, 13); .to_julian_day();
let d = g.gen_range::<u32>(1, 32); let max = Date::from_calendar_date(9999, Month::December, 31)
match Utc.ymd_opt(y, m, d) { .unwrap()
LocalResult::None => {} .to_julian_day();
LocalResult::Single(d) => { let date =
let h = g.gen_range::<u32>(0, 24); Date::from_julian_day(i32::arbitrary(g).rem_euclid(max - min + 1) + min).unwrap();
let m = g.gen_range::<u32>(0, 60);
let s = g.gen_range::<u32>(0, 60); let time = Time::arbitrary(g);
let n = g.gen_range::<u32>(0, 1000000000);
let t = d.and_hms_nano(h, m, s, n); let t = PrimitiveDateTime::new(date, time);
return ASN1Block::GeneralizedTime(0, t); ASN1Block::GeneralizedTime(0, t)
}
LocalResult::Ambiguous(_, _) => {}
}
}
} }
fn arb_explicit<G: Gen>(g: &mut G, d: usize) -> ASN1Block { fn arb_explicit(g: &mut Gen, d: usize) -> ASN1Block {
let mut class = ASN1Class::arbitrary(g); let mut class = ASN1Class::arbitrary(g);
if class == ASN1Class::Universal { if class == ASN1Class::Universal {
// Universal is invalid for an explicitly tagged block // Universal is invalid for an explicitly tagged block
@@ -1323,17 +1343,28 @@ mod tests {
ASN1Block::Explicit(class, 0, tag.x, Box::new(item)) ASN1Block::Explicit(class, 0, tag.x, Box::new(item))
} }
fn arb_unknown<G: Gen>(g: &mut G, _d: usize) -> ASN1Block { fn arb_unknown(g: &mut Gen, _d: usize) -> ASN1Block {
let class = ASN1Class::arbitrary(g); let class = ASN1Class::arbitrary(g);
let tag = RandomUint::arbitrary(g); let tag = loop {
let size = g.gen_range::<usize>(0, 128); let potential = RandomUint::arbitrary(g);
let items = g.sample_iter::<u8, _>(&Standard).take(size).collect(); match potential.x.to_u8() {
None => break potential,
Some(x) if KNOWN_TAGS.contains(&x) => {}
Some(_) => break potential,
}
};
let size = usize::arbitrary(g) % 128;
let mut items = Vec::with_capacity(size);
while items.len() < size {
items.push(u8::arbitrary(g));
}
ASN1Block::Unknown(class, false, 0, tag.x, items) ASN1Block::Unknown(class, false, 0, tag.x, items)
} }
fn limited_arbitrary<G: Gen>(g: &mut G, d: usize) -> ASN1Block { fn limited_arbitrary(g: &mut Gen, d: usize) -> ASN1Block {
let mut possibles: Vec<ASN1BlockGen<G>> = vec![ let mut possibles: Vec<ASN1BlockGen> = vec![
arb_boolean, arb_boolean,
arb_integer, arb_integer,
arb_bitstr, arb_bitstr,
@@ -1364,7 +1395,7 @@ mod tests {
} }
impl Arbitrary for ASN1Block { impl Arbitrary for ASN1Block {
fn arbitrary<G: Gen>(g: &mut G) -> ASN1Block { fn arbitrary(g: &mut Gen) -> ASN1Block {
limited_arbitrary(g, 2) limited_arbitrary(g, 2)
} }
} }
@@ -1404,23 +1435,43 @@ mod tests {
Ok(vec![ASN1Block::Integer(0, val)]) Ok(vec![ASN1Block::Integer(0, val)])
} }
#[test]
fn utc_time_tests() {
// Check for a regression against issue #27, in which this would
// cause a panic.
let input = [
55, 13, 13, 133, 13, 13, 50, 13, 13, 133, 13, 13, 50, 13, 133,
];
let output = from_der(&input);
assert!(output.is_err());
}
#[test] #[test]
fn generalized_time_tests() { fn generalized_time_tests() {
check_spec( check_spec(
&Utc.ymd(1992, 5, 21).and_hms(0, 0, 0), &PrimitiveDateTime::new(
Date::from_calendar_date(1992, Month::May, 21).unwrap(),
Time::from_hms(0, 0, 0).unwrap(),
),
"19920521000000Z".to_string(), "19920521000000Z".to_string(),
); );
check_spec( check_spec(
&Utc.ymd(1992, 6, 22).and_hms(12, 34, 21), &PrimitiveDateTime::new(
Date::from_calendar_date(1992, Month::June, 22).unwrap(),
Time::from_hms(12, 34, 21).unwrap(),
),
"19920622123421Z".to_string(), "19920622123421Z".to_string(),
); );
check_spec( check_spec(
&Utc.ymd(1992, 7, 22).and_hms_milli(13, 21, 00, 300), &PrimitiveDateTime::new(
Date::from_calendar_date(1992, Month::July, 22).unwrap(),
Time::from_hms_milli(13, 21, 00, 300).unwrap(),
),
"19920722132100.3Z".to_string(), "19920722132100.3Z".to_string(),
); );
} }
fn check_spec(d: &DateTime<Utc>, s: String) { fn check_spec(d: &PrimitiveDateTime, s: String) {
let b = ASN1Block::GeneralizedTime(0, d.clone()); let b = ASN1Block::GeneralizedTime(0, d.clone());
match to_der(&b) { match to_der(&b) {
Err(_) => assert_eq!(format!("Broken: {}", d), s), Err(_) => assert_eq!(format!("Broken: {}", d), s),
@@ -1546,4 +1597,23 @@ mod tests {
assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]); assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]);
} }
} }
#[test]
fn vec_oid() {
let vec_u64: Vec<u64> = vec![1, 2, 840, 10045, 4, 3, 2];
let vec_i64: Vec<i64> = vec![1, 2, 840, 10045, 4, 3, 2];
let vec_usize: Vec<usize> = vec![1, 2, 840, 10045, 4, 3, 2];
let mut o = Vec::new();
for val in vec_u64.iter() {
o.push(BigUint::from(*val));
}
let oid = OID::new(o);
assert_eq!(Ok(vec_u64), oid.as_vec());
assert_eq!(Ok(vec_i64), oid.as_vec());
assert_eq!(Ok(vec_usize), oid.as_vec());
assert_eq!(Err(ASN1DecodeErr::Overflow), oid.as_vec::<u8>());
}
} }

View File

@@ -1,201 +0,0 @@
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!()
}
}

View File

@@ -1,23 +0,0 @@
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,
}

View File

@@ -1,33 +0,0 @@
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)
}
}
}
}