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 IF_FALSE_LOGE_AND_RETURN_VAL(enroll_ != nullptr, false);
111 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
112 std::vector<uint8_t> scheduleResult;
113 bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
114 IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
115 std::shared_ptr<CredentialInfoInterface> infoToDel;
116 bool updateRet = enroll_->Update(scheduleResult, credentialId, infoToDel, pinInfo, secUserId);
117 if (!updateRet) {
118 IAM_LOGE("%{public}s enroll update fail", GetDescription());
119 SetLatestError(enroll_->GetLatestError());
120 return updateRet;
121 }
122
123 return true;
124 }
125
InvokeResultCallback(int32_t resultCode, const uint64_t credentialId, const std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) const126 void EnrollContext::InvokeResultCallback(int32_t resultCode, const uint64_t credentialId,
127 const std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) const
128 {
129 IAM_LOGI("%{public}s start", GetDescription());
130 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
131 Attributes finalResult;
132 if (secUserId.has_value()) {
133 IAM_LOGI("%{public}s get sec user id has value", GetDescription());
134 bool setSecUserIdRet = finalResult.SetUint64Value(Attributes::ATTR_SEC_USER_ID, secUserId.value());
135 IF_FALSE_LOGE_AND_RETURN(setSecUserIdRet == true);
136 }
137 bool setCredIdRet = finalResult.SetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
138 IF_FALSE_LOGE_AND_RETURN(setCredIdRet == true);
139 if (pinInfo != nullptr) {
140 bool setOldCredId = finalResult.SetUint64Value(Attributes::ATTR_OLD_CREDENTIAL_ID,
141 pinInfo->GetOldCredentialId());
142 IF_FALSE_LOGE_AND_RETURN(setOldCredId == true);
143 std::vector<uint8_t> rootSecret = pinInfo->GetRootSecret();
144 if (rootSecret.size() != 0) {
145 bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, rootSecret);
146 IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
147 }
148 std::vector<uint8_t> oldRootSecret = pinInfo->GetOldRootSecret();
149 if (oldRootSecret.size() != 0) {
150 bool setRet = finalResult.SetUint8ArrayValue(Attributes::ATTR_OLD_ROOT_SECRET, oldRootSecret);
151 IF_FALSE_LOGE_AND_RETURN(setRet == true);
152 }
153 std::vector<uint8_t> authToken = pinInfo->GetAuthToken();
154 if (authToken.size() != 0) {
155 bool setAuthToken = finalResult.SetUint8ArrayValue(Attributes::ATTR_AUTH_TOKEN, authToken);
156 IF_FALSE_LOGE_AND_RETURN(setAuthToken == true);
157 }
158 }
159
160 callback_->OnResult(resultCode, finalResult);
161 IAM_LOGI("%{public}s invoke result callback success", GetDescription());
162 }
163 } // namespace UserAuth
164 } // namespace UserIam
165 } // namespace OHOS
166