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 #ifndef CM_FUZZ_TEST_COMMON_H
17 #define CM_FUZZ_TEST_COMMON_H
18 
19 #include "cert_manager_service_ipc_interface_code.h"
20 #include "cm_mem.h"
21 #include "cm_param.h"
22 #include "cm_type.h"
23 #include "securec.h"
24 
25 namespace CmFuzzTest {
26 constexpr uint32_t CERT_INFO_LEN = sizeof(uint32_t) + MAX_LEN_CERTIFICATE + sizeof(uint32_t) +
27     MAX_LEN_CERT_ALIAS + sizeof(uint32_t);
28 
29 constexpr uint32_t CERT_LIST_LEN = sizeof(uint32_t) + (sizeof(uint32_t) + MAX_LEN_SUBJECT_NAME + sizeof(uint32_t) +
30     sizeof(uint32_t) + MAX_LEN_URI + sizeof(uint32_t) + MAX_LEN_CERT_ALIAS) * MAX_COUNT_CERTIFICATE;
31 
32 bool GetUintFromBuffer(uint8_t *srcData, uint32_t *remSize, uint32_t *offset, uint32_t *outVal);
33 
34 bool GetCmBlobFromBuffer(uint8_t *srcData, uint32_t *remSize, uint32_t *offset, struct CmBlob *outBlob);
35 
36 bool GetCertListFromBuffer(uint8_t *srcData, uint32_t *remSize, uint32_t *offset, struct CertList *outList);
37 
38 bool GetCertInfoFromBuffer(uint8_t *srcData, uint32_t *remSize, uint32_t *offset, struct CertInfo *outInfo);
39 
40 bool TenPercentChanceOfBeingTrue(uint8_t *srcData, uint32_t *remSize, uint32_t *offset);
41 
42 bool CopyMyData(const uint8_t *data, const size_t size, const uint32_t minSize, uint8_t **myData);
43 
44 bool ConstructParamSet(uint8_t *srcData, uint32_t *remainSize, uint32_t *offset,
45     CertManagerInterfaceCode code, struct CmParamSet **paramSetOut);
46 
47 bool IpcServiceApiFuzzerTest(const uint8_t *data, const size_t size, CertManagerInterfaceCode code,
48     bool isParamsetToBlob, void (*ipcServiceApi)(const struct CmBlob *, struct CmBlob *, const struct CmContext *));
49 } // namespace CmFuzzTest
50 #endif /* CM_FUZZ_TEST_COMMON_H */
51