1 /*
2 * Copyright (c) 2025 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 <fuzzer/FuzzedDataProvider.h>
17 #include "dm_negotiate_process_fuzzer.h"
18
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include <string>
21
22 #include "device_manager_service_listener.h"
23 #include "dm_auth_state.h"
24 #include "dm_negotiate_process.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28
RespQueryProxyAcceseeIdsFuzzTest(const uint8_t * data,size_t size)29 void RespQueryProxyAcceseeIdsFuzzTest(const uint8_t* data, size_t size)
30 {
31 if ((data == nullptr) || (size < sizeof(int32_t))) {
32 return;
33 }
34 FuzzedDataProvider fdp(data, size);
35 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
36 context->IsProxyBind = true;
37 context->accessee.userId = fdp.ConsumeIntegral<int32_t>();
38 DmProxyAuthContext dmProxyAuthContext;
39 dmProxyAuthContext.proxyAccessee.bundleName = fdp.ConsumeRandomLengthString();
40 dmProxyAuthContext.proxyAccessee.tokenId = fdp.ConsumeIntegral<int64_t>();
41 context->subjectProxyOnes.push_back(dmProxyAuthContext);
42 std::shared_ptr<AuthSinkNegotiateStateMachine> authSinkPtr = std::make_shared<AuthSinkNegotiateStateMachine>();
43 authSinkPtr->RespQueryProxyAcceseeIds(context);
44 }
45
GetSinkProxyCredTypeForP2PFuzzTest(const uint8_t * data,size_t size)46 void GetSinkProxyCredTypeForP2PFuzzTest(const uint8_t* data, size_t size)
47 {
48 if ((data == nullptr) || (size < sizeof(int32_t))) {
49 return;
50 }
51 FuzzedDataProvider fdp(data, size);
52 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
53 context->IsProxyBind = true;
54 context->accessee.userId = fdp.ConsumeIntegral<int32_t>();
55 DmProxyAuthContext dmProxyAuthContext;
56 dmProxyAuthContext.proxyAccessee.bundleName = fdp.ConsumeRandomLengthString();
57 dmProxyAuthContext.proxyAccessee.tokenId = fdp.ConsumeIntegral<int64_t>();
58 dmProxyAuthContext.proxyAccessee.credInfoJson = fdp.ConsumeRandomLengthString();
59 dmProxyAuthContext.proxyAccessee.aclTypeList = fdp.ConsumeRandomLengthString();
60 dmProxyAuthContext.proxyAccessee.credTypeList = fdp.ConsumeRandomLengthString();
61 context->subjectProxyOnes.push_back(dmProxyAuthContext);
62 std::shared_ptr<AuthSinkNegotiateStateMachine> authSinkPtr = std::make_shared<AuthSinkNegotiateStateMachine>();
63 std::vector<std::string> deleteCredInfo;
64 authSinkPtr->GetSinkProxyCredTypeForP2P(context, deleteCredInfo);
65 DistributedDeviceProfile::AccessControlProfile profile;
66 authSinkPtr->GetSinkProxyAclInfoForP2P(context, profile);
67 }
68
DmNegotiateProcessFuzzTest(const uint8_t * data,size_t size)69 void DmNegotiateProcessFuzzTest(const uint8_t* data, size_t size)
70 {
71 if ((data == nullptr) || (size < sizeof(int32_t))) {
72 return;
73 }
74 FuzzedDataProvider fdp(data, size);
75 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
76 context->needBind = fdp.ConsumeBool();
77 context->needAgreeCredential = fdp.ConsumeBool();
78 context->needAuth = fdp.ConsumeBool();
79 NoCredNoAclImportAuthType noCredNoAclImportAuthType;
80 noCredNoAclImportAuthType.NegotiateHandle(context);
81 NoCredNoAclInputAuthType noCredNoAclInputAuthType;
82 noCredNoAclInputAuthType.NegotiateHandle(context);
83 IdentCredNoAclImportAuthType identCredNoAclImportAuthType;
84 identCredNoAclImportAuthType.NegotiateHandle(context);
85 IdentCredNoAclInputAuthType identCredNoAclInputAuthType;
86 identCredNoAclInputAuthType.NegotiateHandle(context);
87 IdentCredIdentAclImportAuthType identCredIdentAclImportAuthType;
88 identCredIdentAclImportAuthType.NegotiateHandle(context);
89 IdentCredIdentAclInputAuthType identCredIdentAclInputAuthType;
90 identCredIdentAclInputAuthType.NegotiateHandle(context);
91 IdentCredP2pAclImportAuthType identCredP2pAclImportAuthType;
92 identCredP2pAclImportAuthType.NegotiateHandle(context);
93 IdentCredP2pAclInputAuthType identCredP2pAclInputAuthType;
94 identCredP2pAclInputAuthType.NegotiateHandle(context);
95 ShareCredNoAclImportAuthType shareCredNoAclImportAuthType;
96 shareCredNoAclImportAuthType.NegotiateHandle(context);
97 ShareCredNoAclInputAuthType shareCredNoAclInputAuthType;
98 shareCredNoAclInputAuthType.NegotiateHandle(context);
99 ShareCredShareAclImportAuthType shareCredShareAclImportAuthType;
100 shareCredShareAclImportAuthType.NegotiateHandle(context);
101 ShareCredShareAclInputAuthType shareCredShareAclInputAuthType;
102 shareCredShareAclInputAuthType.NegotiateHandle(context);
103 ShareCredP2pAclImportAuthType shareCredP2pAclImportAuthType;
104 shareCredP2pAclImportAuthType.NegotiateHandle(context);
105 ShareCredP2pAclInputAuthType shareCredP2pAclInputAuthType;
106 shareCredP2pAclInputAuthType.NegotiateHandle(context);
107 P2pCredNoAclImportAuthType p2pCredNoAclImportAuthType;
108 p2pCredNoAclImportAuthType.NegotiateHandle(context);
109 P2pCredNoAclInputAuthType p2pCredNoAclInputAuthType;
110 p2pCredNoAclInputAuthType.NegotiateHandle(context);
111 P2pCredP2pAclImportAuthType p2pCredP2pAclImportAuthType;
112 p2pCredP2pAclImportAuthType.NegotiateHandle(context);
113 P2pCredP2pAclInputAuthType p2pCredP2pAclInputAuthType;
114 p2pCredP2pAclInputAuthType.NegotiateHandle(context);
115
116 RespQueryProxyAcceseeIdsFuzzTest(data, size);
117 GetSinkProxyCredTypeForP2PFuzzTest(data, size);
118 }
119 } // namespace DistributedHardware
120 } // namespace OHOS
121
122 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)123 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
124 {
125 /* Run your code on data */
126 OHOS::DistributedHardware::DmNegotiateProcessFuzzTest(data, size);
127 return 0;
128 }