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