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