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
16 #include "user_idm_stub.h"
17
18 #include <cinttypes>
19
20 #include "iam_logger.h"
21 #include "iam_scope_guard.h"
22 #include "iam_common_defines.h"
23 #include "user_idm_callback_proxy.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30
31 // When true is passed into IRemoteStub, sa will process request serially.
UserIdmStub()32 UserIdmStub::UserIdmStub() : IRemoteStub(true) {};
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t UserIdmStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36 IAM_LOGI("cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
37 if (data.ReadInterfaceToken() != UserIdmInterface::GetDescriptor()) {
38 IAM_LOGE("failed to match descriptor");
39 return GENERAL_ERROR;
40 }
41
42 switch (code) {
43 case UserIdmInterfaceCode::USER_IDM_OPEN_SESSION:
44 return OpenSessionStub(data, reply);
45 case UserIdmInterfaceCode::USER_IDM_CLOSE_SESSION:
46 return CloseSessionStub(data, reply);
47 case UserIdmInterfaceCode::USER_IDM_GET_CRED_INFO:
48 return GetCredentialInfoStub(data, reply);
49 case UserIdmInterfaceCode::USER_IDM_GET_SEC_INFO:
50 return GetSecInfoStub(data, reply);
51 case UserIdmInterfaceCode::USER_IDM_ADD_CREDENTIAL:
52 return AddCredentialStub(data, reply);
53 case UserIdmInterfaceCode::USER_IDM_UPDATE_CREDENTIAL:
54 return UpdateCredentialStub(data, reply);
55 case UserIdmInterfaceCode::USER_IDM_CANCEL:
56 return CancelStub(data, reply);
57 case UserIdmInterfaceCode::USER_IDM_ENFORCE_DEL_USER:
58 return EnforceDelUserStub(data, reply);
59 case UserIdmInterfaceCode::USER_IDM_DEL_USER:
60 return DelUserStub(data, reply);
61 case UserIdmInterfaceCode::USER_IDM_DEL_CRED:
62 return DelCredentialStub(data, reply);
63 case UserIdmInterfaceCode::USER_IDM_CLEAR_REDUNDANCY_CRED:
64 return ClearRedundancyCredentialStub(data, reply);
65 default:
66 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 }
69
OpenSessionStub(MessageParcel & data,MessageParcel & reply)70 int32_t UserIdmStub::OpenSessionStub(MessageParcel &data, MessageParcel &reply)
71 {
72 IAM_LOGI("enter");
73 ON_SCOPE_EXIT(IAM_LOGI("leave"));
74
75 int32_t userId;
76 if (!data.ReadInt32(userId)) {
77 IAM_LOGE("failed to read userId");
78 return READ_PARCEL_ERROR;
79 }
80
81 std::vector<uint8_t> challenge;
82 int32_t ret = OpenSession(userId, challenge);
83 if (ret != SUCCESS) {
84 return ret;
85 }
86
87 if (!reply.WriteUInt8Vector(challenge)) {
88 IAM_LOGE("failed to write challenge");
89 return WRITE_PARCEL_ERROR;
90 }
91
92 return SUCCESS;
93 }
94
CloseSessionStub(MessageParcel & data,MessageParcel & reply)95 int32_t UserIdmStub::CloseSessionStub(MessageParcel &data, MessageParcel &reply)
96 {
97 IAM_LOGI("enter");
98 ON_SCOPE_EXIT(IAM_LOGI("leave"));
99
100 int32_t userId;
101
102 if (!data.ReadInt32(userId)) {
103 IAM_LOGE("failed to read userId");
104 return READ_PARCEL_ERROR;
105 }
106
107 CloseSession(userId);
108 return SUCCESS;
109 }
110
GetCredentialInfoStub(MessageParcel & data,MessageParcel & reply)111 int32_t UserIdmStub::GetCredentialInfoStub(MessageParcel &data, MessageParcel &reply)
112 {
113 IAM_LOGI("enter");
114 ON_SCOPE_EXIT(IAM_LOGI("leave"));
115
116 int32_t userId;
117 if (!data.ReadInt32(userId)) {
118 IAM_LOGE("failed to read userId");
119 return READ_PARCEL_ERROR;
120 }
121
122 int32_t authType;
123 if (!data.ReadInt32(authType)) {
124 IAM_LOGE("failed to read authType");
125 return READ_PARCEL_ERROR;
126 }
127
128 sptr<IdmGetCredInfoCallbackInterface> callback = iface_cast<IdmGetCredentialInfoProxy>(data.ReadRemoteObject());
129 if (callback == nullptr) {
130 IAM_LOGE("callback is nullptr");
131 return ERR_INVALID_VALUE;
132 }
133
134 int32_t ret = GetCredentialInfo(userId, static_cast<AuthType>(authType), callback);
135 static_cast<void>(reply.WriteInt32(ret));
136 return ret;
137 }
138
GetSecInfoStub(MessageParcel & data,MessageParcel & reply)139 int32_t UserIdmStub::GetSecInfoStub(MessageParcel &data, MessageParcel &reply)
140 {
141 IAM_LOGI("enter");
142 ON_SCOPE_EXIT(IAM_LOGI("leave"));
143
144 int32_t userId;
145 if (!data.ReadInt32(userId)) {
146 IAM_LOGE("failed to read userId");
147 return READ_PARCEL_ERROR;
148 }
149 sptr<IdmGetSecureUserInfoCallbackInterface> callback =
150 iface_cast<IdmGetSecureUserInfoProxy>(data.ReadRemoteObject());
151 if (callback == nullptr) {
152 IAM_LOGE("callback is nullptr");
153 return ERR_INVALID_VALUE;
154 }
155
156 int32_t ret = GetSecInfo(userId, callback);
157 static_cast<void>(reply.WriteInt32(ret));
158 return ret;
159 }
160
AddCredentialStub(MessageParcel & data,MessageParcel & reply)161 int32_t UserIdmStub::AddCredentialStub(MessageParcel &data, MessageParcel &reply)
162 {
163 IAM_LOGI("enter");
164 ON_SCOPE_EXIT(IAM_LOGI("leave"));
165
166 int32_t userId;
167 if (!data.ReadInt32(userId)) {
168 IAM_LOGE("failed to read userId");
169 return READ_PARCEL_ERROR;
170 }
171
172 int32_t authType;
173 if (!data.ReadInt32(authType)) {
174 IAM_LOGE("failed to read authType");
175 return READ_PARCEL_ERROR;
176 }
177
178 int32_t authSubType;
179 if (!data.ReadInt32(authSubType)) {
180 IAM_LOGE("failed to read authSubType");
181 return READ_PARCEL_ERROR;
182 }
183
184 std::vector<uint8_t> token;
185 if (!data.ReadUInt8Vector(&token)) {
186 IAM_LOGE("failed to read token");
187 return READ_PARCEL_ERROR;
188 }
189
190 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
191 if (callback == nullptr) {
192 IAM_LOGE("callback is nullptr");
193 return READ_PARCEL_ERROR;
194 }
195 if (authType == PIN && !token.empty()) {
196 IAM_LOGI("auth type is pin, clear token");
197 token.clear();
198 }
199 CredentialPara credPara = {};
200 credPara.authType = static_cast<AuthType>(authType);
201 credPara.pinType = static_cast<PinSubType>(authSubType);
202 credPara.token = token;
203 AddCredential(userId, credPara, callback, false);
204 return SUCCESS;
205 }
206
UpdateCredentialStub(MessageParcel & data,MessageParcel & reply)207 int32_t UserIdmStub::UpdateCredentialStub(MessageParcel &data, MessageParcel &reply)
208 {
209 IAM_LOGI("enter");
210 ON_SCOPE_EXIT(IAM_LOGI("leave"));
211
212 int32_t userId;
213 if (!data.ReadInt32(userId)) {
214 IAM_LOGE("failed to read userId");
215 return READ_PARCEL_ERROR;
216 }
217
218 int32_t authType;
219 if (!data.ReadInt32(authType)) {
220 IAM_LOGE("failed to read authType");
221 return READ_PARCEL_ERROR;
222 }
223
224 int32_t authSubType;
225 if (!data.ReadInt32(authSubType)) {
226 IAM_LOGE("failed to read authSubType");
227 return READ_PARCEL_ERROR;
228 }
229 std::vector<uint8_t> token = {};
230 if (!data.ReadUInt8Vector(&token)) {
231 IAM_LOGE("failed to read token");
232 return READ_PARCEL_ERROR;
233 }
234
235 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
236 if (callback == nullptr) {
237 IAM_LOGE("callback is nullptr");
238 return GENERAL_ERROR;
239 }
240
241 CredentialPara credPara = {};
242 credPara.authType = static_cast<AuthType>(authType);
243 credPara.pinType = static_cast<PinSubType>(authSubType);
244 credPara.token = token;
245 UpdateCredential(userId, credPara, callback);
246 return SUCCESS;
247 }
248
CancelStub(MessageParcel & data,MessageParcel & reply)249 int32_t UserIdmStub::CancelStub(MessageParcel &data, MessageParcel &reply)
250 {
251 IAM_LOGI("enter");
252 ON_SCOPE_EXIT(IAM_LOGI("leave"));
253
254 int32_t userId;
255 if (!data.ReadInt32(userId)) {
256 IAM_LOGE("failed to read userId");
257 return READ_PARCEL_ERROR;
258 }
259
260 int32_t ret = Cancel(userId);
261 static_cast<void>(reply.WriteInt32(ret));
262 return ret;
263 }
264
EnforceDelUserStub(MessageParcel & data,MessageParcel & reply)265 int32_t UserIdmStub::EnforceDelUserStub(MessageParcel &data, MessageParcel &reply)
266 {
267 IAM_LOGI("enter");
268 ON_SCOPE_EXIT(IAM_LOGI("leave"));
269
270 int32_t userId;
271 if (!data.ReadInt32(userId)) {
272 IAM_LOGE("failed to read userId");
273 return READ_PARCEL_ERROR;
274 }
275
276 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
277 if (callback == nullptr) {
278 IAM_LOGE("callback is nullptr");
279 return READ_PARCEL_ERROR;
280 }
281
282 int32_t ret = EnforceDelUser(userId, callback);
283 static_cast<void>(reply.WriteInt32(ret));
284 return ret;
285 }
286
287 int32_t UserIdmStub::DelUserStub(MessageParcel &data, [[maybe_unused]] MessageParcel &reply)
288 {
289 IAM_LOGI("enter");
290 ON_SCOPE_EXIT(IAM_LOGI("leave"));
291
292 int32_t userId;
293 if (!data.ReadInt32(userId)) {
294 IAM_LOGE("failed to read userId");
295 return READ_PARCEL_ERROR;
296 }
297
298 std::vector<uint8_t> authToken = {};
299 if (!data.ReadUInt8Vector(&authToken)) {
300 IAM_LOGE("failed to read authToken");
301 return READ_PARCEL_ERROR;
302 }
303
304 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
305 if (callback == nullptr) {
306 IAM_LOGE("callback is nullptr");
307 return READ_PARCEL_ERROR;
308 }
309
310 DelUser(userId, authToken, callback);
311 return SUCCESS;
312 }
313
DelCredentialStub(MessageParcel & data,MessageParcel & reply)314 int32_t UserIdmStub::DelCredentialStub(MessageParcel &data, MessageParcel &reply)
315 {
316 IAM_LOGI("enter");
317 ON_SCOPE_EXIT(IAM_LOGI("leave"));
318
319 int32_t userId;
320 if (!data.ReadInt32(userId)) {
321 IAM_LOGE("failed to read userId");
322 return READ_PARCEL_ERROR;
323 }
324
325 uint64_t credentialId;
326 if (!data.ReadUint64(credentialId)) {
327 IAM_LOGE("failed to read credentialId");
328 return READ_PARCEL_ERROR;
329 }
330
331 std::vector<uint8_t> authToken;
332 if (!data.ReadUInt8Vector(&authToken)) {
333 IAM_LOGE("failed to read authToken");
334 return READ_PARCEL_ERROR;
335 }
336
337 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
338 if (callback == nullptr) {
339 IAM_LOGE("callback is nullptr");
340 return GENERAL_ERROR;
341 }
342
343 DelCredential(userId, credentialId, authToken, callback);
344 return SUCCESS;
345 }
346
ClearRedundancyCredentialStub(MessageParcel & data,MessageParcel & reply)347 int32_t UserIdmStub::ClearRedundancyCredentialStub(MessageParcel &data, MessageParcel &reply)
348 {
349 IAM_LOGI("enter");
350 ON_SCOPE_EXIT(IAM_LOGI("leave"));
351
352 sptr<IdmCallbackInterface> callback = iface_cast<IdmCallbackProxy>(data.ReadRemoteObject());
353 if (callback == nullptr) {
354 IAM_LOGE("callback is nullptr");
355 return READ_PARCEL_ERROR;
356 }
357
358 ClearRedundancyCredential(callback);
359
360 return SUCCESS;
361 }
362
363 } // namespace UserAuth
364 } // namespace UserIam
365 } // namespace OHOS