summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@sequoia-pgp.org>2024-02-27 13:07:44 +0100
committerNeal H. Walfield <neal@sequoia-pgp.org>2024-02-27 13:11:58 +0100
commitc6a60bf28d49a97362ee7a96008eb960839f0c6e (patch)
tree09afa8625bbd4947297d0abb376d4d0d690233d4
parented9bd709787d6cd6346ab63c966dbbc451d67925 (diff)
openpgp: Add generic forwards for Policy data structures.neal/policy-forwarders
- Implement `Policy` for `&dyn Policy`, `Box<T> where T: Policy`, `&StandardPolicy` and `&NullPolicy` by implementing a generic forwarder.
-rw-r--r--openpgp/NEWS5
-rw-r--r--openpgp/src/policy.rs118
2 files changed, 123 insertions, 0 deletions
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<T> 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<key::PublicParts>)
+ -> 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<T> Policy for Box<T>
+ where T: Policy
+{
+ fn signature(&self, sig: &Signature, sec: HashAlgoSecurity) -> Result<()> {
+ self.as_ref().signature(sig, sec)
+ }
+
+ fn key(&self, ka: &ValidErasedKeyAmalgamation<key::PublicParts>)
+ -> 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<key::PublicParts>)
+ -> 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<key::PublicParts>)
+ -> 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>(_: 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));
+ }
}