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 "user_idm_client_impl.h"
17
18 #include "system_ability_definition.h"
19
20 #include "callback_manager.h"
21 #include "load_mode_client_util.h"
22 #include "iam_logger.h"
23 #include "ipc_client_utils.h"
24 #include "user_idm_callback_service.h"
25
26 #define LOG_TAG "USER_IDM_SDK"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
OpenSession(int32_t userId)31 std::vector<uint8_t> UserIdmClientImpl::OpenSession(int32_t userId)
32 {
33 IAM_LOGI("start, userId:%{public}d", userId);
34 auto proxy = GetProxy();
35 if (!proxy) {
36 IAM_LOGE("proxy is nullptr");
37 return {};
38 }
39
40 std::vector<uint8_t> challenge;
41 auto success = proxy->OpenSession(userId, challenge);
42 if (success != SUCCESS) {
43 IAM_LOGE("OpenSession ret = %{public}d", success);
44 }
45
46 return challenge;
47 }
48
CloseSession(int32_t userId)49 void UserIdmClientImpl::CloseSession(int32_t userId)
50 {
51 IAM_LOGI("start, userId:%{public}d", userId);
52 auto proxy = GetProxy();
53 if (!proxy) {
54 IAM_LOGE("proxy is nullptr");
55 return;
56 }
57
58 proxy->CloseSession(userId);
59 }
60
AddCredential(int32_t userId,const CredentialParameters & para,const std::shared_ptr<UserIdmClientCallback> & callback)61 void UserIdmClientImpl::AddCredential(int32_t userId, const CredentialParameters ¶,
62 const std::shared_ptr<UserIdmClientCallback> &callback)
63 {
64 IAM_LOGI("start, userId:%{public}d, authType:%{public}d, authSubType:%{public}d",
65 userId, para.authType, para.pinType.value_or(PIN_SIX));
66 if (!callback) {
67 IAM_LOGE("user idm client callback is nullptr");
68 return;
69 }
70 auto proxy = GetProxy();
71 if (!proxy) {
72 IAM_LOGE("proxy is nullptr");
73 Attributes extraInfo;
74 callback->OnResult(GENERAL_ERROR, extraInfo);
75 return;
76 }
77
78 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
79 if (wrapper == nullptr) {
80 IAM_LOGE("failed to create wrapper");
81 Attributes extraInfo;
82 callback->OnResult(GENERAL_ERROR, extraInfo);
83 return;
84 }
85 UserIdmInterface::CredentialPara credPara = {};
86 credPara.authType = para.authType;
87 credPara.pinType = para.pinType.value_or(PIN_SIX);
88 credPara.token = std::move(para.token);
89 proxy->AddCredential(userId, credPara, wrapper, false);
90 }
91
UpdateCredential(int32_t userId,const CredentialParameters & para,const std::shared_ptr<UserIdmClientCallback> & callback)92 void UserIdmClientImpl::UpdateCredential(int32_t userId, const CredentialParameters ¶,
93 const std::shared_ptr<UserIdmClientCallback> &callback)
94 {
95 IAM_LOGI("start, userId:%{public}d, authType:%{public}d, authSubType:%{public}d",
96 userId, para.authType, para.pinType.value_or(PIN_SIX));
97 if (!callback) {
98 IAM_LOGE("user idm client callback is nullptr");
99 return;
100 }
101 auto proxy = GetProxy();
102 if (!proxy) {
103 Attributes extraInfo;
104 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
105 MANAGE_USER_IDM_PERMISSION
106 }));
107 callback->OnResult(result, extraInfo);
108 return;
109 }
110
111 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
112 if (wrapper == nullptr) {
113 IAM_LOGE("failed to create wrapper");
114 Attributes extraInfo;
115 callback->OnResult(GENERAL_ERROR, extraInfo);
116 return;
117 }
118 UserIdmInterface::CredentialPara credPara = {};
119 credPara.authType = para.authType;
120 credPara.pinType = para.pinType.value_or(PIN_SIX);
121 credPara.token = std::move(para.token);
122 proxy->UpdateCredential(userId, credPara, wrapper);
123 }
124
Cancel(int32_t userId)125 int32_t UserIdmClientImpl::Cancel(int32_t userId)
126 {
127 IAM_LOGI("start, userId:%{public}d", userId);
128 auto proxy = GetProxy();
129 if (!proxy) {
130 IAM_LOGE("proxy is nullptr");
131 return GENERAL_ERROR;
132 }
133
134 return proxy->Cancel(userId);
135 }
136
DeleteCredential(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,const std::shared_ptr<UserIdmClientCallback> & callback)137 void UserIdmClientImpl::DeleteCredential(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
138 const std::shared_ptr<UserIdmClientCallback> &callback)
139 {
140 IAM_LOGI("start, userId:%{public}d", userId);
141 if (!callback) {
142 IAM_LOGE("user idm client callback is nullptr");
143 return;
144 }
145 auto proxy = GetProxy();
146 if (!proxy) {
147 IAM_LOGE("proxy is nullptr");
148 Attributes extraInfo;
149 callback->OnResult(GENERAL_ERROR, extraInfo);
150 return;
151 }
152
153 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
154 if (wrapper == nullptr) {
155 IAM_LOGE("failed to create wrapper");
156 Attributes extraInfo;
157 callback->OnResult(GENERAL_ERROR, extraInfo);
158 return;
159 }
160 proxy->DelCredential(userId, credentialId, authToken, wrapper);
161 }
162
DeleteUser(int32_t userId,const std::vector<uint8_t> & authToken,const std::shared_ptr<UserIdmClientCallback> & callback)163 void UserIdmClientImpl::DeleteUser(int32_t userId, const std::vector<uint8_t> &authToken,
164 const std::shared_ptr<UserIdmClientCallback> &callback)
165 {
166 IAM_LOGI("start, userId:%{public}d", userId);
167 if (!callback) {
168 IAM_LOGE("user idm client callback is nullptr");
169 return;
170 }
171 auto proxy = GetProxy();
172 if (!proxy) {
173 IAM_LOGE("proxy is nullptr");
174 Attributes extraInfo;
175 callback->OnResult(GENERAL_ERROR, extraInfo);
176 return;
177 }
178
179 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
180 if (wrapper == nullptr) {
181 IAM_LOGE("failed to create wrapper");
182 Attributes extraInfo;
183 callback->OnResult(GENERAL_ERROR, extraInfo);
184 return;
185 }
186 proxy->DelUser(userId, authToken, wrapper);
187 }
188
EraseUser(int32_t userId,const std::shared_ptr<UserIdmClientCallback> & callback)189 int32_t UserIdmClientImpl::EraseUser(int32_t userId, const std::shared_ptr<UserIdmClientCallback> &callback)
190 {
191 IAM_LOGI("start, userId:%{public}d", userId);
192 if (!callback) {
193 IAM_LOGE("user idm client callback is nullptr");
194 return GENERAL_ERROR;
195 }
196 auto proxy = GetProxy();
197 if (!proxy) {
198 IAM_LOGE("proxy is nullptr");
199 Attributes extraInfo;
200 callback->OnResult(GENERAL_ERROR, extraInfo);
201 return GENERAL_ERROR;
202 }
203 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
204 if (wrapper == nullptr) {
205 IAM_LOGE("failed to create wrapper");
206 Attributes extraInfo;
207 callback->OnResult(GENERAL_ERROR, extraInfo);
208 return GENERAL_ERROR;
209 }
210 return proxy->EnforceDelUser(userId, wrapper);
211 }
212
GetCredentialInfo(int32_t userId,AuthType authType,const std::shared_ptr<GetCredentialInfoCallback> & callback)213 int32_t UserIdmClientImpl::GetCredentialInfo(int32_t userId, AuthType authType,
214 const std::shared_ptr<GetCredentialInfoCallback> &callback)
215 {
216 IAM_LOGI("start, userId:%{public}d authType:%{public}d", userId, authType);
217 if (!callback) {
218 IAM_LOGE("get credential info callback is nullptr");
219 return GENERAL_ERROR;
220 }
221
222 auto proxy = GetProxy();
223 if (!proxy) {
224 std::vector<CredentialInfo> infoList;
225 callback->OnCredentialInfo(GENERAL_ERROR, infoList);
226 return GENERAL_ERROR;
227 }
228
229 sptr<IdmGetCredInfoCallbackInterface> wrapper(new (std::nothrow) IdmGetCredInfoCallbackService(callback));
230 if (wrapper == nullptr) {
231 IAM_LOGE("failed to create wrapper");
232 std::vector<CredentialInfo> infoList;
233 callback->OnCredentialInfo(GENERAL_ERROR, infoList);
234 return GENERAL_ERROR;
235 }
236 return proxy->GetCredentialInfo(userId, authType, wrapper);
237 }
238
GetSecUserInfo(int32_t userId,const std::shared_ptr<GetSecUserInfoCallback> & callback)239 int32_t UserIdmClientImpl::GetSecUserInfo(int32_t userId, const std::shared_ptr<GetSecUserInfoCallback> &callback)
240 {
241 IAM_LOGI("start, userId:%{public}d", userId);
242 if (!callback) {
243 IAM_LOGE("get secure info callback is nullptr");
244 return GENERAL_ERROR;
245 }
246
247 auto proxy = GetProxy();
248 if (!proxy) {
249 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
250 USE_USER_IDM_PERMISSION
251 }));
252 SecUserInfo info = {};
253 callback->OnSecUserInfo(result, info);
254 return result;
255 }
256
257 sptr<IdmGetSecureUserInfoCallbackInterface> wrapper(
258 new (std::nothrow) IdmGetSecureUserInfoCallbackService(callback));
259 if (wrapper == nullptr) {
260 IAM_LOGE("failed to create wrapper");
261 SecUserInfo info = {};
262 callback->OnSecUserInfo(GENERAL_ERROR, info);
263 return GENERAL_ERROR;
264 }
265 return proxy->GetSecInfo(userId, wrapper);
266 }
267
GetProxy()268 sptr<UserIdmInterface> UserIdmClientImpl::GetProxy()
269 {
270 std::lock_guard<std::mutex> lock(mutex_);
271 if (proxy_ != nullptr) {
272 return proxy_;
273 }
274 sptr<IRemoteObject> obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERIDM);
275 if (obj == nullptr) {
276 IAM_LOGE("remote object is null");
277 return proxy_;
278 }
279 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) UserIdmImplDeathRecipient());
280 if ((dr == nullptr) || (obj->IsProxyObject() && !obj->AddDeathRecipient(dr))) {
281 IAM_LOGE("add death recipient fail");
282 return proxy_;
283 }
284
285 proxy_ = iface_cast<UserIdmInterface>(obj);
286 deathRecipient_ = dr;
287 return proxy_;
288 }
289
ResetProxy(const wptr<IRemoteObject> & remote)290 void UserIdmClientImpl::ResetProxy(const wptr<IRemoteObject> &remote)
291 {
292 IAM_LOGI("start");
293 std::lock_guard<std::mutex> lock(mutex_);
294 if (proxy_ == nullptr) {
295 IAM_LOGE("proxy_ is null");
296 return;
297 }
298 auto serviceRemote = proxy_->AsObject();
299 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
300 IAM_LOGI("need reset");
301 serviceRemote->RemoveDeathRecipient(deathRecipient_);
302 proxy_ = nullptr;
303 deathRecipient_ = nullptr;
304 }
305 IAM_LOGI("end reset proxy");
306 }
307
ClearRedundancyCredential(const std::shared_ptr<UserIdmClientCallback> & callback)308 void UserIdmClientImpl::ClearRedundancyCredential(const std::shared_ptr<UserIdmClientCallback> &callback)
309 {
310 IAM_LOGI("start");
311 if (!callback) {
312 IAM_LOGE("ClearRedundancyCredential callback is nullptr");
313 return;
314 }
315
316 auto proxy = GetProxy();
317 if (!proxy) {
318 IAM_LOGE("proxy is nullptr");
319 Attributes extraInfo;
320 callback->OnResult(GENERAL_ERROR, extraInfo);
321 return;
322 }
323
324 sptr<IdmCallbackInterface> wrapper(new (std::nothrow) IdmCallbackService(callback));
325 if (wrapper == nullptr) {
326 IAM_LOGE("failed to create wrapper");
327 Attributes extraInfo;
328 callback->OnResult(GENERAL_ERROR, extraInfo);
329 return;
330 }
331
332 proxy->ClearRedundancyCredential(wrapper);
333 }
334
OnRemoteDied(const wptr<IRemoteObject> & remote)335 void UserIdmClientImpl::UserIdmImplDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
336 {
337 IAM_LOGI("start");
338 if (remote == nullptr) {
339 IAM_LOGE("remote is nullptr");
340 return;
341 }
342 CallbackManager::GetInstance().OnServiceDeath();
343 UserIdmClientImpl::Instance().ResetProxy(remote);
344 }
345
Instance()346 UserIdmClientImpl &UserIdmClientImpl::Instance()
347 {
348 static UserIdmClientImpl impl;
349 return impl;
350 }
351
GetInstance()352 UserIdmClient &UserIdmClient::GetInstance()
353 {
354 return UserIdmClientImpl::Instance();
355 }
356 } // namespace UserAuth
357 } // namespace UserIam
358 } // namespace OHOS