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 "pin_auth_collector_hdi.h"
17 
18 #include "iam_logger.h"
19 #include "pin_auth_executor_callback_hdi.h"
20 #include "pin_auth_executor_hdi_common.h"
21 
22 #define LOG_TAG "PIN_AUTH_SA"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace PinAuth {
PinAuthCollectorHdi(const sptr<ICollector> & collectorProxy)27 PinAuthCollectorHdi::PinAuthCollectorHdi(const sptr<ICollector> &collectorProxy)
28     : collectorProxy_(collectorProxy)
29 {
30 }
31 
GetExecutorInfo(UserAuth::ExecutorInfo & info)32 UserAuth::ResultCode PinAuthCollectorHdi::GetExecutorInfo(UserAuth::ExecutorInfo &info)
33 {
34     IAM_LOGI("start");
35     if (collectorProxy_ == nullptr) {
36         IAM_LOGE("collectorProxy is null");
37         return UserAuth::ResultCode::GENERAL_ERROR;
38     }
39 
40     ExecutorInfo collectorInfo = {};
41     UserAuth::ResultCode result = ConvertHdiResultCode(collectorProxy_->GetExecutorInfo(collectorInfo));
42     if (result != UserAuth::ResultCode::SUCCESS) {
43         IAM_LOGE("GetExecutorInfo of collector fail:%{public}d", result);
44         return result;
45     }
46     if (MoveHdiExecutorInfo(collectorInfo, info) != UserAuth::ResultCode::SUCCESS) {
47         IAM_LOGE("MoveHdiExecutorInfo fail");
48         return UserAuth::ResultCode::GENERAL_ERROR;
49     }
50     return UserAuth::ResultCode::SUCCESS;
51 }
52 
OnRegisterFinish(const std::vector<uint64_t> & templateIdList,const std::vector<uint8_t> & frameworkPublicKey,const std::vector<uint8_t> & extraInfo)53 UserAuth::ResultCode PinAuthCollectorHdi::OnRegisterFinish(const std::vector<uint64_t> &templateIdList,
54     const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo)
55 {
56     IAM_LOGI("start");
57     if (collectorProxy_ == nullptr) {
58         IAM_LOGE("collectorProxy is null");
59         return UserAuth::ResultCode::GENERAL_ERROR;
60     }
61     int32_t status = collectorProxy_->OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
62     UserAuth::ResultCode result = ConvertHdiResultCode(status);
63     if (result != UserAuth::ResultCode::SUCCESS) {
64         IAM_LOGE("OnRegisterFinish fail:%{public}d", result);
65         return result;
66     }
67     return UserAuth::ResultCode::SUCCESS;
68 }
69 
Cancel(uint64_t scheduleId)70 UserAuth::ResultCode PinAuthCollectorHdi::Cancel(uint64_t scheduleId)
71 {
72     IAM_LOGI("start");
73     if (collectorProxy_ == nullptr) {
74         IAM_LOGE("collectorProxy is null");
75         return UserAuth::ResultCode::GENERAL_ERROR;
76     }
77     int32_t status = collectorProxy_->Cancel(scheduleId);
78     UserAuth::ResultCode result = ConvertHdiResultCode(status);
79     if (result != UserAuth::ResultCode::SUCCESS) {
80         IAM_LOGE("Cancel fail:%{public}d", result);
81         return result;
82     }
83     return UserAuth::ResultCode::SUCCESS;
84 }
85 
SendMessage(uint64_t scheduleId,int32_t srcRole,const std::vector<uint8_t> & msg)86 UserAuth::ResultCode PinAuthCollectorHdi::SendMessage(
87     uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg)
88 {
89     IAM_LOGI("start");
90     if (collectorProxy_ == nullptr) {
91         IAM_LOGE("collectorProxy is null");
92         return UserAuth::ResultCode::GENERAL_ERROR;
93     }
94     int32_t status = collectorProxy_->SendMessage(scheduleId, srcRole, msg);
95     UserAuth::ResultCode result = ConvertHdiResultCode(status);
96     if (result != UserAuth::ResultCode::SUCCESS) {
97         IAM_LOGE("SendMessage fail:%{public}d", result);
98         return result;
99     }
100     return UserAuth::ResultCode::SUCCESS;
101 }
102 
Collect(uint64_t scheduleId,const UserAuth::CollectParam & param,const std::shared_ptr<UserAuth::IExecuteCallback> & callbackObj)103 UserAuth::ResultCode PinAuthCollectorHdi::Collect(uint64_t scheduleId, const UserAuth::CollectParam &param,
104     const std::shared_ptr<UserAuth::IExecuteCallback> &callbackObj)
105 {
106     IAM_LOGI("start");
107     if (collectorProxy_ == nullptr) {
108         IAM_LOGE("collectorProxy is null");
109         return UserAuth::ResultCode::GENERAL_ERROR;
110     }
111     if (callbackObj == nullptr) {
112         IAM_LOGE("callbackObj is null");
113         return UserAuth::ResultCode::GENERAL_ERROR;
114     }
115     UserAuth::ExecutorParam executorParam = {
116         .tokenId = param.collectorTokenId,
117         .scheduleId = scheduleId,
118     };
119     auto callback = sptr<IExecutorCallback>(new (std::nothrow) PinAuthExecutorCallbackHdi(
120         callbackObj, shared_from_this(), executorParam, GET_DATA_MODE_COLLECTOR_PIN_AUTH));
121     if (callback == nullptr) {
122         IAM_LOGE("get collector callback null");
123         return UserAuth::ResultCode::GENERAL_ERROR;
124     }
125     int32_t status = collectorProxy_->Collect(scheduleId, param.extraInfo, callback);
126     UserAuth::ResultCode result = ConvertHdiResultCode(status);
127     if (result != UserAuth::ResultCode::SUCCESS) {
128         IAM_LOGE("Authenticate fail:%{public}d", result);
129         return result;
130     }
131     return UserAuth::ResultCode::SUCCESS;
132 }
133 
OnSetData(uint64_t scheduleId,uint64_t authSubType,const std::vector<uint8_t> & data,int32_t errorCode)134 UserAuth::ResultCode PinAuthCollectorHdi::OnSetData(uint64_t scheduleId, uint64_t authSubType,
135     const std::vector<uint8_t> &data, int32_t errorCode)
136 {
137     if (collectorProxy_ == nullptr) {
138         IAM_LOGE("collectorProxy is null");
139         return UserAuth::ResultCode::GENERAL_ERROR;
140     }
141     int32_t status = collectorProxy_->SetData(scheduleId, authSubType, data, errorCode);
142     UserAuth::ResultCode result = ConvertHdiResultCode(status);
143     if (result != UserAuth::ResultCode::SUCCESS) {
144         IAM_LOGE("OnSetData fail ret:%{public}d", result);
145         return result;
146     }
147     return UserAuth::ResultCode::SUCCESS;
148 }
149 } // namespace PinAuth
150 } // namespace UserIam
151 } // namespace OHOS