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 "user_idm_client_stub_fuzzer.h"
17
18 #include "parcel.h"
19
20 #include "iam_fuzz_test.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "user_idm_client.h"
24 #include "user_idm_callback_service.h"
25
26 #define LOG_TAG "USER_AUTH_SDK"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 namespace {
32 constexpr uint32_t IDM_CALLBACK_CODE_MIN = 0;
33 constexpr uint32_t IDM_CALLBACK_CODE_MAX = 3;
34 const std::u16string IDM_CALLBACK_INTERFACE_TOKEN = u"ohos.useridm.IIDMCallback";
35 constexpr uint32_t GET_CREDINFO_CALLBACK_CODE_MIN = 0;
36 constexpr uint32_t GET_CREDINFO_CALLBACK_CODE_MAX = 2;
37 const std::u16string GET_CREDINFO_CALLBACK_TOKEN = u"ohos.useridm.IGetInfoCallback";
38 constexpr uint32_t GET_SECURE_USERINFO_CALLBACK_CODE_MIN = 0;
39 constexpr uint32_t GET_SECURE_USERINFO_CALLBACK_CODE_MAX = 2;
40 const std::u16string GET_SECURE_USERINFO_CALLBACK_TOKEN = u"ohos.useridm.IGetSecInfoCallback";
41 class DummyUserIdmClientCallback final : public UserIdmClientCallback {
42 public:
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)43 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo)
44 {
45 IAM_LOGI("start");
46 static_cast<void>(module);
47 static_cast<void>(acquireInfo);
48 static_cast<void>(extraInfo);
49 }
50
OnResult(int32_t result,const Attributes & extraInfo)51 void OnResult(int32_t result, const Attributes &extraInfo)
52 {
53 IAM_LOGI("start");
54 static_cast<void>(result);
55 static_cast<void>(extraInfo);
56 }
57 };
58
59 class DummyGetCredentialInfoCallback final : public GetCredentialInfoCallback {
60 public:
OnCredentialInfo(const std::vector<CredentialInfo> & infoList)61 void OnCredentialInfo(const std::vector<CredentialInfo> &infoList)
62 {
63 IAM_LOGI("start");
64 static_cast<void>(infoList);
65 }
66 };
67
68 class DummyGetSecUserInfoCallback final : public GetSecUserInfoCallback {
69 public:
OnSecUserInfo(const SecUserInfo & info)70 void OnSecUserInfo(const SecUserInfo &info)
71 {
72 IAM_LOGI("start");
73 static_cast<void>(info);
74 }
75 };
76
FuzzIdmCallbackStub(const uint8_t * rawData,size_t size)77 bool FuzzIdmCallbackStub(const uint8_t *rawData, size_t size)
78 {
79 IAM_LOGI("start");
80 if (rawData == nullptr) {
81 IAM_LOGI("%{public}s:rawData is null.", __func__);
82 return false;
83 }
84 auto idmCallbackService =
85 Common::MakeShared<IdmCallbackService>(Common::MakeShared<DummyUserIdmClientCallback>());
86 if (idmCallbackService == nullptr) {
87 IAM_LOGI("%{public}s:new idmCallbackService failed.", __func__);
88 return false;
89 }
90 for (uint32_t code = IDM_CALLBACK_CODE_MIN; code < IDM_CALLBACK_CODE_MAX; code++) {
91 MessageParcel data;
92 MessageParcel reply;
93 MessageOption optionSync = MessageOption::TF_SYNC;
94 MessageOption optionAsync = MessageOption::TF_ASYNC;
95 // Sync
96 data.WriteInterfaceToken(IDM_CALLBACK_INTERFACE_TOKEN);
97 data.WriteBuffer(rawData, size);
98 data.RewindRead(0);
99 (void)idmCallbackService->OnRemoteRequest(code, data, reply, optionSync);
100 // Async
101 data.WriteInterfaceToken(IDM_CALLBACK_INTERFACE_TOKEN);
102 data.WriteBuffer(rawData, size);
103 data.RewindRead(0);
104 (void)idmCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
105 }
106 return true;
107 }
108
FuzzIdmGetInfoCallbackStub(const uint8_t * rawData,size_t size)109 bool FuzzIdmGetInfoCallbackStub(const uint8_t *rawData, size_t size)
110 {
111 IAM_LOGI("start");
112 if (rawData == nullptr) {
113 IAM_LOGI("%{public}s:rawData is null.", __func__);
114 return false;
115 }
116 auto idmGetCredInfoCallbackService =
117 Common::MakeShared<IdmGetCredInfoCallbackService>(Common::MakeShared<DummyGetCredentialInfoCallback>());
118 if (idmGetCredInfoCallbackService == nullptr) {
119 IAM_LOGI("%{public}s:new idmGetCredInfoCallbackService failed.", __func__);
120 return false;
121 }
122 for (uint32_t code = GET_CREDINFO_CALLBACK_CODE_MIN; code < GET_CREDINFO_CALLBACK_CODE_MAX; code++) {
123 MessageParcel data;
124 MessageParcel reply;
125 MessageOption optionSync = MessageOption::TF_SYNC;
126 MessageOption optionAsync = MessageOption::TF_ASYNC;
127 // Sync
128 data.WriteInterfaceToken(GET_CREDINFO_CALLBACK_TOKEN);
129 data.WriteBuffer(rawData, size);
130 data.RewindRead(0);
131 (void)idmGetCredInfoCallbackService->OnRemoteRequest(code, data, reply, optionSync);
132 // Async
133 data.WriteInterfaceToken(GET_CREDINFO_CALLBACK_TOKEN);
134 data.WriteBuffer(rawData, size);
135 data.RewindRead(0);
136 (void)idmGetCredInfoCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
137 }
138 return true;
139 }
140
FuzzIdmGetSecureUserInfoCallbackstub(const uint8_t * rawData,size_t size)141 bool FuzzIdmGetSecureUserInfoCallbackstub(const uint8_t *rawData, size_t size)
142 {
143 IAM_LOGI("start");
144 if (rawData == nullptr) {
145 IAM_LOGI("%{public}s:rawData is null.", __func__);
146 return false;
147 }
148 auto idmGetSecureUserInfoCallbackService =
149 Common::MakeShared<IdmGetSecureUserInfoCallbackService>(Common::MakeShared<DummyGetSecUserInfoCallback>());
150 if (idmGetSecureUserInfoCallbackService == nullptr) {
151 IAM_LOGI("%{public}s:new idmGetSecureUserInfoCallbackService failed.", __func__);
152 return false;
153 }
154 for (uint32_t code = GET_SECURE_USERINFO_CALLBACK_CODE_MIN; code < GET_SECURE_USERINFO_CALLBACK_CODE_MAX; code++) {
155 MessageParcel data;
156 MessageParcel reply;
157 MessageOption optionSync = MessageOption::TF_SYNC;
158 MessageOption optionAsync = MessageOption::TF_ASYNC;
159 // Sync
160 data.WriteInterfaceToken(GET_SECURE_USERINFO_CALLBACK_TOKEN);
161 data.WriteBuffer(rawData, size);
162 data.RewindRead(0);
163 (void)idmGetSecureUserInfoCallbackService->OnRemoteRequest(code, data, reply, optionSync);
164 // Async
165 data.WriteInterfaceToken(GET_SECURE_USERINFO_CALLBACK_TOKEN);
166 data.WriteBuffer(rawData, size);
167 data.RewindRead(0);
168 (void)idmGetSecureUserInfoCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
169 }
170 return true;
171 }
172
UserIdmClientFuzzTest(const uint8_t * data,size_t size)173 void UserIdmClientFuzzTest(const uint8_t *data, size_t size)
174 {
175 FuzzIdmCallbackStub(data, size);
176 FuzzIdmGetInfoCallbackStub(data, size);
177 FuzzIdmGetSecureUserInfoCallbackstub(data, size);
178 return;
179 }
180 } // namespace
181 } // namespace UserAuth
182 } // namespace UserIam
183 } // namespace OHOS
184
185 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)186 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
187 {
188 OHOS::UserIam::UserAuth::UserIdmClientFuzzTest(data, size);
189 return 0;
190 }
191