Check for open session when initializing usage table.
[ Merge of http://go/wvgerrit/122984 ] There was an issue encountered by some vendors with how the usage table was initialized on some devices. Previously, the CDM would open an OEMCrypto session first, then initialize the usage table (loading existing or creating a new one). On these devices, OEMCrypto_CreateUsageTableHeader() and OEMCrypto_LoadUsageTableHeader() would fail if there were any open sessions. This CL changes the initialization process to create/load the usage table before opening an OEMCrypto session. This change also lays the ground work for another usage table fix to address GTS tests failure. In the process, several of the functions for the usage table have been split up into smaller chunks of code. This required additional changes to the usage table unittest to keep them up to date. Bug: 169195093 Bug: 180639135 Test: Linux unittests and MediaDrmTest Change-Id: Ifbf35f5d8cff5b89fea9b16edb998c84803f4fbe
This commit is contained in:
@@ -51,8 +51,9 @@
|
||||
}
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
using UsageTableLock = std::unique_lock<std::recursive_mutex>;
|
||||
|
||||
constexpr size_t KiB = 1024;
|
||||
constexpr size_t MiB = 1024 * 1024;
|
||||
|
||||
@@ -166,20 +167,22 @@ size_t GenericEncryptionBlockSize(CdmEncryptionAlgorithm algorithm) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// CryptoSession variables allocation.
|
||||
shared_mutex CryptoSession::static_field_mutex_;
|
||||
shared_mutex CryptoSession::oem_crypto_mutex_;
|
||||
bool CryptoSession::initialized_ = false;
|
||||
int CryptoSession::session_count_ = 0;
|
||||
int CryptoSession::termination_counter_ = 0;
|
||||
UsageTableHeader* CryptoSession::usage_table_header_l1_ = nullptr;
|
||||
UsageTableHeader* CryptoSession::usage_table_header_l3_ = nullptr;
|
||||
std::unique_ptr<UsageTableHeader> CryptoSession::usage_table_header_l1_;
|
||||
std::unique_ptr<UsageTableHeader> CryptoSession::usage_table_header_l3_;
|
||||
std::recursive_mutex CryptoSession::usage_table_mutex_;
|
||||
std::atomic<uint64_t> CryptoSession::request_id_index_source_(0);
|
||||
|
||||
size_t GetOffset(std::string message, std::string field) {
|
||||
size_t pos = message.find(field);
|
||||
if (pos == std::string::npos) {
|
||||
LOGE("Cannot find the |field| offset in message: field = %s",
|
||||
field.c_str());
|
||||
IdToString(field));
|
||||
pos = 0;
|
||||
}
|
||||
return pos;
|
||||
@@ -195,7 +198,7 @@ OEMCrypto_Substring GetSubstring(const std::string& message,
|
||||
size_t pos = message.find(field);
|
||||
if (pos == std::string::npos) {
|
||||
LOGW("Cannot find the |field| substring in message: field = %s",
|
||||
field.c_str());
|
||||
IdToString(field));
|
||||
substring.offset = 0;
|
||||
substring.length = 0;
|
||||
} else {
|
||||
@@ -359,7 +362,7 @@ void CryptoSession::Init() {
|
||||
|
||||
bool CryptoSession::TryTerminate() {
|
||||
LOGV("Terminating crypto session");
|
||||
WithStaticFieldWriteLock("TryTerminate", [&] {
|
||||
const bool terminated = WithStaticFieldWriteLock("TryTerminate", [&] {
|
||||
LOGV(
|
||||
"Terminating crypto session: initialized_ = %s, session_count_ = %d, "
|
||||
"termination_counter_ = %d",
|
||||
@@ -371,25 +374,20 @@ bool CryptoSession::TryTerminate() {
|
||||
if (session_count_ > 0 || termination_counter_ > 0 || !initialized_)
|
||||
return false;
|
||||
|
||||
OEMCryptoResult sts;
|
||||
WithOecWriteLock("Terminate", [&] { sts = OEMCrypto_Terminate(); });
|
||||
const OEMCryptoResult sts =
|
||||
WithOecWriteLock("Terminate", [&] { return OEMCrypto_Terminate(); });
|
||||
if (OEMCrypto_SUCCESS != sts) {
|
||||
LOGE("OEMCrypto_Terminate failed: status = %d", static_cast<int>(sts));
|
||||
}
|
||||
|
||||
if (usage_table_header_l1_ != nullptr) {
|
||||
delete usage_table_header_l1_;
|
||||
usage_table_header_l1_ = nullptr;
|
||||
}
|
||||
if (usage_table_header_l3_ != nullptr) {
|
||||
delete usage_table_header_l3_;
|
||||
usage_table_header_l3_ = nullptr;
|
||||
}
|
||||
|
||||
initialized_ = false;
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
if (terminated) {
|
||||
UsageTableLock lock(usage_table_mutex_);
|
||||
usage_table_header_l1_.reset();
|
||||
usage_table_header_l3_.reset();
|
||||
}
|
||||
return terminated;
|
||||
}
|
||||
|
||||
void CryptoSession::DisableDelayedTermination() {
|
||||
@@ -398,7 +396,63 @@ void CryptoSession::DisableDelayedTermination() {
|
||||
[&] { termination_counter_ = 0; });
|
||||
}
|
||||
|
||||
bool CryptoSession::SetUpUsageTableHeader(
|
||||
SecurityLevel requested_security_level) {
|
||||
if (usage_table_header_ != nullptr) {
|
||||
LOGE("Usage table is already set up for the current crypto session");
|
||||
return false;
|
||||
}
|
||||
const CdmSecurityLevel security_level =
|
||||
GetSecurityLevel(requested_security_level);
|
||||
if (security_level != kSecurityLevelL1 &&
|
||||
security_level != kSecurityLevelL3) {
|
||||
LOGD("Unsupported security level for usage support: security_level = %d",
|
||||
static_cast<int>(security_level));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if usage support is available.
|
||||
bool supports_usage_table = false;
|
||||
if (!HasUsageInfoSupport(requested_security_level, &supports_usage_table)) {
|
||||
metrics_->oemcrypto_usage_table_support_.SetError(
|
||||
USAGE_INFORMATION_SUPPORT_FAILED);
|
||||
return false;
|
||||
}
|
||||
metrics_->oemcrypto_usage_table_support_.Record(
|
||||
supports_usage_table ? kUsageEntrySupport : kNonSecureUsageSupport);
|
||||
if (!supports_usage_table) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGV("Usage table lock: SetUpUsageTableHeader()");
|
||||
UsageTableLock auto_lock(usage_table_mutex_);
|
||||
// TODO(b/141350978): Prevent any recursive logic.
|
||||
|
||||
// Manipulate only the usage table for the requested security level.
|
||||
std::unique_ptr<UsageTableHeader>& header = security_level == kSecurityLevelL1
|
||||
? usage_table_header_l1_
|
||||
: usage_table_header_l3_;
|
||||
if (!header) {
|
||||
// This may be called twice within the same thread when the table
|
||||
// is initialized. On the second call |header| will not be null,
|
||||
// causing this block to be skipped.
|
||||
header.reset(new UsageTableHeader());
|
||||
if (!header->Init(security_level, this)) {
|
||||
LOGE("Failed to initialize and sync usage usage table");
|
||||
// Must be cleared globally to prevent the next session to be
|
||||
// opened from using the invalid UsageTableHeader.
|
||||
header.reset();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
usage_table_header_ = header.get();
|
||||
metrics_->usage_table_header_initial_size_.Record(
|
||||
usage_table_header_->size());
|
||||
return true;
|
||||
}
|
||||
|
||||
CdmResponseType CryptoSession::GetTokenFromKeybox(std::string* token) {
|
||||
RETURN_IF_UNINITIALIZED(CRYPTO_SESSION_NOT_INITIALIZED);
|
||||
RETURN_IF_NULL(token, PARAMETER_NULL);
|
||||
|
||||
std::string temp_buffer(KEYBOX_KEY_DATA_SIZE, '\0');
|
||||
@@ -422,6 +476,7 @@ CdmResponseType CryptoSession::GetTokenFromKeybox(std::string* token) {
|
||||
}
|
||||
|
||||
CdmResponseType CryptoSession::GetTokenFromOemCert(std::string* token) {
|
||||
RETURN_IF_UNINITIALIZED(CRYPTO_SESSION_NOT_INITIALIZED);
|
||||
RETURN_IF_NULL(token, PARAMETER_NULL);
|
||||
|
||||
OEMCryptoResult status;
|
||||
@@ -752,9 +807,16 @@ uint8_t CryptoSession::GetSecurityPatchLevel() {
|
||||
CdmResponseType CryptoSession::Open(SecurityLevel requested_security_level) {
|
||||
LOGD("Opening crypto session: requested_security_level = %s",
|
||||
SecurityLevelToString(requested_security_level));
|
||||
RETURN_IF_UNINITIALIZED(UNKNOWN_ERROR);
|
||||
RETURN_IF_UNINITIALIZED(CRYPTO_SESSION_NOT_INITIALIZED);
|
||||
if (open_) return NO_ERROR;
|
||||
|
||||
if (!SetUpUsageTableHeader(requested_security_level)) {
|
||||
// Ignore errors since we do not know when a session is opened,
|
||||
// if it is intended to be used for offline/usage session related
|
||||
// or otherwise.
|
||||
LOGW("Session opened without a usage table");
|
||||
}
|
||||
|
||||
CdmResponseType result = GetProvisioningMethod(requested_security_level,
|
||||
&pre_provision_token_type_);
|
||||
if (result != NO_ERROR) return result;
|
||||
@@ -817,70 +879,10 @@ CdmResponseType CryptoSession::Open(SecurityLevel requested_security_level) {
|
||||
key_session_.reset(new ContentKeySession(oec_session_id_, metrics_));
|
||||
});
|
||||
|
||||
// Set up Usage Table support
|
||||
//
|
||||
// This MUST be the last thing in the function because it contains the
|
||||
// successful return paths of the function. It will attempt to initialize
|
||||
// usage tables. However, whether they are employed is determined by
|
||||
// information in the license, which will not be received until later. In case
|
||||
// usage tables are not even needed, initialization errors here are ignored by
|
||||
// returning successfully.
|
||||
//
|
||||
// TODO(b/141350978): Refactor this code so that it does not have this
|
||||
// problem.
|
||||
if (!GetApiVersion(&api_version_)) {
|
||||
LOGE("Failed to get API version");
|
||||
return USAGE_SUPPORT_GET_API_FAILED;
|
||||
}
|
||||
|
||||
bool supports_usage_table = false;
|
||||
if (HasUsageInfoSupport(&supports_usage_table)) {
|
||||
metrics_->oemcrypto_usage_table_support_.Record(
|
||||
supports_usage_table ? kUsageEntrySupport : kNonSecureUsageSupport);
|
||||
if (supports_usage_table) {
|
||||
CdmSecurityLevel security_level = GetSecurityLevel();
|
||||
if (security_level == kSecurityLevelL1 ||
|
||||
security_level == kSecurityLevelL3) {
|
||||
// This block cannot use |WithStaticFieldWriteLock| because it needs
|
||||
// to unlock the lock partway through.
|
||||
LOGV("Static field write lock: Open() initializing usage table");
|
||||
std::unique_lock<shared_mutex> auto_lock(static_field_mutex_);
|
||||
|
||||
UsageTableHeader** header = security_level == kSecurityLevelL1
|
||||
? &usage_table_header_l1_
|
||||
: &usage_table_header_l3_;
|
||||
if (*header == nullptr) {
|
||||
*header = new UsageTableHeader();
|
||||
// Ignore errors since we do not know when a session is opened,
|
||||
// if it is intended to be used for offline/usage session related
|
||||
// or otherwise.
|
||||
auto_lock.unlock();
|
||||
bool is_usage_table_header_inited =
|
||||
(*header)->Init(security_level, this);
|
||||
auto_lock.lock();
|
||||
if (!is_usage_table_header_inited) {
|
||||
delete *header;
|
||||
*header = nullptr;
|
||||
usage_table_header_ = nullptr;
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
usage_table_header_ = *header;
|
||||
metrics_->usage_table_header_initial_size_.Record((*header)->size());
|
||||
} // End |static_field_mutex_| block.
|
||||
}
|
||||
} else {
|
||||
metrics_->oemcrypto_usage_table_support_.SetError(
|
||||
USAGE_INFORMATION_SUPPORT_FAILED);
|
||||
}
|
||||
|
||||
// Do not add logic after this point as it may never get exercised. In the
|
||||
// event of errors initializing the usage tables, the code will return early,
|
||||
// never reaching this point. See the comment above the "Set up Usage Table
|
||||
// support" section for details.
|
||||
//
|
||||
// TODO(b/139973602): Refactor this code.
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
@@ -889,13 +891,13 @@ void CryptoSession::Close() {
|
||||
open_ ? "true" : "false");
|
||||
if (!open_) return;
|
||||
|
||||
OEMCryptoResult close_sts;
|
||||
WithOecWriteLock(
|
||||
"Close", [&] { close_sts = OEMCrypto_CloseSession(oec_session_id_); });
|
||||
const OEMCryptoResult close_sts = WithOecWriteLock(
|
||||
"Close", [&] { return OEMCrypto_CloseSession(oec_session_id_); });
|
||||
metrics_->oemcrypto_close_session_.Increment(close_sts);
|
||||
|
||||
// Clear cached values.
|
||||
has_usage_info_support_ = kBooleanUnset;
|
||||
oem_token_.clear();
|
||||
|
||||
if (close_sts != OEMCrypto_SUCCESS) {
|
||||
LOGW("OEMCrypto_CloseSession failed: status = %d",
|
||||
@@ -905,6 +907,7 @@ void CryptoSession::Close() {
|
||||
case OEMCrypto_SUCCESS:
|
||||
case OEMCrypto_ERROR_INVALID_SESSION:
|
||||
case OEMCrypto_ERROR_SYSTEM_INVALIDATED:
|
||||
usage_table_header_ = nullptr;
|
||||
open_ = false;
|
||||
break;
|
||||
case OEMCrypto_ERROR_CLOSE_SESSION_FAILED:
|
||||
@@ -2488,14 +2491,13 @@ CdmResponseType CryptoSession::LoadUsageTableHeader(
|
||||
LOGV("Loading usage table header: requested_security_level = %s",
|
||||
SecurityLevelToString(requested_security_level));
|
||||
|
||||
OEMCryptoResult result;
|
||||
WithOecWriteLock("LoadUsageTableHeader", [&] {
|
||||
result = OEMCrypto_LoadUsageTableHeader(
|
||||
const OEMCryptoResult result = WithOecWriteLock("LoadUsageTableHeader", [&] {
|
||||
return OEMCrypto_LoadUsageTableHeader(
|
||||
requested_security_level,
|
||||
reinterpret_cast<const uint8_t*>(usage_table_header.data()),
|
||||
usage_table_header.size());
|
||||
metrics_->oemcrypto_load_usage_table_header_.Increment(result);
|
||||
});
|
||||
metrics_->oemcrypto_load_usage_table_header_.Increment(result);
|
||||
|
||||
if (result != OEMCrypto_SUCCESS) {
|
||||
if (result == OEMCrypto_WARNING_GENERATION_SKEW) {
|
||||
|
||||
@@ -15,12 +15,9 @@
|
||||
|
||||
namespace wvcdm {
|
||||
namespace {
|
||||
std::string kEmptyString;
|
||||
wvcdm::CdmKeySetId kDummyKeySetId = "DummyKsid";
|
||||
std::string kOldUsageEntryServerMacKey(wvcdm::MAC_KEY_SIZE, 0);
|
||||
std::string kOldUsageEntryClientMacKey(wvcdm::MAC_KEY_SIZE, 0);
|
||||
std::string kOldUsageEntryPoviderSessionToken =
|
||||
"nahZ6achSheiqua3TohQuei0ahwohv";
|
||||
const std::string kEmptyString;
|
||||
const wvcdm::CdmKeySetId kDummyKeySetId = "DummyKsid";
|
||||
|
||||
constexpr int64_t kDefaultExpireDuration = 33 * 24 * 60 * 60; // 33 Days
|
||||
// Fraction of table capacity of number of unexpired offline licenses
|
||||
// before they are considered to be removed. This could occur if
|
||||
@@ -141,13 +138,19 @@ bool EntryIsOfflineLicense(const CdmUsageEntryInfo& info) {
|
||||
// Used for stl filters.
|
||||
return info.storage_type == kStorageLicense;
|
||||
}
|
||||
|
||||
bool IsValidCdmSecurityLevelForUsageInfo(CdmSecurityLevel security_level) {
|
||||
return security_level == kSecurityLevelL1 ||
|
||||
security_level == kSecurityLevelL3;
|
||||
}
|
||||
|
||||
SecurityLevel CdmSecurityLevelToRequestedLevel(
|
||||
CdmSecurityLevel security_level) {
|
||||
return security_level == kSecurityLevelL3 ? kLevel3 : kLevelDefault;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
UsageTableHeader::UsageTableHeader()
|
||||
: security_level_(kSecurityLevelUninitialized),
|
||||
requested_security_level_(kLevelDefault),
|
||||
is_inited_(false),
|
||||
clock_ref_(&clock_) {
|
||||
UsageTableHeader::UsageTableHeader() : clock_ref_(&clock_) {
|
||||
file_system_.reset(new FileSystem());
|
||||
device_files_.reset(new DeviceFiles(file_system_.get()));
|
||||
}
|
||||
@@ -159,152 +162,89 @@ bool UsageTableHeader::Init(CdmSecurityLevel security_level,
|
||||
LOGE("No crypto session provided");
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (security_level) {
|
||||
case kSecurityLevelL1:
|
||||
case kSecurityLevelL3:
|
||||
break;
|
||||
default:
|
||||
LOGE("Invalid security level provided: security_level = %d",
|
||||
static_cast<int>(security_level));
|
||||
return false;
|
||||
if (is_initialized_) {
|
||||
LOGE("Cannot reinitialize usage table: security_level = %s",
|
||||
CdmSecurityLevelToString(security_level));
|
||||
return false;
|
||||
}
|
||||
if (!IsValidCdmSecurityLevelForUsageInfo(security_level)) {
|
||||
LOGE("Invalid security level provided: security_level = %d",
|
||||
static_cast<int>(security_level));
|
||||
return false;
|
||||
}
|
||||
|
||||
security_level_ = security_level;
|
||||
requested_security_level_ =
|
||||
security_level_ == kSecurityLevelL3 ? kLevel3 : kLevelDefault;
|
||||
requested_security_level_ = CdmSecurityLevelToRequestedLevel(security_level);
|
||||
|
||||
if (!crypto_session->GetMaximumUsageTableEntries(
|
||||
requested_security_level_, &potential_table_capacity_)) {
|
||||
LOGW(
|
||||
"Could not determine usage table capacity, assuming default: "
|
||||
"default = %zu",
|
||||
kMinimumUsageTableEntriesSupported);
|
||||
potential_table_capacity_ = kMinimumUsageTableEntriesSupported;
|
||||
} else if (potential_table_capacity_ == 0) {
|
||||
LOGD("Usage table capacity is unlimited: security_level = %s",
|
||||
CdmSecurityLevelToString(security_level));
|
||||
} else if (potential_table_capacity_ < kMinimumUsageTableEntriesSupported) {
|
||||
LOGW(
|
||||
"Reported usage table capacity is smaller than minimally required: "
|
||||
"capacity = %zu, minimum = %zu",
|
||||
potential_table_capacity_, kMinimumUsageTableEntriesSupported);
|
||||
potential_table_capacity_ = kMinimumUsageTableEntriesSupported;
|
||||
} else {
|
||||
LOGD("Usage table capacity: %zu, security_level = %s",
|
||||
potential_table_capacity_, CdmSecurityLevelToString(security_level));
|
||||
if (!OpenSessionCheck(crypto_session)) {
|
||||
return false;
|
||||
}
|
||||
if (!DetermineTableCapacity(crypto_session)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!device_files_->Init(security_level)) {
|
||||
LOGE("Failed to initialize device files");
|
||||
return false;
|
||||
}
|
||||
// Attempt restoring first, if unable to restore, then create a new
|
||||
// table.
|
||||
return RestoreTable(crypto_session) || CreateNewTable(crypto_session);
|
||||
}
|
||||
|
||||
CdmResponseType status = USAGE_INFO_NOT_FOUND;
|
||||
metrics::CryptoMetrics* metrics = crypto_session->GetCryptoMetrics();
|
||||
if (metrics == nullptr) metrics = &alternate_crypto_metrics_;
|
||||
|
||||
bool UsageTableHeader::RestoreTable(CryptoSession* const crypto_session) {
|
||||
bool run_lru_upgrade = false;
|
||||
if (device_files_->RetrieveUsageTableInfo(
|
||||
if (!device_files_->RetrieveUsageTableInfo(
|
||||
&usage_table_header_, &usage_entry_info_, &run_lru_upgrade)) {
|
||||
LOGI("Number of usage entries: %zu", usage_entry_info_.size());
|
||||
status = crypto_session->LoadUsageTableHeader(requested_security_level_,
|
||||
usage_table_header_);
|
||||
LOGW("Could not retrieve usage table");
|
||||
return false;
|
||||
}
|
||||
LOGI("Found usage table to restore: entry_count = %zu",
|
||||
usage_entry_info_.size());
|
||||
|
||||
bool lru_success = true;
|
||||
if (status == NO_ERROR && run_lru_upgrade) {
|
||||
// If the loaded table info does not contain LRU information, then
|
||||
// the information must be added immediately before being used.
|
||||
if (!LruUpgradeAllUsageEntries()) {
|
||||
LOGE(
|
||||
"Unable to init usage table header: "
|
||||
"Failed to perform LRU upgrade to usage entry table");
|
||||
lru_success = false;
|
||||
}
|
||||
}
|
||||
|
||||
// If the usage table header has been successfully loaded, and is
|
||||
// at minimum capacity (>200) or the table size does not have a
|
||||
// hard limit, we need to make sure we can still add and remove
|
||||
// entries. If not, clear files/data and recreate usage header
|
||||
// table.
|
||||
if (status == NO_ERROR && lru_success) {
|
||||
if ((HasUnlimitedTableCapacity() &&
|
||||
usage_entry_info_.size() > kMinimumUsageTableEntriesSupported) ||
|
||||
(!HasUnlimitedTableCapacity() &&
|
||||
usage_entry_info_.size() > potential_table_capacity())) {
|
||||
LOGD("Checking if new entry can be added: size = %zu, capacity = %s",
|
||||
usage_entry_info_.size(),
|
||||
HasUnlimitedTableCapacity()
|
||||
? "unlimited"
|
||||
: std::to_string(potential_table_capacity()).c_str());
|
||||
uint32_t temporary_usage_entry_number;
|
||||
|
||||
// Create a new temporary usage entry, close the session and then
|
||||
// try to delete it.
|
||||
CdmResponseType result = NO_ERROR;
|
||||
{
|
||||
// |local_crypto_session| points to an object whose scope is this
|
||||
// method or a test object whose scope is the lifetime of this class
|
||||
std::unique_ptr<CryptoSession> scoped_crypto_session;
|
||||
CryptoSession* local_crypto_session = test_crypto_session_.get();
|
||||
if (local_crypto_session == nullptr) {
|
||||
scoped_crypto_session.reset(
|
||||
CryptoSession::MakeCryptoSession(metrics));
|
||||
local_crypto_session = scoped_crypto_session.get();
|
||||
}
|
||||
|
||||
result = local_crypto_session->Open(requested_security_level_);
|
||||
if (result == NO_ERROR) {
|
||||
result = AddEntry(local_crypto_session, true, kDummyKeySetId,
|
||||
kEmptyString, kEmptyString,
|
||||
&temporary_usage_entry_number);
|
||||
}
|
||||
}
|
||||
if (result == NO_ERROR) {
|
||||
result = InvalidateEntry(temporary_usage_entry_number,
|
||||
/* defrag_table = */ true,
|
||||
device_files_.get(), metrics);
|
||||
if (usage_entry_info_.size() > temporary_usage_entry_number) {
|
||||
// The entry should have been deleted from the usage table,
|
||||
// not just marked as type unknown. Failure to call
|
||||
// Shrink() may be an indicator of other issues.
|
||||
LOGE("Temporary entry was not deleted");
|
||||
result = UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
if (result != NO_ERROR) {
|
||||
LOGE(
|
||||
"Unable to create/delete new entry, clearing usage entries: "
|
||||
"security_level = %s, usage_entry_count = %zu",
|
||||
CdmSecurityLevelToString(security_level),
|
||||
usage_entry_info_.size());
|
||||
status = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (status != NO_ERROR || !lru_success) {
|
||||
LOGE("Failed to load usage table: security_level = %s, status = %d",
|
||||
CdmSecurityLevelToString(security_level), static_cast<int>(status));
|
||||
device_files_->DeleteAllLicenses();
|
||||
device_files_->DeleteAllUsageInfo();
|
||||
device_files_->DeleteUsageTableInfo();
|
||||
usage_entry_info_.clear();
|
||||
usage_table_header_.clear();
|
||||
status = crypto_session->CreateUsageTableHeader(requested_security_level_,
|
||||
&usage_table_header_);
|
||||
if (status != NO_ERROR) return false;
|
||||
StoreTable(device_files_.get());
|
||||
}
|
||||
} else {
|
||||
status = crypto_session->CreateUsageTableHeader(requested_security_level_,
|
||||
&usage_table_header_);
|
||||
if (status != NO_ERROR) return false;
|
||||
StoreTable(device_files_.get());
|
||||
const CdmResponseType status = crypto_session->LoadUsageTableHeader(
|
||||
requested_security_level_, usage_table_header_);
|
||||
if (status != NO_ERROR) {
|
||||
LOGE("Failed to load usage table header: sts = %d",
|
||||
static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
is_inited_ = true;
|
||||
// If the saved usage entries/meta data is missing LRU information,
|
||||
// then the entries and their meta data must be updated.
|
||||
if (run_lru_upgrade && !LruUpgradeAllUsageEntries()) {
|
||||
LOGE("Failed to perform LRU upgrade to usage entry table");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CapacityCheck(crypto_session)) {
|
||||
LOGE("Cannot restore table due to failing capacity check");
|
||||
return false;
|
||||
}
|
||||
is_initialized_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsageTableHeader::CreateNewTable(CryptoSession* const crypto_session) {
|
||||
LOGD("Removing all usage table files");
|
||||
// Existing files need to be deleted to avoid attempts to restore
|
||||
// licenses which no longer have a usage entry.
|
||||
device_files_->DeleteAllLicenses();
|
||||
device_files_->DeleteAllUsageInfo();
|
||||
device_files_->DeleteUsageTableInfo();
|
||||
usage_entry_info_.clear();
|
||||
usage_table_header_.clear();
|
||||
|
||||
const CdmResponseType status = crypto_session->CreateUsageTableHeader(
|
||||
requested_security_level_, &usage_table_header_);
|
||||
if (status != NO_ERROR) {
|
||||
LOGE("Failed to create new usage table header");
|
||||
return false;
|
||||
}
|
||||
if (!StoreTable(device_files_.get())) {
|
||||
LOGE("Failed to store new usage table header");
|
||||
return false;
|
||||
}
|
||||
is_initialized_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -395,7 +335,7 @@ CdmResponseType UsageTableHeader::AddEntry(
|
||||
return status;
|
||||
}
|
||||
|
||||
LOGI("New usage entry: usage_entry_number = %u", *usage_entry_number);
|
||||
LOGI("usage_entry_number = %u", *usage_entry_number);
|
||||
StoreTable(device_files_.get());
|
||||
return NO_ERROR;
|
||||
}
|
||||
@@ -508,6 +448,105 @@ size_t UsageTableHeader::OfflineEntryCount() const {
|
||||
EntryIsOfflineLicense);
|
||||
}
|
||||
|
||||
bool UsageTableHeader::OpenSessionCheck(CryptoSession* const crypto_session) {
|
||||
// The UsageTableHeader for the specified |requested_security_level_|
|
||||
// MUST be initialized before any sessions are opened.
|
||||
size_t session_count = 0;
|
||||
const CdmResponseType status = crypto_session->GetNumberOfOpenSessions(
|
||||
requested_security_level_, &session_count);
|
||||
if (status != NO_ERROR || session_count > 0) {
|
||||
LOGE(
|
||||
"Cannot initialize usage table header with open crypto session: "
|
||||
"status = %d, count = %zu",
|
||||
static_cast<int>(status), session_count);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsageTableHeader::CapacityCheck(CryptoSession* const crypto_session) {
|
||||
// If the table is around capacity or if unlimited and the table is
|
||||
// larger than the minimally required capacity, then a test must be
|
||||
// performed to ensure that the usage table is not in a state which
|
||||
// will prevent create operations.
|
||||
const size_t capacity_threshold = HasUnlimitedTableCapacity()
|
||||
? kMinimumUsageTableEntriesSupported
|
||||
: potential_table_capacity();
|
||||
if (usage_entry_info_.size() <= capacity_threshold) {
|
||||
// No need to perform test if below capacity.
|
||||
return true;
|
||||
}
|
||||
|
||||
metrics::CryptoMetrics* metrics = crypto_session->GetCryptoMetrics();
|
||||
if (metrics == nullptr) metrics = &alternate_crypto_metrics_;
|
||||
// |local_crypto_session| points to an object whose scope is this
|
||||
// method or a test object whose scope is the lifetime of this class
|
||||
CryptoSession* local_crypto_session = test_crypto_session_.get();
|
||||
std::unique_ptr<CryptoSession> scoped_crypto_session;
|
||||
if (local_crypto_session == nullptr) {
|
||||
scoped_crypto_session.reset(CryptoSession::MakeCryptoSession(metrics));
|
||||
local_crypto_session = scoped_crypto_session.get();
|
||||
}
|
||||
|
||||
CdmResponseType status =
|
||||
local_crypto_session->Open(requested_security_level_);
|
||||
if (status != NO_ERROR) {
|
||||
LOGE("Failed to open crypto session for capacity test: sts = %d",
|
||||
static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t temporary_usage_entry_number;
|
||||
status = AddEntry(local_crypto_session, true, kDummyKeySetId, kEmptyString,
|
||||
kEmptyString, &temporary_usage_entry_number);
|
||||
if (status != NO_ERROR) {
|
||||
LOGE("Failed to add entry for capacity test: sts = %d",
|
||||
static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
status =
|
||||
InvalidateEntry(temporary_usage_entry_number,
|
||||
/* defrag_table = */ true, device_files_.get(), metrics);
|
||||
if (status != NO_ERROR) {
|
||||
LOGE("Failed to invalidate entry for capacity test: sts = %d",
|
||||
static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
if (usage_entry_info_.size() > temporary_usage_entry_number) {
|
||||
// The entry should have been deleted from the usage table,
|
||||
// not just marked as type unknown. Failure to call
|
||||
// Shrink() may be an indicator of other issues.
|
||||
LOGE("Failed to shrink table for capacity test");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsageTableHeader::DetermineTableCapacity(CryptoSession* crypto_session) {
|
||||
if (!crypto_session->GetMaximumUsageTableEntries(
|
||||
requested_security_level_, &potential_table_capacity_)) {
|
||||
LOGW(
|
||||
"Could not determine usage table capacity, assuming default: "
|
||||
"default = %zu",
|
||||
kMinimumUsageTableEntriesSupported);
|
||||
potential_table_capacity_ = kMinimumUsageTableEntriesSupported;
|
||||
} else if (potential_table_capacity_ == 0) {
|
||||
LOGD("capacity = unlimited, security_level = %s",
|
||||
CdmSecurityLevelToString(security_level_));
|
||||
} else if (potential_table_capacity_ < kMinimumUsageTableEntriesSupported) {
|
||||
LOGW(
|
||||
"Reported usage table capacity is smaller than minimally required: "
|
||||
"capacity = %zu, minimum = %zu",
|
||||
potential_table_capacity_, kMinimumUsageTableEntriesSupported);
|
||||
potential_table_capacity_ = kMinimumUsageTableEntriesSupported;
|
||||
} else {
|
||||
LOGD("capacity = %zu, security_level = %s", potential_table_capacity_,
|
||||
CdmSecurityLevelToString(security_level_));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CdmResponseType UsageTableHeader::MoveEntry(
|
||||
uint32_t from_usage_entry_number, const CdmUsageEntry& from_usage_entry,
|
||||
uint32_t to_usage_entry_number, DeviceFiles* device_files,
|
||||
|
||||
Reference in New Issue
Block a user