Return error codes when storing or retrieving licenses

[ Merge of http://go/wvgerrit/70386 ]

This allows error codes from device files to be added as sub-errors when
errors such as GET_LICENSE_ERROR are encountered.

Bug: 112357085
Bug: 115382201
Test: WV unit/integration tests

Change-Id: I505a87086ce584efc7e482984c0f132ac5329e16
This commit is contained in:
Rahul Frias
2019-01-14 19:09:37 -08:00
parent cfe7221d9e
commit 8b61a03b5f
6 changed files with 206 additions and 90 deletions

View File

@@ -49,6 +49,13 @@ using video_widevine_client::sdk::
using video_widevine::SignedDrmDeviceCertificate;
using video_widevine::DrmDeviceCertificate;
#define RETURN_FALSE_IF_NULL(PARAM) \
if (PARAM == nullptr) { \
LOGE("|PARAM| not provided"); \
*result = kParameterNull; \
return false; \
}
namespace {
const char kCertificateFileName[] = "cert.bin";
@@ -121,7 +128,8 @@ bool DeviceFiles::StoreCertificate(const std::string& certificate,
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(GetCertificateFileName(), serialized_file);
return
StoreFileWithHash(GetCertificateFileName(), serialized_file) == kNoError;
}
bool DeviceFiles::RetrieveCertificate(std::string* certificate,
@@ -134,7 +142,8 @@ bool DeviceFiles::RetrieveCertificate(std::string* certificate,
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(GetCertificateFileName(), &file)) {
if (RetrieveHashedFile(GetCertificateFileName(), &file) != kNoError) {
LOGW("DeviceFiles::RetrieveCertificate: unable to retrieve file");
return false;
}
@@ -223,9 +232,18 @@ bool DeviceFiles::StoreLicense(
int64_t last_playback_time, int64_t grace_period_end_time,
const CdmAppParameterMap& app_parameters,
const CdmUsageEntry& usage_entry,
const uint32_t usage_entry_number) {
const uint32_t usage_entry_number,
ResponseType* result) {
if (result == nullptr) {
LOGE("DeviceFiles::StoreLicense: |result| not provided");
return false;
}
*result = kNoError;
if (!initialized_) {
LOGW("DeviceFiles::StoreLicense: not initialized");
*result = kObjectNotInitialized;
return false;
}
@@ -245,6 +263,7 @@ bool DeviceFiles::StoreLicense(
break;
default:
LOGW("DeviceFiles::StoreLicense: Unknown license state: %u", state);
*result = kUnknownLicenseState;
return false;
break;
}
@@ -271,7 +290,9 @@ bool DeviceFiles::StoreLicense(
file.SerializeToString(&serialized_file);
reserved_license_ids_.erase(key_set_id);
return StoreFileWithHash(key_set_id + kLicenseFileNameExt, serialized_file);
*result =
StoreFileWithHash(key_set_id + kLicenseFileNameExt, serialized_file);
return *result == kNoError;
}
bool DeviceFiles::RetrieveLicense(
@@ -281,29 +302,54 @@ bool DeviceFiles::RetrieveLicense(
std::string* release_server_url, int64_t* playback_start_time,
int64_t* last_playback_time, int64_t* grace_period_end_time,
CdmAppParameterMap* app_parameters, CdmUsageEntry* usage_entry,
uint32_t* usage_entry_number) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveLicense: not initialized");
uint32_t* usage_entry_number, ResponseType* result) {
if (result == nullptr) {
LOGE("DeviceFiles::RetrieveLicense: |result| not provided");
return false;
}
if (!initialized_) {
LOGW("DeviceFiles::RetrieveLicense: not initialized");
*result = kObjectNotInitialized;
return false;
}
RETURN_FALSE_IF_NULL(state);
RETURN_FALSE_IF_NULL(pssh_data);
RETURN_FALSE_IF_NULL(license_request);
RETURN_FALSE_IF_NULL(license_message);
RETURN_FALSE_IF_NULL(license_renewal_request);
RETURN_FALSE_IF_NULL(license_renewal);
RETURN_FALSE_IF_NULL(release_server_url);
RETURN_FALSE_IF_NULL(playback_start_time);
RETURN_FALSE_IF_NULL(last_playback_time);
RETURN_FALSE_IF_NULL(grace_period_end_time);
RETURN_FALSE_IF_NULL(app_parameters);
RETURN_FALSE_IF_NULL(usage_entry);
RETURN_FALSE_IF_NULL(usage_entry_number);
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(key_set_id + kLicenseFileNameExt, &file)) {
*result = RetrieveHashedFile(key_set_id + kLicenseFileNameExt, &file);
if (*result != kNoError) {
LOGW("DeviceFiles::RetrieveLicense: unable to retrieve file: %d", *result);
return false;
}
if (file.type() != video_widevine_client::sdk::File::LICENSE) {
LOGW("DeviceFiles::RetrieveLicense: Incorrect file type");
*result = kIncorrectFileType;
return false;
}
if (file.version() != video_widevine_client::sdk::File::VERSION_1) {
LOGW("DeviceFiles::RetrieveLicense: Incorrect file version");
*result = kIncorrectFileVersion;
return false;
}
if (!file.has_license()) {
LOGW("DeviceFiles::RetrieveLicense: License not present");
*result = kLicenseNotPresent;
return false;
}
@@ -444,8 +490,8 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
file.set_type(video_widevine_client::sdk::File::USAGE_INFO);
file.set_version(video_widevine_client::sdk::File::VERSION_1);
} else {
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
LOGW("DeviceFiles::StoreUsageInfo: Unable to parse file");
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::StoreUsageInfo: Unable to retrieve file");
return false;
}
}
@@ -463,7 +509,7 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file) == kNoError;
}
bool DeviceFiles::ListUsageIds(
@@ -489,8 +535,8 @@ bool DeviceFiles::ListUsageIds(
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(file_name, &file)) {
LOGW("DeviceFiles::ListUsageRecords: Unable to parse file");
if (RetrieveHashedFile(file_name, &file) != kNoError) {
LOGW("DeviceFiles::ListUsageRecords: Unable to retrieve file");
return false;
}
@@ -531,8 +577,8 @@ bool DeviceFiles::GetProviderSessionToken(const std::string& app_id,
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(file_name, &file)) {
LOGW("DeviceFiles::GetProviderSessionToken: unable to parse file");
if (RetrieveHashedFile(file_name, &file) != kNoError) {
LOGW("DeviceFiles::GetProviderSessionToken: unable to retrieve file");
return false;
}
@@ -553,7 +599,10 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& usage_info_file_name,
return false;
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) return false;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::DeleteUsageInfo: Unable to retrieve file");
return false;
}
UsageInfo* usage_info = file.mutable_usage_info();
int index = 0;
@@ -582,7 +631,7 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& usage_info_file_name,
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file) == kNoError;
}
bool DeviceFiles::DeleteAllUsageInfoForApp(
@@ -601,7 +650,7 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
if (!FileExists(usage_info_file_name)) return true;
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file)) {
if (RetrieveHashedFile(usage_info_file_name, &file) == kNoError) {
for (int i = 0; i < file.usage_info().sessions_size(); ++i) {
provider_session_tokens->push_back(file.usage_info().sessions(i).token());
}
@@ -642,8 +691,8 @@ bool DeviceFiles::RetrieveUsageInfo(
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
LOGW("DeviceFiles::RetrieveUsageInfo: Unable to parse file");
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: Unable to retrieve file");
return false;
}
@@ -669,7 +718,8 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
return false;
}
@@ -702,7 +752,8 @@ bool DeviceFiles::RetrieveUsageInfoByKeySetId(
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfoByKeySetId: unable to retrieve file");
return false;
}
@@ -751,7 +802,7 @@ bool DeviceFiles::StoreUsageInfo(const std::string& usage_info_file_name,
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file) == kNoError;
}
bool DeviceFiles::UpdateUsageInfo(const std::string& usage_info_file_name,
@@ -769,8 +820,8 @@ bool DeviceFiles::UpdateUsageInfo(const std::string& usage_info_file_name,
}
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
LOGW("DeviceFiles::UpdateUsageInfo: Unable to parse file");
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::UpdateUsageInfo: Unable to retrieve file");
return false;
}
@@ -788,7 +839,8 @@ bool DeviceFiles::UpdateUsageInfo(const std::string& usage_info_file_name,
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(usage_info_file_name, serialized_file);
return
StoreFileWithHash(usage_info_file_name, serialized_file) == kNoError;
}
}
@@ -814,7 +866,8 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
return false;
}
@@ -848,7 +901,8 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(usage_info_file_name, &file)) {
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
return false;
}
@@ -942,7 +996,7 @@ bool DeviceFiles::StoreHlsAttributes(
file.SerializeToString(&serialized_file);
return StoreFileWithHash(key_set_id + kHlsAttributesFileNameExt,
serialized_file);
serialized_file) == kNoError;
}
bool DeviceFiles::RetrieveHlsAttributes(
@@ -954,7 +1008,9 @@ bool DeviceFiles::RetrieveHlsAttributes(
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(key_set_id + kHlsAttributesFileNameExt, &file)) {
if (RetrieveHashedFile(key_set_id + kHlsAttributesFileNameExt, &file) !=
kNoError) {
LOGW("DeviceFiles::RetrieveHlsAttributes: unable to retrieve file");
return false;
}
@@ -1045,7 +1101,8 @@ bool DeviceFiles::StoreUsageTableInfo(
std::string serialized_file;
file.SerializeToString(&serialized_file);
return StoreFileWithHash(GetUsageTableFileName(), serialized_file);
return
StoreFileWithHash(GetUsageTableFileName(), serialized_file) == kNoError;
}
bool DeviceFiles::RetrieveUsageTableInfo(
@@ -1068,7 +1125,8 @@ bool DeviceFiles::RetrieveUsageTableInfo(
}
video_widevine_client::sdk::File file;
if (!RetrieveHashedFile(GetUsageTableFileName(), &file)) {
if (RetrieveHashedFile(GetUsageTableFileName(), &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: unable to retrieve file");
return false;
}
@@ -1122,13 +1180,14 @@ bool DeviceFiles::DeleteUsageTableInfo() {
return RemoveFile(GetUsageTableFileName());
}
bool DeviceFiles::StoreFileWithHash(const std::string& name,
const std::string& serialized_file) {
DeviceFiles::ResponseType DeviceFiles::StoreFileWithHash(
const std::string& name,
const std::string& serialized_file) {
// calculate SHA hash
std::string hash;
if (!Hash(serialized_file, &hash)) {
LOGW("DeviceFiles::StoreFileWithHash: Hash computation failed");
return false;
return kHashComputationFailed;
}
// Fill in hashed file data
@@ -1142,12 +1201,13 @@ bool DeviceFiles::StoreFileWithHash(const std::string& name,
return StoreFileRaw(name, serialized_hash_file);
}
bool DeviceFiles::StoreFileRaw(const std::string& name,
const std::string& serialized_file) {
DeviceFiles::ResponseType DeviceFiles::StoreFileRaw(
const std::string& name,
const std::string& serialized_file) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::StoreFileRaw: Unable to get base path");
return false;
return kBasePathUnavailable;
}
path += name;
@@ -1156,7 +1216,7 @@ bool DeviceFiles::StoreFileRaw(const std::string& name,
file_system_->Open(path, FileSystem::kCreate | FileSystem::kTruncate);
if (!file) {
LOGW("DeviceFiles::StoreFileRaw: File open failed: %s", path.c_str());
return false;
return kFileOpenFailed;
}
ssize_t bytes = file->Write(serialized_file.data(), serialized_file.size());
@@ -1166,35 +1226,35 @@ bool DeviceFiles::StoreFileRaw(const std::string& name,
"DeviceFiles::StoreFileRaw: write failed: (actual: %d, "
"expected: %d)",
bytes, serialized_file.size());
return false;
return kFileWriteError;
}
LOGV("DeviceFiles::StoreFileRaw: success: %s (%db)", path.c_str(),
serialized_file.size());
return true;
return kNoError;
}
bool DeviceFiles::RetrieveHashedFile(
DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
const std::string& name,
video_widevine_client::sdk::File* deserialized_file) {
std::string serialized_file;
if (!deserialized_file) {
LOGW("DeviceFiles::RetrieveHashedFile: Unspecified file parameter");
return false;
return kParameterNull;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::RetrieveHashedFile: Unable to get base path");
return false;
return kBasePathUnavailable;
}
path += name;
if (!file_system_->Exists(path)) {
LOGW("DeviceFiles::RetrieveHashedFile: %s does not exist", path.c_str());
return false;
return kFileNotFound;
}
ssize_t bytes = file_system_->FileSize(path);
@@ -1204,12 +1264,12 @@ bool DeviceFiles::RetrieveHashedFile(
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return false;
return kInvalidFileSize;
}
auto file = file_system_->Open(path, FileSystem::kReadOnly);
if (!file) {
return false;
return kFileOpenFailed;
}
std::string serialized_hash_file;
@@ -1217,11 +1277,11 @@ bool DeviceFiles::RetrieveHashedFile(
bytes = file->Read(&serialized_hash_file[0], serialized_hash_file.size());
if (bytes != static_cast<ssize_t>(serialized_hash_file.size())) {
LOGW("DeviceFiles::RetrieveHashedFile: read failed");
LOGW("DeviceFiles::RetrieveHashedFile: read failed: %d", bytes);
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return false;
return kFileReadError;
}
LOGV("DeviceFiles::RetrieveHashedFile: success: %s (%db)", path.c_str(),
@@ -1233,13 +1293,13 @@ bool DeviceFiles::RetrieveHashedFile(
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return false;
return kFileParseError1;
}
std::string hash;
if (!Hash(hash_file.file(), &hash)) {
LOGW("DeviceFiles::RetrieveHashedFile: Hash computation failed");
return false;
return kHashComputationFailed;
}
if (hash != hash_file.hash()) {
@@ -1247,7 +1307,7 @@ bool DeviceFiles::RetrieveHashedFile(
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return false;
return kFileHashMismatch;
}
if (!deserialized_file->ParseFromString(hash_file.file())) {
@@ -1255,9 +1315,9 @@ bool DeviceFiles::RetrieveHashedFile(
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return false;
return kFileParseError2;
}
return true;
return kNoError;
}
bool DeviceFiles::FileExists(const std::string& name) {