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 "key_session_service_proxy.h"
17 #include "drm_log.h"
18 #include "remote_request_code.h"
19 #include "drm_error_code.h"
20
21 namespace OHOS {
22 namespace DrmStandard {
MediaKeySessionServiceProxy(const sptr<IRemoteObject> & impl)23 MediaKeySessionServiceProxy::MediaKeySessionServiceProxy(const sptr<IRemoteObject> &impl)
24 : IRemoteProxy<IMediaKeySessionService>(impl)
25 {
26 DRM_INFO_LOG("MediaKeySessionServiceProxy Initialized");
27 }
28
GetMediaDecryptModule(sptr<IMediaDecryptModuleService> & decryptModule)29 int32_t MediaKeySessionServiceProxy::GetMediaDecryptModule(sptr<IMediaDecryptModuleService> &decryptModule)
30 {
31 DRM_INFO_LOG("GetMediaDecryptModule enter.");
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35
36 if (!data.WriteInterfaceToken(GetDescriptor())) {
37 DRM_ERR_LOG("GetMediaDecryptModule Write interface token failed.");
38 return IPC_PROXY_ERR;
39 }
40
41 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(GET_MEDIA_DECRYPT_MODULE, data, reply, option);
42 if (ret != DRM_OK) {
43 DRM_ERR_LOG("GetMediaDecryptModule failed, ret: %{public}d", ret);
44 return ret;
45 }
46
47 auto remoteObject = reply.ReadRemoteObject();
48 if (remoteObject != nullptr) {
49 decryptModule = iface_cast<IMediaDecryptModuleService>(remoteObject);
50 } else {
51 DRM_ERR_LOG("GetMediaDecryptModule decryptModule is nullptr");
52 ret = IPC_PROXY_ERR;
53 }
54 return ret;
55 }
56
GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)57 int32_t MediaKeySessionServiceProxy::GetContentProtectionLevel(
58 IMediaKeySessionService::ContentProtectionLevel *securityLevel)
59 {
60 DRM_INFO_LOG("GetContentProtectionLevel enter.");
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option;
64
65 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
66 DRM_ERR_LOG("GetContentProtectionLevel Write interface token failed.");
67 return IPC_PROXY_ERR;
68 }
69
70 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_GETSECURITYLEVEL, data, reply, option);
71 if (ret != DRM_OK) {
72 DRM_ERR_LOG("GetContentProtectionLevel failed, ret: %{public}d", ret);
73 return ret;
74 }
75
76 *securityLevel = (IMediaKeySessionService::ContentProtectionLevel)reply.ReadInt32();
77
78 return ret;
79 }
80
Release()81 int32_t MediaKeySessionServiceProxy::Release()
82 {
83 DRM_INFO_LOG("Release enter.");
84 MessageParcel data;
85 MessageParcel reply;
86 MessageOption option;
87
88 if (!data.WriteInterfaceToken(GetDescriptor())) {
89 DRM_ERR_LOG("Release Write interface token failed.");
90 return IPC_PROXY_ERR;
91 }
92 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(KEY_SESSION_RELEASE, data, reply, option);
93 if (ret != DRM_OK) {
94 DRM_ERR_LOG("Release failed, ret: %{public}d", ret);
95 }
96 return ret;
97 }
98
GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo & licenseRequestInfo,IMediaKeySessionService::MediaKeyRequest & licenseRequest)99 int32_t MediaKeySessionServiceProxy::GenerateMediaKeyRequest(
100 IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo,
101 IMediaKeySessionService::MediaKeyRequest &licenseRequest)
102 {
103 DRM_INFO_LOG("GenerateMediaKeyRequest enter.");
104 MessageParcel data;
105 MessageParcel reply;
106 MessageOption option;
107 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
108 DRM_ERR_LOG("GenerateMediaKeyRequest Write interface token failed.");
109 return IPC_PROXY_ERR;
110 }
111
112 if (!data.WriteInt32(licenseRequestInfo.optionalData.size())) {
113 DRM_ERR_LOG("GenerateMediaKeyRequest Write optionalData.size failed.");
114 return IPC_PROXY_ERR;
115 }
116 DRM_CHECK_AND_RETURN_RET_LOG(licenseRequestInfo.optionalData.size() < DATA_MAX_LEN, DRM_MEMORY_ERROR,
117 "The size of optionalData is too large.");
118 for (auto optionalData : licenseRequestInfo.optionalData) {
119 data.WriteString(optionalData.first);
120 data.WriteString(optionalData.second);
121 }
122 if (!data.WriteInt32(licenseRequestInfo.mediaKeyType)) {
123 DRM_ERR_LOG("GenerateMediaKeyRequest Write licenseType failed.");
124 return IPC_PROXY_ERR;
125 }
126 if (!data.WriteString(licenseRequestInfo.mimeType)) {
127 DRM_ERR_LOG("GenerateMediaKeyRequest Write mimeType failed.");
128 return IPC_PROXY_ERR;
129 }
130 if (!data.WriteInt32(licenseRequestInfo.initData.size())) {
131 DRM_ERR_LOG("GenerateMediaKeyRequest Write initData.size failed.");
132 return IPC_PROXY_ERR;
133 }
134 DRM_CHECK_AND_RETURN_RET_LOG(licenseRequestInfo.initData.size() < DATA_MAX_LEN, DRM_MEMORY_ERROR,
135 "The size of initData is too large.");
136 if (licenseRequestInfo.initData.size() != 0) {
137 if (!data.WriteBuffer(licenseRequestInfo.initData.data(), licenseRequestInfo.initData.size())) {
138 DRM_ERR_LOG("GenerateMediaKeyRequest Write initData.size failed.");
139 return IPC_PROXY_ERR;
140 }
141 }
142 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST, data,
143 reply, option);
144 if (ret != DRM_OK) {
145 DRM_ERR_LOG("GenerateMediaKeyRequest failed, errcode: %{public}d", ret);
146 return ret;
147 }
148 licenseRequest.mDefaultURL = reply.ReadString();
149 licenseRequest.requestType = (IMediaKeySessionService::RequestType)reply.ReadInt32();
150 int32_t dataSize = reply.ReadInt32();
151 DRM_CHECK_AND_RETURN_RET_LOG(dataSize < DATA_MAX_LEN, DRM_MEMORY_ERROR, "The size of initData is too large.");
152 if (dataSize != 0) {
153 const uint8_t *mDataBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(dataSize));
154 if (mDataBuf == nullptr) {
155 DRM_ERR_LOG("read licenseRequest.mData failed.");
156 return IPC_STUB_WRITE_PARCEL_ERR;
157 }
158 licenseRequest.mData.assign(mDataBuf, mDataBuf + dataSize);
159 }
160 return ret;
161 }
162
ProcessMediaKeyResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & licenseResponse)163 int32_t MediaKeySessionServiceProxy::ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId,
164 std::vector<uint8_t> &licenseResponse)
165 {
166 DRM_INFO_LOG("ProcessMediaKeyResponse enter.");
167 MessageParcel data;
168 MessageParcel reply;
169 MessageOption option;
170
171 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
172 DRM_ERR_LOG("ProcessMediaKeyResponse Write interface token failed.");
173 return IPC_PROXY_ERR;
174 }
175 if (!data.WriteInt32(licenseResponse.size())) {
176 DRM_ERR_LOG("ProcessMediaKeyResponse Write licenseResponse size failed.");
177 return IPC_PROXY_ERR;
178 }
179 DRM_CHECK_AND_RETURN_RET_LOG(licenseResponse.size() < RESPONSE_MAX_LEN, DRM_MEMORY_ERROR,
180 "The size of response is too large.");
181 if (licenseResponse.size() != 0) {
182 if (!data.WriteBuffer(licenseResponse.data(), licenseResponse.size())) {
183 DRM_ERR_LOG("ProcessMediaKeyResponse write licenseResponse failed.");
184 return IPC_PROXY_ERR;
185 }
186 }
187 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE, data,
188 reply, option);
189 if (ret != DRM_OK) {
190 DRM_ERR_LOG("ProcessMediaKeyResponse failed, ret: %{public}d", ret);
191 return ret;
192 }
193 int32_t licenseIdSize = reply.ReadInt32();
194 DRM_CHECK_AND_RETURN_RET_LOG(licenseIdSize < LICENSEID_MAX_LEN, DRM_MEMORY_ERROR,
195 "The size of licenseId is too large.");
196 if (licenseIdSize != 0) {
197 const uint8_t *licenseIdBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(licenseIdSize));
198 if (licenseIdBuf == nullptr) {
199 DRM_ERR_LOG("ProcessMediaKeyResponse read licenseId failed.");
200 return IPC_STUB_WRITE_PARCEL_ERR;
201 }
202 licenseId.assign(licenseIdBuf, licenseIdBuf + licenseIdSize);
203 }
204 return ret;
205 }
206
GenerateOfflineReleaseRequest(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseRequest)207 int32_t MediaKeySessionServiceProxy::GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId,
208 std::vector<uint8_t> &releaseRequest)
209 {
210 DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
211 MessageParcel data;
212 MessageParcel reply;
213 MessageOption option;
214
215 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
216 DRM_ERR_LOG("GenerateOfflineReleaseRequest Write interface token failed.");
217 return IPC_PROXY_ERR;
218 }
219 if (!data.WriteInt32(licenseId.size())) {
220 DRM_ERR_LOG("GenerateOfflineReleaseRequest Write licenseId size failed.");
221 return IPC_PROXY_ERR;
222 }
223 DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_MEMORY_ERROR,
224 "The size of licenseId is too large.");
225 if (licenseId.size() != 0) {
226 if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
227 DRM_ERR_LOG("ProcessMediaKeyResponse write licenseId failed.");
228 return IPC_PROXY_ERR;
229 }
230 }
231
232 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST,
233 data, reply, option);
234 if (ret != DRM_OK) {
235 DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, ret: %{public}d", ret);
236 return ret;
237 }
238 int32_t requestSize = reply.ReadInt32();
239 DRM_CHECK_AND_RETURN_RET_LOG(requestSize < REQUEST_MAX_LEN, DRM_MEMORY_ERROR, "The size of request is too large.");
240 if (requestSize != 0) {
241 const uint8_t *requestBuf = static_cast<const uint8_t *>(reply.ReadBuffer(requestSize));
242 if (requestBuf == nullptr) {
243 DRM_ERR_LOG("ProcessMediaKeyResponse read licenseId failed.");
244 return IPC_STUB_WRITE_PARCEL_ERR;
245 }
246 releaseRequest.assign(requestBuf, requestBuf + requestSize);
247 }
248 return ret;
249 }
250
ProcessOfflineReleaseResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseReponse)251 int32_t MediaKeySessionServiceProxy::ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId,
252 std::vector<uint8_t> &releaseReponse)
253 {
254 DRM_INFO_LOG("ProcessOfflineReleaseResponse enter.");
255 MessageParcel data;
256 MessageParcel reply;
257 MessageOption option;
258
259 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
260 DRM_ERR_LOG("ProcessOfflineReleaseResponse Write interface token failed.");
261 return IPC_PROXY_ERR;
262 }
263
264 if (!data.WriteInt32(licenseId.size())) {
265 DRM_ERR_LOG("ProcessOfflineReleaseResponse Write licenseId size failed.");
266 return IPC_PROXY_ERR;
267 }
268 DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_MEMORY_ERROR,
269 "The size of licenseId is too large.");
270 if (licenseId.size() != 0) {
271 if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
272 DRM_ERR_LOG("ProcessOfflineReleaseResponse write licenseId failed.");
273 return IPC_PROXY_ERR;
274 }
275 }
276 if (!data.WriteInt32(releaseReponse.size())) {
277 DRM_ERR_LOG("ProcessOfflineReleaseResponse Write releaseReponse size failed.");
278 return IPC_PROXY_ERR;
279 }
280 DRM_CHECK_AND_RETURN_RET_LOG(releaseReponse.size() < RESPONSE_MAX_LEN, DRM_MEMORY_ERROR,
281 "The size of response is too large.");
282 if (releaseReponse.size() != 0) {
283 if (!data.WriteBuffer(releaseReponse.data(), releaseReponse.size())) {
284 DRM_ERR_LOG("ProcessOfflineReleaseResponse write releaseReponse failed.");
285 return IPC_PROXY_ERR;
286 }
287 }
288 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE,
289 data, reply, option);
290 if (ret != DRM_OK) {
291 DRM_ERR_LOG("ProcessOfflineReleaseResponse failed, errcode: %{public}d", ret);
292 return ret;
293 }
294 return ret;
295 }
296
CheckMediaKeyStatus(std::map<std::string,std::string> & licenseStatus)297 int32_t MediaKeySessionServiceProxy::CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus)
298 {
299 DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
300 MessageParcel data;
301 MessageParcel reply;
302 MessageOption option;
303
304 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
305 DRM_ERR_LOG("GenerateOfflineReleaseRequest Write interface token failed.");
306 return IPC_PROXY_ERR;
307 }
308
309 int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS,
310 data, reply, option);
311 if (ret != DRM_OK) {
312 DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, errcode: %{public}d", ret);
313 return ret;
314 }
315 int32_t licenseStatusMapSize = reply.ReadInt32();
316 for (int32_t i = 0; i < licenseStatusMapSize; i++) {
317 std::string name = reply.ReadString();
318 std::string status = reply.ReadString();
319 licenseStatus.insert(std::make_pair(name, status));
320 }
321 return ret;
322 }
323
RestoreOfflineMediaKeys(std::vector<uint8_t> & licenseId)324 int32_t MediaKeySessionServiceProxy::RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId)
325 {
326 DRM_INFO_LOG("RestoreOfflineMediaKeys enter.");
327 MessageParcel data;
328 MessageParcel reply;
329 MessageOption option;
330
331 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
332 DRM_ERR_LOG("RestoreOfflineMediaKeys Write interface token failed.");
333 return IPC_PROXY_ERR;
334 }
335
336 if (!data.WriteInt32(licenseId.size())) {
337 DRM_ERR_LOG("RestoreOfflineMediaKeys Write licenseId size failed.");
338 return IPC_PROXY_ERR;
339 }
340 DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_MEMORY_ERROR,
341 "The size of licenseId is too large.");
342 if (licenseId.size() != 0) {
343 if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
344 DRM_ERR_LOG("RestoreOfflineMediaKeys write licenseId failed.");
345 return IPC_PROXY_ERR;
346 }
347 }
348
349 int32_t ret =
350 MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS, data, reply, option);
351 if (ret != DRM_OK) {
352 DRM_ERR_LOG("RestoreOfflineMediaKeys failed, errcode: %{public}d", ret);
353 return ret;
354 }
355 return ret;
356 }
357
ClearMediaKeys()358 int32_t MediaKeySessionServiceProxy::ClearMediaKeys()
359 {
360 DRM_INFO_LOG("ClearMediaKeys enter.");
361 MessageParcel data;
362 MessageParcel reply;
363 MessageOption option;
364
365 if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
366 DRM_ERR_LOG("ClearMediaKeys Write interface token failed.");
367 return IPC_PROXY_ERR;
368 }
369
370 int32_t ret =
371 MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_REMOVE_LICENSE, data, reply, option);
372 if (ret != DRM_OK) {
373 DRM_ERR_LOG("ClearMediaKeys failed, errcode: %{public}d", ret);
374 return ret;
375 }
376 return ret;
377 }
378
RequireSecureDecoderModule(std::string & mimeType,bool * status)379 int32_t MediaKeySessionServiceProxy::RequireSecureDecoderModule(std::string &mimeType, bool *status)
380 {
381 DRM_INFO_LOG("RequireSecureDecoderModule enter.");
382 MessageParcel data;
383 MessageParcel reply;
384 MessageOption option;
385
386 if (!data.WriteInterfaceToken(GetDescriptor())) {
387 DRM_ERR_LOG("RequireSecureDecoderModule Write interface token failed.");
388 return IPC_PROXY_ERR;
389 }
390
391 if (!data.WriteString(mimeType)) {
392 DRM_ERR_LOG("RequireSecureDecoderModule Write response failed.");
393 return IPC_PROXY_ERR;
394 }
395
396 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_REQUIRE_SECURE_DECODER, data, reply, option);
397 if (ret != DRM_OK) {
398 DRM_ERR_LOG("RequireSecureDecoderModule failed, errcode: %{public}d", ret);
399 return ret;
400 }
401 *status = reply.ReadBool();
402 return ret;
403 }
404
SetListenerObject(const sptr<IRemoteObject> & object)405 int32_t MediaKeySessionServiceProxy::SetListenerObject(const sptr<IRemoteObject> &object)
406 {
407 DRM_INFO_LOG("SetListenerObject enter.");
408 MessageParcel data;
409 MessageParcel reply;
410 MessageOption option;
411
412 data.WriteInterfaceToken(GetDescriptor());
413 (void)data.WriteRemoteObject(object);
414 int ret = Remote()->SendRequest(MEDIA_KEY_SESSION_SET_LISTENER_OBJ, data, reply, option);
415 if (ret != DRM_OK) {
416 DRM_ERR_LOG("Set listener obj failed, errcode: %{public}d", ret);
417 return IPC_PROXY_ERR;
418 }
419 return reply.ReadInt32();
420 }
421
SetCallback(sptr<IMediaKeySessionServiceCallback> & callback)422 int32_t MediaKeySessionServiceProxy::SetCallback(sptr<IMediaKeySessionServiceCallback> &callback)
423 {
424 DRM_INFO_LOG("SetCallback enter.");
425 MessageParcel data;
426 MessageParcel reply;
427 MessageOption option;
428
429 if (callback == nullptr) {
430 DRM_ERR_LOG("SetCallback callback is null");
431 return IPC_PROXY_ERR;
432 }
433
434 if (!data.WriteInterfaceToken(GetDescriptor())) {
435 DRM_ERR_LOG("SetCallback Write interface token failed.");
436 return IPC_PROXY_ERR;
437 }
438 if (!data.WriteRemoteObject(callback->AsObject())) {
439 DRM_ERR_LOG("SetCallback write CameraServiceCallback obj failed.");
440 return IPC_PROXY_ERR;
441 }
442
443 int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_SET_CALLBACK, data, reply, option);
444 if (ret != DRM_OK) {
445 DRM_ERR_LOG("SetCallback failed, errcode: %{public}d", ret);
446 }
447 return ret;
448 }
449 } // DrmStandard
450 } // OHOS