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