• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "cmappcert_fuzzer.h"
17 
18 #include "cm_fuzz_test_common.h"
19 #include "cm_cert_data_rsa.h"
20 #include "cm_ipc_client_serialization.h"
21 #include "cm_ipc_service.h"
22 #include "cm_param.h"
23 #include "cert_manager_api.h"
24 #include "cm_type.h"
25 
26 using namespace CmFuzzTest;
27 namespace OHOS {
28     constexpr uint32_t TEST_USERID = 100;
29     constexpr uint32_t MIN_DATA_USE_TIME = 4;
InstallAppCert(uint8_t * tmpData,uint32_t * remainSize,uint32_t * offset,uint32_t store,struct CmBlob * keyUri)30     static bool InstallAppCert(uint8_t *tmpData, uint32_t *remainSize, uint32_t *offset, uint32_t store,
31         struct CmBlob *keyUri)
32     {
33         uint8_t certAliasBuf[] = "keyA";
34         struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
35         if (TenPercentChanceOfBeingTrue(tmpData, remainSize, offset)) {
36             if (!GetCmBlobFromBuffer(tmpData, remainSize, offset, keyUri)) {
37                 return false;
38             }
39         }
40 
41         const struct CmBlob appCert = { sizeof(g_rsa2048P12CertInfo), const_cast<uint8_t *>(g_rsa2048P12CertInfo) };
42         const struct CmBlob appCertPwd = { sizeof(g_certPwd), const_cast<uint8_t *>(g_certPwd) };
43         struct CmParam params01[] = {
44             { .tag = CM_TAG_PARAM0_BUFFER, .blob = appCert },
45             { .tag = CM_TAG_PARAM1_BUFFER, .blob = appCertPwd },
46             { .tag = CM_TAG_PARAM2_BUFFER, .blob = certAlias },
47             { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = store },
48             { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = TEST_USERID },
49         };
50         struct CmParamSet *paramSet01 = NULL;
51         int32_t ret = CmParamsToParamSet(params01, CM_ARRAY_SIZE(params01), &paramSet01);
52         if (ret != CM_SUCCESS) {
53             return false;
54         }
55         struct CmBlob paramSetBlob = { paramSet01->paramSetSize, reinterpret_cast<uint8_t *>(paramSet01) };
56 
57         (void)CmIpcServiceInstallAppCert(&paramSetBlob, keyUri, nullptr);
58         CmFreeParamSet(&paramSet01);
59         return true;
60     }
61 
GetAllAppCert(uint8_t * tmpData,uint32_t * remainSize,uint32_t * offset,uint32_t store)62     static bool GetAllAppCert(uint8_t *tmpData, uint32_t *remainSize, uint32_t *offset, uint32_t store)
63     {
64         struct CmParam params02[] = { { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = store } };
65         struct CmParamSet *paramSet02 = NULL;
66         int32_t ret = CmParamsToParamSet(params02, CM_ARRAY_SIZE(params02), &paramSet02);
67         if (ret != CM_SUCCESS) {
68             return false;
69         }
70         struct CmBlob paramSetBlob = { paramSet02->paramSetSize, reinterpret_cast<uint8_t *>(paramSet02) };
71 
72         (void)CmIpcServiceGetAppCertList(&paramSetBlob, nullptr, nullptr);
73         CmFreeParamSet(&paramSet02);
74         return true;
75     }
76 
GetAppCert(uint8_t * tmpData,uint32_t * remainSize,uint32_t * offset,uint32_t store,struct CmBlob * keyUri)77     static bool GetAppCert(uint8_t *tmpData, uint32_t *remainSize, uint32_t *offset, uint32_t store,
78         struct CmBlob *keyUri)
79     {
80         if (TenPercentChanceOfBeingTrue(tmpData, remainSize, offset)) {
81             if (!GetCmBlobFromBuffer(tmpData, remainSize, offset, keyUri)) {
82                 return false;
83             }
84         }
85 
86         struct CmParam params03[] = {
87             { .tag = CM_TAG_PARAM0_BUFFER, .blob = *keyUri },
88             { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = store },
89         };
90         struct CmParamSet *paramSet03 = NULL;
91         int32_t ret = CmParamsToParamSet(params03, CM_ARRAY_SIZE(params03), &paramSet03);
92         if (ret != CM_SUCCESS) {
93             return false;
94         }
95         struct CmBlob paramSetBlob = { paramSet03->paramSetSize, reinterpret_cast<uint8_t *>(paramSet03) };
96 
97         (void)CmIpcServiceGetAppCert(&paramSetBlob, nullptr, nullptr);
98         CmFreeParamSet(&paramSet03);
99         return true;
100     }
101 
UnInstallAppCert(uint8_t * tmpData,uint32_t * remainSize,uint32_t * offset,uint32_t store,struct CmBlob * keyUri)102     static bool UnInstallAppCert(uint8_t *tmpData, uint32_t *remainSize, uint32_t *offset, uint32_t store,
103         struct CmBlob *keyUri)
104     {
105         if (TenPercentChanceOfBeingTrue(tmpData, remainSize, offset)) {
106             if (!GetCmBlobFromBuffer(tmpData, remainSize, offset, keyUri)) {
107                 return false;
108             }
109         }
110 
111         struct CmParam params04[] = {
112             { .tag = CM_TAG_PARAM0_BUFFER, .blob = *keyUri },
113             { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = store },
114         };
115         struct CmParamSet *paramSet04 = NULL;
116         int32_t ret = CmParamsToParamSet(params04, CM_ARRAY_SIZE(params04), &paramSet04);
117         if (ret != CM_SUCCESS) {
118             return false;
119         }
120         struct CmBlob paramSetBlob = { paramSet04->paramSetSize, reinterpret_cast<uint8_t *>(paramSet04) };
121 
122         (void)CmIpcServiceUninstallAppCert(&paramSetBlob, nullptr, nullptr);
123         CmFreeParamSet(&paramSet04);
124         return true;
125     }
126 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)127     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
128     {
129         uint8_t *tmpData = nullptr;
130         if (!CopyMyData(data, size, sizeof(uint32_t) * MIN_DATA_USE_TIME, &tmpData)) {
131             return false;
132         }
133 
134         uint32_t remainSize = static_cast<uint32_t>(size);
135         uint32_t offset = 0;
136         SetATPermission();
137 
138         uint32_t store;
139         if (!GetUintFromBuffer(tmpData, &remainSize, &offset, &store)) {
140             CmFree(tmpData);
141             return false;
142         }
143         store %= CM_SYS_CREDENTIAL_STORE + 1;
144         bool ret = false;
145         uint8_t uriBuf[MAX_LEN_URI] = {0};
146         struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
147 
148         do {
149             if (!InstallAppCert(tmpData, &remainSize, &offset, store, &keyUri)) {
150                 break;
151             }
152 
153             if (!GetAllAppCert(tmpData, &remainSize, &offset, store)) {
154                 break;
155             }
156 
157             if (!GetAppCert(tmpData, &remainSize, &offset, store, &keyUri)) {
158                 break;
159             }
160 
161             if (!UnInstallAppCert(tmpData, &remainSize, &offset, store, &keyUri)) {
162                 break;
163             }
164             ret = true;
165         } while (0);
166         (void)CmIpcServiceUninstallAllAppCert(nullptr, nullptr, nullptr);
167         CmFree(tmpData);
168         return ret;
169     }
170 }
171 
172 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)173 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
174 {
175     /* Run your code on data */
176     OHOS::DoSomethingInterestingWithMyAPI(data, size);
177     return 0;
178 }
179 
180