openpgp: Simplify key conversions in key amalgamations.

  - The new key conversion functions allow conversion into the target
    key type without the need to constrain the type P.

  - Remove KeyAmalgamation::generic_key.  This was a workaround we
    used to reduce the functions requiring said constraints on P.
parent d22960fd
......@@ -38,9 +38,7 @@ pub struct KeyAmalgamation<'a, P: key::KeyParts> {
bundle: KeyAmalgamationBundle<'a, P>,
}
impl<'a, P: key::KeyParts> Deref for KeyAmalgamation<'a, P>
where &'a Key<P, key::UnspecifiedRole>: From<&'a key::PublicKey>
{
impl<'a, P: key::KeyParts> Deref for KeyAmalgamation<'a, P> {
type Target = Key<P, key::UnspecifiedRole>;
fn deref(&self) -> &Self::Target {
......@@ -151,25 +149,16 @@ impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> {
}
/// Returns the key.
pub fn key(&self) -> &'a Key<P, key::UnspecifiedRole>
where &'a Key<P, key::UnspecifiedRole>: From<&'a key::PublicKey>
{
match self {
KeyAmalgamation { bundle: KeyAmalgamationBundle::Primary(), .. } =>
self.cert.primary.key().into(),
KeyAmalgamation { bundle: KeyAmalgamationBundle::Subordinate(bundle), .. } =>
bundle.key().into(),
}
}
/// Returns the key, but without conversion to P.
fn generic_key(&self)
-> &'a Key<key::UnspecifiedParts, key::UnspecifiedRole> {
pub fn key(&self) -> &'a Key<P, key::UnspecifiedRole> {
match self {
KeyAmalgamation { bundle: KeyAmalgamationBundle::Primary(), .. } =>
self.cert.primary.key().into(),
P::convert_key_ref(self.cert.primary.key().into())
.expect("secret key amalgamations contain secret keys"),
KeyAmalgamation { bundle: KeyAmalgamationBundle::Subordinate(bundle), .. } =>
bundle.key().mark_parts_unspecified_ref().into(),
P::convert_key_ref(bundle.key()
.mark_parts_unspecified_ref()
.mark_role_unspecified_ref())
.expect("secret key amalgamations contain secret keys"),
}
}
......@@ -182,14 +171,14 @@ impl<'a, P: 'a + key::KeyParts> KeyAmalgamation<'a, P> {
/// Returns whether the key contains secret key material.
pub fn has_secret(&self) -> bool
{
self.generic_key().secret().is_some()
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.generic_key().secret() {
if let Some(secret) = self.key().secret() {
if let SecretKeyMaterial::Unencrypted { .. } = secret {
true
} else {
......@@ -287,9 +276,7 @@ impl<'a, P> From<PrimaryKeyAmalgamation<'a, P>>
}
}
impl<'a, P: key::KeyParts> Deref for PrimaryKeyAmalgamation<'a, P>
where &'a Key<P, key::PrimaryRole>: From<&'a key::PublicKey>
{
impl<'a, P: key::KeyParts> Deref for PrimaryKeyAmalgamation<'a, P> {
type Target = KeyAmalgamation<'a, P>;
fn deref(&self) -> &Self::Target {
......@@ -315,10 +302,7 @@ impl<'a, P: key::KeyParts> PrimaryKeyAmalgamation<'a, P> {
}
/// Returns the key.
pub fn key(&self) -> &'a Key<P, key::PrimaryRole>
where &'a Key<P, key::UnspecifiedRole>:
From<&'a Key<key::PublicParts, key::PrimaryRole>>
{
pub fn key(&self) -> &'a Key<P, key::PrimaryRole> {
self.a.key().into()
}
......@@ -526,7 +510,7 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyAmalgamation<'a, P> {
}
};
if let Some(sig) = sig {
sig.key_alive(self.generic_key(), self.time())
sig.key_alive(self.key(), self.time())
} else {
// There is no key expiration time on the binding
// signature. This key does not expire.
......@@ -537,14 +521,14 @@ impl<'a, P: 'a + key::KeyParts> ValidKeyAmalgamation<'a, P> {
/// Returns whether the key contains secret key material.
pub fn has_secret(&self) -> bool
{
self.generic_key().secret().is_some()
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.generic_key().secret() {
if let Some(secret) = self.key().secret() {
if let SecretKeyMaterial::Unencrypted { .. } = secret {
true
} else {
......@@ -578,9 +562,7 @@ impl<'a, P> From<ValidPrimaryKeyAmalgamation<'a, P>>
}
}
impl<'a, P: key::KeyParts> Deref for ValidPrimaryKeyAmalgamation<'a, P>
where &'a Key<P, key::PrimaryRole>: From<&'a key::PublicKey>
{
impl<'a, P: key::KeyParts> Deref for ValidPrimaryKeyAmalgamation<'a, P> {
type Target = ValidKeyAmalgamation<'a, P>;
fn deref(&self) -> &Self::Target {
......@@ -610,10 +592,7 @@ impl<'a, P: key::KeyParts> ValidPrimaryKeyAmalgamation<'a, P> {
}
/// Returns the key.
pub fn key(&self) -> &'a Key<P, key::PrimaryRole>
where &'a Key<P, key::UnspecifiedRole>:
From<&'a Key<key::PublicParts, key::PrimaryRole>>
{
pub fn key(&self) -> &'a Key<P, key::PrimaryRole> {
self.a.key().into()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment