diff --git a/Cargo.toml b/Cargo.toml index 1fe91a5..376f268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pkix" -version = "0.1.3" +version = "0.2.0" authors = ["Fortanix Inc."] license = "MPL-2.0" description = "TLS Certificate encoding and decoding helpers." @@ -15,7 +15,7 @@ num-bigint = { version = "0.2", default-features = false } num-integer = { version = "0.1", default-features = false } bit-vec = "0.6" lazy_static = "1" -chrono = "0.4" +chrono = "0.4.23" b64-ct = "0.1.1" [dev-dependencies] diff --git a/src/types.rs b/src/types.rs index e9c1543..5ba3a2b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -658,12 +658,16 @@ impl Into> for DateTime { } impl DateTime { - pub fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Self { - DateTime(Utc.ymd(year.into(), month.into(), day.into()).and_hms(hour.into(), minute.into(), second.into())) + pub fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Option { + Utc.with_ymd_and_hms(year.into(), month.into(), day.into(), hour.into(), minute.into(), second.into()) + .earliest() + .map(Self) } - pub fn from_seconds_since_epoch(seconds: i64) -> Self { - DateTime(Utc.timestamp(seconds, 0)) + pub fn from_seconds_since_epoch(seconds: i64) -> Option { + Utc.timestamp_opt(seconds, 0) + .earliest() + .map(Self) } pub fn to_seconds_since_epoch(&self) -> i64 { @@ -744,7 +748,8 @@ impl BERDecodable for DateTime { let minute = iter.next().ok_or(ASN1Error::new(ASN1ErrorKind::Invalid))?; let second = iter.next().ok_or(ASN1Error::new(ASN1ErrorKind::Invalid))?; - Ok(DateTime::new(year, month, day, hour, minute, second)) + DateTime::new(year, month, day, hour, minute, second) + .ok_or(ASN1Error::new(ASN1ErrorKind::Invalid)) } } @@ -974,7 +979,7 @@ mod tests { #[test] fn datetime() { - let datetime = DateTime::new(2017, 5, 19, 12, 34, 56); + let datetime = DateTime::new(2017, 5, 19, 12, 34, 56).unwrap(); let der = vec![0x17, 0x0d, 0x31, 0x37, 0x30, 0x35, 0x31, 0x39, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x5a]; diff --git a/tests/fakes.rs b/tests/fakes.rs index 9e59ab5..c044161 100644 --- a/tests/fakes.rs +++ b/tests/fakes.rs @@ -115,8 +115,8 @@ pub fn cert(get_random_printable_string: fn(usize) -> Vec) issuer: vec![(oid::dnQualifier.clone(), TaggedDerValue::from_tag_and_bytes(TAG_PRINTABLESTRING, get_random_printable_string(42)))].into(), - validity_notbefore: DateTime::new(1970, 1, 1, 0, 0, 0), - validity_notafter: DateTime::new(1970, 1, 1, 0, 0, 0), + validity_notbefore: DateTime::new(1970, 1, 1, 0, 0, 0).unwrap(), + validity_notafter: DateTime::new(1970, 1, 1, 0, 0, 0).unwrap(), subject: vec![(oid::description.clone(), TaggedDerValue::from_tag_and_bytes(TAG_UTF8STRING, b"Known keys only".to_vec())),