Merge "Keystore 2.0: Make MonotonicRawTime use milliseconds."
This commit is contained in:
commit
c1c58f0f77
3 changed files with 17 additions and 24 deletions
|
@ -46,7 +46,7 @@ mod perboot;
|
|||
use crate::impl_metadata; // This is in db_utils.rs
|
||||
use crate::key_parameter::{KeyParameter, Tag};
|
||||
use crate::permission::KeyPermSet;
|
||||
use crate::utils::{get_current_time_in_seconds, watchdog as wd, AID_USER_OFFSET};
|
||||
use crate::utils::{get_current_time_in_milliseconds, watchdog as wd, AID_USER_OFFSET};
|
||||
use crate::{
|
||||
db_utils::{self, SqlField},
|
||||
gc::Gc,
|
||||
|
@ -737,29 +737,24 @@ pub struct KeystoreDB {
|
|||
}
|
||||
|
||||
/// Database representation of the monotonic time retrieved from the system call clock_gettime with
|
||||
/// CLOCK_MONOTONIC_RAW. Stores monotonic time as i64 in seconds.
|
||||
/// CLOCK_MONOTONIC_RAW. Stores monotonic time as i64 in milliseconds.
|
||||
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
|
||||
pub struct MonotonicRawTime(i64);
|
||||
|
||||
impl MonotonicRawTime {
|
||||
/// Constructs a new MonotonicRawTime
|
||||
pub fn now() -> Self {
|
||||
Self(get_current_time_in_seconds())
|
||||
Self(get_current_time_in_milliseconds())
|
||||
}
|
||||
|
||||
/// Constructs a new MonotonicRawTime from a given number of seconds.
|
||||
pub fn from_secs(val: i64) -> Self {
|
||||
Self(val)
|
||||
/// Returns the value of MonotonicRawTime in milliseconds as i64
|
||||
pub fn milliseconds(&self) -> i64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Returns the integer value of MonotonicRawTime as i64
|
||||
pub fn seconds(&self) -> i64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Returns the value of MonotonicRawTime in milli seconds as i64
|
||||
pub fn milli_seconds(&self) -> i64 {
|
||||
self.0 * 1000
|
||||
self.0 / 1000
|
||||
}
|
||||
|
||||
/// Like i64::checked_sub.
|
||||
|
@ -785,6 +780,7 @@ impl FromSql for MonotonicRawTime {
|
|||
#[derive(Clone)]
|
||||
pub struct AuthTokenEntry {
|
||||
auth_token: HardwareAuthToken,
|
||||
// Time received in milliseconds
|
||||
time_received: MonotonicRawTime,
|
||||
}
|
||||
|
||||
|
@ -5207,7 +5203,7 @@ mod tests {
|
|||
let tx2 = db.conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
tx2.commit()?;
|
||||
let last_off_body_2 = db.get_last_off_body();
|
||||
assert!(last_off_body_1.seconds() < last_off_body_2.seconds());
|
||||
assert!(last_off_body_1 < last_off_body_2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -824,12 +824,12 @@ impl Enforcements {
|
|||
} else {
|
||||
// Filter the matching auth tokens by age.
|
||||
if auth_token_max_age_millis != 0 {
|
||||
let now_in_millis = MonotonicRawTime::now().milli_seconds();
|
||||
let now_in_millis = MonotonicRawTime::now();
|
||||
let result = Self::find_auth_token(|auth_token_entry: &AuthTokenEntry| {
|
||||
let token_valid = now_in_millis
|
||||
.checked_sub(auth_token_entry.time_received().milli_seconds())
|
||||
.checked_sub(&auth_token_entry.time_received())
|
||||
.map_or(false, |token_age_in_millis| {
|
||||
auth_token_max_age_millis > token_age_in_millis
|
||||
auth_token_max_age_millis > token_age_in_millis.milliseconds()
|
||||
});
|
||||
token_valid && auth_token_entry.satisfies(&sids, auth_type)
|
||||
});
|
||||
|
|
|
@ -185,18 +185,15 @@ pub fn key_parameters_to_authorizations(
|
|||
parameters.into_iter().map(|p| p.into_authorization()).collect()
|
||||
}
|
||||
|
||||
/// This returns the current time (in seconds) as an instance of a monotonic clock, by invoking the
|
||||
/// system call since Rust does not support getting monotonic time instance as an integer.
|
||||
pub fn get_current_time_in_seconds() -> i64 {
|
||||
/// This returns the current time (in milliseconds) as an instance of a monotonic clock,
|
||||
/// by invoking the system call since Rust does not support getting monotonic time instance
|
||||
/// as an integer.
|
||||
pub fn get_current_time_in_milliseconds() -> i64 {
|
||||
let mut current_time = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
||||
// Following unsafe block includes one system call to get monotonic time.
|
||||
// Therefore, it is not considered harmful.
|
||||
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_RAW, &mut current_time) };
|
||||
// It is safe to unwrap here because try_from() returns std::convert::Infallible, which is
|
||||
// defined to be an error that can never happen (i.e. the result is always ok).
|
||||
// This suppresses the compiler's complaint about converting tv_sec to i64 in method
|
||||
// get_current_time_in_seconds.
|
||||
current_time.tv_sec as i64
|
||||
current_time.tv_sec as i64 * 1000 + (current_time.tv_nsec as i64 / 1_000_000)
|
||||
}
|
||||
|
||||
/// Converts a response code as returned by the Android Protected Confirmation HIDL compatibility
|
||||
|
|
Loading…
Reference in a new issue