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