summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2023-05-12 15:40:02 +0200
committerNeal H. Walfield <neal@pep.foundation>2023-05-12 15:40:02 +0200
commitdbf22d26d368e9dbe3316d25b0c942de655d3c2e (patch)
tree27d5f45322715e772a39d9944ab8597c1c53d97a
parentd76a5abbb8688500874be0e6b8404bb24c50a80d (diff)
openpgp: Clear the fingerprint cache when the key is updated.
- When a `Key4` is changed, make sure the fingerprint cache is cleared. - Fixes #1016
-rw-r--r--openpgp/src/packet/key.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs
index 9dcce43e..be446af9 100644
--- a/openpgp/src/packet/key.rs
+++ b/openpgp/src/packet/key.rs
@@ -1122,6 +1122,9 @@ impl<P, R> Key4<P, R>
-> Result<time::SystemTime>
where T: Into<time::SystemTime>
{
+ // Clear the cache.
+ self.fingerprint = Default::default();
+
Ok(std::mem::replace(&mut self.creation_time,
timestamp.into().try_into()?)
.into())
@@ -1138,6 +1141,9 @@ impl<P, R> Key4<P, R>
pub fn set_pk_algo(&mut self, pk_algo: PublicKeyAlgorithm)
-> PublicKeyAlgorithm
{
+ // Clear the cache.
+ self.fingerprint = Default::default();
+
::std::mem::replace(&mut self.pk_algo, pk_algo)
}
@@ -1148,6 +1154,9 @@ impl<P, R> Key4<P, R>
/// Returns a mutable reference to the `Key`'s MPIs.
pub fn mpis_mut(&mut self) -> &mut mpi::PublicKey {
+ // Clear the cache.
+ self.fingerprint = Default::default();
+
&mut self.mpis
}
@@ -1155,6 +1164,9 @@ impl<P, R> Key4<P, R>
///
/// This function returns the old MPIs, if any.
pub fn set_mpis(&mut self, mpis: mpi::PublicKey) -> mpi::PublicKey {
+ // Clear the cache.
+ self.fingerprint = Default::default();
+
::std::mem::replace(&mut self.mpis, mpis)
}
@@ -2342,4 +2354,33 @@ FwPoSAbbsLkNS/iNN2MDGAVYvezYn2QZ
mutate_eq_discriminates_key(key, i)
}
}
+
+ #[test]
+ fn issue_1016() {
+ // The fingerprint is a function of the creation time,
+ // algorithm, and public MPIs. When we change them make sure
+ // the fingerprint also changes.
+
+ let mut g = quickcheck::Gen::new(256);
+
+ let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
+ let fpr1 = key.fingerprint();
+ key.set_creation_time(std::time::UNIX_EPOCH).expect("ok");
+ assert_ne!(fpr1, key.fingerprint());
+
+ let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
+ let fpr1 = key.fingerprint();
+ key.set_pk_algo(PublicKeyAlgorithm::Unknown(222));
+ assert_ne!(fpr1, key.fingerprint());
+
+ let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
+ let fpr1 = key.fingerprint();
+ *key.mpis_mut() = mpi::PublicKey::arbitrary(&mut g);
+ assert_ne!(fpr1, key.fingerprint());
+
+ let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
+ let fpr1 = key.fingerprint();
+ key.set_mpis(mpi::PublicKey::arbitrary(&mut g));
+ assert_ne!(fpr1, key.fingerprint());
+ }
}