1 /*
2  * Copyright (c) 2022-2023 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_hitrace_helper.h"
19 #include "iam_logger.h"
20 #include "schedule_node_helper.h"
21 
22 #define LOG_TAG "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 
GetAccessTokenId() const64 uint32_t IdentificationImpl::GetAccessTokenId() const
65 {
66     return tokenId_;
67 }
68 
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)69 bool IdentificationImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
70     std::shared_ptr<ScheduleNodeCallback> callback)
71 {
72     auto hdi = HdiWrapper::GetHdiInstance();
73     if (!hdi) {
74         IAM_LOGE("bad hdi");
75         return false;
76     }
77 
78     HdiScheduleInfo info;
79     IamHitraceHelper traceHelper("hdi BeginIdentification");
80     auto result =
81         hdi->BeginIdentification(contextId_, static_cast<HdiAuthType>(authType_), challenge_, executorIndex_, info);
82     if (result != HDF_SUCCESS) {
83         IAM_LOGE("hdi BeginAuthentication failed, err is %{public}d", result);
84         SetLatestError(result);
85         return false;
86     }
87 
88     std::vector<HdiScheduleInfo> infos = {};
89     infos.emplace_back(info);
90 
91     ScheduleNodeHelper::NodeOptionalPara para;
92     para.tokenId = tokenId_;
93 
94     if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
95         IAM_LOGE("BuildFromHdi failed");
96         return false;
97     }
98 
99     running_ = true;
100     return true;
101 }
102 
Update(const std::vector<uint8_t> & scheduleResult,IdentifyResultInfo & resultInfo)103 bool IdentificationImpl::Update(const std::vector<uint8_t> &scheduleResult, IdentifyResultInfo &resultInfo)
104 {
105     auto hdi = HdiWrapper::GetHdiInstance();
106     if (!hdi) {
107         IAM_LOGE("bad hdi");
108         return false;
109     }
110 
111     HdiIdentifyResultInfo info;
112     auto result = hdi->UpdateIdentificationResult(contextId_, scheduleResult, info);
113     if (result != HDF_SUCCESS) {
114         IAM_LOGE("hdi UpdateAuthenticationResult failed, err is %{public}d", result);
115         SetLatestError(result);
116         return false;
117     }
118 
119     resultInfo.result = info.result;
120     resultInfo.userId = info.userId;
121     resultInfo.token = info.token;
122 
123     return true;
124 }
125 
Cancel()126 bool IdentificationImpl::Cancel()
127 {
128     if (!running_) {
129         return false;
130     }
131     running_ = false;
132 
133     auto hdi = HdiWrapper::GetHdiInstance();
134     if (!hdi) {
135         IAM_LOGE("bad hdi");
136         return false;
137     }
138 
139     auto result = hdi->CancelIdentification(contextId_);
140     if (result != HDF_SUCCESS) {
141         IAM_LOGE("hdi CancelAuthentication failed, err is %{public}d", result);
142         SetLatestError(result);
143         return false;
144     }
145     return true;
146 }
147 } // namespace UserAuth
148 } // namespace UserIam
149 } // namespace OHOS