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
16 #include "framework_executor_callback.h"
17
18 #include <mutex>
19 #include <sstream>
20
21 #include "auth_command.h"
22 #include "custom_command.h"
23 #include "enroll_command.h"
24 #include "hisysevent_adapter.h"
25 #include "iam_check.h"
26 #include "iam_defines.h"
27 #include "iam_hitrace_helper.h"
28 #include "iam_logger.h"
29 #include "iam_mem.h"
30 #include "iam_para2str.h"
31 #include "iam_ptr.h"
32 #include "identify_command.h"
33
34 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
35
36 namespace OHOS {
37 namespace UserIam {
38 namespace UserAuth {
FrameworkExecutorCallback(std::weak_ptr<Executor> executor)39 FrameworkExecutorCallback::FrameworkExecutorCallback(std::weak_ptr<Executor> executor) : executor_(executor)
40 {
41 uint32_t callbackId = GenerateExecutorCallbackId();
42 std::ostringstream ss;
43 ss << "ExecutorCallback(Id:" << callbackId << ")";
44 description_ = ss.str();
45 }
46
OnBeginExecute(uint64_t scheduleId,const std::vector<uint8_t> & publicKey,const Attributes & commandAttrs)47 int32_t FrameworkExecutorCallback::OnBeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
48 const Attributes &commandAttrs)
49 {
50 auto pk(publicKey);
51
52 return OnBeginExecuteInner(scheduleId, pk, commandAttrs);
53 }
54
OnBeginExecuteInner(uint64_t scheduleId,std::vector<uint8_t> & publicKey,const Attributes & commandAttrs)55 ResultCode FrameworkExecutorCallback::OnBeginExecuteInner(uint64_t scheduleId, std::vector<uint8_t> &publicKey,
56 const Attributes &commandAttrs)
57 {
58 static_cast<void>(publicKey);
59 int32_t commandId = 0;
60 bool getScheduleModeRet =
61 commandAttrs.GetInt32Value(Attributes::ATTR_SCHEDULE_MODE, commandId);
62 IF_FALSE_LOGE_AND_RETURN_VAL(getScheduleModeRet == true, ResultCode::GENERAL_ERROR);
63
64 IAM_LOGI("%{public}s start process cmd %{public}u", GetDescription(), commandId);
65 ResultCode ret = ResultCode::GENERAL_ERROR;
66 switch (commandId) {
67 case ENROLL:
68 ret = ProcessEnrollCommand(scheduleId, commandAttrs);
69 break;
70 case AUTH:
71 ret = ProcessAuthCommand(scheduleId, commandAttrs);
72 break;
73 case IDENTIFY:
74 ret = ProcessIdentifyCommand(scheduleId, commandAttrs);
75 break;
76 default:
77 IAM_LOGE("command id %{public}u is not supported", commandId);
78 }
79
80 IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret);
81 return ret;
82 }
83
OnEndExecute(uint64_t scheduleId,const Attributes & commandAttrs)84 int32_t FrameworkExecutorCallback::OnEndExecute(uint64_t scheduleId, const Attributes &commandAttrs)
85 {
86 return OnEndExecuteInner(scheduleId, commandAttrs);
87 }
88
OnEndExecuteInner(uint64_t scheduleId,const Attributes & consumerAttr)89 ResultCode FrameworkExecutorCallback::OnEndExecuteInner(uint64_t scheduleId, const Attributes &consumerAttr)
90 {
91 ResultCode ret = ProcessCancelCommand(scheduleId);
92 IAM_LOGI("%{public}s cancel scheduleId %{public}s ret %{public}d", GetDescription(),
93 GET_MASKED_STRING(scheduleId).c_str(), ret);
94 return ret;
95 }
96
OnMessengerReady(const std::shared_ptr<ExecutorMessenger> & messenger,const std::vector<uint8_t> & publicKey,const std::vector<uint64_t> & templateIds)97 void FrameworkExecutorCallback::OnMessengerReady(const std::shared_ptr<ExecutorMessenger> &messenger,
98 const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIds)
99 {
100 IAM_LOGI("%{public}s start", GetDescription());
101 auto executor = executor_.lock();
102 if (executor == nullptr) {
103 IAM_LOGE("executor has been released, process failed");
104 return;
105 }
106 auto hdi = executor->GetExecutorHdi();
107 IF_FALSE_LOGE_AND_RETURN(hdi != nullptr);
108 executorMessenger_ = messenger;
109 std::vector<uint8_t> extraInfo;
110 hdi->OnRegisterFinish(templateIds, publicKey, extraInfo);
111 }
112
OnSetProperty(const Attributes & properties)113 int32_t FrameworkExecutorCallback::OnSetProperty(const Attributes &properties)
114 {
115 return OnSetPropertyInner(properties);
116 }
117
OnSetPropertyInner(const Attributes & properties)118 ResultCode FrameworkExecutorCallback::OnSetPropertyInner(const Attributes &properties)
119 {
120 uint32_t commandId = 0;
121 bool getAuthPropertyModeRet =
122 properties.GetUint32Value(Attributes::ATTR_PROPERTY_MODE, commandId);
123 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthPropertyModeRet == true, ResultCode::GENERAL_ERROR);
124 IAM_LOGI("%{public}s start process cmd %{public}u", GetDescription(), commandId);
125 ResultCode ret = ResultCode::GENERAL_ERROR;
126 if (commandId == PROPERTY_MODE_DEL) {
127 ret = ProcessDeleteTemplateCommand(properties);
128 } else {
129 ret = ProcessCustomCommand(properties);
130 }
131 IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret);
132 return ret;
133 }
134
OnGetProperty(const Attributes & conditions,Attributes & results)135 int32_t FrameworkExecutorCallback::OnGetProperty(const Attributes &conditions, Attributes &results)
136 {
137 auto cond = Common::MakeShared<Attributes>(conditions.Serialize());
138 auto values = Common::MakeShared<Attributes>(results.Serialize());
139 auto ret = OnGetPropertyInner(cond, values);
140 if (values) {
141 results = std::move(*values);
142 }
143 return ret;
144 }
145
OnGetPropertyInner(std::shared_ptr<Attributes> conditions,std::shared_ptr<Attributes> values)146 ResultCode FrameworkExecutorCallback::OnGetPropertyInner(std::shared_ptr<Attributes> conditions,
147 std::shared_ptr<Attributes> values)
148 {
149 IAM_LOGI("%{public}s start", GetDescription());
150 IF_FALSE_LOGE_AND_RETURN_VAL(conditions != nullptr, ResultCode::GENERAL_ERROR);
151 IF_FALSE_LOGE_AND_RETURN_VAL(values != nullptr, ResultCode::GENERAL_ERROR);
152 uint32_t commandId = 0;
153 bool getAuthPropertyModeRet =
154 conditions->GetUint32Value(Attributes::ATTR_PROPERTY_MODE, commandId);
155 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthPropertyModeRet == true, ResultCode::GENERAL_ERROR);
156 if (commandId != PROPERTY_MODE_GET) {
157 IAM_LOGE("command id not recognised");
158 return ResultCode::GENERAL_ERROR;
159 }
160 ResultCode ret = ProcessGetTemplateCommand(conditions, values);
161 IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret);
162 return ret;
163 }
164
ProcessEnrollCommand(uint64_t scheduleId,const Attributes & properties)165 ResultCode FrameworkExecutorCallback::ProcessEnrollCommand(uint64_t scheduleId, const Attributes &properties)
166 {
167 auto command = Common::MakeShared<EnrollCommand>(executor_, scheduleId, properties, executorMessenger_);
168 IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR);
169 return command->StartProcess();
170 }
171
ProcessAuthCommand(uint64_t scheduleId,const Attributes & properties)172 ResultCode FrameworkExecutorCallback::ProcessAuthCommand(uint64_t scheduleId, const Attributes &properties)
173 {
174 auto command = Common::MakeShared<AuthCommand>(executor_, scheduleId, properties, executorMessenger_);
175 IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR);
176 return command->StartProcess();
177 }
178
ProcessIdentifyCommand(uint64_t scheduleId,const Attributes & properties)179 ResultCode FrameworkExecutorCallback::ProcessIdentifyCommand(uint64_t scheduleId, const Attributes &properties)
180 {
181 auto command = Common::MakeShared<IdentifyCommand>(executor_, scheduleId, properties, executorMessenger_);
182 IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR);
183 return command->StartProcess();
184 }
185
ProcessCancelCommand(uint64_t scheduleId)186 ResultCode FrameworkExecutorCallback::ProcessCancelCommand(uint64_t scheduleId)
187 {
188 auto executor = executor_.lock();
189 if (executor == nullptr) {
190 IAM_LOGE("executor has been released, process failed");
191 return ResultCode::GENERAL_ERROR;
192 }
193 auto hdi = executor->GetExecutorHdi();
194 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
195 return hdi->Cancel(scheduleId);
196 }
197
ProcessDeleteTemplateCommand(const Attributes & properties)198 ResultCode FrameworkExecutorCallback::ProcessDeleteTemplateCommand(const Attributes &properties)
199 {
200 IAM_LOGI("start");
201 auto executor = executor_.lock();
202 if (executor == nullptr) {
203 IAM_LOGE("executor has been released, process failed");
204 return ResultCode::GENERAL_ERROR;
205 }
206 auto hdi = executor->GetExecutorHdi();
207 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
208 uint64_t templateId = 0;
209 bool getAuthTemplateIdRet = properties.GetUint64Value(Attributes::ATTR_TEMPLATE_ID, templateId);
210 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthTemplateIdRet == true, ResultCode::GENERAL_ERROR);
211 std::vector<uint64_t> templateIdList;
212
213 templateIdList.push_back(templateId);
214 IamHitraceHelper traceHelper("hdi Delete");
215 ResultCode ret = hdi->Delete(templateIdList);
216 if (ret == ResultCode::SUCCESS) {
217 UserIam::UserAuth::ReportTemplateChange(executor->GetAuthType(), TRACE_DELETE_CREDENTIAL, "User Operation");
218 }
219 return ret;
220 }
221
ProcessCustomCommand(const Attributes & properties)222 ResultCode FrameworkExecutorCallback::ProcessCustomCommand(const Attributes &properties)
223 {
224 auto command = Common::MakeShared<CustomCommand>(executor_, properties);
225 IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR);
226 ResultCode ret = command->StartProcess();
227 if (ret != ResultCode::SUCCESS) {
228 IAM_LOGE("start process command fail ret = %{public}d", ret);
229 return ret;
230 }
231
232 return command->GetResult();
233 }
234
ProcessGetTemplateCommand(std::shared_ptr<Attributes> conditions,std::shared_ptr<Attributes> values)235 ResultCode FrameworkExecutorCallback::ProcessGetTemplateCommand(
236 std::shared_ptr<Attributes> conditions, std::shared_ptr<Attributes> values)
237 {
238 IAM_LOGI("start");
239 IF_FALSE_LOGE_AND_RETURN_VAL(conditions != nullptr, ResultCode::GENERAL_ERROR);
240 IF_FALSE_LOGE_AND_RETURN_VAL(values != nullptr, ResultCode::GENERAL_ERROR);
241 auto executor = executor_.lock();
242 if (executor == nullptr) {
243 IAM_LOGE("executor has been released, process failed");
244 return ResultCode::GENERAL_ERROR;
245 }
246 auto hdi = executor->GetExecutorHdi();
247 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
248 uint64_t templateId = 0;
249 bool getAuthTemplateIdRet = conditions->GetUint64Value(Attributes::ATTR_TEMPLATE_ID, templateId);
250 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthTemplateIdRet == true, ResultCode::GENERAL_ERROR);
251 TemplateInfo templateInfo = {};
252 ResultCode ret = hdi->GetTemplateInfo(templateId, templateInfo);
253 IF_FALSE_LOGE_AND_RETURN_VAL(ret == SUCCESS, ret);
254 int32_t subType = 0;
255 Common::UnpackInt32(templateInfo.extraInfo, 0, subType);
256 bool setAuthSubTypeRet = values->SetInt32Value(Attributes::ATTR_PIN_SUB_TYPE, subType);
257 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthSubTypeRet == true, ResultCode::GENERAL_ERROR);
258 bool setAuthRemainTimeRet =
259 values->SetInt32Value(Attributes::ATTR_FREEZING_TIME, templateInfo.freezingTime);
260 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthRemainTimeRet == true, ResultCode::GENERAL_ERROR);
261 bool setAuthRemainCountRet =
262 values->SetInt32Value(Attributes::ATTR_REMAIN_TIMES, templateInfo.remainTimes);
263 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthRemainCountRet == true, ResultCode::GENERAL_ERROR);
264 return ResultCode::SUCCESS;
265 }
266
GenerateExecutorCallbackId()267 uint32_t FrameworkExecutorCallback::GenerateExecutorCallbackId()
268 {
269 static std::mutex mutex;
270 static uint32_t callbackId = 0;
271 std::lock_guard<std::mutex> guard(mutex);
272 // callbackId is only used in log, uint32 overflow or duplicate is ok
273 ++callbackId;
274 return callbackId;
275 }
276
GetDescription()277 const char *FrameworkExecutorCallback::GetDescription()
278 {
279 return description_.c_str();
280 }
281 } // namespace UserAuth
282 } // namespace UserIam
283 } // namespace OHOS
284