1 /*
2 * Copyright (c) 2024 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 "el5_filekey_manager_proxy.h"
17
18 #include "el5_filekey_manager_error.h"
19 #include "el5_filekey_manager_log.h"
20 #include "parcel_utils.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
El5FilekeyManagerProxy(const sptr<IRemoteObject> & impl)25 El5FilekeyManagerProxy::El5FilekeyManagerProxy(const sptr<IRemoteObject>& impl)
26 : IRemoteProxy<El5FilekeyManagerInterface>(impl)
27 {
28 }
29
~El5FilekeyManagerProxy()30 El5FilekeyManagerProxy::~El5FilekeyManagerProxy()
31 {
32 }
33
AcquireAccess(DataLockType type)34 int32_t El5FilekeyManagerProxy::AcquireAccess(DataLockType type)
35 {
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
38 LOG_ERROR("Failed to write WriteInterfaceToken.");
39 return EFM_ERR_IPC_WRITE_DATA;
40 }
41 if (!data.WriteInt32(static_cast<int32_t>(type))) {
42 LOG_ERROR("Failed to WriteInt32(%{public}d).", type);
43 return EFM_ERR_IPC_WRITE_DATA;
44 }
45
46 MessageParcel reply;
47 MessageOption option;
48 sptr<IRemoteObject> remote = Remote();
49 if (remote == nullptr) {
50 LOG_ERROR("Remote service is null.");
51 return EFM_ERR_REMOTE_CONNECTION;
52 }
53 int32_t result = remote->SendRequest(static_cast<int32_t>(EFMInterfaceCode::ACQUIRE_ACCESS), data, reply, option);
54 if (result != NO_ERROR) {
55 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
56 } else {
57 result = reply.ReadInt32();
58 }
59 return result;
60 }
61
ReleaseAccess(DataLockType type)62 int32_t El5FilekeyManagerProxy::ReleaseAccess(DataLockType type)
63 {
64 MessageParcel data;
65 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
66 LOG_ERROR("Failed to write WriteInterfaceToken.");
67 return EFM_ERR_IPC_WRITE_DATA;
68 }
69 if (!data.WriteInt32(static_cast<int32_t>(type))) {
70 LOG_ERROR("Failed to WriteInt32(%{public}d).", type);
71 return EFM_ERR_IPC_WRITE_DATA;
72 }
73
74 MessageParcel reply;
75 MessageOption option;
76 sptr<IRemoteObject> remote = Remote();
77 if (remote == nullptr) {
78 LOG_ERROR("Remote service is null.");
79 return EFM_ERR_REMOTE_CONNECTION;
80 }
81 int32_t result = remote->SendRequest(static_cast<int32_t>(EFMInterfaceCode::RELEASE_ACCESS), data, reply, option);
82 if (result != NO_ERROR) {
83 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
84 } else {
85 result = reply.ReadInt32();
86 }
87 return result;
88 }
89
GenerateAppKey(uint32_t uid,const std::string & bundleName,std::string & keyId)90 int32_t El5FilekeyManagerProxy::GenerateAppKey(uint32_t uid, const std::string& bundleName, std::string& keyId)
91 {
92 MessageParcel data;
93 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
94 LOG_ERROR("Failed to write WriteInterfaceToken.");
95 return EFM_ERR_IPC_WRITE_DATA;
96 }
97 if (!data.WriteUint32(uid)) {
98 LOG_ERROR("Failed to WriteUint32(%{public}d).", uid);
99 return EFM_ERR_IPC_WRITE_DATA;
100 }
101 if (!data.WriteString(bundleName)) {
102 LOG_ERROR("Failed to WriteString(%{public}s).", bundleName.c_str());
103 return EFM_ERR_IPC_WRITE_DATA;
104 }
105
106 MessageParcel reply;
107 MessageOption option;
108 sptr<IRemoteObject> remote = Remote();
109 if (remote == nullptr) {
110 LOG_ERROR("Remote service is null.");
111 return EFM_ERR_REMOTE_CONNECTION;
112 }
113 int32_t result = remote->SendRequest(
114 static_cast<int32_t>(EFMInterfaceCode::GENERATE_APP_KEY), data, reply, option);
115 if (result != NO_ERROR) {
116 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
117 } else {
118 result = reply.ReadInt32();
119 keyId = reply.ReadString();
120 }
121 return result;
122 }
123
DeleteAppKey(const std::string & bundleName,int32_t userId)124 int32_t El5FilekeyManagerProxy::DeleteAppKey(const std::string& bundleName, int32_t userId)
125 {
126 MessageParcel data;
127 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
128 LOG_ERROR("Failed to write WriteInterfaceToken.");
129 return EFM_ERR_IPC_WRITE_DATA;
130 }
131 if (!data.WriteString(bundleName)) {
132 LOG_ERROR("Failed to WriteString(%{public}s).", bundleName.c_str());
133 return EFM_ERR_IPC_WRITE_DATA;
134 }
135 if (!data.WriteUint32(userId)) {
136 LOG_ERROR("Failed to WriteUint32(%{public}d).", userId);
137 return EFM_ERR_IPC_WRITE_DATA;
138 }
139
140 MessageParcel reply;
141 MessageOption option;
142 sptr<IRemoteObject> remote = Remote();
143 if (remote == nullptr) {
144 LOG_ERROR("Remote service is null.");
145 return EFM_ERR_REMOTE_CONNECTION;
146 }
147 int32_t result = remote->SendRequest(static_cast<int32_t>(EFMInterfaceCode::DELETE_APP_KEY), data, reply, option);
148 if (result != NO_ERROR) {
149 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
150 } else {
151 result = reply.ReadInt32();
152 }
153 return result;
154 }
155
GetUserAppKey(int32_t userId,bool getAllFlag,std::vector<std::pair<int32_t,std::string>> & keyInfos)156 int32_t El5FilekeyManagerProxy::GetUserAppKey(int32_t userId, bool getAllFlag,
157 std::vector<std::pair<int32_t, std::string>> &keyInfos)
158 {
159 MessageParcel data;
160 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
161 LOG_ERROR("Failed to write WriteInterfaceToken.");
162 return EFM_ERR_IPC_WRITE_DATA;
163 }
164 if (!data.WriteUint32(userId)) {
165 LOG_ERROR("Failed to WriteUint32(%{public}d).", userId);
166 return EFM_ERR_IPC_WRITE_DATA;
167 }
168 if (!data.WriteBool(getAllFlag)) {
169 LOG_ERROR("Failed to WriteBool");
170 return EFM_ERR_IPC_WRITE_DATA;
171 }
172
173 MessageParcel reply;
174 MessageOption option;
175 sptr<IRemoteObject> remote = Remote();
176 if (remote == nullptr) {
177 LOG_ERROR("Remote service is null.");
178 return EFM_ERR_REMOTE_CONNECTION;
179 }
180
181 int32_t result = remote->SendRequest(
182 static_cast<int32_t>(EFMInterfaceCode::GET_USER_APP_KEY), data, reply, option);
183 if (result != NO_ERROR) {
184 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
185 return result;
186 }
187
188 result = reply.ReadInt32();
189 if (result == EFM_SUCCESS) {
190 uint32_t keyInfoSize = reply.ReadUint32();
191 if (keyInfoSize > MAX_RECORD_SIZE) {
192 LOG_ERROR("Parse keyInfos failed, results oversize %{public}d.", keyInfoSize);
193 return EFM_ERR_IPC_READ_DATA;
194 }
195 for (uint32_t i = 0; i < keyInfoSize; ++i) {
196 int32_t uid = reply.ReadInt32();
197 std::string keyId = reply.ReadString();
198 keyInfos.emplace_back(std::make_pair(uid, keyId));
199 }
200 }
201 return result;
202 }
203
ChangeUserAppkeysLoadInfo(int32_t userId,std::vector<std::pair<std::string,bool>> & loadInfos)204 int32_t El5FilekeyManagerProxy::ChangeUserAppkeysLoadInfo(int32_t userId,
205 std::vector<std::pair<std::string, bool>> &loadInfos)
206 {
207 MessageParcel data;
208 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
209 LOG_ERROR("Failed to write WriteInterfaceToken.");
210 return EFM_ERR_IPC_WRITE_DATA;
211 }
212 if (!data.WriteUint32(userId)) {
213 LOG_ERROR("Failed to WriteUint32(%{public}d).", userId);
214 return EFM_ERR_IPC_WRITE_DATA;
215 }
216 if (!data.WriteUint32(loadInfos.size())) {
217 LOG_ERROR("Failed to WriteUint32(%{public}d).", static_cast<uint32_t>(loadInfos.size()));
218 return EFM_ERR_IPC_WRITE_DATA;
219 }
220 for (std::pair<std::string, bool> &loadInfo : loadInfos) {
221 if (!data.WriteString(loadInfo.first)) {
222 LOG_ERROR("Failed to write keyId.");
223 return EFM_ERR_IPC_WRITE_DATA;
224 }
225 if (!data.WriteBool(loadInfo.second)) {
226 LOG_ERROR("Failed to write load status.");
227 return EFM_ERR_IPC_WRITE_DATA;
228 }
229 }
230
231 MessageParcel reply;
232 MessageOption option;
233 sptr<IRemoteObject> remote = Remote();
234 if (remote == nullptr) {
235 LOG_ERROR("Remote service is null.");
236 return EFM_ERR_REMOTE_CONNECTION;
237 }
238
239 int32_t result = remote->SendRequest(
240 static_cast<int32_t>(EFMInterfaceCode::CHANGE_USER_APP_KEYS_LOAD_INFO), data, reply, option);
241 if (result != NO_ERROR) {
242 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
243 } else {
244 result = reply.ReadInt32();
245 }
246 return result;
247 }
248
SetFilePathPolicy()249 int32_t El5FilekeyManagerProxy::SetFilePathPolicy()
250 {
251 MessageParcel data;
252 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
253 LOG_ERROR("Failed to write WriteInterfaceToken.");
254 return EFM_ERR_IPC_WRITE_DATA;
255 }
256
257 MessageParcel reply;
258 MessageOption option;
259 sptr<IRemoteObject> remote = Remote();
260 if (remote == nullptr) {
261 LOG_ERROR("Remote service is null.");
262 return EFM_ERR_REMOTE_CONNECTION;
263 }
264
265 int32_t result = remote->SendRequest(
266 static_cast<int32_t>(EFMInterfaceCode::SET_FILE_PATH_POLICY), data, reply, option);
267 if (result != NO_ERROR) {
268 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
269 } else {
270 result = reply.ReadInt32();
271 }
272 return result;
273 }
274
RegisterCallback(const sptr<El5FilekeyCallbackInterface> & callback)275 int32_t El5FilekeyManagerProxy::RegisterCallback(const sptr<El5FilekeyCallbackInterface> &callback)
276 {
277 MessageParcel data;
278 if (!data.WriteInterfaceToken(El5FilekeyManagerInterface::GetDescriptor())) {
279 LOG_ERROR("Failed to write WriteInterfaceToken.");
280 return EFM_ERR_IPC_WRITE_DATA;
281 }
282 if (!data.WriteRemoteObject(callback->AsObject())) {
283 LOG_ERROR("Failed to write callback.");
284 return EFM_ERR_IPC_WRITE_DATA;
285 }
286
287 MessageParcel reply;
288 MessageOption option;
289 sptr<IRemoteObject> remote = Remote();
290 if (remote == nullptr) {
291 LOG_ERROR("Remote service is null.");
292 return EFM_ERR_REMOTE_CONNECTION;
293 }
294
295 int32_t result = remote->SendRequest(
296 static_cast<int32_t>(EFMInterfaceCode::REGISTER_CALLBACK), data, reply, option);
297 if (result != NO_ERROR) {
298 LOG_ERROR("SendRequest failed, result: %{public}d.", result);
299 } else {
300 result = reply.ReadInt32();
301 }
302 return result;
303 }
304 } // namespace AccessToken
305 } // namespace Security
306 } // namespace OHOS
307