1 /*
2 * Copyright (c) 2022-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_proxy.h"
17
18 #include <cinttypes>
19
20 #include "iam_logger.h"
21
22 #define LOG_TAG "USER_IDM_SDK"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
UserIdmProxy(const sptr<IRemoteObject> & object)27 UserIdmProxy::UserIdmProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<UserIdmInterface>(object)
28 {
29 }
30
OpenSession(int32_t userId,std::vector<uint8_t> & challenge)31 int32_t UserIdmProxy::OpenSession(int32_t userId, std::vector<uint8_t> &challenge)
32 {
33 MessageParcel data;
34 MessageParcel reply;
35
36 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
37 IAM_LOGE("failed to write descriptor");
38 return WRITE_PARCEL_ERROR;
39 }
40 if (!data.WriteInt32(userId)) {
41 IAM_LOGE("failed to write userId");
42 return WRITE_PARCEL_ERROR;
43 }
44
45 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_OPEN_SESSION, data, reply);
46 if (!ret) {
47 return GENERAL_ERROR;
48 }
49 if (!reply.ReadUInt8Vector(&challenge)) {
50 IAM_LOGE("failed to read challenge");
51 return READ_PARCEL_ERROR;
52 }
53 return SUCCESS;
54 }
55
CloseSession(int32_t userId)56 void UserIdmProxy::CloseSession(int32_t userId)
57 {
58 MessageParcel data;
59 MessageParcel reply;
60
61 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
62 IAM_LOGE("failed to write descriptor");
63 return;
64 }
65 if (!data.WriteInt32(userId)) {
66 IAM_LOGE("failed to write userId");
67 return;
68 }
69
70 SendRequest(UserIdmInterfaceCode::USER_IDM_CLOSE_SESSION, data, reply);
71 }
72
GetCredentialInfo(int32_t userId,AuthType authType,const sptr<IdmGetCredInfoCallbackInterface> & callback)73 int32_t UserIdmProxy::GetCredentialInfo(int32_t userId, AuthType authType,
74 const sptr<IdmGetCredInfoCallbackInterface> &callback)
75 {
76 if (callback == nullptr) {
77 IAM_LOGE("callback is nullptr");
78 return GENERAL_ERROR;
79 }
80 MessageParcel data;
81 MessageParcel reply;
82
83 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
84 IAM_LOGE("failed to write descriptor");
85 return WRITE_PARCEL_ERROR;
86 }
87 if (!data.WriteInt32(userId)) {
88 IAM_LOGE("failed to write userId");
89 return WRITE_PARCEL_ERROR;
90 }
91 if (!data.WriteInt32(authType)) {
92 IAM_LOGE("failed to write authType");
93 return WRITE_PARCEL_ERROR;
94 }
95 if (!data.WriteRemoteObject(callback->AsObject())) {
96 IAM_LOGE("failed to write callback");
97 return WRITE_PARCEL_ERROR;
98 }
99
100 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_GET_CRED_INFO, data, reply);
101 if (!ret) {
102 return GENERAL_ERROR;
103 }
104 int32_t result = GENERAL_ERROR;
105 if (!reply.ReadInt32(result)) {
106 IAM_LOGE("failed to read result");
107 }
108 return result;
109 }
110
GetSecInfo(int32_t userId,const sptr<IdmGetSecureUserInfoCallbackInterface> & callback)111 int32_t UserIdmProxy::GetSecInfo(int32_t userId, const sptr<IdmGetSecureUserInfoCallbackInterface> &callback)
112 {
113 if (callback == nullptr) {
114 IAM_LOGE("callback is nullptr");
115 return GENERAL_ERROR;
116 }
117 MessageParcel data;
118 MessageParcel reply;
119
120 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
121 IAM_LOGE("failed to write descriptor");
122 return WRITE_PARCEL_ERROR;
123 }
124 if (!data.WriteInt32(userId)) {
125 IAM_LOGE("failed to write userId");
126 return WRITE_PARCEL_ERROR;
127 }
128 if (!data.WriteRemoteObject(callback->AsObject())) {
129 IAM_LOGE("failed to write callback");
130 return WRITE_PARCEL_ERROR;
131 }
132
133 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_GET_SEC_INFO, data, reply);
134 if (!ret) {
135 SecUserInfo secUserInfo = {};
136 callback->OnSecureUserInfo(secUserInfo);
137 return GENERAL_ERROR;
138 }
139 int32_t result = GENERAL_ERROR;
140 if (!reply.ReadInt32(result)) {
141 IAM_LOGE("failed to read result");
142 }
143 return result;
144 }
145
AddCredential(int32_t userId,const CredentialPara & credPara,const sptr<IdmCallbackInterface> & callback,bool isUpdate)146 void UserIdmProxy::AddCredential(int32_t userId, const CredentialPara &credPara,
147 const sptr<IdmCallbackInterface> &callback, bool isUpdate)
148 {
149 if (callback == nullptr) {
150 IAM_LOGE("callback is nullptr");
151 return;
152 }
153 MessageParcel data;
154 MessageParcel reply;
155
156 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
157 IAM_LOGE("failed to write descriptor");
158 return;
159 }
160 if (!data.WriteInt32(userId)) {
161 IAM_LOGE("failed to write userId");
162 return;
163 }
164 if (!data.WriteInt32(credPara.authType)) {
165 IAM_LOGE("failed to write authType");
166 return;
167 }
168 if (!data.WriteInt32(credPara.pinType)) {
169 IAM_LOGE("failed to write pinSubType");
170 return;
171 }
172 if (!data.WriteUInt8Vector(credPara.token)) {
173 IAM_LOGE("failed to write token");
174 return;
175 }
176 if (!data.WriteRemoteObject(callback->AsObject())) {
177 IAM_LOGE("failed to write callback");
178 return;
179 }
180
181 SendRequest(UserIdmInterfaceCode::USER_IDM_ADD_CREDENTIAL, data, reply);
182 }
183
UpdateCredential(int32_t userId,const CredentialPara & credPara,const sptr<IdmCallbackInterface> & callback)184 void UserIdmProxy::UpdateCredential(int32_t userId, const CredentialPara &credPara,
185 const sptr<IdmCallbackInterface> &callback)
186 {
187 if (callback == nullptr) {
188 IAM_LOGE("callback is nullptr");
189 return;
190 }
191 MessageParcel data;
192 MessageParcel reply;
193
194 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
195 IAM_LOGE("failed to write descriptor");
196 return;
197 }
198 if (!data.WriteInt32(userId)) {
199 IAM_LOGE("failed to write userId");
200 return;
201 }
202 if (!data.WriteInt32(credPara.authType)) {
203 IAM_LOGE("failed to write authType");
204 return;
205 }
206 if (!data.WriteInt32(credPara.pinType)) {
207 IAM_LOGE("failed to write pinSubType");
208 return;
209 }
210 if (!data.WriteUInt8Vector(credPara.token)) {
211 IAM_LOGE("failed to write token");
212 return;
213 }
214 if (!data.WriteRemoteObject(callback->AsObject())) {
215 IAM_LOGE("failed to write callback");
216 return;
217 }
218
219 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_UPDATE_CREDENTIAL, data, reply);
220 if (!ret) {
221 Attributes extraInfo;
222 callback->OnResult(GENERAL_ERROR, extraInfo);
223 }
224 }
225
Cancel(int32_t userId)226 int32_t UserIdmProxy::Cancel(int32_t userId)
227 {
228 MessageParcel data;
229 MessageParcel reply;
230
231 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
232 IAM_LOGE("failed to write descriptor");
233 return WRITE_PARCEL_ERROR;
234 }
235 if (!data.WriteInt32(userId)) {
236 IAM_LOGE("failed to write userId");
237 return WRITE_PARCEL_ERROR;
238 }
239
240 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_CANCEL, data, reply);
241 if (!ret) {
242 return GENERAL_ERROR;
243 }
244 int32_t result = GENERAL_ERROR;
245 if (!reply.ReadInt32(result)) {
246 IAM_LOGE("failed to read result");
247 }
248 return result;
249 }
250
EnforceDelUser(int32_t userId,const sptr<IdmCallbackInterface> & callback)251 int32_t UserIdmProxy::EnforceDelUser(int32_t userId, const sptr<IdmCallbackInterface> &callback)
252 {
253 if (callback == nullptr) {
254 IAM_LOGE("callback is nullptr");
255 return GENERAL_ERROR;
256 }
257 MessageParcel data;
258 MessageParcel reply;
259
260 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
261 IAM_LOGE("failed to write descriptor");
262 return WRITE_PARCEL_ERROR;
263 }
264 if (!data.WriteInt32(userId)) {
265 IAM_LOGE("failed to write userId");
266 return WRITE_PARCEL_ERROR;
267 }
268 if (!data.WriteRemoteObject(callback->AsObject())) {
269 IAM_LOGE("failed to write callback");
270 return WRITE_PARCEL_ERROR;
271 }
272
273 bool ret = SendRequest(UserIdmInterfaceCode::USER_IDM_ENFORCE_DEL_USER, data, reply);
274 if (!ret) {
275 Attributes attr;
276 callback->OnResult(GENERAL_ERROR, attr);
277 return GENERAL_ERROR;
278 }
279 int32_t result = GENERAL_ERROR;
280 if (!reply.ReadInt32(result)) {
281 IAM_LOGE("failed to read result");
282 }
283 return result;
284 }
285
DelUser(int32_t userId,const std::vector<uint8_t> authToken,const sptr<IdmCallbackInterface> & callback)286 void UserIdmProxy::DelUser(int32_t userId, const std::vector<uint8_t> authToken,
287 const sptr<IdmCallbackInterface> &callback)
288 {
289 if (callback == nullptr) {
290 IAM_LOGE("callback is nullptr");
291 return;
292 }
293 MessageParcel data;
294 MessageParcel reply;
295
296 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
297 IAM_LOGE("failed to write descriptor");
298 return;
299 }
300 if (!data.WriteInt32(userId)) {
301 IAM_LOGE("failed to write userId");
302 return;
303 }
304 if (!data.WriteUInt8Vector(authToken)) {
305 IAM_LOGE("failed to write authToken");
306 return;
307 }
308 if (!data.WriteRemoteObject(callback->AsObject())) {
309 IAM_LOGE("failed to write callback");
310 return;
311 }
312
313 SendRequest(UserIdmInterfaceCode::USER_IDM_DEL_USER, data, reply);
314 }
315
DelCredential(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,const sptr<IdmCallbackInterface> & callback)316 void UserIdmProxy::DelCredential(int32_t userId, uint64_t credentialId,
317 const std::vector<uint8_t> &authToken, const sptr<IdmCallbackInterface> &callback)
318 {
319 if (callback == nullptr) {
320 IAM_LOGE("callback is nullptr");
321 return;
322 }
323 MessageParcel data;
324 MessageParcel reply;
325
326 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
327 IAM_LOGE("failed to write descriptor");
328 return;
329 }
330 if (!data.WriteInt32(userId)) {
331 IAM_LOGE("failed to write userId");
332 return;
333 }
334 if (!data.WriteUint64(credentialId)) {
335 IAM_LOGE("failed to write credentialId");
336 return;
337 }
338 if (!data.WriteUInt8Vector(authToken)) {
339 IAM_LOGE("failed to write authToken");
340 return;
341 }
342 if (!data.WriteRemoteObject(callback->AsObject())) {
343 IAM_LOGE("failed to write callback");
344 return;
345 }
346
347 SendRequest(UserIdmInterfaceCode::USER_IDM_DEL_CRED, data, reply);
348 }
349
ClearRedundancyCredential(const sptr<IdmCallbackInterface> & callback)350 void UserIdmProxy::ClearRedundancyCredential(const sptr<IdmCallbackInterface> &callback)
351 {
352 if (callback == nullptr) {
353 IAM_LOGE("callback is nullptr");
354 return;
355 }
356 MessageParcel data;
357 MessageParcel reply;
358
359 if (!data.WriteInterfaceToken(UserIdmProxy::GetDescriptor())) {
360 IAM_LOGE("failed to write descriptor");
361 return;
362 }
363
364 if (!data.WriteRemoteObject(callback->AsObject())) {
365 IAM_LOGE("failed to write callback");
366 return;
367 }
368
369 SendRequest(UserIdmInterfaceCode::USER_IDM_CLEAR_REDUNDANCY_CRED, data, reply);
370 }
371
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)372 bool UserIdmProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
373 {
374 IAM_LOGI("code = %{public}u", code);
375 sptr<IRemoteObject> remote = Remote();
376 if (remote == nullptr) {
377 IAM_LOGE("failed to get remote");
378 return false;
379 }
380 MessageOption option(MessageOption::TF_SYNC);
381 int32_t result = remote->SendRequest(code, data, reply, option);
382 if (result != OHOS::NO_ERROR) {
383 IAM_LOGE("failed to send request, result = %{public}d", result);
384 return false;
385 }
386 return true;
387 }
388 } // namespace UserAuth
389 } // namespace UserIam
390 } // namespace OHOS