1 /* 2 * Copyright (C) 2016 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 #define LOG_TAG "android.hardware.drm@1.0-impl" 17 18 #include "CryptoPlugin.h" 19 #include "TypeConvert.h" 20 21 #include <android/hidl/memory/1.0/IMemory.h> 22 #include <hidlmemory/mapping.h> 23 #include <log/log.h> 24 #include <media/stagefright/foundation/AString.h> 25 26 using android::hardware::hidl_memory; 27 using android::hidl::memory::V1_0::IMemory; 28 29 namespace android { 30 namespace hardware { 31 namespace drm { 32 namespace V1_0 { 33 namespace implementation { 34 35 // Methods from ::android::hardware::drm::V1_0::ICryptoPlugin follow requiresSecureDecoderComponent(const hidl_string & mime)36 Return<bool> CryptoPlugin::requiresSecureDecoderComponent( 37 const hidl_string& mime) { 38 return mLegacyPlugin->requiresSecureDecoderComponent(mime.c_str()); 39 } 40 notifyResolution(uint32_t width,uint32_t height)41 Return<void> CryptoPlugin::notifyResolution(uint32_t width, 42 uint32_t height) { 43 mLegacyPlugin->notifyResolution(width, height); 44 return Void(); 45 } 46 setMediaDrmSession(const hidl_vec<uint8_t> & sessionId)47 Return<Status> CryptoPlugin::setMediaDrmSession( 48 const hidl_vec<uint8_t>& sessionId) { 49 return toStatus(mLegacyPlugin->setMediaDrmSession(toVector(sessionId))); 50 } 51 setSharedBufferBase(const hidl_memory & base,uint32_t bufferId)52 Return<void> CryptoPlugin::setSharedBufferBase(const hidl_memory& base, 53 uint32_t bufferId) { 54 sp<IMemory> hidlMemory = mapMemory(base); 55 ALOGE_IF(hidlMemory == nullptr, "mapMemory returns nullptr"); 56 57 // allow mapMemory to return nullptr 58 mSharedBufferMap[bufferId] = hidlMemory; 59 return Void(); 60 } 61 decrypt(bool secure,const hidl_array<uint8_t,16> & keyId,const hidl_array<uint8_t,16> & iv,Mode mode,const Pattern & pattern,const hidl_vec<SubSample> & subSamples,const SharedBuffer & source,uint64_t offset,const DestinationBuffer & destination,decrypt_cb _hidl_cb)62 Return<void> CryptoPlugin::decrypt(bool secure, 63 const hidl_array<uint8_t, 16>& keyId, 64 const hidl_array<uint8_t, 16>& iv, Mode mode, 65 const Pattern& pattern, const hidl_vec<SubSample>& subSamples, 66 const SharedBuffer& source, uint64_t offset, 67 const DestinationBuffer& destination, 68 decrypt_cb _hidl_cb) { 69 70 if (mSharedBufferMap.find(source.bufferId) == mSharedBufferMap.end()) { 71 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "source decrypt buffer base not set"); 72 return Void(); 73 } 74 75 if (destination.type == BufferType::SHARED_MEMORY) { 76 const SharedBuffer& dest = destination.nonsecureMemory; 77 if (mSharedBufferMap.find(dest.bufferId) == mSharedBufferMap.end()) { 78 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "destination decrypt buffer base not set"); 79 return Void(); 80 } 81 } 82 83 android::CryptoPlugin::Mode legacyMode; 84 switch(mode) { 85 case Mode::UNENCRYPTED: 86 legacyMode = android::CryptoPlugin::kMode_Unencrypted; 87 break; 88 case Mode::AES_CTR: 89 legacyMode = android::CryptoPlugin::kMode_AES_CTR; 90 break; 91 case Mode::AES_CBC_CTS: 92 legacyMode = android::CryptoPlugin::kMode_AES_WV; 93 break; 94 case Mode::AES_CBC: 95 legacyMode = android::CryptoPlugin::kMode_AES_CBC; 96 break; 97 } 98 android::CryptoPlugin::Pattern legacyPattern; 99 legacyPattern.mEncryptBlocks = pattern.encryptBlocks; 100 legacyPattern.mSkipBlocks = pattern.skipBlocks; 101 102 android::CryptoPlugin::SubSample *legacySubSamples = 103 new android::CryptoPlugin::SubSample[subSamples.size()]; 104 105 size_t destSize = 0; 106 for (size_t i = 0; i < subSamples.size(); i++) { 107 uint32_t numBytesOfClearData = subSamples[i].numBytesOfClearData; 108 legacySubSamples[i].mNumBytesOfClearData = numBytesOfClearData; 109 uint32_t numBytesOfEncryptedData = subSamples[i].numBytesOfEncryptedData; 110 legacySubSamples[i].mNumBytesOfEncryptedData = numBytesOfEncryptedData; 111 if (__builtin_add_overflow(destSize, numBytesOfClearData, &destSize)) { 112 delete[] legacySubSamples; 113 _hidl_cb(Status::BAD_VALUE, 0, "subsample clear size overflow"); 114 return Void(); 115 } 116 if (__builtin_add_overflow(destSize, numBytesOfEncryptedData, &destSize)) { 117 delete[] legacySubSamples; 118 _hidl_cb(Status::BAD_VALUE, 0, "subsample encrypted size overflow"); 119 return Void(); 120 } 121 } 122 123 AString detailMessage; 124 sp<IMemory> sourceBase = mSharedBufferMap[source.bufferId]; 125 if (sourceBase == nullptr) { 126 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "source is a nullptr"); 127 return Void(); 128 } 129 130 if (source.offset + offset + source.size > sourceBase->getSize()) { 131 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size"); 132 return Void(); 133 } 134 135 uint8_t *base = static_cast<uint8_t *> 136 (static_cast<void *>(sourceBase->getPointer())); 137 void *srcPtr = static_cast<void *>(base + source.offset + offset); 138 139 void *destPtr = NULL; 140 if (destination.type == BufferType::SHARED_MEMORY) { 141 const SharedBuffer& destBuffer = destination.nonsecureMemory; 142 sp<IMemory> destBase = mSharedBufferMap[destBuffer.bufferId]; 143 if (destBase == nullptr) { 144 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "destination is a nullptr"); 145 return Void(); 146 } 147 148 if (destBuffer.offset + destBuffer.size > destBase->getSize()) { 149 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size"); 150 return Void(); 151 } 152 153 if (destSize > destBuffer.size) { 154 delete[] legacySubSamples; 155 _hidl_cb(Status::BAD_VALUE, 0, "subsample sum too large"); 156 return Void(); 157 } 158 159 base = static_cast<uint8_t *>(static_cast<void *>(destBase->getPointer())); 160 destPtr = static_cast<void *>(base + destination.nonsecureMemory.offset); 161 } else if (destination.type == BufferType::NATIVE_HANDLE) { 162 if (!secure) { 163 delete[] legacySubSamples; 164 _hidl_cb(Status::BAD_VALUE, 0, "native handle destination must be secure"); 165 return Void(); 166 } 167 native_handle_t *handle = const_cast<native_handle_t *>( 168 destination.secureMemory.getNativeHandle()); 169 destPtr = static_cast<void *>(handle); 170 } else { 171 delete[] legacySubSamples; 172 _hidl_cb(Status::BAD_VALUE, 0, "invalid destination type"); 173 return Void(); 174 } 175 ssize_t result = mLegacyPlugin->decrypt(secure, keyId.data(), iv.data(), 176 legacyMode, legacyPattern, srcPtr, legacySubSamples, 177 subSamples.size(), destPtr, &detailMessage); 178 179 delete[] legacySubSamples; 180 181 uint32_t status; 182 uint32_t bytesWritten; 183 184 if (result >= 0) { 185 status = android::OK; 186 bytesWritten = result; 187 } else { 188 status = result; 189 bytesWritten = 0; 190 } 191 192 _hidl_cb(toStatus(status), bytesWritten, detailMessage.c_str()); 193 return Void(); 194 } 195 196 } // namespace implementation 197 } // namespace V1_0 198 } // namespace drm 199 } // namespace hardware 200 } // namespace android 201