1 /*
2 * Copyright (c) 2022 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 #include "identification_impl.h"
16
17 #include "hdi_wrapper.h"
18 #include "iam_logger.h"
19 #include "iam_hitrace_helper.h"
20 #include "schedule_node_helper.h"
21
22 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
IdentificationImpl(uint64_t contextId,AuthType authType)26 IdentificationImpl::IdentificationImpl(uint64_t contextId, AuthType authType)
27 : contextId_(contextId),
28 authType_(authType)
29 {
30 }
31
~IdentificationImpl()32 IdentificationImpl::~IdentificationImpl()
33 {
34 Cancel();
35 }
36
SetLatestError(int32_t error)37 void IdentificationImpl::SetLatestError(int32_t error)
38 {
39 if (error != ResultCode::SUCCESS) {
40 latestError_ = error;
41 }
42 }
43
GetLatestError() const44 int32_t IdentificationImpl::GetLatestError() const
45 {
46 return latestError_;
47 }
48
SetExecutor(uint32_t executorIndex)49 void IdentificationImpl::SetExecutor(uint32_t executorIndex)
50 {
51 executorIndex_ = executorIndex;
52 }
53
SetChallenge(const std::vector<uint8_t> & challenge)54 void IdentificationImpl::SetChallenge(const std::vector<uint8_t> &challenge)
55 {
56 challenge_ = challenge;
57 }
58
SetAccessTokenId(uint32_t tokenId)59 void IdentificationImpl::SetAccessTokenId(uint32_t tokenId)
60 {
61 tokenId_ = tokenId;
62 }
63
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)64 bool IdentificationImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
65 std::shared_ptr<ScheduleNodeCallback> callback)
66 {
67 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
68 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
69 auto hdi = HdiWrapper::GetHdiInstance();
70 if (!hdi) {
71 IAM_LOGE("bad hdi");
72 return false;
73 }
74
75 HdiScheduleInfo info;
76 IamHitraceHelper traceHelper("hdi BeginIdentification");
77 auto result =
78 hdi->BeginIdentification(contextId_, static_cast<HdiAuthType>(authType_), challenge_, executorIndex_, info);
79 if (result != HDF_SUCCESS) {
80 IAM_LOGE("hdi BeginAuthentication failed, err is %{public}d", result);
81 SetLatestError(result);
82 return false;
83 }
84
85 std::vector<HdiScheduleInfo> infos = {};
86 infos.emplace_back(info);
87
88 ScheduleNodeHelper::NodeOptionalPara para;
89 para.tokenId = tokenId_;
90
91 if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
92 IAM_LOGE("BuildFromHdi failed");
93 return false;
94 }
95
96 running_ = true;
97 return true;
98 }
99
Update(const std::vector<uint8_t> & scheduleResult,IdentifyResultInfo & resultInfo)100 bool IdentificationImpl::Update(const std::vector<uint8_t> &scheduleResult, IdentifyResultInfo &resultInfo)
101 {
102 auto hdi = HdiWrapper::GetHdiInstance();
103 if (!hdi) {
104 IAM_LOGE("bad hdi");
105 return false;
106 }
107
108 using HdiIdentifyResultInfo = OHOS::HDI::UserAuth::V1_0::IdentifyResultInfo;
109 HdiIdentifyResultInfo info;
110 auto result = hdi->UpdateIdentificationResult(contextId_, scheduleResult, info);
111 if (result != HDF_SUCCESS) {
112 IAM_LOGE("hdi UpdateAuthenticationResult failed, err is %{public}d", result);
113 SetLatestError(result);
114 return false;
115 }
116
117 resultInfo.result = info.result;
118 resultInfo.userId = info.userId;
119 resultInfo.token = info.token;
120
121 return true;
122 }
123
Cancel()124 bool IdentificationImpl::Cancel()
125 {
126 if (!running_) {
127 return false;
128 }
129 running_ = false;
130
131 auto hdi = HdiWrapper::GetHdiInstance();
132 if (!hdi) {
133 IAM_LOGE("bad hdi");
134 return false;
135 }
136
137 auto result = hdi->CancelIdentification(contextId_);
138 if (result != HDF_SUCCESS) {
139 IAM_LOGE("hdi CancelAuthentication failed, err is %{public}d", result);
140 SetLatestError(result);
141 return false;
142 }
143 return true;
144 }
145 } // namespace UserAuth
146 } // namespace UserIam
147 } // namespace OHOS