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_verifier_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 {
PinAuthVerifierHdi(const sptr<IVerifier> & verifierProxy)27 PinAuthVerifierHdi::PinAuthVerifierHdi(const sptr<IVerifier> &verifierProxy)
28     : verifierProxy_(verifierProxy)
29 {
30 }
31 
GetExecutorInfo(UserAuth::ExecutorInfo & info)32 UserAuth::ResultCode PinAuthVerifierHdi::GetExecutorInfo(UserAuth::ExecutorInfo &info)
33 {
34     IAM_LOGI("start");
35     if (verifierProxy_ == nullptr) {
36         IAM_LOGE("verifierProxy is null");
37         return UserAuth::ResultCode::GENERAL_ERROR;
38     }
39 
40     ExecutorInfo verifierInfo = {};
41     UserAuth::ResultCode result = ConvertHdiResultCode(verifierProxy_->GetExecutorInfo(verifierInfo));
42     if (result != UserAuth::ResultCode::SUCCESS) {
43         IAM_LOGE("GetExecutorInfo of verifier fail:%{public}d", result);
44         return result;
45     }
46     if (MoveHdiExecutorInfo(verifierInfo, 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 PinAuthVerifierHdi::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 (verifierProxy_ == nullptr) {
58         IAM_LOGE("verifierProxy is null");
59         return UserAuth::ResultCode::GENERAL_ERROR;
60     }
61     int32_t status = verifierProxy_->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 PinAuthVerifierHdi::Cancel(uint64_t scheduleId)
71 {
72     IAM_LOGI("start");
73     if (verifierProxy_ == nullptr) {
74         IAM_LOGE("verifierProxy is null");
75         return UserAuth::ResultCode::GENERAL_ERROR;
76     }
77     int32_t status = verifierProxy_->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 PinAuthVerifierHdi::SendMessage(
87     uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg)
88 {
89     IAM_LOGI("start");
90     if (verifierProxy_ == nullptr) {
91         IAM_LOGE("verifierProxy is null");
92         return UserAuth::ResultCode::GENERAL_ERROR;
93     }
94     int32_t status = verifierProxy_->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 
Authenticate(uint64_t scheduleId,const UserAuth::AuthenticateParam & param,const std::shared_ptr<UserAuth::IExecuteCallback> & callbackObj)103 UserAuth::ResultCode PinAuthVerifierHdi::Authenticate(uint64_t scheduleId, const UserAuth::AuthenticateParam &param,
104     const std::shared_ptr<UserAuth::IExecuteCallback> &callbackObj)
105 {
106     IAM_LOGI("start");
107     if (verifierProxy_ == nullptr) {
108         IAM_LOGE("verifierProxy is null");
109         return UserAuth::ResultCode::GENERAL_ERROR;
110     }
111     if (callbackObj == nullptr || param.templateIdList.empty()) {
112         IAM_LOGE("get bad param is null");
113         return UserAuth::ResultCode::GENERAL_ERROR;
114     }
115     UserAuth::ExecutorParam executorParam = {
116         .tokenId = param.tokenId,
117         .authIntent = param.authIntent,
118         .scheduleId = scheduleId,
119         .userId = param.userId,
120     };
121     auto callback = sptr<IExecutorCallback>(
122         new (std::nothrow) PinAuthExecutorCallbackHdi(callbackObj, executorParam, GET_DATA_MODE_NONE));
123     if (callback == nullptr) {
124         IAM_LOGE("get verifier callback null");
125         return UserAuth::ResultCode::GENERAL_ERROR;
126     }
127     int32_t status = verifierProxy_->Authenticate(scheduleId, param.templateIdList, param.extraInfo, callback);
128     UserAuth::ResultCode result = ConvertHdiResultCode(status);
129     if (result != UserAuth::ResultCode::SUCCESS) {
130         IAM_LOGE("Authenticate fail:%{public}d", result);
131         return result;
132     }
133     return UserAuth::ResultCode::SUCCESS;
134 }
135 
NotifyCollectorReady(uint64_t scheduleId)136 UserAuth::ResultCode PinAuthVerifierHdi::NotifyCollectorReady(uint64_t scheduleId)
137 {
138     IAM_LOGI("start");
139     if (verifierProxy_ == nullptr) {
140         IAM_LOGE("verifierProxy is null");
141         return UserAuth::ResultCode::GENERAL_ERROR;
142     }
143     int32_t status = verifierProxy_->NotifyCollectorReady(scheduleId);
144     UserAuth::ResultCode result = ConvertHdiResultCode(status);
145     if (result != UserAuth::ResultCode::SUCCESS) {
146         IAM_LOGE("NotifyCollectorReady fail:%{public}d", result);
147         return result;
148     }
149     return UserAuth::ResultCode::SUCCESS;
150 }
151 } // namespace PinAuth
152 } // namespace UserIam
153 } // namespace OHOS