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 "hdi_wrapper.h"
23 #include "iam_logger.h"
24 #include "iam_ptr.h"
25 #include "iam_common_defines.h"
26
27 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 class ResourceNodeImpl : public ResourceNode, public NoCopyable {
33 public:
34 using IUserAuthInterface = OHOS::HDI::UserAuth::V1_0::IUserAuthInterface;
35 ResourceNodeImpl(ExecutorRegisterInfo info, std::shared_ptr<ExecutorCallbackInterface> callback);
36 ~ResourceNodeImpl() override;
37
38 uint64_t GetExecutorIndex() const override;
39 std::string GetOwnerDeviceId() const override;
40 uint32_t GetOwnerPid() const override;
41 AuthType GetAuthType() const override;
42 ExecutorRole GetExecutorRole() const override;
43 uint64_t GetExecutorSensorHint() const override;
44 uint64_t GetExecutorMatcher() const override;
45 ExecutorSecureLevel GetExecutorEsl() const override;
46 std::vector<uint8_t> GetExecutorPublicKey() const override;
47
48 int32_t BeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
49 const Attributes &command) override;
50 int32_t EndExecute(uint64_t scheduleId, const Attributes &command) override;
51 int32_t SetProperty(const Attributes &properties) override;
52 int32_t GetProperty(const Attributes &condition, Attributes &values) override;
53 void Detach() override;
54 friend ResourceNode;
55
56 private:
57 int32_t SyncWithDriver(std::vector<uint64_t> &templateIdList, std::vector<uint8_t> &fwkPublicKey);
58
59 ExecutorRegisterInfo info_;
60 std::shared_ptr<ExecutorCallbackInterface> callback_;
61 uint64_t executeIndex_ {0};
62 bool synced {false};
63 };
64
ResourceNodeImpl(ExecutorRegisterInfo info,std::shared_ptr<ExecutorCallbackInterface> callback)65 ResourceNodeImpl::ResourceNodeImpl(ExecutorRegisterInfo info, std::shared_ptr<ExecutorCallbackInterface> callback)
66 : info_(std::move(info)),
67 callback_(std::move(callback))
68 {
69 }
70
~ResourceNodeImpl()71 ResourceNodeImpl::~ResourceNodeImpl()
72 {
73 if (!synced) {
74 return;
75 }
76 auto hdi = HdiWrapper::GetHdiInstance();
77 if (!hdi) {
78 IAM_LOGE("bad hdi");
79 return;
80 }
81
82 auto result = hdi->DeleteExecutor(executeIndex_);
83 if (result != HDF_SUCCESS) {
84 IAM_LOGE("hdi DeleteExecutor ****%{public}hx with %{public}d", static_cast<uint16_t>(executeIndex_), result);
85 return;
86 }
87 IAM_LOGI("hdi DeleteExecutor ****%{public}hx success", static_cast<uint16_t>(executeIndex_));
88 }
89
GetExecutorIndex() const90 uint64_t ResourceNodeImpl::GetExecutorIndex() const
91 {
92 return executeIndex_;
93 }
94
GetOwnerDeviceId() const95 std::string ResourceNodeImpl::GetOwnerDeviceId() const
96 {
97 return {};
98 }
99
GetOwnerPid() const100 uint32_t ResourceNodeImpl::GetOwnerPid() const
101 {
102 return SUCCESS;
103 }
104
GetAuthType() const105 AuthType ResourceNodeImpl::GetAuthType() const
106 {
107 return info_.authType;
108 }
109
GetExecutorRole() const110 ExecutorRole ResourceNodeImpl::GetExecutorRole() const
111 {
112 return info_.executorRole;
113 }
114
GetExecutorSensorHint() const115 uint64_t ResourceNodeImpl::GetExecutorSensorHint() const
116 {
117 return info_.executorSensorHint;
118 }
119
GetExecutorMatcher() const120 uint64_t ResourceNodeImpl::GetExecutorMatcher() const
121 {
122 return info_.executorMatcher;
123 }
124
GetExecutorEsl() const125 ExecutorSecureLevel ResourceNodeImpl::GetExecutorEsl() const
126 {
127 return info_.esl;
128 }
129
GetExecutorPublicKey() const130 std::vector<uint8_t> ResourceNodeImpl::GetExecutorPublicKey() const
131 {
132 return info_.publicKey;
133 }
134
BeginExecute(uint64_t scheduleId,const std::vector<uint8_t> & publicKey,const Attributes & command)135 int32_t ResourceNodeImpl::BeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
136 const Attributes &command)
137 {
138 IAM_LOGI("start");
139 if (callback_ != nullptr) {
140 return callback_->OnBeginExecute(scheduleId, publicKey, command);
141 }
142 return GENERAL_ERROR;
143 }
144
EndExecute(uint64_t scheduleId,const Attributes & command)145 int32_t ResourceNodeImpl::EndExecute(uint64_t scheduleId, const Attributes &command)
146 {
147 IAM_LOGI("start");
148 if (callback_ != nullptr) {
149 return callback_->OnEndExecute(scheduleId, command);
150 }
151 return GENERAL_ERROR;
152 }
153
SetProperty(const Attributes & properties)154 int32_t ResourceNodeImpl::SetProperty(const Attributes &properties)
155 {
156 IAM_LOGI("start");
157 if (callback_ != nullptr) {
158 return callback_->OnSetProperty(properties);
159 }
160 return GENERAL_ERROR;
161 }
162
GetProperty(const Attributes & condition,Attributes & values)163 int32_t ResourceNodeImpl::GetProperty(const Attributes &condition, Attributes &values)
164 {
165 IAM_LOGI("start");
166 if (callback_ != nullptr) {
167 return callback_->OnGetProperty(condition, values);
168 }
169 return GENERAL_ERROR;
170 }
171
Detach()172 void ResourceNodeImpl::Detach()
173 {
174 IAM_LOGI("start");
175 synced = false;
176 }
177
SyncWithDriver(std::vector<uint64_t> & templateIdList,std::vector<uint8_t> & fwkPublicKey)178 int32_t ResourceNodeImpl::SyncWithDriver(std::vector<uint64_t> &templateIdList, std::vector<uint8_t> &fwkPublicKey)
179 {
180 using HdiExecutorRegisterInfo = OHOS::HDI::UserAuth::V1_0::ExecutorRegisterInfo;
181 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
182 using HdiExecutorRole = OHOS::HDI::UserAuth::V1_0::ExecutorRole;
183 using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V1_0::ExecutorSecureLevel;
184
185 HdiExecutorRegisterInfo hdiInfo = {
186 .authType = static_cast<HdiAuthType>(info_.authType),
187 .executorRole = static_cast<HdiExecutorRole>(info_.executorRole),
188 .executorSensorHint = info_.executorSensorHint,
189 .executorMatcher = info_.executorMatcher,
190 .esl = static_cast<HdiExecutorSecureLevel>(info_.esl),
191 .publicKey = info_.publicKey,
192 };
193
194 auto hdi = HdiWrapper::GetHdiInstance();
195 if (!hdi) {
196 IAM_LOGE("bad hdi");
197 return GENERAL_ERROR;
198 }
199
200 int32_t result = hdi->AddExecutor(hdiInfo, executeIndex_, fwkPublicKey, templateIdList);
201 if (result != HDF_SUCCESS) {
202 IAM_LOGE("hdi AddExecutor failed with code %{public}d", result);
203 return GENERAL_ERROR;
204 }
205 synced = true;
206 IAM_LOGI("hdi AddExecutor ****%{public}hx success", static_cast<uint16_t>(executeIndex_));
207 return SUCCESS;
208 }
209
MakeNewResource(const ExecutorRegisterInfo & info,const std::shared_ptr<ExecutorCallbackInterface> & callback,std::vector<uint64_t> & templateIdList,std::vector<uint8_t> & fwkPublicKey)210 std::shared_ptr<ResourceNode> ResourceNode::MakeNewResource(const ExecutorRegisterInfo &info,
211 const std::shared_ptr<ExecutorCallbackInterface> &callback, std::vector<uint64_t> &templateIdList,
212 std::vector<uint8_t> &fwkPublicKey)
213 {
214 auto node = Common::MakeShared<ResourceNodeImpl>(info, callback);
215 if (node == nullptr) {
216 IAM_LOGE("bad alloc");
217 return nullptr;
218 }
219
220 int32_t result = node->SyncWithDriver(templateIdList, fwkPublicKey);
221 if (result != 0) {
222 IAM_LOGE("hdi error with %{public}d", result);
223 return nullptr;
224 }
225
226 return node;
227 }
228 } // namespace UserAuth
229 } // namespace UserIam
230 } // namespace OHOS
231