blob: 6d5e2d5e3262d703f196acc16eb42751fdce2dee [file] [log] [blame]
Chong Zhanga4f67512017-04-24 17:18:25 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "android.hardware.cas@1.0-DescramblerImpl"
19
20#include <hidlmemory/mapping.h>
Chong Zhanga4f67512017-04-24 17:18:25 -070021#include <media/cas/DescramblerAPI.h>
Chong Zhang16a3cd02017-10-24 22:42:30 -070022#include <media/hardware/CryptoAPI.h>
23#include <media/stagefright/foundation/AUtils.h>
Chong Zhanga4f67512017-04-24 17:18:25 -070024#include <utils/Log.h>
25
26#include "DescramblerImpl.h"
27#include "SharedLibrary.h"
28#include "TypeConvert.h"
29
30namespace android {
31using hidl::memory::V1_0::IMemory;
32
33namespace hardware {
34namespace cas {
35namespace V1_0 {
36namespace implementation {
37
38#define CHECK_SUBSAMPLE_DEF(type) \
39static_assert(sizeof(SubSample) == sizeof(type::SubSample), \
40 "SubSample: size doesn't match"); \
41static_assert(offsetof(SubSample, numBytesOfClearData) \
42 == offsetof(type::SubSample, mNumBytesOfClearData), \
43 "SubSample: numBytesOfClearData offset doesn't match"); \
44static_assert(offsetof(SubSample, numBytesOfEncryptedData) \
45 == offsetof(type::SubSample, mNumBytesOfEncryptedData), \
46 "SubSample: numBytesOfEncryptedData offset doesn't match")
47
48CHECK_SUBSAMPLE_DEF(DescramblerPlugin);
49CHECK_SUBSAMPLE_DEF(CryptoPlugin);
50
51DescramblerImpl::DescramblerImpl(
52 const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
Chong Zhangaddcb3a2018-03-21 15:52:21 -070053 mLibrary(library), mPluginHolder(plugin) {
54 ALOGV("CTOR: plugin=%p", mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -070055}
56
57DescramblerImpl::~DescramblerImpl() {
Chong Zhangaddcb3a2018-03-21 15:52:21 -070058 ALOGV("DTOR: plugin=%p", mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -070059 release();
60}
61
62Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) {
63 ALOGV("%s: sessionId=%s", __FUNCTION__,
64 sessionIdToString(sessionId).string());
65
Chong Zhangaddcb3a2018-03-21 15:52:21 -070066 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
67 if (holder.get() == nullptr) {
68 return toStatus(INVALID_OPERATION);
69 }
70
71 return toStatus(holder->setMediaCasSession(sessionId));
Chong Zhanga4f67512017-04-24 17:18:25 -070072}
73
74Return<bool> DescramblerImpl::requiresSecureDecoderComponent(
75 const hidl_string& mime) {
Chong Zhangaddcb3a2018-03-21 15:52:21 -070076 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
77 if (holder.get() == nullptr) {
78 return false;
79 }
80
81 return holder->requiresSecureDecoderComponent(String8(mime.c_str()));
Chong Zhanga4f67512017-04-24 17:18:25 -070082}
83
Chong Zhang16a3cd02017-10-24 22:42:30 -070084static inline bool validateRangeForSize(
85 uint64_t offset, uint64_t length, uint64_t size) {
86 return isInRange<uint64_t, uint64_t>(0, size, offset, length);
87}
88
Chong Zhanga4f67512017-04-24 17:18:25 -070089Return<void> DescramblerImpl::descramble(
90 ScramblingControl scramblingControl,
91 const hidl_vec<SubSample>& subSamples,
92 const SharedBuffer& srcBuffer,
93 uint64_t srcOffset,
94 const DestinationBuffer& dstBuffer,
95 uint64_t dstOffset,
96 descramble_cb _hidl_cb) {
97 ALOGV("%s", __FUNCTION__);
98
Chong Zhange1ce7452018-05-10 13:53:12 -070099 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
100 // size in size_t. If size is over SIZE_MAX, mapMemory mapMemory could succeed
101 // but the mapped memory's actual size will be smaller than the reported size.
102 if (srcBuffer.heapBase.size() > SIZE_MAX) {
103 ALOGE("Invalid hidl_memory size: %llu", srcBuffer.heapBase.size());
104 android_errorWriteLog(0x534e4554, "79376389");
105 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700106 return Void();
107 }
108
Chong Zhanga4f67512017-04-24 17:18:25 -0700109 sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase);
Chong Zhang16a3cd02017-10-24 22:42:30 -0700110
111 // Validate if the offset and size in the SharedBuffer is consistent with the
112 // mapped ashmem, since the offset and size is controlled by client.
113 if (srcMem == NULL) {
114 ALOGE("Failed to map src buffer.");
115 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
116 return Void();
117 }
118 if (!validateRangeForSize(
119 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize())) {
120 ALOGE("Invalid src buffer range: offset %llu, size %llu, srcMem size %llu",
121 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize());
122 android_errorWriteLog(0x534e4554, "67962232");
123 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
124 return Void();
125 }
126
127 // use 64-bit here to catch bad subsample size that might be overflowing.
128 uint64_t totalBytesInSubSamples = 0;
129 for (size_t i = 0; i < subSamples.size(); i++) {
130 totalBytesInSubSamples += (uint64_t)subSamples[i].numBytesOfClearData +
131 subSamples[i].numBytesOfEncryptedData;
132 }
133 // Further validate if the specified srcOffset and requested total subsample size
134 // is consistent with the source shared buffer size.
135 if (!validateRangeForSize(srcOffset, totalBytesInSubSamples, srcBuffer.size)) {
136 ALOGE("Invalid srcOffset and subsample size: "
137 "srcOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
138 srcOffset, totalBytesInSubSamples, srcBuffer.size);
139 android_errorWriteLog(0x534e4554, "67962232");
140 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
141 return Void();
142 }
143
Chong Zhanga4f67512017-04-24 17:18:25 -0700144 void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset;
145 void *dstPtr = NULL;
146 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
147 // When using shared memory, src buffer is also used as dst,
148 // we don't map it again here.
149 dstPtr = srcPtr;
Chong Zhang16a3cd02017-10-24 22:42:30 -0700150
151 // In this case the dst and src would be the same buffer, need to validate
152 // dstOffset against the buffer size too.
153 if (!validateRangeForSize(dstOffset, totalBytesInSubSamples, srcBuffer.size)) {
154 ALOGE("Invalid dstOffset and subsample size: "
155 "dstOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
156 dstOffset, totalBytesInSubSamples, srcBuffer.size);
157 android_errorWriteLog(0x534e4554, "67962232");
158 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
159 return Void();
160 }
Chong Zhanga4f67512017-04-24 17:18:25 -0700161 } else {
162 native_handle_t *handle = const_cast<native_handle_t *>(
163 dstBuffer.secureMemory.getNativeHandle());
164 dstPtr = static_cast<void *>(handle);
165 }
Chong Zhange1ce7452018-05-10 13:53:12 -0700166
167 // Get a local copy of the shared_ptr for the plugin. Note that before
168 // calling the HIDL callback, this shared_ptr must be manually reset,
169 // since the client side could proceed as soon as the callback is called
170 // without waiting for this method to go out of scope.
171 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
172 if (holder.get() == nullptr) {
173 _hidl_cb(toStatus(INVALID_OPERATION), 0, NULL);
174 return Void();
175 }
176
Chong Zhanga4f67512017-04-24 17:18:25 -0700177 // Casting hidl SubSample to DescramblerPlugin::SubSample, but need
178 // to ensure structs are actually idential
179
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700180 int32_t result = holder->descramble(
Chong Zhanga4f67512017-04-24 17:18:25 -0700181 dstBuffer.type != BufferType::SHARED_MEMORY,
182 (DescramblerPlugin::ScramblingControl)scramblingControl,
183 subSamples.size(),
184 (DescramblerPlugin::SubSample*)subSamples.data(),
185 srcPtr,
186 srcOffset,
187 dstPtr,
188 dstOffset,
189 NULL);
190
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700191 holder.reset();
Chong Zhanga4f67512017-04-24 17:18:25 -0700192 _hidl_cb(toStatus(result >= 0 ? OK : result), result, NULL);
193 return Void();
194}
195
196Return<Status> DescramblerImpl::release() {
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700197 ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -0700198
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700199 std::shared_ptr<DescramblerPlugin> holder(nullptr);
200 std::atomic_store(&mPluginHolder, holder);
201
Chong Zhanga4f67512017-04-24 17:18:25 -0700202 return Status::OK;
203}
204
205} // namespace implementation
206} // namespace V1_0
207} // namespace cas
208} // namespace hardware
209} // namespace android