• 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 
16 #include "useridm_controller.h"
17 #include "useridm_callback_proxy.h"
18 #include "useridm_hilog_wrapper.h"
19 #include "useridm_adapter.h"
20 
21 namespace OHOS {
22 namespace UserIAM {
23 namespace UserIDM {
UserIDMController()24 UserIDMController::UserIDMController()
25 {
26     USERIDM_HILOGD(MODULE_SERVICE, "UserIDMController constructor");
27     data_ = std::make_shared<UserIDMModule>();
28 }
29 
~UserIDMController()30 UserIDMController::~UserIDMController()
31 {
32     USERIDM_HILOGD(MODULE_SERVICE, "UserIDMController deconstructor");
33 }
34 
OpenEditSessionCtrl(int32_t userId,uint64_t & challenge)35 void UserIDMController::OpenEditSessionCtrl(int32_t userId, uint64_t &challenge)
36 {
37     USERIDM_HILOGD(MODULE_SERVICE, "OpenEditSessionCtrl start");
38 
39     uint64_t sessionId = 0;
40     uint64_t temp = 0;
41     bool res = data_->CheckScheduleIdIsActive(sessionId);
42     if (res && (data_->CheckChallenge(temp))) {
43         CoAuth::CoAuth::GetInstance().Cancel(sessionId);
44     }
45     data_->CleanData();
46 
47     UserIDMAdapter::GetInstance().OpenEditSession(userId, challenge);
48     data_->InsertChallenge(challenge);
49 }
50 
CloseEditSessionCtrl()51 void UserIDMController::CloseEditSessionCtrl()
52 {
53     USERIDM_HILOGD(MODULE_SERVICE, "CloseEditSessionCtrl start");
54 
55     uint64_t sessionId = 0;
56     uint64_t temp = 0;
57     bool res = data_->CheckScheduleIdIsActive(sessionId);
58     if (res && (data_->CheckChallenge(temp))) {
59         CoAuth::CoAuth::GetInstance().Cancel(sessionId);
60     }
61     data_->CleanData();
62 
63     UserIDMAdapter::GetInstance().CloseEditSession();
64 }
65 
GetAuthInfoCtrl(int32_t userId,AuthType authType,std::vector<CredentialInfo> & credInfos)66 int32_t UserIDMController::GetAuthInfoCtrl(int32_t userId, AuthType authType, std::vector<CredentialInfo>& credInfos)
67 {
68     USERIDM_HILOGD(MODULE_SERVICE, "GetAuthInfoCtrl start");
69     return UserIDMAdapter::GetInstance().QueryCredential(userId, authType, credInfos);
70 }
71 
GetSecureInfoCtrl(int32_t userId,uint64_t & secureUid,std::vector<EnrolledInfo> & enrolledInfos)72 int32_t UserIDMController::GetSecureInfoCtrl(int32_t userId, uint64_t& secureUid,
73     std::vector<EnrolledInfo>& enrolledInfos)
74 {
75     USERIDM_HILOGD(MODULE_SERVICE, "GetSecureInfoCtrl start");
76     return UserIDMAdapter::GetInstance().GetSecureUid(userId, secureUid, enrolledInfos);
77 }
78 
DeleteCredentialCtrl(int32_t userId,uint64_t credentialId,std::vector<uint8_t> authToken,CredentialInfo & credInfo)79 int32_t UserIDMController::DeleteCredentialCtrl(int32_t userId, uint64_t credentialId, std::vector<uint8_t> authToken,
80     CredentialInfo& credInfo)
81 {
82     USERIDM_HILOGD(MODULE_SERVICE, "DeleteCredentialCtrl start");
83     return UserIDMAdapter::GetInstance().DeleteCredential(userId, credentialId, authToken, credInfo);
84 }
85 
DeleteUserCtrl(int32_t userId,std::vector<uint8_t> authToken,std::vector<CredentialInfo> & credInfo)86 int32_t UserIDMController::DeleteUserCtrl(int32_t userId, std::vector<uint8_t> authToken,
87     std::vector<CredentialInfo>& credInfo)
88 {
89     USERIDM_HILOGD(MODULE_SERVICE, "DeleteUserCtrl start");
90     return UserIDMAdapter::GetInstance().DeleteUser(userId, authToken, credInfo);
91 }
92 
DeleteUserByForceCtrl(int32_t userId,std::vector<CredentialInfo> & credInfo)93 int32_t UserIDMController::DeleteUserByForceCtrl(int32_t userId, std::vector<CredentialInfo>& credInfo)
94 {
95     USERIDM_HILOGD(MODULE_SERVICE, "DeleteUserByForceCtrl start");
96     return UserIDMAdapter::GetInstance().DeleteUserEnforce(userId, credInfo);
97 }
98 
AddCredentialCallCoauth(uint64_t callerID,AddCredInfo & credInfo,const sptr<IIDMCallback> & innerkitsCallback,uint64_t & challenge,uint64_t & scheduleId)99 int32_t UserIDMController::AddCredentialCallCoauth(uint64_t callerID, AddCredInfo& credInfo,
100     const sptr<IIDMCallback>& innerkitsCallback, uint64_t& challenge, uint64_t& scheduleId)
101 {
102     USERIDM_HILOGD(MODULE_SERVICE, "AddCredentialCallCoauth start");
103     if (innerkitsCallback == nullptr) {
104         USERIDM_HILOGE(MODULE_SERVICE, "sorry: innerkitsCallback is nullptr");
105         return INVALID_PARAMETERS;
106     }
107 
108     std::string callerName = std::to_string(callerID);
109     data_->InsertScheduleId(scheduleId);
110     CoAuth::AuthInfo paramInfo;
111     paramInfo.SetPkgName(callerName);
112     paramInfo.SetCallerUid(callerID);
113     std::shared_ptr<UserIDMCoAuthHandler> coAuthCallback;
114 
115     USERIDM_HILOGI(MODULE_SERVICE, "credInfo.authType is %{public}d", credInfo.authType);
116     if (credInfo.authType == PIN) {
117         coAuthCallback = std::make_shared<UserIDMCoAuthHandler>(ADD_PIN_CRED, challenge, scheduleId, data_,
118             innerkitsCallback);
119     } else if (FACE == credInfo.authType) {
120         coAuthCallback = std::make_shared<UserIDMCoAuthHandler>(ADD_FACE_CRED, challenge, scheduleId, data_,
121             innerkitsCallback);
122     } else {
123         USERIDM_HILOGE(MODULE_SERVICE, "credInfo.authType error: %{public}d", credInfo.authType);
124         coAuthCallback = std::make_shared<UserIDMCoAuthHandler>(ADD_PIN_CRED, challenge, scheduleId, data_,
125             innerkitsCallback);
126     }
127 
128     if (coAuthCallback == nullptr) {
129         USERIDM_HILOGE(MODULE_SERVICE, "sorry: coAuthCallback is nullptr");
130         return FAIL;
131     } else {
132         CoAuth::CoAuth::GetInstance().BeginSchedule(scheduleId, paramInfo, coAuthCallback);
133     }
134     return SUCCESS;
135 }
136 
AddCredentialCtrl(int32_t userId,uint64_t callerID,AddCredInfo & credInfo,const sptr<IIDMCallback> & innerkitsCallback)137 int32_t UserIDMController::AddCredentialCtrl(int32_t userId, uint64_t callerID, AddCredInfo& credInfo,
138     const sptr<IIDMCallback>& innerkitsCallback)
139 {
140     USERIDM_HILOGD(MODULE_SERVICE, "AddCredentialCtrl start");
141 
142     if (innerkitsCallback == nullptr) {
143         USERIDM_HILOGE(MODULE_SERVICE, "sorry: innerkitsCallback is nullptr");
144         return INVALID_PARAMETERS;
145     }
146     uint64_t scheduleId = 0;
147     uint64_t challenge = 0;
148 
149     bool result = data_->CheckChallenge(challenge);
150     if (!result) {
151         USERIDM_HILOGE(MODULE_SERVICE, "check challenge num error: no challenge");
152         RequestResult reqRet;
153         innerkitsCallback->OnResult(FAIL, reqRet);
154         return FAIL;
155     }
156     result = data_->CheckScheduleIdIsActive(scheduleId);
157     if (result) {
158         USERIDM_HILOGE(MODULE_SERVICE, "current session in active: busy");
159         RequestResult reqRet;
160         innerkitsCallback->OnResult(BUSY, reqRet);
161         return BUSY;
162     }
163     int32_t ret = UserIDMAdapter::GetInstance().InitSchedulation(credInfo.token, userId, credInfo.authType,
164         credInfo.authSubType, scheduleId);
165     if (ret == SUCCESS) {
166         ret = AddCredentialCallCoauth(callerID, credInfo, innerkitsCallback, challenge, scheduleId);
167     } else {
168         USERIDM_HILOGE(MODULE_SERVICE, "call TA info: InitSchedulation failed");
169         RequestResult reqRet;
170         innerkitsCallback->OnResult(FAIL, reqRet);
171     }
172     return ret;
173 }
174 
UpdateCredentialCtrl(int32_t userId,uint64_t callerID,std::string callerName,AddCredInfo & credInfo,const sptr<IIDMCallback> & innerkitsCallback)175 int32_t UserIDMController::UpdateCredentialCtrl(int32_t userId, uint64_t callerID, std::string callerName,
176     AddCredInfo& credInfo, const sptr<IIDMCallback>& innerkitsCallback)
177 {
178     USERIDM_HILOGD(MODULE_SERVICE, "UpdateCredentialCtrl start");
179     if (innerkitsCallback == nullptr) {
180         USERIDM_HILOGE(MODULE_SERVICE, "sorry: innerkitsCallback is nullptr");
181         return INVALID_PARAMETERS;
182     }
183 
184     uint64_t scheduleId = 0;
185     uint64_t challenge = 0;
186     bool result = data_->CheckChallenge(challenge);
187     if (!result) {
188         USERIDM_HILOGE(MODULE_SERVICE, "check challenge num error: no challenge");
189         RequestResult reqRet;
190         innerkitsCallback->OnResult(FAIL, reqRet);
191         return FAIL;
192     }
193     result = data_->CheckScheduleIdIsActive(scheduleId);
194     if (result) {
195         USERIDM_HILOGE(MODULE_SERVICE, "current session in active: busy");
196         RequestResult reqRet;
197         innerkitsCallback->OnResult(BUSY, reqRet);
198         return BUSY;
199     }
200     int32_t ret = UserIDMAdapter::GetInstance().InitSchedulation(credInfo.token, userId, credInfo.authType,
201         credInfo.authSubType, scheduleId);
202     if (ret == SUCCESS) {
203         USERIDM_HILOGI(MODULE_SERVICE, "InitSchedulation SUCCESS");
204         data_->InsertScheduleId(scheduleId);
205         std::shared_ptr<UserIDMCoAuthHandler> coAuthCallback =
206             std::make_shared<UserIDMCoAuthHandler>(MODIFY_CRED, challenge, scheduleId, data_, innerkitsCallback);
207         sptr<IRemoteObject::DeathRecipient> dr = new CoAuthCallbackDeathRecipient(coAuthCallback);
208         if (!innerkitsCallback->AsObject()->AddDeathRecipient(dr)) {
209             USERIDM_HILOGE(MODULE_SERVICE, "Failed to add death recipient CoAuthCallbackDeathRecipient");
210         }
211         CoAuth::AuthInfo paramInfo;
212         paramInfo.SetPkgName(callerName);
213         paramInfo.SetCallerUid(callerID);
214         CoAuth::CoAuth::GetInstance().BeginSchedule(scheduleId, paramInfo, coAuthCallback);
215     } else {
216         USERIDM_HILOGE(MODULE_SERVICE, "call TA info: InitSchedulation failed");
217         RequestResult reqRet;
218         innerkitsCallback->OnResult(FAIL, reqRet);
219     }
220     return ret;
221 }
222 
DelSchedleIdCtrl(uint64_t challenge)223 int32_t UserIDMController::DelSchedleIdCtrl(uint64_t challenge)
224 {
225     USERIDM_HILOGD(MODULE_SERVICE, "DelSchedleIdCtrl start");
226     int32_t result = FAIL;
227     uint64_t sessionId = 0;
228     uint64_t lastChallenge = 0;
229     data_->CheckChallenge(lastChallenge);
230     if (lastChallenge != challenge) {
231         USERIDM_HILOGE(MODULE_SERVICE, "Not same challenge num");
232         return result;
233     }
234     if (!data_->CheckScheduleIdIsActive(sessionId)) {
235         USERIDM_HILOGE(MODULE_SERVICE, "CheckScheduleIdIsActive fail");
236         return result;
237     }
238     result = CoAuth::CoAuth::GetInstance().Cancel(sessionId);
239     if (result != SUCCESS) {
240         USERIDM_HILOGE(MODULE_SERVICE, "Cancel fail");
241         return result;
242     }
243     data_->DeleteSessionId();
244     return result;
245 }
246 
DelFaceCredentialCtrl(AuthType authType,AuthSubType authSubType,uint64_t credentialId,uint64_t templateId,const sptr<IIDMCallback> & innerCallback)247 int32_t UserIDMController::DelFaceCredentialCtrl(AuthType authType, AuthSubType authSubType, uint64_t credentialId,
248     uint64_t templateId, const sptr<IIDMCallback>& innerCallback)
249 {
250     USERIDM_HILOGD(MODULE_SERVICE, "DelFaceCredentialCtrl start authType: %{public}d", authType);
251     if (authType != FACE) {
252         USERIDM_HILOGE(MODULE_SERVICE, "authType error");
253         return FAIL;
254     }
255     std::shared_ptr<UserIDMSetPropHandler> setPropCallback =
256         std::make_shared<UserIDMSetPropHandler>(FACE, 0, 0, credentialId, data_, innerCallback);
257     sptr<IRemoteObject::DeathRecipient> dr = new SetPropCallbackDeathRecipient(setPropCallback);
258     if (!innerCallback->AsObject()->AddDeathRecipient(dr)) {
259         USERIDM_HILOGE(MODULE_SERVICE, "Failed to add death recipient SetPropCallbackDeathRecipient");
260     }
261     AuthResPool::AuthAttributes condition;
262     condition.SetUint32Value(AuthAttributeType::AUTH_PROPERTY_MODE, 0);
263     condition.SetUint64Value(AuthAttributeType::AUTH_CALLER_UID, 0);
264     condition.SetUint32Value(AuthAttributeType::AUTH_TYPE, authType);
265     condition.SetUint64Value(AuthAttributeType::AUTH_SUBTYPE, authSubType);
266     condition.SetUint64Value(AuthAttributeType::AUTH_CREDENTIAL_ID, credentialId);
267     condition.SetUint64Value(AuthAttributeType::AUTH_TEMPLATE_ID, templateId);
268     CoAuth::CoAuth::GetInstance().SetExecutorProp(condition, setPropCallback);
269     return SUCCESS;
270 }
271 
DelExecutorPinInfoCtrl(const sptr<IIDMCallback> & innerCallback,std::vector<CredentialInfo> & info)272 int32_t UserIDMController::DelExecutorPinInfoCtrl(const sptr<IIDMCallback>& innerCallback,
273     std::vector<CredentialInfo>& info)
274 {
275     USERIDM_HILOGD(MODULE_SERVICE, "DelExecutorPinInfoCtrl start: info.size(): %{public}zu", info.size());
276 
277     const size_t minDelSize = 1;
278     if (info.size() < minDelSize) {
279         USERIDM_HILOGE(MODULE_SERVICE, "info size error!: %{public}zu", info.size());
280         RequestResult reqRet;
281         innerCallback->OnResult(FAIL, reqRet);
282         return FAIL;
283     }
284 
285     for (uint32_t i = 0; i < info.size(); i++) {
286         if (info[i].authType == PIN) {
287             std::shared_ptr<UserIDMSetPropHandler> setPropCallback =
288                 std::make_shared<UserIDMSetPropHandler>(PIN, 0, 0, info[i].credentialId, data_, innerCallback);
289             AuthResPool::AuthAttributes condition;
290             condition.SetUint32Value(AuthAttributeType::AUTH_PROPERTY_MODE, 0);
291             condition.SetUint64Value(AuthAttributeType::AUTH_CALLER_UID, 0);
292             condition.SetUint32Value(AuthAttributeType::AUTH_TYPE, info[i].authType);
293             condition.SetUint64Value(AuthAttributeType::AUTH_SUBTYPE, info[i].authSubType);
294             condition.SetUint64Value(AuthAttributeType::AUTH_CREDENTIAL_ID, info[i].credentialId);
295             condition.SetUint64Value(AuthAttributeType::AUTH_TEMPLATE_ID, info[i].templateId);
296             CoAuth::CoAuth::GetInstance().SetExecutorProp(condition, setPropCallback);
297         } else if (info[i].authType == FACE) {
298             std::shared_ptr<UserIDMSetPropHandler> setPropCallback =
299                 std::make_shared<UserIDMSetPropHandler>(FACE, 0, 0, info[i].credentialId, data_, innerCallback);
300             AuthResPool::AuthAttributes condition;
301             condition.SetUint32Value(AuthAttributeType::AUTH_PROPERTY_MODE, 0);
302             condition.SetUint64Value(AuthAttributeType::AUTH_CALLER_UID, 0);
303             condition.SetUint32Value(AuthAttributeType::AUTH_TYPE, info[i].authType);
304             condition.SetUint64Value(AuthAttributeType::AUTH_SUBTYPE, info[i].authSubType);
305             condition.SetUint64Value(AuthAttributeType::AUTH_CREDENTIAL_ID, info[i].credentialId);
306             condition.SetUint64Value(AuthAttributeType::AUTH_TEMPLATE_ID, info[i].templateId);
307             CoAuth::CoAuth::GetInstance().SetExecutorProp(condition, setPropCallback);
308         }
309     }
310     return SUCCESS;
311 }
312 
CoAuthCallbackDeathRecipient(std::shared_ptr<UserIDMCoAuthHandler> callback)313 UserIDMController::CoAuthCallbackDeathRecipient::CoAuthCallbackDeathRecipient(
314     std::shared_ptr<UserIDMCoAuthHandler> callback) : callback_(callback)
315 {
316 }
317 
OnRemoteDied(const wptr<IRemoteObject> & remote)318 void UserIDMController::CoAuthCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
319 {
320     USERIDM_HILOGE(MODULE_SERVICE, "CoAuthCallbackDeathRecipient OnRemoteDied start");
321     if (remote == nullptr) {
322         USERIDM_HILOGE(MODULE_SERVICE, "CoAuthCallbackDeathRecipient OnRemoteDied failed, remote is nullptr");
323         return;
324     }
325     if (callback_ == nullptr) {
326         USERIDM_HILOGE(MODULE_SERVICE, "CoAuthCallbackDeathRecipient OnRemoteDied callback_ is nullptr");
327         return;
328     }
329     callback_->ResetCallback();
330     callback_ = nullptr;
331 }
332 
SetPropCallbackDeathRecipient(std::shared_ptr<UserIDMSetPropHandler> callback)333 UserIDMController::SetPropCallbackDeathRecipient::SetPropCallbackDeathRecipient(
334     std::shared_ptr<UserIDMSetPropHandler> callback) : callback_(callback)
335 {
336 }
337 
OnRemoteDied(const wptr<IRemoteObject> & remote)338 void UserIDMController::SetPropCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
339 {
340     USERIDM_HILOGE(MODULE_SERVICE, "SetPropCallbackDeathRecipient OnRemoteDied start");
341     if (remote == nullptr) {
342         USERIDM_HILOGE(MODULE_SERVICE, "SetPropCallbackDeathRecipient OnRemoteDied failed, remote is nullptr");
343         return;
344     }
345     if (callback_ == nullptr) {
346         USERIDM_HILOGE(MODULE_SERVICE, "SetPropCallbackDeathRecipient OnRemoteDied callback_ is nullptr");
347         return;
348     }
349     callback_->ResetCallback();
350     callback_ = nullptr;
351 }
352 }  // namespace UserIDM
353 }  // namespace UserIAM
354 }  // namespace OHOS
355