Skip to content

Commit

Permalink
Merge #17
Browse files Browse the repository at this point in the history
17: Upgrade to chrono v0.4.23 r=[janosimas] a=Pagten

The `DateTime` constructs we were using are deprecated in chrono v0.4.23. The replacement constructors force clients to take into account the possibility of invalid or ambiguous dates.

Co-authored-by: Pieter Agten <[email protected]>
  • Loading branch information
bors[bot] and Pagten authored Dec 2, 2022
2 parents dfe3fa8 + b252edd commit ee20d14
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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]
Expand Down
17 changes: 11 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,16 @@ impl Into<chrono::DateTime<Utc>> 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<Self> {
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<Self> {
Utc.timestamp_opt(seconds, 0)
.earliest()
.map(Self)
}

pub fn to_seconds_since_epoch(&self) -> i64 {
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions tests/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ pub fn cert(get_random_printable_string: fn(usize) -> Vec<u8>)
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())),
Expand Down

0 comments on commit ee20d14

Please sign in to comment.