Updated cdm_session_fuzzer

Implemented google c++ code style changes for cdm_session_fuzzer

exec/s: 21
Test: ./cdm_session_fuzzer
Bug: 312374669

Change-Id: I4dd20d3120876dd12b599ebf6fe8aa43dc0207f9
This commit is contained in:
Onkar Shinde
2023-11-28 12:06:37 +00:00
committed by Aditya Wazir
parent f3c22ff3f1
commit 02441b620f

View File

@@ -55,65 +55,65 @@ class FuzzEventListener : public WvCdmEventListener {
}; };
class CdmSessionFuzzer { class CdmSessionFuzzer {
public: public:
CdmSessionFuzzer(const uint8_t *data, size_t size) : mFdp(data, size){}; CdmSessionFuzzer(const uint8_t *data, size_t size) : fdp_(data, size){};
void process(); void Process();
private: private:
FuzzedDataProvider mFdp; FuzzedDataProvider fdp_;
void invokeCdmSessionAPIs(CdmSession *cdmSession); void InvokeCdmSessionAPIs(CdmSession *cdm_session);
void invokeCdmSessionMapAPIs(); void InvokeCdmSessionMapAPIs();
CdmResponseType CdmResponseType
initCdmSession(CdmSession *cdmSession, InitCdmSession(CdmSession *cdm_session,
FuzzCdmClientPropertySet *fuzzCdmClientPropertySet, FuzzCdmClientPropertySet *fuzz_cdm_client_property_set,
CdmSessionId forcedsessionId, CdmSessionId forced_session_id,
FuzzEventListener *fuzzeventListener, bool forcedLevel); FuzzEventListener *fuzz_event_listener, bool forced_level);
void createResponse(CdmKeyResponse *response); void CreateResponse(CdmKeyResponse *response);
void setLicenseMsg(std::string *msg); void SetLicenseMsg(std::string *msg);
void storeLicense(DeviceFiles::CdmLicenseData licenseData); void StoreLicense(DeviceFiles::CdmLicenseData license_data);
}; };
class FuzzFile : public File { class FuzzFile : public File {
public: public:
FuzzFile(FuzzedDataProvider *fdp) : mFdp(fdp) {} FuzzFile(FuzzedDataProvider *fdp) : fdp_(fdp) {}
ssize_t Read(char *buffer, size_t bytes) { ssize_t Read(char *buffer, size_t bytes) {
std::vector<char> initData = mFdp->ConsumeBytes<char>( std::vector<char> init_data = fdp_->ConsumeBytes<char>(
mFdp->ConsumeIntegralInRange<size_t>(kMinByte, bytes)); fdp_->ConsumeIntegralInRange<size_t>(kMinByte, bytes));
buffer = initData.data(); buffer = init_data.data();
return initData.size(); return init_data.size();
}; };
ssize_t Write(const char * /* buffer*/, size_t bytes) { ssize_t Write(const char * /* buffer*/, size_t bytes) {
return mFdp->ConsumeIntegralInRange<ssize_t>(kMinByte, bytes); return fdp_->ConsumeIntegralInRange<ssize_t>(kMinByte, bytes);
}; };
private: private:
FuzzedDataProvider *mFdp; FuzzedDataProvider *fdp_;
}; };
class FuzzFileSystem : public FileSystem { class FuzzFileSystem : public FileSystem {
public: public:
FuzzFileSystem(FuzzedDataProvider *fdp) : mFdp(fdp) {} FuzzFileSystem(FuzzedDataProvider *fdp) : fdp_(fdp) {}
std::unique_ptr<File> Open(const std::string &, int) { std::unique_ptr<File> Open(const std::string &, int) {
return std::unique_ptr<File>(new FuzzFile(mFdp)); return std::unique_ptr<File>(new FuzzFile(fdp_));
} }
bool Exists(const std::string &) { return mFdp->ConsumeBool(); } bool Exists(const std::string &) { return fdp_->ConsumeBool(); }
bool Remove(const std::string &) { return mFdp->ConsumeBool(); } bool Remove(const std::string &) { return fdp_->ConsumeBool(); }
ssize_t FileSize(const std::string &) { ssize_t FileSize(const std::string &) {
return mFdp->ConsumeIntegralInRange<ssize_t>(kMinByte, kMaxByte); return fdp_->ConsumeIntegralInRange<ssize_t>(kMinByte, kMaxByte);
} }
bool List(const std::string &, std::vector<std::string> *) { bool List(const std::string &, std::vector<std::string> *) {
return mFdp->ConsumeBool(); return fdp_->ConsumeBool();
} }
private: private:
FuzzedDataProvider *mFdp; FuzzedDataProvider *fdp_;
}; };
void policySetBool(std::function<void(bool)> function, void policySetBool(std::function<void(bool)> function,
@@ -123,446 +123,446 @@ void policySetBool(std::function<void(bool)> function,
} }
} }
void CdmSessionFuzzer::setLicenseMsg(std::string *msg) { void CdmSessionFuzzer::SetLicenseMsg(std::string *msg) {
video_widevine::License license; video_widevine::License license;
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
std::string provider_client_token = std::string provider_client_token =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
license.set_provider_client_token(provider_client_token); license.set_provider_client_token(provider_client_token);
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
uint32_t scheme = mFdp.ConsumeIntegral<uint32_t>(); uint32_t scheme = fdp_.ConsumeIntegral<uint32_t>();
license.set_protection_scheme(scheme); license.set_protection_scheme(scheme);
} }
video_widevine::License::KeyContainer *container = license.add_key(); video_widevine::License::KeyContainer *container = license.add_key();
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
uint32_t keyType = mFdp.ConsumeIntegralInRange<uint8_t>( uint32_t key_type = fdp_.ConsumeIntegralInRange<uint8_t>(
video_widevine::License::KeyContainer::SIGNING, video_widevine::License::KeyContainer::SIGNING,
video_widevine::License::KeyContainer::ENTITLEMENT); video_widevine::License::KeyContainer::ENTITLEMENT);
container->set_type((video_widevine::License_KeyContainer_KeyType)keyType); container->set_type((video_widevine::License_KeyContainer_KeyType)key_type);
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
container->mutable_iv(); container->mutable_iv();
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
std::string keyString = mFdp.ConsumeRandomLengthString(kMaxByte); std::string key_string = fdp_.ConsumeRandomLengthString(kMaxByte);
container->set_key(keyString); container->set_key(key_string);
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
container->mutable_id(); container->mutable_id();
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
container->mutable_track_label(); container->mutable_track_label();
} }
video_widevine::License_KeyContainer_KeyControl *keyControl = video_widevine::License_KeyContainer_KeyControl *key_control =
container->mutable_key_control(); container->mutable_key_control();
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
keyControl->mutable_key_control_block(); key_control->mutable_key_control_block();
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
keyControl->mutable_iv(); key_control->mutable_iv();
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
video_widevine::License_Policy *policy = license.mutable_policy(); video_widevine::License_Policy *policy = license.mutable_policy();
policySetBool(std::bind(&video_widevine::License_Policy::set_can_persist, policySetBool(std::bind(&video_widevine::License_Policy::set_can_persist,
policy, std::placeholders::_1), policy, std::placeholders::_1),
&mFdp); &fdp_);
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
std::string server_url = mFdp.ConsumeRandomLengthString(kMaxByte); std::string server_url = fdp_.ConsumeRandomLengthString(kMaxByte);
policy->set_renewal_server_url(server_url); policy->set_renewal_server_url(server_url);
} }
policySetBool( policySetBool(
std::bind(&video_widevine::License_Policy::set_always_include_client_id, std::bind(&video_widevine::License_Policy::set_always_include_client_id,
policy, std::placeholders::_1), policy, std::placeholders::_1),
&mFdp); &fdp_);
} }
LicenseIdentification *id = license.mutable_id(); LicenseIdentification *id = license.mutable_id();
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
id->set_type(mFdp.ConsumeBool() ? video_widevine::STREAMING id->set_type(fdp_.ConsumeBool() ? video_widevine::STREAMING
: video_widevine::OFFLINE); : video_widevine::OFFLINE);
} }
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
std::string provider_session_token = std::string provider_session_token =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
id->set_provider_session_token(provider_session_token); id->set_provider_session_token(provider_session_token);
} }
license.SerializeToString(msg); license.SerializeToString(msg);
} }
void CdmSessionFuzzer::createResponse(CdmKeyResponse *response) { void CdmSessionFuzzer::CreateResponse(CdmKeyResponse *response) {
video_widevine::SignedMessage signed_message; video_widevine::SignedMessage signed_message;
video_widevine::SignedMessage type; video_widevine::SignedMessage type;
signed_message.set_type(video_widevine::SignedMessage::LICENSE); signed_message.set_type(video_widevine::SignedMessage::LICENSE);
signed_message.mutable_service_version_info(); signed_message.mutable_service_version_info();
std::string sign = mFdp.ConsumeRandomLengthString(kMaxByte); std::string sign = fdp_.ConsumeRandomLengthString(kMaxByte);
sign.resize(kMaxId, '0'); sign.resize(kMaxId, '0');
signed_message.set_signature(sign); signed_message.set_signature(sign);
std::string key = mFdp.ConsumeRandomLengthString(kMaxByte); std::string key = fdp_.ConsumeRandomLengthString(kMaxByte);
key.resize(kMaxId, '0'); key.resize(kMaxId, '0');
signed_message.set_session_key(key); signed_message.set_session_key(key);
std::string message = mFdp.ConsumeRandomLengthString(kMaxByte); std::string message = fdp_.ConsumeRandomLengthString(kMaxByte);
message.resize(kMaxId, '0'); message.resize(kMaxId, '0');
signed_message.set_oemcrypto_core_message(message); signed_message.set_oemcrypto_core_message(message);
signed_message.set_using_secondary_key(mFdp.ConsumeBool()); signed_message.set_using_secondary_key(fdp_.ConsumeBool());
std::string setMsg = mFdp.ConsumeRandomLengthString(kMaxByte); std::string set_msg = fdp_.ConsumeRandomLengthString(kMaxByte);
signed_message.set_msg(setMsg); signed_message.set_msg(set_msg);
signed_message.SerializeToString(response); signed_message.SerializeToString(response);
} }
CdmResponseType CdmSessionFuzzer::initCdmSession( CdmResponseType CdmSessionFuzzer::InitCdmSession(
CdmSession *cdmSession, FuzzCdmClientPropertySet *fuzzCdmClientPropertySet, CdmSession *cdm_session, FuzzCdmClientPropertySet *fuzz_cdm_client_property_set,
CdmSessionId forcedsessionId, FuzzEventListener *fuzzeventListener, CdmSessionId forced_session_id, FuzzEventListener *fuzz_event_listener,
bool forcedLevel) { bool forced_level) {
CdmResponseType result; CdmResponseType result;
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
result = result =
cdmSession->Init(fuzzCdmClientPropertySet /*cdm_client_property_set*/, cdm_session->Init(fuzz_cdm_client_property_set /*cdm_client_property_set*/,
&forcedsessionId /*forced_session_id*/, &forced_session_id /*forced_session_id*/,
fuzzeventListener /*event_listener*/, forcedLevel); fuzz_event_listener /*event_listener*/, forced_level);
} else { } else {
result = result =
cdmSession->Init(fuzzCdmClientPropertySet /*cdm_client_property_set*/); cdm_session->Init(fuzz_cdm_client_property_set /*cdm_client_property_set*/);
} }
return result; return result;
} }
void CdmSessionFuzzer::storeLicense(DeviceFiles::CdmLicenseData licenseData) { void CdmSessionFuzzer::StoreLicense(DeviceFiles::CdmLicenseData license_data) {
FuzzFileSystem fileSystemdev(&mFdp); FuzzFileSystem file_system_dev(&fdp_);
DeviceFiles deviceFiles(mFdp.ConsumeBool() ? &fileSystemdev : nullptr); DeviceFiles deviceFiles(fdp_.ConsumeBool() ? &file_system_dev : nullptr);
deviceFiles.Init((CdmSecurityLevel)mFdp.ConsumeIntegralInRange<int32_t>( deviceFiles.Init((CdmSecurityLevel)fdp_.ConsumeIntegralInRange<int32_t>(
kSecurityLevelL1, kSecurityLevelL3)); kSecurityLevelL1, kSecurityLevelL3));
std::map<std::string, std::string> appParameters; std::map<std::string, std::string> app_parameters;
appParameters[mFdp.ConsumeBytesAsString(kMaxByte)] = app_parameters[fdp_.ConsumeBytesAsString(kMaxByte)] =
mFdp.ConsumeBytesAsString(kMaxByte); fdp_.ConsumeBytesAsString(kMaxByte);
licenseData.key_set_id = license_data.key_set_id =
std::to_string(mFdp.ConsumeIntegralInRange(kMinId, kMaxId)); std::to_string(fdp_.ConsumeIntegralInRange(kMinId, kMaxId));
licenseData.state = license_data.state =
(CdmOfflineLicenseState)mFdp.ConsumeIntegralInRange<int32_t>( (CdmOfflineLicenseState)fdp_.ConsumeIntegralInRange<int32_t>(
kLicenseStateActive, kLicenseStateUnknown); kLicenseStateActive, kLicenseStateUnknown);
licenseData.pssh_data = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.pssh_data = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.license_request = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.license_request = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.license = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.license = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.license_renewal_request = license_data.license_renewal_request =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.license_renewal = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.license_renewal = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.release_server_url = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.release_server_url = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.playback_start_time = mFdp.ConsumeIntegral<int64_t>(); license_data.playback_start_time = fdp_.ConsumeIntegral<int64_t>();
licenseData.last_playback_time = mFdp.ConsumeIntegral<int64_t>(); license_data.last_playback_time = fdp_.ConsumeIntegral<int64_t>();
licenseData.grace_period_end_time = mFdp.ConsumeIntegral<int64_t>(); license_data.grace_period_end_time = fdp_.ConsumeIntegral<int64_t>();
licenseData.app_parameters = appParameters; license_data.app_parameters = app_parameters;
licenseData.usage_entry = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.usage_entry = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.usage_entry_index = mFdp.ConsumeIntegral<uint32_t>(); license_data.usage_entry_index = fdp_.ConsumeIntegral<uint32_t>();
licenseData.drm_certificate = mFdp.ConsumeRandomLengthString(kMaxByte); license_data.drm_certificate = fdp_.ConsumeRandomLengthString(kMaxByte);
licenseData.wrapped_private_key = CryptoWrappedKey( license_data.wrapped_private_key = CryptoWrappedKey(
(CryptoWrappedKey::Type)mFdp.ConsumeIntegralInRange<int32_t>( (CryptoWrappedKey::Type)fdp_.ConsumeIntegralInRange<int32_t>(
CryptoWrappedKey::kUninitialized, CryptoWrappedKey::kEcc), CryptoWrappedKey::kUninitialized, CryptoWrappedKey::kEcc),
mFdp.ConsumeRandomLengthString(kMaxByte)); fdp_.ConsumeRandomLengthString(kMaxByte));
DeviceFiles::ResponseType result; DeviceFiles::ResponseType result;
deviceFiles.StoreLicense(licenseData, &result); deviceFiles.StoreLicense(license_data, &result);
} }
void CdmSessionFuzzer::invokeCdmSessionAPIs(CdmSession *cdmSession) { void CdmSessionFuzzer::InvokeCdmSessionAPIs(CdmSession *cdm_session) {
CdmKeyResponse response; CdmKeyResponse response;
createResponse(&response); CreateResponse(&response);
CdmKeyRequest keyRequest; CdmKeyRequest key_request;
CdmKeyMessage signedRequest; CdmKeyMessage signed_request;
keyRequest.message = signedRequest; key_request.message = signed_request;
keyRequest.type = (CdmKeyRequestType)mFdp.ConsumeIntegralInRange<int32_t>( key_request.type = (CdmKeyRequestType)fdp_.ConsumeIntegralInRange<int32_t>(
kKeyRequestTypeUnknown, kKeyRequestTypeRelease); kKeyRequestTypeUnknown, kKeyRequestTypeRelease);
std::string url; std::string url;
keyRequest.url = url; key_request.url = url;
while (mFdp.remaining_bytes()) { while (fdp_.remaining_bytes()) {
auto invokeCdmSessionAPI = mFdp.PickValueInArray< auto invokeCdmSessionAPI = fdp_.PickValueInArray<
const std::function<void()>>( const std::function<void()>>(
{[&]() { cdmSession->AddKey(response /*key_response*/); }, {[&]() { cdm_session->AddKey(response /*key_response*/); },
[&]() { [&]() {
FuzzCdmClientPropertySet propertyset(&mFdp); FuzzCdmClientPropertySet property_set(&fdp_);
propertyset.enable_privacy_mode(); property_set.enable_privacy_mode();
propertyset.set_service_certificate(kTestSignedCertificate); property_set.set_service_certificate(kTestSignedCertificate);
PropertiesTestPeer::ForceReinit(); PropertiesTestPeer::ForceReinit();
PropertiesTestPeer::AddSessionPropertySet( PropertiesTestPeer::AddSessionPropertySet(
mFdp.ConsumeBool() ? kTestSessionId1 fdp_.ConsumeBool() ? kTestSessionId1
: mFdp.ConsumeRandomLengthString(kMaxByte), : fdp_.ConsumeRandomLengthString(kMaxByte),
&propertyset); &property_set);
std::string rawServiceCertificate; std::string raw_service_certificate;
PropertiesTestPeer::GetServiceCertificate( PropertiesTestPeer::GetServiceCertificate(
mFdp.ConsumeBool() ? kTestSessionId1 fdp_.ConsumeBool() ? kTestSessionId1
: mFdp.ConsumeRandomLengthString(kMaxByte), : fdp_.ConsumeRandomLengthString(kMaxByte),
&rawServiceCertificate); &raw_service_certificate);
cdmSession->SetServiceCertificate( cdm_session->SetServiceCertificate(
rawServiceCertificate /*service_certificate*/); raw_service_certificate /*service_certificate*/);
}, },
[&]() { [&]() {
CdmQueryMap queryResponse; CdmQueryMap query_response;
cdmSession->QueryKeyStatus(&queryResponse /*query_response*/); cdm_session->QueryKeyStatus(&query_response /*query_response*/);
}, },
[&]() { [&]() {
const std::string oecVersion = std::to_string( const std::string oec_version = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
std::string dataType = mFdp.PickValueInArray(kInitType); std::string data_type = fdp_.PickValueInArray(kInitType);
std::unique_ptr<InitializationData> initData(new InitializationData( std::unique_ptr<InitializationData> init_data(new InitializationData(
dataType, a2bs_hex(mFdp.ConsumeRandomLengthString(kMaxByte)), data_type, a2bs_hex(fdp_.ConsumeRandomLengthString(kMaxByte)),
oecVersion)); oec_version));
CdmLicenseType licenseType = CdmLicenseType license_type =
(CdmLicenseType)mFdp.ConsumeIntegralInRange<int32_t>( (CdmLicenseType)fdp_.ConsumeIntegralInRange<int32_t>(
kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData); kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData);
CdmAppParameterMap appParameters; CdmAppParameterMap app_parameters;
cdmSession->GenerateKeyRequest( cdm_session->GenerateKeyRequest(
*initData /*init_data*/, licenseType /*license_type*/, *init_data /*init_data*/, license_type /*license_type*/,
appParameters /*app_parameters*/, &keyRequest /*key_request*/); app_parameters /*app_parameters*/, &key_request /*key_request*/);
}, },
[&]() { [&]() {
CdmQueryMap queryResponse; CdmQueryMap query_response;
cdmSession->QueryStatus(&queryResponse /*query_response*/); cdm_session->QueryStatus(&query_response /*query_response*/);
}, },
[&]() { [&]() {
DeviceFiles::CdmLicenseData licenseData; DeviceFiles::CdmLicenseData license_data;
storeLicense(licenseData); StoreLicense(license_data);
std::string randomData = mFdp.ConsumeRandomLengthString(kMaxByte); std::string random_data = fdp_.ConsumeRandomLengthString(kMaxByte);
const CdmKeySetId keySetId = const CdmKeySetId key_set_id =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? (kKeyId + wvutil::b2a_hex(randomData)) ? (kKeyId + wvutil::b2a_hex(random_data))
: std::to_string( : std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
CdmLicenseType licenseType = CdmLicenseType license_type =
(CdmLicenseType)mFdp.ConsumeIntegralInRange<int32_t>( (CdmLicenseType)fdp_.ConsumeIntegralInRange<int32_t>(
kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData); kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData);
int errorDetail = mFdp.ConsumeIntegral<int32_t>(); int error_detail = fdp_.ConsumeIntegral<int32_t>();
cdmSession->RestoreOfflineSession(keySetId /*key_set_id*/, cdm_session->RestoreOfflineSession(key_set_id /*key_set_id*/,
licenseType /*license_type*/, license_type /*license_type*/,
&errorDetail /*error_detail*/); &error_detail /*error_detail*/);
}, },
[&]() { [&]() {
DeviceFiles::CdmUsageData usageData; DeviceFiles::CdmUsageData usage_Data;
usageData.provider_session_token = usage_Data.provider_session_token =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
usageData.license_request = mFdp.ConsumeRandomLengthString(kMaxByte); usage_Data.license_request = fdp_.ConsumeRandomLengthString(kMaxByte);
usageData.license = mFdp.ConsumeRandomLengthString(kMaxByte); usage_Data.license = fdp_.ConsumeRandomLengthString(kMaxByte);
std::string randomData = mFdp.ConsumeRandomLengthString(kMaxByte); std::string random_data = fdp_.ConsumeRandomLengthString(kMaxByte);
usageData.key_set_id = usage_Data.key_set_id =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? (kKeyId + wvutil::b2a_hex(randomData)) ? (kKeyId + wvutil::b2a_hex(random_data))
: std::to_string( : std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
usageData.usage_entry = mFdp.ConsumeRandomLengthString(kMaxByte); usage_Data.usage_entry = fdp_.ConsumeRandomLengthString(kMaxByte);
usageData.usage_entry_index = mFdp.ConsumeIntegral<uint32_t>(); usage_Data.usage_entry_index = fdp_.ConsumeIntegral<uint32_t>();
usageData.drm_certificate = mFdp.ConsumeRandomLengthString(kMaxByte); usage_Data.drm_certificate = fdp_.ConsumeRandomLengthString(kMaxByte);
usageData.wrapped_private_key = CryptoWrappedKey( usage_Data.wrapped_private_key = CryptoWrappedKey(
(CryptoWrappedKey::Type)mFdp.ConsumeIntegralInRange<int32_t>( (CryptoWrappedKey::Type)fdp_.ConsumeIntegralInRange<int32_t>(
CryptoWrappedKey::kUninitialized, CryptoWrappedKey::kEcc), CryptoWrappedKey::kUninitialized, CryptoWrappedKey::kEcc),
mFdp.ConsumeRandomLengthString(kMaxByte)); fdp_.ConsumeRandomLengthString(kMaxByte));
int errorDetail = mFdp.ConsumeIntegral<int32_t>(); int error_detail = fdp_.ConsumeIntegral<int32_t>();
cdmSession->RestoreUsageSession(usageData /*usage_data*/, cdm_session->RestoreUsageSession(usage_Data /*usage_data*/,
&errorDetail /*error_detail*/); &error_detail /*error_detail*/);
}, },
[&]() { [&]() {
const std::string keyId = std::to_string( const std::string key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
CdmKeyAllowedUsage keyUsage; CdmKeyAllowedUsage key_usage;
cdmSession->QueryKeyAllowedUsage(keyId /*key_id*/, cdm_session->QueryKeyAllowedUsage(key_id /*key_id*/,
&keyUsage /*key_usage*/); &key_usage /*key_usage*/);
}, },
[&]() { [&]() {
CdmQueryMap queryResponse; CdmQueryMap query_response;
cdmSession->QueryOemCryptoSessionId( cdm_session->QueryOemCryptoSessionId(
&queryResponse /*query_response*/); &query_response /*query_response*/);
}, },
[&]() { [&]() {
const KeyId keyId = std::to_string( const KeyId key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
CdmDecryptionParametersV16 params(keyId); CdmDecryptionParametersV16 params(key_id);
const uint8_t encryptBufferParam = mFdp.ConsumeIntegral<uint8_t>(); const uint8_t encrypt_buffer_param = fdp_.ConsumeIntegral<uint8_t>();
void *decryptBufferParam; void *decrypt_buffer_param;
size_t decryptbufferOffsetParam = mFdp.ConsumeIntegral<size_t>(); size_t decrypt_buffer_offset_param = fdp_.ConsumeIntegral<size_t>();
size_t length = mFdp.ConsumeIntegral<size_t>(); size_t length = fdp_.ConsumeIntegral<size_t>();
const std::vector<uint8_t> iv = mFdp.ConsumeBytes<uint8_t>(kMaxSize); const std::vector<uint8_t> iv = fdp_.ConsumeBytes<uint8_t>(kMaxSize);
CdmDecryptionSample sample(&encryptBufferParam, decryptBufferParam, CdmDecryptionSample sample(&encrypt_buffer_param, decrypt_buffer_param,
decryptbufferOffsetParam, length, iv); decrypt_buffer_offset_param, length, iv);
CdmDecryptionSubsample subsample(mFdp.ConsumeIntegral<size_t>(), CdmDecryptionSubsample sub_sample(fdp_.ConsumeIntegral<size_t>(),
mFdp.ConsumeIntegral<size_t>()); fdp_.ConsumeIntegral<size_t>());
sample.subsamples.push_back(subsample); sample.subsamples.push_back(sub_sample);
params.samples.push_back(sample); params.samples.push_back(sample);
cdmSession->Decrypt(params /*parameters*/); cdm_session->Decrypt(params /*parameters*/);
}, },
[&]() { [&]() {
cdmSession->GenerateRenewalRequest(&keyRequest /*key_request*/); cdm_session->GenerateRenewalRequest(&key_request /*key_request*/);
}, },
[&]() { cdmSession->RenewKey(response /*key_response*/); }, [&]() { cdm_session->RenewKey(response /*key_response*/); },
[&]() { [&]() {
const KeyId keyId = std::to_string( const KeyId key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
cdmSession->IsKeyLoaded(keyId /*key_id*/); cdm_session->IsKeyLoaded(key_id /*key_id*/);
}, },
[&]() { cdmSession->GetDurationRemaining(); }, [&]() { cdm_session->GetDurationRemaining(); },
[&]() { [&]() {
cdmSession->NotifyResolution( cdm_session->NotifyResolution(
mFdp.ConsumeIntegral<uint32_t>() /*width*/, fdp_.ConsumeIntegral<uint32_t>() /*width*/,
mFdp.ConsumeIntegral<uint32_t>() /*height*/); fdp_.ConsumeIntegral<uint32_t>() /*height*/);
}, },
[&]() { [&]() {
cdmSession->OnTimerEvent(mFdp.ConsumeBool() /*update_usage*/); cdm_session->OnTimerEvent(fdp_.ConsumeBool() /*update_usage*/);
}, },
[&]() { [&]() {
const CdmKeySetId keySetId = const CdmKeySetId key_set_id =
kKeyId + mFdp.ConsumeRandomLengthString(kMaxByte); kKeyId + fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->OnKeyReleaseEvent(keySetId /*key_set_id*/); cdm_session->OnKeyReleaseEvent(key_set_id /*key_set_id*/);
}, },
[&]() { [&]() {
std::string appId = std::string app_id =
kSessionId + kSessionId +
std::to_string( std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
cdmSession->GetApplicationId(&appId /*app_id*/); cdm_session->GetApplicationId(&app_id /*app_id*/);
}, },
[&]() { cdmSession->UpdateUsageEntryInformation(); }, [&]() { cdm_session->UpdateUsageEntryInformation(); },
[&]() { cdmSession->GenerateSessionId(); }, [&]() { cdm_session->GenerateSessionId(); },
[&]() { [&]() {
const std::string inBuffer = const std::string in_buffer =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
const std::string keyId = std::to_string( const std::string key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
const std::string iv = mFdp.ConsumeRandomLengthString(kMaxByte); const std::string iv = fdp_.ConsumeRandomLengthString(kMaxByte);
CdmEncryptionAlgorithm algorithm = CdmEncryptionAlgorithm algorithm =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? CdmEncryptionAlgorithm::kEncryptionAlgorithmAesCbc128 ? CdmEncryptionAlgorithm::kEncryptionAlgorithmAesCbc128
: CdmEncryptionAlgorithm::kEncryptionAlgorithmUnknown; : CdmEncryptionAlgorithm::kEncryptionAlgorithmUnknown;
std::string outBuffer = mFdp.ConsumeRandomLengthString(kMaxByte); std::string out_buffer = fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->GenericEncrypt(inBuffer /*in_buffer*/, keyId /*key_id*/, cdm_session->GenericEncrypt(in_buffer /*in_buffer*/, key_id /*key_id*/,
iv, algorithm, &outBuffer /*out_buffer*/); iv, algorithm, &out_buffer /*out_buffer*/);
}, },
[&]() { [&]() {
const std::string inBuffer = const std::string in_buffer =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
const std::string keyId = std::to_string( const std::string key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
const std::string iv = mFdp.ConsumeRandomLengthString(kMaxByte); const std::string iv = fdp_.ConsumeRandomLengthString(kMaxByte);
CdmEncryptionAlgorithm algorithm = CdmEncryptionAlgorithm algorithm =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? CdmEncryptionAlgorithm::kEncryptionAlgorithmAesCbc128 ? CdmEncryptionAlgorithm::kEncryptionAlgorithmAesCbc128
: CdmEncryptionAlgorithm::kEncryptionAlgorithmUnknown; : CdmEncryptionAlgorithm::kEncryptionAlgorithmUnknown;
std::string outBuffer = mFdp.ConsumeRandomLengthString(kMaxByte); std::string out_buffer = fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->GenericDecrypt(inBuffer /*in_buffer*/, keyId /*key_id*/, cdm_session->GenericDecrypt(in_buffer /*in_buffer*/, key_id /*key_id*/,
iv, algorithm, &outBuffer /*out_buffer*/); iv, algorithm, &out_buffer /*out_buffer*/);
}, },
[&]() { [&]() {
const std::string message = mFdp.ConsumeRandomLengthString(kMaxByte); const std::string message = fdp_.ConsumeRandomLengthString(kMaxByte);
const std::string keyId = std::to_string( const std::string key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
CdmSigningAlgorithm algorithm = CdmSigningAlgorithm algorithm =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? CdmSigningAlgorithm::kSigningAlgorithmHmacSha256 ? CdmSigningAlgorithm::kSigningAlgorithmHmacSha256
: CdmSigningAlgorithm::kSigningAlgorithmUnknown; : CdmSigningAlgorithm::kSigningAlgorithmUnknown;
std::string signature = mFdp.ConsumeRandomLengthString(kMaxByte); std::string signature = fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->GenericSign(message, keyId /*key_id*/, algorithm, cdm_session->GenericSign(message, key_id /*key_id*/, algorithm,
&signature); &signature);
}, },
[&]() { [&]() {
const std::string message = mFdp.ConsumeRandomLengthString(kMaxByte); const std::string message = fdp_.ConsumeRandomLengthString(kMaxByte);
const std::string keyId = std::to_string( const std::string key_id = std::to_string(
mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
CdmSigningAlgorithm algorithm = CdmSigningAlgorithm algorithm =
mFdp.ConsumeBool() fdp_.ConsumeBool()
? CdmSigningAlgorithm::kSigningAlgorithmHmacSha256 ? CdmSigningAlgorithm::kSigningAlgorithmHmacSha256
: CdmSigningAlgorithm::kSigningAlgorithmUnknown; : CdmSigningAlgorithm::kSigningAlgorithmUnknown;
const std::string signature = const std::string signature =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->GenericVerify(message, keyId /*key_id*/, algorithm, cdm_session->GenericVerify(message, key_id /*key_id*/, algorithm,
signature); signature);
}, },
[&]() { [&]() {
uint32_t frameNumber = mFdp.ConsumeIntegral<uint32_t>(); uint32_t frame_number = fdp_.ConsumeIntegral<uint32_t>();
const std::string hash = mFdp.ConsumeRandomLengthString(kMaxByte); const std::string hash = fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->SetDecryptHash(frameNumber /*frame_number*/, hash); cdm_session->SetDecryptHash(frame_number /*frame_number*/, hash);
}, },
[&]() { [&]() {
std::string hashErrorString = std::string hash_error_string =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
cdmSession->GetDecryptHashError( cdm_session->GetDecryptHashError(
&hashErrorString /*hash_error_string*/); &hash_error_string /*hash_error_string*/);
}, },
[&]() { cdmSession->ReleaseKey(response /*key_response*/); }, [&]() { cdm_session->ReleaseKey(response /*key_response*/); },
[&]() { [&]() {
cdmSession->GenerateReleaseRequest(&keyRequest /*key_request*/); cdm_session->GenerateReleaseRequest(&key_request /*key_request*/);
}}); }});
invokeCdmSessionAPI(); invokeCdmSessionAPI();
} }
UsageEntryIndex entryIndex = mFdp.ConsumeIntegral<uint32_t>(); UsageEntryIndex entry_index = fdp_.ConsumeIntegral<uint32_t>();
cdmSession->DeleteUsageEntry(entryIndex /*usage_entry_index*/); cdm_session->DeleteUsageEntry(entry_index /*usage_entry_index*/);
cdmSession->DeleteLicenseFile(); cdm_session->DeleteLicenseFile();
cdmSession->RemoveKeys(); cdm_session->RemoveKeys();
cdmSession->RemoveLicense(); cdm_session->RemoveLicense();
} }
void CdmSessionFuzzer::invokeCdmSessionMapAPIs() { void CdmSessionFuzzer::InvokeCdmSessionMapAPIs() {
FuzzFileSystem fileSystemMap(&mFdp); FuzzFileSystem file_system_map(&fdp_);
CdmSessionMap cdmsessionMap; CdmSessionMap cdm_session_map;
std::shared_ptr<metrics::SessionMetrics> CdmMetricsMap( std::shared_ptr<metrics::SessionMetrics> cdm_metrics_map(
new metrics::SessionMetrics); new metrics::SessionMetrics);
CdmSession *session = new CdmSession(&fileSystemMap, CdmMetricsMap); CdmSession *session = new CdmSession(&file_system_map, cdm_metrics_map);
const CdmSessionId forcedSessionId = const CdmSessionId forced_session_id =
kSessionId + kSessionId +
std::to_string(mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); std::to_string(fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
cdmsessionMap.Add(forcedSessionId /*id*/, session /*session*/); cdm_session_map.Add(forced_session_id /*id*/, session /*session*/);
while (mFdp.remaining_bytes()) { while (fdp_.remaining_bytes()) {
auto invokeCdmSessionMapAPI = auto invokeCdmSessionMapAPI =
mFdp.PickValueInArray<const std::function<void()>>({ fdp_.PickValueInArray<const std::function<void()>>({
[&]() { cdmsessionMap.Exists(forcedSessionId /*id*/); }, [&]() { cdm_session_map.Exists(forced_session_id /*id*/); },
[&]() { cdmsessionMap.Size(); }, [&]() { cdm_session_map.Size(); },
[&]() { [&]() {
std::shared_ptr<CdmSession> cdmsessionShared; std::shared_ptr<CdmSession> cdm_session_shared;
cdmsessionMap.FindSession(forcedSessionId /*id*/, cdm_session_map.FindSession(forced_session_id /*id*/,
&cdmsessionShared /*session*/); &cdm_session_shared /*session*/);
}, },
[&]() { [&]() {
CdmSessionList sessions; CdmSessionList sessions;
cdmsessionMap.GetSessionList(sessions); cdm_session_map.GetSessionList(sessions);
}, },
}); });
invokeCdmSessionMapAPI(); invokeCdmSessionMapAPI();
cdmsessionMap.CloseSession(forcedSessionId); cdm_session_map.CloseSession(forced_session_id);
} }
} }
void CdmSessionFuzzer::process() { void CdmSessionFuzzer::Process() {
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
FuzzFileSystem fileSystem(&mFdp); FuzzFileSystem file_system(&fdp_);
FuzzEventListener fuzzeventListener; FuzzEventListener fuzz_event_listener;
std::shared_ptr<metrics::SessionMetrics> metrics = std::shared_ptr<metrics::SessionMetrics> metrics =
std::make_shared<metrics::SessionMetrics>(); std::make_shared<metrics::SessionMetrics>();
std::unique_ptr<CdmSession> cdmSession = std::unique_ptr<CdmSession> cdm_session =
std::make_unique<CdmSession>(&fileSystem, metrics); std::make_unique<CdmSession>(&file_system, metrics);
std::unique_ptr<FuzzCdmClientPropertySet> fuzzCdmClientPropertySet( std::unique_ptr<FuzzCdmClientPropertySet> fuzz_cdm_client_property_set(
new FuzzCdmClientPropertySet(&mFdp)); new FuzzCdmClientPropertySet(&fdp_));
const CdmSessionId forcedSessionId = const CdmSessionId forced_session_id =
kSessionId + kSessionId +
std::to_string(mFdp.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId)); std::to_string(fdp_.ConsumeIntegralInRange<uint32_t>(kMinId, kMaxId));
bool forcedLevel = mFdp.ConsumeBool(); bool forced_level = fdp_.ConsumeBool();
CdmResponseType result = CdmResponseType result =
initCdmSession(cdmSession.get(), fuzzCdmClientPropertySet.get(), InitCdmSession(cdm_session.get(), fuzz_cdm_client_property_set.get(),
forcedSessionId, &fuzzeventListener, forcedLevel); forced_session_id, &fuzz_event_listener, forced_level);
if (result == NO_ERROR) { if (result == NO_ERROR) {
invokeCdmSessionAPIs(cdmSession.get()); InvokeCdmSessionAPIs(cdm_session.get());
} }
} else { } else {
invokeCdmSessionMapAPIs(); InvokeCdmSessionMapAPIs();
} }
} }
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
CdmSessionFuzzer cdmSessionFuzzer(data, size); CdmSessionFuzzer cdm_session_fuzzer(data, size);
cdmSessionFuzzer.process(); cdm_session_fuzzer.Process();
return 0; return 0;
} }