• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "enrollment_impl.h"
16 
17 #include "hdi_wrapper.h"
18 #include "iam_hitrace_helper.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21 #include "ipc_common.h"
22 #include "publish_event_adapter.h"
23 #include "credential_info_impl.h"
24 #include "schedule_node_helper.h"
25 #include "update_pin_param_impl.h"
26 #include "user_idm_database.h"
27 
28 #define LOG_TAG "USER_AUTH_SA"
29 
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
EnrollmentImpl(EnrollmentPara enrollPara)33 EnrollmentImpl::EnrollmentImpl(EnrollmentPara enrollPara)
34     : enrollPara_(enrollPara)
35 {
36 }
37 
~EnrollmentImpl()38 EnrollmentImpl::~EnrollmentImpl()
39 {
40     Cancel();
41 }
42 
SetLatestError(int32_t error)43 void EnrollmentImpl::SetLatestError(int32_t error)
44 {
45     if (error != ResultCode::SUCCESS) {
46         latestError_ = error;
47     }
48 }
49 
GetLatestError() const50 int32_t EnrollmentImpl::GetLatestError() const
51 {
52     return latestError_;
53 }
54 
SetExecutorSensorHint(uint32_t executorSensorHint)55 void EnrollmentImpl::SetExecutorSensorHint(uint32_t executorSensorHint)
56 {
57     executorSensorHint_ = executorSensorHint;
58 }
59 
SetAuthToken(const std::vector<uint8_t> & authToken)60 void EnrollmentImpl::SetAuthToken(const std::vector<uint8_t> &authToken)
61 {
62     authToken_ = authToken;
63 }
64 
SetAccessTokenId(uint32_t tokenId)65 void EnrollmentImpl::SetAccessTokenId(uint32_t tokenId)
66 {
67     tokenId_ = tokenId;
68 }
69 
GetAccessTokenId() const70 uint32_t EnrollmentImpl::GetAccessTokenId() const
71 {
72     return tokenId_;
73 }
74 
SetPinSubType(PinSubType pinSubType)75 void EnrollmentImpl::SetPinSubType(PinSubType pinSubType)
76 {
77     pinSubType_ = pinSubType;
78 }
79 
SetIsUpdate(bool isUpdate)80 void EnrollmentImpl::SetIsUpdate(bool isUpdate)
81 {
82     isUpdate_ = isUpdate;
83 }
84 
GetUserId() const85 int32_t EnrollmentImpl::GetUserId() const
86 {
87     return enrollPara_.userId;
88 }
89 
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)90 bool EnrollmentImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
91     std::shared_ptr<ScheduleNodeCallback> callback)
92 {
93     IAM_LOGE("UserId:%{public}d AuthType:%{public}d", enrollPara_.userId, enrollPara_.authType);
94     auto hdi = HdiWrapper::GetHdiInstance();
95     if (!hdi) {
96         IAM_LOGE("bad hdi");
97         return false;
98     }
99     // cache secUserId first in case of update
100     if (isUpdate_ && !GetSecUserId(secUserId_)) {
101         IAM_LOGE("get and cache secUserId fail");
102         return false;
103     }
104 
105     HdiScheduleInfo info = {};
106     int32_t userType;
107     if (IpcCommon::GetUserTypeByUserId(enrollPara_.userId, userType) != SUCCESS) {
108         IAM_LOGE("failed to get userType");
109         return false;
110     }
111     HdiEnrollParam param = {
112         .authType = static_cast<HdiAuthType>(enrollPara_.authType),
113         .executorSensorHint = executorSensorHint_,
114         .callerName = enrollPara_.callerName,
115         .callerType = enrollPara_.callerType,
116         .apiVersion = enrollPara_.sdkVersion,
117         .userId = enrollPara_.userId,
118         .userType = userType,
119     };
120     IamHitraceHelper traceHelper("hdi BeginEnrollment");
121     auto result = hdi->BeginEnrollment(authToken_, param, info);
122     if (result != HDF_SUCCESS) {
123         IAM_LOGE("hdi BeginEnrollment failed, err is %{public}d", result);
124         SetLatestError(result);
125         return false;
126     }
127 
128     std::vector<HdiScheduleInfo> infos = {};
129     infos.emplace_back(info);
130 
131     ScheduleNodeHelper::NodeOptionalPara para;
132     para.tokenId = tokenId_;
133     para.userId = enrollPara_.userId;
134 
135     if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
136         IAM_LOGE("BuildFromHdi failed");
137         return false;
138     }
139     if (scheduleList.size() == 0 || scheduleList[0] == nullptr) {
140         IAM_LOGE("Bad Parameter!");
141         return false;
142     }
143     scheduleId_ = scheduleList[0]->GetScheduleId();
144 
145     running_ = true;
146     return true;
147 }
148 
GetSecUserId(std::optional<uint64_t> & secUserId)149 bool EnrollmentImpl::GetSecUserId(std::optional<uint64_t> &secUserId)
150 {
151     secUserId = std::nullopt;
152     if (enrollPara_.authType != PIN) {
153         IAM_LOGI("no need return sec user id");
154         return true;
155     }
156     std::shared_ptr<SecureUserInfoInterface> userInfo = nullptr;
157     int32_t ret = UserIdmDatabase::Instance().GetSecUserInfo(enrollPara_.userId, userInfo);
158     if (ret != SUCCESS) {
159         IAM_LOGE("get secUserInfo fail, ret:%{public}d, userId:%{public}d", ret, enrollPara_.userId);
160         return false;
161     }
162     if (userInfo != nullptr) {
163         secUserId = userInfo->GetSecUserId();
164         return true;
165     }
166 
167     // do not delete users in case of updates
168     if (isUpdate_) {
169         return false;
170     }
171 
172     IAM_LOGE("current user id %{public}d get fail", enrollPara_.userId);
173     std::vector<std::shared_ptr<CredentialInfoInterface>> credInfos;
174     if (UserIdmDatabase::Instance().DeleteUserEnforce(enrollPara_.userId, credInfos) != SUCCESS) {
175         IAM_LOGE("failed to enforce delete user");
176     }
177     return false;
178 }
179 
Update(const std::vector<uint8_t> & scheduleResult,uint64_t & credentialId,std::shared_ptr<CredentialInfoInterface> & info,std::shared_ptr<UpdatePinParamInterface> & pinInfo,std::optional<uint64_t> & secUserId)180 bool EnrollmentImpl::Update(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
181     std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
182     std::optional<uint64_t> &secUserId)
183 {
184     auto hdi = HdiWrapper::GetHdiInstance();
185     if (!hdi) {
186         IAM_LOGE("bad hdi");
187         return false;
188     }
189 
190     HdiEnrollResultInfo resultInfo = {};
191     auto result = hdi->UpdateEnrollmentResult(enrollPara_.userId, scheduleResult, resultInfo);
192     if (result != HDF_SUCCESS) {
193         IAM_LOGE("hdi UpdateEnrollmentResult failed, err is %{public}d, userId is %{public}d", result,
194             enrollPara_.userId);
195         SetLatestError(result);
196         return false;
197     }
198     IAM_LOGI("hdi UpdateEnrollmentResult success, userId is %{public}d", enrollPara_.userId);
199 
200     credentialId = resultInfo.credentialId;
201     pinInfo = Common::MakeShared<UpdatePinParamImpl>(resultInfo.oldInfo.credentialId, resultInfo.oldRootSecret,
202         resultInfo.rootSecret, resultInfo.authToken);
203     if (pinInfo == nullptr) {
204         IAM_LOGE("pinInfo bad alloc");
205         return false;
206     }
207 
208     if (isUpdate_) {
209         secUserId = secUserId_;
210         info = Common::MakeShared<CredentialInfoImpl>(enrollPara_.userId, resultInfo.oldInfo);
211         if (info == nullptr) {
212             IAM_LOGE("bad alloc");
213             return false;
214         }
215     } else {
216         if (!GetSecUserId(secUserId)) {
217             IAM_LOGE("enroll get secUserId fail");
218             return false;
219         }
220         IAM_LOGI("enroll not need to delete old cred");
221         info = nullptr;
222     }
223     PublishPinEvent(resultInfo.oldInfo.credentialId);
224     PublishCredentialUpdateEvent();
225     return true;
226 }
227 
PublishPinEvent(uint64_t credentialId)228 void EnrollmentImpl::PublishPinEvent(uint64_t credentialId)
229 {
230     if (enrollPara_.authType != PIN) {
231         return;
232     }
233     IAM_LOGI("begin to publish pin event");
234     if (isUpdate_) {
235         PublishEventAdapter::GetInstance().CachePinUpdateParam(enrollPara_.userId, scheduleId_, credentialId);
236     } else {
237         PublishEventAdapter::GetInstance().PublishCreatedEvent(enrollPara_.userId, scheduleId_);
238     }
239 }
240 
Cancel()241 bool EnrollmentImpl::Cancel()
242 {
243     if (!running_) {
244         return false;
245     }
246     running_ = false;
247 
248     auto hdi = HdiWrapper::GetHdiInstance();
249     if (!hdi) {
250         IAM_LOGE("bad hdi");
251         return false;
252     }
253 
254     auto result = hdi->CancelEnrollment(enrollPara_.userId);
255     if (result != HDF_SUCCESS) {
256         IAM_LOGE("hdi CancelEnrollment failed, err is %{public}d", result);
257         SetLatestError(result);
258         return false;
259     }
260     return true;
261 }
262 
PublishCredentialUpdateEvent()263 void EnrollmentImpl::PublishCredentialUpdateEvent()
264 {
265     IAM_LOGI("begin to publish credential update event");
266     if (isUpdate_ && enrollPara_.authType == PIN) {
267         IAM_LOGI("pin update");
268         return;
269     }
270 
271     std::vector<std::shared_ptr<CredentialInfoInterface>> credentialInfos;
272     int32_t ret = UserIdmDatabase::Instance().GetCredentialInfo(enrollPara_.userId, enrollPara_.authType,
273         credentialInfos);
274     if (ret != SUCCESS) {
275         IAM_LOGE("get credential fail, ret:%{public}d, userId:%{public}d, authType:%{public}d", ret,
276             enrollPara_.userId, enrollPara_.authType);
277         return;
278     }
279 
280     PublishEventAdapter::GetInstance().PublishCredentialUpdatedEvent(enrollPara_.userId,
281         static_cast<int32_t>(enrollPara_.authType), credentialInfos.size());
282 }
283 } // namespace UserAuth
284 } // namespace UserIam
285 } // namespace OHOS