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 "enrollment_impl.h"
16
17 #include "hdi_wrapper.h"
18 #include "iam_logger.h"
19 #include "iam_ptr.h"
20 #include "iam_hitrace_helper.h"
21
22 #include "credential_info_impl.h"
23 #include "schedule_node_helper.h"
24
25 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
EnrollmentImpl(int32_t userId,AuthType authType)30 EnrollmentImpl::EnrollmentImpl(int32_t userId, AuthType authType) : userId_(userId), authType_(authType)
31 {
32 }
33
~EnrollmentImpl()34 EnrollmentImpl::~EnrollmentImpl()
35 {
36 Cancel();
37 }
38
SetLatestError(int32_t error)39 void EnrollmentImpl::SetLatestError(int32_t error)
40 {
41 if (error != ResultCode::SUCCESS) {
42 latestError_ = error;
43 }
44 }
45
GetLatestError() const46 int32_t EnrollmentImpl::GetLatestError() const
47 {
48 return latestError_;
49 }
50
SetExecutorSensorHint(uint32_t executorSensorHint)51 void EnrollmentImpl::SetExecutorSensorHint(uint32_t executorSensorHint)
52 {
53 executorSensorHint_ = executorSensorHint;
54 }
55
SetAuthToken(const std::vector<uint8_t> & authToken)56 void EnrollmentImpl::SetAuthToken(const std::vector<uint8_t> &authToken)
57 {
58 authToken_ = authToken;
59 }
60
SetAccessTokenId(uint32_t tokenId)61 void EnrollmentImpl::SetAccessTokenId(uint32_t tokenId)
62 {
63 tokenId_ = tokenId;
64 }
65
SetPinSubType(PinSubType pinSubType)66 void EnrollmentImpl::SetPinSubType(PinSubType pinSubType)
67 {
68 pinSubType_ = pinSubType;
69 }
70
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)71 bool EnrollmentImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
72 std::shared_ptr<ScheduleNodeCallback> callback)
73 {
74 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
75 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
76 using EnrollParam = OHOS::HDI::UserAuth::V1_0::EnrollParam;
77 auto hdi = HdiWrapper::GetHdiInstance();
78 if (!hdi) {
79 IAM_LOGE("bad hdi");
80 return false;
81 }
82
83 HdiScheduleInfo info = {};
84 EnrollParam param = {
85 .authType = static_cast<HdiAuthType>(authType_),
86 .executorSensorHint = executorSensorHint_,
87 };
88 IamHitraceHelper traceHelper("hdi BeginEnrollment");
89 auto result = hdi->BeginEnrollment(userId_, authToken_, param, info);
90 if (result != HDF_SUCCESS) {
91 IAM_LOGE("hdi BeginEnrollment failed, err is %{public}d", result);
92 SetLatestError(result);
93 return false;
94 }
95
96 std::vector<HdiScheduleInfo> infos = {};
97 infos.emplace_back(info);
98
99 ScheduleNodeHelper::NodeOptionalPara para;
100 para.tokenId = tokenId_;
101
102 if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
103 IAM_LOGE("BuildFromHdi failed");
104 return false;
105 }
106
107 running_ = true;
108 return true;
109 }
110
Update(const std::vector<uint8_t> & scheduleResult,uint64_t & credentialId,std::shared_ptr<CredentialInfo> & info,std::vector<uint8_t> & rootSecret)111 bool EnrollmentImpl::Update(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
112 std::shared_ptr<CredentialInfo> &info, std::vector<uint8_t> &rootSecret)
113 {
114 using HdiEnrollResultInfo = OHOS::HDI::UserAuth::V1_0::EnrollResultInfo;
115
116 auto hdi = HdiWrapper::GetHdiInstance();
117 if (!hdi) {
118 IAM_LOGE("bad hdi");
119 return false;
120 }
121
122 HdiEnrollResultInfo resultInfo = {};
123 auto result = hdi->UpdateEnrollmentResult(userId_, scheduleResult, resultInfo);
124 if (result != HDF_SUCCESS) {
125 IAM_LOGE("hdi UpdateEnrollmentResult failed, err is %{public}d, userId is %{public}d", result, userId_);
126 SetLatestError(result);
127 return false;
128 }
129 IAM_LOGI("hdi UpdateEnrollmentResult success, userId is %{public}d", userId_);
130 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId_, resultInfo.oldInfo);
131 if (infoRet == nullptr) {
132 IAM_LOGE("bad alloc");
133 return false;
134 }
135 credentialId = resultInfo.credentialId;
136 info = infoRet;
137 rootSecret = resultInfo.rootSecret;
138
139 return true;
140 }
141
Cancel()142 bool EnrollmentImpl::Cancel()
143 {
144 if (!running_) {
145 return false;
146 }
147 running_ = false;
148
149 auto hdi = HdiWrapper::GetHdiInstance();
150 if (!hdi) {
151 IAM_LOGE("bad hdi");
152 return false;
153 }
154
155 auto result = hdi->CancelEnrollment(userId_);
156 if (result != HDF_SUCCESS) {
157 IAM_LOGE("hdi CancelEnrollment failed, err is %{public}d", result);
158 SetLatestError(result);
159 return false;
160 }
161 return true;
162 }
163 } // namespace UserAuth
164 } // namespace UserIam
165 } // namespace OHOS