43 Commits

Author SHA1 Message Date
dc596d7601 Merge pull request #37 from acw/warning-clean
Clean some warnings and add some basic CI.
2025-01-12 13:47:19 -08:00
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
a453128181 Merge pull request #36 from muturgan/master
bump a thiserror version
2025-01-12 13:39:20 -08:00
Сахаров Андрей Александрович
d7b31d67d3 bump a thiserror version 2025-01-11 16:53:28 +03: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
3 changed files with 362 additions and 250 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]
name = "simple_asn1"
version = "0.5.0"
version = "0.6.3"
authors = ["Adam Wick <awick@uhsure.com>"]
description = "A simple DER/ASN.1 encoding/decoding library."
categories = ["encoding"]
keywords = ["ASN1","encoding","DER"]
license-file = "LICENSE"
license = "ISC"
repository = "https://github.com/acw/simple_asn1"
edition = "2018"
[dependencies]
chrono = "^0.4.0"
num-bigint = "^0.2.0"
num-traits = "^0.2.0"
num-bigint = { default-features = false, version = "0.4" }
num-traits = { default-features = false, version = "0.2" }
thiserror = { default-features = false, version = "2" }
time = { default-features = false, version = "0.3", features = ["formatting", "macros", "parsing"] }
[dev-dependencies]
quickcheck = "^0.7.1"
rand = "0.5.5"
quickcheck = "1.0.3"
rand = "0.8.4"
time = { default-features = false, version = "0.3", features = ["formatting", "macros", "parsing", "quickcheck"] }

View File

@@ -26,15 +26,16 @@
//!
//! Please send any bug reports, patches, and curses to the GitHub repository
//! at <code>https://github.com/acw/simple_asn1</code>.
use chrono::{DateTime, TimeZone, Utc};
pub use num_bigint::{BigInt, BigUint};
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
#[cfg(test)]
use quickcheck::quickcheck;
use std::fmt;
use std::convert::TryFrom;
use std::iter::FromIterator;
use std::mem::size_of;
use std::str::Utf8Error;
use thiserror::Error;
use time::PrimitiveDateTime;
/// An ASN.1 block class.
///
@@ -77,8 +78,8 @@ pub enum ASN1Block {
PrintableString(usize, String),
TeletexString(usize, String),
IA5String(usize, String),
UTCTime(usize, DateTime<Utc>),
GeneralizedTime(usize, DateTime<Utc>),
UTCTime(usize, PrimitiveDateTime),
GeneralizedTime(usize, PrimitiveDateTime),
UniversalString(usize, String),
BMPString(usize, String),
Sequence(usize, Vec<ASN1Block>),
@@ -101,50 +102,49 @@ impl ASN1Block {
/// Get the class associated with the given ASN1Block, regardless of what
/// kind of block it is.
pub fn class(&self) -> ASN1Class {
match self {
&ASN1Block::Boolean(_, _) => ASN1Class::Universal,
&ASN1Block::Integer(_, _) => ASN1Class::Universal,
&ASN1Block::BitString(_, _, _) => ASN1Class::Universal,
&ASN1Block::OctetString(_, _) => ASN1Class::Universal,
&ASN1Block::Null(_) => ASN1Class::Universal,
&ASN1Block::ObjectIdentifier(_, _) => ASN1Class::Universal,
&ASN1Block::UTF8String(_, _) => ASN1Class::Universal,
&ASN1Block::PrintableString(_, _) => ASN1Class::Universal,
&ASN1Block::TeletexString(_, _) => ASN1Class::Universal,
&ASN1Block::IA5String(_, _) => ASN1Class::Universal,
&ASN1Block::UTCTime(_, _) => ASN1Class::Universal,
&ASN1Block::GeneralizedTime(_, _) => ASN1Class::Universal,
&ASN1Block::UniversalString(_, _) => ASN1Class::Universal,
&ASN1Block::BMPString(_, _) => ASN1Class::Universal,
&ASN1Block::Sequence(_, _) => ASN1Class::Universal,
&ASN1Block::Set(_, _) => ASN1Class::Universal,
&ASN1Block::Explicit(c, _, _, _) => c,
&ASN1Block::Unknown(c, _, _, _, _) => c,
match *self {
ASN1Block::Boolean(_, _) => ASN1Class::Universal,
ASN1Block::Integer(_, _) => ASN1Class::Universal,
ASN1Block::BitString(_, _, _) => ASN1Class::Universal,
ASN1Block::OctetString(_, _) => ASN1Class::Universal,
ASN1Block::Null(_) => ASN1Class::Universal,
ASN1Block::ObjectIdentifier(_, _) => ASN1Class::Universal,
ASN1Block::UTF8String(_, _) => ASN1Class::Universal,
ASN1Block::PrintableString(_, _) => ASN1Class::Universal,
ASN1Block::TeletexString(_, _) => ASN1Class::Universal,
ASN1Block::IA5String(_, _) => ASN1Class::Universal,
ASN1Block::UTCTime(_, _) => ASN1Class::Universal,
ASN1Block::GeneralizedTime(_, _) => ASN1Class::Universal,
ASN1Block::UniversalString(_, _) => ASN1Class::Universal,
ASN1Block::BMPString(_, _) => ASN1Class::Universal,
ASN1Block::Sequence(_, _) => ASN1Class::Universal,
ASN1Block::Set(_, _) => ASN1Class::Universal,
ASN1Block::Explicit(c, _, _, _) => c,
ASN1Block::Unknown(c, _, _, _, _) => c,
}
}
/// Get the starting offset associated with the given ASN1Block, regardless
/// of what kind of block it is.
pub fn offset(&self) -> usize {
match self {
&ASN1Block::Boolean(o, _) => o,
&ASN1Block::Integer(o, _) => o,
&ASN1Block::BitString(o, _, _) => o,
&ASN1Block::OctetString(o, _) => o,
&ASN1Block::Null(o) => o,
&ASN1Block::ObjectIdentifier(o, _) => o,
&ASN1Block::UTF8String(o, _) => o,
&ASN1Block::PrintableString(o, _) => o,
&ASN1Block::TeletexString(o, _) => o,
&ASN1Block::IA5String(o, _) => o,
&ASN1Block::UTCTime(o, _) => o,
&ASN1Block::GeneralizedTime(o, _) => o,
&ASN1Block::UniversalString(o, _) => o,
&ASN1Block::BMPString(o, _) => o,
&ASN1Block::Sequence(o, _) => o,
&ASN1Block::Set(o, _) => o,
&ASN1Block::Explicit(_, o, _, _) => o,
&ASN1Block::Unknown(_, _, o, _, _) => o,
match *self {
ASN1Block::Boolean(o, _) => o,
ASN1Block::Integer(o, _) => o,
ASN1Block::BitString(o, _, _) => o,
ASN1Block::OctetString(o, _) => o,
ASN1Block::Null(o) => o,
ASN1Block::ObjectIdentifier(o, _) => o,
ASN1Block::UTF8String(o, _) => o,
ASN1Block::PrintableString(o, _) => o,
ASN1Block::TeletexString(o, _) => o,
ASN1Block::IA5String(o, _) => o,
ASN1Block::UTCTime(o, _) => o,
ASN1Block::GeneralizedTime(o, _) => o,
ASN1Block::UniversalString(o, _) => o,
ASN1Block::BMPString(o, _) => o,
ASN1Block::Sequence(o, _) => o,
ASN1Block::Set(o, _) => o,
ASN1Block::Explicit(_, o, _, _) => o,
ASN1Block::Unknown(_, _, o, _, _) => o,
}
}
}
@@ -152,34 +152,34 @@ impl ASN1Block {
impl PartialEq for ASN1Block {
fn eq(&self, other: &ASN1Block) -> bool {
match (self, other) {
(&ASN1Block::Boolean(_, a1), &ASN1Block::Boolean(_, a2)) => (a1 == a2),
(&ASN1Block::Integer(_, ref a1), &ASN1Block::Integer(_, ref a2)) => (a1 == a2),
(&ASN1Block::Boolean(_, a1), &ASN1Block::Boolean(_, a2)) => a1 == a2,
(&ASN1Block::Integer(_, ref a1), &ASN1Block::Integer(_, ref a2)) => a1 == a2,
(&ASN1Block::BitString(_, a1, ref b1), &ASN1Block::BitString(_, a2, ref 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::ObjectIdentifier(_, ref a1), &ASN1Block::ObjectIdentifier(_, ref 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)) => {
a1 == a2
}
(&ASN1Block::TeletexString(_, ref a1), &ASN1Block::TeletexString(_, 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::IA5String(_, ref a1), &ASN1Block::IA5String(_, ref a2)) => a1 == a2,
(&ASN1Block::UTCTime(_, ref a1), &ASN1Block::UTCTime(_, ref a2)) => a1 == a2,
(&ASN1Block::GeneralizedTime(_, ref a1), &ASN1Block::GeneralizedTime(_, ref a2)) => {
a1 == a2
}
(&ASN1Block::UniversalString(_, ref a1), &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::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),
@@ -194,7 +194,7 @@ impl PartialEq for ASN1Block {
}
/// An ASN.1 OID.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OID(Vec<BigUint>);
impl OID {
@@ -232,7 +232,7 @@ impl OID {
// now we can build all the rest of the body
let mut body = vec![byte1];
for num in self.0.iter().skip(2) {
let mut local = encode_base127(&num);
let mut local = encode_base127(num);
body.append(&mut local);
}
@@ -241,6 +241,19 @@ impl OID {
_ => 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 {
@@ -269,88 +282,58 @@ impl<'a> PartialEq<OID> for &'a OID {
#[macro_export]
macro_rules! oid {
( $( $e: expr ),* ) => {{
let mut res = Vec::new();
$(
res.push(BigUint::from($e as u64));
)*
OID::new(res)
$crate::OID::new(vec![$($crate::BigUint::from($e as u64)),*])
}};
}
const PRINTABLE_CHARS: &'static str =
"ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+,-./:=? ";
const PRINTABLE_CHARS: &str =
"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.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum ASN1DecodeErr {
#[error("Encountered an empty buffer decoding ASN1 block.")]
EmptyBuffer,
#[error("Bad length field in boolean block: {0}")]
BadBooleanLength(usize),
#[error("Length field too large for object type: {0}")]
LengthTooLarge(usize),
#[error("UTF8 string failed to properly decode: {0}")]
UTF8DecodeFailure(Utf8Error),
#[error("Printable string failed to properly decode.")]
PrintableStringDecodeFailure,
#[error("Invalid date value: {0}")]
InvalidDateValue(String),
#[error("Invalid length of bit string: {0}")]
InvalidBitStringLength(isize),
/// Not a valid ASN.1 class
#[error("Invalid class value: {0}")]
InvalidClass(u8),
/// Expected more input
///
/// Invalid ASN.1 input can lead to this error.
#[error("Incomplete data or invalid ASN1")]
Incomplete,
}
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"),
}
}
#[error("Value overflow")]
Overflow,
}
/// An error that can arise encoding ASN.1 primitive blocks.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum ASN1EncodeErr {
#[error("ASN1 object identifier has too few fields.")]
ObjectIdentHasTooFewFields,
#[error("First value in ASN1 OID is too big.")]
ObjectIdentVal1TooLarge,
#[error("Second value in ASN1 OID is too big.")]
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
/// error if it didn't work.
pub fn from_der(i: &[u8]) -> Result<Vec<ASN1Block>, ASN1DecodeErr> {
@@ -377,8 +360,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
if class != ASN1Class::Universal {
if constructed {
// Try to read as explicitly tagged
match from_der_(body, start_offset + index) {
Ok(mut items) => {
if let Ok(mut items) = from_der_(body, start_offset + index) {
if items.len() == 1 {
result.push(ASN1Block::Explicit(
class,
@@ -390,8 +372,6 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
continue;
}
}
Err(_) => {}
}
}
result.push(ASN1Block::Unknown(
class,
@@ -415,11 +395,11 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
}
// INTEGER
Some(0x02) => {
let res = BigInt::from_signed_bytes_be(&body);
let res = BigInt::from_signed_bytes_be(body);
result.push(ASN1Block::Integer(soff, res));
}
// 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) => {
let bits = (&body[1..]).to_vec();
let bitcount = bits.len() * 8;
@@ -442,7 +422,7 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
// OBJECT IDENTIFIER
Some(0x06) => {
let mut value1 = BigUint::zero();
if body.len() == 0 {
if body.is_empty() {
return Err(ASN1DecodeErr::Incomplete);
}
let mut value2 = BigUint::from_u8(body[0]).unwrap();
@@ -452,10 +432,10 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
if body[0] >= 40 {
if body[0] < 80 {
value1 = BigUint::one();
value2 = value2 - BigUint::from_u8(40).unwrap();
value2 -= BigUint::from_u8(40).unwrap();
} else {
value1 = BigUint::from_u8(2).unwrap();
value2 = value2 - BigUint::from_u8(80).unwrap();
value2 -= BigUint::from_u8(80).unwrap();
}
}
@@ -515,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));
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)),
Ok(t) => result.push(ASN1Block::UTCTime(soff, t)),
}
@@ -542,7 +549,13 @@ fn from_der_(i: &[u8], start_offset: usize) -> Result<Vec<ASN1Block>, ASN1Decode
let idx = v.len() - 1;
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)),
Ok(t) => result.push(ASN1Block::GeneralizedTime(soff, t)),
}
@@ -665,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
/// if something broke along the way.
pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
match i {
match *i {
// BOOLEAN
&ASN1Block::Boolean(_, val) => {
ASN1Block::Boolean(_, val) => {
let inttag = BigUint::one();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
tagbytes.push(1);
@@ -675,7 +688,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(tagbytes)
}
// INTEGER
&ASN1Block::Integer(_, ref int) => {
ASN1Block::Integer(_, ref int) => {
let mut base = int.to_signed_bytes_be();
let mut lenbytes = encode_len(base.len());
let inttag = BigUint::from_u8(0x02).unwrap();
@@ -688,7 +701,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(result)
}
// BIT STRING
&ASN1Block::BitString(_, bits, ref vs) => {
ASN1Block::BitString(_, bits, ref vs) => {
let inttag = BigUint::from_u8(0x03).unwrap();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
@@ -708,7 +721,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
}
}
// OCTET STRING
&ASN1Block::OctetString(_, ref bytes) => {
ASN1Block::OctetString(_, ref bytes) => {
let inttag = BigUint::from_u8(0x04).unwrap();
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
let mut lenbytes = encode_len(bytes.len());
@@ -720,14 +733,14 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(result)
}
// NULL
&ASN1Block::Null(_) => {
ASN1Block::Null(_) => {
let inttag = BigUint::from_u8(0x05).unwrap();
let mut result = encode_tag(ASN1Class::Universal, false, &inttag);
result.push(0);
Ok(result)
}
// OBJECT IDENTIFIER
&ASN1Block::ObjectIdentifier(_, OID(ref nums)) => {
ASN1Block::ObjectIdentifier(_, OID(ref nums)) => {
match (nums.get(0), nums.get(1)) {
(Some(v1), Some(v2)) => {
let two = BigUint::from_u8(2).unwrap();
@@ -754,7 +767,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
// now we can build all the rest of the body
let mut body = vec![byte1];
for num in nums.iter().skip(2) {
let mut local = encode_base127(&num);
let mut local = encode_base127(num);
body.append(&mut local);
}
@@ -772,7 +785,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
}
}
// SEQUENCE
&ASN1Block::Sequence(_, ref items) => {
ASN1Block::Sequence(_, ref items) => {
let mut body = Vec::new();
// put all the subsequences into a block
@@ -794,7 +807,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(res)
}
// SET
&ASN1Block::Set(_, ref items) => {
ASN1Block::Set(_, ref items) => {
let mut body = Vec::new();
// put all the subsequences into a block
@@ -815,8 +828,13 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
res.append(&mut body);
Ok(res)
}
&ASN1Block::UTCTime(_, ref time) => {
let mut body = time.format("%y%m%d%H%M%SZ").to_string().into_bytes();
ASN1Block::UTCTime(_, ref time) => {
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 mut lenbytes = encode_len(body.len());
let mut tagbytes = encode_tag(ASN1Class::Universal, false, &inttag);
@@ -827,8 +845,12 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
res.append(&mut body);
Ok(res)
}
&ASN1Block::GeneralizedTime(_, ref time) => {
let base = time.format("%Y%m%d%H%M%S.%f").to_string();
ASN1Block::GeneralizedTime(_, ref time) => {
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 dclear = zclear.trim_end_matches('.');
let mut body = format!("{}Z", dclear).into_bytes();
@@ -843,25 +865,25 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
res.append(&mut body);
Ok(res)
}
&ASN1Block::UTF8String(_, ref str) => {
ASN1Block::UTF8String(_, ref 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)
}
&ASN1Block::TeletexString(_, ref str) => {
ASN1Block::TeletexString(_, ref 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)
}
&ASN1Block::IA5String(_, ref str) => {
ASN1Block::IA5String(_, ref 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)
}
&ASN1Block::Explicit(class, _, ref tag, ref item) => {
ASN1Block::Explicit(class, _, ref tag, ref item) => {
let mut tagbytes = encode_tag(class, true, tag);
let mut bytes = to_der(item)?;
let mut lenbytes = encode_len(bytes.len());
@@ -873,7 +895,7 @@ pub fn to_der(i: &ASN1Block) -> Result<Vec<u8>, ASN1EncodeErr> {
Ok(res)
}
// 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 lenbytes = encode_len(bytes.len());
@@ -890,7 +912,7 @@ fn encode_asn1_string(
tag: u8,
force_chars: bool,
c: ASN1Class,
s: &String,
s: &str,
) -> Result<Vec<u8>, ASN1EncodeErr> {
let mut body = {
if force_chars {
@@ -901,7 +923,7 @@ fn encode_asn1_string(
}
out
} else {
s.clone().into_bytes()
s.to_string().into_bytes()
}
};
let inttag = BigUint::from_u8(tag).unwrap();
@@ -951,7 +973,7 @@ fn encode_base127(v: &BigUint) -> Vec<u8> {
while acc > zero {
// we build this vector backwards
let digit = &acc % &u128;
acc = acc >> 7;
acc >>= 7;
match digit.to_u8() {
None => panic!("7 bits don't fit into 8, cause ..."),
@@ -983,7 +1005,7 @@ fn encode_len(x: usize) -> Vec<u8> {
// convert this into bytes, backwards
while work > 0 {
bstr.push(work as u8);
work = work >> 8;
work >>= 8;
}
// encode the front of the length
@@ -1034,7 +1056,7 @@ impl<T: FromASN1> FromASN1WithBody for T {
/// is a member of `FromASN1` or `FromASN1WithBody`.
pub fn der_decode<T: FromASN1WithBody>(v: &[u8]) -> Result<T, T::Error> {
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
@@ -1054,7 +1076,7 @@ pub trait ToASN1 {
/// Automatically encode a type into binary via DER encoding, assuming
/// that the type is a member of `ToASN1`.
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();
for block in blocks {
@@ -1070,15 +1092,14 @@ pub fn der_encode<T: ToASN1>(v: &T) -> Result<Vec<u8>, T::Error> {
#[cfg(test)]
mod tests {
use super::*;
use chrono::offset::LocalResult;
use quickcheck::{Arbitrary, Gen};
use rand::{distributions::Standard, Rng};
use std::fs::File;
use std::io::Read;
use time::{Date, Month, Time};
impl Arbitrary for ASN1Class {
fn arbitrary<G: Gen>(g: &mut G) -> ASN1Class {
match g.gen::<u8>() % 4 {
fn arbitrary(g: &mut Gen) -> ASN1Class {
match u8::arbitrary(g) % 4 {
0 => ASN1Class::Private,
1 => ASN1Class::ContextSpecific,
2 => ASN1Class::Universal,
@@ -1104,8 +1125,8 @@ mod tests {
}
impl Arbitrary for RandomUint {
fn arbitrary<G: Gen>(g: &mut G) -> RandomUint {
let v = BigUint::from_u32(g.gen::<u32>()).unwrap();
fn arbitrary(g: &mut Gen) -> RandomUint {
let v = BigUint::from_u32(u32::arbitrary(g)).unwrap();
RandomUint { x: v }
}
}
@@ -1134,54 +1155,64 @@ mod tests {
}
impl Arbitrary for RandomInt {
fn arbitrary<G: Gen>(g: &mut G) -> RandomInt {
let v = BigInt::from_i64(g.gen::<i64>()).unwrap();
fn arbitrary(g: &mut Gen) -> RandomInt {
let v = BigInt::from_i64(i64::arbitrary(g)).unwrap();
RandomInt { x: v }
}
}
#[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 {
let v = g.gen::<bool>();
fn arb_boolean(g: &mut Gen, _d: usize) -> ASN1Block {
let v = bool::arbitrary(g);
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);
ASN1Block::Integer(0, d.x)
}
fn arb_bitstr<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
let size = g.gen::<u16>() as usize % 16;
fn arb_bitstr(g: &mut Gen, _d: usize) -> ASN1Block {
let size = u16::arbitrary(g) as usize % 16;
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 {
maxbits
} else {
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)
}
fn arb_octstr<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
let size = g.gen::<u16>() as usize % 16;
let bytes = g.sample_iter::<u8, _>(&Standard).take(size).collect();
fn arb_octstr(g: &mut Gen, _d: usize) -> ASN1Block {
let size = usize::arbitrary(g) % 16;
let mut bytes = Vec::with_capacity(size);
while bytes.len() < size {
bytes.push(u8::arbitrary(g));
}
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)
}
impl Arbitrary for OID {
fn arbitrary<G: Gen>(g: &mut G) -> OID {
let count = g.gen_range::<usize>(0, 40);
let val1 = g.gen::<u8>() % 3;
fn arbitrary(g: &mut Gen) -> OID {
let count = usize::arbitrary(g) % 40;
let val1 = u8::arbitrary(g) % 3;
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 v2 = BigUint::from_u8(val2).unwrap();
let mut nums = vec![v1, v2];
@@ -1195,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);
ASN1Block::ObjectIdentifier(0, oid)
}
fn arb_seq<G: Gen>(g: &mut G, d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(1, 64);
fn arb_seq(g: &mut Gen, d: usize) -> ASN1Block {
let count = usize::arbitrary(g) % 63 + 1;
let mut items = Vec::new();
for _ in 0..count {
@@ -1211,8 +1242,8 @@ mod tests {
ASN1Block::Sequence(0, items)
}
fn arb_set<G: Gen>(g: &mut G, d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(1, 64);
fn arb_set(g: &mut Gen, d: usize) -> ASN1Block {
let count = usize::arbitrary(g) % 63 + 1;
let mut items = Vec::new();
for _ in 0..count {
@@ -1222,89 +1253,85 @@ mod tests {
ASN1Block::Set(0, items)
}
fn arb_print<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(0, 384);
fn arb_print(g: &mut Gen, _d: usize) -> ASN1Block {
let count = usize::arbitrary(g) % 384;
let mut items = Vec::new();
for _ in 0..count {
let v = g.choose(PRINTABLE_CHARS.as_bytes()).unwrap();
items.push(*v as char);
let v = g.choose(PRINTABLE_CHARS.as_bytes());
items.push(*v.unwrap() as char);
}
ASN1Block::PrintableString(0, String::from_iter(items.iter()))
}
fn arb_ia5<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
let count = g.gen_range::<usize>(0, 384);
fn arb_ia5(g: &mut Gen, _d: usize) -> ASN1Block {
let count = usize::arbitrary(g) % 384;
let mut items = Vec::new();
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()))
}
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);
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);
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);
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);
ASN1Block::BMPString(0, val)
}
fn arb_utc<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
loop {
let y = g.gen_range::<i32>(1970, 2069);
let m = g.gen_range::<u32>(1, 13);
let d = g.gen_range::<u32>(1, 32);
match Utc.ymd_opt(y, m, d) {
LocalResult::None => {}
LocalResult::Single(d) => {
let h = g.gen_range::<u32>(0, 24);
let m = g.gen_range::<u32>(0, 60);
let s = g.gen_range::<u32>(0, 60);
let t = d.and_hms(h, m, s);
return ASN1Block::UTCTime(0, t);
}
LocalResult::Ambiguous(_, _) => {}
}
}
fn arb_utc(g: &mut Gen, _d: usize) -> ASN1Block {
let min = Date::from_calendar_date(1950, Month::January, 01)
.unwrap()
.to_julian_day();
let max = Date::from_calendar_date(2049, Month::December, 31)
.unwrap()
.to_julian_day();
let date =
Date::from_julian_day(i32::arbitrary(g).rem_euclid(max - min + 1) + min).unwrap();
let h = u8::arbitrary(g).rem_euclid(24);
let m = u8::arbitrary(g).rem_euclid(60);
let s = u8::arbitrary(g).rem_euclid(60);
let time = Time::from_hms(h, m, s).unwrap();
let t = PrimitiveDateTime::new(date, time);
ASN1Block::UTCTime(0, t)
}
fn arb_time<G: Gen>(g: &mut G, _d: usize) -> ASN1Block {
loop {
let y = g.gen_range::<i32>(0, 10000);
let m = g.gen_range::<u32>(1, 13);
let d = g.gen_range::<u32>(1, 32);
match Utc.ymd_opt(y, m, d) {
LocalResult::None => {}
LocalResult::Single(d) => {
let h = g.gen_range::<u32>(0, 24);
let m = g.gen_range::<u32>(0, 60);
let s = g.gen_range::<u32>(0, 60);
let n = g.gen_range::<u32>(0, 1000000000);
let t = d.and_hms_nano(h, m, s, n);
return ASN1Block::GeneralizedTime(0, t);
}
LocalResult::Ambiguous(_, _) => {}
}
}
fn arb_time(g: &mut Gen, _d: usize) -> ASN1Block {
let min = Date::from_calendar_date(0, Month::January, 01)
.unwrap()
.to_julian_day();
let max = Date::from_calendar_date(9999, Month::December, 31)
.unwrap()
.to_julian_day();
let date =
Date::from_julian_day(i32::arbitrary(g).rem_euclid(max - min + 1) + min).unwrap();
let time = Time::arbitrary(g);
let t = PrimitiveDateTime::new(date, time);
ASN1Block::GeneralizedTime(0, t)
}
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);
if class == ASN1Class::Universal {
// Universal is invalid for an explicitly tagged block
@@ -1316,17 +1343,28 @@ mod tests {
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 tag = RandomUint::arbitrary(g);
let size = g.gen_range::<usize>(0, 128);
let items = g.sample_iter::<u8, _>(&Standard).take(size).collect();
let tag = loop {
let potential = RandomUint::arbitrary(g);
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)
}
fn limited_arbitrary<G: Gen>(g: &mut G, d: usize) -> ASN1Block {
let mut possibles: Vec<ASN1BlockGen<G>> = vec![
fn limited_arbitrary(g: &mut Gen, d: usize) -> ASN1Block {
let mut possibles: Vec<ASN1BlockGen> = vec![
arb_boolean,
arb_integer,
arb_bitstr,
@@ -1357,7 +1395,7 @@ mod tests {
}
impl Arbitrary for ASN1Block {
fn arbitrary<G: Gen>(g: &mut G) -> ASN1Block {
fn arbitrary(g: &mut Gen) -> ASN1Block {
limited_arbitrary(g, 2)
}
}
@@ -1397,23 +1435,43 @@ mod tests {
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]
fn generalized_time_tests() {
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(),
);
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(),
);
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(),
);
}
fn check_spec(d: &DateTime<Utc>, s: String) {
fn check_spec(d: &PrimitiveDateTime, s: String) {
let b = ASN1Block::GeneralizedTime(0, d.clone());
match to_der(&b) {
Err(_) => assert_eq!(format!("Broken: {}", d), s),
@@ -1539,4 +1597,23 @@ mod tests {
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>());
}
}