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 "simple_auth_context.h"
16
17 #include <set>
18 #include <vector>
19
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #include "iam_ptr.h"
24 #include "resource_node.h"
25 #include "resource_node_utils.h"
26 #include "schedule_node.h"
27 #include "schedule_node_callback.h"
28
29 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
SetFreezingTimeAndRemainTimes(int32_t & freezingTime,int32_t & remainTimes)33 ResultCode SimpleAuthContext::SetFreezingTimeAndRemainTimes(int32_t &freezingTime, int32_t &remainTimes)
34 {
35 IAM_LOGI("start");
36 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, GENERAL_ERROR);
37 auto scheduleNode = scheduleList_[0];
38 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleNode != nullptr, GENERAL_ERROR);
39
40 auto resourceNode = scheduleNode->GetVerifyExecutor().lock();
41 IF_FALSE_LOGE_AND_RETURN_VAL(resourceNode != nullptr, GENERAL_ERROR);
42
43 auto optionalTemplateIdList = scheduleNode->GetTemplateIdList();
44 IF_FALSE_LOGE_AND_RETURN_VAL(optionalTemplateIdList.has_value(), GENERAL_ERROR);
45
46 std::vector<uint64_t> templateIdList = optionalTemplateIdList.value();
47
48 Attributes attr;
49 attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_GET);
50 attr.SetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIdList);
51 std::vector<uint32_t> keys = { Attributes::ATTR_FREEZING_TIME, Attributes::ATTR_REMAIN_TIMES };
52 attr.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, keys);
53
54 Attributes values;
55 int32_t ret = resourceNode->GetProperty(attr, values);
56 IF_FALSE_LOGE_AND_RETURN_VAL(ret == SUCCESS, GENERAL_ERROR);
57
58 bool getFreezingTimeRet = values.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime);
59 IF_FALSE_LOGE_AND_RETURN_VAL(getFreezingTimeRet == true, GENERAL_ERROR);
60 bool getRemainTimesRet = values.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTimes);
61 IF_FALSE_LOGE_AND_RETURN_VAL(getRemainTimesRet == true, GENERAL_ERROR);
62
63 IAM_LOGI("set freezingTime:%{public}d and remainTimes:%{public}d success", freezingTime, remainTimes);
64 return SUCCESS;
65 }
66
NeedSetFreezingTimeAndRemainTimes(int32_t result) const67 bool SimpleAuthContext::NeedSetFreezingTimeAndRemainTimes(int32_t result) const
68 {
69 static const std::set<int32_t> needSetCodes = { FAIL, LOCKED };
70 return needSetCodes.find(result) != needSetCodes.end();
71 }
72
SimpleAuthContext(uint64_t contextId,std::shared_ptr<Authentication> auth,std::shared_ptr<ContextCallback> callback)73 SimpleAuthContext::SimpleAuthContext(uint64_t contextId, std::shared_ptr<Authentication> auth,
74 std::shared_ptr<ContextCallback> callback)
75 : BaseContext("SimpleAuth", contextId, callback),
76 auth_(auth)
77 {
78 }
79
GetContextType() const80 ContextType SimpleAuthContext::GetContextType() const
81 {
82 return CONTEXT_SIMPLE_AUTH;
83 }
84
GetTokenId() const85 uint32_t SimpleAuthContext::GetTokenId() const
86 {
87 return auth_->GetAccessTokenId();
88 }
89
OnStart()90 bool SimpleAuthContext::OnStart()
91 {
92 IAM_LOGI("%{public}s start", GetDescription());
93 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
94 bool startRet = auth_->Start(scheduleList_, shared_from_this());
95 if (!startRet) {
96 IAM_LOGE("%{public}s auth start fail", GetDescription());
97 SetLatestError(auth_->GetLatestError());
98 return startRet;
99 }
100 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
101 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
102 bool startScheduleRet = scheduleList_[0]->StartSchedule();
103 IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
104 IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(),
105 GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType());
106 return true;
107 }
108
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)109 void SimpleAuthContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
110 {
111 IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
112 Authentication::AuthResultInfo resultInfo = {};
113 bool updateRet = UpdateScheduleResult(scheduleResultAttr, resultInfo);
114 if (!updateRet) {
115 IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
116 if (resultCode == SUCCESS) {
117 resultCode = GetLatestError();
118 }
119 resultInfo.result = resultCode;
120 }
121 if (NeedSetFreezingTimeAndRemainTimes(resultInfo.result)) {
122 IAM_LOGI("need get freezing time and remain times");
123 ResultCode result = SetFreezingTimeAndRemainTimes(resultInfo.freezingTime,
124 resultInfo.remainTimes);
125 if (result != SUCCESS) {
126 IAM_LOGE("fail to get freezing time and remain times");
127 }
128 }
129 InvokeResultCallback(resultInfo);
130 SendAuthExecutorMsg();
131 IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
132 }
133
OnStop()134 bool SimpleAuthContext::OnStop()
135 {
136 IAM_LOGI("%{public}s start", GetDescription());
137 if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
138 scheduleList_[0]->StopSchedule();
139 }
140
141 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
142 bool cancelRet = auth_->Cancel();
143 if (!cancelRet) {
144 IAM_LOGE("%{public}s auth stop fail", GetDescription());
145 SetLatestError(auth_->GetLatestError());
146 return cancelRet;
147 }
148 return true;
149 }
150
SendAuthExecutorMsg()151 void SimpleAuthContext::SendAuthExecutorMsg()
152 {
153 IF_FALSE_LOGE_AND_RETURN(auth_ != nullptr);
154 auto authExecutorMsgs = auth_->GetAuthExecutorMsgs();
155 for (auto &authExecutorMsg : authExecutorMsgs) {
156 ResourceNodeUtils::SendMsgToExecutor(
157 authExecutorMsg.executorIndex, authExecutorMsg.commandId, authExecutorMsg.msg);
158 }
159 }
160
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,Authentication::AuthResultInfo & resultInfo)161 bool SimpleAuthContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
162 Authentication::AuthResultInfo &resultInfo)
163 {
164 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
165 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
166 std::vector<uint8_t> scheduleResult;
167 bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
168 IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
169 bool updateRet = auth_->Update(scheduleResult, resultInfo);
170 if (!updateRet) {
171 IAM_LOGE("%{public}s auth update fail", GetDescription());
172 SetLatestError(auth_->GetLatestError());
173 return updateRet;
174 }
175 return true;
176 }
177
InvokeResultCallback(const Authentication::AuthResultInfo & resultInfo) const178 void SimpleAuthContext::InvokeResultCallback(const Authentication::AuthResultInfo &resultInfo) const
179 {
180 IAM_LOGI("%{public}s start", GetDescription());
181 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
182 Attributes finalResult;
183 bool setResultCodeRet = finalResult.SetInt32Value(Attributes::ATTR_RESULT_CODE, resultInfo.result);
184 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
185
186 if (resultInfo.result == SUCCESS || NeedSetFreezingTimeAndRemainTimes(resultInfo.result)) {
187 bool setFreezingTimeRet = finalResult.SetInt32Value(Attributes::ATTR_FREEZING_TIME, resultInfo.freezingTime);
188 IF_FALSE_LOGE_AND_RETURN(setFreezingTimeRet == true);
189 bool setRemainTimesRet = finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, resultInfo.remainTimes);
190 IF_FALSE_LOGE_AND_RETURN(setRemainTimesRet == true);
191 }
192
193 if (resultInfo.token.size() != 0) {
194 bool setSignatureResult = finalResult.SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, resultInfo.token);
195 IF_FALSE_LOGE_AND_RETURN(setSignatureResult == true);
196 }
197 if (resultInfo.rootSecret.size() != 0) {
198 bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, resultInfo.rootSecret);
199 IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
200 }
201 callback_->OnResult(resultInfo.result, finalResult);
202 IAM_LOGI("%{public}s invoke result callback success", GetDescription());
203 }
204 } // namespace UserAuth
205 } // namespace UserIam
206 } // namespace OHOS
207