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_executor_callback_hdi.h"
17 
18 #include <cinttypes>
19 #include <hdf_base.h>
20 
21 #ifdef SENSORS_MISCDEVICE_ENABLE
22 #include "vibrator_agent.h"
23 #endif
24 
25 #include "iam_check.h"
26 #include "iam_logger.h"
27 #include "iam_para2str.h"
28 #include "iam_common_defines.h"
29 #include "i_inputer_data_impl.h"
30 #include "inputer_get_data_proxy.h"
31 #include "pin_auth_hdi.h"
32 
33 #define LOG_TAG "PIN_AUTH_SA"
34 
35 namespace OHOS {
36 namespace UserIam {
37 namespace PinAuth {
38 static const uint32_t SPECIFY_PIN_COMPLEXITY = 10002;
39 
PinAuthExecutorCallbackHdi(std::shared_ptr<UserIam::UserAuth::IExecuteCallback> frameworkCallback,std::shared_ptr<PinAuthAllInOneHdi> pinAuthAllInOneHdi,const UserAuth::ExecutorParam & param,GetDataMode mode)40 PinAuthExecutorCallbackHdi::PinAuthExecutorCallbackHdi(
41     std::shared_ptr<UserIam::UserAuth::IExecuteCallback> frameworkCallback,
42     std::shared_ptr<PinAuthAllInOneHdi> pinAuthAllInOneHdi, const UserAuth::ExecutorParam &param,
43     GetDataMode mode)
44     : frameworkCallback_(frameworkCallback), pinAuthAllInOneHdi_(pinAuthAllInOneHdi), pinAuthCollectorHdi_(nullptr),
45       tokenId_(param.tokenId), mode_(mode), scheduleId_(param.scheduleId), authIntent_(param.authIntent),
46       userId_(param.userId) {}
47 
PinAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,std::shared_ptr<PinAuthCollectorHdi> pinAuthCollectorHdi,const UserAuth::ExecutorParam & param,GetDataMode mode)48 PinAuthExecutorCallbackHdi::PinAuthExecutorCallbackHdi(
49     std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,
50     std::shared_ptr<PinAuthCollectorHdi> pinAuthCollectorHdi, const UserAuth::ExecutorParam &param,
51     GetDataMode mode)
52     : frameworkCallback_(frameworkCallback), pinAuthAllInOneHdi_(nullptr), pinAuthCollectorHdi_(pinAuthCollectorHdi),
53       tokenId_(param.tokenId), mode_(mode), scheduleId_(param.scheduleId), authIntent_(param.authIntent),
54       userId_(param.userId) {}
55 
PinAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,const UserAuth::ExecutorParam & param,GetDataMode mode)56 PinAuthExecutorCallbackHdi::PinAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,
57     const UserAuth::ExecutorParam &param, GetDataMode mode)
58     : frameworkCallback_(frameworkCallback), pinAuthAllInOneHdi_(nullptr), pinAuthCollectorHdi_(nullptr),
59       tokenId_(param.tokenId), mode_(mode), scheduleId_(param.scheduleId), authIntent_(param.authIntent),
60       userId_(param.userId) {}
61 
DoVibrator()62 void PinAuthExecutorCallbackHdi::DoVibrator()
63 {
64 #ifdef SENSORS_MISCDEVICE_ENABLE
65     IAM_LOGI("begin");
66     static const char *pinAuthEffect = "haptic.notice.fail";
67     bool pinEffectState = false;
68     int32_t ret = Sensors::IsSupportEffect(pinAuthEffect, &pinEffectState);
69     if (ret != 0) {
70         IAM_LOGE("call IsSupportEffect fail %{public}d", ret);
71         return;
72     }
73     if (!pinEffectState) {
74         IAM_LOGE("effect not support");
75         return;
76     }
77     if (!Sensors::SetUsage(USAGE_PHYSICAL_FEEDBACK)) {
78         IAM_LOGE("call SetUsage fail");
79         return;
80     }
81     ret = Sensors::StartVibrator(pinAuthEffect);
82     if (ret != 0) {
83         IAM_LOGE("call StartVibrator fail %{public}d", ret);
84         return;
85     }
86     IAM_LOGI("end");
87 #else
88     IAM_LOGE("vibrator not support");
89 #endif
90 }
91 
OnResult(int32_t code,const std::vector<uint8_t> & extraInfo)92 int32_t PinAuthExecutorCallbackHdi::OnResult(int32_t code, const std::vector<uint8_t> &extraInfo)
93 {
94     IAM_LOGI("OnResult %{public}d", code);
95 
96     UserAuth::ResultCode retCode = ConvertResultCode(code);
97     if (((mode_ == GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH) || (mode_ == GET_DATA_MODE_ALL_IN_ONE_PRIVATE_PIN_AUTH)) &&
98         (retCode == UserAuth::FAIL)) {
99         if (authIntent_ != UserAuth::SILENT_AUTH) {
100             DoVibrator();
101         }
102     }
103     IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
104 
105     frameworkCallback_->OnResult(retCode, extraInfo);
106     return HDF_SUCCESS;
107 }
108 
OnGetData(const std::vector<uint8_t> & algoParameter,uint64_t authSubType,uint32_t algoVersion,const std::vector<uint8_t> & challenge,const std::string & complexityReg)109 int32_t PinAuthExecutorCallbackHdi::OnGetData(const std::vector<uint8_t> &algoParameter, uint64_t authSubType,
110     uint32_t algoVersion, const std::vector<uint8_t> &challenge, const std::string &complexityReg)
111 {
112     IAM_LOGI("Start, userId:%{public}d, tokenId_:%{public}s, authIntent:%{public}d, complexityReg size %{public}zu",
113         userId_, GET_MASKED_STRING(tokenId_).c_str(), authIntent_, complexityReg.size());
114     sptr<InputerGetData> inputer = PinAuthManager::GetInstance().GetInputerLock(tokenId_);
115     IF_FALSE_LOGE_AND_RETURN_VAL(inputer != nullptr, HDF_FAILURE);
116     InputerGetDataParam param = {
117         .mode = mode_,
118         .authSubType = authSubType,
119         .algoVersion = algoVersion,
120         .algoParameter = algoParameter,
121         .challenge = challenge,
122         .userId = userId_,
123         .authIntent = authIntent_,
124     };
125     if (mode_ == GET_DATA_MODE_ALL_IN_ONE_PIN_ENROLL ||
126         (authIntent_ == SPECIFY_PIN_COMPLEXITY && mode_ == GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH)) {
127         param.complexityReg = complexityReg;
128     }
129     if (pinAuthAllInOneHdi_ != nullptr) {
130         sptr<IInputerDataImpl> iInputerDataImpl(new (std::nothrow) IInputerDataImpl(scheduleId_, pinAuthAllInOneHdi_));
131         IF_FALSE_LOGE_AND_RETURN_VAL(iInputerDataImpl != nullptr, HDF_FAILURE);
132         param.inputerSetData = iInputerDataImpl;
133         inputer->OnGetData(param);
134         return HDF_SUCCESS;
135     } else if (pinAuthCollectorHdi_ != nullptr) {
136         sptr<IInputerDataImpl> iInputerDataImpl(new (std::nothrow) IInputerDataImpl(scheduleId_, pinAuthCollectorHdi_));
137         IF_FALSE_LOGE_AND_RETURN_VAL(iInputerDataImpl != nullptr, HDF_FAILURE);
138         param.inputerSetData = iInputerDataImpl;
139         inputer->OnGetData(param);
140         return HDF_SUCCESS;
141     }
142     return HDF_FAILURE;
143 }
144 
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)145 int32_t PinAuthExecutorCallbackHdi::OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo)
146 {
147     IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
148     frameworkCallback_->OnAcquireInfo(tip, extraInfo);
149     return HDF_SUCCESS;
150 }
151 
OnMessage(int32_t destRole,const std::vector<uint8_t> & msg)152 int32_t PinAuthExecutorCallbackHdi::OnMessage(int32_t destRole, const std::vector<uint8_t> &msg)
153 {
154     IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
155     frameworkCallback_->OnMessage(destRole, msg);
156     return HDF_SUCCESS;
157 }
158 
ConvertResultCode(const int32_t in)159 UserAuth::ResultCode PinAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
160 {
161     UserAuth::ResultCode hdiIn = static_cast<UserAuth::ResultCode>(in);
162     if (hdiIn < UserAuth::ResultCode::SUCCESS || hdiIn > UserAuth::ResultCode::COMPLEXITY_CHECK_FAILED) {
163         IAM_LOGE("convert hdi undefined result code %{public}d to framework result code GENERAL_ERROR", hdiIn);
164         return UserAuth::ResultCode::GENERAL_ERROR;
165     }
166 
167     IAM_LOGI("covert hdi result code %{public}d to framework result code", hdiIn);
168     return hdiIn;
169 }
170 
171 } // PinAuth
172 } // UserIam
173 } // OHOS