1 /*
2  * Copyright (c) 2023 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 "dlp_permission_async_proxy.h"
17 #include "dlp_permission.h"
18 #include "dlp_permission_log.h"
19 #include "dlp_permission_service_ipc_interface_code.h"
20 #include "dlp_policy_parcel.h"
21 
22 namespace OHOS {
23 namespace Security {
24 namespace DlpPermission {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27     LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpPermissionAsyncProxy"};
28 }
29 
OnGenerateDlpCertificate(int32_t result,const std::vector<uint8_t> & cert)30 void DlpPermissionAsyncProxy::OnGenerateDlpCertificate(int32_t result, const std::vector<uint8_t>& cert)
31 {
32     MessageParcel data;
33     if (!data.WriteInterfaceToken(DlpPermissionAsyncProxy::GetDescriptor())) {
34         DLP_LOG_ERROR(LABEL, "Write descriptor fail");
35         return;
36     }
37     if (!data.WriteInt32(result)) {
38         DLP_LOG_ERROR(LABEL, "Write int32 fail");
39         return;
40     }
41     if (result == DLP_OK) {
42         if (!data.WriteUInt8Vector(cert)) {
43             DLP_LOG_ERROR(LABEL, "Write uint8 vector fail");
44             return;
45         }
46     }
47 
48     MessageParcel reply;
49     MessageOption option(MessageOption::TF_SYNC);
50     sptr<IRemoteObject> remote = Remote();
51     if (remote == nullptr) {
52         DLP_LOG_ERROR(LABEL, "Remote service is null.");
53         return;
54     }
55     int32_t requestResult = remote->SendRequest(
56         static_cast<uint32_t>(DlpPermissionCallbackInterfaceCode::ON_GENERATE_DLP_CERTIFICATE), data, reply, option);
57     if (requestResult != DLP_OK) {
58         DLP_LOG_ERROR(LABEL, "SendRequest fail, result: %{public}d", requestResult);
59         return;
60     }
61 }
62 
OnParseDlpCertificate(int32_t result,const PermissionPolicy & policy,const std::vector<uint8_t> & cert)63 void DlpPermissionAsyncProxy::OnParseDlpCertificate(int32_t result, const PermissionPolicy& policy,
64     const std::vector<uint8_t>& cert)
65 {
66     MessageParcel data;
67     if (!data.WriteInterfaceToken(DlpPermissionAsyncProxy::GetDescriptor())) {
68         DLP_LOG_ERROR(LABEL, "Write descriptor fail");
69         return;
70     }
71 
72     if (!data.WriteInt32(result)) {
73         DLP_LOG_ERROR(LABEL, "Write int32 fail");
74         return;
75     }
76 
77     if (result == DLP_OK) {
78         DlpPolicyParcel policyParcel;
79         policyParcel.policyParams_.CopyPermissionPolicy(policy);
80         if (!data.WriteParcelable(&policyParcel)) {
81             DLP_LOG_ERROR(LABEL, "Write parcel fail");
82             return;
83         }
84 
85         if (!data.WriteUInt8Vector(cert)) {
86             DLP_LOG_ERROR(LABEL, "Write uint8 vector fail");
87             return;
88         }
89     }
90 
91     MessageParcel reply;
92     MessageOption option(MessageOption::TF_SYNC);
93     sptr<IRemoteObject> remote = Remote();
94     if (remote == nullptr) {
95         DLP_LOG_ERROR(LABEL, "Remote service is null.");
96         return;
97     }
98     int32_t requestResult = remote->SendRequest(
99         static_cast<uint32_t>(DlpPermissionCallbackInterfaceCode::ON_PARSE_DLP_CERTIFICATE), data, reply, option);
100     if (requestResult != DLP_OK) {
101         DLP_LOG_ERROR(LABEL, "SendRequest fail, result: %{public}d", requestResult);
102         return;
103     }
104 }
105 }  // namespace DlpPermission
106 }  // namespace Security
107 }  // namespace OHOS
108