• 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_auth_proxy.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 
21 #include "iam_logger.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SDK
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 namespace {
29     const uint64_t BAD_CONTEXT_ID = 0;
30 } // namespace
31 
UserAuthProxy(const sptr<IRemoteObject> & object)32 UserAuthProxy::UserAuthProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<UserAuthInterface>(object)
33 {
34 }
35 
GetAvailableStatus(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel)36 int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
37 {
38     MessageParcel data;
39     MessageParcel reply;
40 
41     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
42         IAM_LOGE("failed to write descriptor");
43         return WRITE_PARCEL_ERROR;
44     }
45     if (!data.WriteInt32(authType)) {
46         IAM_LOGE("failed to write authType");
47         return WRITE_PARCEL_ERROR;
48     }
49     if (!data.WriteUint32(authTrustLevel)) {
50         IAM_LOGE("failed to write authTrustLevel");
51         return WRITE_PARCEL_ERROR;
52     }
53     if (!data.WriteInt32(apiVersion)) {
54         IAM_LOGE("failed to write apiVersion");
55         return WRITE_PARCEL_ERROR;
56     }
57 
58     bool ret = SendRequest(UserAuthInterface::USER_AUTH_GET_AVAILABLE_STATUS, data, reply);
59     if (!ret) {
60         return GENERAL_ERROR;
61     }
62     int32_t result = SUCCESS;
63     if (!reply.ReadInt32(result)) {
64         IAM_LOGE("failed to read result");
65         return READ_PARCEL_ERROR;
66     }
67     return result;
68 }
69 
GetProperty(int32_t userId,AuthType authType,const std::vector<Attributes::AttributeKey> & keys,sptr<GetExecutorPropertyCallbackInterface> & callback)70 void UserAuthProxy::GetProperty(int32_t userId, AuthType authType,
71     const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback)
72 {
73     if (callback == nullptr) {
74         IAM_LOGE("callback is nullptr");
75         return;
76     }
77     MessageParcel data;
78     MessageParcel reply;
79 
80     std::vector<uint32_t> attrKeys;
81     attrKeys.resize(keys.size());
82     std::transform(keys.begin(), keys.end(), attrKeys.begin(), [](Attributes::AttributeKey key) {
83         return static_cast<uint32_t>(key);
84     });
85 
86     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
87         IAM_LOGE("failed to write descriptor");
88         return;
89     }
90     if (!data.WriteInt32(userId)) {
91         IAM_LOGE("failed to write userId");
92         return;
93     }
94     if (!data.WriteInt32(authType)) {
95         IAM_LOGE("failed to write authType");
96         return;
97     }
98     if (!data.WriteUInt32Vector(attrKeys)) {
99         IAM_LOGE("failed to write keys");
100         return;
101     }
102     if (!data.WriteRemoteObject(callback->AsObject())) {
103         IAM_LOGE("failed to write callback");
104         return;
105     }
106 
107     bool ret = SendRequest(UserAuthInterface::USER_AUTH_GET_PROPERTY, data, reply);
108     if (!ret) {
109         Attributes attr;
110         callback->OnGetExecutorPropertyResult(GENERAL_ERROR, attr);
111     }
112 }
113 
SetProperty(int32_t userId,AuthType authType,const Attributes & attributes,sptr<SetExecutorPropertyCallbackInterface> & callback)114 void UserAuthProxy::SetProperty(int32_t userId, AuthType authType, const Attributes &attributes,
115     sptr<SetExecutorPropertyCallbackInterface> &callback)
116 {
117     if (callback == nullptr) {
118         IAM_LOGE("callback is nullptr");
119         return;
120     }
121     MessageParcel data;
122     MessageParcel reply;
123 
124     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
125         IAM_LOGE("failed to write descriptor");
126         return;
127     }
128     if (!data.WriteInt32(userId)) {
129         IAM_LOGE("failed to write userId");
130         return;
131     }
132     if (!data.WriteInt32(authType)) {
133         IAM_LOGE("failed to write authType");
134         return;
135     }
136     auto buffer = attributes.Serialize();
137     if (!data.WriteUInt8Vector(buffer)) {
138         IAM_LOGE("failed to write attributes");
139         return;
140     }
141     if (!data.WriteRemoteObject(callback->AsObject())) {
142         IAM_LOGE("failed to write callback");
143         return;
144     }
145 
146     bool ret = SendRequest(UserAuthInterface::USER_AUTH_SET_PROPERTY, data, reply);
147     if (!ret) {
148         callback->OnSetExecutorPropertyResult(GENERAL_ERROR);
149     }
150 }
151 
WriteAuthParam(MessageParcel & data,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,sptr<UserAuthCallbackInterface> & callback)152 bool UserAuthProxy::WriteAuthParam(MessageParcel &data, const std::vector<uint8_t> &challenge,
153     AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback)
154 {
155     if (!data.WriteUInt8Vector(challenge)) {
156         IAM_LOGE("failed to write challenge");
157         return false;
158     }
159     if (!data.WriteInt32(authType)) {
160         IAM_LOGE("failed to write authType");
161         return false;
162     }
163     if (!data.WriteUint32(authTrustLevel)) {
164         IAM_LOGE("failed to write authTrustLevel");
165         return false;
166     }
167     if (!data.WriteRemoteObject(callback->AsObject())) {
168         IAM_LOGE("failed to write callback");
169         return false;
170     }
171     return true;
172 }
173 
Auth(int32_t apiVersion,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,sptr<UserAuthCallbackInterface> & callback)174 uint64_t UserAuthProxy::Auth(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType,
175     AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback)
176 {
177     if (callback == nullptr) {
178         IAM_LOGE("callback is nullptr");
179         return BAD_CONTEXT_ID;
180     }
181     MessageParcel data;
182     MessageParcel reply;
183 
184     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
185         IAM_LOGE("failed to write descriptor");
186         return BAD_CONTEXT_ID;
187     }
188     if (!WriteAuthParam(data, challenge, authType, authTrustLevel, callback)) {
189         IAM_LOGE("failed to write auth param");
190         return BAD_CONTEXT_ID;
191     }
192     if (!data.WriteInt32(apiVersion)) {
193         IAM_LOGE("failed to write apiVersion");
194         return BAD_CONTEXT_ID;
195     }
196     bool ret = SendRequest(UserAuthInterface::USER_AUTH_AUTH, data, reply);
197     if (!ret) {
198         return BAD_CONTEXT_ID;
199     }
200     uint64_t result = BAD_CONTEXT_ID;
201     if (!reply.ReadUint64(result)) {
202         IAM_LOGE("failed to read result");
203     }
204     return result;
205 }
206 
AuthUser(int32_t userId,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,sptr<UserAuthCallbackInterface> & callback)207 uint64_t UserAuthProxy::AuthUser(int32_t userId, const std::vector<uint8_t> &challenge,
208     AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback)
209 {
210     if (callback == nullptr) {
211         IAM_LOGE("callback is nullptr");
212         return BAD_CONTEXT_ID;
213     }
214     MessageParcel data;
215     MessageParcel reply;
216 
217     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
218         IAM_LOGE("failed to write descriptor");
219         return BAD_CONTEXT_ID;
220     }
221     if (!data.WriteInt32(userId)) {
222         IAM_LOGE("failed to write userId");
223         return BAD_CONTEXT_ID;
224     }
225     if (!WriteAuthParam(data, challenge, authType, authTrustLevel, callback)) {
226         IAM_LOGE("failed to write auth param");
227         return BAD_CONTEXT_ID;
228     }
229 
230     bool ret = SendRequest(UserAuthInterface::USER_AUTH_AUTH_USER, data, reply);
231     if (!ret) {
232         return BAD_CONTEXT_ID;
233     }
234     uint64_t result = BAD_CONTEXT_ID;
235     if (!reply.ReadUint64(result)) {
236         IAM_LOGE("failed to read result");
237     }
238     return result;
239 }
240 
Identify(const std::vector<uint8_t> & challenge,AuthType authType,sptr<UserAuthCallbackInterface> & callback)241 uint64_t UserAuthProxy::Identify(const std::vector<uint8_t> &challenge, AuthType authType,
242     sptr<UserAuthCallbackInterface> &callback)
243 {
244     if (callback == nullptr) {
245         IAM_LOGE("callback is nullptr");
246         return BAD_CONTEXT_ID;
247     }
248     MessageParcel data;
249     MessageParcel reply;
250 
251     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
252         IAM_LOGE("failed to write descriptor");
253         return BAD_CONTEXT_ID;
254     }
255     if (!data.WriteUInt8Vector(challenge)) {
256         IAM_LOGE("failed to write challenge");
257         return BAD_CONTEXT_ID;
258     }
259     if (!data.WriteInt32(authType)) {
260         IAM_LOGE("failed to write authType");
261         return BAD_CONTEXT_ID;
262     }
263     if (!data.WriteRemoteObject(callback->AsObject())) {
264         IAM_LOGE("failed to write callback");
265         return BAD_CONTEXT_ID;
266     }
267 
268     bool ret = SendRequest(UserAuthInterface::USER_AUTH_IDENTIFY, data, reply);
269     if (!ret) {
270         return BAD_CONTEXT_ID;
271     }
272     uint64_t result = BAD_CONTEXT_ID;
273     if (!reply.ReadUint64(result)) {
274         IAM_LOGE("failed to read result");
275     }
276     return result;
277 }
278 
CancelAuthOrIdentify(uint64_t contextId)279 int32_t UserAuthProxy::CancelAuthOrIdentify(uint64_t contextId)
280 {
281     MessageParcel data;
282     MessageParcel reply;
283 
284     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
285         IAM_LOGE("failed to write descriptor");
286         return GENERAL_ERROR;
287     }
288     if (!data.WriteUint64(contextId)) {
289         IAM_LOGE("failed to write contextId");
290         return GENERAL_ERROR;
291     }
292 
293     bool ret = SendRequest(UserAuthInterface::USER_AUTH_CANCEL_AUTH, data, reply);
294     if (!ret) {
295         return GENERAL_ERROR;
296     }
297     int32_t result = GENERAL_ERROR;
298     if (!reply.ReadInt32(result)) {
299         IAM_LOGE("failed to read result");
300     }
301     return result;
302 }
303 
GetVersion(int32_t & version)304 int32_t UserAuthProxy::GetVersion(int32_t &version)
305 {
306     version = 0;
307     MessageParcel data;
308     MessageParcel reply;
309 
310     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
311         IAM_LOGE("failed to write descriptor");
312         return GENERAL_ERROR;
313     }
314 
315     bool ret = SendRequest(UserAuthInterface::USER_AUTH_GET_VERSION, data, reply);
316     if (!ret) {
317         return GENERAL_ERROR;
318     }
319     if (!reply.ReadInt32(version)) {
320         IAM_LOGE("failed to read version");
321         return GENERAL_ERROR;
322     }
323     int32_t result = GENERAL_ERROR;
324     if (!reply.ReadInt32(result)) {
325         IAM_LOGE("failed to read result");
326     }
327     return result;
328 }
329 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)330 bool UserAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
331 {
332     IAM_LOGI("code = %{public}u", code);
333     sptr<IRemoteObject> remote = Remote();
334     if (remote == nullptr) {
335         IAM_LOGE("failed to get remote");
336         return false;
337     }
338     MessageOption option(MessageOption::TF_SYNC);
339     int32_t result = remote->SendRequest(code, data, reply, option);
340     if (result != OHOS::NO_ERROR) {
341         IAM_LOGE("failed to send request, result = %{public}d", result);
342         return false;
343     }
344     return true;
345 }
346 } // namespace UserAuth
347 } // namespace UserIam
348 } // namespace OHOS