1 /*
2  * Copyright (c) 2022 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 "authdevice_fuzzer.h"
17 
18 namespace OHOS {
OnError(int64_t requestId,int operationCode,int errorCode,const char * errorReturn)19     void OnError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn) {}
20 
OnFinish(int64_t requestId,int operationCode,const char * authReturn)21     void OnFinish(int64_t requestId, int operationCode, const char *authReturn) {}
22 
OnSessionKeyReturned(int64_t requestId,const uint8_t * sessionKey,uint32_t sessionKeyLen)23     void OnSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) {}
24 
OnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)25     bool OnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
26     {
27         return true;
28     }
29 
OnRequest(int64_t requestId,int operationCode,const char * reqParam)30     char *OnRequest(int64_t requestId, int operationCode, const char* reqParam)
31     {
32         return nullptr;
33     }
34 
FuzzDoAuthDevice(const uint8_t * data,size_t size)35     bool FuzzDoAuthDevice(const uint8_t* data, size_t size)
36     {
37         const GroupAuthManager *gaInstance = GetGaInstance();
38         if (gaInstance == nullptr) {
39             return false;
40         }
41         if (data == nullptr) {
42             return false;
43         }
44         if (size < sizeof(int64_t)) {
45             return false;
46         }
47         const int32_t *osAccountId = reinterpret_cast<const int32_t *>(data);
48         const int64_t *authReqId = reinterpret_cast<const int64_t *>(data);
49         std::string authParams(reinterpret_cast<const char *>(data), size);
50         DeviceAuthCallback gaCallback;
51         gaCallback.onError = OnError;
52         gaCallback.onFinish = OnFinish;
53         gaCallback.onSessionKeyReturned = OnSessionKeyReturned;
54         gaCallback.onTransmit = OnTransmit;
55         gaCallback.onRequest = OnRequest;
56         gaInstance->authDevice(*osAccountId, *authReqId, authParams.c_str(), &gaCallback);
57         return true;
58     }
59 }
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     OHOS::FuzzDoAuthDevice(data, size);
66     return 0;
67 }
68 
69