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     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, 0);
45     return enroll_->GetAccessTokenId();
46 }
47 
GetUserId() const48 int32_t EnrollContext::GetUserId() const
49 {
50     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, INVALID_USER_ID);
51     return enroll_->GetUserId();
52 }
53 
OnStart()54 bool EnrollContext::OnStart()
55 {
56     IAM_LOGI("%{public}s start", GetDescription());
57     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
58     bool startRet = enroll_->Start(scheduleList_, shared_from_this());
59     if (!startRet) {
60         IAM_LOGE("%{public}s enroll start fail", GetDescription());
61         SetLatestError(enroll_->GetLatestError());
62         return startRet;
63     }
64     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
65     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
66     bool startScheduleRet = scheduleList_[0]->StartSchedule();
67     IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
68     IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(),
69         GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType());
70     return true;
71 }
72 
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)73 void EnrollContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
74 {
75     IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
76     uint64_t credentialId = 0;
77     std::shared_ptr<UpdatePinParamInterface> pinInfo;
78     std::optional<uint64_t> secUserId = std::nullopt;
79     bool updateRet = UpdateScheduleResult(scheduleResultAttr, credentialId, pinInfo, secUserId);
80     if (!updateRet) {
81         IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
82         if (resultCode == SUCCESS) {
83             resultCode = GetLatestError();
84         }
85     }
86     InvokeResultCallback(resultCode, credentialId, pinInfo, secUserId);
87     IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
88 }
89 
OnStop()90 bool EnrollContext::OnStop()
91 {
92     IAM_LOGI("%{public}s start", GetDescription());
93     if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
94         scheduleList_[0]->StopSchedule();
95     }
96 
97     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
98     bool cancelRet = enroll_->Cancel();
99     if (!cancelRet) {
100         IAM_LOGE("%{public}s enroll stop fail", GetDescription());
101         SetLatestError(enroll_->GetLatestError());
102         return cancelRet;
103     }
104     return true;
105 }
106 
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,uint64_t & credentialId,std::shared_ptr<UpdatePinParamInterface> & pinInfo,std::optional<uint64_t> & secUserId)107 bool EnrollContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
108     uint64_t &credentialId, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId)
109 {
110     IAM_LOGI("%{public}s start", GetDescription());
111     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
112     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
113     std::vector<uint8_t> scheduleResult;
114     bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
115     IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
116     std::shared_ptr<CredentialInfoInterface> infoToDel;
117     bool updateRet = enroll_->Update(scheduleResult, credentialId, infoToDel, pinInfo, secUserId);
118     if (!updateRet) {
119         IAM_LOGE("%{public}s enroll update fail", GetDescription());
120         SetLatestError(enroll_->GetLatestError());
121         return updateRet;
122     }
123     if (infoToDel == nullptr) {
124         IAM_LOGI("no credential to delete");
125     } else {
126         if (infoToDel->GetAuthType() != PIN) {
127             std::vector<std::shared_ptr<CredentialInfoInterface>> credInfos = {infoToDel};
128             int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos, "DeleteForUpdate");
129             if (ret != SUCCESS) {
130                 IAM_LOGE("failed to delete executor info, error code : %{public}d", ret);
131             }
132         }
133     }
134 
135     return true;
136 }
137 
InvokeResultCallback(int32_t resultCode,const uint64_t credentialId,const std::shared_ptr<UpdatePinParamInterface> & pinInfo,std::optional<uint64_t> & secUserId) const138 void EnrollContext::InvokeResultCallback(int32_t resultCode, const uint64_t credentialId,
139     const std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) const
140 {
141     IAM_LOGI("%{public}s start", GetDescription());
142     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
143     Attributes finalResult;
144     if (secUserId.has_value()) {
145         IAM_LOGI("%{public}s get sec user id has value", GetDescription());
146         bool setSecUserIdRet = finalResult.SetUint64Value(Attributes::ATTR_SEC_USER_ID, secUserId.value());
147         IF_FALSE_LOGE_AND_RETURN(setSecUserIdRet == true);
148     }
149     bool setCredIdRet = finalResult.SetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
150     IF_FALSE_LOGE_AND_RETURN(setCredIdRet == true);
151     if (pinInfo != nullptr) {
152         bool setOldCredId = finalResult.SetUint64Value(Attributes::ATTR_OLD_CREDENTIAL_ID,
153             pinInfo->GetOldCredentialId());
154         IF_FALSE_LOGE_AND_RETURN(setOldCredId == true);
155         std::vector<uint8_t> rootSecret = pinInfo->GetRootSecret();
156         if (rootSecret.size() != 0) {
157             bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, rootSecret);
158             IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
159         }
160         std::vector<uint8_t> oldRootSecret = pinInfo->GetOldRootSecret();
161         if (oldRootSecret.size() != 0) {
162             bool setRet = finalResult.SetUint8ArrayValue(Attributes::ATTR_OLD_ROOT_SECRET, oldRootSecret);
163             IF_FALSE_LOGE_AND_RETURN(setRet == true);
164         }
165         std::vector<uint8_t> authToken = pinInfo->GetAuthToken();
166         if (authToken.size() != 0) {
167             bool setAuthToken = finalResult.SetUint8ArrayValue(Attributes::ATTR_AUTH_TOKEN, authToken);
168             IF_FALSE_LOGE_AND_RETURN(setAuthToken == true);
169         }
170     }
171 
172     callback_->OnResult(resultCode, finalResult);
173     IAM_LOGI("%{public}s invoke result callback success", GetDescription());
174 }
175 } // namespace UserAuth
176 } // namespace UserIam
177 } // namespace OHOS
178