diff --git a/fuzz/fuzz_targets/fuzz_asn1_parse.rs b/fuzz/fuzz_targets/fuzz_asn1_parse.rs index 06c7e55..516774d 100644 --- a/fuzz/fuzz_targets/fuzz_asn1_parse.rs +++ b/fuzz/fuzz_targets/fuzz_asn1_parse.rs @@ -17,7 +17,7 @@ enum Data<'a> { ObjectIdentifier(asn1::ObjectIdentifier), UtcTime(asn1::UtcTime), - GeneralizedTime(asn1::GeneralizedTime), + X509GeneralizedTime(asn1::X509GeneralizedTime), Enumerated(asn1::Enumerated), diff --git a/src/lib.rs b/src/lib.rs index 133a03c..d573109 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,7 @@ //! #[derive(asn1::Asn1Read, asn1::Asn1Write)] //! enum Time { //! UTCTime(asn1::UtcTime), -//! GeneralizedTime(asn1::GeneralizedTime) +//! X509GeneralizedTime(asn1::X509GeneralizedTime) //! } //! ``` //! @@ -158,10 +158,10 @@ pub use crate::tag::Tag; pub use crate::types::{ Asn1DefinedByReadable, Asn1DefinedByWritable, Asn1Readable, Asn1Writable, BMPString, BigInt, BigUint, Choice1, Choice2, Choice3, DateTime, DefinedByMarker, Enumerated, Explicit, - GeneralizedTime, GeneralizedTimeFractional, IA5String, Implicit, Null, OctetStringEncoded, - OwnedBigInt, OwnedBigUint, PrintableString, Sequence, SequenceOf, SequenceOfWriter, - SequenceWriter, SetOf, SetOfWriter, SimpleAsn1Readable, SimpleAsn1Writable, Tlv, - UniversalString, UtcTime, Utf8String, VisibleString, + GeneralizedTime, IA5String, Implicit, Null, OctetStringEncoded, OwnedBigInt, OwnedBigUint, + PrintableString, Sequence, SequenceOf, SequenceOfWriter, SequenceWriter, SetOf, SetOfWriter, + SimpleAsn1Readable, SimpleAsn1Writable, Tlv, UniversalString, UtcTime, Utf8String, + VisibleString, X509GeneralizedTime, }; pub use crate::writer::{write, write_single, WriteBuf, WriteError, WriteResult, Writer}; diff --git a/src/parser.rs b/src/parser.rs index b52c7fa..9a48af2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -347,10 +347,10 @@ mod tests { use crate::types::Asn1Readable; use crate::{ BMPString, BigInt, BigUint, BitString, Choice1, Choice2, Choice3, DateTime, Enumerated, - Explicit, GeneralizedTime, GeneralizedTimeFractional, IA5String, Implicit, - ObjectIdentifier, OctetStringEncoded, OwnedBigInt, OwnedBigUint, OwnedBitString, - ParseError, ParseErrorKind, ParseLocation, ParseResult, PrintableString, Sequence, - SequenceOf, SetOf, Tag, Tlv, UniversalString, UtcTime, Utf8String, VisibleString, + Explicit, GeneralizedTime, IA5String, Implicit, ObjectIdentifier, OctetStringEncoded, + OwnedBigInt, OwnedBigUint, OwnedBitString, ParseError, ParseErrorKind, ParseLocation, + ParseResult, PrintableString, Sequence, SequenceOf, SetOf, Tag, Tlv, UniversalString, + UtcTime, Utf8String, VisibleString, X509GeneralizedTime, }; #[cfg(not(feature = "std"))] use alloc::boxed::Box; @@ -1440,9 +1440,9 @@ mod tests { #[test] fn test_generalizedtime() { - assert_parses::(&[ + assert_parses::(&[ ( - Ok(GeneralizedTime::new(DateTime::new(2010, 1, 2, 3, 4, 5).unwrap()).unwrap()), + Ok(X509GeneralizedTime::new(DateTime::new(2010, 1, 2, 3, 4, 5).unwrap()).unwrap()), b"\x18\x0f20100102030405Z", ), ( @@ -1459,7 +1459,7 @@ mod tests { ), ( // 29th of February (Leap Year) - Ok(GeneralizedTime::new(DateTime::new(2000, 2, 29, 3, 4, 5).unwrap()).unwrap()), + Ok(X509GeneralizedTime::new(DateTime::new(2000, 2, 29, 3, 4, 5).unwrap()).unwrap()), b"\x18\x0f20000229030405Z", ), ( @@ -1590,10 +1590,10 @@ mod tests { #[test] fn test_generalized_time_fractional() { - assert_parses::(&[ + assert_parses::(&[ ( // General case - Ok(GeneralizedTimeFractional::new( + Ok(GeneralizedTime::new( DateTime::new(2010, 1, 2, 3, 4, 5).unwrap(), Some(123_456_000), ) @@ -1602,16 +1602,15 @@ mod tests { ), ( // No fractional time - Ok(GeneralizedTimeFractional::new( - DateTime::new(2010, 1, 2, 3, 4, 5).unwrap(), - None, - ) - .unwrap()), + Ok( + GeneralizedTime::new(DateTime::new(2010, 1, 2, 3, 4, 5).unwrap(), None) + .unwrap(), + ), b"\x18\x0f20100102030405Z", ), ( // Starting with 0 is ok - Ok(GeneralizedTimeFractional::new( + Ok(GeneralizedTime::new( DateTime::new(2010, 1, 2, 3, 4, 5).unwrap(), Some(12_375_600), ) diff --git a/src/types.rs b/src/types.rs index 8c52afb..4eef4c7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -915,8 +915,11 @@ fn push_four_digits(dest: &mut WriteBuf, val: u16) -> WriteResult { } /// A structure representing a (UTC timezone) date and time. -/// Wrapped by `UtcTime` and `GeneralizedTime` and used in -/// `GeneralizedTimeFractional`. +/// Wrapped by `UtcTime` and `X509GeneralizedTime` and used in +/// `GeneralizedTime`. +/// The difference between in `X509GeneralizedTime` and +/// `GeneralizedTime` is that the `X509GeneralizedTime` does not +/// accept fractional seconds. #[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd)] pub struct DateTime { year: u16, @@ -1044,11 +1047,11 @@ impl SimpleAsn1Writable for UtcTime { /// Used for parsing and writing ASN.1 `GENERALIZED TIME` values. Wraps a /// `DateTime`. #[derive(Debug, Clone, PartialEq, Hash, Eq)] -pub struct GeneralizedTime(DateTime); +pub struct X509GeneralizedTime(DateTime); -impl GeneralizedTime { - pub fn new(dt: DateTime) -> ParseResult { - Ok(GeneralizedTime(dt)) +impl X509GeneralizedTime { + pub fn new(dt: DateTime) -> ParseResult { + Ok(X509GeneralizedTime(dt)) } pub fn as_datetime(&self) -> &DateTime { @@ -1056,9 +1059,9 @@ impl GeneralizedTime { } } -impl SimpleAsn1Readable<'_> for GeneralizedTime { +impl SimpleAsn1Readable<'_> for X509GeneralizedTime { const TAG: Tag = Tag::primitive(0x18); - fn parse_data(mut data: &[u8]) -> ParseResult { + fn parse_data(mut data: &[u8]) -> ParseResult { let year = read_4_digits(&mut data)?; let month = read_2_digits(&mut data)?; let day = read_2_digits(&mut data)?; @@ -1068,11 +1071,11 @@ impl SimpleAsn1Readable<'_> for GeneralizedTime { read_tz_and_finish(&mut data)?; - GeneralizedTime::new(DateTime::new(year, month, day, hour, minute, second)?) + X509GeneralizedTime::new(DateTime::new(year, month, day, hour, minute, second)?) } } -impl SimpleAsn1Writable for GeneralizedTime { +impl SimpleAsn1Writable for X509GeneralizedTime { const TAG: Tag = Tag::primitive(0x18); fn write_data(&self, dest: &mut WriteBuf) -> WriteResult { let dt = self.as_datetime(); @@ -1092,20 +1095,20 @@ impl SimpleAsn1Writable for GeneralizedTime { /// fractional seconds value. /// See https://github.com/alex/rust-asn1/issues/491 for discussion. #[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq)] -pub struct GeneralizedTimeFractional { +pub struct GeneralizedTime { datetime: DateTime, nanoseconds: Option, // Up to 1 ns precision (10^9) } -impl GeneralizedTimeFractional { - pub fn new(dt: DateTime, nanoseconds: Option) -> ParseResult { +impl GeneralizedTime { + pub fn new(dt: DateTime, nanoseconds: Option) -> ParseResult { if let Some(val) = nanoseconds { if val < 1 || val >= 1e9 as u32 { return Err(ParseError::new(ParseErrorKind::InvalidValue)); } } - Ok(GeneralizedTimeFractional { + Ok(GeneralizedTime { datetime: dt, nanoseconds, }) @@ -1156,9 +1159,9 @@ fn read_fractional_time(data: &mut &[u8]) -> ParseResult> { } } -impl SimpleAsn1Readable<'_> for GeneralizedTimeFractional { +impl SimpleAsn1Readable<'_> for GeneralizedTime { const TAG: Tag = Tag::primitive(0x18); - fn parse_data(mut data: &[u8]) -> ParseResult { + fn parse_data(mut data: &[u8]) -> ParseResult { let year = read_4_digits(&mut data)?; let month = read_2_digits(&mut data)?; let day = read_2_digits(&mut data)?; @@ -1169,14 +1172,14 @@ impl SimpleAsn1Readable<'_> for GeneralizedTimeFractional { let fraction = read_fractional_time(&mut data)?; read_tz_and_finish(&mut data)?; - GeneralizedTimeFractional::new( + GeneralizedTime::new( DateTime::new(year, month, day, hour, minute, second)?, fraction, ) } } -impl SimpleAsn1Writable for GeneralizedTimeFractional { +impl SimpleAsn1Writable for GeneralizedTime { const TAG: Tag = Tag::primitive(0x18); fn write_data(&self, dest: &mut WriteBuf) -> WriteResult { let dt = self.as_datetime(); @@ -1841,9 +1844,9 @@ impl DefinedByMarker { mod tests { use crate::{ parse_single, BigInt, BigUint, DateTime, DefinedByMarker, Enumerated, GeneralizedTime, - GeneralizedTimeFractional, IA5String, ObjectIdentifier, OctetStringEncoded, OwnedBigInt, - OwnedBigUint, ParseError, ParseErrorKind, PrintableString, SequenceOf, SequenceOfWriter, - SetOf, SetOfWriter, Tag, Tlv, UtcTime, Utf8String, VisibleString, + IA5String, ObjectIdentifier, OctetStringEncoded, OwnedBigInt, OwnedBigUint, ParseError, + ParseErrorKind, PrintableString, SequenceOf, SequenceOfWriter, SetOf, SetOfWriter, Tag, + Tlv, UtcTime, Utf8String, VisibleString, X509GeneralizedTime, }; use crate::{Explicit, Implicit}; #[cfg(not(feature = "std"))] @@ -2117,22 +2120,19 @@ mod tests { #[test] fn test_generalized_time_new() { - assert!(GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap()).is_ok()); + assert!(X509GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap()).is_ok()); } #[test] fn test_generalized_time_fractional_new() { - assert!(GeneralizedTimeFractional::new( - DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), - Some(1234) - ) - .is_ok()); - assert!(GeneralizedTimeFractional::new( - DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), - None - ) - .is_ok()); - assert!(GeneralizedTimeFractional::new( + assert!( + GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), Some(1234)) + .is_ok() + ); + assert!( + GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), None).is_ok() + ); + assert!(GeneralizedTime::new( DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), Some(1e9 as u32 + 1) ) @@ -2141,34 +2141,23 @@ mod tests { #[test] fn test_generalized_time_fractional_partial_ord() { - let point = GeneralizedTimeFractional::new( - DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), - Some(1234), - ) - .unwrap(); + let point = + GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), Some(1234)) + .unwrap(); assert!( point - < GeneralizedTimeFractional::new( - DateTime::new(2023, 6, 30, 23, 59, 59).unwrap(), - Some(1234) - ) - .unwrap() + < GeneralizedTime::new(DateTime::new(2023, 6, 30, 23, 59, 59).unwrap(), Some(1234)) + .unwrap() ); assert!( point - < GeneralizedTimeFractional::new( - DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), - Some(1235) - ) - .unwrap() + < GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), Some(1235)) + .unwrap() ); assert!( point - > GeneralizedTimeFractional::new( - DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), - None - ) - .unwrap() + > GeneralizedTime::new(DateTime::new(2015, 6, 30, 23, 59, 59).unwrap(), None) + .unwrap() ); } diff --git a/src/writer.rs b/src/writer.rs index 9266685..b587f81 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -216,10 +216,10 @@ mod tests { use crate::types::Asn1Writable; use crate::{ parse_single, BMPString, BigInt, BigUint, BitString, Choice1, Choice2, Choice3, DateTime, - Enumerated, Explicit, GeneralizedTime, GeneralizedTimeFractional, IA5String, Implicit, - ObjectIdentifier, OctetStringEncoded, OwnedBigInt, OwnedBigUint, OwnedBitString, - PrintableString, Sequence, SequenceOf, SequenceOfWriter, SequenceWriter, SetOf, - SetOfWriter, Tlv, UniversalString, UtcTime, Utf8String, VisibleString, WriteError, + Enumerated, Explicit, GeneralizedTime, IA5String, Implicit, ObjectIdentifier, + OctetStringEncoded, OwnedBigInt, OwnedBigUint, OwnedBitString, PrintableString, Sequence, + SequenceOf, SequenceOfWriter, SequenceWriter, SetOf, SetOfWriter, Tlv, UniversalString, + UtcTime, Utf8String, VisibleString, WriteError, X509GeneralizedTime, }; #[cfg(not(feature = "std"))] use alloc::vec::Vec; @@ -553,18 +553,18 @@ mod tests { } #[test] - fn test_write_generalizedtime() { + fn test_write_generalizedtime_x509() { assert_writes(&[ ( - GeneralizedTime::new(DateTime::new(1991, 5, 6, 23, 45, 40).unwrap()).unwrap(), + X509GeneralizedTime::new(DateTime::new(1991, 5, 6, 23, 45, 40).unwrap()).unwrap(), b"\x18\x0f19910506234540Z", ), ( - GeneralizedTime::new(DateTime::new(1970, 1, 1, 0, 0, 0).unwrap()).unwrap(), + X509GeneralizedTime::new(DateTime::new(1970, 1, 1, 0, 0, 0).unwrap()).unwrap(), b"\x18\x0f19700101000000Z", ), ( - GeneralizedTime::new(DateTime::new(2009, 11, 15, 22, 56, 16).unwrap()).unwrap(), + X509GeneralizedTime::new(DateTime::new(2009, 11, 15, 22, 56, 16).unwrap()).unwrap(), b"\x18\x0f20091115225616Z", ), ]); @@ -574,28 +574,21 @@ mod tests { fn test_write_generalizedtime_fractional() { assert_writes(&[ ( - GeneralizedTimeFractional::new( - DateTime::new(1991, 5, 6, 23, 45, 40).unwrap(), - Some(1_234), - ) - .unwrap(), + GeneralizedTime::new(DateTime::new(1991, 5, 6, 23, 45, 40).unwrap(), Some(1_234)) + .unwrap(), b"\x18\x1919910506234540.000001234Z", ), ( - GeneralizedTimeFractional::new( - DateTime::new(1991, 5, 6, 23, 45, 40).unwrap(), - Some(1), - ) - .unwrap(), + GeneralizedTime::new(DateTime::new(1991, 5, 6, 23, 45, 40).unwrap(), Some(1)) + .unwrap(), b"\x18\x1919910506234540.000000001Z", ), ( - GeneralizedTimeFractional::new(DateTime::new(1970, 1, 1, 0, 0, 0).unwrap(), None) - .unwrap(), + GeneralizedTime::new(DateTime::new(1970, 1, 1, 0, 0, 0).unwrap(), None).unwrap(), b"\x18\x0f19700101000000Z", ), ( - GeneralizedTimeFractional::new( + GeneralizedTime::new( DateTime::new(2009, 11, 15, 22, 56, 16).unwrap(), Some(100_000_000), ) @@ -603,7 +596,7 @@ mod tests { b"\x18\x1120091115225616.1Z", ), ( - GeneralizedTimeFractional::new( + GeneralizedTime::new( DateTime::new(2009, 11, 15, 22, 56, 16).unwrap(), Some(999_999_999), )