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 "verifier_impl.h" 17#include <cinttypes> 18#include <hdf_base.h> 19#include <securec.h> 20#include "executor_impl_common.h" 21#include "iam_logger.h" 22 23#undef LOG_TAG 24#define LOG_TAG "PIN_AUTH_IMPL_V" 25 26namespace OHOS { 27namespace HDI { 28namespace PinAuth { 29VerifierImpl::VerifierImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi) 30 : pinHdi_(pinHdi), 31 threadPool_("pin_verifier_async") 32{ 33 threadPool_.Start(1); 34} 35 36VerifierImpl::~VerifierImpl() 37{ 38 threadPool_.Stop(); 39} 40 41int32_t VerifierImpl::GetExecutorInfo(HdiExecutorInfo &executorInfo) 42{ 43 IAM_LOGI("start"); 44 if (pinHdi_ == nullptr) { 45 IAM_LOGE("pinHdi_ is nullptr"); 46 return HDF_FAILURE; 47 } 48 executorInfo.sensorId = SENSOR_ID; 49 executorInfo.executorMatcher = EXECUTOR_MATCHER; 50 executorInfo.executorRole = HdiExecutorRole::VERIFIER; 51 executorInfo.authType = HdiAuthType::PIN; 52 uint32_t eslRet = 0; 53 int32_t result = pinHdi_->GetExecutorInfo(HdiExecutorRole::VERIFIER, executorInfo.publicKey, eslRet, 54 executorInfo.maxTemplateAcl); 55 if (result != SUCCESS) { 56 IAM_LOGE("Get verifier ExecutorInfo failed, fail code:%{public}d", result); 57 return HDF_FAILURE; 58 } 59 executorInfo.esl = static_cast<HdiExecutorSecureLevel>(eslRet); 60 return HDF_SUCCESS; 61} 62 63int32_t VerifierImpl::OnRegisterFinish(const std::vector<uint64_t> &templateIdList, 64 const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) 65{ 66 IAM_LOGI("start"); 67 static_cast<void>(templateIdList); 68 static_cast<void>(extraInfo); 69 if (pinHdi_ == nullptr) { 70 IAM_LOGE("pinHdi_ is nullptr"); 71 return HDF_FAILURE; 72 } 73 int32_t result = pinHdi_->SetVerifierFwkParam(frameworkPublicKey); 74 if (result != SUCCESS) { 75 IAM_LOGE("Hdi SetVerifierFwkParam fail"); 76 return HDF_FAILURE; 77 } 78 return HDF_SUCCESS; 79} 80 81bool VerifierImpl::IsCurrentSchedule(uint64_t scheduleId) 82{ 83 if (!scheduleId_.has_value()) { 84 IAM_LOGE("verify schedule not exist"); 85 return false; 86 } 87 if (scheduleId_.value() != scheduleId) { 88 IAM_LOGE("verify schedule:%{public}x not match current:%{public}x", 89 (uint16_t)scheduleId, (uint16_t)scheduleId_.value()); 90 return false; 91 } 92 return true; 93} 94 95void VerifierImpl::CancelCurrentAuth(int32_t errorCode) 96{ 97 if (!scheduleId_.has_value()) { 98 return; 99 } 100 if (pinHdi_->CancelVerifierAuth() != SUCCESS) { 101 IAM_LOGE("Hdi CancelVerify fail"); 102 } 103 CallError(callback_, errorCode); 104 scheduleId_ = std::nullopt; 105 callback_ = nullptr; 106} 107 108int32_t VerifierImpl::Cancel(uint64_t scheduleId) 109{ 110 IAM_LOGI("start %{public}x", (uint16_t)scheduleId); 111 if (pinHdi_ == nullptr) { 112 IAM_LOGE("pinHdi_ is nullptr"); 113 return HDF_FAILURE; 114 } 115 threadPool_.AddTask([this, id = scheduleId]() { 116 if (IsCurrentSchedule(id)) { 117 CancelCurrentAuth(); 118 } 119 }); 120 return HDF_SUCCESS; 121} 122 123void VerifierImpl::HandleVerifierMsg(uint64_t scheduleId, const std::vector<uint8_t> &msg) 124{ 125 IAM_LOGI("start"); 126 std::vector<uint8_t> msgOut; 127 bool isAuthEnd = false; 128 int32_t compareResult = FAIL; 129 if (pinHdi_->SendMessageToVerifier(scheduleId, msg, msgOut, isAuthEnd, compareResult) != SUCCESS) { 130 IAM_LOGE("Hdi SendMessageToVerifier fail"); 131 return; 132 } 133 if (!isAuthEnd) { 134 int32_t result = callback_->OnMessage(HdiExecutorRole::COLLECTOR, msgOut); 135 if (result != SUCCESS) { 136 IAM_LOGE("Send verifier ack msg fail"); 137 CancelCurrentAuth(result); 138 return; 139 } 140 } else { 141 int32_t result = callback_->OnResult(compareResult, msgOut); 142 if (result != SUCCESS) { 143 IAM_LOGE("call OnResult fail"); 144 CancelCurrentAuth(result); 145 return; 146 } 147 } 148} 149 150int32_t VerifierImpl::SendMessage(uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg) 151{ 152 IAM_LOGI("start schedule:%{public}x src:%{public}d", (uint16_t)scheduleId, srcRole); 153 if (pinHdi_ == nullptr) { 154 IAM_LOGE("pinHdi_ is nullptr"); 155 return HDF_FAILURE; 156 } 157 threadPool_.AddTask([this, id = scheduleId, role = srcRole, msgIn = msg]() { 158 if (!IsCurrentSchedule(id)) { 159 return; 160 } 161 if (role == HdiExecutorRole::COLLECTOR) { 162 return HandleVerifierMsg(id, msgIn); 163 } 164 IAM_LOGE("message from %{public}d not handled", role); 165 }); 166 return HDF_SUCCESS; 167} 168 169int32_t VerifierImpl::Authenticate(uint64_t scheduleId, const std::vector<uint64_t> &templateIdList, 170 const std::vector<uint8_t> &extraInfo, const sptr<HdiIExecutorCallback> &callbackObj) 171{ 172 IAM_LOGI("start %{public}x", (uint16_t)scheduleId); 173 if (callbackObj == nullptr) { 174 IAM_LOGE("callbackObj is nullptr"); 175 return HDF_FAILURE; 176 } 177 if ((pinHdi_ == nullptr) || (templateIdList.size() != 1)) { 178 IAM_LOGE("pinHdi_ is nullptr or templateIdList size not 1"); 179 CallError(callbackObj, INVALID_PARAMETERS); 180 return HDF_FAILURE; 181 } 182 threadPool_.AddTask( 183 [this, id = scheduleId, templateId = templateIdList, extra = extraInfo, callback = callbackObj]() { 184 CancelCurrentAuth(); 185 std::vector<uint8_t> msg; 186 int32_t result = pinHdi_->VerifierAuth(id, templateId[0], extra, msg); 187 if (result != SUCCESS) { 188 IAM_LOGE("VerifierAuth fail"); 189 callback->OnResult(result, msg); 190 return; 191 } 192 scheduleId_ = id; 193 callback_ = callback; 194 }); 195 return HDF_SUCCESS; 196} 197 198int32_t VerifierImpl::NotifyCollectorReady(uint64_t scheduleId) 199{ 200 IAM_LOGI("start %{public}x", (uint16_t)scheduleId); 201 static_cast<void>(scheduleId); 202 return HDF_SUCCESS; 203} 204 205} // PinAuth 206} // HDI 207} // OHOS