summaryrefslogtreecommitdiffstats
path: root/openpgp/src/keyid.rs
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2024-01-12 17:20:26 +0100
committerNeal H. Walfield <neal@sequoia-pgp.org>2024-01-25 09:28:28 +0100
commit049b0590040ebf08338b05f9483663dd280c1ca5 (patch)
tree033eddfcf3e3d98c3ad1999cbe3eaa03252d7771 /openpgp/src/keyid.rs
parenta55cd9025faf9fea54b63d168a5bec555c95ba7f (diff)
openpgp: Reject short key IDs.
- When parsing a key ID string, reject short key IDs. - Note: we can't reject short key IDs in `KeyID::from_bytes`, because that function in infallible. But, that function does return `KeyID::Invalid` when presented with a short key ID. - Fixes #388.
Diffstat (limited to 'openpgp/src/keyid.rs')
-rw-r--r--openpgp/src/keyid.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs
index 2d880706..1dc08fe8 100644
--- a/openpgp/src/keyid.rs
+++ b/openpgp/src/keyid.rs
@@ -107,6 +107,8 @@ impl std::str::FromStr for KeyID {
// A KeyID is exactly 8 bytes long.
if bytes.len() == 8 {
Ok(KeyID::from_bytes(&bytes[..]))
+ } else if bytes.len() == 4 {
+ Err(Error::ShortKeyID(s.to_string()).into())
} else {
// Maybe a fingerprint was given. Try to parse it and
// convert it to a KeyID.
@@ -456,8 +458,19 @@ mod test {
"GB3751F1587DAEF1".parse::<KeyID>().unwrap_err();
"EFB3751F1587DAEF1".parse::<KeyID>().unwrap_err();
"%FB3751F1587DAEF1".parse::<KeyID>().unwrap_err();
- assert_match!(KeyID::Invalid(_) = "587DAEF1".parse().unwrap());
- assert_match!(KeyID::Invalid(_) = "0x587DAEF1".parse().unwrap());
+ }
+
+ #[test]
+ fn from_hex_short_keyid() {
+ for s in &[ "FB3751F1", "0xFB3751F1", "fb3751f1", "0xfb3751f1" ] {
+ match s.parse::<KeyID>() {
+ Ok(_) => panic!("Failed to reject short Key ID."),
+ Err(err) => {
+ let err = err.downcast_ref::<Error>().unwrap();
+ assert!(matches!(err, Error::ShortKeyID(_)));
+ }
+ }
+ }
}
#[test]