1 /*
2  * Copyright (c) 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 "hks_service_on_remote_request_fuzzer.h"
17 
18 #include <securec.h>
19 
20 #include "hks_client_service.h"
21 #include "hks_sa.h"
22 #include "huks_service_ipc_interface_code.h"
23 
24 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)25     bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
26     {
27         // Initialize the huks service
28         HksServiceInitialize();
29         const std::u16string SA_KEYSTORE_SERVICE_DESCRIPTOR = u"ohos.security.hks.service";
30         sptr<OHOS::Security::Hks::HksService> ptrInstance = OHOS::Security::Hks::HksService::GetInstance();
31 
32         // use loop to call each service function
33         for (uint32_t msgcode = HKS_MSG_BASE; msgcode < HKS_MSG_MAX; msgcode++) {
34             // init parcel
35             MessageParcel dataParcel;
36             MessageParcel replyParcel;
37             MessageOption optionSync = MessageOption::TF_SYNC;
38             MessageOption optionAsync = MessageOption::TF_ASYNC;
39 
40             // Sync
41             dataParcel.WriteInterfaceToken(SA_KEYSTORE_SERVICE_DESCRIPTOR);
42             dataParcel.WriteUint32(0); // outData
43             dataParcel.WriteUint32(static_cast<uint32_t>(size)); // inData
44             dataParcel.WriteBuffer(data, size);
45             (void)ptrInstance->OnRemoteRequest(msgcode, dataParcel, replyParcel, optionSync);
46 
47             // Async
48             dataParcel.WriteInterfaceToken(SA_KEYSTORE_SERVICE_DESCRIPTOR);
49             dataParcel.WriteUint32(0); // outData
50             dataParcel.WriteUint32(static_cast<uint32_t>(size)); // inData
51             dataParcel.WriteBuffer(data, size);
52             (void)ptrInstance->OnRemoteRequest(msgcode, dataParcel, replyParcel, optionAsync);
53         }
54         return true;
55     }
56 }
57 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60     OHOS::DoSomethingInterestingWithMyAPI(data, size);
61     return 0;
62 }
63