From a95b701503dee6557ca57eb9e08df9072aac153c Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 15 Nov 2023 13:34:52 +0100 Subject: openpgp: Implement downcasting of Key types aliasing with packets. - Fixes #878. --- openpgp/src/packet/any.rs | 60 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) (limited to 'openpgp') diff --git a/openpgp/src/packet/any.rs b/openpgp/src/packet/any.rs index 31994e05..5b9f59ae 100644 --- a/openpgp/src/packet/any.rs +++ b/openpgp/src/packet/any.rs @@ -5,12 +5,8 @@ use crate::packet::{ Unknown, Signature, OnePassSig, - key::{ - PublicKey, - PublicSubkey, - SecretKey, - SecretSubkey, - }, + Key, + key, Marker, Trust, UserID, @@ -75,25 +71,28 @@ pub trait Any: crate::seal::Sealed { macro_rules! impl_downcast_for { ($typ: tt) => { + impl_downcast_for!($typ => $typ); + }; + ($($typ: tt)|* => $subtyp: ty) => { #[allow(deprecated)] - impl Any<$typ> for Packet { - fn downcast(self) -> std::result::Result<$typ, Packet> { + impl Any<$subtyp> for Packet { + fn downcast(self) -> std::result::Result<$subtyp, Packet> { match self { - Packet::$typ(v) => Ok(v), + $(Packet::$typ(v) => Ok(v.into()),)* p => Err(p), } } - fn downcast_ref(&self) -> Option<&$typ> { + fn downcast_ref(&self) -> Option<&$subtyp> { match self { - Packet::$typ(v) => Some(v), + $(Packet::$typ(v) => Some(v.into()),)* _ => None, } } - fn downcast_mut(&mut self) -> Option<&mut $typ> { + fn downcast_mut(&mut self) -> Option<&mut $subtyp> { match self { - Packet::$typ(v) => Some(v), + $(Packet::$typ(v) => Some(v.into()),)* _ => None, } } @@ -113,6 +112,11 @@ macro_rules! impl_downcasts { fn check_exhaustion(p: Packet) { match p { $(Packet::$typ(_) => (),)* + // The downcasts to Key are handled below. + Packet::PublicKey(_) => (), + Packet::PublicSubkey(_) => (), + Packet::SecretKey(_) => (), + Packet::SecretSubkey(_) => (), } } } @@ -122,10 +126,6 @@ impl_downcasts!( Unknown, Signature, OnePassSig, - PublicKey, - PublicSubkey, - SecretKey, - SecretSubkey, Marker, Trust, UserID, @@ -139,6 +139,32 @@ impl_downcasts!( AED, ); +// We ow selectively implement downcasts for the key types that alias +// with the packet type. + +// 1. PublicParts, any role. +impl_downcast_for!(PublicKey | SecretKey + => Key); +impl_downcast_for!(PublicSubkey | SecretSubkey + => Key); +impl_downcast_for!(PublicKey | PublicSubkey | SecretKey | SecretSubkey + => Key); + +// 2. SecretParts, any role. +impl_downcast_for!(SecretKey + => Key); +impl_downcast_for!(SecretSubkey + => Key); +impl_downcast_for!(SecretKey | SecretSubkey + => Key); + +// 3. UnspecifiedParts, any role. +impl_downcast_for!(PublicKey | SecretKey + => Key); +impl_downcast_for!(PublicSubkey | SecretSubkey + => Key); +impl_downcast_for!(PublicKey | PublicSubkey | SecretKey | SecretSubkey + => Key); #[cfg(test)] mod test { -- cgit v1.2.3