From c6a60bf28d49a97362ee7a96008eb960839f0c6e Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Tue, 27 Feb 2024 13:07:44 +0100 Subject: openpgp: Add generic forwards for Policy data structures. - Implement `Policy` for `&dyn Policy`, `Box where T: Policy`, `&StandardPolicy` and `&NullPolicy` by implementing a generic forwarder. --- openpgp/NEWS | 5 +++ openpgp/src/policy.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/openpgp/NEWS b/openpgp/NEWS index 3524af4b..1889abf8 100644 --- a/openpgp/NEWS +++ b/openpgp/NEWS @@ -3,6 +3,11 @@ #+TITLE: sequoia-openpgp NEWS – history of user-visible changes #+STARTUP: content hidestars +* Changes in 1.20.0 +** Notable fixes +** New functionality + - Implemented Policy for &dyn Policy, Box where T: Policy, + &StandardPolicy and &NullPolicy. * Changes in 1.19.0 ** Notable fixes - Key4::import_secret_cv25519 will now clamp some bits of the given diff --git a/openpgp/src/policy.rs b/openpgp/src/policy.rs index 9ea4fcb2..1e641356 100644 --- a/openpgp/src/policy.rs +++ b/openpgp/src/policy.rs @@ -153,6 +153,56 @@ pub trait Policy : fmt::Debug + Send + Sync { } } +impl Policy for &dyn Policy { + fn signature(&self, sig: &Signature, sec: HashAlgoSecurity) -> Result<()> { + (*self).signature(sig, sec) + } + + fn key(&self, ka: &ValidErasedKeyAmalgamation) + -> Result<()> + { + (*self).key(ka) + } + + fn symmetric_algorithm(&self, algo: SymmetricAlgorithm) -> Result<()> { + (*self).symmetric_algorithm(algo) + } + + fn aead_algorithm(&self, algo: AEADAlgorithm) -> Result<()> { + (*self).aead_algorithm(algo) + } + + fn packet(&self, packet: &Packet) -> Result<()> { + (*self).packet(packet) + } +} + +impl Policy for Box + where T: Policy +{ + fn signature(&self, sig: &Signature, sec: HashAlgoSecurity) -> Result<()> { + self.as_ref().signature(sig, sec) + } + + fn key(&self, ka: &ValidErasedKeyAmalgamation) + -> Result<()> + { + self.as_ref().key(ka) + } + + fn symmetric_algorithm(&self, algo: SymmetricAlgorithm) -> Result<()> { + self.as_ref().symmetric_algorithm(algo) + } + + fn aead_algorithm(&self, algo: AEADAlgorithm) -> Result<()> { + self.as_ref().aead_algorithm(algo) + } + + fn packet(&self, packet: &Packet) -> Result<()> { + self.as_ref().packet(packet) + } +} + /// Whether the signed data requires a hash algorithm with collision /// resistance. /// @@ -1666,6 +1716,30 @@ impl<'a> Policy for StandardPolicy<'a> { } } +impl<'a> Policy for &StandardPolicy<'a> { + fn signature(&self, sig: &Signature, sec: HashAlgoSecurity) -> Result<()> { + (*self).signature(sig, sec) + } + + fn key(&self, ka: &ValidErasedKeyAmalgamation) + -> Result<()> + { + (*self).key(ka) + } + + fn symmetric_algorithm(&self, algo: SymmetricAlgorithm) -> Result<()> { + (*self).symmetric_algorithm(algo) + } + + fn aead_algorithm(&self, algo: AEADAlgorithm) -> Result<()> { + (*self).aead_algorithm(algo) + } + + fn packet(&self, packet: &Packet) -> Result<()> { + (*self).packet(packet) + } +} + /// Asymmetric encryption algorithms. /// /// This type is for refining the [`StandardPolicy`] with respect to @@ -1848,6 +1922,30 @@ impl Policy for NullPolicy { } +impl Policy for &NullPolicy { + fn signature(&self, sig: &Signature, sec: HashAlgoSecurity) -> Result<()> { + (*self).signature(sig, sec) + } + + fn key(&self, ka: &ValidErasedKeyAmalgamation) + -> Result<()> + { + (*self).key(ka) + } + + fn symmetric_algorithm(&self, algo: SymmetricAlgorithm) -> Result<()> { + (*self).symmetric_algorithm(algo) + } + + fn aead_algorithm(&self, algo: AEADAlgorithm) -> Result<()> { + (*self).aead_algorithm(algo) + } + + fn packet(&self, packet: &Packet) -> Result<()> { + (*self).packet(packet) + } +} + #[cfg(test)] mod test { use std::io::Read; @@ -3181,4 +3279,24 @@ mod test { assert_eq!(p.packet_tag_cutoff(Tag::Signature), Some(Timestamp::Y2007M2.into())); } + + #[test] + fn policy_blanket_impls() { + fn f(_: T) where T: Policy { + } + + let p = StandardPolicy::new(); + f(&p); + f(Box::new(&p)); + f(Box::new(p)); + + let p = NullPolicy::new(); + f(&p); + f(Box::new(&p)); + f(Box::new(p)); + + let p: &dyn Policy = &StandardPolicy::new(); + f(p); + f(Box::new(p)); + } } -- cgit v1.2.3