1 /*
2  * Copyright (c) 2022-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 "user_auth_stub.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 
21 #include "iam_logger.h"
22 #include "iam_scope_guard.h"
23 #include "iam_common_defines.h"
24 #include "user_auth_callback_proxy.h"
25 #include "user_auth_event_listener_proxy.h"
26 #include "widget_callback_proxy.h"
27 #include "user_auth_common_defines.h"
28 
29 #define LOG_TAG "USER_AUTH_SA"
30 
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
34 namespace {
35     const uint32_t MAX_ATTR_COUNT = 512;
36 } // namespace
37 
38 // When true is passed into IRemoteStub, sa will process request serially.
UserAuthStub()39 UserAuthStub::UserAuthStub() : IRemoteStub(true) {};
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t UserAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43     IAM_LOGD("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
44     if (UserAuthStub::GetDescriptor() != data.ReadInterfaceToken()) {
45         IAM_LOGE("descriptor is not matched");
46         return GENERAL_ERROR;
47     }
48     switch (code) {
49         case UserAuthInterfaceCode::USER_AUTH_GET_AVAILABLE_STATUS:
50             return GetAvailableStatusStub(data, reply);
51         case UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY:
52             return GetPropertyStub(data, reply);
53         case UserAuthInterfaceCode::USER_AUTH_SET_PROPERTY:
54             return SetPropertyStub(data, reply);
55         case UserAuthInterfaceCode::USER_AUTH_AUTH:
56             return AuthStub(data, reply);
57         case UserAuthInterfaceCode::USER_AUTH_AUTH_WIDGET:
58             return AuthWidgetStub(data, reply);
59         case UserAuthInterfaceCode::USER_AUTH_AUTH_USER:
60             return AuthUserStub(data, reply);
61         case UserAuthInterfaceCode::USER_AUTH_CANCEL_AUTH:
62             return CancelAuthOrIdentifyStub(data, reply);
63         case UserAuthInterfaceCode::USER_AUTH_IDENTIFY:
64             return IdentifyStub(data, reply);
65         case UserAuthInterfaceCode::USER_AUTH_CANCEL_IDENTIFY:
66             return CancelAuthOrIdentifyStub(data, reply);
67         case UserAuthInterfaceCode::USER_AUTH_GET_VERSION:
68             return GetVersionStub(data, reply);
69         case UserAuthInterfaceCode::USER_AUTH_NOTICE:
70             return NoticeStub(data, reply);
71         case UserAuthInterfaceCode::USER_AUTH_REG_WIDGET_CB:
72             return RegisterWidgetCallbackStub(data, reply);
73         case UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE:
74             return GetEnrolledStateStub(data, reply);
75         case UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER:
76             return RegistUserAuthSuccessEventListenerStub(data, reply);
77         case UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER:
78             return UnRegistUserAuthSuccessEventListenerStub(data, reply);
79         case UserAuthInterfaceCode::USER_AUTH_SET_CLOBAL_CONFIG_PARAM:
80             return SetGlobalConfigParamStub(data, reply);
81         case UserAuthInterfaceCode::USER_AUTH_PREPARE_REMOTE_AUTH:
82             return PrepareRemoteAuthStub(data, reply);
83         default:
84             return OnRemoteRequestExt(code, data, reply, option);
85     }
86 }
87 
OnRemoteRequestExt(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int32_t UserAuthStub::OnRemoteRequestExt(uint32_t code, MessageParcel &data,
89     MessageParcel &reply, MessageOption &option)
90 {
91     switch (code) {
92         case UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY_BY_ID:
93                 return GetPropertyByIdStub(data, reply);
94         default:
95             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
96     }
97 }
98 
GetAvailableStatusStub(MessageParcel & data,MessageParcel & reply)99 int32_t UserAuthStub::GetAvailableStatusStub(MessageParcel &data, MessageParcel &reply)
100 {
101     IAM_LOGI("enter");
102     ON_SCOPE_EXIT(IAM_LOGI("leave"));
103 
104     int32_t authType;
105     uint32_t authTrustLevel;
106     int32_t apiVersion;
107     int32_t userId;
108     bool isSpecificUserId = false;
109     if (!data.ReadBool(isSpecificUserId)) {
110         IAM_LOGE("failed to read isSpecificUserId");
111         return READ_PARCEL_ERROR;
112     }
113     if (isSpecificUserId && !data.ReadInt32(userId)) {
114         IAM_LOGE("failed to read userId");
115         return READ_PARCEL_ERROR;
116     }
117     if (!data.ReadInt32(authType)) {
118         IAM_LOGE("failed to read authType");
119         return READ_PARCEL_ERROR;
120     }
121     if (!data.ReadUint32(authTrustLevel)) {
122         IAM_LOGE("failed to read authTrustLevel");
123         return READ_PARCEL_ERROR;
124     }
125     if (!data.ReadInt32(apiVersion)) {
126         IAM_LOGE("failed to read apiVersion");
127         return READ_PARCEL_ERROR;
128     }
129 
130     int32_t result = GENERAL_ERROR;
131     if (isSpecificUserId) {
132         result =  GetAvailableStatus(apiVersion, userId, static_cast<AuthType>(authType),
133             static_cast<AuthTrustLevel>(authTrustLevel));
134     } else {
135         result =  GetAvailableStatus(apiVersion, static_cast<AuthType>(authType),
136             static_cast<AuthTrustLevel>(authTrustLevel));
137     }
138 
139     if (!reply.WriteInt32(result)) {
140         IAM_LOGE("failed to write GetAvailableStatus result");
141         return WRITE_PARCEL_ERROR;
142     }
143     return SUCCESS;
144 }
145 
GetPropertyStub(MessageParcel & data,MessageParcel & reply)146 int32_t UserAuthStub::GetPropertyStub(MessageParcel &data, MessageParcel &reply)
147 {
148     IAM_LOGI("enter");
149     ON_SCOPE_EXIT(IAM_LOGI("leave"));
150 
151     int32_t userId;
152     int32_t authType;
153     std::vector<uint32_t> keys;
154 
155     if (!data.ReadInt32(userId)) {
156         IAM_LOGE("failed to read userId");
157         return READ_PARCEL_ERROR;
158     }
159     if (!data.ReadInt32(authType)) {
160         IAM_LOGE("failed to read authType");
161         return READ_PARCEL_ERROR;
162     }
163     if (!data.ReadUInt32Vector(&keys)) {
164         IAM_LOGE("failed to read attribute keys");
165         return READ_PARCEL_ERROR;
166     }
167     std::vector<Attributes::AttributeKey> attrKeys;
168     if (keys.empty()) {
169         IAM_LOGE("the attribute key vector is empty");
170         return GENERAL_ERROR;
171     }
172     if (keys.size() > MAX_ATTR_COUNT) {
173         IAM_LOGE("the attribute key vector size exceed limit");
174         return GENERAL_ERROR;
175     }
176     attrKeys.resize(keys.size());
177     std::transform(keys.begin(), keys.end(), attrKeys.begin(), [](uint32_t key) {
178         return static_cast<Attributes::AttributeKey>(key);
179     });
180 
181     sptr<IRemoteObject> obj = data.ReadRemoteObject();
182     if (obj == nullptr) {
183         IAM_LOGE("failed to read remote object");
184         return READ_PARCEL_ERROR;
185     }
186     sptr<GetExecutorPropertyCallbackInterface> callback = iface_cast<GetExecutorPropertyCallbackProxy>(obj);
187     if (callback == nullptr) {
188         IAM_LOGE("GetExecutorPropertyCallbackInterface is nullptr");
189         return GENERAL_ERROR;
190     }
191 
192     GetProperty(userId, static_cast<AuthType>(authType), attrKeys, callback);
193     return SUCCESS;
194 }
195 
GetPropertyByIdStub(MessageParcel & data,MessageParcel & reply)196 int32_t UserAuthStub::GetPropertyByIdStub(MessageParcel &data, MessageParcel &reply)
197 {
198     IAM_LOGI("enter");
199     ON_SCOPE_EXIT(IAM_LOGI("leave"));
200 
201     uint64_t credentialId;
202     std::vector<uint32_t> keys;
203 
204     if (!data.ReadUint64(credentialId)) {
205         IAM_LOGE("failed to read credentialId");
206         return READ_PARCEL_ERROR;
207     }
208     if (!data.ReadUInt32Vector(&keys)) {
209         IAM_LOGE("failed to read attribute keys");
210         return READ_PARCEL_ERROR;
211     }
212     std::vector<Attributes::AttributeKey> attrKeys;
213     if (keys.empty()) {
214         IAM_LOGE("the attribute key vector is empty");
215         return GENERAL_ERROR;
216     }
217     if (keys.size() > MAX_ATTR_COUNT) {
218         IAM_LOGE("the attribute key vector size exceed limit");
219         return GENERAL_ERROR;
220     }
221     attrKeys.resize(keys.size());
222     std::transform(keys.begin(), keys.end(), attrKeys.begin(), [](uint32_t key) {
223         return static_cast<Attributes::AttributeKey>(key);
224     });
225 
226     sptr<IRemoteObject> obj = data.ReadRemoteObject();
227     if (obj == nullptr) {
228         IAM_LOGE("failed to read remote object");
229         return READ_PARCEL_ERROR;
230     }
231     sptr<GetExecutorPropertyCallbackInterface> callback = iface_cast<GetExecutorPropertyCallbackProxy>(obj);
232     if (callback == nullptr) {
233         IAM_LOGE("GetExecutorPropertyCallbackInterface is nullptr");
234         return GENERAL_ERROR;
235     }
236 
237     GetPropertyById(credentialId, attrKeys, callback);
238     return SUCCESS;
239 }
240 
SetPropertyStub(MessageParcel & data,MessageParcel & reply)241 int32_t UserAuthStub::SetPropertyStub(MessageParcel &data, MessageParcel &reply)
242 {
243     IAM_LOGI("enter");
244     ON_SCOPE_EXIT(IAM_LOGI("leave"));
245 
246     int32_t userId;
247     int32_t authType;
248     std::vector<uint8_t> attr;
249 
250     if (!data.ReadInt32(userId)) {
251         IAM_LOGE("failed to read userId");
252         return READ_PARCEL_ERROR;
253     }
254     if (!data.ReadInt32(authType)) {
255         IAM_LOGE("failed to read authType");
256         return READ_PARCEL_ERROR;
257     }
258     if (!data.ReadUInt8Vector(&attr)) {
259         IAM_LOGE("failed to read attributes");
260         return READ_PARCEL_ERROR;
261     }
262     Attributes attributes(attr);
263 
264     sptr<IRemoteObject> obj = data.ReadRemoteObject();
265     if (obj == nullptr) {
266         IAM_LOGE("failed to read remote object");
267         return READ_PARCEL_ERROR;
268     }
269     sptr<SetExecutorPropertyCallbackInterface> callback = iface_cast<SetExecutorPropertyCallbackProxy>(obj);
270     if (callback == nullptr) {
271         IAM_LOGE("SetExecutorPropertyCallbackInterface is nullptr");
272         return GENERAL_ERROR;
273     }
274 
275     SetProperty(userId, static_cast<AuthType>(authType), attributes, callback);
276     return SUCCESS;
277 }
278 
AuthStub(MessageParcel & data,MessageParcel & reply)279 int32_t UserAuthStub::AuthStub(MessageParcel &data, MessageParcel &reply)
280 {
281     IAM_LOGI("enter");
282     ON_SCOPE_EXIT(IAM_LOGI("leave"));
283 
284     int32_t apiVersion;
285     if (!data.ReadInt32(apiVersion)) {
286         IAM_LOGE("failed to read apiVersion");
287         return READ_PARCEL_ERROR;
288     }
289 
290     AuthParamInner authParam;
291     if (!ReadAuthParam(data, authParam)) {
292         IAM_LOGE("failed to read auth param");
293         return READ_PARCEL_ERROR;
294     }
295 
296     sptr<IRemoteObject> obj = data.ReadRemoteObject();
297     if (obj == nullptr) {
298         IAM_LOGE("failed to read remote object");
299         return READ_PARCEL_ERROR;
300     }
301     sptr<UserAuthCallbackInterface> callback = iface_cast<UserAuthCallbackProxy>(obj);
302     if (callback == nullptr) {
303         IAM_LOGE("UserAuthCallbackInterface is nullptr");
304         return GENERAL_ERROR;
305     }
306 
307     uint64_t contextId = Auth(apiVersion, authParam.challenge, authParam.authType, authParam.authTrustLevel,
308         callback);
309     if (!reply.WriteUint64(contextId)) {
310         IAM_LOGE("failed to write AuthUser result");
311         return WRITE_PARCEL_ERROR;
312     }
313     return SUCCESS;
314 }
315 
AuthWidgetStub(MessageParcel & data,MessageParcel & reply)316 int32_t UserAuthStub::AuthWidgetStub(MessageParcel &data, MessageParcel &reply)
317 {
318     IAM_LOGI("enter");
319     ON_SCOPE_EXIT(IAM_LOGI("leave"));
320 
321     AuthParamInner authParam;
322     WidgetParam widgetParam;
323     if (!ReadWidgetAuthParam(data, authParam)) {
324         IAM_LOGE("failed to read widget auth param");
325         return ResultCode::READ_PARCEL_ERROR;
326     }
327 
328     if (!ReadWidgetParam(data, widgetParam)) {
329         IAM_LOGE("failed to read widget param");
330         return ResultCode::READ_PARCEL_ERROR;
331     }
332 
333     sptr<IRemoteObject> obj = data.ReadRemoteObject();
334     if (obj == nullptr) {
335         IAM_LOGE("failed to read remote object");
336         return ResultCode::READ_PARCEL_ERROR;
337     }
338     sptr<UserAuthCallbackInterface> callback = iface_cast<UserAuthCallbackProxy>(obj);
339     if (callback == nullptr) {
340         IAM_LOGE("UserAuthCallbackInterface is nullptr");
341         return ResultCode::GENERAL_ERROR;
342     }
343 
344     int32_t apiVersion;
345     if (!data.ReadInt32(apiVersion)) {
346         IAM_LOGE("failed to read apiVersion");
347         return ResultCode::READ_PARCEL_ERROR;
348     }
349 
350     uint64_t contextId = AuthWidget(apiVersion, authParam, widgetParam, callback);
351     if (!reply.WriteUint64(contextId)) {
352         IAM_LOGE("failed to write contextId");
353         return ResultCode::WRITE_PARCEL_ERROR;
354     }
355     return ResultCode::SUCCESS;
356 }
357 
ReadWidgetAuthParam(MessageParcel & data,AuthParamInner & authParam)358 bool UserAuthStub::ReadWidgetAuthParam(MessageParcel &data, AuthParamInner &authParam)
359 {
360     if (!data.ReadInt32(authParam.userId)) {
361         IAM_LOGE("failed to read userId");
362         return false;
363     }
364     if (!data.ReadBool(authParam.isUserIdSpecified)) {
365         IAM_LOGE("failed to read isUserIdSpecified");
366         return false;
367     }
368     if (!data.ReadUInt8Vector(&authParam.challenge)) {
369         IAM_LOGE("failed to read challenge");
370         return false;
371     }
372     std::vector<int32_t> atList;
373     if (!data.ReadInt32Vector(&atList)) {
374         IAM_LOGE("failed to read authTypeList");
375         return false;
376     }
377     for (auto at : atList) {
378         authParam.authTypes.push_back(static_cast<AuthType>(at));
379     }
380 
381     uint32_t authTrustLevel;
382     if (!data.ReadUint32(authTrustLevel)) {
383         IAM_LOGE("failed to read authTrustLevel");
384         return false;
385     }
386     authParam.authTrustLevel = static_cast<AuthTrustLevel>(authTrustLevel);
387 
388     if (!data.ReadBool(authParam.reuseUnlockResult.isReuse)) {
389         IAM_LOGE("failed to read isReuse unlock result");
390         return false;
391     }
392     authParam.reuseUnlockResult.reuseDuration = 0;
393     uint32_t reuseMode = AUTH_TYPE_IRRELEVANT;
394     if (authParam.reuseUnlockResult.isReuse) {
395         if (!data.ReadUint32(reuseMode)) {
396             IAM_LOGE("failed to read reuseMode");
397             return false;
398         }
399         if (!data.ReadUint64(authParam.reuseUnlockResult.reuseDuration)) {
400             IAM_LOGE("failed to read reuseDuration");
401             return false;
402         }
403     }
404     authParam.reuseUnlockResult.reuseMode = static_cast<ReuseMode>(reuseMode);
405     return true;
406 }
407 
ReadWidgetParam(MessageParcel & data,WidgetParam & widgetParam)408 bool UserAuthStub::ReadWidgetParam(MessageParcel &data, WidgetParam &widgetParam)
409 {
410     if (!data.ReadString(widgetParam.title)) {
411         IAM_LOGE("failed to read title");
412         return READ_PARCEL_ERROR;
413     }
414     if (!data.ReadString(widgetParam.navigationButtonText)) {
415         IAM_LOGE("failed to read navigationButtonText");
416         return READ_PARCEL_ERROR;
417     }
418     int32_t winMode;
419     if (!data.ReadInt32(winMode)) {
420         IAM_LOGE("failed to read window mode");
421         return false;
422     }
423     widgetParam.windowMode = static_cast<WindowModeType>(winMode);
424     return true;
425 }
426 
AuthUserStub(MessageParcel & data,MessageParcel & reply)427 int32_t UserAuthStub::AuthUserStub(MessageParcel &data, MessageParcel &reply)
428 {
429     IAM_LOGI("enter");
430     ON_SCOPE_EXIT(IAM_LOGI("leave"));
431 
432     AuthParamInner authParam;
433     if (!ReadAuthParam(data, authParam)) {
434         IAM_LOGE("failed to read auth param");
435         return READ_PARCEL_ERROR;
436     }
437 
438     std::optional<RemoteAuthParam> remoteAuthParam;
439     if (!ReadRemoteAuthParam(data, remoteAuthParam)) {
440         IAM_LOGE("failed to read auth param");
441         return READ_PARCEL_ERROR;
442     }
443 
444     sptr<IRemoteObject> obj = data.ReadRemoteObject();
445     if (obj == nullptr) {
446         IAM_LOGE("failed to read remote object");
447         return READ_PARCEL_ERROR;
448     }
449     sptr<UserAuthCallbackInterface> callback = iface_cast<UserAuthCallbackProxy>(obj);
450     if (callback == nullptr) {
451         IAM_LOGE("UserAuthCallbackInterface is nullptr");
452         return GENERAL_ERROR;
453     }
454 
455     uint64_t contextId = AuthUser(authParam, remoteAuthParam, callback);
456     if (!reply.WriteUint64(contextId)) {
457         IAM_LOGE("failed to write AuthUser result");
458         return WRITE_PARCEL_ERROR;
459     }
460     return SUCCESS;
461 }
462 
IdentifyStub(MessageParcel & data,MessageParcel & reply)463 int32_t UserAuthStub::IdentifyStub(MessageParcel &data, MessageParcel &reply)
464 {
465     IAM_LOGI("enter");
466     ON_SCOPE_EXIT(IAM_LOGI("leave"));
467 
468     std::vector<uint8_t> challenge;
469     int32_t authType;
470 
471     if (!data.ReadUInt8Vector(&challenge)) {
472         IAM_LOGE("failed to read challenge");
473         return READ_PARCEL_ERROR;
474     }
475     if (!data.ReadInt32(authType)) {
476         IAM_LOGE("failed to read authType");
477         return READ_PARCEL_ERROR;
478     }
479 
480     sptr<IRemoteObject> obj = data.ReadRemoteObject();
481     if (obj == nullptr) {
482         IAM_LOGE("failed to read remote object");
483         return READ_PARCEL_ERROR;
484     }
485     sptr<UserAuthCallbackInterface> callback = iface_cast<UserAuthCallbackProxy>(obj);
486     if (callback == nullptr) {
487         IAM_LOGE("UserAuthCallbackInterface is nullptr");
488         return GENERAL_ERROR;
489     }
490 
491     uint64_t contextId = Identify(challenge, static_cast<AuthType>(authType), callback);
492     if (!reply.WriteUint64(contextId)) {
493         IAM_LOGE("failed to write Identify result");
494         return WRITE_PARCEL_ERROR;
495     }
496     return SUCCESS;
497 }
498 
CancelAuthOrIdentifyStub(MessageParcel & data,MessageParcel & reply)499 int32_t UserAuthStub::CancelAuthOrIdentifyStub(MessageParcel &data, MessageParcel &reply)
500 {
501     IAM_LOGI("enter");
502     ON_SCOPE_EXIT(IAM_LOGI("leave"));
503 
504     uint64_t contextId;
505 
506     if (!data.ReadUint64(contextId)) {
507         IAM_LOGE("failed to read contextId");
508         return READ_PARCEL_ERROR;
509     }
510 
511     int32_t result = CancelAuthOrIdentify(contextId);
512     if (!reply.WriteInt32(result)) {
513         IAM_LOGE("failed to write CancelAuthOrIdentify result");
514         return WRITE_PARCEL_ERROR;
515     }
516     return SUCCESS;
517 }
518 
GetVersionStub(MessageParcel & data,MessageParcel & reply)519 int32_t UserAuthStub::GetVersionStub(MessageParcel &data, MessageParcel &reply)
520 {
521     IAM_LOGI("enter");
522     ON_SCOPE_EXIT(IAM_LOGI("leave"));
523 
524     int32_t version;
525     int32_t result = GetVersion(version);
526     if (!reply.WriteInt32(version)) {
527         IAM_LOGE("failed to write GetVersion version");
528         return WRITE_PARCEL_ERROR;
529     }
530     if (!reply.WriteInt32(result)) {
531         IAM_LOGE("failed to write GetVersion result");
532         return WRITE_PARCEL_ERROR;
533     }
534     return SUCCESS;
535 }
536 
NoticeStub(MessageParcel & data,MessageParcel & reply)537 int32_t UserAuthStub::NoticeStub(MessageParcel &data, MessageParcel &reply)
538 {
539     IAM_LOGI("enter");
540     ON_SCOPE_EXIT(IAM_LOGI("leave"));
541 
542     int32_t type;
543     if (!data.ReadInt32(type)) {
544         IAM_LOGE("failed to read type");
545         return ResultCode::READ_PARCEL_ERROR;
546     }
547     NoticeType noticeType = static_cast<NoticeType>(type);
548     if (noticeType != WIDGET_NOTICE) {
549         IAM_LOGE("NoticeStub unsupport notice type");
550         return ResultCode::GENERAL_ERROR;
551     }
552     std::string eventData = data.ReadString();
553 
554     int32_t result = Notice(noticeType, eventData);
555     if (!reply.WriteInt32(result)) {
556         IAM_LOGE("failed to write notice result");
557         return ResultCode::WRITE_PARCEL_ERROR;
558     }
559     IAM_LOGI("noticeStub success");
560     return ResultCode::SUCCESS;
561 }
562 
RegisterWidgetCallbackStub(MessageParcel & data,MessageParcel & reply)563 int32_t UserAuthStub::RegisterWidgetCallbackStub(MessageParcel &data, MessageParcel &reply)
564 {
565     IAM_LOGI("enter");
566     ON_SCOPE_EXIT(IAM_LOGI("leave"));
567 
568     int32_t version;
569     if (!data.ReadInt32(version)) {
570         IAM_LOGE("failed to read version");
571         return READ_PARCEL_ERROR;
572     }
573 
574     sptr<IRemoteObject> obj = data.ReadRemoteObject();
575     if (obj == nullptr) {
576         IAM_LOGE("failed to read remote object");
577         return READ_PARCEL_ERROR;
578     }
579     sptr<WidgetCallbackInterface> callback = iface_cast<WidgetCallbackProxy>(obj);
580     if (callback == nullptr) {
581         IAM_LOGE("RegisterWidgetCallbackStub is nullptr");
582         return GENERAL_ERROR;
583     }
584     int32_t result = RegisterWidgetCallback(version, callback);
585     if (!reply.WriteInt32(result)) {
586         IAM_LOGE("failed to write register widget callback result");
587         return WRITE_PARCEL_ERROR;
588     }
589     IAM_LOGI("RegisterWidgetCallbackStub success");
590     return SUCCESS;
591 }
592 
GetEnrolledStateStub(MessageParcel & data,MessageParcel & reply)593 int32_t UserAuthStub::GetEnrolledStateStub(MessageParcel &data, MessageParcel &reply)
594 {
595     IAM_LOGI("enter");
596     ON_SCOPE_EXIT(IAM_LOGI("leave"));
597 
598     int32_t apiVersion;
599     if (!data.ReadInt32(apiVersion)) {
600         IAM_LOGE("failed to read apiVersion");
601         return READ_PARCEL_ERROR;
602     }
603 
604     int32_t authType;
605     if (!data.ReadInt32(authType)) {
606         IAM_LOGE("failed to read authType");
607         return READ_PARCEL_ERROR;
608     }
609     EnrolledState enrolledState = {};
610     int32_t ret = GetEnrolledState(apiVersion, static_cast<AuthType>(authType), enrolledState);
611     if (!reply.WriteInt32(ret)) {
612         IAM_LOGE("failed to write ret");
613         return WRITE_PARCEL_ERROR;
614     }
615     if (!reply.WriteUint64(enrolledState.credentialDigest)) {
616         IAM_LOGE("failed to write credentialDigest");
617         return WRITE_PARCEL_ERROR;
618     }
619     if (!reply.WriteUint16(enrolledState.credentialCount)) {
620         IAM_LOGE("failed to write credentialCount");
621         return WRITE_PARCEL_ERROR;
622     }
623     return SUCCESS;
624 }
625 
RegistUserAuthSuccessEventListenerStub(MessageParcel & data,MessageParcel & reply)626 int32_t UserAuthStub::RegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply)
627 {
628     IAM_LOGI("enter");
629     ON_SCOPE_EXIT(IAM_LOGI("leave"));
630 
631     std::vector<int32_t> authType;
632     if (!data.ReadInt32Vector(&authType)) {
633         IAM_LOGE("failed to read authTypeList");
634         return READ_PARCEL_ERROR;
635     }
636     sptr<IRemoteObject> obj = data.ReadRemoteObject();
637     if (obj == nullptr) {
638         IAM_LOGE("failed to read remote object");
639         return READ_PARCEL_ERROR;
640     }
641     sptr<AuthEventListenerInterface> listener = iface_cast<AuthEventListenerProxy>(obj);
642     if (listener == nullptr) {
643         IAM_LOGE("authEventListener listener is nullptr");
644         return GENERAL_ERROR;
645     }
646     std::vector<AuthType> authTypeList;
647     for (auto &iter : authType) {
648         authTypeList.emplace_back(static_cast<AuthType>(iter));
649     }
650     int32_t result = RegistUserAuthSuccessEventListener(authTypeList, listener);
651     if (!reply.WriteInt32(result)) {
652         IAM_LOGE("failed to write regist event listener result");
653         return WRITE_PARCEL_ERROR;
654     }
655     IAM_LOGI("RegistUserAuthSuccessEventListenerStub success");
656     return SUCCESS;
657 }
658 
UnRegistUserAuthSuccessEventListenerStub(MessageParcel & data,MessageParcel & reply)659 int32_t UserAuthStub::UnRegistUserAuthSuccessEventListenerStub(MessageParcel &data, MessageParcel &reply)
660 {
661     IAM_LOGI("enter");
662     ON_SCOPE_EXIT(IAM_LOGI("leave"));
663 
664     sptr<IRemoteObject> obj = data.ReadRemoteObject();
665     if (obj == nullptr) {
666         IAM_LOGE("failed to read remote object");
667         return READ_PARCEL_ERROR;
668     }
669     sptr<AuthEventListenerInterface> listener = iface_cast<AuthEventListenerProxy>(obj);
670     if (listener == nullptr) {
671         IAM_LOGE("authEventListener listener is nullptr");
672         return GENERAL_ERROR;
673     }
674     int32_t result = UnRegistUserAuthSuccessEventListener(listener);
675     if (!reply.WriteInt32(result)) {
676         IAM_LOGE("failed to write regist event listener result");
677         return WRITE_PARCEL_ERROR;
678     }
679     IAM_LOGI("UnRegistUserAuthSuccessEventListener success");
680     return SUCCESS;
681 }
682 
SetGlobalConfigParamStub(MessageParcel & data,MessageParcel & reply)683 int32_t UserAuthStub::SetGlobalConfigParamStub(MessageParcel &data, MessageParcel &reply)
684 {
685     IAM_LOGI("enter");
686     ON_SCOPE_EXIT(IAM_LOGI("leave"));
687 
688     GlobalConfigParam globalConfigParam = {};
689     int32_t globalConfigType;
690     if (!data.ReadInt32(globalConfigType)) {
691         IAM_LOGE("failed to read globalConfigType");
692         return READ_PARCEL_ERROR;
693     }
694     globalConfigParam.type = static_cast<GlobalConfigType>(globalConfigType);
695 
696     if (globalConfigParam.type == GlobalConfigType::PIN_EXPIRED_PERIOD) {
697         if (!data.ReadInt64(globalConfigParam.value.pinExpiredPeriod)) {
698             IAM_LOGE("failed to read pinExpiredPeriod");
699             return READ_PARCEL_ERROR;
700         }
701     }
702 
703     int32_t ret = SetGlobalConfigParam(globalConfigParam);
704     if (!reply.WriteInt32(ret)) {
705         IAM_LOGE("failed to write ret");
706         return WRITE_PARCEL_ERROR;
707     }
708     return SUCCESS;
709 }
710 
PrepareRemoteAuthStub(MessageParcel & data,MessageParcel & reply)711 int32_t UserAuthStub::PrepareRemoteAuthStub(MessageParcel &data, MessageParcel &reply)
712 {
713     IAM_LOGI("enter");
714     ON_SCOPE_EXIT(IAM_LOGI("leave"));
715 
716     std::string networkId;
717     if (!data.ReadString(networkId)) {
718         IAM_LOGE("failed to read networkId");
719         return READ_PARCEL_ERROR;
720     }
721 
722     sptr<IRemoteObject> obj = data.ReadRemoteObject();
723     if (obj == nullptr) {
724         IAM_LOGE("failed to read remote object");
725         return READ_PARCEL_ERROR;
726     }
727     sptr<UserAuthCallbackInterface> callback = iface_cast<UserAuthCallbackProxy>(obj);
728     if (callback == nullptr) {
729         IAM_LOGE("UserAuthCallbackInterface is nullptr");
730         return GENERAL_ERROR;
731     }
732 
733     int32_t result = PrepareRemoteAuth(networkId, callback);
734     if (!reply.WriteInt32(result)) {
735         IAM_LOGE("failed to write PrepareRemoteAuth result");
736         return WRITE_PARCEL_ERROR;
737     }
738 
739     return SUCCESS;
740 }
741 
ReadAuthParam(MessageParcel & data,AuthParamInner & authParam)742 bool UserAuthStub::ReadAuthParam(MessageParcel &data, AuthParamInner &authParam)
743 {
744     if (!data.ReadInt32(authParam.userId)) {
745         IAM_LOGE("failed to read userId");
746         return false;
747     }
748     if (!data.ReadUInt8Vector(&authParam.challenge)) {
749         IAM_LOGE("failed to read challenge");
750         return false;
751     }
752     int32_t authTypeInt;
753     if (!data.ReadInt32(authTypeInt)) {
754         IAM_LOGE("failed to read authType");
755         return false;
756     }
757     authParam.authType = static_cast<AuthType>(authTypeInt);
758 
759     std::vector<int32_t> authTypeInts;
760     if (!data.ReadInt32Vector(&authTypeInts)) {
761         IAM_LOGE("failed to read authTypeInts");
762         return false;
763     }
764 
765     for (auto val : authTypeInts) {
766         authParam.authTypes.push_back(static_cast<AuthType>(val));
767     }
768 
769     uint32_t authTrustLevelUint;
770     if (!data.ReadUint32(authTrustLevelUint)) {
771         IAM_LOGE("failed to read authTrustLevel");
772         return false;
773     }
774     authParam.authTrustLevel = static_cast<AuthTrustLevel>(authTrustLevelUint);
775 
776     uint32_t authIntent;
777     if (!data.ReadUint32(authIntent)) {
778         IAM_LOGE("failed to write authIntent");
779         return false;
780     }
781     authParam.authIntent = static_cast<AuthIntent>(authIntent);
782 
783     return true;
784 }
785 
ReadRemoteAuthParam(MessageParcel & data,std::optional<RemoteAuthParam> & remoteAuthParam)786 bool UserAuthStub::ReadRemoteAuthParam(MessageParcel &data, std::optional<RemoteAuthParam> &remoteAuthParam)
787 {
788     bool hasRemoteAuthParam;
789     if (!data.ReadBool(hasRemoteAuthParam)) {
790         IAM_LOGE("failed to read hasRemoteAuthParam");
791         return false;
792     }
793 
794     if (!hasRemoteAuthParam) {
795         remoteAuthParam = std::nullopt;
796         return true;
797     }
798     remoteAuthParam = RemoteAuthParam{};
799 
800     if (!ReadOptionalString(data, remoteAuthParam->verifierNetworkId)) {
801         IAM_LOGE("failed to read verifierNetworkId");
802         return false;
803     }
804 
805     if (!ReadOptionalString(data, remoteAuthParam->collectorNetworkId)) {
806         IAM_LOGE("failed to read collectorNetworkId");
807         return false;
808     }
809 
810     if (!ReadOptionalUint32(data, remoteAuthParam->collectorTokenId)) {
811         IAM_LOGE("failed to read collectorTokenId");
812         return false;
813     }
814 
815     return true;
816 }
817 
ReadOptionalString(MessageParcel & data,std::optional<std::string> & str)818 bool UserAuthStub::ReadOptionalString(MessageParcel &data, std::optional<std::string> &str)
819 {
820     bool hasStr;
821     if (!data.ReadBool(hasStr)) {
822         IAM_LOGE("failed to read hasStr");
823         return false;
824     }
825 
826     if (hasStr) {
827         std::string readStr;
828         if (!data.ReadString(readStr)) {
829             IAM_LOGE("failed to read value");
830             return false;
831         }
832         str = readStr;
833     } else {
834         str = std::nullopt;
835     }
836     return true;
837 }
ReadOptionalUint32(MessageParcel & data,std::optional<uint32_t> & val)838 bool UserAuthStub::ReadOptionalUint32(MessageParcel &data, std::optional<uint32_t> &val)
839 {
840     bool hasVal;
841     if (!data.ReadBool(hasVal)) {
842         IAM_LOGE("failed to read hasVal");
843         return false;
844     }
845 
846     if (hasVal) {
847         uint32_t readValue;
848         if (!data.ReadUint32(readValue)) {
849             IAM_LOGE("failed to read data");
850             return false;
851         }
852         val = readValue;
853     } else {
854         val = std::nullopt;
855     }
856     return true;
857 }
858 } // namespace UserAuth
859 } // namespace UserIam
860 } // namespace OHOS