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