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