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