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