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