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 "resource_node.h"
17
18 #include <cinttypes>
19 #include <mutex>
20 #include <unordered_map>
21
22 #include "device_manager_util.h"
23 #include "hdi_wrapper.h"
24 #include "iam_check.h"
25 #include "iam_common_defines.h"
26 #include "iam_logger.h"
27 #include "iam_ptr.h"
28 #include "remote_msg_util.h"
29
30 #define LOG_TAG "USER_AUTH_SA"
31
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 class ResourceNodeImpl : public ResourceNode, public NoCopyable {
36 public:
37 ResourceNodeImpl(ExecutorRegisterInfo info, std::shared_ptr<IExecutorCallback> callback);
38 ~ResourceNodeImpl() override;
39
40 uint64_t GetExecutorIndex() const override;
41 std::string GetOwnerDeviceId() const override;
42 uint32_t GetOwnerPid() const override;
43 AuthType GetAuthType() const override;
44 ExecutorRole GetExecutorRole() const override;
45 uint64_t GetExecutorSensorHint() const override;
46 uint64_t GetExecutorMatcher() const override;
47 ExecutorSecureLevel GetExecutorEsl() const override;
48 std::vector<uint8_t> GetExecutorPublicKey() const override;
49 std::string GetExecutorDeviceUdid() const override;
50
51 int32_t BeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
52 const Attributes &command) override;
53 int32_t EndExecute(uint64_t scheduleId, const Attributes &command) override;
54 int32_t SetProperty(const Attributes &properties) override;
55 int32_t GetProperty(const Attributes &condition, Attributes &values) override;
56 int32_t SendData(uint64_t scheduleId, const Attributes &data) override;
57 void DeleteFromDriver() override;
58 void DetachFromDriver() override;
59 friend ResourceNode;
60
61 private:
62 int32_t AddToDriver(std::vector<uint64_t> &templateIdList, std::vector<uint8_t> &fwkPublicKey);
63 static void DeleteExecutorFromDriver(uint64_t executorIndex);
64
65 ExecutorRegisterInfo info_;
66 std::shared_ptr<IExecutorCallback> callback_;
67 uint64_t executorIndex_ {0};
68 std::recursive_mutex mutex_;
69 bool addedToDriver_ {false};
70 };
71
ResourceNodeImpl(ExecutorRegisterInfo info,std::shared_ptr<IExecutorCallback> callback)72 ResourceNodeImpl::ResourceNodeImpl(ExecutorRegisterInfo info, std::shared_ptr<IExecutorCallback> callback)
73 : info_(std::move(info)),
74 callback_(std::move(callback))
75 {
76 if (info_.deviceUdid.empty()) {
77 bool setUdidRet = DeviceManagerUtil::GetInstance().GetLocalDeviceUdid(info_.deviceUdid);
78 IF_FALSE_LOGE_AND_RETURN(setUdidRet);
79 }
80 }
81
~ResourceNodeImpl()82 ResourceNodeImpl::~ResourceNodeImpl()
83 {
84 if (!addedToDriver_) {
85 return;
86 }
87
88 DeleteExecutorFromDriver(executorIndex_);
89 }
90
GetExecutorIndex() const91 uint64_t ResourceNodeImpl::GetExecutorIndex() const
92 {
93 return executorIndex_;
94 }
95
GetOwnerDeviceId() const96 std::string ResourceNodeImpl::GetOwnerDeviceId() const
97 {
98 return {};
99 }
100
GetOwnerPid() const101 uint32_t ResourceNodeImpl::GetOwnerPid() const
102 {
103 return SUCCESS;
104 }
105
GetAuthType() const106 AuthType ResourceNodeImpl::GetAuthType() const
107 {
108 return info_.authType;
109 }
110
GetExecutorRole() const111 ExecutorRole ResourceNodeImpl::GetExecutorRole() const
112 {
113 return info_.executorRole;
114 }
115
GetExecutorSensorHint() const116 uint64_t ResourceNodeImpl::GetExecutorSensorHint() const
117 {
118 return info_.executorSensorHint;
119 }
120
GetExecutorMatcher() const121 uint64_t ResourceNodeImpl::GetExecutorMatcher() const
122 {
123 return info_.executorMatcher;
124 }
125
GetExecutorEsl() const126 ExecutorSecureLevel ResourceNodeImpl::GetExecutorEsl() const
127 {
128 return info_.esl;
129 }
130
GetExecutorPublicKey() const131 std::vector<uint8_t> ResourceNodeImpl::GetExecutorPublicKey() const
132 {
133 return info_.publicKey;
134 }
135
GetExecutorDeviceUdid() const136 std::string ResourceNodeImpl::GetExecutorDeviceUdid() const
137 {
138 return info_.deviceUdid;
139 }
140
BeginExecute(uint64_t scheduleId,const std::vector<uint8_t> & publicKey,const Attributes & command)141 int32_t ResourceNodeImpl::BeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
142 const Attributes &command)
143 {
144 IAM_LOGD("start");
145 if (callback_ != nullptr) {
146 return callback_->OnBeginExecute(scheduleId, publicKey, command.Serialize());
147 }
148 return GENERAL_ERROR;
149 }
150
EndExecute(uint64_t scheduleId,const Attributes & command)151 int32_t ResourceNodeImpl::EndExecute(uint64_t scheduleId, const Attributes &command)
152 {
153 IAM_LOGD("start");
154 if (callback_ != nullptr) {
155 return callback_->OnEndExecute(scheduleId, command.Serialize());
156 }
157 return GENERAL_ERROR;
158 }
159
SetProperty(const Attributes & properties)160 int32_t ResourceNodeImpl::SetProperty(const Attributes &properties)
161 {
162 IAM_LOGD("start");
163 if (callback_ != nullptr) {
164 return callback_->OnSetProperty(properties.Serialize());
165 }
166 return GENERAL_ERROR;
167 }
168
GetProperty(const Attributes & condition,Attributes & values)169 int32_t ResourceNodeImpl::GetProperty(const Attributes &condition, Attributes &values)
170 {
171 IAM_LOGD("start");
172 if (callback_ != nullptr) {
173 std::vector<uint8_t> attribute;
174 auto ret = callback_->OnGetProperty(condition.Serialize(), attribute);
175 if (ret == SUCCESS) {
176 values = Attributes(attribute);
177 }
178 return ret;
179 }
180 return GENERAL_ERROR;
181 }
182
SendData(uint64_t scheduleId,const Attributes & data)183 int32_t ResourceNodeImpl::SendData(uint64_t scheduleId, const Attributes &data)
184 {
185 IAM_LOGD("start");
186
187 if (callback_ != nullptr) {
188 return callback_->OnSendData(scheduleId, data.Serialize());
189 }
190 return GENERAL_ERROR;
191 }
192
DeleteFromDriver()193 void ResourceNodeImpl::DeleteFromDriver()
194 {
195 IAM_LOGI("start");
196 std::lock_guard<std::recursive_mutex> lock(mutex_);
197 if (addedToDriver_) {
198 DeleteExecutorFromDriver(executorIndex_);
199 }
200 addedToDriver_ = false;
201 }
202
DetachFromDriver()203 void ResourceNodeImpl::DetachFromDriver()
204 {
205 IAM_LOGI("start");
206 std::lock_guard<std::recursive_mutex> lock(mutex_);
207 addedToDriver_ = false;
208 }
209
AddToDriver(std::vector<uint64_t> & templateIdList,std::vector<uint8_t> & fwkPublicKey)210 int32_t ResourceNodeImpl::AddToDriver(std::vector<uint64_t> &templateIdList, std::vector<uint8_t> &fwkPublicKey)
211 {
212 HdiExecutorRegisterInfo hdiInfo = {
213 .authType = static_cast<HdiAuthType>(info_.authType),
214 .executorRole = static_cast<HdiExecutorRole>(info_.executorRole),
215 .executorSensorHint = info_.executorSensorHint,
216 .executorMatcher = info_.executorMatcher,
217 .esl = static_cast<HdiExecutorSecureLevel>(info_.esl),
218 .maxTemplateAcl = info_.maxTemplateAcl,
219 .publicKey = info_.publicKey,
220 .deviceUdid = info_.deviceUdid,
221 .signedRemoteExecutorInfo = info_.signedRemoteExecutorInfo,
222 };
223
224 auto hdi = HdiWrapper::GetHdiInstance();
225 if (!hdi) {
226 IAM_LOGE("bad hdi");
227 return GENERAL_ERROR;
228 }
229
230 int32_t result = hdi->AddExecutor(hdiInfo, executorIndex_, fwkPublicKey, templateIdList);
231 if (result != HDF_SUCCESS) {
232 IAM_LOGE("hdi AddExecutor failed with code %{public}d", result);
233 return GENERAL_ERROR;
234 }
235 addedToDriver_ = true;
236 IAM_LOGI("hdi AddExecutor ****%{public}hx success", static_cast<uint16_t>(executorIndex_));
237 return SUCCESS;
238 }
239
DeleteExecutorFromDriver(uint64_t executorIndex)240 void ResourceNodeImpl::DeleteExecutorFromDriver(uint64_t executorIndex)
241 {
242 auto hdi = HdiWrapper::GetHdiInstance();
243 if (!hdi) {
244 IAM_LOGE("bad hdi");
245 return;
246 }
247
248 auto result = hdi->DeleteExecutor(executorIndex);
249 if (result != HDF_SUCCESS) {
250 IAM_LOGE("hdi DeleteExecutor ****%{public}hx with %{public}d", static_cast<uint16_t>(executorIndex), result);
251 return;
252 }
253 IAM_LOGI("hdi DeleteExecutor ****%{public}hx success", static_cast<uint16_t>(executorIndex));
254 }
255
MakeNewResource(const ExecutorRegisterInfo & info,const std::shared_ptr<IExecutorCallback> & callback,std::vector<uint64_t> & templateIdList,std::vector<uint8_t> & fwkPublicKey)256 std::shared_ptr<ResourceNode> ResourceNode::MakeNewResource(const ExecutorRegisterInfo &info,
257 const std::shared_ptr<IExecutorCallback> &callback, std::vector<uint64_t> &templateIdList,
258 std::vector<uint8_t> &fwkPublicKey)
259 {
260 auto node = Common::MakeShared<ResourceNodeImpl>(info, callback);
261 if (node == nullptr) {
262 IAM_LOGE("bad alloc");
263 return nullptr;
264 }
265
266 int32_t result = node->AddToDriver(templateIdList, fwkPublicKey);
267 if (result != 0) {
268 IAM_LOGE("hdi error with %{public}d", result);
269 return nullptr;
270 }
271
272 return node;
273 }
274 } // namespace UserAuth
275 } // namespace UserIam
276 } // namespace OHOS
277