• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cmgetcertinfo_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 buffSize = sizeof(struct CmBlob) * CM_BLOB_COUNT + sizeof(uint32_t) + sizeof(uint32_t);
32         uint8_t *myData = nullptr;
33         if (!CopyMyData(data, size, buffSize, &myData)) {
34             return false;
35         }
36 
37         uint32_t remainSize = static_cast<uint32_t>(size);
38         uint32_t offset = 0;
39         struct CmBlob sysUri = { 0, nullptr };
40         if (!GetCmBlobFromBuffer(myData, &remainSize, &offset, &sysUri)) {
41             CmFree(myData);
42             return false;
43         }
44 
45         uint32_t store;
46         if (!GetUintFromBuffer(myData, &remainSize, &offset, &store)) {
47             CmFree(myData);
48             return false;
49         }
50 
51         uint32_t status;
52         if (!GetUintFromBuffer(myData, &remainSize, &offset, &status)) {
53             CmFree(myData);
54             return false;
55         }
56 
57         struct CmBlob certInfo = { 0, nullptr };
58         if (!GetCmBlobFromBuffer(myData, &remainSize, &offset, &certInfo)) {
59             CmFree(myData);
60             return false;
61         }
62 
63         struct CertInfo sysCertInfo = {
64             .uri = "uri",
65             .certAlias = "alias",
66             .status = status % MAX_BOOL,
67             .issuerName = "issuerName",
68             .subjectName = "subjectName",
69             .serial = "serial",
70             .notBefore = "notBefore",
71             .notAfter = "notAfter",
72             .fingerprintSha256 = "sha256",
73             .certInfo = certInfo
74         };
75 
76         CertmanagerTest::MockHapToken mockHap;
77         (void)CmGetCertInfo(&sysUri, store, &sysCertInfo);
78 
79         CmFree(myData);
80         return true;
81     }
82 }
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87     /* Run your code on data */
88     OHOS::DoSomethingInterestingWithMyAPI(data, size);
89     return 0;
90 }
91