diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-01-02 12:44:47 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-01-02 13:32:02 +0100 |
commit | 2ea47f61ef6aca4097d7abba850dc4ba6a9018be (patch) | |
tree | a2b6c9527dc77ef32bd2dded88c051cbf5177539 /openpgp | |
parent | 984d3645cbb68437475316fda35a2726f5cc21c1 (diff) |
openpgp: Mark packet version enums as non-exhaustive.
- Fixes #405.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/cert/parser/mod.rs | 1 | ||||
-rw-r--r-- | openpgp/src/crypto/hash.rs | 1 | ||||
-rw-r--r-- | openpgp/src/packet/mod.rs | 72 | ||||
-rw-r--r-- | openpgp/src/packet/signature/mod.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/skesk.rs | 1 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 2 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 23 |
7 files changed, 97 insertions, 5 deletions
diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index 429f3c06..53a49beb 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -660,6 +660,7 @@ impl<'a, I: Iterator<Item=Packet>> CertParser<'a, I> { } } }, + Signature::__Nonexhaustive => unreachable!(), } } diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index 292776c2..f7decd49 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -282,6 +282,7 @@ impl Hash for Signature { fn hash(&self, hash: &mut Context) { match self { Signature::V4(sig) => sig.hash(hash), + Signature::__Nonexhaustive => unreachable!(), } } } diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index b05d8a2b..bd40059a 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -56,7 +56,7 @@ impl<'a> Deref for Packet { fn deref(&self) -> &Self::Target { match self { &Packet::Unknown(ref packet) => &packet.common, - &Packet::Signature(Signature::V4(ref packet)) => &packet.common, + &Packet::Signature(ref packet) => &packet.common, &Packet::OnePassSig(ref packet) => &packet.common, &Packet::PublicKey(ref packet) => &packet.common, &Packet::PublicSubkey(ref packet) => &packet.common, @@ -71,9 +71,10 @@ impl<'a> Deref for Packet { &Packet::PKESK(ref packet) => &packet.common, &Packet::SKESK(SKESK::V4(ref packet)) => &packet.common, &Packet::SKESK(SKESK::V5(ref packet)) => &packet.skesk4.common, + Packet::SKESK(SKESK::__Nonexhaustive) => unreachable!(), &Packet::SEIP(ref packet) => &packet.common, &Packet::MDC(ref packet) => &packet.common, - &Packet::AED(AED::V1(ref packet)) => &packet.common, + &Packet::AED(ref packet) => &packet.common, Packet::__Nonexhaustive => unreachable!(), } } @@ -83,8 +84,7 @@ impl<'a> DerefMut for Packet { fn deref_mut(&mut self) -> &mut Common { match self { &mut Packet::Unknown(ref mut packet) => &mut packet.common, - &mut Packet::Signature(Signature::V4(ref mut packet)) => - &mut packet.common, + &mut Packet::Signature(ref mut packet) => &mut packet.common, &mut Packet::OnePassSig(ref mut packet) => &mut packet.common, &mut Packet::PublicKey(ref mut packet) => &mut packet.common, &mut Packet::PublicSubkey(ref mut packet) => &mut packet.common, @@ -99,9 +99,10 @@ impl<'a> DerefMut for Packet { &mut Packet::PKESK(ref mut packet) => &mut packet.common, &mut Packet::SKESK(SKESK::V4(ref mut packet)) => &mut packet.common, &mut Packet::SKESK(SKESK::V5(ref mut packet)) => &mut packet.skesk4.common, + Packet::SKESK(SKESK::__Nonexhaustive) => unreachable!(), &mut Packet::SEIP(ref mut packet) => &mut packet.common, &mut Packet::MDC(ref mut packet) => &mut packet.common, - &mut Packet::AED(AED::V1(ref mut packet)) => &mut packet.common, + &mut Packet::AED(ref mut packet) => &mut packet.common, Packet::__Nonexhaustive => unreachable!(), } } @@ -301,10 +302,17 @@ fn packet_path_iter() { /// See [Section 5.2 of RFC 4880] for details. /// /// [Section 5.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.2 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum Signature { /// Signature packet version 4. V4(self::signature::Signature4), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl Signature { @@ -312,6 +320,7 @@ impl Signature { pub fn version(&self) -> u8 { match self { &Signature::V4(_) => 4, + Signature::__Nonexhaustive => unreachable!(), } } } @@ -329,6 +338,7 @@ impl Deref for Signature { fn deref(&self) -> &Self::Target { match self { Signature::V4(sig) => sig, + Signature::__Nonexhaustive => unreachable!(), } } } @@ -338,6 +348,7 @@ impl DerefMut for Signature { fn deref_mut(&mut self) -> &mut Self::Target { match self { Signature::V4(ref mut sig) => sig, + Signature::__Nonexhaustive => unreachable!(), } } } @@ -347,10 +358,17 @@ impl DerefMut for Signature { /// See [Section 5.4 of RFC 4880] for details. /// /// [Section 5.4 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.4 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum OnePassSig { /// OnePassSig packet version 3. V3(self::one_pass_sig::OnePassSig3), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl OnePassSig { @@ -358,6 +376,7 @@ impl OnePassSig { pub fn version(&self) -> u8 { match self { &OnePassSig::V3(_) => 3, + OnePassSig::__Nonexhaustive => unreachable!(), } } } @@ -375,6 +394,7 @@ impl Deref for OnePassSig { fn deref(&self) -> &Self::Target { match self { OnePassSig::V3(ops) => ops, + OnePassSig::__Nonexhaustive => unreachable!(), } } } @@ -384,6 +404,7 @@ impl DerefMut for OnePassSig { fn deref_mut(&mut self) -> &mut Self::Target { match self { OnePassSig::V3(ref mut ops) => ops, + OnePassSig::__Nonexhaustive => unreachable!(), } } } @@ -394,10 +415,17 @@ impl DerefMut for OnePassSig { /// [Section 5.1 of RFC 4880] for details. /// /// [Section 5.1 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.1 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum PKESK { /// PKESK packet version 3. V3(self::pkesk::PKESK3), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl PKESK { @@ -405,6 +433,7 @@ impl PKESK { pub fn version(&self) -> u8 { match self { PKESK::V3(_) => 3, + PKESK::__Nonexhaustive => unreachable!(), } } } @@ -422,6 +451,7 @@ impl Deref for PKESK { fn deref(&self) -> &Self::Target { match self { PKESK::V3(ref p) => p, + PKESK::__Nonexhaustive => unreachable!(), } } } @@ -431,6 +461,7 @@ impl DerefMut for PKESK { fn deref_mut(&mut self) -> &mut Self::Target { match self { PKESK::V3(ref mut p) => p, + PKESK::__Nonexhaustive => unreachable!(), } } } @@ -442,12 +473,19 @@ impl DerefMut for PKESK { /// 4880] for details. /// /// [Section 5.3 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.3 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum SKESK { /// SKESK packet version 4. V4(self::skesk::SKESK4), /// SKESK packet version 5. V5(self::skesk::SKESK5), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl SKESK { @@ -456,6 +494,7 @@ impl SKESK { match self { &SKESK::V4(_) => 4, &SKESK::V5(_) => 5, + SKESK::__Nonexhaustive => unreachable!(), } } } @@ -471,16 +510,24 @@ impl From<SKESK> for Packet { /// See [Section 5.5 of RFC 4880] for details. /// /// [Section 5.5 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.5 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum Key<P: key::KeyParts, R: key::KeyRole> { /// Key packet version 4. V4(self::key::Key4<P, R>), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl<P: key::KeyParts, R: key::KeyRole> fmt::Display for Key<P, R> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Key::V4(k) => k.fmt(f), + Key::__Nonexhaustive => unreachable!(), } } } @@ -490,6 +537,7 @@ impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> { pub fn version(&self) -> u8 { match self { Key::V4(_) => 4, + Key::__Nonexhaustive => unreachable!(), } } @@ -505,6 +553,8 @@ impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> { { match (self, b) { (Key::V4(a), Key::V4(b)) => a.public_cmp(b), + (Key::__Nonexhaustive, _) => unreachable!(), + (_, Key::__Nonexhaustive) => unreachable!(), } } } @@ -591,6 +641,7 @@ impl<P: key::KeyParts, R: key::KeyRole> Deref for Key<P, R> { fn deref(&self) -> &Self::Target { match self { Key::V4(ref p) => p, + Key::__Nonexhaustive => unreachable!(), } } } @@ -600,6 +651,7 @@ impl<P: key::KeyParts, R: key::KeyRole> DerefMut for Key<P, R> { fn deref_mut(&mut self) -> &mut Self::Target { match self { Key::V4(ref mut p) => p, + Key::__Nonexhaustive => unreachable!(), } } } @@ -657,10 +709,17 @@ impl DerefMut for SEIP { /// of RFC 4880bis] for details. /// /// [Section 5.16 of RFC 4880bis]: https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-05#section-5.16 +/// +/// Note: This enum cannot be exhaustively matched to allow future +/// extensions. #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub enum AED { /// AED packet version 1. V1(self::aed::AED1), + + /// This marks this enum as non-exhaustive. Do not use this + /// variant. + #[doc(hidden)] __Nonexhaustive, } impl AED { @@ -668,6 +727,7 @@ impl AED { pub fn version(&self) -> u8 { match self { AED::V1(_) => 1, + AED::__Nonexhaustive => unreachable!(), } } } @@ -685,6 +745,7 @@ impl Deref for AED { fn deref(&self) -> &Self::Target { match self { AED::V1(ref p) => p, + AED::__Nonexhaustive => unreachable!(), } } } @@ -694,6 +755,7 @@ impl DerefMut for AED { fn deref_mut(&mut self) -> &mut Self::Target { match self { AED::V1(ref mut p) => p, + AED::__Nonexhaustive => unreachable!(), } } } diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index e47e744c..3c549f71 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -308,6 +308,7 @@ impl From<Signature> for Builder { fn from(sig: Signature) -> Self { match sig { Signature::V4(sig) => sig.into(), + Signature::__Nonexhaustive => unreachable!(), } } } @@ -322,6 +323,7 @@ impl<'a> From<&'a Signature> for &'a Builder { fn from(sig: &'a Signature) -> Self { match sig { Signature::V4(ref sig) => sig.into(), + Signature::__Nonexhaustive => unreachable!(), } } } diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index b39c11f5..77d3cf4a 100644 --- a/openpgp/src/packet/skesk.rs +++ b/openpgp/src/packet/skesk.rs @@ -32,6 +32,7 @@ impl SKESK { match self { &SKESK::V4(ref s) => s.decrypt(password), &SKESK::V5(ref s) => s.decrypt(password), + SKESK::__Nonexhaustive => unreachable!(), } } } diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index d0123803..1c6bc4ec 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -1672,6 +1672,7 @@ impl<'a> Parse<'a, OnePassSig3> for OnePassSig3 { // // p => Err(Error::InvalidOperation( // format!("Not a OnePassSig::V3 packet: {:?}", p)).into()), + OnePassSig::__Nonexhaustive => unreachable!(), }) } } @@ -2543,6 +2544,7 @@ impl<'a> Parse<'a, PKESK3> for PKESK3 { // // p => Err(Error::InvalidOperation( // format!("Not a PKESKv3 packet: {:?}", p)).into()), + PKESK::__Nonexhaustive => unreachable!(), }) } } diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 86df736c..51cb8311 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -1133,12 +1133,14 @@ impl Serialize for Signature { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { &Signature::V4(ref s) => s.serialize(o), + Signature::__Nonexhaustive => unreachable!(), } } fn export(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { &Signature::V4(ref s) => s.export(o), + Signature::__Nonexhaustive => unreachable!(), } } } @@ -1147,24 +1149,28 @@ impl SerializeInto for Signature { fn serialized_len(&self) -> usize { match self { &Signature::V4(ref s) => s.serialized_len(), + Signature::__Nonexhaustive => unreachable!(), } } fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &Signature::V4(ref s) => s.serialize_into(buf), + Signature::__Nonexhaustive => unreachable!(), } } fn export_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &Signature::V4(ref s) => s.export_into(buf), + Signature::__Nonexhaustive => unreachable!(), } } fn export_to_vec(&self) -> Result<Vec<u8>> { match self { &Signature::V4(ref s) => s.export_to_vec(), + Signature::__Nonexhaustive => unreachable!(), } } } @@ -1267,6 +1273,7 @@ impl Serialize for OnePassSig { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { &OnePassSig::V3(ref s) => s.serialize(o), + OnePassSig::__Nonexhaustive => unreachable!(), } } } @@ -1275,12 +1282,14 @@ impl SerializeInto for OnePassSig { fn serialized_len(&self) -> usize { match self { &OnePassSig::V3(ref s) => s.serialized_len(), + OnePassSig::__Nonexhaustive => unreachable!(), } } fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &OnePassSig::V3(ref s) => s.serialize_into(buf), + OnePassSig::__Nonexhaustive => unreachable!(), } } } @@ -1323,6 +1332,7 @@ impl<P: key::KeyParts, R: key::KeyRole> Serialize for Key<P, R> { fn serialize(&self, o: &mut dyn io::Write) -> Result<()> { match self { &Key::V4(ref p) => p.serialize(o), + Key::__Nonexhaustive => unreachable!(), } } } @@ -1331,6 +1341,7 @@ impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> { fn net_len_key(&self, serialize_secrets: bool) -> usize { match self { &Key::V4(ref p) => p.net_len_key(serialize_secrets), + Key::__Nonexhaustive => unreachable!(), } } } @@ -1339,12 +1350,14 @@ impl<P: key::KeyParts, R: key::KeyRole> SerializeInto for Key<P, R> { fn serialized_len(&self) -> usize { match self { &Key::V4(ref p) => p.serialized_len(), + Key::__Nonexhaustive => unreachable!(), } } fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &Key::V4(ref p) => p.serialize_into(buf), + Key::__Nonexhaustive => unreachable!(), } } } @@ -1738,6 +1751,7 @@ impl Serialize for PKESK { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { &PKESK::V3(ref p) => p.serialize(o), + PKESK::__Nonexhaustive => unreachable!(), } } } @@ -1746,12 +1760,14 @@ impl SerializeInto for PKESK { fn serialized_len(&self) -> usize { match self { &PKESK::V3(ref p) => p.serialized_len(), + PKESK::__Nonexhaustive => unreachable!(), } } fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &PKESK::V3(ref p) => generic_serialize_into(p, buf), + PKESK::__Nonexhaustive => unreachable!(), } } } @@ -1791,6 +1807,7 @@ impl Serialize for SKESK { match self { &SKESK::V4(ref s) => s.serialize(o), &SKESK::V5(ref s) => s.serialize(o), + SKESK::__Nonexhaustive => unreachable!(), } } } @@ -1800,6 +1817,7 @@ impl NetLength for SKESK { match self { &SKESK::V4(ref s) => s.net_len(), &SKESK::V5(ref s) => s.net_len(), + SKESK::__Nonexhaustive => unreachable!(), } } } @@ -1809,6 +1827,7 @@ impl SerializeInto for SKESK { match self { &SKESK::V4(ref s) => s.serialized_len(), &SKESK::V5(ref s) => s.serialized_len(), + SKESK::__Nonexhaustive => unreachable!(), } } @@ -1816,6 +1835,7 @@ impl SerializeInto for SKESK { match self { &SKESK::V4(ref s) => generic_serialize_into(s, buf), &SKESK::V5(ref s) => generic_serialize_into(s, buf), + SKESK::__Nonexhaustive => unreachable!(), } } } @@ -1961,6 +1981,7 @@ impl Serialize for AED { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { &AED::V1(ref p) => p.serialize(o), + AED::__Nonexhaustive => unreachable!(), } } } @@ -1969,12 +1990,14 @@ impl SerializeInto for AED { fn serialized_len(&self) -> usize { match self { &AED::V1(ref p) => p.serialized_len(), + AED::__Nonexhaustive => unreachable!(), } } fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> { match self { &AED::V1(ref p) => p.serialize_into(buf), + AED::__Nonexhaustive => unreachable!(), } } } |