• 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_stub.h"
17 #include "drm_error_code.h"
18 #include "drm_log.h"
19 #include "ipc_skeleton.h"
20 #include "xcollie/xcollie.h"
21 #include "xcollie/xcollie_define.h"
22 
23 namespace OHOS {
24 namespace DrmStandard {
25 using ProcessRemoteRequestFunc = int32_t (*)(MediaKeySessionServiceStub *stub, MessageParcel &data,
26     MessageParcel &reply, MessageOption &option);
27 struct ProcessRemoteRequestFuncArray {
28     OHOS::DrmStandard::MediaKeySessionServiceRequestCode requestCode;
29     ProcessRemoteRequestFunc processFunc;
30 };
31 
32 static int32_t ProcessGetMediaDecryptModule(MediaKeySessionServiceStub *stub, MessageParcel &data,
33     MessageParcel &reply, MessageOption &option);
34 
35 static int32_t ProcessReleaseKeySession(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
36     MessageOption &option);
37 
38 static int32_t ProcessMediaKeyRequest(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
39     MessageOption &option);
40 
41 static int32_t ProcessMediaKeyResponse(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
42     MessageOption &option);
43 
44 static int32_t ProcessOfflineReleaseRequest(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
45     MessageOption &option);
46 
47 static int32_t ProcessOfflineReleaseResponse(MediaKeySessionServiceStub *stub, MessageParcel &data,
48     MessageParcel &reply, MessageOption &option);
49 
50 static int32_t ProcessCheckMediaKeyStatus(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
51     MessageOption &option);
52 
53 static int32_t ProcessRestoreOfflineMediaKey(MediaKeySessionServiceStub *stub, MessageParcel &data,
54     MessageParcel &reply, MessageOption &option);
55 
56 static int32_t ProcessClearMediaKeys(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
57     MessageOption &option);
58 
59 static int32_t ProcessSetCallback(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
60     MessageOption &option);
61 
62 static int32_t ProcessRequireSecureDecoder(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
63     MessageOption &option);
64 
65 static int32_t ProcessGetContentProtectionLevel(MediaKeySessionServiceStub *stub, MessageParcel &data,
66     MessageParcel &reply, MessageOption &option);
67 
68 static struct ProcessRemoteRequestFuncArray g_mediaKeySessionServiceStubRequestProcessFunc[] = {
69     {GET_MEDIA_DECRYPT_MODULE, ProcessGetMediaDecryptModule},
70     {KEY_SESSION_RELEASE, ProcessReleaseKeySession},
71     {MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST, ProcessMediaKeyRequest},
72     {MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE, ProcessMediaKeyResponse},
73     {MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST, ProcessOfflineReleaseRequest},
74     {MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE, ProcessOfflineReleaseResponse},
75     {MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS, ProcessCheckMediaKeyStatus},
76     {MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS, ProcessRestoreOfflineMediaKey},
77     {MEDIA_KEY_SESSION_REMOVE_LICENSE, ProcessClearMediaKeys},
78     {MEDIA_KEY_SESSION_SET_CALLBACK, ProcessSetCallback},
79     {MEDIA_KEY_SESSION_REQUIRE_SECURE_DECODER, ProcessRequireSecureDecoder},
80     {MEDIA_KEY_SESSION_GETSECURITYLEVEL, ProcessGetContentProtectionLevel},
81 };
82 
ProcessGetMediaDecryptModule(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)83 static int32_t ProcessGetMediaDecryptModule(MediaKeySessionServiceStub *stub, MessageParcel &data,
84     MessageParcel &reply, MessageOption &option)
85 {
86     DRM_INFO_LOG("ProcessGetMediaDecryptModule enter.");
87     sptr<IMediaDecryptModuleService> decryptModuleServiceProxy = nullptr;
88     int32_t ret = stub->GetMediaDecryptModule(decryptModuleServiceProxy);
89     if (ret != 0) {
90         DRM_ERR_LOG("GetMediaDecryptModule failed. errCode:%{public}d", ret);
91         return ret;
92     }
93     if (!reply.WriteRemoteObject(decryptModuleServiceProxy->AsObject())) {
94         DRM_ERR_LOG("Write MediaDecryptModule obj failed.");
95         return IPC_STUB_WRITE_PARCEL_ERR;
96     }
97     return 0;
98 }
99 
ProcessReleaseKeySession(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)100 static int32_t ProcessReleaseKeySession(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
101     MessageOption &option)
102 {
103     DRM_INFO_LOG("ProcessReleaseKeySession enter.");
104     int32_t ret = stub->Release();
105     return ret;
106 }
107 
ProcessMediaKeyRequest(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)108 static int32_t ProcessMediaKeyRequest(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
109     MessageOption &option)
110 {
111     DRM_INFO_LOG("ProcessMediaKeyRequest enter.");
112     IMediaKeySessionService::MediaKeyRequestInfo licenseRequestInfo;
113     IMediaKeySessionService::MediaKeyRequest licenseRequest;
114     int32_t optionalDataMapSize = data.ReadInt32();
115     DRM_CHECK_AND_RETURN_RET_LOG(optionalDataMapSize < DATA_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
116         "The size of optionalData is too large.");
117     for (int32_t i = 0; i < optionalDataMapSize; i++) {
118         std::string name = data.ReadString();
119         std::string value = data.ReadString();
120         licenseRequestInfo.optionalData.insert(std::make_pair(name, value));
121     }
122     licenseRequestInfo.mediaKeyType = (OHOS::DrmStandard::IMediaKeySessionService::MediaKeyType)data.ReadInt32();
123     licenseRequestInfo.mimeType = data.ReadString();
124     int32_t initDataSize = data.ReadInt32();
125     DRM_CHECK_AND_RETURN_RET_LOG(initDataSize < DATA_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
126         "The size of initData is too large.");
127     if (initDataSize != 0) {
128         const uint8_t *initDataBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(initDataSize));
129         if (initDataBuf == nullptr) {
130             DRM_ERR_LOG("ReadUnpadBuffer failed.");
131             return IPC_STUB_WRITE_PARCEL_ERR;
132         }
133         licenseRequestInfo.initData.assign(initDataBuf, initDataBuf + initDataSize);
134     }
135     int32_t ret = stub->GenerateMediaKeyRequest(licenseRequestInfo, licenseRequest);
136     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "GenerateMediaKeyRequest faild, errCode:%{public}d", ret);
137     if (!reply.WriteString(licenseRequest.mDefaultURL)) {
138         DRM_ERR_LOG("Write licenseRequest.mDefaultURL GenerateMediaKeyRequest failed.");
139         return IPC_STUB_WRITE_PARCEL_ERR;
140     }
141 
142     if (!reply.WriteInt32(licenseRequest.requestType)) {
143         DRM_ERR_LOG("Write requestType GenerateMediaKeyRequest failed.");
144         return IPC_STUB_WRITE_PARCEL_ERR;
145     }
146     if (!reply.WriteInt32(licenseRequest.mData.size())) {
147         DRM_ERR_LOG("Write licenseRequestmDatasize GenerateMediaKeyRequest failed.");
148         return IPC_STUB_WRITE_PARCEL_ERR;
149     }
150     DRM_CHECK_AND_RETURN_RET_LOG(licenseRequest.mData.size() < REQUEST_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
151         "The size of mData is too large.");
152     if (licenseRequest.mData.size() != 0) {
153         if (!reply.WriteBuffer(licenseRequest.mData.data(), licenseRequest.mData.size())) {
154             DRM_ERR_LOG("GenerateMediaKeyRequest write mData failed.");
155             return IPC_STUB_WRITE_PARCEL_ERR;
156         }
157     }
158     return ret;
159 }
160 
ProcessMediaKeyResponse(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)161 static int32_t ProcessMediaKeyResponse(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
162     MessageOption &option)
163 {
164     DRM_INFO_LOG("ProcessMediaKeyResponse enter.");
165     std::vector<uint8_t> response;
166     std::vector<uint8_t> licenseId;
167     int32_t responseSize = data.ReadInt32();
168     DRM_CHECK_AND_RETURN_RET_LOG(responseSize < RESPONSE_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
169         "The size of response is too large.");
170     if (responseSize != 0) {
171         const uint8_t *responseBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(responseSize));
172         if (responseBuf == nullptr) {
173             DRM_ERR_LOG("ProcessMediaKeyResponse read response failed.");
174             return IPC_STUB_WRITE_PARCEL_ERR;
175         }
176         response.assign(responseBuf, responseBuf + responseSize);
177     }
178     int32_t ret = stub->ProcessMediaKeyResponse(licenseId, response);
179     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ProcessMediaKeyResponse faild, ret:%{public}d", ret);
180     if (!reply.WriteInt32(licenseId.size())) {
181         DRM_ERR_LOG("GenerateMediaKeyRequest Write licenseId.size failed.");
182         return IPC_STUB_WRITE_PARCEL_ERR;
183     }
184     DRM_CHECK_AND_RETURN_RET_LOG(licenseId.size() < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
185         "The size of licenseId is too large.");
186     if (licenseId.size() != 0) {
187         if (!reply.WriteBuffer(licenseId.data(), licenseId.size())) {
188             DRM_ERR_LOG("GenerateMediaKeyRequest write licenseId failed.");
189             return IPC_STUB_WRITE_PARCEL_ERR;
190         }
191     }
192     return ret;
193 }
194 
ProcessOfflineReleaseRequest(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)195 static int32_t ProcessOfflineReleaseRequest(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
196     MessageOption &option)
197 {
198     DRM_INFO_LOG("ProcessOfflineReleaseRequest enter.");
199     std::vector<uint8_t> licenseId;
200     int32_t licenseIdSize = data.ReadInt32();
201     DRM_CHECK_AND_RETURN_RET_LOG(licenseIdSize < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
202         "The size of licenseId is too large.");
203     if (licenseIdSize != 0) {
204         const uint8_t *licenseIdBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(licenseIdSize));
205         if (licenseIdBuf == nullptr) {
206             DRM_ERR_LOG("ProcessOfflineReleaseRequest read licenseId failed.");
207             return IPC_STUB_WRITE_PARCEL_ERR;
208         }
209         licenseId.assign(licenseIdBuf, licenseIdBuf + licenseIdSize);
210     }
211     std::vector<uint8_t> releaseRequest;
212     int32_t ret = stub->GenerateOfflineReleaseRequest(licenseId, releaseRequest);
213     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "GenerateOfflineReleaseRequest faild, errCode:%{public}d", ret);
214     if (!reply.WriteInt32(releaseRequest.size())) {
215         DRM_ERR_LOG("Write releaseRequest.size failed.");
216         return IPC_STUB_WRITE_PARCEL_ERR;
217     }
218     DRM_CHECK_AND_RETURN_RET_LOG(releaseRequest.size() < REQUEST_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
219         "The size of releaseRequest is too large.");
220     if (releaseRequest.size() != 0) {
221         if (!reply.WriteBuffer(releaseRequest.data(), releaseRequest.size())) {
222             DRM_ERR_LOG("ProcessOfflineReleaseRequest Write licenseId failed.");
223             return IPC_STUB_WRITE_PARCEL_ERR;
224         }
225     }
226     return ret;
227 }
228 
ProcessOfflineReleaseResponse(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)229 static int32_t ProcessOfflineReleaseResponse(MediaKeySessionServiceStub *stub, MessageParcel &data,
230     MessageParcel &reply, MessageOption &option)
231 {
232     DRM_INFO_LOG("ProcessOfflineReleaseResponse enter.");
233     int32_t licenseIdSize = data.ReadInt32();
234     std::vector<uint8_t> licenseId;
235     DRM_CHECK_AND_RETURN_RET_LOG(licenseIdSize < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
236         "The size of licenseId is too large.");
237     const uint8_t *licenseIdBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(licenseIdSize));
238     if (licenseIdBuf == nullptr) {
239         DRM_ERR_LOG("ProcessOfflineReleaseResponse read licenseId failed.");
240         return IPC_STUB_WRITE_PARCEL_ERR;
241     }
242     licenseId.assign(licenseIdBuf, licenseIdBuf + licenseIdSize);
243     std::vector<uint8_t> response;
244     int32_t responseSize = data.ReadInt32();
245     DRM_CHECK_AND_RETURN_RET_LOG(responseSize < RESPONSE_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
246         "The size of response is too large.");
247     if (responseSize != 0) {
248         const uint8_t *responseBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(responseSize));
249         if (responseBuf == nullptr) {
250             DRM_ERR_LOG("ProcessOfflineReleaseResponse read response failed.");
251             return IPC_STUB_WRITE_PARCEL_ERR;
252         }
253         response.assign(responseBuf, responseBuf + responseSize);
254     }
255     int32_t ret = stub->ProcessOfflineReleaseResponse(licenseId, response);
256     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ProcessOfflineReleaseResponse faild, errCode:%{public}d", ret);
257     return ret;
258 }
259 
ProcessCheckMediaKeyStatus(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)260 static int32_t ProcessCheckMediaKeyStatus(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
261     MessageOption &option)
262 {
263     DRM_INFO_LOG("ProcessCheckMediaKeyStatus enter.");
264     std::map<std::string, std::string> licenseStatus;
265     int32_t ret = stub->CheckMediaKeyStatus(licenseStatus);
266     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "CheckMediaKeyStatus faild, errCode:%{public}d", ret);
267 
268     reply.WriteInt32(licenseStatus.size());
269     for (auto licenseStatu : licenseStatus) {
270         reply.WriteString(licenseStatu.first);
271         reply.WriteString(licenseStatu.second);
272     }
273     return ret;
274 }
275 
ProcessRestoreOfflineMediaKey(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)276 static int32_t ProcessRestoreOfflineMediaKey(MediaKeySessionServiceStub *stub, MessageParcel &data,
277     MessageParcel &reply, MessageOption &option)
278 {
279     DRM_INFO_LOG("ProcessRestoreOfflineMediaKey enter.");
280     std::vector<uint8_t> licenseId;
281     int32_t licenseIdSize = data.ReadInt32();
282     DRM_CHECK_AND_RETURN_RET_LOG(licenseIdSize < LICENSEID_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
283         "The size of licenseId is too large.");
284     if (licenseIdSize != 0) {
285         const uint8_t *licenseIdBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(licenseIdSize));
286         if (licenseIdBuf == nullptr) {
287             DRM_ERR_LOG("ProcessRestoreOfflineMediaKey read licenseId failed.");
288             return IPC_STUB_WRITE_PARCEL_ERR;
289         }
290         licenseId.assign(licenseIdBuf, licenseIdBuf + licenseIdSize);
291     }
292     int32_t ret = stub->RestoreOfflineMediaKeys(licenseId);
293     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "RestoreOfflineMediaKeys faild, errCode:%{public}d", ret);
294     return ret;
295 }
296 
ProcessClearMediaKeys(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)297 static int32_t ProcessClearMediaKeys(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
298     MessageOption &option)
299 {
300     DRM_INFO_LOG("ProcessClearMediaKeys enter.");
301     int32_t ret = stub->ClearMediaKeys();
302     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ClearMediaKeys faild, errCode:%{public}d", ret);
303     return ret;
304 }
305 
ProcessSetCallback(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)306 static int32_t ProcessSetCallback(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
307     MessageOption &option)
308 {
309     DRM_INFO_LOG("ProcessSetCallback enter.");
310     auto remoteObject = data.ReadRemoteObject();
311     if (remoteObject == nullptr) {
312         DRM_ERR_LOG("ProcessSetCallback ReadRemoteObject is null");
313         return IPC_STUB_INVALID_DATA_ERR;
314     }
315     auto callback = iface_cast<IMediaKeySessionServiceCallback>(remoteObject);
316     if (callback == nullptr) {
317         DRM_ERR_LOG("iface_cast cast nullptr");
318         return IPC_STUB_INVALID_DATA_ERR;
319     }
320     int32_t ret = stub->SetCallback(callback);
321     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "SetCallback faild, errCode:%{public}d", ret);
322     return ret;
323 }
324 
ProcessRequireSecureDecoder(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)325 static int32_t ProcessRequireSecureDecoder(MediaKeySessionServiceStub *stub, MessageParcel &data, MessageParcel &reply,
326     MessageOption &option)
327 {
328     DRM_INFO_LOG("ProcessRequireSecureDecoder enter.");
329     std::string mimeType = data.ReadString();
330     bool status;
331     int32_t ret = stub->RequireSecureDecoderModule(mimeType, &status);
332     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ProcessRequireSecureDecoder faild, errCode:%{public}d", ret);
333     reply.WriteBool(status);
334     return ret;
335 }
336 
MediaKeySessionClientDied(pid_t pid)337 void MediaKeySessionServiceStub::MediaKeySessionClientDied(pid_t pid)
338 {
339     DRM_ERR_LOG("MediaKeySession client has died, pid:%{public}d", pid);
340 }
341 
ProcessGetContentProtectionLevel(MediaKeySessionServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)342 static int32_t ProcessGetContentProtectionLevel(MediaKeySessionServiceStub *stub, MessageParcel &data,
343     MessageParcel &reply, MessageOption &option)
344 {
345     DRM_INFO_LOG("ProcessGetContentProtectionLevel enter.");
346     IMediaKeySessionService::ContentProtectionLevel securityLevel =
347         IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN;
348     int32_t ret = stub->GetContentProtectionLevel(&securityLevel);
349     DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ProcessGetContentProtectionLevel faild, errCode:%{public}d", ret);
350     if (!reply.WriteInt32(securityLevel)) {
351         DRM_ERR_LOG("Write GetContentProtectionLevel failed.");
352         return IPC_STUB_WRITE_PARCEL_ERR;
353     }
354     return ret;
355 }
356 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)357 int32_t MediaKeySessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
358     MessageOption &option)
359 {
360     DRM_INFO_LOG("OnRemoteRequest enter.");
361     int32_t ret = DRM_INNER_ERR_BASE;
362     DRM_DEBUG_LOG("OnRemoteRequest, cmd = %{public}u", code);
363     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " is keySessionServiceStub", FAKE_POINTER(this));
364 
365     DRM_CHECK_AND_RETURN_RET_LOG(data.ReadInterfaceToken() == GetDescriptor(), ret,
366         "ReadInterfaceToken failed.");
367 
368     DRM_CHECK_AND_RETURN_RET_LOG((code >= GET_MEDIA_DECRYPT_MODULE) && (code <= MEDIA_KEY_SESSION_GETSECURITYLEVEL),
369         IPCObjectStub::OnRemoteRequest(code, data, reply, option),
370         "code not match, need check MediaKeySessionServiceStub");
371     return g_mediaKeySessionServiceStubRequestProcessFunc[code].processFunc(this, data, reply, option);
372 }
373 } // DrmStandard
374 } // OHOS