summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2020-02-27 09:50:13 +0100
committerNeal H. Walfield <neal@pep.foundation>2020-02-27 09:50:48 +0100
commit95d986f75418748c91b85911d922f605fb2facd0 (patch)
treee0d6c36c0169461be6be3f5331987aee3de27be8
parenta5bfdd6c2f560b91cfdea3e89102d16e441fd4e2 (diff)
openpgp: Make KeyIter and ValidKeyIter generic over the key's role.
-rw-r--r--openpgp-ffi/src/cert.rs6
-rw-r--r--openpgp/src/cert/keyiter.rs65
-rw-r--r--openpgp/src/cert/mod.rs4
3 files changed, 53 insertions, 22 deletions
diff --git a/openpgp-ffi/src/cert.rs b/openpgp-ffi/src/cert.rs
index 81b19123..a1c42337 100644
--- a/openpgp-ffi/src/cert.rs
+++ b/openpgp-ffi/src/cert.rs
@@ -435,7 +435,8 @@ pub extern "C" fn pgp_user_id_bundle_iter_next<'a>(
/// Wraps a KeyIter for export via the FFI.
pub struct KeyIterWrapper<'a> {
pub(crate) // For serialize.rs.
- iter: KeyIter<'a, openpgp::packet::key::PublicParts>,
+ iter: KeyIter<'a, openpgp::packet::key::PublicParts,
+ openpgp::packet::key::UnspecifiedRole>,
// Whether next has been called.
next_called: bool,
}
@@ -555,7 +556,8 @@ pub extern "C" fn pgp_cert_key_iter_next<'a>(
/// Wraps a ValidKeyIter for export via the FFI.
pub struct ValidKeyIterWrapper<'a> {
pub(crate) // For serialize.rs.
- iter: ValidKeyIter<'a, openpgp::packet::key::PublicParts>,
+ iter: ValidKeyIter<'a, openpgp::packet::key::PublicParts,
+ openpgp::packet::key::UnspecifiedRole>,
// Whether next has been called.
next_called: bool,
}
diff --git a/openpgp/src/cert/keyiter.rs b/openpgp/src/cert/keyiter.rs
index c88a9e97..eee2899e 100644
--- a/openpgp/src/cert/keyiter.rs
+++ b/openpgp/src/cert/keyiter.rs
@@ -28,7 +28,10 @@ use crate::{
/// include secret key material. Of course, since `KeyIter`
/// implements `Iterator`, it is possible to use `Iterator::filter` to
/// implement custom filters.
-pub struct KeyIter<'a, P: key::KeyParts> {
+pub struct KeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
+{
// This is an option to make it easier to create an empty KeyIter.
cert: Option<&'a Cert>,
primary: bool,
@@ -47,9 +50,12 @@ pub struct KeyIter<'a, P: key::KeyParts> {
key_handles: Vec<KeyHandle>,
_p: std::marker::PhantomData<P>,
+ _r: std::marker::PhantomData<R>,
}
-impl<'a, P: key::KeyParts> fmt::Debug for KeyIter<'a, P>
+impl<'a, P, R> fmt::Debug for KeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("KeyIter")
@@ -66,7 +72,7 @@ impl<'a, P: key::KeyParts> fmt::Debug for KeyIter<'a, P>
// implementation for Key<SecretParts, _> below.
macro_rules! impl_iterator {
($parts:path) => {
- impl<'a> Iterator for KeyIter<'a, $parts>
+ impl<'a> Iterator for KeyIter<'a, $parts, key::UnspecifiedRole>
{
type Item = ErasedKeyAmalgamation<'a, $parts>;
@@ -79,7 +85,7 @@ macro_rules! impl_iterator {
impl_iterator!(key::PublicParts);
impl_iterator!(key::UnspecifiedParts);
-impl<'a> Iterator for KeyIter<'a, key::SecretParts> {
+impl<'a> Iterator for KeyIter<'a, key::SecretParts, key::UnspecifiedRole> {
type Item = ErasedKeyAmalgamation<'a, key::SecretParts>;
fn next(&mut self) -> Option<Self::Item> {
@@ -87,7 +93,10 @@ impl<'a> Iterator for KeyIter<'a, key::SecretParts> {
}
}
-impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P> {
+impl<'a, P, R> KeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
+{
fn next_common(&mut self) -> Option<ErasedKeyAmalgamation<'a, key::PublicParts>>
{
tracer!(false, "KeyIter::next", 0);
@@ -161,7 +170,9 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P> {
}
}
-impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
+impl<'a, P, R> KeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
{
/// Returns a new `KeyIter` instance.
pub(crate) fn new(cert: &'a Cert) -> Self where Self: 'a {
@@ -176,11 +187,12 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
key_handles: Vec::with_capacity(0),
_p: std::marker::PhantomData,
+ _r: std::marker::PhantomData,
}
}
/// Changes the filter to only return keys with secret key material.
- pub fn secret(self) -> KeyIter<'a, key::SecretParts> {
+ pub fn secret(self) -> KeyIter<'a, key::SecretParts, R> {
KeyIter {
cert: self.cert,
primary: self.primary,
@@ -192,12 +204,13 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
key_handles: self.key_handles,
_p: std::marker::PhantomData,
+ _r: std::marker::PhantomData,
}
}
/// Changes the filter to only return keys with unencrypted secret
/// key material.
- pub fn unencrypted_secret(self) -> KeyIter<'a, key::SecretParts> {
+ pub fn unencrypted_secret(self) -> KeyIter<'a, key::SecretParts, R> {
KeyIter {
cert: self.cert,
primary: self.primary,
@@ -209,6 +222,7 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
key_handles: self.key_handles,
_p: std::marker::PhantomData,
+ _r: std::marker::PhantomData,
}
}
@@ -373,7 +387,7 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
/// [signature expirations]: https://tools.ietf.org/html/rfc4880#section-5.2.3.10
/// [This discussion]: https://crypto.stackexchange.com/a/12138
pub fn with_policy<T>(self, policy: &'a dyn Policy, time: T)
- -> ValidKeyIter<'a, P>
+ -> ValidKeyIter<'a, P, R>
where T: Into<Option<SystemTime>>
{
ValidKeyIter {
@@ -393,6 +407,7 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
revoked: None,
_p: self._p,
+ _r: self._r,
}
}
@@ -455,7 +470,10 @@ impl<'a, P: 'a + key::KeyParts> KeyIter<'a, P>
/// `ValidKeyIter` follows the builder pattern. There is no need to
/// explicitly finalize it, however: it already implements the
/// `Iterator` trait.
-pub struct ValidKeyIter<'a, P: key::KeyParts> {
+pub struct ValidKeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
+{
// This is an option to make it easier to create an empty ValidKeyIter.
cert: Option<&'a Cert>,
primary: bool,
@@ -490,9 +508,12 @@ pub struct ValidKeyIter<'a, P: key::KeyParts> {
revoked: Option<bool>,
_p: std::marker::PhantomData<P>,
+ _r: std::marker::PhantomData<R>,
}
-impl<'a, P: key::KeyParts> fmt::Debug for ValidKeyIter<'a, P>
+impl<'a, P, R> fmt::Debug for ValidKeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ValidKeyIter")
@@ -514,7 +535,7 @@ impl<'a, P: key::KeyParts> fmt::Debug for ValidKeyIter<'a, P>
// implementation for Key<SecretParts, _> below.
macro_rules! impl_valid_key_iterator {
($parts:path) => {
- impl<'a> Iterator for ValidKeyIter<'a, $parts>
+ impl<'a> Iterator for ValidKeyIter<'a, $parts, key::UnspecifiedRole>
{
type Item = ValidErasedKeyAmalgamation<'a, $parts>;
@@ -527,7 +548,7 @@ macro_rules! impl_valid_key_iterator {
impl_valid_key_iterator!(key::PublicParts);
impl_valid_key_iterator!(key::UnspecifiedParts);
-impl<'a> Iterator for ValidKeyIter<'a, key::SecretParts>
+impl<'a> Iterator for ValidKeyIter<'a, key::SecretParts, key::UnspecifiedRole>
{
type Item = ValidErasedKeyAmalgamation<'a, key::SecretParts>;
@@ -536,8 +557,12 @@ impl<'a> Iterator for ValidKeyIter<'a, key::SecretParts>
}
}
-impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P> {
- fn next_common(&mut self) -> Option<ValidErasedKeyAmalgamation<'a, key::PublicParts>>
+impl<'a, P, R> ValidKeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
+{
+ fn next_common(&mut self)
+ -> Option<ValidErasedKeyAmalgamation<'a, key::PublicParts>>
{
tracer!(false, "ValidKeyIter::next", 0);
t!("ValidKeyIter: {:?}", self);
@@ -667,7 +692,9 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P> {
}
}
-impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P>
+impl<'a, P, R> ValidKeyIter<'a, P, R>
+ where P: key::KeyParts,
+ R: key::KeyRole,
{
/// Returns keys that have the at least one of the flags specified
/// in `flags`.
@@ -808,7 +835,7 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P>
}
/// Changes the filter to only return keys with secret key material.
- pub fn secret(self) -> ValidKeyIter<'a, key::SecretParts> {
+ pub fn secret(self) -> ValidKeyIter<'a, key::SecretParts, R> {
ValidKeyIter {
cert: self.cert,
primary: self.primary,
@@ -826,12 +853,13 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P>
revoked: self.revoked,
_p: std::marker::PhantomData,
+ _r: std::marker::PhantomData,
}
}
/// Changes the filter to only return keys with unencrypted secret
/// key material.
- pub fn unencrypted_secret(self) -> ValidKeyIter<'a, key::SecretParts> {
+ pub fn unencrypted_secret(self) -> ValidKeyIter<'a, key::SecretParts, R> {
ValidKeyIter {
cert: self.cert,
primary: self.primary,
@@ -849,6 +877,7 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyIter<'a, P>
revoked: self.revoked,
_p: std::marker::PhantomData,
+ _r: std::marker::PhantomData,
}
}
diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs
index 80f2311f..207d8594 100644
--- a/openpgp/src/cert/mod.rs
+++ b/openpgp/src/cert/mod.rs
@@ -728,7 +728,7 @@ impl Cert {
///
/// That is, this returns an iterator over the primary key and any
/// subkeys.
- pub fn keys(&self) -> KeyIter<key::PublicParts>
+ pub fn keys(&self) -> KeyIter<key::PublicParts, key::UnspecifiedRole>
{
KeyIter::new(self)
}
@@ -1481,7 +1481,7 @@ impl<'a> CertAmalgamation<'a> {
///
/// That is, this returns an iterator over the primary key and any
/// subkeys.
- pub fn keys(&self) -> ValidKeyIter<key::PublicParts> {
+ pub fn keys(&self) -> ValidKeyIter<key::PublicParts, key::UnspecifiedRole> {
self.cert.keys().with_policy(self.policy, self.time)
}