diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-20 19:02:34 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-21 16:25:30 +0100 |
commit | b251f9e8857fba284f515061ac62013519997e30 (patch) | |
tree | 8cb3501b8cb32e43496e56dd76446ba7559c7eed /openpgp-ffi | |
parent | 13c437470cc7377d7b761b5bb9b8d4efb0ba385e (diff) |
openpgp: Replace time crate with std::time.
- In sq and sqv, use chrono to interface with the user.
- Fixes #341.
Diffstat (limited to 'openpgp-ffi')
-rw-r--r-- | openpgp-ffi/Cargo.toml | 1 | ||||
-rw-r--r-- | openpgp-ffi/src/common.rs | 26 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/key.rs | 3 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/mod.rs | 1 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/signature.rs | 12 | ||||
-rw-r--r-- | openpgp-ffi/src/parse/mod.rs | 1 | ||||
-rw-r--r-- | openpgp-ffi/src/parse/stream.rs | 10 | ||||
-rw-r--r-- | openpgp-ffi/src/serialize.rs | 1 | ||||
-rw-r--r-- | openpgp-ffi/src/tpk.rs | 31 |
9 files changed, 42 insertions, 44 deletions
diff --git a/openpgp-ffi/Cargo.toml b/openpgp-ffi/Cargo.toml index 6e2f5bff..0fc86188 100644 --- a/openpgp-ffi/Cargo.toml +++ b/openpgp-ffi/Cargo.toml @@ -29,7 +29,6 @@ lazy_static = "1.0.0" libc = "0.2.33" memsec = "0.5.6" nettle = "5.0" -time = "0.1.40" [dev-dependencies] filetime = "0.2" diff --git a/openpgp-ffi/src/common.rs b/openpgp-ffi/src/common.rs index 2e8afb99..078c035e 100644 --- a/openpgp-ffi/src/common.rs +++ b/openpgp-ffi/src/common.rs @@ -279,6 +279,32 @@ pub(crate) fn build_hasher() -> DefaultHasher { RANDOM_STATE.build_hasher() } +/* time_t support. */ + +/// Converts a time_t for use in Sequoia. +pub(crate) fn maybe_time(t: libc::time_t) -> Option<std::time::SystemTime> { + if t == 0 { + None + } else { + Some(std::time::UNIX_EPOCH + std::time::Duration::new(t as u64, 0)) + } +} + +/// Converts a time_t for use in C. +#[allow(dead_code)] +pub(crate) fn to_time_t<T>(t: T) -> libc::time_t + where T: Into<Option<std::time::SystemTime>> +{ + if let Some(t) = t.into() { + match t.duration_since(std::time::UNIX_EPOCH) { + Ok(d) => d.as_secs() as libc::time_t, + Err(_) => 0, // Unrepresentable. + } + } else { + 0 + } +} + pub mod armor; pub mod crypto; pub mod error; diff --git a/openpgp-ffi/src/packet/key.rs b/openpgp-ffi/src/packet/key.rs index 0229a301..e0eead22 100644 --- a/openpgp-ffi/src/packet/key.rs +++ b/openpgp-ffi/src/packet/key.rs @@ -47,7 +47,8 @@ fn pgp_key_creation_time(key: *const Key) -> time_t { let key = key.ref_raw(); let ct = key.creation_time(); - ct.to_timespec().sec as time_t + ct.duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()) + .unwrap_or(0) as time_t } /// Returns the key's public key algorithm. diff --git a/openpgp-ffi/src/packet/mod.rs b/openpgp-ffi/src/packet/mod.rs index 7bab6195..deef792a 100644 --- a/openpgp-ffi/src/packet/mod.rs +++ b/openpgp-ffi/src/packet/mod.rs @@ -7,7 +7,6 @@ use libc::c_char; extern crate sequoia_openpgp as openpgp; -extern crate time; use self::openpgp::{ packet::Tag, diff --git a/openpgp-ffi/src/packet/signature.rs b/openpgp-ffi/src/packet/signature.rs index 2b1b301e..14df1ce3 100644 --- a/openpgp-ffi/src/packet/signature.rs +++ b/openpgp-ffi/src/packet/signature.rs @@ -172,7 +172,7 @@ fn pgp_signature_alive(sig: *const Signature, time: time_t) let time = if time == 0 { None } else { - Some(time::at(time::Timespec::new(time as i64, 0))) + Some(std::time::UNIX_EPOCH + std::time::Duration::new(time as u64, 0)) }; sig.ref_raw().signature_alive(time, None) } @@ -233,9 +233,9 @@ fn pgp_signature_alive_with_tolerance(sig: *const Signature, let time = if time == 0 { None } else { - Some(time::at(time::Timespec::new(time as i64, 0))) + Some(std::time::UNIX_EPOCH + std::time::Duration::new(time as u64, 0)) }; - let tolerance = time::Duration::seconds(tolerance as i64); + let tolerance = std::time::Duration::new(tolerance as u64, 0); sig.ref_raw().signature_alive(time, Some(tolerance)) } @@ -247,7 +247,7 @@ fn pgp_signature_expired(sig: *const Signature, when: time_t) -> bool { let t = if when == 0 { None } else { - Some(time::at(time::Timespec::new(when as i64, 0))) + Some(std::time::UNIX_EPOCH + std::time::Duration::new(when as u64, 0)) }; sig.ref_raw().signature_expired(t) } @@ -265,7 +265,7 @@ fn pgp_signature_key_alive(sig: *const Signature, key: *const Key, let t = if when == 0 { None } else { - Some(time::at(time::Timespec::new(when as i64, 0))) + Some(std::time::UNIX_EPOCH + std::time::Duration::new(when as u64, 0)) }; sig.ref_raw().key_alive(key.ref_raw(), t) } @@ -279,7 +279,7 @@ fn pgp_signature_key_expired(sig: *const Signature, key: *const Key, let t = if when == 0 { None } else { - Some(time::at(time::Timespec::new(when as i64, 0))) + Some(std::time::UNIX_EPOCH + std::time::Duration::new(when as u64, 0)) }; sig.ref_raw().key_expired(key.ref_raw(), t) } diff --git a/openpgp-ffi/src/parse/mod.rs b/openpgp-ffi/src/parse/mod.rs index c030e6a0..a4f213b5 100644 --- a/openpgp-ffi/src/parse/mod.rs +++ b/openpgp-ffi/src/parse/mod.rs @@ -12,7 +12,6 @@ use std::slice; use libc::{c_char, c_int, size_t}; extern crate sequoia_openpgp as openpgp; -extern crate time; use super::packet::{ Packet, diff --git a/openpgp-ffi/src/parse/stream.rs b/openpgp-ffi/src/parse/stream.rs index d46f9042..87582ee6 100644 --- a/openpgp-ffi/src/parse/stream.rs +++ b/openpgp-ffi/src/parse/stream.rs @@ -14,7 +14,6 @@ use std::ptr; use libc::{c_int, c_void, time_t}; extern crate sequoia_openpgp as openpgp; -extern crate time; use self::openpgp::{ crypto::SessionKey, @@ -39,6 +38,7 @@ use crate::MoveIntoRaw; use crate::MoveResultIntoRaw; use crate::RefRaw; use crate::RefMutRaw; +use crate::maybe_time; use super::super::{ error::Status, @@ -542,14 +542,6 @@ fn pgp_verifier_new<'a>(errp: Option<&mut *mut crate::error::Error>, .move_into_raw(errp) } -fn maybe_time(t: time_t) -> Option<time::Tm> { - if t == 0 { - None - } else { - Some(time::at(time::Timespec::new(t as i64, 0))) - } -} - /// Verifies a detached OpenPGP signature. /// /// # Example diff --git a/openpgp-ffi/src/serialize.rs b/openpgp-ffi/src/serialize.rs index d5d7afa6..d2244bab 100644 --- a/openpgp-ffi/src/serialize.rs +++ b/openpgp-ffi/src/serialize.rs @@ -11,7 +11,6 @@ use std::io::Write; use libc::{c_char, size_t, ssize_t}; extern crate sequoia_openpgp as openpgp; -extern crate time; use self::openpgp::constants::{ AEADAlgorithm, diff --git a/openpgp-ffi/src/tpk.rs b/openpgp-ffi/src/tpk.rs index ecc0ee0b..75c51b88 100644 --- a/openpgp-ffi/src/tpk.rs +++ b/openpgp-ffi/src/tpk.rs @@ -43,6 +43,7 @@ use crate::RefRaw; use crate::MoveFromRaw; use crate::MoveIntoRaw; use crate::MoveResultIntoRaw; +use crate::maybe_time; /// A transferable public key (TPK). /// @@ -166,14 +167,7 @@ fn pgp_tpk_primary_key(tpk: *const TPK) -> *const Key { fn pgp_tpk_revoked(tpk: *const TPK, when: time_t) -> *mut RevocationStatus<'static> { - let when = when as i64; - let when = if when == 0 { - None - } else { - Some(time::at(time::Timespec::new(when, 0))) - }; - - tpk.ref_raw().revoked(when).move_into_raw() + tpk.ref_raw().revoked(maybe_time(when)).move_into_raw() } fn int_to_reason_for_revocation(code: c_int) -> ReasonForRevocation { @@ -328,13 +322,7 @@ fn pgp_tpk_revoke_in_place(errp: Option<&mut *mut crate::error::Error>, /// If `when` is 0, then the current time is used. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" fn pgp_tpk_expired(tpk: *const TPK, when: time_t) -> c_int { - let tpk = tpk.ref_raw(); - let t = if when == 0 { - None - } else { - Some(time::at(time::Timespec::new(when as i64, 0))) - }; - tpk.expired(t) as c_int + tpk.ref_raw().expired(maybe_time(when)) as c_int } /// Returns whether the TPK is alive at the specified time. @@ -343,13 +331,7 @@ fn pgp_tpk_expired(tpk: *const TPK, when: time_t) -> c_int { #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" fn pgp_tpk_alive(tpk: *const TPK, when: time_t) -> c_int { - let tpk = tpk.ref_raw(); - let t = if when == 0 { - None - } else { - Some(time::at(time::Timespec::new(when as i64, 0))) - }; - tpk.alive(t) as c_int + tpk.ref_raw().alive(maybe_time(when)) as c_int } /// Changes the TPK's expiration. @@ -368,7 +350,7 @@ fn pgp_tpk_set_expiry(errp: Option<&mut *mut crate::error::Error>, let signer = ffi_param_ref_mut!(primary_signer); tpk.set_expiry(signer.as_mut(), - Some(time::Duration::seconds(expiry as i64))) + Some(std::time::Duration::new(expiry as u64, 0))) .move_into_raw(errp) } @@ -642,7 +624,8 @@ pub extern "C" fn pgp_tpk_key_iter_alive_at<'a>( use std::mem; let tmp = mem::replace(&mut iter_wrapper.iter, KeyIter::empty()); - iter_wrapper.iter = tmp.alive_at(time::at(time::Timespec::new(when as i64, 0))); + iter_wrapper.iter = + tmp.alive_at(maybe_time(when).unwrap_or(std::time::UNIX_EPOCH)); } /// Changes the iterator to only return keys whose revocation status |