1 /*
2 * Copyright (c) 2022 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
OnStart()41 bool EnrollContext::OnStart()
42 {
43 IAM_LOGI("%{public}s start", GetDescription());
44 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
45 bool startRet = enroll_->Start(scheduleList_, shared_from_this());
46 if (!startRet) {
47 IAM_LOGE("%{public}s enroll start fail", GetDescription());
48 SetLatestError(enroll_->GetLatestError());
49 return startRet;
50 }
51 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
52 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
53 bool startScheduleRet = scheduleList_[0]->StartSchedule();
54 IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
55 IAM_LOGI("%{public}s success", GetDescription());
56 return true;
57 }
58
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)59 void EnrollContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
60 {
61 IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
62 uint64_t credentialId = 0;
63 std::vector<uint8_t> rootSecret;
64 bool updateRet = UpdateScheduleResult(scheduleResultAttr, credentialId, rootSecret);
65 if (!updateRet) {
66 IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
67 if (resultCode == SUCCESS) {
68 resultCode = GetLatestError();
69 }
70 }
71 InvokeResultCallback(resultCode, credentialId, rootSecret);
72 IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
73 }
74
OnStop()75 bool EnrollContext::OnStop()
76 {
77 IAM_LOGI("%{public}s start", GetDescription());
78 if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
79 scheduleList_[0]->StopSchedule();
80 }
81
82 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
83 bool cancelRet = enroll_->Cancel();
84 if (!cancelRet) {
85 IAM_LOGE("%{public}s enroll stop fail", GetDescription());
86 SetLatestError(enroll_->GetLatestError());
87 return cancelRet;
88 }
89 return true;
90 }
91
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,uint64_t & credentialId,std::vector<uint8_t> & rootSecret)92 bool EnrollContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
93 uint64_t &credentialId, std::vector<uint8_t> &rootSecret)
94 {
95 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
96 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
97 std::vector<uint8_t> scheduleResult;
98 bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
99 IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
100 std::shared_ptr<CredentialInfo> infoToDel;
101 bool updateRet = enroll_->Update(scheduleResult, credentialId, infoToDel, rootSecret);
102 if (!updateRet) {
103 IAM_LOGE("%{public}s enroll update fail", GetDescription());
104 SetLatestError(enroll_->GetLatestError());
105 return updateRet;
106 }
107 if (infoToDel == nullptr) {
108 IAM_LOGI("no credential to delete");
109 } else {
110 std::vector<std::shared_ptr<CredentialInfo>> credInfos = {infoToDel};
111 int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos);
112 if (ret != SUCCESS) {
113 IAM_LOGE("failed to delete executor info, error code : %{public}d", ret);
114 }
115 }
116 return true;
117 }
118
InvokeResultCallback(int32_t resultCode,const uint64_t credentialId,const std::vector<uint8_t> & rootSecret) const119 void EnrollContext::InvokeResultCallback(int32_t resultCode, const uint64_t credentialId,
120 const std::vector<uint8_t> &rootSecret) const
121 {
122 IAM_LOGI("%{public}s start", GetDescription());
123 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
124 Attributes finalResult;
125 bool setCredIdRet = finalResult.SetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
126 IF_FALSE_LOGE_AND_RETURN(setCredIdRet == true);
127 if (rootSecret.size() != 0) {
128 bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, rootSecret);
129 IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
130 }
131 callback_->OnResult(resultCode, finalResult);
132 IAM_LOGI("%{public}s invoke result callback success", GetDescription());
133 }
134 } // namespace UserAuth
135 } // namespace UserIam
136 } // namespace OHOS
137