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 "ashmem.h"
17 #include "drm_error_code.h"
18 #include "media_decrypt_module_service_proxy.h"
19 
20 namespace OHOS {
21 namespace DrmStandard {
MediaDecryptModuleServiceProxy(const sptr<IRemoteObject> & impl)22 MediaDecryptModuleServiceProxy::MediaDecryptModuleServiceProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<IMediaDecryptModuleService>(impl)
24 {
25 }
26 
Release()27 int32_t MediaDecryptModuleServiceProxy::Release()
28 {
29     DRM_INFO_LOG("Release enter.");
30     MessageParcel data;
31     MessageParcel reply;
32     MessageOption option;
33 
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         DRM_ERR_LOG("Release Write interface token failed.");
36         return IPC_PROXY_ERR;
37     }
38 
39     int32_t ret = Remote()->SendRequest(DECRYPT_MODULE_RELEASE, data, reply, option);
40     if (ret != DRM_OK) {
41         DRM_ERR_LOG("Release failed, errcode: %{public}d", ret);
42         return ret;
43     }
44     return ret;
45 }
46 
ProcessDrmBuffer(MessageParcel & data,IMediaDecryptModuleService::DrmBuffer & srcBuffer,IMediaDecryptModuleService::DrmBuffer & dstBuffer)47 int32_t MediaDecryptModuleServiceProxy::ProcessDrmBuffer(MessageParcel &data,
48     IMediaDecryptModuleService::DrmBuffer &srcBuffer, IMediaDecryptModuleService::DrmBuffer &dstBuffer)
49 {
50     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.bufferType), IPC_PROXY_ERR,
51         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.bufferType failed.");
52     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteFileDescriptor(srcBuffer.fd), IPC_PROXY_ERR,
53         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.fd failed.");
54     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.bufferLen), IPC_PROXY_ERR,
55         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.bufferLen failed.");
56     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.allocLen), IPC_PROXY_ERR,
57         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.allocLen failed.");
58     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.filledLen), IPC_PROXY_ERR,
59         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.filledLen failed.");
60     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.offset), IPC_PROXY_ERR,
61         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.offset failed.");
62     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(srcBuffer.sharedMemType), IPC_PROXY_ERR,
63         "MediaDecryptModuleServiceProxy DecryptMediaData Write srcBuffer.sharedMemType failed.");
64 
65     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.bufferType), IPC_PROXY_ERR,
66         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.bufferType failed.");
67     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteFileDescriptor(dstBuffer.fd), IPC_PROXY_ERR,
68         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.fd failed.");
69     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.bufferLen), IPC_PROXY_ERR,
70         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.bufferLen failed.");
71     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.allocLen), IPC_PROXY_ERR,
72         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.allocLen failed.");
73     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.filledLen), IPC_PROXY_ERR,
74         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.filledLen failed.");
75     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.offset), IPC_PROXY_ERR,
76         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.offset failed.");
77     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(dstBuffer.sharedMemType), IPC_PROXY_ERR,
78         "MediaDecryptModuleServiceProxy DecryptMediaData Write dstBuffer.sharedMemType failed.");
79     return DRM_OK;
80 }
81 
DecryptMediaData(bool secureDecodrtState,IMediaDecryptModuleService::CryptInfo & cryptInfo,IMediaDecryptModuleService::DrmBuffer & srcBuffer,IMediaDecryptModuleService::DrmBuffer & dstBuffer)82 int32_t MediaDecryptModuleServiceProxy::DecryptMediaData(bool secureDecodrtState,
83     IMediaDecryptModuleService::CryptInfo &cryptInfo, IMediaDecryptModuleService::DrmBuffer &srcBuffer,
84     IMediaDecryptModuleService::DrmBuffer &dstBuffer)
85 {
86     DRM_INFO_LOG("DecryptMediaData enter.");
87     MessageParcel data;
88     MessageParcel reply;
89     MessageOption option;
90 
91     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), IPC_PROXY_ERR,
92         "MediaDecryptModuleServiceProxy DecryptMediaData Write interface token failed.");
93 
94     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteBool(secureDecodrtState), IPC_PROXY_ERR,
95         "MediaDecryptModuleServiceProxy DecryptMediaData Write secureDecodrtState failed.");
96 
97     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.type), IPC_PROXY_ERR,
98         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.type failed.");
99 
100     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.keyId.size()), IPC_PROXY_ERR,
101         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.keyId size failed.");
102     DRM_CHECK_AND_RETURN_RET_LOG(cryptInfo.keyId.size() <= KEYID_MAX_LEN, DRM_MEMORY_ERROR,
103         "The size of keyId is too large.");
104     if (cryptInfo.keyId.size() != 0) {
105         DRM_CHECK_AND_RETURN_RET_LOG(data.WriteBuffer(cryptInfo.keyId.data(), cryptInfo.keyId.size()), IPC_PROXY_ERR,
106             "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.keyId failed.");
107     }
108 
109     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.iv.size()), IPC_PROXY_ERR,
110         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.iv size failed.");
111     DRM_CHECK_AND_RETURN_RET_LOG(cryptInfo.iv.size() <= IV_MAX_LEN, DRM_MEMORY_ERROR, "The size of iv is too large.");
112 
113     if (cryptInfo.iv.size() != 0) {
114         DRM_CHECK_AND_RETURN_RET_LOG(data.WriteBuffer(cryptInfo.iv.data(), cryptInfo.iv.size()), IPC_PROXY_ERR,
115             "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.iv failed.");
116     }
117 
118     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.pattern.encryptBlocks), IPC_PROXY_ERR,
119         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.pattern.encryptBlocks failed.");
120 
121     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.pattern.skipBlocks), IPC_PROXY_ERR,
122         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.pattern.skipBlocks failed.");
123 
124     DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.subSample.size()), IPC_PROXY_ERR,
125         "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.subSample size failed.");
126     for (size_t i = 0; i < cryptInfo.subSample.size(); i++) {
127         DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.subSample[i].clearHeaderLen), IPC_PROXY_ERR,
128             "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.subSample.clearHeaderLen failed.");
129 
130         DRM_CHECK_AND_RETURN_RET_LOG(data.WriteUint32(cryptInfo.subSample[i].payLoadLen), IPC_PROXY_ERR,
131             "MediaDecryptModuleServiceProxy DecryptMediaData Write cryptInfo.subSample.payLoadLen failed.");
132     }
133     int32_t ret = ProcessDrmBuffer(data, srcBuffer, dstBuffer);
134     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_OK, IPC_PROXY_ERR,
135         "MediaDecryptModuleServiceProxy DecryptMediaData failed, errcode: %{public}d", ret);
136 
137     ret = Remote()->SendRequest(DECRYPT_MODULE_DECRYPT_DATA, data, reply, option);
138     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_OK, IPC_PROXY_ERR,
139         "MediaDecryptModuleServiceProxy DecryptMediaData failed, errcode: %{public}d", ret);
140     return ret;
141 }
142 } // DrmStandard
143 } // OHOS