1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 
17 #ifndef DRM_DRMSERVICE_FUZZER_H
18 #define DRM_DRMSERVICE_FUZZER_H
19 
20 #include "v1_0/imedia_key_session.h"
21 #include "v1_0/imedia_key_system.h"
22 #include "v1_0/imedia_decrypt_module.h"
23 
24 #define FUZZ_PROJECT_NAME "mediakeysystemndkfactory_fuzzer"
25 
26 namespace OHOS {
27 namespace DrmStandard {
28 class DrmServiceNdkFuzzer : public NoCopyable {
29 public:
30     DrmServiceNdkFuzzer();
31     ~DrmServiceNdkFuzzer();
32     /* KeySystemFactory */
33     bool DrmserviceIsMediaKeySystemSupportedV1Test(uint8_t *rawData, size_t size,
34         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
35     bool DrmserviceIsMediaKeySystemSupportedV2Test(uint8_t *rawData, size_t size,
36         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
37     bool DrmserviceIsMediaKeySystemSupportedV3Test(uint8_t *rawData, size_t size,
38         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
39     bool DrmserviceCreateMediaKeySystemTest(uint8_t *rawData, size_t size,
40         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
41     bool DrmserviceGetMediaKeySystemsTest(uint8_t *rawData, size_t size,
42         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
43     bool DrmserviceGetMediaKeySystemUuidTest(uint8_t *rawData, size_t size,
44         std::shared_ptr<MediaKeySystemFactoryService> mediaKeySystemFactoryServicePtr);
45     /* KeySystem */
46     bool DrmserviceCreateMediaKeySessionTest(uint8_t *rawData, size_t size,
47         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
48     bool DrmserviceCloseMediaKeySessionServiceTest(uint8_t *rawData, size_t size,
49         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
50     bool DrmserviceGenerateKeySystemRequestTest(uint8_t *rawData, size_t size,
51         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
52     bool DrmserviceProcessKeySystemResponseTest(uint8_t *rawData, size_t size,
53         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
54     bool DrmserviceGenerateSetConfigurationStringTest(uint8_t *rawData, size_t size,
55         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
56     bool DrmserviceSetConfigurationStringTest(uint8_t *rawData, size_t size,
57         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
58     bool DrmserviceGetConfigurationStringTest(uint8_t *rawData, size_t size,
59         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
60     bool DrmserviceSetConfigurationByteArrayTest(uint8_t *rawData, size_t size,
61         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
62     bool DrmserviceGetConfigurationByteArrayTest(uint8_t *rawData, size_t size,
63         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
64     bool DrmserviceGetStatisticsTest(uint8_t *rawData, size_t size,
65         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
66     bool DrmserviceReleaseTest(uint8_t *rawData, size_t size,
67         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
68     bool DrmserviceGetMaxContentProtectionLevelTest(uint8_t *rawData, size_t size,
69         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
70     bool DrmserviceGetCertificateStatusTest(uint8_t *rawData, size_t size,
71         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
72     bool DrmserviceGetOfflineMediaKeyIdsTest(uint8_t *rawData, size_t size,
73         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
74     bool DrmserviceGetOfflineMediaKeyStatusTest(uint8_t *rawData, size_t size,
75         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
76     bool DrmserviceClearOfflineMediaKeysTest(uint8_t *rawData, size_t size,
77         std::shared_ptr<MediaKeySystemService> mediaKeySystemServicePtr);
78     /* MediaKeySession */
79     bool DrmserviceGetMediaDecryptModuleTest(uint8_t *rawData, size_t size,
80         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
81     bool DrmserviceGenerateMediaKeyRequestTest(uint8_t *rawData, size_t size,
82         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
83     bool DrmserviceProcessMediaKeyResponseTest(uint8_t *rawData, size_t size,
84         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
85     bool DrmserviceGenerateOfflineReleaseRequestTest(uint8_t *rawData, size_t size,
86         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
87     bool DrmserviceProcessOfflineReleaseResponseTest(uint8_t *rawData, size_t size,
88         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
89     bool DrmserviceCheckMediaKeyStatusTest(uint8_t *rawData, size_t size,
90         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
91     bool DrmserviceRestoreOfflineMediaKeysTest(uint8_t *rawData, size_t size,
92         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
93     bool DrmserviceClearMediaKeysTest(uint8_t *rawData, size_t size,
94         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
95     bool DrmserviceRequireSecureDecoderModuleTest(uint8_t *rawData, size_t size,
96         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
97     bool DrmserviceGetContentProtectionLevelTest(uint8_t *rawData, size_t size,
98         std::shared_ptr<MediaKeySessionService> mediaKeySessionService);
99     bool DrmserviceDecryptMediaDataTest(uint8_t *rawData, size_t size,
100         std::shared_ptr<MediaDecryptModuleService> mediaDecryptService);
101 };
102 
103 class IMediaKeySessionMock : public IMediaKeySession {
104 public:
105     ~IMediaKeySessionMock() override = default;
106 
GenerateMediaKeyRequest(const OHOS::HDI::Drm::V1_0::MediaKeyRequestInfo & mediaKeyRequestInfo,OHOS::HDI::Drm::V1_0::MediaKeyRequest & mediaKeyRequest)107     int32_t GenerateMediaKeyRequest(const OHOS::HDI::Drm::V1_0::MediaKeyRequestInfo &mediaKeyRequestInfo,
108         OHOS::HDI::Drm::V1_0::MediaKeyRequest &mediaKeyRequest) override
109     {
110         // Implement the mock behavior for GenerateMediaKeyRequest
111         return HDF_SUCCESS;
112     }
113 
ProcessMediaKeyResponse(const std::vector<uint8_t> & mediaKeyResponse,std::vector<uint8_t> & mediaKeyId)114     int32_t ProcessMediaKeyResponse(const std::vector<uint8_t> &mediaKeyResponse,
115         std::vector<uint8_t> &mediaKeyId) override
116     {
117         // Implement the mock behavior for ProcessMediaKeyResponse
118         return HDF_SUCCESS;
119     }
120 
CheckMediaKeyStatus(std::map<std::string,std::string> & mediaKeyStatus)121     int32_t CheckMediaKeyStatus(std::map<std::string, std::string> &mediaKeyStatus) override
122     {
123         // Implement the mock behavior for CheckMediaKeyStatus
124         return HDF_SUCCESS;
125     }
126 
ClearMediaKeys()127     int32_t ClearMediaKeys() override
128     {
129         // Implement the mock behavior for ClearMediaKeys
130         return HDF_SUCCESS;
131     }
132 
GetOfflineReleaseRequest(const std::vector<uint8_t> & mediaKeyId,std::vector<uint8_t> & releaseRequest)133     int32_t GetOfflineReleaseRequest(const std::vector<uint8_t> &mediaKeyId,
134         std::vector<uint8_t> &releaseRequest) override
135     {
136         // Implement the mock behavior for GetOfflineReleaseRequest
137         return HDF_SUCCESS;
138     }
139 
ProcessOfflineReleaseResponse(const std::vector<uint8_t> & mediaKeyId,const std::vector<uint8_t> & response)140     int32_t ProcessOfflineReleaseResponse(const std::vector<uint8_t> &mediaKeyId,
141         const std::vector<uint8_t> &response) override
142     {
143         // Implement the mock behavior for ProcessOfflineReleaseResponse
144         return HDF_SUCCESS;
145     }
146 
RestoreOfflineMediaKeys(const std::vector<uint8_t> & mediaKeyId)147     int32_t RestoreOfflineMediaKeys(const std::vector<uint8_t> &mediaKeyId) override
148     {
149         // Implement the mock behavior for RestoreOfflineMediaKeys
150         return HDF_SUCCESS;
151     }
152 
GetContentProtectionLevel(OHOS::HDI::Drm::V1_0::ContentProtectionLevel & level)153     int32_t GetContentProtectionLevel(OHOS::HDI::Drm::V1_0::ContentProtectionLevel &level) override
154     {
155         // Implement the mock behavior for GetContentProtectionLevel
156         return HDF_SUCCESS;
157     }
158 
RequiresSecureDecoderModule(const std::string & mimeType,bool & required)159     int32_t RequiresSecureDecoderModule(const std::string &mimeType, bool &required) override
160     {
161         // Implement the mock behavior for RequiresSecureDecoderModule
162         return HDF_SUCCESS;
163     }
164 
SetCallback(const sptr<OHOS::HDI::Drm::V1_0::IMediaKeySessionCallback> & sessionCallback)165     int32_t SetCallback(const sptr<OHOS::HDI::Drm::V1_0::IMediaKeySessionCallback> &sessionCallback) override
166     {
167         // Implement the mock behavior for SetCallback
168         return HDF_SUCCESS;
169     }
170 
GetMediaDecryptModule(sptr<OHOS::HDI::Drm::V1_0::IMediaDecryptModule> & decryptModule)171     int32_t GetMediaDecryptModule(sptr<OHOS::HDI::Drm::V1_0::IMediaDecryptModule> &decryptModule) override
172     {
173         // Implement the mock behavior for GetMediaDecryptModule
174         return HDF_SUCCESS;
175     }
176 
Destroy()177     int32_t Destroy() override
178     {
179         // Implement the mock behavior for Destroy
180         return HDF_SUCCESS;
181     }
182 
GetVersion(uint32_t & majorVer,uint32_t & minorVer)183     int32_t GetVersion(uint32_t &majorVer, uint32_t &minorVer) override
184     {
185         majorVer = 1;
186         minorVer = 0;
187         return HDF_SUCCESS;
188     }
189 
IsProxy()190     bool IsProxy() override
191     {
192         return false;
193     }
194 
GetDesc()195     const std::u16string GetDesc() override
196     {
197         return u"ohos.hdi.drm.v1_0.IMediaKeySession";
198     }
199 };
200 
201 class IMediaKeySystemMock : public IMediaKeySystem {
202 public:
203     ~IMediaKeySystemMock() override = default;
204 
GetConfigurationString(const std::string & name,std::string & value)205     int32_t GetConfigurationString(const std::string &name, std::string &value) override
206     {
207         value = "mock_value";
208         return HDF_SUCCESS;
209     }
210 
SetConfigurationString(const std::string & name,const std::string & value)211     int32_t SetConfigurationString(const std::string &name, const std::string &value) override
212     {
213         return HDF_SUCCESS;
214     }
215 
GetConfigurationByteArray(const std::string & name,std::vector<uint8_t> & value)216     int32_t GetConfigurationByteArray(const std::string &name, std::vector<uint8_t> &value) override
217     {
218         value = { 0x01, 0x02, 0x03 };
219         return HDF_SUCCESS;
220     }
221 
SetConfigurationByteArray(const std::string & name,const std::vector<uint8_t> & value)222     int32_t SetConfigurationByteArray(const std::string &name, const std::vector<uint8_t> &value) override
223     {
224         return HDF_SUCCESS;
225     }
226 
GetStatistics(std::map<std::string,std::string> & statistics)227     int32_t GetStatistics(std::map<std::string, std::string> &statistics) override
228     {
229         statistics = { { "key1", "value1" }, { "key2", "value2" } };
230         return HDF_SUCCESS;
231     }
232 
GetMaxContentProtectionLevel(OHOS::HDI::Drm::V1_0::ContentProtectionLevel & level)233     int32_t GetMaxContentProtectionLevel(OHOS::HDI::Drm::V1_0::ContentProtectionLevel &level) override
234     {
235         level = OHOS::HDI::Drm::V1_0::ContentProtectionLevel::HW_ENHANCED_SECURE_CRYPTO;
236         return HDF_SUCCESS;
237     }
238 
GenerateKeySystemRequest(std::string & defaultUrl,std::vector<uint8_t> & request)239     int32_t GenerateKeySystemRequest(std::string &defaultUrl, std::vector<uint8_t> &request) override
240     {
241         defaultUrl = "mock_url";
242         request = { 0x04, 0x05, 0x06 };
243         return HDF_SUCCESS;
244     }
245 
ProcessKeySystemResponse(const std::vector<uint8_t> & response)246     int32_t ProcessKeySystemResponse(const std::vector<uint8_t> &response) override
247     {
248         return HDF_SUCCESS;
249     }
250 
GetOemCertificateStatus(OHOS::HDI::Drm::V1_0::CertificateStatus & status)251     int32_t GetOemCertificateStatus(OHOS::HDI::Drm::V1_0::CertificateStatus &status) override
252     {
253         status = OHOS::HDI::Drm::V1_0::CertificateStatus::CERT_STATUS_INVALID;
254         return HDF_SUCCESS;
255     }
256 
SetCallback(const sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystemCallback> & systemCallback)257     int32_t SetCallback(const sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystemCallback> &systemCallback) override
258     {
259         return HDF_SUCCESS;
260     }
261 
CreateMediaKeySession(OHOS::HDI::Drm::V1_0::ContentProtectionLevel level,sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> & keySession)262     int32_t CreateMediaKeySession(OHOS::HDI::Drm::V1_0::ContentProtectionLevel level,
263         sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> &keySession) override
264     {
265         return HDF_SUCCESS;
266     }
267 
GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> & mediaKeyIds)268     int32_t GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &mediaKeyIds) override
269     {
270         mediaKeyIds = { { 0x07, 0x08, 0x09 }, { 0x0A, 0x0B, 0x0C } };
271         return HDF_SUCCESS;
272     }
273 
GetOfflineMediaKeyStatus(const std::vector<uint8_t> & mediaKeyId,OHOS::HDI::Drm::V1_0::OfflineMediaKeyStatus & mediaKeyStatus)274     int32_t GetOfflineMediaKeyStatus(const std::vector<uint8_t> &mediaKeyId,
275         OHOS::HDI::Drm::V1_0::OfflineMediaKeyStatus &mediaKeyStatus) override
276     {
277         mediaKeyStatus = OHOS::HDI::Drm::V1_0::OfflineMediaKeyStatus::OFFLINE_MEDIA_KEY_STATUS_USABLE;
278         return HDF_SUCCESS;
279     }
280 
ClearOfflineMediaKeys(const std::vector<uint8_t> & mediaKeyId)281     int32_t ClearOfflineMediaKeys(const std::vector<uint8_t> &mediaKeyId) override
282     {
283         return HDF_SUCCESS;
284     }
285 
GetOemCertificate(sptr<OHOS::HDI::Drm::V1_0::IOemCertificate> & oemCert)286     int32_t GetOemCertificate(sptr<OHOS::HDI::Drm::V1_0::IOemCertificate> &oemCert) override
287     {
288         return HDF_SUCCESS;
289     }
290 
Destroy()291     int32_t Destroy() override
292     {
293         return HDF_SUCCESS;
294     }
295 
GetVersion(uint32_t & majorVer,uint32_t & minorVer)296     int32_t GetVersion(uint32_t &majorVer, uint32_t &minorVer) override
297     {
298         majorVer = 1;
299         minorVer = 0;
300         return HDF_SUCCESS;
301     }
302 
IsProxy()303     bool IsProxy() override
304     {
305         return false;
306     }
307 
GetDesc()308     const std::u16string GetDesc() override
309     {
310         return u"mock_descriptor";
311     }
312 };
313 
314 class IMediaDecryptModuleMock : public IMediaDecryptModule {
315 public:
316     ~IMediaDecryptModuleMock() override = default;
317 
DecryptMediaData(bool secure,const OHOS::HDI::Drm::V1_0::CryptoInfo & cryptoInfo,const OHOS::HDI::Drm::V1_0::DrmBuffer & srcBuffer,const OHOS::HDI::Drm::V1_0::DrmBuffer & destBuffer)318     int32_t DecryptMediaData(bool secure, const OHOS::HDI::Drm::V1_0::CryptoInfo &cryptoInfo,
319         const OHOS::HDI::Drm::V1_0::DrmBuffer &srcBuffer, const OHOS::HDI::Drm::V1_0::DrmBuffer &destBuffer) override
320     {
321         return HDF_SUCCESS;
322     }
323 
Release()324     int32_t Release() override
325     {
326         return HDF_SUCCESS;
327     }
328 
GetVersion(uint32_t & majorVer,uint32_t & minorVer)329     int32_t GetVersion(uint32_t &majorVer, uint32_t &minorVer) override
330     {
331         majorVer = 1;
332         minorVer = 0;
333         return HDF_SUCCESS;
334     }
335 
IsProxy()336     bool IsProxy() override
337     {
338         return false;
339     }
340 
GetDesc()341     const std::u16string GetDesc() override
342     {
343         return u"mock_media_decrypt_module";
344     }
345 };
346 } // namespace DrmStandard
347 bool FuzzSystemFactoryNdk(uint8_t *data, size_t size);
348 bool FuzzMediaKeySystemFactoryNdk(uint8_t *data, size_t size);
349 } // namesapce OHOS
350 
351 #endif // DRM_MEDIAKEYSYSTEMNDKFACTORY_FUZZER_H