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 "collector_impl.h"
17 #include <hdf_base.h>
18 #include <securec.h>
19 #include "executor_impl_common.h"
20 #include "iam_logger.h"
21 
22 #undef LOG_TAG
23 #define LOG_TAG "PIN_AUTH_IMPL_C"
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace PinAuth {
CollectorImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi)28 CollectorImpl::CollectorImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi)
29     : pinHdi_(pinHdi),
30       threadPool_("pin_collector_async")
31 {
32     threadPool_.Start(1);
33 }
34 
~CollectorImpl()35 CollectorImpl::~CollectorImpl()
36 {
37     threadPool_.Stop();
38 }
39 
GetExecutorInfo(HdiExecutorInfo & executorInfo)40 int32_t CollectorImpl::GetExecutorInfo(HdiExecutorInfo &executorInfo)
41 {
42     IAM_LOGI("start");
43     if (pinHdi_ == nullptr) {
44         IAM_LOGE("pinHdi_ is nullptr");
45         return HDF_FAILURE;
46     }
47     executorInfo.sensorId = SENSOR_ID;
48     executorInfo.executorMatcher = EXECUTOR_MATCHER;
49     executorInfo.executorRole = HdiExecutorRole::COLLECTOR;
50     executorInfo.authType = HdiAuthType::PIN;
51     uint32_t eslRet = 0;
52     int32_t result = pinHdi_->GetExecutorInfo(HdiExecutorRole::COLLECTOR, executorInfo.publicKey, eslRet,
53         executorInfo.maxTemplateAcl);
54     if (result != SUCCESS) {
55         IAM_LOGE("Get collector ExecutorInfo failed, fail code:%{public}d", result);
56         return HDF_FAILURE;
57     }
58     executorInfo.esl = static_cast<HdiExecutorSecureLevel>(eslRet);
59     return HDF_SUCCESS;
60 }
61 
OnRegisterFinish(const std::vector<uint64_t> & templateIdList,const std::vector<uint8_t> & frameworkPublicKey,const std::vector<uint8_t> & extraInfo)62 int32_t CollectorImpl::OnRegisterFinish(const std::vector<uint64_t> &templateIdList,
63     const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo)
64 {
65     IAM_LOGI("start");
66     static_cast<void>(templateIdList);
67     static_cast<void>(extraInfo);
68     if (pinHdi_ == nullptr) {
69         IAM_LOGE("pinHdi_ is nullptr");
70         return HDF_FAILURE;
71     }
72     int32_t result = pinHdi_->SetCollectorFwkParam(frameworkPublicKey);
73     if (result != SUCCESS) {
74         IAM_LOGE("Hdi SetCollectorFwkParam fail");
75         return HDF_FAILURE;
76     }
77     return HDF_SUCCESS;
78 }
79 
IsCurrentSchedule(uint64_t scheduleId)80 bool CollectorImpl::IsCurrentSchedule(uint64_t scheduleId)
81 {
82     if (!scheduleId_.has_value()) {
83         IAM_LOGE("collect schedule not exist");
84         return false;
85     }
86     if (scheduleId_.value() != scheduleId) {
87         IAM_LOGE("collect schedule:%{public}x not match current:%{public}x",
88             (uint16_t)scheduleId, (uint16_t)scheduleId_.value());
89         return false;
90     }
91     return true;
92 }
93 
CancelCurrentCollect(int32_t errorCode)94 void CollectorImpl::CancelCurrentCollect(int32_t errorCode)
95 {
96     if (!scheduleId_.has_value()) {
97         return;
98     }
99     if (pinHdi_->CancelCollect() != SUCCESS) {
100         IAM_LOGE("Hdi CancelCollect fail");
101     }
102     CallError(callback_, errorCode);
103     scheduleId_ = std::nullopt;
104     callback_ = nullptr;
105 }
106 
Cancel(uint64_t scheduleId)107 int32_t CollectorImpl::Cancel(uint64_t scheduleId)
108 {
109     IAM_LOGI("start %{public}x", (uint16_t)scheduleId);
110     if (pinHdi_ == nullptr) {
111         IAM_LOGE("pinHdi_ is nullptr");
112         return HDF_FAILURE;
113     }
114     threadPool_.AddTask([this, id = scheduleId]() {
115         if (IsCurrentSchedule(id)) {
116             CancelCurrentCollect();
117         }
118     });
119     return HDF_SUCCESS;
120 }
121 
SendMessage(uint64_t scheduleId,int32_t srcRole,const std::vector<uint8_t> & msg)122 int32_t CollectorImpl::SendMessage(uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg)
123 {
124     IAM_LOGI("start schedule:%{public}x src:%{public}d", (uint16_t)scheduleId, srcRole);
125     if (pinHdi_ == nullptr) {
126         IAM_LOGE("pinHdi_ is nullptr");
127         return HDF_FAILURE;
128     }
129     if (srcRole != HdiExecutorRole::VERIFIER) {
130         IAM_LOGI("only verifier src handled");
131         return HDF_SUCCESS;
132     }
133     threadPool_.AddTask([this, id = scheduleId, message = msg]() {
134         if (!IsCurrentSchedule(id)) {
135             return;
136         }
137         OHOS::UserIam::PinAuth::PinAlgoParam pinAlgoParam = {};
138         if (pinHdi_->SendMessageToCollector(id, message, pinAlgoParam) != SUCCESS) {
139             IAM_LOGE("Hdi SendMessageToCollector fail");
140             return;
141         }
142         std::string pinComplexityReg = "";
143         if (callback_->OnGetData(pinAlgoParam.algoParameter, pinAlgoParam.subType, pinAlgoParam.algoVersion,
144             pinAlgoParam.challenge, pinComplexityReg) != SUCCESS) {
145             IAM_LOGE("Hdi callback OnGetData fail");
146             CancelCurrentCollect();
147         }
148     });
149     return HDF_SUCCESS;
150 }
151 
ClearPinData(const std::vector<uint8_t> & pinData)152 void CollectorImpl::ClearPinData(const std::vector<uint8_t> &pinData)
153 {
154     if (pinData.size() != 0) {
155         uint8_t *data = const_cast<uint8_t *>(pinData.data());
156         (void)memset_s(data, pinData.size(), 0, pinData.size());
157     }
158 }
159 
SetData(uint64_t scheduleId,uint64_t authSubType,const std::vector<uint8_t> & data,int32_t resultCode)160 int32_t CollectorImpl::SetData(
161     uint64_t scheduleId, uint64_t authSubType, const std::vector<uint8_t> &data, int32_t resultCode)
162 {
163     IAM_LOGI("start %{public}x", (uint16_t)scheduleId);
164     static_cast<void>(authSubType);
165     if (pinHdi_ == nullptr) {
166         IAM_LOGE("pinHdi_ is nullptr");
167         return HDF_FAILURE;
168     }
169     threadPool_.AddTask([this, id = scheduleId, pinData = data, result = resultCode]() {
170         if (!IsCurrentSchedule(id)) {
171             ClearPinData(pinData);
172             return;
173         }
174         if (result != SUCCESS) {
175             CancelCurrentCollect(result);
176             ClearPinData(pinData);
177             return;
178         }
179         std::vector<uint8_t> msg;
180         if (pinHdi_->SetDataToCollector(id, pinData, msg) != SUCCESS) {
181             IAM_LOGE("Hdi SetDataToCollector fail");
182             CancelCurrentCollect();
183             ClearPinData(pinData);
184             return;
185         }
186         int32_t ret = callback_->OnMessage(HdiExecutorRole::VERIFIER, msg);
187         if (ret != SUCCESS) {
188             IAM_LOGE("Send collector ack msg fail");
189             CancelCurrentCollect();
190             ClearPinData(pinData);
191             return;
192         }
193     });
194     ClearPinData(data);
195     return HDF_SUCCESS;
196 }
197 
Collect(uint64_t scheduleId,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj)198 int32_t CollectorImpl::Collect(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
199     const sptr<HdiIExecutorCallback> &callbackObj)
200 {
201     IAM_LOGI("start %{public}x", (uint16_t)scheduleId);
202     if (callbackObj == nullptr) {
203         IAM_LOGE("callbackObj is nullptr");
204         return HDF_FAILURE;
205     }
206     if (pinHdi_ == nullptr) {
207         IAM_LOGE("pinHdi_ is nullptr");
208         CallError(callbackObj, INVALID_PARAMETERS);
209         return HDF_SUCCESS;
210     }
211     threadPool_.AddTask([this, id = scheduleId, extra = extraInfo, callback = callbackObj]() {
212         CancelCurrentCollect();
213         std::vector<uint8_t> msg;
214         int32_t result = pinHdi_->Collect(id, extra, msg);
215         if (result != SUCCESS) {
216             IAM_LOGE("Collect fail");
217             CallError(callback, result);
218             return;
219         }
220         result = callback->OnMessage(HdiExecutorRole::VERIFIER, msg);
221         if (result != SUCCESS) {
222             IAM_LOGE("Send collector sync msg fail");
223             CallError(callback, result);
224             return;
225         }
226         scheduleId_ = id;
227         callback_ = callback;
228     });
229     return HDF_SUCCESS;
230 }
231 } // PinAuth
232 } // HDI
233 } // OHOS