Merge "Adjust time for unit tests" into rvc-dev

This commit is contained in:
Fred Gylys-Colwell
2020-04-03 16:30:53 +00:00
committed by Android (Google) Code Review
2 changed files with 19 additions and 22 deletions

View File

@@ -95,19 +95,22 @@ SessionContext* CryptoEngine::FindSession(SessionId sid) {
return nullptr; return nullptr;
} }
int64_t CryptoEngine::OnlineTime() { int64_t CryptoEngine::MonotonicTime() {
// Use the monotonic clock for times that don't have to be stable across // Use the monotonic clock for times that don't have to be stable across
// device boots. // device boots.
int64_t now = wvcdm::Clock().GetCurrentTime(); int64_t now =
wvcdm::Clock().GetCurrentTime() + offline_time_info_.rollback_offset;
static int64_t then = now; static int64_t then = now;
if (now < then) now = then; if (now < then) {
offline_time_info_.rollback_offset += then - now;
now = then;
}
then = now; then = now;
return now; return now;
} }
int64_t CryptoEngine::RollbackCorrectedOfflineTime() { int64_t CryptoEngine::SystemTime() {
// Add any time offsets in the past to the current time. const int64_t current_time = MonotonicTime();
int64_t current_time = OnlineTime() + offline_time_info_.rollback_offset;
// Write time info to disk if kTimeInfoUpdateWindowInSeconds has elapsed since // Write time info to disk if kTimeInfoUpdateWindowInSeconds has elapsed since
// last write. // last write.
if (current_time - offline_time_info_.previous_time > if (current_time - offline_time_info_.previous_time >
@@ -125,9 +128,9 @@ std::string CryptoEngine::GetUsageTimeFileFullPath() const {
// TODO(fredgc, jfore): Address how this property is presented to the ref. // TODO(fredgc, jfore): Address how this property is presented to the ref.
// For now, the file path is empty. // For now, the file path is empty.
/*if (!wvcdm::Properties::GetDeviceFilesBasePath(wvcdm::kSecurityLevelL3, /*if (!wvcdm::Properties::GetDeviceFilesBasePath(wvcdm::kSecurityLevelL3,
&file_path)) { &file_path)) {
LOGE("RollbackCorrectedOfflineTime: Unable to get base path"); LOGE("Unable to get base path");
}*/ }*/
return file_path + kStoredUsageTimeFileName; return file_path + kStoredUsageTimeFileName;
} }
@@ -148,8 +151,7 @@ bool CryptoEngine::LoadOfflineTimeInfo(const std::string& file_path) {
std::unique_ptr<wvcdm::File> file = std::unique_ptr<wvcdm::File> file =
file_system->Open(file_path, wvcdm::FileSystem::kReadOnly); file_system->Open(file_path, wvcdm::FileSystem::kReadOnly);
if (!file) { if (!file) {
LOGE("RollbackCorrectedOfflineTime: File open failed: %s", LOGE("File open failed: %s", file_path.c_str());
file_path.c_str());
return false; return false;
} }
// Load time info from previous call. // Load time info from previous call.
@@ -164,7 +166,7 @@ bool CryptoEngine::LoadOfflineTimeInfo(const std::string& file_path) {
// Detect offline time rollback after loading from disk. // Detect offline time rollback after loading from disk.
// Add any time offsets in the past to the current time. // Add any time offsets in the past to the current time.
int64_t current_time = OnlineTime() + offline_time_info_.rollback_offset; int64_t current_time = MonotonicTime();
if (offline_time_info_.previous_time > current_time) { if (offline_time_info_.previous_time > current_time) {
// Current time is earlier than the previously saved time. Time has been // Current time is earlier than the previously saved time. Time has been
// rolled back. Update the rollback offset. // rolled back. Update the rollback offset.
@@ -184,7 +186,7 @@ bool CryptoEngine::SaveOfflineTimeInfo(const std::string& file_path) {
// earlier offline rollback, the rollback offset will be updated in // earlier offline rollback, the rollback offset will be updated in
// LoadOfflineTimeInfo(). It guarantees that the current time to be saved // LoadOfflineTimeInfo(). It guarantees that the current time to be saved
// will never go back. // will never go back.
int64_t current_time = OnlineTime() + offline_time_info_.rollback_offset; const int64_t current_time = MonotonicTime();
// The new previous_time will either stay the same or move forward. // The new previous_time will either stay the same or move forward.
if (current_time > offline_time_info_.previous_time) if (current_time > offline_time_info_.previous_time)
offline_time_info_.previous_time = current_time; offline_time_info_.previous_time = current_time;
@@ -213,8 +215,7 @@ bool CryptoEngine::SaveOfflineTimeInfo(const std::string& file_path) {
file = file_system->Open( file = file_system->Open(
file_path, wvcdm::FileSystem::kCreate | wvcdm::FileSystem::kTruncate); file_path, wvcdm::FileSystem::kCreate | wvcdm::FileSystem::kTruncate);
if (!file) { if (!file) {
LOGE("RollbackCorrectedOfflineTime: File open failed: %s", LOGE("File open failed: %s", file_path.c_str());
file_path.c_str());
return false; return false;
} }
file->Write(reinterpret_cast<char*>(&encrypted_buffer[0]), sizeof(TimeInfo)); file->Write(reinterpret_cast<char*>(&encrypted_buffer[0]), sizeof(TimeInfo));

View File

@@ -104,8 +104,7 @@ class CryptoEngine {
} }
// The OEMCrypto system time. Prevents time rollback. // The OEMCrypto system time. Prevents time rollback.
// TODO(b/145836634): Combine RollbackCorrectedOfflineTime with OnlineTime(). int64_t SystemTime();
int64_t SystemTime() { return RollbackCorrectedOfflineTime(); }
// Verify that this nonce does not collide with another nonce in any session. // Verify that this nonce does not collide with another nonce in any session.
virtual bool NonceCollision(uint32_t nonce); virtual bool NonceCollision(uint32_t nonce);
@@ -224,11 +223,8 @@ class CryptoEngine {
} }
protected: protected:
// System clock, measuring time in seconds. // System clock, measuring time in seconds, including anti-rollback offset.
int64_t OnlineTime(); int64_t MonotonicTime();
// System clock with antirollback protection, measuring time in seconds.
int64_t RollbackCorrectedOfflineTime();
bool LoadOfflineTimeInfo(const std::string& file_path); bool LoadOfflineTimeInfo(const std::string& file_path);
bool SaveOfflineTimeInfo(const std::string& file_path); bool SaveOfflineTimeInfo(const std::string& file_path);