• 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 <string>
17 #include <vector>
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include "device_manager_service.h"
20 #include "device_manager_service_impl.h"
21 #include "authenticate_device_service_impl_fuzzer.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 DmPublishInfo publishInfo = {
29     .publishId = 1234,
30     .mode = DmDiscoverMode::DM_DISCOVER_MODE_ACTIVE,
31     .freq = DmExchangeFreq::DM_MID,
32     .ranging = true,
33 };
34 
35 PeerTargetId peerTargetId = {
36     .deviceId = "deviceId",
37     .brMac = "brMac",
38     .bleMac = "bleMac",
39     .wifiIp = "wifiIp",
40     .wifiPort = 1,
41 };
42 
43 std::vector<DmDeviceInfo> deviceList;
44 std::map<std::string, std::string> bindParam;
45 std::string g_returnJsonStr;
46 int32_t g_eventId = 1;
47 int32_t g_action = 1;
48 int32_t g_authType = -1;
49 uint16_t g_subscribeId = 123;
50 
51 std::string g_reqJsonStr = R"(
52 {
53     "userId":"4269DC28B639681698809A67EDAD08E39F207900038F91EFF95DD042FE2874E4"
54 }
55 )";
56 
57 std::string g_credentialInfo = R"(
58 {
59     "processType" : 1,
60     "authType" : 1,
61     "userId" : "123",
62     "credentialData" :
63     [
64         {
65             "credentialType" : 1,
66             "credentialId" : "104",
67             "authCode" : "1234567812345678123456781234567812345678123456781234567812345678",
68             "serverPk" : "",
69             "pkInfoSignature" : "",
70             "pkInfo" : "",
71             "peerDeviceId" : ""
72         }
73     ]
74 }
75 )";
76 
77 std::string g_deleteInfo = R"(
78 {
79     "processType" : 1,
80     "authType" : 1,
81     "userId" : "123"
82 }
83 )";
84 
AddPermission()85 void AddPermission()
86 {
87     const int32_t permsNum = 3;
88     const int32_t indexZero = 0;
89     const int32_t indexOne = 1;
90     const int32_t indexTwo = 2;
91     uint64_t tokenId;
92     const char *perms[permsNum];
93     perms[indexZero] = "ohos.permission.ACCESS_SERVICE_DM";
94     perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
95     perms[indexTwo] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER";
96     NativeTokenInfoParams infoInstance = {
97         .dcapsNum = 0,
98         .permsNum = permsNum,
99         .aclsNum = 0,
100         .dcaps = NULL,
101         .perms = perms,
102         .acls = NULL,
103         .processName = "device_manager",
104         .aplStr = "system_core",
105     };
106     tokenId = GetAccessTokenId(&infoInstance);
107     SetSelfTokenID(tokenId);
108     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
109 }
110 
AuthenticateDeviceServiceImplFuzzTest(const uint8_t * data,size_t size)111 void AuthenticateDeviceServiceImplFuzzTest(const uint8_t* data, size_t size)
112 {
113     if ((data == nullptr) || (size == 0) || (size < sizeof(int32_t))) {
114         return;
115     }
116 
117     std::string str(reinterpret_cast<const char*>(data), size);
118     FuzzedDataProvider fdp(data, size);
119     int32_t bindLevel = fdp.ConsumeIntegral<int32_t>();
120     AddPermission();
121     DmSubscribeInfo subscribeInfo = {
122         .subscribeId = 0,
123         .mode = DmDiscoverMode::DM_DISCOVER_MODE_ACTIVE,
124         .medium = DmExchangeMedium::DM_AUTO,
125         .freq = DmExchangeFreq::DM_MID,
126         .isSameAccount = true,
127         .isWakeRemote = true,
128     };
129     if (strcpy_s(subscribeInfo.capability, DM_MAX_DEVICE_CAPABILITY_LEN, "capability")) {
130         return;
131     }
132     std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
133     auto deviceManagerServiceImpl = std::make_shared<DeviceManagerServiceImpl>();
134 
135     deviceManagerServiceImpl->Initialize(listener);
136     deviceManagerServiceImpl->RegisterUiStateCallback(str);
137     deviceManagerServiceImpl->RegisterCredentialCallback(str);
138     deviceManagerServiceImpl->UnAuthenticateDevice(str, str, bindLevel);
139     deviceManagerServiceImpl->UnBindDevice(str, str, bindLevel);
140     deviceManagerServiceImpl->SetUserOperation(str, g_action, str);
141     deviceManagerServiceImpl->RequestCredential(g_reqJsonStr, g_returnJsonStr);
142     deviceManagerServiceImpl->ImportCredential(str, g_credentialInfo);
143     deviceManagerServiceImpl->DeleteCredential(str, g_deleteInfo);
144     deviceManagerServiceImpl->MineRequestCredential(str, g_returnJsonStr);
145     deviceManagerServiceImpl->CheckCredential(str, g_reqJsonStr, g_returnJsonStr);
146     deviceManagerServiceImpl->ImportCredential(str, g_reqJsonStr, g_returnJsonStr);
147     deviceManagerServiceImpl->DeleteCredential(str, g_reqJsonStr, g_returnJsonStr);
148     deviceManagerServiceImpl->NotifyEvent(str, g_eventId, str);
149     deviceManagerServiceImpl->GetGroupType(deviceList);
150     deviceManagerServiceImpl->GetUdidHashByNetWorkId(str.c_str(), str);
151     deviceManagerServiceImpl->ImportAuthCode(str, str);
152     deviceManagerServiceImpl->ExportAuthCode(str);
153     deviceManagerServiceImpl->BindTarget(str, peerTargetId, bindParam);
154     deviceManagerServiceImpl->UnRegisterCredentialCallback(str);
155     deviceManagerServiceImpl->UnRegisterUiStateCallback(str);
156     deviceManagerServiceImpl->Release();
157 }
158 }
159 }
160 
161 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)162 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
163 {
164     /* Run your code on data */
165     OHOS::DistributedHardware::AuthenticateDeviceServiceImplFuzzTest(data, size);
166 
167     return 0;
168 }