diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-02-17 19:12:46 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-02-17 19:15:11 +0100 |
commit | c0675768802d825043931678a89ae3cde3a33149 (patch) | |
tree | 87b1b0b39ed93c09827eb1f3d1facd5e80241ef3 | |
parent | 58ad7f331cb8c3d69c328e1b14604fac57bce5f7 (diff) |
openpgp: Move secret predicates.
-rw-r--r-- | openpgp/src/cert/components.rs | 12 | ||||
-rw-r--r-- | openpgp/src/cert/key_amalgamation.rs | 43 | ||||
-rw-r--r-- | openpgp/src/packet/key/mod.rs | 33 | ||||
-rw-r--r-- | openpgp/src/serialize/cert.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 4 |
5 files changed, 34 insertions, 62 deletions
diff --git a/openpgp/src/cert/components.rs b/openpgp/src/cert/components.rs index d52bfc37..87eaa4c6 100644 --- a/openpgp/src/cert/components.rs +++ b/openpgp/src/cert/components.rs @@ -47,18 +47,6 @@ pub use super::keyiter::{ /// signatures. pub type KeyBundle<KeyPart, KeyRole> = ComponentBundle<Key<KeyPart, KeyRole>>; -impl<K: key::KeyParts, R: key::KeyRole> KeyBundle<K, R> -{ - /// Gets the key packet's `SecretKeyMaterial`. - /// - /// Note: The key module installs conversion functions on - /// KeyBundle. They need to access the key's secret. - pub(crate) fn secret(&self) - -> Option<&crate::packet::key::SecretKeyMaterial> { - self.key().secret() - } -} - /// A primary key and any associated signatures. pub(crate) type PrimaryKeyBundle<KeyPart> = KeyBundle<KeyPart, key::PrimaryRole>; diff --git a/openpgp/src/cert/key_amalgamation.rs b/openpgp/src/cert/key_amalgamation.rs index 7642638b..de1168e2 100644 --- a/openpgp/src/cert/key_amalgamation.rs +++ b/openpgp/src/cert/key_amalgamation.rs @@ -13,7 +13,6 @@ use crate::{ Error, packet::key, packet::Key, - packet::key::SecretKeyMaterial, packet::key::KeyParts, packet::Signature, policy::Policy, @@ -94,27 +93,6 @@ impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> { self.cert } - /// Returns whether the key contains secret key material. - pub fn has_secret(&self) -> bool - { - self.key().secret().is_some() - } - - /// Returns whether the key contains unencrypted secret key - /// material. - pub fn has_unencrypted_secret(&self) -> bool - { - if let Some(secret) = self.key().secret() { - if let SecretKeyMaterial::Unencrypted { .. } = secret { - true - } else { - false - } - } else { - false - } - } - /// Returns this key's bundle. pub fn bundle(&self) -> &'a KeyBundle<P, key::UnspecifiedRole> { match self { @@ -415,27 +393,6 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyAmalgamation<'a, P> { } } - /// Returns whether the key contains secret key material. - pub fn has_secret(&self) -> bool - { - self.key().secret().is_some() - } - - /// Returns whether the key contains unencrypted secret key - /// material. - pub fn has_unencrypted_secret(&self) -> bool - { - if let Some(secret) = self.key().secret() { - if let SecretKeyMaterial::Unencrypted { .. } = secret { - true - } else { - false - } - } else { - false - } - } - // NOTE: If you add a method to ValidKeyAmalgamation that takes // ownership of self, then don't forget to write a forwarder for // it for ValidPrimaryKeyAmalgamation. diff --git a/openpgp/src/packet/key/mod.rs b/openpgp/src/packet/key/mod.rs index 7a8e173f..a548f7eb 100644 --- a/openpgp/src/packet/key/mod.rs +++ b/openpgp/src/packet/key/mod.rs @@ -495,7 +495,7 @@ macro_rules! create_part_conversions { { type Error = failure::Error; fn try_from(p: &$Key<$($l, )* $from_parts, $($g, )* >) -> Result<Self> { - if p.secret().is_some() { + if p.has_secret() { Ok(convert_ref!(p)) } else { Err(Error::InvalidArgument("No secret key".into()) @@ -551,7 +551,7 @@ macro_rules! create_part_conversions { /// Changes the key's parts tag to `SecretParts`. pub fn mark_parts_secret(self) -> Result<$Key<$($l, )* SecretParts, $($g, )*>> { - if self.secret().is_some() { + if self.has_secret() { Ok(convert!(self)) } else { Err(Error::InvalidArgument("No secret key".into()).into()) @@ -561,7 +561,7 @@ macro_rules! create_part_conversions { /// Changes the key's parts tag to `SecretParts`. pub fn mark_parts_secret_ref(&self) -> Result<&$Key<$($l, )* SecretParts, $($g, )*>> { - if self.secret().is_some() { + if self.has_secret() { Ok(convert_ref!(self)) } else { Err(Error::InvalidArgument("No secret key".into()).into()) @@ -773,6 +773,13 @@ macro_rules! create_conversions { create_conversions!(Key); create_conversions!(Key4); + +impl<K: key::KeyParts, R: key::KeyRole> KeyBundle<K, R> +{ + fn has_secret(&self) -> bool { + self.key().secret.is_some() + } +} create_conversions!(KeyBundle); create_part_conversions!(KeyAmalgamation<'a;> where); @@ -1299,6 +1306,26 @@ impl<P, R> Key4<P, R> ::std::mem::replace(&mut self.mpis, mpis) } + /// Returns whether the key contains secret key material. + pub fn has_secret(&self) -> bool { + self.secret.is_some() + } + + /// Returns whether the key contains unencrypted secret key + /// material. + pub fn has_unencrypted_secret(&self) -> bool + { + if let Some(secret) = &self.secret { + if let SecretKeyMaterial::Unencrypted { .. } = secret { + true + } else { + false + } + } else { + false + } + } + /// Gets the key packet's `SecretKeyMaterial`. pub fn secret(&self) -> Option<&SecretKeyMaterial> { self.secret.as_ref() diff --git a/openpgp/src/serialize/cert.rs b/openpgp/src/serialize/cert.rs index c19c3b7b..11b2341d 100644 --- a/openpgp/src/serialize/cert.rs +++ b/openpgp/src/serialize/cert.rs @@ -383,7 +383,7 @@ impl<'a> TSK<'a> { |o: &mut dyn std::io::Write, key: &'a key::UnspecifiedSecret, tag_public, tag_secret| { - let tag = if key.secret().is_some() + let tag = if key.has_secret() && self.filter.as_ref().map(|f| f(key)).unwrap_or(true) { tag_secret } else { @@ -540,7 +540,7 @@ impl<'a> SerializeInto for TSK<'a> { let serialized_len_key = |key: &'a key::UnspecifiedSecret, tag_public, tag_secret| { - let tag = if key.secret().is_some() + let tag = if key.has_secret() && self.filter.as_ref().map(|f| f(key)).unwrap_or(true) { tag_secret } else { diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 2222fca4..172c4d55 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -1398,7 +1398,7 @@ impl<P, R> Key4<P, R> pub(crate) // For tests in key. fn serialize_key(&self, o: &mut dyn io::Write, serialize_secrets: bool) -> Result<()> { - let have_secret_key = self.secret().is_some() && serialize_secrets; + let have_secret_key = self.has_secret() && serialize_secrets; write_byte(o, 4)?; // Version. write_be_u32(o, Timestamp::try_from(self.creation_time())?.into())?; @@ -1436,7 +1436,7 @@ impl<P, R> Key4<P, R> } fn net_len_key(&self, serialize_secrets: bool) -> usize { - let have_secret_key = self.secret().is_some() && serialize_secrets; + let have_secret_key = self.has_secret() && serialize_secrets; 1 // Version. + 4 // Creation time. |