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 "mediakeysystem_service_proxy.h"
17 #include "remote_request_code.h"
18 #include "drm_log.h"
19 #include "drm_error_code.h"
20
21 namespace OHOS {
22 namespace DrmStandard {
MediaKeySystemServiceProxy(const sptr<IRemoteObject> & impl)23 MediaKeySystemServiceProxy::MediaKeySystemServiceProxy(const sptr<IRemoteObject> &impl)
24 : IRemoteProxy<IMediaKeySystemService>(impl)
25 {
26 }
27
Release()28 int32_t MediaKeySystemServiceProxy::Release()
29 {
30 DRM_INFO_LOG("Release enter.");
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 DRM_ERR_LOG("Release Write interface token failed.");
37 return IPC_PROXY_ERR;
38 }
39 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_RELEASE, data, reply, option);
40 if (ret != 0) {
41 DRM_ERR_LOG("Release failed, ret: %{public}d", ret);
42 return ret;
43 }
44 return ret;
45 }
46
GenerateKeySystemRequest(std::vector<uint8_t> & request,std::string & defaultUrl)47 int32_t MediaKeySystemServiceProxy::GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl)
48 {
49 DRM_INFO_LOG("GenerateKeySystemRequest enter.");
50 MessageParcel data;
51 MessageParcel reply;
52 MessageOption option;
53
54 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
55 DRM_ERR_LOG("GenerateKeySystemRequest Write interface token failed.");
56 return IPC_PROXY_ERR;
57 }
58
59 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST, data, reply, option);
60 if (ret != 0) {
61 DRM_ERR_LOG("GenerateKeySystemRequest failed, errcode: %{public}d", ret);
62 return ret;
63 }
64
65 defaultUrl = reply.ReadString();
66 int32_t requestSize = reply.ReadInt32();
67 DRM_CHECK_AND_RETURN_RET_LOG(requestSize <= REQUEST_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
68 "The size of request is too large.");
69 if (requestSize != 0) {
70 const uint8_t *requestBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(requestSize));
71 if (requestBuf == nullptr) {
72 DRM_ERR_LOG("ProcessOfflineReleaseResponse read response failed.");
73 return IPC_PROXY_ERR;
74 }
75 request.assign(requestBuf, requestBuf + requestSize);
76 }
77 return ret;
78 }
79
ProcessKeySystemResponse(const std::vector<uint8_t> & response)80 int32_t MediaKeySystemServiceProxy::ProcessKeySystemResponse(const std::vector<uint8_t> &response)
81 {
82 DRM_INFO_LOG("ProcessKeySystemResponse enter.");
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option;
86
87 if (!data.WriteInterfaceToken(GetDescriptor())) {
88 DRM_ERR_LOG("ProcessKeySystemResponse Write interface token failed.");
89 return IPC_PROXY_ERR;
90 }
91
92 if (!data.WriteInt32(response.size())) {
93 DRM_ERR_LOG("ProcessKeySystemResponse Write response size failed.");
94 return IPC_PROXY_ERR;
95 }
96 DRM_CHECK_AND_RETURN_RET_LOG(response.size() < RESPONSE_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
97 "The size of response is too large.");
98 if (response.size() != 0) {
99 if (!data.WriteBuffer(response.data(), response.size())) {
100 DRM_ERR_LOG("RestoreOfflineMediaKeys write response failed.");
101 return IPC_PROXY_ERR;
102 }
103 }
104
105 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_PROCESS_KEYSYSTEM_RESPONSE, data, reply, option);
106 if (ret != 0) {
107 DRM_ERR_LOG("ProcessKeySystemResponse failed, errcode: %{public}d", ret);
108 return ret;
109 }
110 return ret;
111 }
112
GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)113 int32_t MediaKeySystemServiceProxy::GetMaxContentProtectionLevel(
114 IMediaKeySessionService::ContentProtectionLevel *securityLevel)
115 {
116 DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
117 MessageParcel data;
118 MessageParcel reply;
119 MessageOption option;
120
121 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
122 DRM_ERR_LOG("GetMaxContentProtectionLevel Write interface token failed.");
123 return IPC_PROXY_ERR;
124 }
125
126 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETMAXSECURITYLEVEL, data, reply, option);
127 if (ret != 0) {
128 DRM_ERR_LOG("GetMaxContentProtectionLevel failed, ret: %{public}d", ret);
129 return ret;
130 }
131
132 *securityLevel = (IMediaKeySessionService::ContentProtectionLevel)reply.ReadInt32();
133
134 return ret;
135 }
136
GetCertificateStatus(IMediaKeySystemService::CertificateStatus * certStatus)137 int32_t MediaKeySystemServiceProxy::GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus)
138 {
139 DRM_INFO_LOG("GetCertificateStatus enter.");
140 MessageParcel data;
141 MessageParcel reply;
142 MessageOption option;
143
144 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
145 DRM_ERR_LOG("GetCertificateStatus Write interface token failed.");
146 return IPC_PROXY_ERR;
147 }
148
149 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETCERTIFICATESTATUS, data, reply, option);
150 if (ret != 0) {
151 DRM_ERR_LOG("GetCertificateStatus failed, errcode: %{public}d", ret);
152 return ret;
153 }
154
155 *certStatus = (IMediaKeySystemService::CertificateStatus)reply.ReadInt32();
156
157 return ret;
158 }
159
SetConfigurationString(std::string & configName,std::string & value)160 int32_t MediaKeySystemServiceProxy::SetConfigurationString(std::string &configName, std::string &value)
161 {
162 DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s, value:%{public}s.",
163 configName.c_str(), value.c_str());
164 MessageParcel data;
165 MessageParcel reply;
166 MessageOption option;
167
168 if (!data.WriteInterfaceToken(GetDescriptor())) {
169 DRM_ERR_LOG("SetConfiguration Write interface token failed.");
170 return IPC_PROXY_ERR;
171 }
172
173 if (!data.WriteString(configName)) {
174 DRM_ERR_LOG("SetConfiguration Write configName failed.");
175 return IPC_PROXY_ERR;
176 }
177 if (!data.WriteString(value)) {
178 DRM_ERR_LOG("SetConfiguration Write value failed.");
179 return IPC_PROXY_ERR;
180 }
181
182 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_SETCONFIGURATION_STRING, data, reply, option);
183 if (ret != 0) {
184 DRM_ERR_LOG("SetConfiguration failed, errcode: %{public}d", ret);
185 return ret;
186 }
187 return ret;
188 }
189
GetConfigurationString(std::string & configName,std::string & value)190 int32_t MediaKeySystemServiceProxy::GetConfigurationString(std::string &configName, std::string &value)
191 {
192 DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
193 MessageParcel data;
194 MessageParcel reply;
195 MessageOption option;
196
197 if (!data.WriteInterfaceToken(GetDescriptor())) {
198 DRM_ERR_LOG("GetConfiguration Write interface token failed.");
199 return IPC_PROXY_ERR;
200 }
201
202 if (!data.WriteString(configName)) {
203 DRM_ERR_LOG("GetConfiguration Write configName failed.");
204 return IPC_PROXY_ERR;
205 }
206
207 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETCONFIGURATION_STRING, data, reply, option);
208 if (ret != 0) {
209 DRM_ERR_LOG("GetConfiguration failed, errcode: %{public}d", ret);
210 return ret;
211 }
212
213 value = reply.ReadString();
214
215 return ret;
216 }
217
SetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)218 int32_t MediaKeySystemServiceProxy::SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
219 {
220 DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s.", configName.c_str());
221 MessageParcel data;
222 MessageParcel reply;
223 MessageOption option;
224
225 if (!data.WriteInterfaceToken(GetDescriptor())) {
226 DRM_ERR_LOG("SetConfiguration Write interface token failed.");
227 return IPC_PROXY_ERR;
228 }
229
230 if (!data.WriteString(configName)) {
231 DRM_ERR_LOG("SetConfiguration Write configName failed.");
232 return IPC_PROXY_ERR;
233 }
234
235 if (!data.WriteInt32(value.size())) {
236 DRM_ERR_LOG("SetConfiguration Write value.size size failed.");
237 return IPC_PROXY_ERR;
238 }
239 DRM_CHECK_AND_RETURN_RET_LOG(value.size() < DATA_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
240 "The size of configuration value is too large.");
241 if (value.size() != 0) {
242 if (!data.WriteBuffer(value.data(), value.size())) {
243 DRM_ERR_LOG("SetConfiguration write value failed.");
244 return IPC_PROXY_ERR;
245 }
246 }
247
248 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_SETCONFIGURATION_BYTEARRAY, data, reply, option);
249 if (ret != 0) {
250 DRM_ERR_LOG("SetConfiguration failed, ret: %{public}d", ret);
251 return ret;
252 }
253 return ret;
254 }
255
GetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)256 int32_t MediaKeySystemServiceProxy::GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
257 {
258 DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
259 MessageParcel data;
260 MessageParcel reply;
261 MessageOption option;
262
263 if (!data.WriteInterfaceToken(GetDescriptor())) {
264 DRM_ERR_LOG("GetConfiguration Write interface token failed.");
265 return IPC_PROXY_ERR;
266 }
267
268 if (!data.WriteString(configName)) {
269 DRM_ERR_LOG("GetConfiguration Write configName failed.");
270 return IPC_PROXY_ERR;
271 }
272
273 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETCONFIGURATION_BYTEARRAY, data, reply, option);
274 if (ret != 0) {
275 DRM_ERR_LOG("GetConfiguration failed, errcode: %{public}d", ret);
276 return ret;
277 }
278
279 int32_t configSize = reply.ReadInt32();
280 DRM_CHECK_AND_RETURN_RET_LOG(configSize < DATA_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
281 "The size of configuration value is too large.");
282 if (configSize != 0) {
283 const uint8_t *valueBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(configSize));
284 if (valueBuf == nullptr) {
285 DRM_ERR_LOG("GetConfigurationByteArray read response failed.");
286 return IPC_STUB_WRITE_PARCEL_ERR;
287 }
288 value.assign(valueBuf, valueBuf + configSize);
289 }
290 return ret;
291 }
292
CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,sptr<IMediaKeySessionService> & keySessionProxy)293 int32_t MediaKeySystemServiceProxy::CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,
294 sptr<IMediaKeySessionService> &keySessionProxy)
295 {
296 DRM_INFO_LOG("CreateMediaKeySession enter, securityLevel:%{public}d.", securityLevel);
297 MessageParcel data;
298 MessageParcel reply;
299 MessageOption option;
300
301 if (!data.WriteInterfaceToken(GetDescriptor())) {
302 DRM_ERR_LOG("CreateMediaKeySession Write interface token failed.");
303 return IPC_PROXY_ERR;
304 }
305 if (!data.WriteInt32(securityLevel)) {
306 DRM_ERR_LOG("CreateMediaKeySession Write securityLevel failed.");
307 return IPC_PROXY_ERR;
308 }
309
310 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION, data, reply, option);
311 if (ret != 0) {
312 DRM_ERR_LOG("CreateMediaKeySession failed, errcode: %{public}d", ret);
313 return ret;
314 }
315
316 auto remoteObject = reply.ReadRemoteObject();
317 if (remoteObject != nullptr) {
318 keySessionProxy = iface_cast<IMediaKeySessionService>(remoteObject);
319 } else {
320 DRM_ERR_LOG("CreateMediaKeySession keySessionProxy is nullptr");
321 ret = IPC_PROXY_ERR;
322 }
323 return ret;
324 }
325
GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics)326 int32_t MediaKeySystemServiceProxy::GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics)
327 {
328 DRM_INFO_LOG("GetStatistics enter.");
329 MessageParcel data;
330 MessageParcel reply;
331 MessageOption option;
332
333 if (!data.WriteInterfaceToken(GetDescriptor())) {
334 DRM_ERR_LOG("GetStatistics Write interface token failed.");
335 return IPC_PROXY_ERR;
336 }
337
338 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETMETRIC, data, reply, option);
339 if (ret != 0) {
340 DRM_ERR_LOG("GetStatistics failed, errcode: %{public}d", ret);
341 return ret;
342 }
343 int32_t metricsSize = reply.ReadInt32();
344 DRM_CHECK_AND_RETURN_RET_LOG(metricsSize <= MAX_STATISTICS_NUMBER, DRM_INNER_ERR_MEMORY_ERROR,
345 "The amount of metric information is too large.");
346 for (int32_t i = 0; i < metricsSize; i++) {
347 IMediaKeySystemService::MetircKeyValue keyValue;
348 keyValue.name = reply.ReadString();
349 keyValue.value = reply.ReadString();
350 metrics.push_back(keyValue);
351 }
352 return ret;
353 }
354
GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> & licenseIds)355 int32_t MediaKeySystemServiceProxy::GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds)
356 {
357 DRM_INFO_LOG("GetOfflineMediaKeyIds enter.");
358 MessageParcel data;
359 MessageParcel reply;
360 MessageOption option;
361
362 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
363 DRM_ERR_LOG("GetOfflineMediaKeyIds Write interface token failed.");
364 return IPC_PROXY_ERR;
365 }
366 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GET_OFFLINELICENSEIDS, data, reply, option);
367 if (ret != 0) {
368 DRM_ERR_LOG("GetOfflineMediaKeyIds failed, errcode: %{public}d", ret);
369 return ret;
370 }
371 uint32_t licenseIdsSize = reply.ReadUint32();
372 DRM_CHECK_AND_RETURN_RET_LOG(licenseIdsSize <= MAX_OFFLINE_MEDIA_KEY_ID_NUMBER, DRM_INNER_ERR_MEMORY_ERROR,
373 "The amount of license is too large.");
374 licenseIds.resize(licenseIdsSize);
375 for (uint32_t i = 0; i < licenseIdsSize; i++) {
376 uint32_t licenseIdSize = reply.ReadUint32();
377 if (licenseIdSize == 0 || licenseIdSize > LICENSEID_MAX_LEN) {
378 continue;
379 }
380 licenseIds[i].resize(licenseIdSize);
381 const uint8_t *licenseIdBuf =
382 static_cast<const uint8_t *>(reply.ReadUnpadBuffer(licenseIdSize));
383 if (licenseIdBuf == nullptr) {
384 DRM_ERR_LOG("GetOfflineMediaKeyIds ReadUnpadBuffer failed.");
385 return IPC_PROXY_ERR;
386 }
387 std::copy(licenseIdBuf, licenseIdBuf + licenseIdSize, licenseIds[i].begin());
388 }
389 return ret;
390 }
391
GetOfflineMediaKeyStatus(std::vector<uint8_t> & licenseId,IMediaKeySessionService::OfflineMediaKeyStatus & status)392 int32_t MediaKeySystemServiceProxy::GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
393 IMediaKeySessionService::OfflineMediaKeyStatus &status)
394 {
395 DRM_INFO_LOG("GetOfflineMediaKeyStatus enter.");
396 MessageParcel data;
397 MessageParcel reply;
398 MessageOption option;
399
400 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
401 DRM_ERR_LOG("Write interface token failed.");
402 return IPC_PROXY_ERR;
403 }
404 if (!data.WriteInt32(licenseId.size())) {
405 DRM_ERR_LOG("Write licenseId size failed.");
406 return IPC_PROXY_ERR;
407 }
408 DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
409 "The size of licenseId is too large.");
410 if (licenseId.size() != 0) {
411 if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
412 DRM_ERR_LOG("write licenseId failed.");
413 return IPC_PROXY_ERR;
414 }
415 }
416
417 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GET_OFFLINEKEY_STATUS, data, reply, option);
418 if (ret != 0) {
419 DRM_ERR_LOG("SendRequest failed, errcode: %{public}d", ret);
420 return ret;
421 }
422 status = (IMediaKeySessionService::OfflineMediaKeyStatus)reply.ReadInt32();
423 return ret;
424 }
425
ClearOfflineMediaKeys(std::vector<uint8_t> & licenseId)426 int32_t MediaKeySystemServiceProxy::ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId)
427 {
428 DRM_INFO_LOG("ClearOfflineMediaKeys enter.");
429 MessageParcel data;
430 MessageParcel reply;
431 MessageOption option;
432
433 if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) {
434 DRM_ERR_LOG("Write interface token failed.");
435 return IPC_PROXY_ERR;
436 }
437
438 if (!data.WriteInt32(licenseId.size())) {
439 DRM_ERR_LOG("Write licenseId size failed.");
440 return IPC_PROXY_ERR;
441 }
442 DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
443 "The size of licenseId is too large.");
444 if (licenseId.size() != 0) {
445 if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
446 DRM_ERR_LOG("write licenseId failed.");
447 return IPC_PROXY_ERR;
448 }
449 }
450
451 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_REMOVE_OFFLINELICENSE, data, reply, option);
452 if (ret != 0) {
453 DRM_ERR_LOG("SendRequest failed, errcode: %{public}d", ret);
454 return ret;
455 }
456 return ret;
457 }
458
SetCallback(sptr<IMediaKeySystemServiceCallback> & callback)459 int32_t MediaKeySystemServiceProxy::SetCallback(sptr<IMediaKeySystemServiceCallback> &callback)
460 {
461 DRM_INFO_LOG("SetCallback enter.");
462 MessageParcel data;
463 MessageParcel reply;
464 MessageOption option;
465
466 DRM_CHECK_AND_RETURN_RET_LOG(callback != nullptr, IPC_PROXY_ERR, "callback is nullptr");
467
468 bool result = data.WriteInterfaceToken(GetDescriptor());
469 DRM_CHECK_AND_RETURN_RET_LOG(result, IPC_PROXY_ERR, "Write interface token failed.");
470 result = data.WriteRemoteObject(callback->AsObject());
471 DRM_CHECK_AND_RETURN_RET_LOG(result, IPC_PROXY_ERR, "write CameraServiceCallback obj failed.");
472
473 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SYSTEM_SETCALLBACK, data, reply, option);
474 if (ret != 0) {
475 DRM_ERR_LOG("SetCallback failed, errcode: %{public}d", ret);
476 }
477 return ret;
478 }
479 } // DrmStandard
480 } // OHOS