Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 21 | #include <media/cas/DescramblerAPI.h> |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 22 | #include <media/hardware/CryptoAPI.h> |
| 23 | #include <media/stagefright/foundation/AUtils.h> |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include "DescramblerImpl.h" |
| 27 | #include "SharedLibrary.h" |
| 28 | #include "TypeConvert.h" |
| 29 | |
| 30 | namespace android { |
| 31 | using hidl::memory::V1_0::IMemory; |
| 32 | |
| 33 | namespace hardware { |
| 34 | namespace cas { |
| 35 | namespace V1_0 { |
| 36 | namespace implementation { |
| 37 | |
| 38 | #define CHECK_SUBSAMPLE_DEF(type) \ |
| 39 | static_assert(sizeof(SubSample) == sizeof(type::SubSample), \ |
| 40 | "SubSample: size doesn't match"); \ |
| 41 | static_assert(offsetof(SubSample, numBytesOfClearData) \ |
| 42 | == offsetof(type::SubSample, mNumBytesOfClearData), \ |
| 43 | "SubSample: numBytesOfClearData offset doesn't match"); \ |
| 44 | static_assert(offsetof(SubSample, numBytesOfEncryptedData) \ |
| 45 | == offsetof(type::SubSample, mNumBytesOfEncryptedData), \ |
| 46 | "SubSample: numBytesOfEncryptedData offset doesn't match") |
| 47 | |
| 48 | CHECK_SUBSAMPLE_DEF(DescramblerPlugin); |
| 49 | CHECK_SUBSAMPLE_DEF(CryptoPlugin); |
| 50 | |
| 51 | DescramblerImpl::DescramblerImpl( |
| 52 | const sp<SharedLibrary>& library, DescramblerPlugin *plugin) : |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 53 | mLibrary(library), mPluginHolder(plugin) { |
| 54 | ALOGV("CTOR: plugin=%p", mPluginHolder.get()); |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | DescramblerImpl::~DescramblerImpl() { |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 58 | ALOGV("DTOR: plugin=%p", mPluginHolder.get()); |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 59 | release(); |
| 60 | } |
| 61 | |
| 62 | Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) { |
| 63 | ALOGV("%s: sessionId=%s", __FUNCTION__, |
| 64 | sessionIdToString(sessionId).string()); |
| 65 | |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 66 | 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 Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Return<bool> DescramblerImpl::requiresSecureDecoderComponent( |
| 75 | const hidl_string& mime) { |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 76 | 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 Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 84 | static 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 Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 89 | Return<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 Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 99 | // Get a local copy of the shared_ptr for the plugin. Note that before |
| 100 | // calling the HIDL callback, this shared_ptr must be manually reset, |
| 101 | // since the client side could proceed as soon as the callback is called |
| 102 | // without waiting for this method to go out of scope. |
| 103 | std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder); |
| 104 | if (holder.get() == nullptr) { |
| 105 | _hidl_cb(toStatus(INVALID_OPERATION), 0, NULL); |
| 106 | return Void(); |
| 107 | } |
| 108 | |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 109 | sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase); |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 110 | |
| 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."); |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 115 | holder.reset(); |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 116 | _hidl_cb(toStatus(BAD_VALUE), 0, NULL); |
| 117 | return Void(); |
| 118 | } |
| 119 | if (!validateRangeForSize( |
| 120 | srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize())) { |
| 121 | ALOGE("Invalid src buffer range: offset %llu, size %llu, srcMem size %llu", |
| 122 | srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize()); |
| 123 | android_errorWriteLog(0x534e4554, "67962232"); |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 124 | holder.reset(); |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 125 | _hidl_cb(toStatus(BAD_VALUE), 0, NULL); |
| 126 | return Void(); |
| 127 | } |
| 128 | |
| 129 | // use 64-bit here to catch bad subsample size that might be overflowing. |
| 130 | uint64_t totalBytesInSubSamples = 0; |
| 131 | for (size_t i = 0; i < subSamples.size(); i++) { |
| 132 | totalBytesInSubSamples += (uint64_t)subSamples[i].numBytesOfClearData + |
| 133 | subSamples[i].numBytesOfEncryptedData; |
| 134 | } |
| 135 | // Further validate if the specified srcOffset and requested total subsample size |
| 136 | // is consistent with the source shared buffer size. |
| 137 | if (!validateRangeForSize(srcOffset, totalBytesInSubSamples, srcBuffer.size)) { |
| 138 | ALOGE("Invalid srcOffset and subsample size: " |
| 139 | "srcOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu", |
| 140 | srcOffset, totalBytesInSubSamples, srcBuffer.size); |
| 141 | android_errorWriteLog(0x534e4554, "67962232"); |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 142 | holder.reset(); |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 143 | _hidl_cb(toStatus(BAD_VALUE), 0, NULL); |
| 144 | return Void(); |
| 145 | } |
| 146 | |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 147 | void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset; |
| 148 | void *dstPtr = NULL; |
| 149 | if (dstBuffer.type == BufferType::SHARED_MEMORY) { |
| 150 | // When using shared memory, src buffer is also used as dst, |
| 151 | // we don't map it again here. |
| 152 | dstPtr = srcPtr; |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 153 | |
| 154 | // In this case the dst and src would be the same buffer, need to validate |
| 155 | // dstOffset against the buffer size too. |
| 156 | if (!validateRangeForSize(dstOffset, totalBytesInSubSamples, srcBuffer.size)) { |
| 157 | ALOGE("Invalid dstOffset and subsample size: " |
| 158 | "dstOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu", |
| 159 | dstOffset, totalBytesInSubSamples, srcBuffer.size); |
| 160 | android_errorWriteLog(0x534e4554, "67962232"); |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 161 | holder.reset(); |
Chong Zhang | 16a3cd0 | 2017-10-24 22:42:30 -0700 | [diff] [blame] | 162 | _hidl_cb(toStatus(BAD_VALUE), 0, NULL); |
| 163 | return Void(); |
| 164 | } |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 165 | } else { |
| 166 | native_handle_t *handle = const_cast<native_handle_t *>( |
| 167 | dstBuffer.secureMemory.getNativeHandle()); |
| 168 | dstPtr = static_cast<void *>(handle); |
| 169 | } |
| 170 | // Casting hidl SubSample to DescramblerPlugin::SubSample, but need |
| 171 | // to ensure structs are actually idential |
| 172 | |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 173 | int32_t result = holder->descramble( |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 174 | dstBuffer.type != BufferType::SHARED_MEMORY, |
| 175 | (DescramblerPlugin::ScramblingControl)scramblingControl, |
| 176 | subSamples.size(), |
| 177 | (DescramblerPlugin::SubSample*)subSamples.data(), |
| 178 | srcPtr, |
| 179 | srcOffset, |
| 180 | dstPtr, |
| 181 | dstOffset, |
| 182 | NULL); |
| 183 | |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 184 | holder.reset(); |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 185 | _hidl_cb(toStatus(result >= 0 ? OK : result), result, NULL); |
| 186 | return Void(); |
| 187 | } |
| 188 | |
| 189 | Return<Status> DescramblerImpl::release() { |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 190 | ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get()); |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 191 | |
Chong Zhang | addcb3a | 2018-03-21 15:52:21 -0700 | [diff] [blame] | 192 | std::shared_ptr<DescramblerPlugin> holder(nullptr); |
| 193 | std::atomic_store(&mPluginHolder, holder); |
| 194 | |
Chong Zhang | a4f6751 | 2017-04-24 17:18:25 -0700 | [diff] [blame] | 195 | return Status::OK; |
| 196 | } |
| 197 | |
| 198 | } // namespace implementation |
| 199 | } // namespace V1_0 |
| 200 | } // namespace cas |
| 201 | } // namespace hardware |
| 202 | } // namespace android |