Skip to content

Commit

Permalink
Merge pull request #84 from NLnetLabs/time-fix
Browse files Browse the repository at this point in the history
Reorganize Time encoding.
  • Loading branch information
partim authored Oct 16, 2019
2 parents 400c6c5 + 61e4fdc commit e986fa4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
13 changes: 13 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@

Breaking

* Encoding of `x509::Time` values changed since in some cases it needs to
encode as either UTCTime or GeneralizedTime depending on the year. Thus,
there is no simple `encode` method anymore but rather, there now is
`encode_utc_time`, `encode_generalized_time`, or `encode_varied` to make
the choice explicit. [(#84)]

New

Bug Fixes

* Stop refusing to make IPv6-only ROAs (this wasn’t on purpose, honest).
[(#82)]
* Empty `IpBlocks` and `AsBlocks` where equal to everything. [(#83)]

Dependencies

[(#82)]: https://github.com/NLnetLabs/rpki-rs/pull/82
[(#83)]: https://github.com/NLnetLabs/rpki-rs/pull/83
[(#84)]: https://github.com/NLnetLabs/rpki-rs/pull/84


# 0.7.0
Expand Down
6 changes: 3 additions & 3 deletions src/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ impl TbsCertList<RevokedCertificates> {
1.encode(), // version
self.signature.x509_encode(),
self.issuer.encode_ref(),
self.this_update.encode(),
self.next_update.encode(),
self.this_update.encode_varied(),
self.next_update.encode_varied(),
self.revoked_certs.encode_ref(),
encode::sequence_as(Tag::CTX_0,
encode::sequence((
Expand Down Expand Up @@ -597,7 +597,7 @@ impl CrlEntry {
pub fn encode(self) -> impl encode::Values {
encode::sequence((
self.user_certificate.encode(),
self.revocation_date.encode(),
self.revocation_date.encode_varied(),
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ impl ManifestContent {
pub fn encode_ref<'a>(&'a self) -> impl encode::Values + 'a {
encode::sequence((
self.manifest_number.encode(),
self.this_update.encode(),
self.next_update.encode(),
self.this_update.encode_generalized_time(),
self.next_update.encode_generalized_time(),
self.file_hash_alg.encode_oid(),
encode::sequence(
&self.file_list
Expand Down
2 changes: 1 addition & 1 deletion src/sigobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl SignedAttrs {
encode::sequence((
oid::SIGNING_TIME.encode(),
encode::set(
time.encode(),
time.encode_varied(),
)
))
}),
Expand Down
50 changes: 46 additions & 4 deletions src/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,23 @@ impl Time {
Ok(())
}
}

pub fn encode_utc_time(self) -> impl encode::Values {
UtcTime(self).encode()
}

pub fn encode_generalized_time(self) -> impl encode::Values {
GeneralizedTime(self).encode()
}

pub fn encode_varied(self) -> impl encode::Values {
if self.year() < 1950 || self.year() > 2049 {
(None, Some(self.encode_generalized_time()))
}
else {
(Some(self.encode_utc_time()), None)
}
}
}


Expand Down Expand Up @@ -794,9 +811,34 @@ fn read_four_char<S: decode::Source>(source: &mut S) -> Result<u32, S::Err> {
}


//--- PrimitiveContent
//------------ AsUtcTime -----------------------------------------------------

pub struct UtcTime(Time);

impl PrimitiveContent for UtcTime {
const TAG: Tag = Tag::UTC_TIME;

fn encoded_len(&self, _: Mode) -> usize {
13 // yyMMddhhmmssZ
}

fn write_encoded<W: io::Write>(
&self, _: Mode, target: &mut W
) -> Result<(), io::Error> {
write!(
target, "{:02}{:02}{:02}{:02}{:02}{:02}Z",
self.0.year() % 100, self.0.month(), self.0.day(),
self.0.hour(), self.0.minute(), self.0.second()
)
}
}


//------------ AsGeneralizedTime ---------------------------------------------

pub struct GeneralizedTime(Time);

impl PrimitiveContent for Time {
impl PrimitiveContent for GeneralizedTime {
const TAG: Tag = Tag::GENERALIZED_TIME;

fn encoded_len(&self, _: Mode) -> usize {
Expand Down Expand Up @@ -881,8 +923,8 @@ impl Validity {

pub fn encode(self) -> impl encode::Values {
encode::sequence((
self.not_before.encode(),
self.not_after.encode(),
self.not_before.encode_varied(),
self.not_after.encode_varied(),
))
}
}
Expand Down

0 comments on commit e986fa4

Please sign in to comment.