summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-09-18 14:23:55 +0200
committerNeal H. Walfield <neal@pep.foundation>2019-09-18 15:22:50 +0200
commit212202e5bec054cc3b2b83edffdd8b83db5bf011 (patch)
treec70a8449148eaf23af1bb6d7ee15fd0aba4a029f
parent446dfdbcb63af00daa3a777958419e1dfb6fc737 (diff)
openpgp: Combine Signature4::key_alive and its _at variant.
- Combine Signature4::key_alive and Signature4::key_alive_at. - Use an Into<Option<time::Tm>> to distinguish the two previous cases: the current time (None), and a specific time (a time::Tm).
-rw-r--r--openpgp-ffi/include/sequoia/openpgp.h14
-rw-r--r--openpgp-ffi/src/packet/signature.rs26
-rw-r--r--openpgp/src/packet/signature/subpacket.rs44
-rw-r--r--openpgp/src/parse/stream.rs4
-rw-r--r--openpgp/src/tpk/keyiter.rs2
-rw-r--r--openpgp/src/tpk/mod.rs4
-rw-r--r--tool/src/commands/inspect.rs2
7 files changed, 37 insertions, 59 deletions
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h
index ef156c62..84633002 100644
--- a/openpgp-ffi/include/sequoia/openpgp.h
+++ b/openpgp-ffi/include/sequoia/openpgp.h
@@ -405,21 +405,15 @@ bool pgp_signature_expired(pgp_signature_t signature);
bool pgp_signature_expired_at(pgp_signature_t signature, time_t when);
/*/
-/// Returns whether the signature is alive.
-///
-/// A signature is alive if the creation date is in the past, and the
-/// signature has not expired.
-/*/
-bool pgp_signature_key_alive(pgp_signature_t signature, pgp_key_t key);
-
-/*/
/// Returns whether the signature is alive at the specified time.
///
/// A signature is alive if the creation date is in the past, and the
/// signature has not expired at the specified time.
+///
+/// If `when` is 0, then the current time is used.
/*/
-bool pgp_signature_key_alive_at(pgp_signature_t signature, pgp_key_t key,
- time_t when);
+bool pgp_signature_key_alive(pgp_signature_t signature, pgp_key_t key,
+ time_t when);
/*/
/// Returns whether the signature is expired at the specified time.
diff --git a/openpgp-ffi/src/packet/signature.rs b/openpgp-ffi/src/packet/signature.rs
index 654e9105..4d3f4ae3 100644
--- a/openpgp-ffi/src/packet/signature.rs
+++ b/openpgp-ffi/src/packet/signature.rs
@@ -148,26 +148,22 @@ fn pgp_signature_expired_at(sig: *const Signature, when: time_t) -> bool {
.signature_expired_at(time::at(time::Timespec::new(when as i64, 0)))
}
-/// Returns whether the signature is alive.
+/// Returns whether the signature is alive at the specified time.
///
/// A signature is alive if the creation date is in the past, and the
/// signature has not expired.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C"
-fn pgp_signature_key_alive(sig: *const Signature, key: *const Key)
- -> bool {
- sig.ref_raw().key_alive(key.ref_raw())
-}
-
-/// Returns whether the signature is alive at the specified time.
///
-/// A signature is alive if the creation date is in the past, and the
-/// signature has not expired at the specified time.
+/// If `when` is 0, then the current time is used.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C"
-fn pgp_signature_key_alive_at(sig: *const Signature, key: *const Key,
- when: time_t) -> bool {
- sig.ref_raw()
- .key_alive_at(key.ref_raw(),
- time::at(time::Timespec::new(when as i64, 0)))
+fn pgp_signature_key_alive(sig: *const Signature, key: *const Key,
+ when: time_t)
+ -> bool {
+ let t = if when == 0 {
+ None
+ } else {
+ Some(time::at(time::Timespec::new(when as i64, 0)))
+ };
+ sig.ref_raw().key_alive(key.ref_raw(), t)
}
/// Returns whether the signature is expired at the specified time.
diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs
index eda0010a..c1d7dddd 100644
--- a/openpgp/src/packet/signature/subpacket.rs
+++ b/openpgp/src/packet/signature/subpacket.rs
@@ -1552,35 +1552,23 @@ impl Signature4 {
}
}
- /// Returns whether or not the given key is alive, i.e. the
- /// creation time has passed, but the expiration time has not.
- ///
- /// This function does not check whether the key was revoked.
+ /// Returns whether or not the given key is alive at `t`.
///
- /// See [Section 5.2.3.6 of RFC 4880].
- ///
- /// [Section 5.2.3.6 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.2.3.6
- pub fn key_alive<P, R>(&self, key: &Key<P, R>) -> bool
- where P: key::KeyParts,
- R: key::KeyRole
- {
- self.key_alive_at(key, time::now_utc())
- }
-
- /// Returns whether or not the given key is alive at the given
- /// time, i.e. the creation time has passed, but the expiration
- /// time has not.
+ /// A key is considered to be alive if `creation time <= t` and `t
+ /// <= expiration time`.
///
/// This function does not check whether the key was revoked.
///
/// See [Section 5.2.3.6 of RFC 4880].
///
/// [Section 5.2.3.6 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.2.3.6
- pub fn key_alive_at<P, R>(&self, key: &Key<P, R>, tm: time::Tm) -> bool
+ pub fn key_alive<P, R, T>(&self, key: &Key<P, R>, t: T) -> bool
where P: key::KeyParts,
- R: key::KeyRole
+ R: key::KeyRole,
+ T: Into<Option<time::Tm>>
{
- *key.creation_time() <= tm && ! self.key_expired(key, tm)
+ let t = t.into().unwrap_or_else(time::now_utc);
+ *key.creation_time() <= t && ! self.key_expired(key, t)
}
/// Returns the value of the Preferred Symmetric Algorithms
@@ -2506,10 +2494,10 @@ fn accessors() {
assert!(!sig_.key_expired(&key, now));
assert!(sig_.key_expired(&key, now + ten_minutes));
- assert!(sig_.key_alive(&key));
- assert!(sig_.key_alive_at(&key, now));
- assert!(!sig_.key_alive_at(&key, now - five_minutes));
- assert!(!sig_.key_alive_at(&key, now + ten_minutes));
+ assert!(sig_.key_alive(&key, None));
+ assert!(sig_.key_alive(&key, now));
+ assert!(!sig_.key_alive(&key, now - five_minutes));
+ assert!(!sig_.key_alive(&key, now + ten_minutes));
sig = sig.set_key_expiration_time(None).unwrap();
let sig_ =
@@ -2519,10 +2507,10 @@ fn accessors() {
assert!(!sig_.key_expired(&key, now));
assert!(!sig_.key_expired(&key, now + ten_minutes));
- assert!(sig_.key_alive(&key));
- assert!(sig_.key_alive_at(&key, now));
- assert!(!sig_.key_alive_at(&key, now - five_minutes));
- assert!(sig_.key_alive_at(&key, now + ten_minutes));
+ assert!(sig_.key_alive(&key, None));
+ assert!(sig_.key_alive(&key, now));
+ assert!(!sig_.key_alive(&key, now - five_minutes));
+ assert!(sig_.key_alive(&key, now + ten_minutes));
let pref = vec![SymmetricAlgorithm::AES256,
SymmetricAlgorithm::AES192,
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs
index 4565db6c..7ab8033d 100644
--- a/openpgp/src/parse/stream.rs
+++ b/openpgp/src/parse/stream.rs
@@ -480,7 +480,7 @@ impl<'a, H: VerificationHelper> Verifier<'a, H> {
sig.key_flags().can_sign()
// Check expiry.
&& sig.signature_alive_at(t)
- && sig.key_alive_at(key, t)
+ && sig.key_alive(key, t)
} else {
false
}
@@ -1299,7 +1299,7 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> {
sig.key_flags().can_sign()
// Check expiry.
&& sig.signature_alive_at(t)
- && sig.key_alive_at(key, t)
+ && sig.key_alive(key, t)
} else {
false
}
diff --git a/openpgp/src/tpk/keyiter.rs b/openpgp/src/tpk/keyiter.rs
index af9f8eae..98a0b57c 100644
--- a/openpgp/src/tpk/keyiter.rs
+++ b/openpgp/src/tpk/keyiter.rs
@@ -123,7 +123,7 @@ impl<'a, P: 'a + key::KeyParts, R: 'a + key::KeyRole> Iterator
if let Some(alive_at) = self.alive_at {
if let Some(sig) = sigo {
- if ! sig.key_alive_at(key, alive_at) {
+ if ! sig.key_alive(key, alive_at) {
t!("Key not alive... skipping.");
continue;
}
diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs
index 6396cb6f..dc230513 100644
--- a/openpgp/src/tpk/mod.rs
+++ b/openpgp/src/tpk/mod.rs
@@ -1301,7 +1301,7 @@ impl TPK {
/// Returns whether or not the TPK is alive.
pub fn alive(&self) -> bool {
if let Some(sig) = self.primary_key_signature(None) {
- sig.key_alive(self.primary().key())
+ sig.key_alive(self.primary().key(), None)
} else {
false
}
@@ -1310,7 +1310,7 @@ impl TPK {
/// Returns whether or not the key is alive at the given time.
pub fn alive_at(&self, tm: time::Tm) -> bool {
if let Some(sig) = self.primary_key_signature(tm) {
- sig.key_alive_at(self.primary().key(), tm)
+ sig.key_alive(self.primary().key(), tm)
} else {
false
}
diff --git a/tool/src/commands/inspect.rs b/tool/src/commands/inspect.rs
index e1e8a217..438a2f77 100644
--- a/tool/src/commands/inspect.rs
+++ b/tool/src/commands/inspect.rs
@@ -178,7 +178,7 @@ fn inspect_key<P, R>(output: &mut io::Write,
if let Some(sig) = binding_signature {
if sig.key_expired(key, None) {
writeln!(output, "{} Expired", indent)?;
- } else if ! sig.key_alive(key) {
+ } else if ! sig.key_alive(key, None) {
writeln!(output, "{} Not yet valid", indent)?;
}
}