• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "iam_ptr.h"
22 #include "resource_node_utils.h"
23 #include "schedule_node.h"
24 #include "schedule_node_callback.h"
25 
26 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
EnrollContext(uint64_t contextId,std::shared_ptr<Enrollment> enroll,std::shared_ptr<ContextCallback> callback)31 EnrollContext::EnrollContext(uint64_t contextId, std::shared_ptr<Enrollment> enroll,
32     std::shared_ptr<ContextCallback> callback)
33     : BaseContext("Enroll", contextId, callback),
34       enroll_(enroll)
35 {
36 }
37 
GetContextType() const38 ContextType EnrollContext::GetContextType() const
39 {
40     return CONTEXT_ENROLL;
41 }
42 
GetTokenId() const43 uint32_t EnrollContext::GetTokenId() const
44 {
45     return enroll_->GetAccessTokenId();
46 }
47 
OnStart()48 bool EnrollContext::OnStart()
49 {
50     IAM_LOGI("%{public}s start", GetDescription());
51     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
52     bool startRet = enroll_->Start(scheduleList_, shared_from_this());
53     if (!startRet) {
54         IAM_LOGE("%{public}s enroll start fail", GetDescription());
55         SetLatestError(enroll_->GetLatestError());
56         return startRet;
57     }
58     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
59     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
60     bool startScheduleRet = scheduleList_[0]->StartSchedule();
61     IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
62     IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(),
63         GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType());
64     return true;
65 }
66 
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)67 void EnrollContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
68 {
69     IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
70     uint64_t credentialId = 0;
71     std::vector<uint8_t> rootSecret;
72     std::optional<uint64_t> secUserId = std::nullopt;
73     bool updateRet = UpdateScheduleResult(scheduleResultAttr, credentialId, rootSecret, secUserId);
74     if (!updateRet) {
75         IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
76         if (resultCode == SUCCESS) {
77             resultCode = GetLatestError();
78         }
79     }
80     InvokeResultCallback(resultCode, credentialId, rootSecret, secUserId);
81     IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
82 }
83 
OnStop()84 bool EnrollContext::OnStop()
85 {
86     IAM_LOGI("%{public}s start", GetDescription());
87     if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
88         scheduleList_[0]->StopSchedule();
89     }
90 
91     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
92     bool cancelRet = enroll_->Cancel();
93     if (!cancelRet) {
94         IAM_LOGE("%{public}s enroll stop fail", GetDescription());
95         SetLatestError(enroll_->GetLatestError());
96         return cancelRet;
97     }
98     return true;
99 }
100 
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,uint64_t & credentialId,std::vector<uint8_t> & rootSecret,std::optional<uint64_t> & secUserId)101 bool EnrollContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
102     uint64_t &credentialId, std::vector<uint8_t> &rootSecret, std::optional<uint64_t> &secUserId)
103 {
104     IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
105     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
106     std::vector<uint8_t> scheduleResult;
107     bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
108     IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
109     std::shared_ptr<CredentialInfoInterface> infoToDel;
110     bool updateRet = enroll_->Update(scheduleResult, credentialId, infoToDel, rootSecret, secUserId);
111     if (!updateRet) {
112         IAM_LOGE("%{public}s enroll update fail", GetDescription());
113         SetLatestError(enroll_->GetLatestError());
114         return updateRet;
115     }
116     if (infoToDel == nullptr) {
117         IAM_LOGI("no credential to delete");
118     } else {
119         std::vector<std::shared_ptr<CredentialInfoInterface>> credInfos = {infoToDel};
120         int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos, "DeleteForUpdate");
121         if (ret != SUCCESS) {
122             IAM_LOGE("failed to delete executor info, error code : %{public}d", ret);
123         }
124     }
125     return true;
126 }
127 
InvokeResultCallback(int32_t resultCode,const uint64_t credentialId,const std::vector<uint8_t> & rootSecret,std::optional<uint64_t> & secUserId) const128 void EnrollContext::InvokeResultCallback(int32_t resultCode, const uint64_t credentialId,
129     const std::vector<uint8_t> &rootSecret, std::optional<uint64_t> &secUserId) const
130 {
131     IAM_LOGI("%{public}s start", GetDescription());
132     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
133     Attributes finalResult;
134     if (secUserId.has_value()) {
135         IAM_LOGI("%{public}s get sec user id has value", GetDescription());
136         bool setSecUserIdRet = finalResult.SetUint64Value(Attributes::ATTR_SEC_USER_ID, secUserId.value());
137         IF_FALSE_LOGE_AND_RETURN(setSecUserIdRet == true);
138     }
139     bool setCredIdRet = finalResult.SetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
140     IF_FALSE_LOGE_AND_RETURN(setCredIdRet == true);
141     if (rootSecret.size() != 0) {
142         bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, rootSecret);
143         IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
144     }
145 
146     callback_->OnResult(resultCode, finalResult);
147     IAM_LOGI("%{public}s invoke result callback success", GetDescription());
148 }
149 } // namespace UserAuth
150 } // namespace UserIam
151 } // namespace OHOS
152