• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 != 0) {
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 != 0) {
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 != 0) {
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_INNER_ERR_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_INNER_ERR_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 != 0) {
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_INNER_ERR_MEMORY_ERROR,
152         "The size of initData is too large.");
153     if (dataSize != 0) {
154         const uint8_t *mDataBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(dataSize));
155         if (mDataBuf == nullptr) {
156             DRM_ERR_LOG("read licenseRequest.mData failed.");
157             return IPC_STUB_WRITE_PARCEL_ERR;
158         }
159         licenseRequest.mData.assign(mDataBuf, mDataBuf + dataSize);
160     }
161     return ret;
162 }
163 
ProcessMediaKeyResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & licenseResponse)164 int32_t MediaKeySessionServiceProxy::ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId,
165     std::vector<uint8_t> &licenseResponse)
166 {
167     DRM_INFO_LOG("ProcessMediaKeyResponse enter.");
168     MessageParcel data;
169     MessageParcel reply;
170     MessageOption option;
171 
172     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
173         DRM_ERR_LOG("ProcessMediaKeyResponse Write interface token failed.");
174         return IPC_PROXY_ERR;
175     }
176     if (!data.WriteInt32(licenseResponse.size())) {
177         DRM_ERR_LOG("ProcessMediaKeyResponse Write licenseResponse size failed.");
178         return IPC_PROXY_ERR;
179     }
180     DRM_CHECK_AND_RETURN_RET_LOG(licenseResponse.size() < RESPONSE_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
181         "The size of response is too large.");
182     if (licenseResponse.size() != 0) {
183         if (!data.WriteBuffer(licenseResponse.data(), licenseResponse.size())) {
184             DRM_ERR_LOG("ProcessMediaKeyResponse write licenseResponse failed.");
185             return IPC_PROXY_ERR;
186         }
187     }
188     int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE, data,
189         reply, option);
190     if (ret != 0) {
191         DRM_ERR_LOG("ProcessMediaKeyResponse failed, ret: %{public}d", ret);
192         return ret;
193     }
194     int32_t licenseIdSize = reply.ReadInt32();
195     DRM_CHECK_AND_RETURN_RET_LOG(licenseIdSize < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
196         "The size of licenseId is too large.");
197     if (licenseIdSize != 0) {
198         const uint8_t *licenseIdBuf = static_cast<const uint8_t *>(reply.ReadUnpadBuffer(licenseIdSize));
199         if (licenseIdBuf == nullptr) {
200             DRM_ERR_LOG("ProcessMediaKeyResponse read licenseId failed.");
201             return IPC_STUB_WRITE_PARCEL_ERR;
202         }
203         licenseId.assign(licenseIdBuf, licenseIdBuf + licenseIdSize);
204     }
205     return ret;
206 }
207 
GenerateOfflineReleaseRequest(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseRequest)208 int32_t MediaKeySessionServiceProxy::GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId,
209     std::vector<uint8_t> &releaseRequest)
210 {
211     DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
212     MessageParcel data;
213     MessageParcel reply;
214     MessageOption option;
215 
216     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
217         DRM_ERR_LOG("GenerateOfflineReleaseRequest Write interface token failed.");
218         return IPC_PROXY_ERR;
219     }
220     if (!data.WriteInt32(licenseId.size())) {
221         DRM_ERR_LOG("GenerateOfflineReleaseRequest Write licenseId size failed.");
222         return IPC_PROXY_ERR;
223     }
224     DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
225         "The size of licenseId is too large.");
226     if (licenseId.size() != 0) {
227         if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
228             DRM_ERR_LOG("ProcessMediaKeyResponse write licenseId failed.");
229             return IPC_PROXY_ERR;
230         }
231     }
232 
233     int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST,
234         data, reply, option);
235     if (ret != 0) {
236         DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, ret: %{public}d", ret);
237         return ret;
238     }
239     int32_t requestSize = reply.ReadInt32();
240     DRM_CHECK_AND_RETURN_RET_LOG(requestSize < REQUEST_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
241         "The size of request is too large.");
242     if (requestSize != 0) {
243         const uint8_t *requestBuf = static_cast<const uint8_t *>(reply.ReadBuffer(requestSize));
244         if (requestBuf == nullptr) {
245             DRM_ERR_LOG("ProcessMediaKeyResponse read licenseId failed.");
246             return IPC_STUB_WRITE_PARCEL_ERR;
247         }
248         releaseRequest.assign(requestBuf, requestBuf + requestSize);
249     }
250     return ret;
251 }
252 
ProcessOfflineReleaseResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseReponse)253 int32_t MediaKeySessionServiceProxy::ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId,
254     std::vector<uint8_t> &releaseReponse)
255 {
256     DRM_INFO_LOG("ProcessOfflineReleaseResponse enter.");
257     MessageParcel data;
258     MessageParcel reply;
259     MessageOption option;
260 
261     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
262         DRM_ERR_LOG("ProcessOfflineReleaseResponse Write interface token failed.");
263         return IPC_PROXY_ERR;
264     }
265 
266     if (!data.WriteInt32(licenseId.size())) {
267         DRM_ERR_LOG("ProcessOfflineReleaseResponse Write licenseId size failed.");
268         return IPC_PROXY_ERR;
269     }
270     DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
271         "The size of licenseId is too large.");
272     if (licenseId.size() != 0) {
273         if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
274             DRM_ERR_LOG("ProcessOfflineReleaseResponse write licenseId failed.");
275             return IPC_PROXY_ERR;
276         }
277     }
278     if (!data.WriteInt32(releaseReponse.size())) {
279         DRM_ERR_LOG("ProcessOfflineReleaseResponse Write releaseReponse size failed.");
280         return IPC_PROXY_ERR;
281     }
282     DRM_CHECK_AND_RETURN_RET_LOG(releaseReponse.size() < RESPONSE_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
283         "The size of response is too large.");
284     if (releaseReponse.size() != 0) {
285         if (!data.WriteBuffer(releaseReponse.data(), releaseReponse.size())) {
286             DRM_ERR_LOG("ProcessOfflineReleaseResponse write releaseReponse failed.");
287             return IPC_PROXY_ERR;
288         }
289     }
290     int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE,
291         data, reply, option);
292     if (ret != 0) {
293         DRM_ERR_LOG("ProcessOfflineReleaseResponse failed, errcode: %{public}d", ret);
294         return ret;
295     }
296     return ret;
297 }
298 
CheckMediaKeyStatus(std::map<std::string,std::string> & licenseStatus)299 int32_t MediaKeySessionServiceProxy::CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus)
300 {
301     DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
302     MessageParcel data;
303     MessageParcel reply;
304     MessageOption option;
305 
306     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
307         DRM_ERR_LOG("GenerateOfflineReleaseRequest Write interface token failed.");
308         return IPC_PROXY_ERR;
309     }
310 
311     int32_t ret = MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS,
312         data, reply, option);
313     if (ret != 0) {
314         DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, errcode: %{public}d", ret);
315         return ret;
316     }
317     int32_t licenseStatusMapSize = reply.ReadInt32();
318     DRM_CHECK_AND_RETURN_RET_LOG(licenseStatusMapSize <= MAX_MEDIA_KEY_STATUS_NUMBER, DRM_INNER_ERR_MEMORY_ERROR,
319         "The number of license status is too large.");
320     for (int32_t i = 0; i < licenseStatusMapSize; i++) {
321         std::string name = reply.ReadString();
322         std::string status = reply.ReadString();
323         licenseStatus.insert(std::make_pair(name, status));
324     }
325     return ret;
326 }
327 
RestoreOfflineMediaKeys(std::vector<uint8_t> & licenseId)328 int32_t MediaKeySessionServiceProxy::RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId)
329 {
330     DRM_INFO_LOG("RestoreOfflineMediaKeys enter.");
331     MessageParcel data;
332     MessageParcel reply;
333     MessageOption option;
334 
335     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
336         DRM_ERR_LOG("RestoreOfflineMediaKeys Write interface token failed.");
337         return IPC_PROXY_ERR;
338     }
339 
340     if (!data.WriteInt32(licenseId.size())) {
341         DRM_ERR_LOG("RestoreOfflineMediaKeys Write licenseId size failed.");
342         return IPC_PROXY_ERR;
343     }
344     DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
345         "The size of licenseId is too large.");
346     if (licenseId.size() != 0) {
347         if (!data.WriteBuffer(licenseId.data(), licenseId.size())) {
348             DRM_ERR_LOG("RestoreOfflineMediaKeys write licenseId failed.");
349             return IPC_PROXY_ERR;
350         }
351     }
352 
353     int32_t ret =
354         MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS, data, reply, option);
355     if (ret != 0) {
356         DRM_ERR_LOG("RestoreOfflineMediaKeys failed, errcode: %{public}d", ret);
357         return ret;
358     }
359     return ret;
360 }
361 
ClearMediaKeys()362 int32_t MediaKeySessionServiceProxy::ClearMediaKeys()
363 {
364     DRM_INFO_LOG("ClearMediaKeys enter.");
365     MessageParcel data;
366     MessageParcel reply;
367     MessageOption option;
368 
369     if (!data.WriteInterfaceToken(MediaKeySessionServiceProxy::GetDescriptor())) {
370         DRM_ERR_LOG("ClearMediaKeys Write interface token failed.");
371         return IPC_PROXY_ERR;
372     }
373 
374     int32_t ret =
375         MediaKeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_REMOVE_LICENSE, data, reply, option);
376     if (ret != 0) {
377         DRM_ERR_LOG("ClearMediaKeys failed, errcode: %{public}d", ret);
378         return ret;
379     }
380     return ret;
381 }
382 
RequireSecureDecoderModule(std::string & mimeType,bool * status)383 int32_t MediaKeySessionServiceProxy::RequireSecureDecoderModule(std::string &mimeType, bool *status)
384 {
385     DRM_INFO_LOG("RequireSecureDecoderModule enter.");
386     MessageParcel data;
387     MessageParcel reply;
388     MessageOption option;
389 
390     if (!data.WriteInterfaceToken(GetDescriptor())) {
391         DRM_ERR_LOG("RequireSecureDecoderModule Write interface token failed.");
392         return IPC_PROXY_ERR;
393     }
394 
395     if (!data.WriteString(mimeType)) {
396         DRM_ERR_LOG("RequireSecureDecoderModule Write response failed.");
397         return IPC_PROXY_ERR;
398     }
399 
400     int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_REQUIRE_SECURE_DECODER, data, reply, option);
401     if (ret != 0) {
402         DRM_ERR_LOG("RequireSecureDecoderModule failed, errcode: %{public}d", ret);
403         return ret;
404     }
405     *status = reply.ReadBool();
406     return ret;
407 }
408 
SetCallback(sptr<IMediaKeySessionServiceCallback> & callback)409 int32_t MediaKeySessionServiceProxy::SetCallback(sptr<IMediaKeySessionServiceCallback> &callback)
410 {
411     DRM_INFO_LOG("SetCallback enter.");
412     MessageParcel data;
413     MessageParcel reply;
414     MessageOption option;
415 
416     if (callback == nullptr) {
417         DRM_ERR_LOG("SetCallback callback is null");
418         return IPC_PROXY_ERR;
419     }
420 
421     if (!data.WriteInterfaceToken(GetDescriptor())) {
422         DRM_ERR_LOG("SetCallback Write interface token failed.");
423         return IPC_PROXY_ERR;
424     }
425     if (!data.WriteRemoteObject(callback->AsObject())) {
426         DRM_ERR_LOG("SetCallback write CameraServiceCallback obj failed.");
427         return IPC_PROXY_ERR;
428     }
429 
430     int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_SET_CALLBACK, data, reply, option);
431     if (ret != 0) {
432         DRM_ERR_LOG("SetCallback failed, errcode: %{public}d", ret);
433     }
434     return ret;
435 }
436 } // DrmStandard
437 } // OHOS