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 | |
parent | 13c437470cc7377d7b761b5bb9b8d4efb0ba385e (diff) |
openpgp: Replace time crate with std::time.
- In sq and sqv, use chrono to interface with the user.
- Fixes #341.
45 files changed, 522 insertions, 438 deletions
@@ -298,6 +298,17 @@ dependencies = [ ] [[package]] +name = "chrono" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "clang-sys" version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1028,6 +1039,15 @@ dependencies = [ ] [[package]] +name = "num-integer" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "num-traits" version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1576,7 +1596,6 @@ dependencies = [ "sequoia-net 0.11.0", "sequoia-openpgp 0.11.0", "sequoia-store 0.11.0", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1663,7 +1682,6 @@ dependencies = [ "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1678,7 +1696,6 @@ dependencies = [ "nettle 5.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "sequoia-ffi-macros 0.11.0", "sequoia-openpgp 0.11.0", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1686,10 +1703,10 @@ name = "sequoia-sqv" version = "0.11.0" dependencies = [ "assert_cli 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "sequoia-openpgp 0.11.0", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1707,7 +1724,6 @@ dependencies = [ "sequoia-ipc 0.11.0", "sequoia-net 0.11.0", "sequoia-openpgp 0.11.0", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1717,6 +1733,7 @@ name = "sequoia-tool" version = "0.11.0" dependencies = [ "assert_cli 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1728,7 +1745,6 @@ dependencies = [ "sequoia-store 0.11.0", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "termsize 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2301,6 +2317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7fa24eb00d5ffab90eaeaf1092ac85c04c64aaf358ea6f84505b8116d24c6af" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64a4b57c8f4e3a2e9ac07e0f6abc9c24b6fc9e1b54c3478cfb598f3d0023e51c" +"checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68" "checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" @@ -2385,6 +2402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f40f005c60db6e03bae699e414c58bf9aa7ea02a2d0b9bfbcf19286cc4c82b30" "checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" "checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" +"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml index 4ca1ba41..10b80a26 100644 --- a/ffi/Cargo.toml +++ b/ffi/Cargo.toml @@ -33,7 +33,6 @@ libc = "0.2.33" memsec = "0.5.6" native-tls = "0.2.0" nettle = "5.0" -time = "0.1.40" tokio-core = "0.1" [dev-dependencies] diff --git a/ffi/include/sequoia/store.h b/ffi/include/sequoia/store.h index 50abc67a..38eb04d0 100644 --- a/ffi/include/sequoia/store.h +++ b/ffi/include/sequoia/store.h @@ -119,12 +119,12 @@ struct sq_stamps { /*/ /// Records the time when this has been used first. /*/ - uint64_t first; + time_t first; /*/ /// Records the time when this has been used last. /*/ - uint64_t last; + time_t last; }; /*/ @@ -134,12 +134,12 @@ struct sq_stats { /*/ /// Records the time this item was created. /*/ - uint64_t created; + time_t created; /*/ /// Records the time this item was last updated. /*/ - uint64_t updated; + time_t updated; /*/ /// Records counters and timestamps of encryptions. diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index bc864f76..92cfab0d 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -140,6 +140,8 @@ pub(crate) use crate::openpgp::{ MoveIntoRaw, MoveResultIntoRaw, Maybe, + maybe_time, + to_time_t, }; /* Error handling with implicit context. */ diff --git a/ffi/src/store.rs b/ffi/src/store.rs index abf47962..8f273258 100644 --- a/ffi/src/store.rs +++ b/ffi/src/store.rs @@ -42,6 +42,7 @@ use crate::RefRaw; use crate::MoveIntoRaw; use crate::MoveResultIntoRaw; use crate::Maybe; +use crate::to_time_t; /// Lists all mappings with the given prefix. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" @@ -159,7 +160,7 @@ fn sq_log_iter_next(iter: *mut LogIter) -> *mut Log { }; box_raw!(Log{ - timestamp: e.timestamp.sec as u64, + timestamp: to_time_t(e.timestamp), mapping: maybe_box_raw!(e.mapping), binding: maybe_box_raw!(e.binding), key: maybe_box_raw!(e.key), @@ -572,20 +573,18 @@ pub struct Stamps { pub count: u64, /// Records the time when this has been used first. - pub first: u64, + pub first: libc::time_t, /// Records the time when this has been used last. - pub last: u64, + pub last: libc::time_t, } impl Stamps { fn new(s: &sequoia_store::Stamps) -> Stamps { Stamps{ count: s.count as u64, - first: s.first.map(|t| t.sec).unwrap_or(0) - as u64, - last: s.last.map(|t| t.sec).unwrap_or(0) - as u64, + first: to_time_t(s.first), + last: to_time_t(s.last), } } } @@ -598,10 +597,10 @@ impl Stamps { #[repr(C)] pub struct Stats { /// Records the time this item was created. - pub created: u64, + pub created: libc::time_t, /// Records the time this item was last updated. - pub updated: u64, + pub updated: libc::time_t, /// Records counters and timestamps of encryptions. pub encryption: Stamps, @@ -613,8 +612,8 @@ pub struct Stats { impl Stats { fn new(s: sequoia_store::Stats) -> Stats { Stats { - created: s.created.map(|t| t.sec).unwrap_or(0) as u64, - updated: s.updated.map(|t| t.sec).unwrap_or(0) as u64, + created: to_time_t(s.created), + updated: to_time_t(s.updated), encryption: Stamps::new(&s.encryption), verification: Stamps::new(&s.verification), } @@ -625,7 +624,7 @@ impl Stats { #[repr(C)] pub struct Log { /// Records the time of the entry. - pub timestamp: u64, + pub timestamp: libc::time_t, /// Relates the entry to a mapping. /// 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/ |