1 /*
2 * Copyright (c) 2024 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 "remote_executor_stub.h"
17
18 #include "iam_check.h"
19 #include "schedule_node.h"
20
21 #include "context_factory.h"
22 #include "context_pool.h"
23 #include "device_manager_util.h"
24 #include "hdi_wrapper.h"
25 #include "iam_logger.h"
26 #include "iam_para2str.h"
27 #include "iam_ptr.h"
28 #include "remote_auth_service.h"
29 #include "remote_msg_util.h"
30 #include "resource_node_pool.h"
31 #include "schedule_resource_node_listener.h"
32 #include "thread_handler.h"
33
34 #define LOG_TAG "USER_AUTH_SA"
35
36 namespace OHOS {
37 namespace UserIam {
38 namespace UserAuth {
39 class RemoteExecutorStubScheduleNode : public ScheduleNode,
40 public std::enable_shared_from_this<ScheduleNode>,
41 public NoCopyable {
42 public:
RemoteExecutorStubScheduleNode(HdiScheduleInfo & scheduleInfo,std::weak_ptr<RemoteExecutorStub> callback,std::weak_ptr<ResourceNode> collectorExecutor)43 RemoteExecutorStubScheduleNode(HdiScheduleInfo &scheduleInfo, std::weak_ptr<RemoteExecutorStub> callback,
44 std::weak_ptr<ResourceNode> collectorExecutor)
45 : scheduleId_(scheduleInfo.scheduleId),
46 callback_(callback),
47 collectorExecutor_(collectorExecutor)
48 {
49 }
50
~RemoteExecutorStubScheduleNode()51 ~RemoteExecutorStubScheduleNode()
52 {
53 if (resourceNodePoolListener_ != nullptr) {
54 ResourceNodePool::Instance().DeregisterResourceNodePoolListener(resourceNodePoolListener_);
55 }
56 }
57
GetScheduleId() const58 uint64_t GetScheduleId() const override
59 {
60 return scheduleId_;
61 }
62
GetContextId() const63 uint64_t GetContextId() const override
64 {
65 return 0;
66 }
67
GetAuthType() const68 AuthType GetAuthType() const override
69 {
70 return AuthType::ALL;
71 }
72
GetExecutorMatcher() const73 uint64_t GetExecutorMatcher() const override
74 {
75 return 0;
76 }
GetScheduleMode() const77 ScheduleMode GetScheduleMode() const override
78 {
79 return ScheduleMode::AUTH;
80 }
GetCollectorExecutor() const81 std::weak_ptr<ResourceNode> GetCollectorExecutor() const override
82 {
83 return collectorExecutor_;
84 }
GetVerifyExecutor() const85 std::weak_ptr<ResourceNode> GetVerifyExecutor() const override
86 {
87 static std::weak_ptr<ResourceNode> nullNode;
88 return nullNode;
89 }
GetTemplateIdList() const90 std::optional<std::vector<uint64_t>> GetTemplateIdList() const override
91 {
92 return std::nullopt;
93 }
GetCurrentScheduleState() const94 State GetCurrentScheduleState() const override
95 {
96 return State::S_INIT;
97 }
GetScheduleCallback()98 std::shared_ptr<ScheduleNodeCallback> GetScheduleCallback() override
99 {
100 return nullptr;
101 }
ClearScheduleCallback()102 void ClearScheduleCallback() override
103 {
104 return;
105 }
StartSchedule()106 bool StartSchedule() override
107 {
108 std::lock_guard<std::recursive_mutex> lock(mutex_);
109 resourceNodePoolListener_ = Common::MakeShared<ScheduleResourceNodeListener>(weak_from_this());
110 IF_FALSE_LOGE_AND_RETURN_VAL(resourceNodePoolListener_ != nullptr, false);
111 bool registerRet = ResourceNodePool::Instance().RegisterResourceNodePoolListener(resourceNodePoolListener_);
112 IF_FALSE_LOGE_AND_RETURN_VAL(registerRet, false);
113 return true;
114 }
StopSchedule()115 bool StopSchedule() override
116 {
117 return true;
118 }
StopSchedule(ResultCode errorCode)119 bool StopSchedule(ResultCode errorCode) override
120 {
121 std::lock_guard<std::recursive_mutex> lock(mutex_);
122 IAM_LOGI("stop schedule errorCode %{public}d", errorCode);
123 auto finalResult = Common::MakeShared<Attributes>();
124 IF_FALSE_LOGE_AND_RETURN_VAL(finalResult != nullptr, false);
125 bool setResultCodeRet = finalResult->SetInt32Value(Attributes::ATTR_RESULT_CODE, errorCode);
126 IF_FALSE_LOGE_AND_RETURN_VAL(setResultCodeRet, false);
127
128 return ContinueSchedule(errorCode, finalResult);
129 }
SendMessage(ExecutorRole dstRole,const std::vector<uint8_t> & msg)130 bool SendMessage(ExecutorRole dstRole, const std::vector<uint8_t> &msg) override
131 {
132 std::lock_guard<std::recursive_mutex> lock(mutex_);
133 auto callback = callback_.lock();
134 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, GENERAL_ERROR);
135 int32_t ret = callback->OnMessage(dstRole, msg);
136 return ret == ResultCode::SUCCESS;
137 }
ContinueSchedule(ResultCode resultCode,const std::shared_ptr<Attributes> & finalResult)138 bool ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult) override
139 {
140 std::lock_guard<std::recursive_mutex> lock(mutex_);
141 auto callback = callback_.lock();
142 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, GENERAL_ERROR);
143 int32_t ret = callback->ContinueSchedule(resultCode, finalResult);
144 return ret == ResultCode::SUCCESS;
145 }
146
147 private:
148 std::recursive_mutex mutex_;
149 uint64_t scheduleId_;
150 std::weak_ptr<RemoteExecutorStub> callback_;
151 std::weak_ptr<ResourceNode> collectorExecutor_;
152 std::shared_ptr<ResourceNodePool::ResourceNodePoolListener> resourceNodePoolListener_ {nullptr};
153 };
154
155 class RemoteExecutorStubMessageCallback : public ConnectionListener, public NoCopyable {
156 public:
RemoteExecutorStubMessageCallback(uint64_t scheduleId,std::weak_ptr<RemoteExecutorStub> callback)157 explicit RemoteExecutorStubMessageCallback(uint64_t scheduleId, std::weak_ptr<RemoteExecutorStub> callback)
158 : scheduleId_(scheduleId),
159 callback_(callback),
160 threadHandler_(ThreadHandler::GetSingleThreadInstance())
161 {
162 }
163 ~RemoteExecutorStubMessageCallback() = default;
164
OnMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::shared_ptr<Attributes> & request,std::shared_ptr<Attributes> & reply)165 void OnMessage(const std::string &connectionName, const std::string &srcEndPoint,
166 const std::shared_ptr<Attributes> &request, std::shared_ptr<Attributes> &reply) override
167 {
168 IF_FALSE_LOGE_AND_RETURN(request != nullptr);
169 IF_FALSE_LOGE_AND_RETURN(reply != nullptr);
170
171 IAM_LOGI("connectionName: %{public}s, srcEndPoint: %{public}s", connectionName.c_str(), srcEndPoint.c_str());
172
173 auto callback = callback_.lock();
174 IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
175 callback->OnMessage(connectionName, srcEndPoint, request, reply);
176 }
177
OnConnectStatus(const std::string & connectionName,ConnectStatus connectStatus)178 void OnConnectStatus(const std::string &connectionName, ConnectStatus connectStatus) override
179 {
180 IAM_LOGI("connectionName: %{public}s, connectStatus %{public}d, scheduleId "
181 "%{public}s",
182 connectionName.c_str(), connectStatus, GET_MASKED_STRING(scheduleId_).c_str());
183
184 IF_FALSE_LOGE_AND_RETURN(connectStatus == ConnectStatus::DISCONNECTED);
185
186 IF_FALSE_LOGE_AND_RETURN(threadHandler_ != nullptr);
187
188 threadHandler_->PostTask([scheduleId = scheduleId_]() {
189 IAM_LOGI("OnConnectStatus process begin, scheduleId %{public}s", GET_MASKED_STRING(scheduleId).c_str());
190
191 auto request = Common::MakeShared<Attributes>();
192 IF_FALSE_LOGE_AND_RETURN(request != nullptr);
193 bool setScheduleIdRet = request->SetUint64Value(Attributes::ATTR_SCHEDULE_ID, scheduleId);
194 IF_FALSE_LOGE_AND_RETURN(setScheduleIdRet);
195
196 auto reply = Common::MakeShared<Attributes>();
197 IF_FALSE_LOGE_AND_RETURN(reply != nullptr);
198 RemoteAuthService::GetInstance().ProcEndExecuteRequest(request, reply);
199 IAM_LOGI("OnConnectStatus process success, scheduleId %{public}s", GET_MASKED_STRING(scheduleId).c_str());
200 });
201
202 IAM_LOGI("task posted");
203 }
204
205 private:
206 uint64_t scheduleId_;
207 std::weak_ptr<RemoteExecutorStub> callback_;
208 std::shared_ptr<ThreadHandler> threadHandler_ = nullptr;
209 };
210
RemoteExecutorStub()211 RemoteExecutorStub::RemoteExecutorStub() : endPointName_(EXECUTOR_STUB_ENDPOINT_NAME)
212 {
213 }
214
~RemoteExecutorStub()215 RemoteExecutorStub::~RemoteExecutorStub()
216 {
217 std::lock_guard<std::recursive_mutex> lock(mutex_);
218 if (connectionCallback_ != nullptr) {
219 RemoteConnectionManager::GetInstance().UnregisterConnectionListener(connectionName_, endPointName_);
220 }
221 if (contextId_.has_value()) {
222 ContextPool::Instance().Delete(contextId_.value());
223 }
224 IAM_LOGI("ConnectionName %{public}s RemoteExecutorStub destructed", connectionName_.c_str());
225 }
226
ProcBeginExecuteRequest(Attributes & attr)227 int32_t RemoteExecutorStub::ProcBeginExecuteRequest(Attributes &attr)
228 {
229 std::lock_guard<std::recursive_mutex> lock(mutex_);
230 IAM_LOGI("start");
231
232 uint64_t scheduleId;
233 bool getScheduleIdRet = attr.GetUint64Value(Attributes::ATTR_SCHEDULE_ID, scheduleId);
234 IF_FALSE_LOGE_AND_RETURN_VAL(getScheduleIdRet, GENERAL_ERROR);
235 IAM_LOGI("scheduleId %{public}s begin execute", GET_MASKED_STRING(scheduleId).c_str());
236
237 connectionCallback_ = Common::MakeShared<RemoteExecutorStubMessageCallback>(scheduleId, shared_from_this());
238 IF_FALSE_LOGE_AND_RETURN_VAL(connectionCallback_ != nullptr, GENERAL_ERROR);
239
240 bool getConnectionName = attr.GetStringValue(Attributes::ATTR_CONNECTION_NAME, connectionName_);
241 IF_FALSE_LOGE_AND_RETURN_VAL(getConnectionName, GENERAL_ERROR);
242
243 ResultCode registerResult = RemoteConnectionManager::GetInstance().RegisterConnectionListener(connectionName_,
244 endPointName_, connectionCallback_);
245 IF_FALSE_LOGE_AND_RETURN_VAL(registerResult == SUCCESS, GENERAL_ERROR);
246
247 std::string srcUdid;
248 bool getSrcUdidRet = attr.GetStringValue(Attributes::ATTR_MSG_SRC_UDID, srcUdid);
249 IF_FALSE_LOGE_AND_RETURN_VAL(getSrcUdidRet, GENERAL_ERROR);
250
251 std::vector<uint8_t> scheduleData;
252 bool getScheduleDataRet = attr.GetUint8ArrayValue(Attributes::ATTR_SCHEDULE_DATA, scheduleData);
253 IF_FALSE_LOGE_AND_RETURN_VAL(getScheduleDataRet, GENERAL_ERROR);
254
255 HdiScheduleInfo scheduleInfo;
256 auto hdi = HdiWrapper::GetHdiInstance();
257 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, GENERAL_ERROR);
258
259 int32_t ret = hdi->GetLocalScheduleFromMessage(srcUdid, scheduleData, scheduleInfo);
260 IF_FALSE_LOGE_AND_RETURN_VAL(ret == SUCCESS, GENERAL_ERROR);
261 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleInfo.executorIndexes.size() == 1, GENERAL_ERROR);
262 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleInfo.executorMessages.size() == 1, GENERAL_ERROR);
263
264 executorIndex_ = scheduleInfo.executorIndexes[0];
265 std::weak_ptr<ResourceNode> weakNode = ResourceNodePool::Instance().Select(executorIndex_);
266 std::shared_ptr<ResourceNode> node = weakNode.lock();
267 IF_FALSE_LOGE_AND_RETURN_VAL(node != nullptr, GENERAL_ERROR);
268
269 remoteScheduleNode_ = Common::MakeShared<RemoteExecutorStubScheduleNode>(scheduleInfo, weak_from_this(), node);
270 IF_FALSE_LOGE_AND_RETURN_VAL(remoteScheduleNode_ != nullptr, GENERAL_ERROR);
271
272 bool startScheduleRet = remoteScheduleNode_->StartSchedule();
273 IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, GENERAL_ERROR);
274
275 auto context = ContextFactory::CreateScheduleHolderContext(remoteScheduleNode_);
276 IF_FALSE_LOGE_AND_RETURN_VAL(context != nullptr, GENERAL_ERROR);
277
278 bool addContextRet = ContextPool::Instance().Insert(context);
279 IF_FALSE_LOGE_AND_RETURN_VAL(addContextRet, GENERAL_ERROR);
280 contextId_ = context->GetContextId();
281
282 bool setExtraInfo = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, scheduleInfo.executorMessages[0]);
283 IF_FALSE_LOGE_AND_RETURN_VAL(setExtraInfo, GENERAL_ERROR);
284
285 std::vector<uint8_t> publicKey;
286 node->BeginExecute(scheduleInfo.scheduleId, publicKey, attr);
287
288 IAM_LOGI("ConnectionName %{public}s scheduleId %{public}s begin execute success", connectionName_.c_str(),
289 GET_MASKED_STRING(scheduleId).c_str());
290 return ResultCode::SUCCESS;
291 }
292
OnMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::shared_ptr<Attributes> & request,std::shared_ptr<Attributes> & reply)293 void RemoteExecutorStub::OnMessage(const std::string &connectionName, const std::string &srcEndPoint,
294 const std::shared_ptr<Attributes> &request, std::shared_ptr<Attributes> &reply)
295 {
296 std::lock_guard<std::recursive_mutex> lock(mutex_);
297 IAM_LOGI("start");
298 IF_FALSE_LOGE_AND_RETURN(request != nullptr);
299 IF_FALSE_LOGE_AND_RETURN(reply != nullptr);
300
301 int32_t msgType;
302 bool getMsgTypeRet = request->GetInt32Value(Attributes::ATTR_MSG_TYPE, msgType);
303 IF_FALSE_LOGE_AND_RETURN(getMsgTypeRet);
304
305 int32_t resultCode = ResultCode::GENERAL_ERROR;
306 switch (msgType) {
307 case MessageType::SEND_DATA_TO_EXECUTOR:
308 resultCode = ProcSendDataMsg(*request);
309 break;
310 default:
311 IAM_LOGE("unsupported message type: %{public}d", msgType);
312 break;
313 }
314
315 IF_FALSE_LOGE_AND_RETURN(resultCode == ResultCode::SUCCESS);
316 bool setResultCodeRet = reply->SetInt32Value(Attributes::ATTR_RESULT_CODE, ResultCode::SUCCESS);
317 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet);
318
319 IAM_LOGI("success");
320 }
321
OnMessage(ExecutorRole dstRole,const std::vector<uint8_t> & msg)322 int32_t RemoteExecutorStub::OnMessage(ExecutorRole dstRole, const std::vector<uint8_t> &msg)
323 {
324 std::lock_guard<std::recursive_mutex> lock(mutex_);
325 IF_FALSE_LOGE_AND_RETURN_VAL(remoteScheduleNode_ != nullptr, GENERAL_ERROR);
326
327 IAM_LOGI("start, scheduleId %{public}s", GET_MASKED_STRING(remoteScheduleNode_->GetScheduleId()).c_str());
328
329 std::shared_ptr<Attributes> request = Common::MakeShared<Attributes>(msg);
330 IF_FALSE_LOGE_AND_RETURN_VAL(request != nullptr, GENERAL_ERROR);
331
332 bool setMsgTypeRet = request->SetInt32Value(Attributes::ATTR_MSG_TYPE, MessageType::EXECUTOR_SEND_DATA);
333 IF_FALSE_LOGE_AND_RETURN_VAL(setMsgTypeRet, GENERAL_ERROR);
334
335 bool setScheduleIdRet = request->SetUint64Value(Attributes::ATTR_SCHEDULE_ID, remoteScheduleNode_->GetScheduleId());
336 IF_FALSE_LOGE_AND_RETURN_VAL(setScheduleIdRet, GENERAL_ERROR);
337
338 bool setDestRoleRet = request->SetInt32Value(Attributes::ATTR_DEST_ROLE, dstRole);
339 IF_FALSE_LOGE_AND_RETURN_VAL(setDestRoleRet, GENERAL_ERROR);
340
341 MsgCallback msgCallback = [](const std::shared_ptr<Attributes> &) { IAM_LOGI("message sent"); };
342
343 ResultCode sendMsgRet = RemoteConnectionManager::GetInstance().SendMessage(connectionName_, endPointName_,
344 EXECUTOR_PROXY_ENDPOINT_NAME, request, msgCallback);
345 IF_FALSE_LOGE_AND_RETURN_VAL(sendMsgRet == ResultCode::SUCCESS, GENERAL_ERROR);
346
347 IAM_LOGI("success, ConnectionName %{public}s scheduleId %{public}s", connectionName_.c_str(),
348 GET_MASKED_STRING(remoteScheduleNode_->GetScheduleId()).c_str());
349 return SUCCESS;
350 }
351
ContinueSchedule(ResultCode resultCode,const std::shared_ptr<Attributes> & finalResult)352 int32_t RemoteExecutorStub::ContinueSchedule(ResultCode resultCode, const std::shared_ptr<Attributes> &finalResult)
353 {
354 std::lock_guard<std::recursive_mutex> lock(mutex_);
355 IAM_LOGI("start");
356
357 IF_FALSE_LOGE_AND_RETURN_VAL(finalResult != nullptr, GENERAL_ERROR);
358 IF_FALSE_LOGE_AND_RETURN_VAL(remoteScheduleNode_ != nullptr, GENERAL_ERROR);
359 IAM_LOGI("ConnectionName %{public}s scheduleId %{public}s continue schedule", connectionName_.c_str(),
360 GET_MASKED_STRING(remoteScheduleNode_->GetScheduleId()).c_str());
361
362 std::shared_ptr<Attributes> request = Common::MakeShared<Attributes>(finalResult->Serialize());
363 IF_FALSE_LOGE_AND_RETURN_VAL(request != nullptr, GENERAL_ERROR);
364
365 bool setMsgTypeRet = request->SetInt32Value(Attributes::ATTR_MSG_TYPE, MessageType::EXECUTOR_FINISH);
366 IF_FALSE_LOGE_AND_RETURN_VAL(setMsgTypeRet, GENERAL_ERROR);
367
368 bool setScheduleIdRet = request->SetUint64Value(Attributes::ATTR_SCHEDULE_ID, remoteScheduleNode_->GetScheduleId());
369 IF_FALSE_LOGE_AND_RETURN_VAL(setScheduleIdRet, GENERAL_ERROR);
370
371 bool setResultCodeRet = request->SetInt32Value(Attributes::ATTR_RESULT_CODE, resultCode);
372 IF_FALSE_LOGE_AND_RETURN_VAL(setResultCodeRet, GENERAL_ERROR);
373
374 MsgCallback msgCallback = [](const std::shared_ptr<Attributes> &) { IAM_LOGI("message sent"); };
375
376 ResultCode sendMsgRet = RemoteConnectionManager::GetInstance().SendMessage(connectionName_, endPointName_,
377 EXECUTOR_PROXY_ENDPOINT_NAME, request, msgCallback);
378 IF_FALSE_LOGE_AND_RETURN_VAL(sendMsgRet == ResultCode::SUCCESS, GENERAL_ERROR);
379
380 IAM_LOGI("ConnectionName %{public}s scheduleId %{public}s continue schedule "
381 "success",
382 connectionName_.c_str(), GET_MASKED_STRING(remoteScheduleNode_->GetScheduleId()).c_str());
383 return SUCCESS;
384 }
385
ProcSendDataMsg(Attributes & attr)386 int32_t RemoteExecutorStub::ProcSendDataMsg(Attributes &attr)
387 {
388 std::lock_guard<std::recursive_mutex> lock(mutex_);
389 IAM_LOGI("start");
390
391 uint64_t scheduleId;
392 bool getScheduleIdRet = attr.GetUint64Value(Attributes::ATTR_SCHEDULE_ID, scheduleId);
393 IF_FALSE_LOGE_AND_RETURN_VAL(getScheduleIdRet, GENERAL_ERROR);
394 IAM_LOGI("ConnectionName %{public}s scheduleId %{public}s proc send data", connectionName_.c_str(),
395 GET_MASKED_STRING(scheduleId).c_str());
396
397 std::weak_ptr<ResourceNode> weakNode = ResourceNodePool::Instance().Select(executorIndex_);
398 std::shared_ptr<ResourceNode> node = weakNode.lock();
399 IF_FALSE_LOGE_AND_RETURN_VAL(node != nullptr, GENERAL_ERROR);
400
401 int32_t ret = node->SendData(scheduleId, attr);
402 IF_FALSE_LOGE_AND_RETURN_VAL(ret == ResultCode::SUCCESS, GENERAL_ERROR);
403
404 IAM_LOGI("ConnectionName %{public}s scheduleId %{public}s proc send data success", connectionName_.c_str(),
405 GET_MASKED_STRING(scheduleId).c_str());
406 return ret;
407 }
408
409 } // namespace UserAuth
410 } // namespace UserIam
411 } // namespace OHOS