1 /*
2 * Copyright (c) 2022-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 #include "cmsendrequest_fuzzer.h"
17
18 #include "cm_fuzz_test_common.h"
19 #include "cm_param.h"
20 #include "cm_request.h"
21 #include "cm_test_common.h"
22
23 using namespace CmFuzzTest;
24 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)25 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
26 {
27 uint32_t minSize = sizeof(CertManagerInterfaceCode) + sizeof(struct CmParamSet) + sizeof(struct CmBlob);
28 uint8_t *myData = nullptr;
29 if (!CopyMyData(data, size, minSize, &myData)) {
30 return false;
31 }
32
33 uint32_t remainSize = static_cast<uint32_t>(size);
34 uint32_t offset = 0;
35
36 CertManagerInterfaceCode type;
37 (void)memcpy_s(&type, sizeof(CertManagerInterfaceCode), myData, sizeof(CertManagerInterfaceCode));
38 type = static_cast<CertManagerInterfaceCode>(static_cast<uint32_t>(type) %
39 (static_cast<uint32_t>(CM_MSG_MAX) - static_cast<uint32_t>(CM_MSG_BASE)) +
40 static_cast<uint32_t>(CM_MSG_BASE));
41 offset += sizeof(uint32_t);
42 remainSize -= sizeof(uint32_t);
43
44 struct CmParamSet *sendParamSet = nullptr;
45 if (ConstructParamSet(myData, &remainSize, &offset, type, &sendParamSet) == false) {
46 CmFree(myData);
47 return false;
48 }
49
50 struct CmBlob inBlob = { sendParamSet->paramSetSize, reinterpret_cast<uint8_t *>(sendParamSet) };
51
52 struct CmBlob outBlob = { 0, nullptr };
53 if (!GetCmBlobFromBuffer(myData, &remainSize, &offset, &outBlob)) {
54 CmFree(myData);
55 CmFreeParamSet(&sendParamSet);
56 return false;
57 }
58
59 CertmanagerTest::SetATPermission();
60 (void)SendRequest(type, &inBlob, &outBlob);
61
62 CmFree(myData);
63 CmFreeParamSet(&sendParamSet);
64 return true;
65 }
66 }
67
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71 /* Run your code on data */
72 OHOS::DoSomethingInterestingWithMyAPI(data, size);
73 return 0;
74 }
75