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