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 "cmgetusercertinfo_fuzzer.h"
17
18 #include "cert_manager_api.h"
19 #include "cm_fuzz_test_common.h"
20 #include "cm_test_common.h"
21
22 namespace {
23 const uint32_t CM_BLOB_COUNT = 2;
24 const uint32_t MAX_BOOL = 2;
25 }
26
27 using namespace CmFuzzTest;
28 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
30 {
31 uint32_t minSize = sizeof(struct CmBlob) * CM_BLOB_COUNT + sizeof(uint32_t) + sizeof(uint32_t);
32 uint8_t *myData = nullptr;
33 if (!CopyMyData(data, size, minSize, &myData)) {
34 return false;
35 }
36
37 uint32_t remainSize = static_cast<uint32_t>(size);
38 uint32_t offset = 0;
39
40 struct CmBlob userCertUri = { 0, nullptr };
41 if (!GetCmBlobFromBuffer(myData, &remainSize, &offset, &userCertUri)) {
42 CmFree(myData);
43 return false;
44 }
45
46 uint32_t store;
47 if (!GetUintFromBuffer(myData, &remainSize, &offset, &store)) {
48 CmFree(myData);
49 return false;
50 }
51
52 uint32_t status;
53 if (!GetUintFromBuffer(myData, &remainSize, &offset, &status)) {
54 CmFree(myData);
55 return false;
56 }
57
58 struct CmBlob certInfo = { 0, nullptr };
59 if (!GetCmBlobFromBuffer(myData, &remainSize, &offset, &certInfo)) {
60 CmFree(myData);
61 return false;
62 }
63
64 struct CertInfo userCertInfo = {
65 .uri = "uri",
66 .certAlias = "alias",
67 .status = status % MAX_BOOL,
68 .issuerName = "issuerName",
69 .subjectName = "subjectName",
70 .serial = "serial",
71 .notBefore = "notBefore",
72 .notAfter = "notAfter",
73 .fingerprintSha256 = "sha256",
74 .certInfo = certInfo
75 };
76
77 CertmanagerTest::MockHapToken mockHap;
78 (void)CmGetUserCertInfo(&userCertUri, store, &userCertInfo);
79
80 CmFree(myData);
81 return true;
82 }
83 }
84
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88 /* Run your code on data */
89 OHOS::DoSomethingInterestingWithMyAPI(data, size);
90 return 0;
91 }
92
93