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