• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "pin_auth_all_in_one_hdi.h"
17 
18 #include <map>
19 
20 #include "hdf_base.h"
21 
22 #include "iam_check.h"
23 #include "iam_defines.h"
24 #include "iam_logger.h"
25 #include "pin_auth_executor_callback_hdi.h"
26 #include "pin_auth_executor_hdi_common.h"
27 
28 #define LOG_TAG "PIN_AUTH_SA"
29 
30 namespace OHOS {
31 namespace UserIam {
32 namespace PinAuth {
PinAuthAllInOneHdi(const sptr<IAllInOneExecutor> & allInOneProxy)33 PinAuthAllInOneHdi::PinAuthAllInOneHdi(const sptr<IAllInOneExecutor> &allInOneProxy)
34     : allInOneProxy_(allInOneProxy)
35 {
36 }
37 
GetExecutorInfo(UserAuth::ExecutorInfo & info)38 UserAuth::ResultCode PinAuthAllInOneHdi::GetExecutorInfo(UserAuth::ExecutorInfo &info)
39 {
40     if (allInOneProxy_ == nullptr) {
41         IAM_LOGE("allInOneProxy is null");
42         return UserAuth::ResultCode::GENERAL_ERROR;
43     }
44 
45     ExecutorInfo localInfo = { };
46     int32_t status = allInOneProxy_->GetExecutorInfo(localInfo);
47     UserAuth::ResultCode result = ConvertHdiResultCode(status);
48     if (result != UserAuth::ResultCode::SUCCESS) {
49         IAM_LOGE("GetExecutorInfo fail ret=%{public}d", result);
50         return result;
51     }
52     int32_t ret = MoveHdiExecutorInfo(localInfo, info);
53     if (ret != UserAuth::ResultCode::SUCCESS) {
54         IAM_LOGE("MoveHdiExecutorInfo fail ret=%{public}d", ret);
55         return UserAuth::ResultCode::GENERAL_ERROR;
56     }
57     return UserAuth::ResultCode::SUCCESS;
58 }
59 
OnRegisterFinish(const std::vector<uint64_t> & templateIdList,const std::vector<uint8_t> & frameworkPublicKey,const std::vector<uint8_t> & extraInfo)60 UserAuth::ResultCode PinAuthAllInOneHdi::OnRegisterFinish(const std::vector<uint64_t> &templateIdList,
61     const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo)
62 {
63     if (allInOneProxy_ == nullptr) {
64         IAM_LOGE("allInOneProxy is null");
65         return UserAuth::ResultCode::GENERAL_ERROR;
66     }
67     int32_t status = allInOneProxy_->OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
68     UserAuth::ResultCode result = ConvertHdiResultCode(status);
69     if (result != UserAuth::ResultCode::SUCCESS) {
70         IAM_LOGE("OnRegisterFinish fail result %{public}d", result);
71         return result;
72     }
73 
74     return UserAuth::ResultCode::SUCCESS;
75 }
76 
SendMessage(uint64_t scheduleId,int32_t srcRole,const std::vector<uint8_t> & msg)77 UserAuth::ResultCode PinAuthAllInOneHdi::SendMessage(
78     uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg)
79 {
80     if (allInOneProxy_ == nullptr) {
81         IAM_LOGE("allInOneProxy is null");
82         return UserAuth::ResultCode::GENERAL_ERROR;
83     }
84     int32_t status = allInOneProxy_->SendMessage(scheduleId, srcRole, msg);
85     UserAuth::ResultCode result = ConvertHdiResultCode(status);
86     if (result != UserAuth::ResultCode::SUCCESS) {
87         IAM_LOGE("SendMessage fail result %{public}d", result);
88         return result;
89     }
90     return UserAuth::ResultCode::SUCCESS;
91 }
92 
OnSetData(uint64_t scheduleId,uint64_t authSubType,const std::vector<uint8_t> & data,int32_t errorCode)93 UserAuth::ResultCode PinAuthAllInOneHdi::OnSetData(uint64_t scheduleId, uint64_t authSubType,
94     const std::vector<uint8_t> &data, int32_t errorCode)
95 {
96     if (allInOneProxy_ == nullptr) {
97         IAM_LOGE("allInOneProxy is null");
98         return UserAuth::ResultCode::GENERAL_ERROR;
99     }
100     int32_t status = allInOneProxy_->SetData(scheduleId, authSubType, data, errorCode);
101     UserAuth::ResultCode result = ConvertHdiResultCode(status);
102     if (result != UserAuth::ResultCode::SUCCESS) {
103         IAM_LOGE("OnSetData fail ret=%{public}d", result);
104         return result;
105     }
106     return UserAuth::ResultCode::SUCCESS;
107 }
108 
Enroll(uint64_t scheduleId,const UserAuth::EnrollParam & param,const std::shared_ptr<UserAuth::IExecuteCallback> & callbackObj)109 UserAuth::ResultCode PinAuthAllInOneHdi::Enroll(uint64_t scheduleId, const UserAuth::EnrollParam &param,
110     const std::shared_ptr<UserAuth::IExecuteCallback> &callbackObj)
111 {
112     if (allInOneProxy_ == nullptr) {
113         IAM_LOGE("allInOneProxy is null");
114         return UserAuth::ResultCode::GENERAL_ERROR;
115     }
116     if (callbackObj == nullptr) {
117         IAM_LOGE("callbackObj is null");
118         return UserAuth::ResultCode::GENERAL_ERROR;
119     }
120     UserAuth::ExecutorParam executorParam = {
121         .tokenId = param.tokenId,
122         .scheduleId = scheduleId,
123         .userId = param.userId,
124     };
125     auto callback = sptr<IExecutorCallback>(new (std::nothrow) PinAuthExecutorCallbackHdi(callbackObj,
126         shared_from_this(), executorParam, GET_DATA_MODE_ALL_IN_ONE_ENROLL));
127     if (callback == nullptr) {
128         IAM_LOGE("callback is null");
129         return UserAuth::ResultCode::GENERAL_ERROR;
130     }
131     int32_t status = allInOneProxy_->Enroll(scheduleId, param.extraInfo, callback);
132     UserAuth::ResultCode result = ConvertHdiResultCode(status);
133     if (result != UserAuth::ResultCode::SUCCESS) {
134         IAM_LOGE("Enroll fail ret=%{public}d", result);
135         return result;
136     }
137     return UserAuth::ResultCode::SUCCESS;
138 }
139 
Authenticate(uint64_t scheduleId,const UserAuth::AuthenticateParam & param,const std::shared_ptr<UserAuth::IExecuteCallback> & callbackObj)140 UserAuth::ResultCode PinAuthAllInOneHdi::Authenticate(
141     uint64_t scheduleId, const UserAuth::AuthenticateParam &param,
142     const std::shared_ptr<UserAuth::IExecuteCallback> &callbackObj)
143 {
144     if (allInOneProxy_ == nullptr) {
145         IAM_LOGE("allInOneProxy is null");
146         return UserAuth::ResultCode::GENERAL_ERROR;
147     }
148     if (callbackObj == nullptr) {
149         IAM_LOGE("callbackObj is null");
150         return UserAuth::ResultCode::GENERAL_ERROR;
151     }
152     UserAuth::ExecutorParam executorParam = {
153         .tokenId = param.tokenId,
154         .authIntent = param.authIntent,
155         .scheduleId = scheduleId,
156         .userId = param.userId,
157     };
158     auto callback = sptr<IExecutorCallback>(new (std::nothrow) PinAuthExecutorCallbackHdi(callbackObj,
159         shared_from_this(), executorParam, GET_DATA_MODE_ALL_IN_ONE_AUTH));
160     if (callback == nullptr) {
161         IAM_LOGE("callback is null");
162         return UserAuth::ResultCode::GENERAL_ERROR;
163     }
164     if (param.templateIdList.size() == 0) {
165         IAM_LOGE("Error param");
166         return UserAuth::ResultCode::GENERAL_ERROR;
167     }
168     int32_t status = allInOneProxy_->Authenticate(scheduleId, param.templateIdList,
169         param.extraInfo, callback);
170     UserAuth::ResultCode result = ConvertHdiResultCode(status);
171     if (result != UserAuth::ResultCode::SUCCESS) {
172         IAM_LOGE("Authenticate fail ret=%{public}d", result);
173         return result;
174     }
175     return UserAuth::ResultCode::SUCCESS;
176 }
177 
Delete(const std::vector<uint64_t> & templateIdList)178 UserAuth::ResultCode PinAuthAllInOneHdi::Delete(const std::vector<uint64_t> &templateIdList)
179 {
180     if (allInOneProxy_ == nullptr) {
181         IAM_LOGE("allInOneProxy is null");
182         return UserAuth::ResultCode::GENERAL_ERROR;
183     }
184     if (templateIdList.empty()) {
185         IAM_LOGE("templateIdList is empty");
186         return UserAuth::ResultCode::GENERAL_ERROR;
187     }
188     int32_t status = allInOneProxy_->Delete(templateIdList[0]);
189     UserAuth::ResultCode result = ConvertHdiResultCode(status);
190     if (result != UserAuth::ResultCode::SUCCESS) {
191         IAM_LOGE("Delete fail ret=%{public}d", result);
192         return result;
193     }
194     return UserAuth::ResultCode::SUCCESS;
195 }
196 
Cancel(uint64_t scheduleId)197 UserAuth::ResultCode PinAuthAllInOneHdi::Cancel(uint64_t scheduleId)
198 {
199     if (allInOneProxy_ == nullptr) {
200         IAM_LOGE("allInOneProxy is null");
201         return UserAuth::ResultCode::GENERAL_ERROR;
202     }
203     int32_t status = allInOneProxy_->Cancel(scheduleId);
204     UserAuth::ResultCode result = ConvertHdiResultCode(status);
205     if (result != UserAuth::ResultCode::SUCCESS) {
206         IAM_LOGE("Cancel fail ret=%{public}d", result);
207         return result;
208     }
209     return UserAuth::ResultCode::SUCCESS;
210 }
211 
GetProperty(const std::vector<uint64_t> & templateIdList,const std::vector<UserAuth::Attributes::AttributeKey> & keys,UserAuth::Property & property)212 UserAuth::ResultCode PinAuthAllInOneHdi::GetProperty(const std::vector<uint64_t> &templateIdList,
213     const std::vector<UserAuth::Attributes::AttributeKey> &keys, UserAuth::Property &property)
214 {
215     IF_FALSE_LOGE_AND_RETURN_VAL(allInOneProxy_ != nullptr, UserAuth::ResultCode::GENERAL_ERROR);
216 
217     std::vector<int32_t> propertyTypes;
218     UserAuth::ResultCode result = ConvertAttributeKeyVectorToPropertyType(keys, propertyTypes);
219     IF_FALSE_LOGE_AND_RETURN_VAL(result == UserAuth::ResultCode::SUCCESS, UserAuth::ResultCode::GENERAL_ERROR);
220 
221     Property hdiProperty;
222     int32_t status = allInOneProxy_->GetProperty(templateIdList, propertyTypes, hdiProperty);
223     result = ConvertHdiResultCode(status);
224     if (result != UserAuth::ResultCode::SUCCESS) {
225         IAM_LOGE("SendCommand fail result %{public}d", result);
226         return result;
227     }
228     MoveHdiProperty(hdiProperty, property);
229     return UserAuth::ResultCode::SUCCESS;
230 }
231 
MoveHdiProperty(Property & in,UserAuth::Property & out)232 void PinAuthAllInOneHdi::MoveHdiProperty(Property &in, UserAuth::Property &out)
233 {
234     out.authSubType = in.authSubType;
235     out.lockoutDuration = in.lockoutDuration;
236     out.remainAttempts = in.remainAttempts;
237     out.nextFailLockoutDuration = in.nextFailLockoutDuration;
238 }
239 
ConvertAttributeKeyVectorToPropertyType(const std::vector<UserAuth::Attributes::AttributeKey> inItems,std::vector<int32_t> & outItems)240 UserAuth::ResultCode PinAuthAllInOneHdi::ConvertAttributeKeyVectorToPropertyType(
241     const std::vector<UserAuth::Attributes::AttributeKey> inItems, std::vector<int32_t> &outItems)
242 {
243     outItems.clear();
244     for (auto &inItem : inItems) {
245         if (inItem == UserAuth::Attributes::ATTR_ENROLL_PROGRESS ||
246             inItem == UserAuth::Attributes::ATTR_SENSOR_INFO) {
247             continue;
248         }
249         int32_t outItem;
250         UserAuth::ResultCode result = ConvertAttributeKeyToPropertyType(inItem, outItem);
251         IF_FALSE_LOGE_AND_RETURN_VAL(result == UserAuth::ResultCode::SUCCESS, UserAuth::ResultCode::GENERAL_ERROR);
252         outItems.push_back(outItem);
253     }
254 
255     return UserAuth::ResultCode::SUCCESS;
256 }
257 
ConvertAttributeKeyToPropertyType(const UserAuth::Attributes::AttributeKey in,int32_t & out)258 UserAuth::ResultCode PinAuthAllInOneHdi::ConvertAttributeKeyToPropertyType(const UserAuth::Attributes::AttributeKey in,
259     int32_t &out)
260 {
261     static const std::map<UserAuth::Attributes::AttributeKey, GetPropertyType> data = {
262         { UserAuth::Attributes::ATTR_PIN_SUB_TYPE, GetPropertyType::AUTH_SUB_TYPE },
263         { UserAuth::Attributes::ATTR_FREEZING_TIME, GetPropertyType::LOCKOUT_DURATION },
264         { UserAuth::Attributes::ATTR_REMAIN_TIMES, GetPropertyType::REMAIN_ATTEMPTS },
265         { UserAuth::Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION, GetPropertyType::NEXT_FAIL_LOCKOUT_DURATION },
266     };
267 
268     auto iter = data.find(in);
269     if (iter == data.end()) {
270         IAM_LOGE("attribute %{public}d is invalid", in);
271         return UserAuth::ResultCode::GENERAL_ERROR;
272     } else {
273         out = static_cast<int32_t>(iter->second);
274     }
275     IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", in, out);
276     return UserAuth::ResultCode::SUCCESS;
277 }
278 } // namespace PinAuth
279 } // namespace UserIam
280 } // namespace OHOS
281