diff options
author | Neal H. Walfield <neal@pep.foundation> | 2020-02-20 22:10:48 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2020-02-20 22:11:54 +0100 |
commit | a57262d26636590eaec123d7b60b9efdd604a61e (patch) | |
tree | c3b24fe46302468ce68ff7317ebb2de314bfec42 /openpgp/src/cert | |
parent | 291edaec90f6ef8dd6daab96478bd7d33a0066ab (diff) |
openpgp: Add the bundle method to the Amalgamation trait.
- Add the `bundle()` method to the Amalgamation trait instead of
implementing it on each struct.
Diffstat (limited to 'openpgp/src/cert')
-rw-r--r-- | openpgp/src/cert/amalgamation.rs | 30 | ||||
-rw-r--r-- | openpgp/src/cert/builder.rs | 1 | ||||
-rw-r--r-- | openpgp/src/cert/component_iter.rs | 3 | ||||
-rw-r--r-- | openpgp/src/cert/key_amalgamation.rs | 56 | ||||
-rw-r--r-- | openpgp/src/cert/mod.rs | 4 |
5 files changed, 54 insertions, 40 deletions
diff --git a/openpgp/src/cert/amalgamation.rs b/openpgp/src/cert/amalgamation.rs index 380abbeb..9b7d0f1e 100644 --- a/openpgp/src/cert/amalgamation.rs +++ b/openpgp/src/cert/amalgamation.rs @@ -31,11 +31,14 @@ impl<'a, C> std::ops::Deref for ComponentAmalgamation<'a, C> { } } -impl<'a, C> Amalgamation<'a> for ComponentAmalgamation<'a, C> { - /// Returns the certificate that the component came from. +impl<'a, C> Amalgamation<'a, C> for ComponentAmalgamation<'a, C> { fn cert(&self) -> &'a Cert { self.cert } + + fn bundle(&self) -> &'a ComponentBundle<C> { + &self.bundle + } } impl<'a, C> ComponentAmalgamation<'a, C> { @@ -48,11 +51,6 @@ impl<'a, C> ComponentAmalgamation<'a, C> { } } - /// Returns this component's bundle. - pub fn bundle(&self) -> &'a ComponentBundle<C> { - &self.bundle - } - /// Returns the components's binding signature as of the reference /// time, if any. /// @@ -202,13 +200,17 @@ impl<'a, C> ValidComponentAmalgamation<'a, C> } /// Represents a component. -pub trait Amalgamation<'a> { +pub trait Amalgamation<'a, C> { /// Returns the certificate that the component came from. fn cert(&self) -> &'a Cert; + + + /// Returns this component's bundle. + fn bundle(&self) -> &'a ComponentBundle<C>; } /// Represents a component under a given policy. -pub trait ValidAmalgamation<'a> : Amalgamation<'a>{ +pub trait ValidAmalgamation<'a, C> : Amalgamation<'a, C>{ /// Returns the amalgamation's reference time. /// /// For queries that are with respect to a point in time, this @@ -382,15 +384,19 @@ pub trait ValidAmalgamation<'a> : Amalgamation<'a>{ } } -impl<'a, C> Amalgamation<'a> for ValidComponentAmalgamation<'a, C> { +impl<'a, C> Amalgamation<'a, C> 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 } + + fn bundle(&self) -> &'a ComponentBundle<C> { + self.bundle + } } -impl<'a, C> ValidAmalgamation<'a> for ValidComponentAmalgamation<'a, C> { +impl<'a, C> ValidAmalgamation<'a, C> for ValidComponentAmalgamation<'a, C> { /// Returns the amalgamation's reference time. /// /// For queries that are with respect to a point in time, this @@ -461,5 +467,5 @@ impl<'a, C> ValidAmalgamation<'a> for ValidComponentAmalgamation<'a, C> { } } -impl<'a, C> crate::cert::Preferences<'a> +impl<'a, C> crate::cert::Preferences<'a, C> for ValidComponentAmalgamation<'a, C> {} diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs index 00676576..1a8ea4be 100644 --- a/openpgp/src/cert/builder.rs +++ b/openpgp/src/cert/builder.rs @@ -428,6 +428,7 @@ impl CertBuilder { #[cfg(test)] mod tests { use super::*; + use crate::cert::components::Amalgamation; use crate::cert::components::ValidAmalgamation; 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 ff844ac8..ec017fe9 100644 --- a/openpgp/src/cert/component_iter.rs +++ b/openpgp/src/cert/component_iter.rs @@ -6,6 +6,7 @@ use crate::{ cert::{ Cert, components::{ + Amalgamation, ComponentBundle, ComponentBundleIter, ComponentAmalgamation, @@ -182,7 +183,7 @@ impl<'a, C> ValidComponentIter<'a, C> { /// # use openpgp::Result; /// # use openpgp::cert::CertBuilder; /// use openpgp::types::RevocationStatus; - /// use openpgp::cert::components::ValidAmalgamation; + /// use openpgp::cert::components::{Amalgamation, ValidAmalgamation}; /// use sequoia_openpgp::policy::StandardPolicy; /// /// # fn main() { f().unwrap(); } diff --git a/openpgp/src/cert/key_amalgamation.rs b/openpgp/src/cert/key_amalgamation.rs index dcc74599..be72ebe2 100644 --- a/openpgp/src/cert/key_amalgamation.rs +++ b/openpgp/src/cert/key_amalgamation.rs @@ -47,10 +47,25 @@ impl<'a, P: key::KeyParts> Deref for KeyAmalgamation<'a, P> { } } -impl<'a, P: 'a + key::KeyParts> Amalgamation<'a> for KeyAmalgamation<'a, P> { +impl<'a, P: 'a + key::KeyParts> Amalgamation<'a, Key<P, key::UnspecifiedRole>> + for KeyAmalgamation<'a, P> +{ fn cert(&self) -> &'a Cert { self.cert } + + fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { + match self { + KeyAmalgamation { bundle: KeyAmalgamationBundle::Primary(), .. } => + P::convert_bundle_ref((&self.cert.primary).into()) + .expect("secret key amalgamations contain secret keys"), + KeyAmalgamation { bundle: KeyAmalgamationBundle::Subordinate(bundle), .. } => + P::convert_bundle_ref((*bundle) + .mark_parts_unspecified_ref() + .mark_role_unspecified_ref()) + .expect("secret key amalgamations contain secret keys"), + } + } } impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> { @@ -94,20 +109,6 @@ impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> { } } - /// Returns this key's bundle. - pub fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { - match self { - KeyAmalgamation { bundle: KeyAmalgamationBundle::Primary(), .. } => - P::convert_bundle_ref((&self.cert.primary).into()) - .expect("secret key amalgamations contain secret keys"), - KeyAmalgamation { bundle: KeyAmalgamationBundle::Subordinate(bundle), .. } => - P::convert_bundle_ref((*bundle) - .mark_parts_unspecified_ref() - .mark_role_unspecified_ref()) - .expect("secret key amalgamations contain secret keys"), - } - } - /// Returns the key's binding signature as of the reference time, /// if any. /// @@ -274,17 +275,21 @@ impl<'a, P: key::KeyParts> From<ValidKeyAmalgamation<'a, P>> } } -impl<'a, P: 'a + key::KeyParts> Amalgamation<'a> +impl<'a, P: 'a + key::KeyParts> Amalgamation<'a, Key<P, key::UnspecifiedRole>> for ValidKeyAmalgamation<'a, P> { // NOTE: No docstring, because KeyAmalgamation has the same method. // Returns the certificate that the component came from. fn cert(&self) -> &'a Cert { - self.cert + self.a.cert + } + + fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { + self.a.bundle() } } -impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a> +impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a, Key<P, key::UnspecifiedRole>> for ValidKeyAmalgamation<'a, P> { /// Returns the amalgamation's reference time. @@ -364,11 +369,6 @@ impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a> } impl<'a, P: 'a + key::KeyParts> ValidKeyAmalgamation<'a, P> { - /// Returns this key's bundle. - pub fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { - self.a.bundle() - } - /// Returns whether the key is alive as of the amalgamtion's /// reference time. /// @@ -457,7 +457,7 @@ impl<'a, P: key::KeyParts> ValidPrimaryKeyAmalgamation<'a, P> { } } -impl<'a, P: 'a + key::KeyParts> Amalgamation<'a> +impl<'a, P: 'a + key::KeyParts> Amalgamation<'a, Key<P, key::UnspecifiedRole>> for ValidPrimaryKeyAmalgamation<'a, P> { // NOTE: No docstring, because KeyAmalgamation has the same method. @@ -465,9 +465,13 @@ impl<'a, P: 'a + key::KeyParts> Amalgamation<'a> fn cert(&self) -> &'a Cert { self.a.cert() } + + fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { + &self.a.bundle() + } } -impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a> +impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a, Key<P, key::UnspecifiedRole>> for ValidPrimaryKeyAmalgamation<'a, P> { /// Returns the amalgamation's reference time. @@ -541,5 +545,5 @@ impl<'a, P: 'a + key::KeyParts> ValidAmalgamation<'a> } } -impl<'a, P: key::KeyParts> crate::cert::Preferences<'a> +impl<'a, P: key::KeyParts> crate::cert::Preferences<'a, Key<P, key::UnspecifiedRole>> for ValidPrimaryKeyAmalgamation<'a, P> {} diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 988ef3fb..b640b8b5 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -48,6 +48,7 @@ mod builder; mod bindings; pub mod components; use components::{ + Amalgamation, ComponentBundle, PrimaryKeyBundle, UnfilteredKeyBundleIter, @@ -252,7 +253,7 @@ type UnknownBindings = ComponentBundles<Unknown>; /// on self signatures can be used to express preferences for /// algorithms and key management. Furthermore, the key holder's /// OpenPGP implementation can express its feature set. -pub trait Preferences<'a>: components::ValidAmalgamation<'a> { +pub trait Preferences<'a, C>: components::ValidAmalgamation<'a, C> { /// Returns symmetric algorithms that the key holder prefers. /// /// The algorithms are ordered according by the key holder's @@ -349,6 +350,7 @@ use super::*; /// ```rust /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; +/// # use openpgp::cert::components::Amalgamation; /// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// use openpgp::{Cert, cert::CertBuilder}; /// |