• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "authentication_impl.h"
16 
17 #include "hdi_wrapper.h"
18 #include "iam_logger.h"
19 #include "iam_hitrace_helper.h"
20 #include "resource_node_utils.h"
21 #include "schedule_node_helper.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
AuthenticationImpl(uint64_t contextId,int32_t userId,AuthType authType,AuthTrustLevel atl)27 AuthenticationImpl::AuthenticationImpl(uint64_t contextId, int32_t userId, AuthType authType, AuthTrustLevel atl)
28     : contextId_(contextId),
29       userId_(userId),
30       authType_(authType),
31       atl_(atl)
32 {
33 }
34 
~AuthenticationImpl()35 AuthenticationImpl::~AuthenticationImpl()
36 {
37     Cancel();
38 }
39 
SetLatestError(int32_t error)40 void AuthenticationImpl::SetLatestError(int32_t error)
41 {
42     if (error != ResultCode::SUCCESS) {
43         latestError_ = error;
44     }
45 }
46 
GetLatestError() const47 int32_t AuthenticationImpl::GetLatestError() const
48 {
49     return latestError_;
50 }
51 
SetExecutor(uint32_t executorIndex)52 void AuthenticationImpl::SetExecutor(uint32_t executorIndex)
53 {
54     executorIndex_ = executorIndex;
55 }
56 
SetChallenge(const std::vector<uint8_t> & challenge)57 void AuthenticationImpl::SetChallenge(const std::vector<uint8_t> &challenge)
58 {
59     challenge_ = challenge;
60 }
61 
SetAccessTokenId(uint32_t tokenId)62 void AuthenticationImpl::SetAccessTokenId(uint32_t tokenId)
63 {
64     tokenId_ = tokenId;
65 }
66 
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)67 bool AuthenticationImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
68     std::shared_ptr<ScheduleNodeCallback> callback)
69 {
70     using HdiAuthSolution = OHOS::HDI::UserAuth::V1_0::AuthSolution;
71     using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
72     using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
73     auto hdi = HdiWrapper::GetHdiInstance();
74     if (!hdi) {
75         IAM_LOGE("bad hdi");
76         return false;
77     }
78     HdiAuthSolution solution = {
79         .userId = userId_,
80         .authTrustLevel = atl_,
81         .authType = static_cast<HdiAuthType>(authType_),
82         .executorSensorHint = executorSensorHint,
83         .challenge = challenge_,
84     };
85     std::vector<HdiScheduleInfo> infos;
86     IamHitraceHelper traceHelper("hdi BeginAuthentication");
87     auto result = hdi->BeginAuthentication(contextId_, solution, infos);
88     if (result != HDF_SUCCESS) {
89         IAM_LOGE("hdi BeginAuthentication failed, err is %{public}d", result);
90         SetLatestError(result);
91         return false;
92     }
93     if (infos.empty()) {
94         IAM_LOGE("hdi BeginAuthentication failed, infos is empty");
95         return false;
96     }
97 
98     ScheduleNodeHelper::NodeOptionalPara para;
99     para.tokenId = tokenId_;
100 
101     if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
102         IAM_LOGE("BuildFromHdi failed");
103         return false;
104     }
105 
106     running_ = true;
107     return true;
108 }
109 
Update(const std::vector<uint8_t> & scheduleResult,AuthResultInfo & resultInfo)110 bool AuthenticationImpl::Update(const std::vector<uint8_t> &scheduleResult, AuthResultInfo &resultInfo)
111 {
112     auto hdi = HdiWrapper::GetHdiInstance();
113     if (!hdi) {
114         IAM_LOGE("bad hdi");
115         return false;
116     }
117 
118     using HdiAuthResultInfo = OHOS::HDI::UserAuth::V1_0::AuthResultInfo;
119     HdiAuthResultInfo info;
120     auto result = hdi->UpdateAuthenticationResult(contextId_, scheduleResult, info);
121     if (result != HDF_SUCCESS) {
122         IAM_LOGE("hdi UpdateAuthenticationResult failed, err is %{public}d", result);
123         SetLatestError(result);
124     }
125 
126     for (auto &[executorIndex, commandId, msg] : info.msgs) {
127         ResourceNodeUtils::SendMsgToExecutor(executorIndex, msg);
128     }
129 
130     resultInfo.result = static_cast<decltype(resultInfo.result)>(info.result);
131     resultInfo.freezingTime = info.lockoutDuration;
132     resultInfo.remainTimes = info.remainAttempts;
133     resultInfo.token = info.token;
134     resultInfo.rootSecret = info.rootSecret;
135 
136     if (resultInfo.result != SUCCESS) {
137         SetLatestError(resultInfo.result);
138     }
139 
140     return result == HDF_SUCCESS && resultInfo.result == SUCCESS;
141 }
142 
Cancel()143 bool AuthenticationImpl::Cancel()
144 {
145     if (!running_) {
146         return false;
147     }
148     running_ = false;
149 
150     auto hdi = HdiWrapper::GetHdiInstance();
151     if (!hdi) {
152         IAM_LOGE("bad hdi");
153         return false;
154     }
155 
156     auto result = hdi->CancelAuthentication(contextId_);
157     if (result != HDF_SUCCESS) {
158         IAM_LOGE("hdi CancelAuthentication failed, err is %{public}d", result);
159         SetLatestError(result);
160         return false;
161     }
162     return true;
163 }
164 } // namespace UserAuth
165 } // namespace UserIam
166 } // namespace OHOS