From 996b680ec47fcb6da46e182cc1b1c4bddb24b6cb Mon Sep 17 00:00:00 2001 From: Joseph Birr-Pixton Date: Mon, 28 Aug 2017 18:30:19 +0100 Subject: [PATCH] Remove time crate dependency --- Cargo.toml | 1 - src/client/hs.rs | 14 +++++--------- src/lib.rs | 3 --- src/ticketer.rs | 19 ++++++++++++++----- src/verify.rs | 12 ++++++++++-- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 489421ca..4fda2bb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ categories = ["network-programming", "cryptography"] [dependencies] untrusted = "0.5.1" -time = "0.1.37" base64 = "0.6" log = { version = "0.3.6", optional = true } ring = { version = "0.12", features = ["rsa_signing"] } diff --git a/src/client/hs.rs b/src/client/hs.rs index e692106f..81a52c1f 100644 --- a/src/client/hs.rs +++ b/src/client/hs.rs @@ -27,7 +27,7 @@ use suites; use hash_hs; use verify; use rand; -use time; +use ticketer; use error::TLSError; use handshake::{check_message, check_handshake_message}; @@ -77,10 +77,6 @@ fn illegal_param(sess: &mut ClientSessionImpl, why: &str) -> TLSError { TLSError::PeerMisbehavedError(why.to_string()) } -fn ticket_timebase() -> u64 { - time::get_time().sec as u64 -} - fn check_aligned_handshake(sess: &mut ClientSessionImpl) -> Result<(), TLSError> { if !sess.common.handshake_joiner.is_empty() { Err(illegal_param(sess, "keys changed with pending hs fragment")) @@ -102,7 +98,7 @@ fn find_session(sess: &mut ClientSessionImpl, dns_name: &str) -> Option u64 { + time::SystemTime::now() + .duration_since(time::UNIX_EPOCH) + .unwrap() + .as_secs() +} + /// This is a `ProducesTickets` implementation which uses /// any *ring* `aead::Algorithm` to encrypt and authentication /// the ticket payload. It does not enforce any lifetime @@ -96,7 +105,7 @@ impl ProducesTickets for AEADTicketer { struct TicketSwitcherState { current: Box, previous: Option>, - next_switch_time: i64, + next_switch_time: u64, } /// A ticketer that has a 'current' sub-ticketer and a single @@ -122,7 +131,7 @@ impl TicketSwitcher { state: Mutex::new(TicketSwitcherState { current: generator(), previous: None, - next_switch_time: time::get_time().sec + lifetime as i64, + next_switch_time: timebase() + lifetime as u64, }), } } @@ -135,11 +144,11 @@ impl TicketSwitcher { /// key erasure will be delayed until the next encrypt/decrypt call. pub fn maybe_roll(&self) { let mut state = self.state.lock().unwrap(); - let now = time::get_time().sec; + let now = timebase(); if now > state.next_switch_time { state.previous = Some(mem::replace(&mut state.current, (self.generator)())); - state.next_switch_time = now + self.lifetime as i64; + state.next_switch_time = now + self.lifetime as u64; } } } diff --git a/src/verify.rs b/src/verify.rs index 0fd144e7..ee280631 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -1,5 +1,4 @@ use webpki; -use time; use untrusted; use sct; use std; @@ -267,11 +266,20 @@ pub fn verify_tls13(cert: &Certificate, .map(|_| HandshakeSignatureValid::assertion()) } +fn unix_time_millis() -> Result { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|dur| dur.as_secs()) + .map_err(|_| TLSError::FailedToGetCurrentTime) + .and_then(|secs| secs.checked_mul(1000) + .ok_or(TLSError::FailedToGetCurrentTime)) +} + pub fn verify_scts(cert: &Certificate, scts: &SCTList, logs: &[&sct::Log]) -> Result<(), TLSError> { let mut valid_scts = 0; - let now = (time::get_time().sec * 1000) as u64; + let now = unix_time_millis()?; let mut last_sct_error = None; for sct in scts {