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 "media_decrypt_module_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 #include "remote_request_code.h"
23 
24 namespace OHOS {
25 namespace DrmStandard {
MediaDecryptModuleServiceStub()26 MediaDecryptModuleServiceStub::MediaDecryptModuleServiceStub()
27 {
28 }
29 
~MediaDecryptModuleServiceStub()30 MediaDecryptModuleServiceStub::~MediaDecryptModuleServiceStub()
31 {
32     DRM_INFO_LOG("~MediaDecryptModuleServiceStub");
33 }
34 
MediaDecryptModuleClientDied(pid_t pid)35 void MediaDecryptModuleServiceStub::MediaDecryptModuleClientDied(pid_t pid)
36 {
37     DRM_ERR_LOG("MediaDecryptModule client has died, pid:%{public}d", pid);
38     std::lock_guard<std::recursive_mutex> lock(decryptModuleStubMutex_);
39     if (clientListener_ != nullptr && clientListener_->AsObject() != nullptr && deathRecipient_ != nullptr) {
40         DRM_DEBUG_LOG("MediaDecryptModuleServiceStub release listener!");
41         (void)clientListener_->AsObject()->RemoveDeathRecipient(deathRecipient_);
42         deathRecipient_ = nullptr;
43         clientListener_ = nullptr;
44     }
45 }
46 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)47 int32_t MediaDecryptModuleServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
48     MessageOption &option)
49 {
50     DRM_DEBUG_LOG("OnRemoteRequest, cmd = %{public}u", code);
51     if (data.ReadInterfaceToken() != GetDescriptor()) {
52         DRM_ERR_LOG("MediaDecryptModuleServiceStub: ReadInterfaceToken failed.");
53         return -1;
54     }
55     switch (code) {
56         case DECRYPT_MODULE_DECRYPT_DATA: {
57             DRM_DEBUG_LOG("DECRYPT_MODULE_DECRYPT_DATA enter.");
58             IMediaDecryptModuleService::CryptInfo cryptInfo;
59             bool secureDecodrtState = data.ReadBool();
60             cryptInfo.type = (OHOS::DrmStandard::IMediaDecryptModuleService::CryptAlgorithmType)data.ReadUint32();
61             uint32_t keyIdSize = data.ReadUint32();
62             DRM_CHECK_AND_RETURN_RET_LOG(keyIdSize < KEYID_MAX_LEN, DRM_MEMORY_ERROR,
63                 "The size of keyId is too large.");
64             if (keyIdSize != 0) {
65                 const uint8_t *keyIdBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(keyIdSize));
66                 if (keyIdBuf == nullptr) {
67                     DRM_ERR_LOG("DECRYPT_MODULE_DECRYPT_DATA read keyId failed.");
68                     return IPC_STUB_WRITE_PARCEL_ERR;
69                 }
70                 cryptInfo.keyId.assign(keyIdBuf, keyIdBuf + keyIdSize);
71             }
72             uint32_t ivSize = data.ReadUint32();
73             DRM_CHECK_AND_RETURN_RET_LOG(ivSize < IV_MAX_LEN, DRM_MEMORY_ERROR, "The size of iv is too large.");
74             if (ivSize != 0) {
75                 const uint8_t *ivBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(ivSize));
76                 if (ivBuf == nullptr) {
77                     DRM_ERR_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_DECRYPT_DATA read ivSize failed.");
78                     return IPC_STUB_WRITE_PARCEL_ERR;
79                 }
80                 cryptInfo.iv.assign(ivBuf, ivBuf + ivSize);
81             }
82             cryptInfo.pattern.encryptBlocks = data.ReadUint32();
83             cryptInfo.pattern.skipBlocks = data.ReadUint32();
84             uint32_t subSampleNumber = data.ReadUint32();
85             DRM_CHECK_AND_RETURN_RET_LOG(subSampleNumber < SUBSAMPLE_MAX_NUM, DRM_MEMORY_ERROR,
86                 "The number of subSample is too large.");
87             cryptInfo.subSample.resize(subSampleNumber);
88             for (uint32_t i = 0; i < subSampleNumber; i++) {
89                 cryptInfo.subSample[i].clearHeaderLen = data.ReadUint32();
90                 cryptInfo.subSample[i].payLoadLen = data.ReadUint32();
91             }
92             IMediaDecryptModuleService::DrmBuffer srcBuffer;
93             IMediaDecryptModuleService::DrmBuffer dstBuffer;
94             srcBuffer.bufferType  = data.ReadUint32();
95             srcBuffer.fd  = data.ReadFileDescriptor();
96             srcBuffer.bufferLen  = data.ReadUint32();
97             srcBuffer.allocLen  = data.ReadUint32();
98             srcBuffer.filledLen  = data.ReadUint32();
99             srcBuffer.offset  = data.ReadUint32();
100             srcBuffer.sharedMemType  = data.ReadUint32();
101             dstBuffer.bufferType  = data.ReadUint32();
102             dstBuffer.fd  = data.ReadFileDescriptor();
103             dstBuffer.bufferLen  = data.ReadUint32();
104             dstBuffer.allocLen  = data.ReadUint32();
105             dstBuffer.filledLen  = data.ReadUint32();
106             dstBuffer.offset  = data.ReadUint32();
107             dstBuffer.sharedMemType  = data.ReadUint32();
108             int32_t ret = DecryptMediaData(secureDecodrtState, cryptInfo, srcBuffer, dstBuffer);
109             if (ret != 0) {
110                 DRM_ERR_LOG("DecryptMediaData faild.");
111                 return ret;
112             }
113             return ret;
114         }
115         case DECRYPT_MODULE_RELEASE: {
116             DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_RELEASE enter.");
117             int32_t ret = Release();
118             return ret;
119         }
120         default: {
121             DRM_ERR_LOG("default case, need check Decrypt Module");
122             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
123         }
124     }
125     return -1;
126 }
127 } // DrmStandard
128 } // OHOS