Updated content_decryption_fuzzer

Implemented google c++ code style changes for content_decryption_fuzzer

exec/s: 37
Test: ./content_decryption_fuzzer
Bug: 312374669

Change-Id: Ic9d6e59716811b3d90bfbe96b7d59674bf53277c
This commit is contained in:
Onkar Shinde
2023-12-01 16:35:23 +05:30
committed by Aditya Wazir
parent 2b3de00a36
commit 214fec8bb4

View File

@@ -14,12 +14,13 @@
* limitations under the License. * limitations under the License.
* *
*/ */
#include <fuzzer/FuzzedDataProvider.h>
#include "cdm_client_property_set.h" #include "cdm_client_property_set.h"
#include "cdm_identifier.h" #include "cdm_identifier.h"
#include "wv_cdm_constants.h" #include "wv_cdm_constants.h"
#include "wv_cdm_event_listener.h" #include "wv_cdm_event_listener.h"
#include "wv_content_decryption_module.h" #include "wv_content_decryption_module.h"
#include <fuzzer/FuzzedDataProvider.h>
using namespace wvcdm; using namespace wvcdm;
using namespace android; using namespace android;
@@ -70,7 +71,7 @@ const std::string kInitDataTypes[] = {
}; };
class FuzzWvCdmEventListener : public WvCdmEventListener { class FuzzWvCdmEventListener : public WvCdmEventListener {
public: public:
void OnSessionRenewalNeeded(const CdmSessionId & /*session_id*/) {} void OnSessionRenewalNeeded(const CdmSessionId & /*session_id*/) {}
void OnSessionKeysChange(const CdmSessionId & /*session_id*/, void OnSessionKeysChange(const CdmSessionId & /*session_id*/,
const CdmKeyStatusMap & /*keys_status*/, const CdmKeyStatusMap & /*keys_status*/,
@@ -80,272 +81,270 @@ public:
}; };
class FuzzCdmClientPropertySet : public CdmClientPropertySet { class FuzzCdmClientPropertySet : public CdmClientPropertySet {
public: public:
FuzzCdmClientPropertySet(FuzzedDataProvider *fdp) { FuzzCdmClientPropertySet(FuzzedDataProvider *fdp) {
mFdp = fdp; fdp_ = fdp;
mSecurityLevel = fdp->ConsumeBool() security_level_ = fdp->ConsumeBool()
? fdp->ConsumeRandomLengthString(kMaxByte) ? fdp->ConsumeRandomLengthString(kMaxByte)
: fdp->PickValueInArray(kSecurityLevel); : fdp->PickValueInArray(kSecurityLevel);
mUsePrivacyMode = fdp->ConsumeBool(); user_privacy_mode_ = fdp->ConsumeBool();
mIsSessionSharingEnabled = fdp->ConsumeBool(); is_session_sharing_enabled_ = fdp->ConsumeBool();
mSessionSharingId = fdp->ConsumeIntegral<int32_t>(); session_sharing_id_ = fdp->ConsumeIntegral<int32_t>();
mAppId = app_id_ =
fdp->ConsumeBool() ? kAppId : fdp->ConsumeRandomLengthString(kMaxByte); fdp->ConsumeBool() ? kAppId : fdp->ConsumeRandomLengthString(kMaxByte);
} }
const std::string &security_level() const override { return mSecurityLevel; } const std::string &security_level() const override { return security_level_; }
bool use_privacy_mode() const override { return mUsePrivacyMode; } bool use_privacy_mode() const override { return user_privacy_mode_; }
const std::string &service_certificate() const override { const std::string &service_certificate() const override {
return mRawServiceCertificate; return raw_service_certificate_;
} }
void set_service_certificate(const std::string &cert) override { void set_service_certificate(const std::string &cert) override {
if (mFdp->ConsumeBool()) { if (fdp_->ConsumeBool()) {
mRawServiceCertificate = cert; raw_service_certificate_ = cert;
} }
} }
bool is_session_sharing_enabled() const override { bool is_session_sharing_enabled() const override {
return mIsSessionSharingEnabled; return is_session_sharing_enabled_;
} }
uint32_t session_sharing_id() const override { uint32_t session_sharing_id() const override {
uint32_t sessionId = 0; uint32_t session_id = 0;
if (mFdp->ConsumeBool()) { if (fdp_->ConsumeBool()) {
sessionId = mSessionSharingId; session_id = session_sharing_id_;
} }
return sessionId; return session_id;
} }
bool use_atsc_mode() const override { return false; } bool use_atsc_mode() const override { return false; }
void set_session_sharing_id(uint32_t id) override { void set_session_sharing_id(uint32_t id) override {
if (mFdp->ConsumeBool()) { if (fdp_->ConsumeBool()) {
mSessionSharingId = id; session_sharing_id_ = id;
} }
} }
const std::string &app_id() const override { return mAppId; } const std::string &app_id() const override { return app_id_; }
void enable_privacy_mode() { void enable_privacy_mode() {
if (mFdp->ConsumeBool()) { if (fdp_->ConsumeBool()) {
mUsePrivacyMode = true; user_privacy_mode_ = true;
} }
} }
private: private:
FuzzedDataProvider *mFdp; FuzzedDataProvider *fdp_;
std::string mSecurityLevel; std::string security_level_;
std::string mRawServiceCertificate; std::string raw_service_certificate_;
std::string mAppId; std::string app_id_;
uint32_t mSessionSharingId; uint32_t session_sharing_id_;
bool mUsePrivacyMode; bool user_privacy_mode_;
bool mIsSessionSharingEnabled; bool is_session_sharing_enabled_;
}; };
class ContentDecryptionFuzzer { class ContentDecryptionFuzzer {
public: public:
ContentDecryptionFuzzer(const uint8_t *data, size_t size) : mFdp(data, size) { ContentDecryptionFuzzer(const uint8_t *data, size_t size) : fdp_(data, size) {
mDecryptor = sp<WvContentDecryptionModule>::make(); decryptor_ = sp<WvContentDecryptionModule>::make();
FuzzWvCdmEventListener fuzzWvCdmEventListener; FuzzWvCdmEventListener fuzz_wv_cdm_event_listener_;
mFuzzCdmClientPropertySet.reset(new FuzzCdmClientPropertySet(&mFdp)); fuzz_cdm_client_property_set_.reset(new FuzzCdmClientPropertySet(&fdp_));
if (mFdp.ConsumeBool()) { if (fdp_.ConsumeBool()) {
mCdmIdentifier = kDefaultCdmIdentifier; cdm_identifier_ = kDefaultCdmIdentifier;
} else { } else {
mCdmIdentifier.spoid = mFdp.ConsumeRandomLengthString(kMaxByte); cdm_identifier_.spoid = fdp_.ConsumeRandomLengthString(kMaxByte);
mCdmIdentifier.origin = mFdp.ConsumeRandomLengthString(kMaxByte); cdm_identifier_.origin = fdp_.ConsumeRandomLengthString(kMaxByte);
mCdmIdentifier.app_package_name = cdm_identifier_.app_package_name =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
mCdmIdentifier.unique_id = mFdp.ConsumeIntegral<uint32_t>(); cdm_identifier_.unique_id = fdp_.ConsumeIntegral<uint32_t>();
mCdmIdentifier.user_id = mFdp.ConsumeIntegral<uint32_t>(); cdm_identifier_.user_id = fdp_.ConsumeIntegral<uint32_t>();
} }
mDecryptor->OpenSession(KEY_SYSTEM, mFuzzCdmClientPropertySet.get(), decryptor_->OpenSession(KEY_SYSTEM, fuzz_cdm_client_property_set_.get(),
mCdmIdentifier, &fuzzWvCdmEventListener, cdm_identifier_, &fuzz_wv_cdm_event_listener_,
&mSessionId); &session_id_);
}; };
~ContentDecryptionFuzzer() { mDecryptor->CloseSession(mSessionId); } ~ContentDecryptionFuzzer() { decryptor_->CloseSession(session_id_); }
void process(); void Process();
private: private:
FuzzedDataProvider mFdp; FuzzedDataProvider fdp_;
std::unique_ptr<FuzzCdmClientPropertySet> mFuzzCdmClientPropertySet; std::unique_ptr<FuzzCdmClientPropertySet> fuzz_cdm_client_property_set_;
CdmSessionId mSessionId; CdmSessionId session_id_;
CdmIdentifier mCdmIdentifier; CdmIdentifier cdm_identifier_;
sp<WvContentDecryptionModule> mDecryptor; sp<WvContentDecryptionModule> decryptor_;
void invokeDecryptorSessionAPIs(); void InvokeDecryptorSessionAPI();
void invokeProvisionAPIs(); void InvokeProvisionAPI();
}; };
void ContentDecryptionFuzzer::invokeDecryptorSessionAPIs() { void ContentDecryptionFuzzer::InvokeDecryptorSessionAPI() {
CdmKeySetId keySetId = mFdp.ConsumeRandomLengthString(kMaxByte); CdmKeySetId key_set_id = fdp_.ConsumeRandomLengthString(kMaxByte);
int32_t runs = kMaxRuns; int32_t runs = kMaxRuns;
/* Limited the while loop to prevent a timeout caused by the /* Limited the while loop to prevent a timeout caused by the
/* CryptoSession constructor, which took time to initialize /* CryptoSession constructor, which took time to initialize
/* OEMCrypto in each iteration.*/ /* OEMCrypto in each iteration.*/
while (mFdp.remaining_bytes() > 0 && --runs) { while (fdp_.remaining_bytes() > 0 && --runs) {
auto invokeDecryptionSessionAPI = auto invoke_decryption_session_API =
mFdp.PickValueInArray<const std::function<void()>>({ fdp_.PickValueInArray<const std::function<void()>>({
[&]() { mDecryptor->IsOpenSession(mSessionId); }, [&]() { decryptor_->IsOpenSession(session_id_); },
[&]() { [&]() {
CdmAppParameterMap appParameters; CdmAppParameterMap app_parameters;
int32_t maxSize = int32_t max_size =
mFdp.ConsumeIntegralInRange<int32_t>(0, kMaxAppParamSize); fdp_.ConsumeIntegralInRange<int32_t>(0, kMaxAppParamSize);
for (size_t i = 0; i < maxSize; ++i) { for (size_t i = 0; i < max_size; ++i) {
std::string key = mFdp.ConsumeRandomLengthString(kMaxByte); std::string key = fdp_.ConsumeRandomLengthString(kMaxByte);
std::string value = mFdp.ConsumeRandomLengthString(kMaxByte); std::string value = fdp_.ConsumeRandomLengthString(kMaxByte);
std::string cdmKey(key.c_str(), key.size()); std::string cdm_key(key.c_str(), key.size());
std::string cdmValue(value.c_str(), value.size()); std::string cdm_value(value.c_str(), value.size());
appParameters[cdmKey] = cdmValue; app_parameters[cdm_key] = cdm_value;
} }
std::string initDataType = mFdp.PickValueInArray(kInitDataTypes); std::string init_data_type = fdp_.PickValueInArray(kInitDataTypes);
CdmInitData initData = mFdp.ConsumeRandomLengthString(kMaxByte); CdmInitData init_data = fdp_.ConsumeRandomLengthString(kMaxByte);
CdmLicenseType licenseType = CdmLicenseType license_type =
(CdmLicenseType)mFdp.ConsumeIntegralInRange<int32_t>( (CdmLicenseType)fdp_.ConsumeIntegralInRange<int32_t>(
kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData); kLicenseTypeOffline, kLicenseTypeEmbeddedKeyData);
CdmKeyRequest keyRequest; CdmKeyRequest key_request;
keyRequest.message = mFdp.ConsumeRandomLengthString(kMaxByte); key_request.message = fdp_.ConsumeRandomLengthString(kMaxByte);
keyRequest.type = key_request.type =
(CdmKeyRequestType)mFdp.ConsumeIntegralInRange<int32_t>( (CdmKeyRequestType)fdp_.ConsumeIntegralInRange<int32_t>(
kKeyRequestTypeUnknown, kKeyRequestTypeUpdate); kKeyRequestTypeUnknown, kKeyRequestTypeUpdate);
keyRequest.url = mFdp.ConsumeRandomLengthString(kMaxByte); key_request.url = fdp_.ConsumeRandomLengthString(kMaxByte);
FuzzCdmClientPropertySet *propertySet = FuzzCdmClientPropertySet *property_set =
mFdp.ConsumeBool() ? mFuzzCdmClientPropertySet.get() fdp_.ConsumeBool() ? fuzz_cdm_client_property_set_.get()
: nullptr; : nullptr;
mDecryptor->GenerateKeyRequest( decryptor_->GenerateKeyRequest(
mSessionId, keySetId, initDataType, initData, licenseType, session_id_, key_set_id, init_data_type, init_data, license_type,
appParameters, propertySet, mCdmIdentifier, app_parameters, property_set, cdm_identifier_,
(mFdp.ConsumeBool() ? nullptr : &keyRequest)); (fdp_.ConsumeBool() ? nullptr : &key_request));
}, },
[&]() { [&]() {
CdmKeyResponse response = CdmKeyResponse response =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
mDecryptor->AddKey(mSessionId, response, &keySetId); decryptor_->AddKey(session_id_, response, &key_set_id);
}, },
[&]() { mDecryptor->RestoreKey(mSessionId, keySetId); }, [&]() { decryptor_->RestoreKey(session_id_, key_set_id); },
[&]() { mDecryptor->RemoveKeys(mSessionId); }, [&]() { decryptor_->RemoveKeys(session_id_); },
[&]() { [&]() {
RequestedSecurityLevel securityLevel = RequestedSecurityLevel security_level =
(RequestedSecurityLevel)mFdp.ConsumeIntegral<uint32_t>(); (RequestedSecurityLevel)fdp_.ConsumeIntegral<uint32_t>();
std::string outputStr; std::string output_str;
std::string queryToken = std::string query_token =
mFdp.ConsumeBool() ? mFdp.ConsumeRandomLengthString(kMaxByte) fdp_.ConsumeBool() ? fdp_.ConsumeRandomLengthString(kMaxByte)
: mFdp.PickValueInArray(kQueryLevel); : fdp_.PickValueInArray(kQueryLevel);
; decryptor_->QueryStatus(
mDecryptor->QueryStatus( security_level, query_token.c_str(),
securityLevel, queryToken.c_str(), (fdp_.ConsumeBool() ? nullptr : &output_str));
(mFdp.ConsumeBool() ? nullptr : &outputStr));
}, },
[&]() { [&]() {
CdmQueryMap keyInfo; CdmQueryMap key_info;
mDecryptor->QuerySessionStatus( decryptor_->QuerySessionStatus(
mSessionId, (mFdp.ConsumeBool() ? nullptr : &keyInfo)); session_id_, (fdp_.ConsumeBool() ? nullptr : &key_info));
}, },
[&]() { [&]() {
CdmQueryMap keyInfo; CdmQueryMap key_info;
mDecryptor->QueryKeyStatus( decryptor_->QueryKeyStatus(
mSessionId, (mFdp.ConsumeBool() ? nullptr : &keyInfo)); session_id_, (fdp_.ConsumeBool() ? nullptr : &key_info));
}, },
[&]() { [&]() {
CdmQueryMap keyInfo; CdmQueryMap key_info;
mDecryptor->QueryOemCryptoSessionId( decryptor_->QueryOemCryptoSessionId(
mSessionId, (mFdp.ConsumeBool() ? nullptr : &keyInfo)); session_id_, (fdp_.ConsumeBool() ? nullptr : &key_info));
}, },
[&]() { [&]() {
CdmSecurityLevel level = CdmSecurityLevel level =
(CdmSecurityLevel)mFdp.ConsumeIntegralInRange<int32_t>( (CdmSecurityLevel)fdp_.ConsumeIntegralInRange<int32_t>(
kSecurityLevelUninitialized, kSecurityLevelUnknown); kSecurityLevelUninitialized, kSecurityLevelUnknown);
mDecryptor->IsSecurityLevelSupported(level); decryptor_->IsSecurityLevelSupported(level);
}, },
}); });
invokeDecryptionSessionAPI(); invoke_decryption_session_API();
} }
} }
void ContentDecryptionFuzzer::invokeProvisionAPIs() { void ContentDecryptionFuzzer::InvokeProvisionAPI() {
int32_t runs = kMaxRuns; int32_t runs = kMaxRuns;
/* Limited the while loop to prevent a timeout caused by the /* Limited the while loop to prevent a timeout caused by the
/* CryptoSession constructor, which took time to initialize /* CryptoSession constructor, which took time to initialize
/* OEMCrypto in each iteration.*/ /* OEMCrypto in each iteration.*/
while (mFdp.remaining_bytes() > 0 && --runs) { while (fdp_.remaining_bytes() > 0 && --runs) {
auto invokeProvisionAPI = auto invoke_provision_API =
mFdp.PickValueInArray<const std::function<void()>>({ fdp_.PickValueInArray<const std::function<void()>>({
[&]() { [&]() {
CdmSecurityLevel level = CdmSecurityLevel level =
(CdmSecurityLevel)mFdp.ConsumeIntegralInRange<int32_t>( (CdmSecurityLevel)fdp_.ConsumeIntegralInRange<int32_t>(
kSecurityLevelUninitialized, kSecurityLevelUnknown); kSecurityLevelUninitialized, kSecurityLevelUnknown);
mDecryptor->Unprovision(level, mCdmIdentifier); decryptor_->Unprovision(level, cdm_identifier_);
}, },
[&]() { [&]() {
CdmKeyMessage keyMsg; CdmKeyMessage key_msg;
CdmCertificateType certificateType = CdmCertificateType certificate_type =
(CdmCertificateType)mFdp.ConsumeIntegral<int32_t>(); (CdmCertificateType)fdp_.ConsumeIntegral<int32_t>();
std::string certAuthority = std::string cert_authority =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
RequestedSecurityLevel securityLevel = RequestedSecurityLevel security_level =
(RequestedSecurityLevel)mFdp.ConsumeIntegral<uint32_t>(); (RequestedSecurityLevel)fdp_.ConsumeIntegral<uint32_t>();
std::string serverUrl = mFdp.ConsumeRandomLengthString(kMaxByte); std::string server_url = fdp_.ConsumeRandomLengthString(kMaxByte);
std::string serviceCertificate = std::string service_certificate =
mFdp.ConsumeRandomLengthString(kMaxByte); fdp_.ConsumeRandomLengthString(kMaxByte);
mDecryptor->GetProvisioningRequest( decryptor_->GetProvisioningRequest(
certificateType, certAuthority, mCdmIdentifier, certificate_type, cert_authority, cdm_identifier_,
serviceCertificate, securityLevel, service_certificate, security_level,
(mFdp.ConsumeBool() ? nullptr : &keyMsg), (fdp_.ConsumeBool() ? nullptr : &key_msg),
(mFdp.ConsumeBool() ? nullptr : &serverUrl)); (fdp_.ConsumeBool() ? nullptr : &server_url));
}, },
[&]() { [&]() {
std::string response = mFdp.ConsumeRandomLengthString(kMaxByte); std::string response = fdp_.ConsumeRandomLengthString(kMaxByte);
RequestedSecurityLevel securityLevel = RequestedSecurityLevel security_level =
(RequestedSecurityLevel)mFdp.ConsumeIntegral<uint32_t>(); (RequestedSecurityLevel)fdp_.ConsumeIntegral<uint32_t>();
std::string cert, wrappedKey; std::string cert, wrapped_key;
mDecryptor->HandleProvisioningResponse( decryptor_->HandleProvisioningResponse(
mCdmIdentifier, response, securityLevel, cdm_identifier_, response, security_level,
(mFdp.ConsumeBool() ? nullptr : &cert), (fdp_.ConsumeBool() ? nullptr : &cert),
(mFdp.ConsumeBool() ? nullptr : &wrappedKey)); (fdp_.ConsumeBool() ? nullptr : &wrapped_key));
}, },
[&]() { [&]() {
CdmSecurityLevel level = CdmSecurityLevel level =
(CdmSecurityLevel)mFdp.ConsumeIntegralInRange<int32_t>( (CdmSecurityLevel)fdp_.ConsumeIntegralInRange<int32_t>(
kSecurityLevelUninitialized, kSecurityLevelUnknown); kSecurityLevelUninitialized, kSecurityLevelUnknown);
std::string origin = mFdp.ConsumeRandomLengthString(kMaxByte); std::string origin = fdp_.ConsumeRandomLengthString(kMaxByte);
std::string spoid = mFdp.ConsumeRandomLengthString(kMaxByte); std::string spoid = fdp_.ConsumeRandomLengthString(kMaxByte);
mDecryptor->IsProvisioned( decryptor_->IsProvisioned(
level, origin, spoid, level, origin, spoid,
mFdp.ConsumeBool() /*atsc_mode_enabled*/); fdp_.ConsumeBool() /*atsc_mode_enabled*/);
}, },
}); });
invokeProvisionAPI(); invoke_provision_API();
} }
} }
void ContentDecryptionFuzzer::process() { void ContentDecryptionFuzzer::Process() {
auto invokeContentDecryptionAPI = auto invoke_content_decryption_API =
mFdp.PickValueInArray<const std::function<void()>>({ fdp_.PickValueInArray<const std::function<void()>>({
[&]() { invokeDecryptorSessionAPIs(); }, [&]() { InvokeDecryptorSessionAPI(); },
[&]() { invokeProvisionAPIs(); }, [&]() { InvokeProvisionAPI(); },
}); });
invokeContentDecryptionAPI(); invoke_content_decryption_API();
} }
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ContentDecryptionFuzzer contentDecryptionFuzzer(data, size); ContentDecryptionFuzzer content_decryption_fuzzer(data, size);
contentDecryptionFuzzer.process(); content_decryption_fuzzer.Process();
return 0; return 0;
} }