1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "enroll_context.h"
16
17 #include "hisysevent_adapter.h"
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "iam_para2str.h"
21 #include "resource_node_utils.h"
22 #include "schedule_node.h"
23 #include "schedule_node_callback.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
EnrollContext(uint64_t contextId,std::shared_ptr<Enrollment> enroll,std::shared_ptr<ContextCallback> callback)30 EnrollContext::EnrollContext(uint64_t contextId, std::shared_ptr<Enrollment> enroll,
31 std::shared_ptr<ContextCallback> callback)
32 : BaseContext("Enroll", contextId, callback),
33 enroll_(enroll)
34 {
35 }
36
GetContextType() const37 ContextType EnrollContext::GetContextType() const
38 {
39 return CONTEXT_ENROLL;
40 }
41
GetTokenId() const42 uint32_t EnrollContext::GetTokenId() const
43 {
44 return enroll_->GetAccessTokenId();
45 }
46
OnStart()47 bool EnrollContext::OnStart()
48 {
49 IAM_LOGI("%{public}s start", GetDescription());
50 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
51 bool startRet = enroll_->Start(scheduleList_, shared_from_this());
52 if (!startRet) {
53 IAM_LOGE("%{public}s enroll start fail", GetDescription());
54 SetLatestError(enroll_->GetLatestError());
55 return startRet;
56 }
57 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
58 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
59 bool startScheduleRet = scheduleList_[0]->StartSchedule();
60 IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
61 IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(),
62 GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType());
63 return true;
64 }
65
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)66 void EnrollContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
67 {
68 IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
69 uint64_t credentialId = 0;
70 std::shared_ptr<UpdatePinParamInterface> pinInfo;
71 std::optional<uint64_t> secUserId = std::nullopt;
72 bool updateRet = UpdateScheduleResult(scheduleResultAttr, credentialId, pinInfo, secUserId);
73 if (!updateRet) {
74 IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
75 if (resultCode == SUCCESS) {
76 resultCode = GetLatestError();
77 }
78 }
79 InvokeResultCallback(resultCode, credentialId, pinInfo, secUserId);
80 IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
81 }
82
OnStop()83 bool EnrollContext::OnStop()
84 {
85 IAM_LOGI("%{public}s start", GetDescription());
86 if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
87 scheduleList_[0]->StopSchedule();
88 }
89
90 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
91 bool cancelRet = enroll_->Cancel();
92 if (!cancelRet) {
93 IAM_LOGE("%{public}s enroll stop fail", GetDescription());
94 SetLatestError(enroll_->GetLatestError());
95 return cancelRet;
96 }
97 return true;
98 }
99
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,uint64_t & credentialId,std::shared_ptr<UpdatePinParamInterface> & pinInfo,std::optional<uint64_t> & secUserId)100 bool EnrollContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
101 uint64_t &credentialId, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId)
102 {
103 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
104 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
105 std::vector<uint8_t> scheduleResult;
106 bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
107 IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
108 std::shared_ptr<CredentialInfoInterface> infoToDel;
109 bool updateRet = enroll_->Update(scheduleResult, credentialId, infoToDel, pinInfo, secUserId);
110 if (!updateRet) {
111 IAM_LOGE("%{public}s enroll update fail", GetDescription());
112 SetLatestError(enroll_->GetLatestError());
113 return updateRet;
114 }
115
116 return true;
117 }
118
InvokeResultCallback(int32_t resultCode,const uint64_t credentialId,const std::shared_ptr<UpdatePinParamInterface> & pinInfo,std::optional<uint64_t> & secUserId) const119 void EnrollContext::InvokeResultCallback(int32_t resultCode, const uint64_t credentialId,
120 const std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) const
121 {
122 IAM_LOGI("%{public}s start", GetDescription());
123 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
124 Attributes finalResult;
125 if (secUserId.has_value()) {
126 IAM_LOGI("%{public}s get sec user id has value", GetDescription());
127 bool setSecUserIdRet = finalResult.SetUint64Value(Attributes::ATTR_SEC_USER_ID, secUserId.value());
128 IF_FALSE_LOGE_AND_RETURN(setSecUserIdRet == true);
129 }
130 bool setCredIdRet = finalResult.SetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
131 IF_FALSE_LOGE_AND_RETURN(setCredIdRet == true);
132 if (pinInfo != nullptr) {
133 bool setOldCredId = finalResult.SetUint64Value(Attributes::ATTR_OLD_CREDENTIAL_ID,
134 pinInfo->GetOldCredentialId());
135 IF_FALSE_LOGE_AND_RETURN(setOldCredId == true);
136 std::vector<uint8_t> rootSecret = pinInfo->GetRootSecret();
137 if (rootSecret.size() != 0) {
138 bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, rootSecret);
139 IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
140 }
141 std::vector<uint8_t> oldRootSecret = pinInfo->GetOldRootSecret();
142 if (oldRootSecret.size() != 0) {
143 bool setRet = finalResult.SetUint8ArrayValue(Attributes::ATTR_OLD_ROOT_SECRET, oldRootSecret);
144 IF_FALSE_LOGE_AND_RETURN(setRet == true);
145 }
146 std::vector<uint8_t> authToken = pinInfo->GetAuthToken();
147 if (authToken.size() != 0) {
148 bool setAuthToken = finalResult.SetUint8ArrayValue(Attributes::ATTR_AUTH_TOKEN, authToken);
149 IF_FALSE_LOGE_AND_RETURN(setAuthToken == true);
150 }
151 }
152
153 callback_->OnResult(resultCode, finalResult);
154 IAM_LOGI("%{public}s invoke result callback success", GetDescription());
155 }
156 } // namespace UserAuth
157 } // namespace UserIam
158 } // namespace OHOS
159