• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "accesstoken_manager_proxy.h"
17 
18 #include "accesstoken_log.h"
19 
20 #include "parcel.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenManagerProxy"};
28 }
29 
AccessTokenManagerProxy(const sptr<IRemoteObject> & impl)30 AccessTokenManagerProxy::AccessTokenManagerProxy(const sptr<IRemoteObject>& impl)
31     : IRemoteProxy<IAccessTokenManager>(impl) {
32 }
33 
~AccessTokenManagerProxy()34 AccessTokenManagerProxy::~AccessTokenManagerProxy()
35 {}
36 
VerifyAccessToken(AccessTokenID tokenID,const std::string & permissionName)37 int AccessTokenManagerProxy::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)
38 {
39     MessageParcel data;
40     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
41     if (!data.WriteUint32(tokenID)) {
42         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
43         return PERMISSION_DENIED;
44     }
45     if (!data.WriteString(permissionName)) {
46         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
47         return PERMISSION_DENIED;
48     }
49 
50     MessageParcel reply;
51     MessageOption option(MessageOption::TF_SYNC);
52     sptr<IRemoteObject> remote = Remote();
53     if (remote == nullptr) {
54         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
55         return PERMISSION_DENIED;
56     }
57     int32_t requestResult = remote->SendRequest(
58         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::VERIFY_ACCESSTOKEN), data, reply, option);
59     if (requestResult != NO_ERROR) {
60         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
61         return PERMISSION_DENIED;
62     }
63 
64     int32_t result = reply.ReadInt32();
65     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
66     return result;
67 }
68 
VerifyNativeToken(AccessTokenID tokenID,const std::string & permissionName)69 int AccessTokenManagerProxy::VerifyNativeToken(AccessTokenID tokenID, const std::string& permissionName)
70 {
71     MessageParcel data;
72     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
73     if (!data.WriteUint32(tokenID)) {
74         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
75         return PERMISSION_DENIED;
76     }
77     if (!data.WriteString(permissionName)) {
78         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
79         return PERMISSION_DENIED;
80     }
81 
82     MessageParcel reply;
83     MessageOption option(MessageOption::TF_SYNC);
84     sptr<IRemoteObject> remote = Remote();
85     if (remote == nullptr) {
86         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
87         return PERMISSION_DENIED;
88     }
89     int32_t requestResult = remote->SendRequest(
90         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::VERIFY_NATIVETOKEN), data, reply, option);
91     if (requestResult != NO_ERROR) {
92         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
93         return PERMISSION_DENIED;
94     }
95 
96     int32_t result = reply.ReadInt32();
97     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
98     return result;
99 }
100 
GetDefPermission(const std::string & permissionName,PermissionDefParcel & permissionDefResult)101 int AccessTokenManagerProxy::GetDefPermission(
102     const std::string& permissionName, PermissionDefParcel& permissionDefResult)
103 {
104     MessageParcel data;
105     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
106     if (!data.WriteString(permissionName)) {
107         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
108         return RET_FAILED;
109     }
110 
111     MessageParcel reply;
112     MessageOption option(MessageOption::TF_SYNC);
113     sptr<IRemoteObject> remote = Remote();
114     if (remote == nullptr) {
115         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
116         return RET_FAILED;
117     }
118     int32_t requestResult = remote->SendRequest(
119         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_DEF_PERMISSION), data, reply, option);
120     if (requestResult != NO_ERROR) {
121         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
122         return RET_FAILED;
123     }
124 
125     sptr<PermissionDefParcel> resultSptr = reply.ReadParcelable<PermissionDefParcel>();
126     if (resultSptr == nullptr) {
127         ACCESSTOKEN_LOG_ERROR(LABEL, "read permission def parcel fail");
128         return RET_FAILED;
129     }
130     permissionDefResult = *resultSptr;
131     int32_t result = reply.ReadInt32();
132     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
133     return result;
134 }
135 
GetDefPermissions(AccessTokenID tokenID,std::vector<PermissionDefParcel> & permList)136 int AccessTokenManagerProxy::GetDefPermissions(AccessTokenID tokenID,
137     std::vector<PermissionDefParcel>& permList)
138 {
139     MessageParcel data;
140     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
141     if (!data.WriteUint32(tokenID)) {
142         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
143         return RET_FAILED;
144     }
145 
146     MessageParcel reply;
147     MessageOption option(MessageOption::TF_SYNC);
148     sptr<IRemoteObject> remote = Remote();
149     if (remote == nullptr) {
150         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
151         return RET_FAILED;
152     }
153     int32_t requestResult = remote->SendRequest(
154         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_DEF_PERMISSIONS), data, reply, option);
155     if (requestResult != NO_ERROR) {
156         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
157         return RET_FAILED;
158     }
159 
160     int32_t size = reply.ReadInt32();
161     if (size > MAX_PERMISSION_SIZE) {
162         ACCESSTOKEN_LOG_ERROR(LABEL, "size = %{public}d get from request is invalid", size);
163         return RET_FAILED;
164     }
165     for (int i = 0; i < size; i++) {
166         sptr<PermissionDefParcel> permissionDef = reply.ReadParcelable<PermissionDefParcel>();
167         if (permissionDef != nullptr) {
168             permList.emplace_back(*permissionDef);
169         }
170     }
171     int32_t result = reply.ReadInt32();
172     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
173     return result;
174 }
175 
GetReqPermissions(AccessTokenID tokenID,std::vector<PermissionStateFullParcel> & reqPermList,bool isSystemGrant)176 int AccessTokenManagerProxy::GetReqPermissions(
177     AccessTokenID tokenID, std::vector<PermissionStateFullParcel>& reqPermList, bool isSystemGrant)
178 {
179     MessageParcel data;
180     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
181     if (!data.WriteUint32(tokenID)) {
182         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
183         return RET_FAILED;
184     }
185     if (!data.WriteInt32(isSystemGrant)) {
186         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write isSystemGrant");
187         return RET_FAILED;
188     }
189 
190     MessageParcel reply;
191     MessageOption option(MessageOption::TF_SYNC);
192     sptr<IRemoteObject> remote = Remote();
193     if (remote == nullptr) {
194         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
195         return RET_FAILED;
196     }
197     int32_t requestResult = remote->SendRequest(
198         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_REQ_PERMISSIONS), data, reply, option);
199     if (requestResult != NO_ERROR) {
200         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
201         return RET_FAILED;
202     }
203 
204     int32_t size = reply.ReadInt32();
205     if (size > MAX_PERMISSION_SIZE) {
206         ACCESSTOKEN_LOG_ERROR(LABEL, "size = %{public}d get from request is invalid", size);
207         return RET_FAILED;
208     }
209     for (int i = 0; i < size; i++) {
210         sptr<PermissionStateFullParcel> permissionReq = reply.ReadParcelable<PermissionStateFullParcel>();
211         if (permissionReq != nullptr) {
212             reqPermList.emplace_back(*permissionReq);
213         }
214     }
215     int32_t result = reply.ReadInt32();
216     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
217     return result;
218 }
219 
GetPermissionFlag(AccessTokenID tokenID,const std::string & permissionName)220 int AccessTokenManagerProxy::GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName)
221 {
222     MessageParcel data;
223     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
224     if (!data.WriteUint32(tokenID)) {
225         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
226         return DEFAULT_PERMISSION_FLAGS;
227     }
228     if (!data.WriteString(permissionName)) {
229         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
230         return DEFAULT_PERMISSION_FLAGS;
231     }
232 
233     MessageParcel reply;
234     MessageOption option(MessageOption::TF_SYNC);
235     sptr<IRemoteObject> remote = Remote();
236     if (remote == nullptr) {
237         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
238         return DEFAULT_PERMISSION_FLAGS;
239     }
240     int32_t requestResult = remote->SendRequest(
241         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_PERMISSION_FLAG), data, reply, option);
242     if (requestResult != NO_ERROR) {
243         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
244         return DEFAULT_PERMISSION_FLAGS;
245     }
246 
247     int32_t result = reply.ReadInt32();
248     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
249     return result;
250 }
251 
GetSelfPermissionsState(std::vector<PermissionListStateParcel> & permListParcel)252 PermissionOper AccessTokenManagerProxy::GetSelfPermissionsState(
253     std::vector<PermissionListStateParcel>& permListParcel)
254 {
255     MessageParcel data;
256     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
257     if (!data.WriteUint32(permListParcel.size())) {
258         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permListParcel size.");
259         return INVALID_OPER;
260     }
261     for (auto permission : permListParcel) {
262         if (!data.WriteParcelable(&permission)) {
263             ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permListParcel.");
264             return INVALID_OPER;
265         }
266     }
267 
268     MessageParcel reply;
269     MessageOption option(MessageOption::TF_SYNC);
270     sptr<IRemoteObject> remote = Remote();
271     if (remote == nullptr) {
272         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
273         return INVALID_OPER;
274     }
275     int32_t requestResult = remote->SendRequest(
276         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_PERMISSION_OPER_STATE), data, reply, option);
277     if (requestResult != NO_ERROR) {
278         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
279         return INVALID_OPER;
280     }
281 
282     PermissionOper result = static_cast<PermissionOper>(reply.ReadInt32());
283     if (result == INVALID_OPER) {
284         ACCESSTOKEN_LOG_ERROR(LABEL, "result from server is invalid.");
285         return result;
286     }
287     size_t size = reply.ReadUint32();
288     if (size != permListParcel.size()) {
289         ACCESSTOKEN_LOG_ERROR(LABEL, "permListParcel size from server is invalid.");
290         return INVALID_OPER;
291     }
292     for (uint32_t i = 0; i < size; i++) {
293         sptr<PermissionListStateParcel> permissionReq = reply.ReadParcelable<PermissionListStateParcel>();
294         if (permissionReq != nullptr) {
295             permListParcel[i].permsState.state = permissionReq->permsState.state;
296         }
297     }
298 
299     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
300     return result;
301 }
302 
GrantPermission(AccessTokenID tokenID,const std::string & permissionName,int flag)303 int AccessTokenManagerProxy::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
304 {
305     MessageParcel data;
306     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
307     if (!data.WriteUint32(tokenID)) {
308         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
309         return RET_FAILED;
310     }
311     if (!data.WriteString(permissionName)) {
312         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
313         return RET_FAILED;
314     }
315     if (!data.WriteInt32(flag)) {
316         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write flag");
317         return RET_FAILED;
318     }
319 
320     MessageParcel reply;
321     MessageOption option(MessageOption::TF_SYNC);
322     sptr<IRemoteObject> remote = Remote();
323     if (remote == nullptr) {
324         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
325         return RET_FAILED;
326     }
327     int32_t requestResult = remote->SendRequest(
328         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GRANT_PERMISSION), data, reply, option);
329     if (requestResult != NO_ERROR) {
330         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
331         return RET_FAILED;
332     }
333 
334     int32_t result = reply.ReadInt32();
335     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
336     return result;
337 }
338 
RevokePermission(AccessTokenID tokenID,const std::string & permissionName,int flag)339 int AccessTokenManagerProxy::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
340 {
341     MessageParcel data;
342     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
343     if (!data.WriteUint32(tokenID)) {
344         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
345         return RET_FAILED;
346     }
347     if (!data.WriteString(permissionName)) {
348         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
349         return RET_FAILED;
350     }
351     if (!data.WriteInt32(flag)) {
352         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write flag");
353         return RET_FAILED;
354     }
355 
356     MessageParcel reply;
357     MessageOption option(MessageOption::TF_SYNC);
358     sptr<IRemoteObject> remote = Remote();
359     if (remote == nullptr) {
360         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
361         return RET_FAILED;
362     }
363     int32_t requestResult = remote->SendRequest(
364         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::REVOKE_PERMISSION), data, reply, option);
365     if (requestResult != NO_ERROR) {
366         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
367         return RET_FAILED;
368     }
369 
370     int32_t result = reply.ReadInt32();
371     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
372     return result;
373 }
374 
ClearUserGrantedPermissionState(AccessTokenID tokenID)375 int AccessTokenManagerProxy::ClearUserGrantedPermissionState(AccessTokenID tokenID)
376 {
377     MessageParcel data;
378     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
379     if (!data.WriteUint32(tokenID)) {
380         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
381         return RET_FAILED;
382     }
383 
384     MessageParcel reply;
385     MessageOption option(MessageOption::TF_SYNC);
386     sptr<IRemoteObject> remote = Remote();
387     if (remote == nullptr) {
388         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
389         return RET_FAILED;
390     }
391     int32_t requestResult = remote->SendRequest(
392         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::CLEAR_USER_GRANT_PERMISSION), data, reply, option);
393     if (requestResult != NO_ERROR) {
394         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
395         return RET_FAILED;
396     }
397 
398     int32_t result = reply.ReadInt32();
399     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
400     return result;
401 }
402 
AllocHapToken(const HapInfoParcel & hapInfo,const HapPolicyParcel & policyParcel)403 AccessTokenIDEx AccessTokenManagerProxy::AllocHapToken(
404     const HapInfoParcel& hapInfo, const HapPolicyParcel& policyParcel)
405 {
406     MessageParcel data;
407     AccessTokenIDEx res;
408     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
409 
410     if (!data.WriteParcelable(&hapInfo)) {
411         res.tokenIDEx = 0;
412         return res;
413     }
414     if (!data.WriteParcelable(&policyParcel)) {
415         res.tokenIDEx = 0;
416         return res;
417     }
418 
419     MessageParcel reply;
420     MessageOption option(MessageOption::TF_SYNC);
421     sptr<IRemoteObject> remote = Remote();
422     if (remote == nullptr) {
423         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
424         res.tokenIDEx = 0;
425         return res;
426     }
427     int32_t requestResult = remote->SendRequest(
428         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::ALLOC_TOKEN_HAP), data, reply, option);
429     if (requestResult != NO_ERROR) {
430         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
431         res.tokenIDEx = 0;
432         return res;
433     }
434 
435     unsigned long long result = reply.ReadUint64();
436     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}llu", result);
437     res.tokenIDEx = result;
438     return res;
439 }
440 
DeleteToken(AccessTokenID tokenID)441 int AccessTokenManagerProxy::DeleteToken(AccessTokenID tokenID)
442 {
443     MessageParcel data;
444     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
445 
446     if (!data.WriteUint32(tokenID)) {
447         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
448         return RET_FAILED;
449     }
450 
451     MessageParcel reply;
452     MessageOption option(MessageOption::TF_SYNC);
453     sptr<IRemoteObject> remote = Remote();
454     if (remote == nullptr) {
455         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
456         return RET_FAILED;
457     }
458     int32_t requestResult = remote->SendRequest(
459         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::TOKEN_DELETE), data, reply, option);
460     if (requestResult != NO_ERROR) {
461         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
462         return RET_FAILED;
463     }
464 
465     int result = reply.ReadInt32();
466     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
467     return result;
468 }
469 
GetTokenType(AccessTokenID tokenID)470 int AccessTokenManagerProxy::GetTokenType(AccessTokenID tokenID)
471 {
472     MessageParcel data;
473     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
474 
475     if (!data.WriteUint32(tokenID)) {
476         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
477         return RET_FAILED;
478     }
479 
480     MessageParcel reply;
481     MessageOption option(MessageOption::TF_SYNC);
482     sptr<IRemoteObject> remote = Remote();
483     if (remote == nullptr) {
484         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
485         return RET_FAILED;
486     }
487     int32_t requestResult = remote->SendRequest(
488         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_TOKEN_TYPE), data, reply, option);
489     if (requestResult != NO_ERROR) {
490         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
491         return RET_FAILED;
492     }
493 
494     int result = reply.ReadInt32();
495     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
496     return result;
497 }
498 
CheckNativeDCap(AccessTokenID tokenID,const std::string & dcap)499 int AccessTokenManagerProxy::CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap)
500 {
501     MessageParcel data;
502     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
503 
504     if (!data.WriteUint32(tokenID)) {
505         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
506         return RET_FAILED;
507     }
508     if (!data.WriteString(dcap)) {
509         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write dcap");
510         return RET_FAILED;
511     }
512     MessageParcel reply;
513     MessageOption option(MessageOption::TF_SYNC);
514     sptr<IRemoteObject> remote = Remote();
515     if (remote == nullptr) {
516         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
517         return RET_FAILED;
518     }
519     int32_t requestResult = remote->SendRequest(
520         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::CHECK_NATIVE_DCAP), data, reply, option);
521     if (requestResult != NO_ERROR) {
522         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
523         return RET_FAILED;
524     }
525 
526     int result = reply.ReadInt32();
527     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
528     return result;
529 }
530 
GetHapTokenID(int userID,const std::string & bundleName,int instIndex)531 AccessTokenID AccessTokenManagerProxy::GetHapTokenID(int userID, const std::string& bundleName, int instIndex)
532 {
533     MessageParcel data;
534     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
535 
536     if (!data.WriteInt32(userID)) {
537         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
538         return 0;
539     }
540     if (!data.WriteString(bundleName)) {
541         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write dcap");
542         return 0;
543     }
544     if (!data.WriteInt32(instIndex)) {
545         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write dcap");
546         return 0;
547     }
548     MessageParcel reply;
549     MessageOption option(MessageOption::TF_SYNC);
550     sptr<IRemoteObject> remote = Remote();
551     if (remote == nullptr) {
552         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
553         return 0;
554     }
555     int32_t requestResult = remote->SendRequest(
556         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_HAP_TOKEN_ID), data, reply, option);
557     if (requestResult != NO_ERROR) {
558         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
559         return 0;
560     }
561 
562     int result = reply.ReadInt32();
563     ACCESSTOKEN_LOG_DEBUG(LABEL, "result from server data = %{public}d", result);
564     return result;
565 }
566 
AllocLocalTokenID(const std::string & remoteDeviceID,AccessTokenID remoteTokenID)567 AccessTokenID AccessTokenManagerProxy::AllocLocalTokenID(
568     const std::string& remoteDeviceID, AccessTokenID remoteTokenID)
569 {
570     MessageParcel data;
571     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
572 
573     if (!data.WriteString(remoteDeviceID)) {
574         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write dcap");
575         return 0;
576     }
577     if (!data.WriteUint32(remoteTokenID)) {
578         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write dcap");
579         return 0;
580     }
581     MessageParcel reply;
582     MessageOption option(MessageOption::TF_SYNC);
583     sptr<IRemoteObject> remote = Remote();
584     if (remote == nullptr) {
585         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
586         return 0;
587     }
588     int32_t requestResult = remote->SendRequest(
589         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::ALLOC_LOCAL_TOKEN_ID), data, reply, option);
590     if (requestResult != NO_ERROR) {
591         ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d", requestResult);
592         return 0;
593     }
594 
595     AccessTokenID result = reply.ReadUint32();
596     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
597     return result;
598 }
599 
GetNativeTokenInfo(AccessTokenID tokenID,NativeTokenInfoParcel & nativeTokenInfoRes)600 int AccessTokenManagerProxy::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfoParcel& nativeTokenInfoRes)
601 {
602     MessageParcel data;
603     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
604     if (!data.WriteUint32(tokenID)) {
605         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
606         return RET_FAILED;
607     }
608 
609     MessageParcel reply;
610     MessageOption option(MessageOption::TF_SYNC);
611     sptr<IRemoteObject> remote = Remote();
612     if (remote == nullptr) {
613         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
614         return RET_FAILED;
615     }
616     int32_t requestResult = remote->SendRequest(
617         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_NATIVE_TOKENINFO), data, reply, option);
618     if (requestResult != NO_ERROR) {
619         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
620         return RET_FAILED;
621     }
622 
623     sptr<NativeTokenInfoParcel> resultSptr = reply.ReadParcelable<NativeTokenInfoParcel>();
624     if (resultSptr == nullptr) {
625         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
626         return RET_FAILED;
627     }
628     nativeTokenInfoRes = *resultSptr;
629     int32_t result = reply.ReadInt32();
630     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
631     return result;
632 }
633 
GetHapTokenInfo(AccessTokenID tokenID,HapTokenInfoParcel & hapTokenInfoRes)634 int AccessTokenManagerProxy::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& hapTokenInfoRes)
635 {
636     MessageParcel data;
637     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
638     if (!data.WriteUint32(tokenID)) {
639         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
640         return RET_FAILED;
641     }
642 
643     MessageParcel reply;
644     MessageOption option(MessageOption::TF_SYNC);
645     sptr<IRemoteObject> remote = Remote();
646     if (remote == nullptr) {
647         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
648         return RET_FAILED;
649     }
650     int32_t requestResult = remote->SendRequest(
651         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_HAP_TOKENINFO), data, reply, option);
652     if (requestResult != NO_ERROR) {
653         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
654         return RET_FAILED;
655     }
656 
657     sptr<HapTokenInfoParcel> resultSptr = reply.ReadParcelable<HapTokenInfoParcel>();
658     if (resultSptr == nullptr) {
659         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
660         return RET_FAILED;
661     }
662     hapTokenInfoRes = *resultSptr;
663     int32_t result = reply.ReadInt32();
664     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
665     return result;
666 }
667 
UpdateHapToken(AccessTokenID tokenID,const std::string & appIDDesc,const HapPolicyParcel & policyParcel)668 int AccessTokenManagerProxy::UpdateHapToken(AccessTokenID tokenID,
669                                             const std::string& appIDDesc, const HapPolicyParcel& policyParcel)
670 {
671     MessageParcel data;
672     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
673     if (!data.WriteUint32(tokenID)) {
674         return RET_FAILED;
675     }
676     if (!data.WriteString(appIDDesc)) {
677         return RET_FAILED;
678     }
679     if (!data.WriteParcelable(&policyParcel)) {
680         return RET_FAILED;
681     }
682 
683     MessageParcel reply;
684     MessageOption option(MessageOption::TF_SYNC);
685     sptr<IRemoteObject> remote = Remote();
686     if (remote == nullptr) {
687         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
688         return RET_FAILED;
689     }
690     int32_t requestResult = remote->SendRequest(
691         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::UPDATE_HAP_TOKEN), data, reply, option);
692     if (requestResult != NO_ERROR) {
693         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
694         return RET_FAILED;
695     }
696 
697     int32_t result = reply.ReadInt32();
698     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
699     return result;
700 }
701 
702 #ifdef TOKEN_SYNC_ENABLE
GetHapTokenInfoFromRemote(AccessTokenID tokenID,HapTokenInfoForSyncParcel & hapSyncParcel)703 int AccessTokenManagerProxy::GetHapTokenInfoFromRemote(AccessTokenID tokenID,
704     HapTokenInfoForSyncParcel& hapSyncParcel)
705 {
706     MessageParcel data;
707     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
708     if (!data.WriteUint32(tokenID)) {
709         return RET_FAILED;
710     }
711 
712     MessageParcel reply;
713     MessageOption option(MessageOption::TF_SYNC);
714     sptr<IRemoteObject> remote = Remote();
715     if (remote == nullptr) {
716         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
717         return RET_FAILED;
718     }
719     int32_t requestResult = remote->SendRequest(
720         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_HAP_TOKEN_FROM_REMOTE), data, reply, option);
721     if (requestResult != NO_ERROR) {
722         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
723         return RET_FAILED;
724     }
725 
726     sptr<HapTokenInfoForSyncParcel> hapResult = reply.ReadParcelable<HapTokenInfoForSyncParcel>();
727     if (hapResult == nullptr) {
728         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
729         return RET_FAILED;
730     }
731     hapSyncParcel = *hapResult;
732 
733     int32_t result = reply.ReadInt32();
734     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
735     return result;
736 }
737 
GetAllNativeTokenInfo(std::vector<NativeTokenInfoParcel> & nativeTokenInfoRes)738 int AccessTokenManagerProxy::GetAllNativeTokenInfo(std::vector<NativeTokenInfoParcel>& nativeTokenInfoRes)
739 {
740     MessageParcel data;
741     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
742 
743     MessageParcel reply;
744     MessageOption option(MessageOption::TF_SYNC);
745     sptr<IRemoteObject> remote = Remote();
746     if (remote == nullptr) {
747         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
748         return RET_FAILED;
749     }
750     int32_t requestResult = remote->SendRequest(
751         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_ALL_NATIVE_TOKEN_FROM_REMOTE),
752         data, reply, option);
753     if (requestResult != NO_ERROR) {
754         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
755         return RET_FAILED;
756     }
757 
758     int32_t size = reply.ReadInt32();
759     if (size > MAX_NATIVE_TOKEN_INFO_SIZE) {
760         ACCESSTOKEN_LOG_ERROR(LABEL, "size = %{public}d get from request is invalid", size);
761         return RET_FAILED;
762     }
763     for (int i = 0; i < size; i++) {
764         sptr<NativeTokenInfoParcel> nativeResult = reply.ReadParcelable<NativeTokenInfoParcel>();
765         if (nativeResult != nullptr) {
766             nativeTokenInfoRes.emplace_back(*nativeResult);
767         }
768     }
769 
770     int32_t result = reply.ReadInt32();
771     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
772     return result;
773 }
774 
SetRemoteHapTokenInfo(const std::string & deviceID,HapTokenInfoForSyncParcel & hapSyncParcel)775 int AccessTokenManagerProxy::SetRemoteHapTokenInfo(const std::string& deviceID,
776     HapTokenInfoForSyncParcel& hapSyncParcel)
777 {
778     MessageParcel data;
779     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
780     if (!data.WriteString(deviceID)) {
781         return RET_FAILED;
782     }
783     if (!data.WriteParcelable(&hapSyncParcel)) {
784         return RET_FAILED;
785     }
786 
787     MessageParcel reply;
788     MessageOption option(MessageOption::TF_SYNC);
789     sptr<IRemoteObject> remote = Remote();
790     if (remote == nullptr) {
791         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
792         return RET_FAILED;
793     }
794     int32_t requestResult = remote->SendRequest(
795         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::SET_REMOTE_HAP_TOKEN_INFO), data, reply, option);
796     if (requestResult != NO_ERROR) {
797         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
798         return RET_FAILED;
799     }
800 
801     int32_t result = reply.ReadInt32();
802     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
803     return result;
804 }
805 
SetRemoteNativeTokenInfo(const std::string & deviceID,std::vector<NativeTokenInfoParcel> & nativeTokenInfoParcel)806 int AccessTokenManagerProxy::SetRemoteNativeTokenInfo(const std::string& deviceID,
807     std::vector<NativeTokenInfoParcel>& nativeTokenInfoParcel)
808 {
809     MessageParcel data;
810     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
811     if (!data.WriteString(deviceID)) {
812         return RET_FAILED;
813     }
814     if (!data.WriteUint32(nativeTokenInfoParcel.size())) {
815         return RET_FAILED;
816     }
817     for (NativeTokenInfoParcel& parcel : nativeTokenInfoParcel) {
818         if (!data.WriteParcelable(&parcel)) {
819             return RET_FAILED;
820         }
821     }
822 
823     MessageParcel reply;
824     MessageOption option(MessageOption::TF_SYNC);
825     sptr<IRemoteObject> remote = Remote();
826     if (remote == nullptr) {
827         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
828         return RET_FAILED;
829     }
830     int32_t requestResult = remote->SendRequest(
831         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::SET_REMOTE_NATIVE_TOKEN_INFO), data, reply, option);
832     if (requestResult != NO_ERROR) {
833         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
834         return RET_FAILED;
835     }
836 
837     int32_t result = reply.ReadInt32();
838     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
839     return result;
840 }
841 
DeleteRemoteToken(const std::string & deviceID,AccessTokenID tokenID)842 int AccessTokenManagerProxy::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID)
843 {
844     MessageParcel data;
845     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
846     if (!data.WriteString(deviceID)) {
847         return RET_FAILED;
848     }
849 
850     if (!data.WriteUint32(tokenID)) {
851         return RET_FAILED;
852     }
853 
854     MessageParcel reply;
855     MessageOption option(MessageOption::TF_SYNC);
856     sptr<IRemoteObject> remote = Remote();
857     if (remote == nullptr) {
858         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
859         return RET_FAILED;
860     }
861     int32_t requestResult = remote->SendRequest(
862         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::DELETE_REMOTE_TOKEN_INFO), data, reply, option);
863     if (requestResult != NO_ERROR) {
864         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
865         return RET_FAILED;
866     }
867 
868     int32_t result = reply.ReadInt32();
869     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
870     return result;
871 }
872 
GetRemoteNativeTokenID(const std::string & deviceID,AccessTokenID tokenID)873 AccessTokenID AccessTokenManagerProxy::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID)
874 {
875     MessageParcel data;
876     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
877     if (!data.WriteString(deviceID)) {
878         return 0;
879     }
880 
881     if (!data.WriteUint32(tokenID)) {
882         return 0;
883     }
884 
885     MessageParcel reply;
886     MessageOption option(MessageOption::TF_SYNC);
887     sptr<IRemoteObject> remote = Remote();
888     if (remote == nullptr) {
889         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
890         return 0;
891     }
892     int32_t requestResult = remote->SendRequest(
893         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::GET_NATIVE_REMOTE_TOKEN), data, reply, option);
894     if (requestResult != NO_ERROR) {
895         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
896         return 0;
897     }
898 
899     AccessTokenID result = reply.ReadUint32();
900     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
901     return result;
902 }
903 
DeleteRemoteDeviceTokens(const std::string & deviceID)904 int AccessTokenManagerProxy::DeleteRemoteDeviceTokens(const std::string& deviceID)
905 {
906     MessageParcel data;
907     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
908     if (!data.WriteString(deviceID)) {
909         return RET_FAILED;
910     }
911 
912     MessageParcel reply;
913     MessageOption option(MessageOption::TF_SYNC);
914     sptr<IRemoteObject> remote = Remote();
915     if (remote == nullptr) {
916         ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
917         return RET_FAILED;
918     }
919     int32_t requestResult = remote->SendRequest(
920         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::DELETE_REMOTE_DEVICE_TOKEN), data, reply, option);
921     if (requestResult != NO_ERROR) {
922         ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
923         return RET_FAILED;
924     }
925 
926     int32_t result = reply.ReadInt32();
927     ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
928     return result;
929 }
930 #endif
931 
DumpTokenInfo(std::string & dumpInfo)932 void AccessTokenManagerProxy::DumpTokenInfo(std::string& dumpInfo)
933 {
934     MessageParcel data;
935     data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
936 
937     MessageParcel reply;
938     MessageOption option;
939     sptr<IRemoteObject> remote = Remote();
940     if (remote == nullptr) {
941         ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s: remote service null.", __func__);
942         return;
943     }
944     int32_t requestResult = remote->SendRequest(
945         static_cast<uint32_t>(IAccessTokenManager::InterfaceCode::DUMP_TOKENINFO), data, reply, option);
946     if (requestResult != NO_ERROR) {
947         ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s send request fail, result: %{public}d", __func__, requestResult);
948         return;
949     }
950 
951     dumpInfo = reply.ReadString();
952     ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s get result from server dumpInfo = %{public}s", __func__, dumpInfo.c_str());
953 }
954 } // namespace AccessToken
955 } // namespace Security
956 } // namespace OHOS
957