diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-01-24 17:49:48 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-01-24 18:08:00 +0100 |
commit | 29affd9dbc309242487bbaaae607c5de0886f83e (patch) | |
tree | 5bced386f928d5ace82d0dec72d19c99ce208212 | |
parent | db26eead9e1e90498f60a396758c3758894b5ddf (diff) |
openpgp: Introduce trait Amalgamation.
- First, we implement it for ValidComponentAmalgamation.
-rw-r--r-- | openpgp/src/autocrypt.rs | 1 | ||||
-rw-r--r-- | openpgp/src/cert/amalgamation.rs | 77 | ||||
-rw-r--r-- | openpgp/src/cert/builder.rs | 1 | ||||
-rw-r--r-- | openpgp/src/cert/component_iter.rs | 4 | ||||
-rw-r--r-- | openpgp/src/cert/components.rs | 1 | ||||
-rw-r--r-- | openpgp/src/cert/key_amalgamation.rs | 5 | ||||
-rw-r--r-- | openpgp/src/cert/mod.rs | 1 | ||||
-rw-r--r-- | openpgp/src/cert/revoke.rs | 4 |
8 files changed, 68 insertions, 26 deletions
diff --git a/openpgp/src/autocrypt.rs b/openpgp/src/autocrypt.rs index b66d9770..b308e29d 100644 --- a/openpgp/src/autocrypt.rs +++ b/openpgp/src/autocrypt.rs @@ -26,6 +26,7 @@ use crate::Result; use crate::Packet; use crate::packet::SKESK; use crate::Cert; +use crate::cert::components::Amalgamation; use crate::parse::{ Parse, PacketParserResult, PacketParser, diff --git a/openpgp/src/cert/amalgamation.rs b/openpgp/src/cert/amalgamation.rs index 037f4cb1..ea07031f 100644 --- a/openpgp/src/cert/amalgamation.rs +++ b/openpgp/src/cert/amalgamation.rs @@ -189,7 +189,11 @@ impl<'a, C> ValidComponentAmalgamation<'a, C> } } -impl<'a, C> ValidComponentAmalgamation<'a, C> { +/// Represents a component under a given policy. +pub trait Amalgamation<'a> { + /// Returns the certificate that the component came from. + fn cert(&self) -> &'a Cert; + /// Returns the amalgamation's reference time. /// /// For queries that are with respect to a point in time, this @@ -197,48 +201,75 @@ impl<'a, C> ValidComponentAmalgamation<'a, C> { /// created at `t_c` and expires at `t_e`, then /// `ValidComponentAmalgamation::alive` will return true if the reference /// time is greater than or equal to `t_c` and less than `t_e`. - pub fn time(&self) -> SystemTime { - self.time - } + fn time(&self) -> SystemTime; /// Changes the amalgamation's policy. /// /// If `time` is `None`, the current time is used. - pub fn policy<T>(self, time: T) -> Result<Self> - where T: Into<Option<time::SystemTime>> - { - let time = time.into().unwrap_or_else(SystemTime::now); - self.a.policy(time) - } + fn policy<T>(self, time: T) -> Result<Self> + where Self: Sized, T: Into<Option<time::SystemTime>>; - /// Returns the component's binding signature as of the reference time, - /// if any. - pub fn binding_signature(&self) -> &'a Signature { - self.binding_signature - } + /// Returns the component's binding signature as of the reference time. + fn binding_signature(&self) -> &'a Signature; /// Returns the component's revocation status as of the amalgamation's /// reference time. /// /// Note: this does not return whether the certificate is valid. - pub fn revoked(&self) -> RevocationStatus<'a> { - self.binding._revoked(false, Some(self.binding_signature), self.time) - } + fn revoked(&self) -> RevocationStatus<'a>; /// Returns the certificate's revocation status as of the /// amalgamtion's reference time. - pub fn cert_revoked(&self) -> RevocationStatus<'a> { + fn cert_revoked(&self) -> RevocationStatus<'a> { self.cert().revoked(self.time()) } /// Returns whether the certificateis alive as of the /// amalgamtion's reference time. - pub fn cert_alive(&self) -> Result<()> { + fn cert_alive(&self) -> Result<()> { self.cert().alive(self.time()) } +} - /// Returns this component's component binding. - pub fn binding(&self) -> &'a ComponentBinding<C> { - &self.binding +impl<'a, C> Amalgamation<'a> for ValidComponentAmalgamation<'a, C> { + // NOTE: No docstring, because ComponentAmalgamation has the same method. + // Returns the certificate that the component came from. + fn cert(&self) -> &'a Cert { + self.cert + } + + /// Returns the amalgamation's reference time. + /// + /// For queries that are with respect to a point in time, this + /// determines that point in time. For instance, if a component is + /// created at `t_c` and expires at `t_e`, then + /// `ValidComponentAmalgamation::alive` will return true if the reference + /// time is greater than or equal to `t_c` and less than `t_e`. + fn time(&self) -> SystemTime { + self.time + } + + /// Changes the amalgamation's policy. + /// + /// If `time` is `None`, the current time is used. + fn policy<T>(self, time: T) -> Result<Self> + where T: Into<Option<time::SystemTime>> + { + let time = time.into().unwrap_or_else(SystemTime::now); + self.a.policy(time) + } + + /// Returns the component's binding signature as of the reference time. + fn binding_signature(&self) -> &'a Signature { + self.binding_signature + } + + /// Returns the component's revocation status as of the amalgamation's + /// reference time. + /// + /// Note: this does not return whether the certificate is valid. + fn revoked(&self) -> RevocationStatus<'a> { + self.binding._revoked(false, Some(self.binding_signature), self.time) } } + diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs index b1152517..2df4606d 100644 --- a/openpgp/src/cert/builder.rs +++ b/openpgp/src/cert/builder.rs @@ -463,6 +463,7 @@ impl CertBuilder { #[cfg(test)] mod tests { use super::*; + use crate::cert::components::Amalgamation; use crate::packet::signature::subpacket::{SubpacketTag, SubpacketValue}; use crate::types::PublicKeyAlgorithm; diff --git a/openpgp/src/cert/component_iter.rs b/openpgp/src/cert/component_iter.rs index 05d4293f..9f7b015e 100644 --- a/openpgp/src/cert/component_iter.rs +++ b/openpgp/src/cert/component_iter.rs @@ -8,8 +8,7 @@ use crate::{ components::{ ComponentBinding, ComponentBindingIter, - }, - amalgamation::{ + Amalgamation, ComponentAmalgamation, ValidComponentAmalgamation, }, @@ -177,6 +176,7 @@ impl<'a, C> ValidComponentIter<'a, C> { /// # use openpgp::Result; /// # use openpgp::cert::CertBuilder; /// use openpgp::RevocationStatus; + /// use openpgp::cert::components::Amalgamation; /// /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { diff --git a/openpgp/src/cert/components.rs b/openpgp/src/cert/components.rs index 7b602cfd..0b9b2da6 100644 --- a/openpgp/src/cert/components.rs +++ b/openpgp/src/cert/components.rs @@ -23,6 +23,7 @@ use super::{ canonical_signature_order, }; pub use super::amalgamation::{ + Amalgamation, ComponentAmalgamation, ValidComponentAmalgamation, }; diff --git a/openpgp/src/cert/key_amalgamation.rs b/openpgp/src/cert/key_amalgamation.rs index 799873aa..31183b24 100644 --- a/openpgp/src/cert/key_amalgamation.rs +++ b/openpgp/src/cert/key_amalgamation.rs @@ -7,7 +7,10 @@ use std::ops::Deref; use crate::{ Cert, - cert::components::KeyBinding, + cert::components::{ + Amalgamation, + KeyBinding, + }, Error, packet::key, packet::key::SecretKeyMaterial, diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 06d97a0e..3b77be37 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -1286,6 +1286,7 @@ impl Cert { #[cfg(test)] mod test { use crate::serialize::Serialize; + use super::components::Amalgamation; use super::*; use crate::{ diff --git a/openpgp/src/cert/revoke.rs b/openpgp/src/cert/revoke.rs index cc0748d7..986af9e4 100644 --- a/openpgp/src/cert/revoke.rs +++ b/openpgp/src/cert/revoke.rs @@ -268,6 +268,8 @@ impl Deref for SubkeyRevocationBuilder { /// /// ``` /// # use sequoia_openpgp::{*, packet::*, types::*, cert::*}; +/// use sequoia_openpgp::cert::components::Amalgamation; +/// /// # f().unwrap(); /// # fn f() -> Result<()> { /// // Generate a Cert, and create a keypair from the primary key. @@ -380,6 +382,8 @@ impl Deref for UserIDRevocationBuilder { /// /// ``` /// # use sequoia_openpgp::{*, packet::*, types::*, cert::*}; +/// use sequoia_openpgp::cert::components::Amalgamation; +/// /// # f().unwrap(); /// # fn f() -> Result<()> { /// # let subpacket |