Update TimeRollbackPrevention unit test am: 75575418d0 am: 7cd14be01d am: 51cf7e4668 am: 40e2e7fa08
Change-Id: I21b29668ed8a9b39e8a7679ed65281d14587bba3
This commit is contained in:
committed by
Automerger Merge Worker
commit
cd4d4f564a
@@ -102,6 +102,7 @@ int64_t CryptoEngine::MonotonicTime() {
|
||||
wvcdm::Clock().GetCurrentTime() + offline_time_info_.rollback_offset;
|
||||
static int64_t then = now;
|
||||
if (now < then) {
|
||||
LOGW("Clock rollback detected: %lld seconds", then - now);
|
||||
offline_time_info_.rollback_offset += then - now;
|
||||
now = then;
|
||||
}
|
||||
@@ -138,31 +139,16 @@ bool CryptoEngine::LoadOfflineTimeInfo(const std::string& file_path) {
|
||||
memset(&offline_time_info_, 0, sizeof(TimeInfo));
|
||||
wvcdm::FileSystem* file_system = file_system_.get();
|
||||
if (file_system->Exists(file_path)) {
|
||||
std::vector<uint8_t> encrypted_buffer(sizeof(TimeInfo));
|
||||
std::vector<uint8_t> clear_buffer(sizeof(TimeInfo));
|
||||
|
||||
KeyboxError error_code = ValidateKeybox();
|
||||
if (error_code != NO_ERROR) {
|
||||
LOGE("Keybox is invalid: %d", error_code);
|
||||
return false;
|
||||
}
|
||||
// Use the device key for encrypt/decrypt.
|
||||
const std::vector<uint8_t>& key = DeviceRootKey();
|
||||
std::unique_ptr<wvcdm::File> file =
|
||||
file_system->Open(file_path, wvcdm::FileSystem::kReadOnly);
|
||||
if (!file) {
|
||||
LOGE("File open failed: %s", file_path.c_str());
|
||||
// This error is expected at first initialization.
|
||||
LOGE("File open failed (this is expected on first initialization): %s",
|
||||
file_path.c_str());
|
||||
return false;
|
||||
}
|
||||
// Load time info from previous call.
|
||||
file->Read(reinterpret_cast<char*>(&encrypted_buffer[0]), sizeof(TimeInfo));
|
||||
// Decrypt the encrypted TimeInfo buffer.
|
||||
AES_KEY aes_key;
|
||||
AES_set_decrypt_key(&key[0], 128, &aes_key);
|
||||
std::vector<uint8_t> iv(wvoec::KEY_IV_SIZE, 0);
|
||||
AES_cbc_encrypt(&encrypted_buffer[0], &clear_buffer[0], sizeof(TimeInfo),
|
||||
&aes_key, iv.data(), AES_DECRYPT);
|
||||
memcpy(&offline_time_info_, &clear_buffer[0], sizeof(TimeInfo));
|
||||
file->Read(reinterpret_cast<char*>(&offline_time_info_), sizeof(TimeInfo));
|
||||
|
||||
// Detect offline time rollback after loading from disk.
|
||||
// Add any time offsets in the past to the current time.
|
||||
@@ -191,34 +177,16 @@ bool CryptoEngine::SaveOfflineTimeInfo(const std::string& file_path) {
|
||||
if (current_time > offline_time_info_.previous_time)
|
||||
offline_time_info_.previous_time = current_time;
|
||||
|
||||
KeyboxError error_code = ValidateKeybox();
|
||||
if (error_code != NO_ERROR) {
|
||||
LOGE("Keybox is invalid: %d", error_code);
|
||||
return false;
|
||||
}
|
||||
// Use the device key for encrypt/decrypt.
|
||||
const std::vector<uint8_t>& key = DeviceRootKey();
|
||||
std::vector<uint8_t> encrypted_buffer(sizeof(TimeInfo));
|
||||
std::vector<uint8_t> clear_buffer(sizeof(TimeInfo));
|
||||
|
||||
// Copy updated data and encrypt the buffer.
|
||||
memcpy(&clear_buffer[0], &offline_time_info_, sizeof(TimeInfo));
|
||||
AES_KEY aes_key;
|
||||
AES_set_encrypt_key(&key[0], 128, &aes_key);
|
||||
std::vector<uint8_t> iv(wvoec::KEY_IV_SIZE, 0);
|
||||
AES_cbc_encrypt(&clear_buffer[0], &encrypted_buffer[0], sizeof(TimeInfo),
|
||||
&aes_key, iv.data(), AES_ENCRYPT);
|
||||
|
||||
std::unique_ptr<wvcdm::File> file;
|
||||
wvcdm::FileSystem* file_system = file_system_.get();
|
||||
// Write the encrypted buffer to disk.
|
||||
// Write the current time and offset to disk.
|
||||
file = file_system->Open(
|
||||
file_path, wvcdm::FileSystem::kCreate | wvcdm::FileSystem::kTruncate);
|
||||
if (!file) {
|
||||
LOGE("File open failed: %s", file_path.c_str());
|
||||
return false;
|
||||
}
|
||||
file->Write(reinterpret_cast<char*>(&encrypted_buffer[0]), sizeof(TimeInfo));
|
||||
file->Write(reinterpret_cast<char*>(&offline_time_info_), sizeof(TimeInfo));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user