diff options
author | Neal H. Walfield <neal@pep.foundation> | 2019-09-18 16:04:18 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2019-09-18 16:04:18 +0200 |
commit | 044854773282eed2be23aab511d5dc38ffbe21ca (patch) | |
tree | d3affcb154624eab106d06c254128730be868f1b | |
parent | c619dffe8562872ad33c501b7f3c3cd9edb7f00b (diff) |
openpgp: Combine Signature4::signature_alive and its _at variant.
- Combine Signature4::signature_alive and
Signature4::signature_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.h | 12 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/signature.rs | 21 | ||||
-rw-r--r-- | openpgp/src/packet/signature/subpacket.rs | 45 | ||||
-rw-r--r-- | openpgp/src/parse/stream.rs | 8 | ||||
-rw-r--r-- | openpgp/src/serialize/tpk_armored.rs | 2 | ||||
-rw-r--r-- | openpgp/src/tpk/mod.rs | 8 | ||||
-rw-r--r-- | tool/src/commands/inspect.rs | 2 |
7 files changed, 41 insertions, 57 deletions
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h index c84fb582..c6157b70 100644 --- a/openpgp-ffi/include/sequoia/openpgp.h +++ b/openpgp-ffi/include/sequoia/openpgp.h @@ -379,20 +379,14 @@ bool pgp_signature_is_split_key(pgp_signature_t signature); bool pgp_signature_is_group_key(pgp_signature_t signature); /*/ -/// 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_alive(pgp_signature_t signature); - -/*/ /// Returns whether the signature is alive at the specified time. /// +/// If `when` is 0, then the current time is used. +/// /// A signature is alive if the creation date is in the past, and the /// signature has not expired at the specified time. /*/ -bool pgp_signature_alive_at(pgp_signature_t signature, time_t when); +bool pgp_signature_alive(pgp_signature_t signature, 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 ed0691f9..636318b3 100644 --- a/openpgp-ffi/src/packet/signature.rs +++ b/openpgp-ffi/src/packet/signature.rs @@ -116,23 +116,20 @@ fn pgp_signature_is_group_key(sig: *const Signature) -> bool { } -/// Returns whether the signature is alive. -/// -/// 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_alive(sig: *const Signature) -> bool { - sig.ref_raw().signature_alive() -} - /// Returns whether the signature is alive at the specified time. /// +/// If `when` is 0, then the current time is used. +/// /// A signature is alive if the creation date is in the past, and the /// signature has not expired at the specified time. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" -fn pgp_signature_alive_at(sig: *const Signature, when: time_t) -> bool { - sig.ref_raw() - .signature_alive_at(time::at(time::Timespec::new(when as i64, 0))) +fn pgp_signature_alive(sig: *const Signature, when: time_t) -> bool { + let t = if when == 0 { + None + } else { + Some(time::at(time::Timespec::new(when as i64, 0))) + }; + sig.ref_raw().signature_alive(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 dda69e2a..ab3a81a5 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -1350,24 +1350,14 @@ impl Signature4 { } } - /// Returns whether or not the signature is alive, i.e. the - /// creation time has passed, but the expiration time has not. + /// Returns whether or not the signature is alive at the given + /// time. /// - /// Note that [Section 5.2.3.4 of RFC 4880] states that "[[A - /// Signature Creation Time subpacket]] MUST be present in the - /// hashed area." Consequently, if such a packet does not exist, - /// but a "Signature Expiration Time" subpacket exists, we - /// conservatively treat the signature as expired, because there - /// is no way to evaluate the expiration time. + /// A signature is considered to be alive if `creation time <= t` + /// and `t <= expiration time`. + /// + /// If `t` is None, uses the current time. /// - /// [Section 5.2.3.4 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.2.3.4 - pub fn signature_alive(&self) -> bool { - self.signature_alive_at(time::now_utc()) - } - - /// Returns whether or not the signature is alive at the given - /// time, i.e. the creation time has passed, but the expiration - /// time has not. /// /// Note that [Section 5.2.3.4 of RFC 4880] states that "[[A /// Signature Creation Time subpacket]] MUST be present in the @@ -1377,9 +1367,12 @@ impl Signature4 { /// is no way to evaluate the expiration time. /// /// [Section 5.2.3.4 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.2.3.4 - pub fn signature_alive_at(&self, tm: time::Tm) -> bool { + pub fn signature_alive<T>(&self, t: T) -> bool + where T: Into<Option<time::Tm>> + { + let t = t.into().unwrap_or_else(time::now_utc); if let Some(creation_time) = self.signature_creation_time() { - creation_time <= tm && ! self.signature_expired(tm) + creation_time <= t && ! self.signature_expired(t) } else { false } @@ -2429,10 +2422,10 @@ fn accessors() { assert!(!sig_.signature_expired(now)); assert!(sig_.signature_expired(now + ten_minutes)); - assert!(sig_.signature_alive()); - assert!(sig_.signature_alive_at(now)); - assert!(!sig_.signature_alive_at(now - five_minutes)); - assert!(!sig_.signature_alive_at(now + ten_minutes)); + assert!(sig_.signature_alive(None)); + assert!(sig_.signature_alive(now)); + assert!(!sig_.signature_alive(now - five_minutes)); + assert!(!sig_.signature_alive(now + ten_minutes)); sig = sig.set_signature_expiration_time(None).unwrap(); let sig_ = @@ -2442,10 +2435,10 @@ fn accessors() { assert!(!sig_.signature_expired(now)); assert!(!sig_.signature_expired(now + ten_minutes)); - assert!(sig_.signature_alive()); - assert!(sig_.signature_alive_at(now)); - assert!(!sig_.signature_alive_at(now - five_minutes)); - assert!(sig_.signature_alive_at(now + ten_minutes)); + assert!(sig_.signature_alive(None)); + assert!(sig_.signature_alive(now)); + assert!(!sig_.signature_alive(now - five_minutes)); + assert!(sig_.signature_alive(now + ten_minutes)); sig = sig.set_exportable_certification(true).unwrap(); let sig_ = diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index 7ab8033d..6a1246af 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -479,7 +479,7 @@ impl<'a, H: VerificationHelper> Verifier<'a, H> { if let Some(sig) = sig { sig.key_flags().can_sign() // Check expiry. - && sig.signature_alive_at(t) + && sig.signature_alive(t) && sig.key_alive(key, t) } else { false @@ -627,7 +627,7 @@ impl<'a, H: VerificationHelper> Verifier<'a, H> { = tpk.keys_all().nth(*j) .unwrap(); if sig.verify(key).unwrap_or(false) - && sig.signature_alive_at(self.time) + && sig.signature_alive(self.time) { VerificationResult::GoodChecksum (sig, tpk, key, binding, @@ -1298,7 +1298,7 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> { if let Some(sig) = sig { sig.key_flags().can_sign() // Check expiry. - && sig.signature_alive_at(t) + && sig.signature_alive(t) && sig.key_alive(key, t) } else { false @@ -1445,7 +1445,7 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> { let (binding, revocation, key) = tpk.keys_all().nth(*j).unwrap(); if sig.verify(key).unwrap_or(false) && - sig.signature_alive_at(self.time) + sig.signature_alive(self.time) { // Check intended recipients. if let Some(identity) = diff --git a/openpgp/src/serialize/tpk_armored.rs b/openpgp/src/serialize/tpk_armored.rs index 7ab2405b..8c4c3253 100644 --- a/openpgp/src/serialize/tpk_armored.rs +++ b/openpgp/src/serialize/tpk_armored.rs @@ -39,7 +39,7 @@ impl TPK { } // Ignore userids not "alive". }).filter_map(|uidb| { - if uidb.binding_signature(None)?.signature_alive() { + if uidb.binding_signature(None)?.signature_alive(None) { Some(uidb) } else { None diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index 7fa91956..8f01ab24 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -153,7 +153,7 @@ impl<C> ComponentBinding<C> { let time_zero = time::at_utc(time::Timespec::new(0, 0)); self.selfsigs.iter().filter(|s| { - s.signature_alive_at(t) + s.signature_alive(t) }).max_by(|a, b| { a.signature_creation_time().unwrap_or(time_zero).cmp( &b.signature_creation_time().unwrap_or(time_zero)) @@ -219,7 +219,7 @@ impl<C> ComponentBinding<C> { selfsig_creation_time.rfc822(), t.rfc822()); if let Some(selfsig) = selfsig { - assert!(selfsig.signature_alive_at(t)); + assert!(selfsig.signature_alive(t)); } macro_rules! check { @@ -249,7 +249,7 @@ impl<C> ComponentBinding<C> { rev.signature_creation_time() .unwrap_or_else(time_zero).rfc822()); None - } else if !rev.signature_alive_at(t) { + } else if !rev.signature_alive(t) { t!(" ignoring revocation that is not alive ({} - {})", rev.signature_creation_time() .unwrap_or_else(time_zero).rfc822(), @@ -1062,7 +1062,7 @@ impl TPK { // No binding signature at time `t` => not alive. let selfsig = b.binding_signature(t)?; - if !selfsig.signature_alive_at(t) { + if !selfsig.signature_alive(t) { return None; } diff --git a/tool/src/commands/inspect.rs b/tool/src/commands/inspect.rs index 526490e0..6d594c51 100644 --- a/tool/src/commands/inspect.rs +++ b/tool/src/commands/inspect.rs @@ -151,7 +151,7 @@ fn inspect_tpk(output: &mut io::Write, tpk: &openpgp::TPK, if let Some(sig) = uidb.binding_signature(None) { if sig.signature_expired(None) { writeln!(output, " Expired")?; - } else if ! sig.signature_alive() { + } else if ! sig.signature_alive(None) { writeln!(output, " Not yet valid")?; } } |