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