| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2012 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 "Crypto" | 
|  | 19 | #include <utils/Log.h> | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 20 | #include <dirent.h> | 
|  | 21 | #include <dlfcn.h> | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include "Crypto.h" | 
|  | 24 |  | 
| Jeff Tinker | c481b50 | 2015-04-06 18:21:05 -0700 | [diff] [blame^] | 25 | #include <binder/IMemory.h> | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 26 | #include <media/hardware/CryptoAPI.h> | 
|  | 27 | #include <media/stagefright/foundation/ADebug.h> | 
| Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/AString.h> | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 29 | #include <media/stagefright/foundation/hexdump.h> | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 30 | #include <media/stagefright/MediaErrors.h> | 
|  | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 34 | KeyedVector<Vector<uint8_t>, String8> Crypto::mUUIDToLibraryPathMap; | 
|  | 35 | KeyedVector<String8, wp<SharedLibrary> > Crypto::mLibraryPathToOpenLibraryMap; | 
|  | 36 | Mutex Crypto::mMapLock; | 
|  | 37 |  | 
|  | 38 | static bool operator<(const Vector<uint8_t> &lhs, const Vector<uint8_t> &rhs) { | 
|  | 39 | if (lhs.size() < rhs.size()) { | 
|  | 40 | return true; | 
|  | 41 | } else if (lhs.size() > rhs.size()) { | 
|  | 42 | return false; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | return memcmp((void *)lhs.array(), (void *)rhs.array(), rhs.size()) < 0; | 
|  | 46 | } | 
|  | 47 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 48 | Crypto::Crypto() | 
|  | 49 | : mInitCheck(NO_INIT), | 
| Andreas Huber | 1608735 | 2012-04-13 14:54:36 -0700 | [diff] [blame] | 50 | mFactory(NULL), | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 51 | mPlugin(NULL) { | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
|  | 54 | Crypto::~Crypto() { | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 55 | delete mPlugin; | 
|  | 56 | mPlugin = NULL; | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 57 | closeFactory(); | 
|  | 58 | } | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 59 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 60 | void Crypto::closeFactory() { | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 61 | delete mFactory; | 
|  | 62 | mFactory = NULL; | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 63 | mLibrary.clear(); | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 66 | status_t Crypto::initCheck() const { | 
|  | 67 | return mInitCheck; | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 70 | /* | 
|  | 71 | * Search the plugins directory for a plugin that supports the scheme | 
|  | 72 | * specified by uuid | 
|  | 73 | * | 
|  | 74 | * If found: | 
|  | 75 | *    mLibrary holds a strong pointer to the dlopen'd library | 
|  | 76 | *    mFactory is set to the library's factory method | 
|  | 77 | *    mInitCheck is set to OK | 
|  | 78 | * | 
|  | 79 | * If not found: | 
|  | 80 | *    mLibrary is cleared and mFactory are set to NULL | 
|  | 81 | *    mInitCheck is set to an error (!OK) | 
|  | 82 | */ | 
|  | 83 | void Crypto::findFactoryForScheme(const uint8_t uuid[16]) { | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 84 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 85 | closeFactory(); | 
| Andreas Huber | 1608735 | 2012-04-13 14:54:36 -0700 | [diff] [blame] | 86 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 87 | // lock static maps | 
|  | 88 | Mutex::Autolock autoLock(mMapLock); | 
|  | 89 |  | 
|  | 90 | // first check cache | 
|  | 91 | Vector<uint8_t> uuidVector; | 
|  | 92 | uuidVector.appendArray(uuid, sizeof(uuid)); | 
|  | 93 | ssize_t index = mUUIDToLibraryPathMap.indexOfKey(uuidVector); | 
|  | 94 | if (index >= 0) { | 
|  | 95 | if (loadLibraryForScheme(mUUIDToLibraryPathMap[index], uuid)) { | 
|  | 96 | mInitCheck = OK; | 
|  | 97 | return; | 
|  | 98 | } else { | 
|  | 99 | ALOGE("Failed to load from cached library path!"); | 
|  | 100 | mInitCheck = ERROR_UNSUPPORTED; | 
|  | 101 | return; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | // no luck, have to search | 
|  | 106 | String8 dirPath("/vendor/lib/mediadrm"); | 
|  | 107 | String8 pluginPath; | 
|  | 108 |  | 
|  | 109 | DIR* pDir = opendir(dirPath.string()); | 
|  | 110 | if (pDir) { | 
|  | 111 | struct dirent* pEntry; | 
|  | 112 | while ((pEntry = readdir(pDir))) { | 
|  | 113 |  | 
|  | 114 | pluginPath = dirPath + "/" + pEntry->d_name; | 
|  | 115 |  | 
|  | 116 | if (pluginPath.getPathExtension() == ".so") { | 
|  | 117 |  | 
|  | 118 | if (loadLibraryForScheme(pluginPath, uuid)) { | 
|  | 119 | mUUIDToLibraryPathMap.add(uuidVector, pluginPath); | 
|  | 120 | mInitCheck = OK; | 
|  | 121 | closedir(pDir); | 
|  | 122 | return; | 
|  | 123 | } | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | closedir(pDir); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | // try the legacy libdrmdecrypt.so | 
|  | 131 | pluginPath = "libdrmdecrypt.so"; | 
|  | 132 | if (loadLibraryForScheme(pluginPath, uuid)) { | 
|  | 133 | mUUIDToLibraryPathMap.add(uuidVector, pluginPath); | 
|  | 134 | mInitCheck = OK; | 
|  | 135 | return; | 
|  | 136 | } | 
|  | 137 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 138 | mInitCheck = ERROR_UNSUPPORTED; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | bool Crypto::loadLibraryForScheme(const String8 &path, const uint8_t uuid[16]) { | 
|  | 142 |  | 
|  | 143 | // get strong pointer to open shared library | 
|  | 144 | ssize_t index = mLibraryPathToOpenLibraryMap.indexOfKey(path); | 
|  | 145 | if (index >= 0) { | 
|  | 146 | mLibrary = mLibraryPathToOpenLibraryMap[index].promote(); | 
|  | 147 | } else { | 
|  | 148 | index = mLibraryPathToOpenLibraryMap.add(path, NULL); | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | if (!mLibrary.get()) { | 
|  | 152 | mLibrary = new SharedLibrary(path); | 
|  | 153 | if (!*mLibrary) { | 
| leozwang@google.com | d1eff57 | 2013-07-13 21:52:50 -0700 | [diff] [blame] | 154 | ALOGE("loadLibraryForScheme failed:%s", mLibrary->lastError()); | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 155 | return false; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | mLibraryPathToOpenLibraryMap.replaceValueAt(index, mLibrary); | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | typedef CryptoFactory *(*CreateCryptoFactoryFunc)(); | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 162 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 163 | CreateCryptoFactoryFunc createCryptoFactory = | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 164 | (CreateCryptoFactoryFunc)mLibrary->lookup("createCryptoFactory"); | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 165 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 166 | if (createCryptoFactory == NULL || | 
|  | 167 | (mFactory = createCryptoFactory()) == NULL || | 
|  | 168 | !mFactory->isCryptoSchemeSupported(uuid)) { | 
| leozwang@google.com | d1eff57 | 2013-07-13 21:52:50 -0700 | [diff] [blame] | 169 | ALOGE("createCryptoFactory failed:%s", mLibrary->lastError()); | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 170 | closeFactory(); | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 171 | return false; | 
|  | 172 | } | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 173 | return true; | 
|  | 174 | } | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 175 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 176 | bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) { | 
|  | 177 | Mutex::Autolock autoLock(mLock); | 
|  | 178 |  | 
|  | 179 | if (mFactory && mFactory->isCryptoSchemeSupported(uuid)) { | 
|  | 180 | return true; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | findFactoryForScheme(uuid); | 
|  | 184 | return (mInitCheck == OK); | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 187 | status_t Crypto::createPlugin( | 
|  | 188 | const uint8_t uuid[16], const void *data, size_t size) { | 
|  | 189 | Mutex::Autolock autoLock(mLock); | 
|  | 190 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 191 | if (mPlugin != NULL) { | 
|  | 192 | return -EINVAL; | 
|  | 193 | } | 
|  | 194 |  | 
| Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 195 | if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) { | 
|  | 196 | findFactoryForScheme(uuid); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | if (mInitCheck != OK) { | 
|  | 200 | return mInitCheck; | 
|  | 201 | } | 
|  | 202 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 203 | return mFactory->createPlugin(uuid, data, size, &mPlugin); | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 206 | status_t Crypto::destroyPlugin() { | 
|  | 207 | Mutex::Autolock autoLock(mLock); | 
|  | 208 |  | 
|  | 209 | if (mInitCheck != OK) { | 
|  | 210 | return mInitCheck; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | if (mPlugin == NULL) { | 
|  | 214 | return -EINVAL; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | delete mPlugin; | 
|  | 218 | mPlugin = NULL; | 
|  | 219 |  | 
|  | 220 | return OK; | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 223 | bool Crypto::requiresSecureDecoderComponent(const char *mime) const { | 
|  | 224 | Mutex::Autolock autoLock(mLock); | 
|  | 225 |  | 
|  | 226 | if (mInitCheck != OK) { | 
|  | 227 | return mInitCheck; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | if (mPlugin == NULL) { | 
|  | 231 | return -EINVAL; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | return mPlugin->requiresSecureDecoderComponent(mime); | 
|  | 235 | } | 
|  | 236 |  | 
| Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 237 | ssize_t Crypto::decrypt( | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 238 | bool secure, | 
|  | 239 | const uint8_t key[16], | 
|  | 240 | const uint8_t iv[16], | 
|  | 241 | CryptoPlugin::Mode mode, | 
| Jeff Tinker | c481b50 | 2015-04-06 18:21:05 -0700 | [diff] [blame^] | 242 | const sp<IMemory> &sharedBuffer, size_t offset, | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 243 | const CryptoPlugin::SubSample *subSamples, size_t numSubSamples, | 
| Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 244 | void *dstPtr, | 
|  | 245 | AString *errorDetailMsg) { | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 246 | Mutex::Autolock autoLock(mLock); | 
|  | 247 |  | 
|  | 248 | if (mInitCheck != OK) { | 
|  | 249 | return mInitCheck; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | if (mPlugin == NULL) { | 
|  | 253 | return -EINVAL; | 
|  | 254 | } | 
|  | 255 |  | 
| Jeff Tinker | c481b50 | 2015-04-06 18:21:05 -0700 | [diff] [blame^] | 256 | const void *srcPtr = static_cast<uint8_t *>(sharedBuffer->pointer()) + offset; | 
|  | 257 |  | 
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 258 | return mPlugin->decrypt( | 
| Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 259 | secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr, | 
|  | 260 | errorDetailMsg); | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Jeff Tinker | 2514d08 | 2014-11-03 13:29:35 -0800 | [diff] [blame] | 263 | void Crypto::notifyResolution(uint32_t width, uint32_t height) { | 
|  | 264 | Mutex::Autolock autoLock(mLock); | 
|  | 265 |  | 
|  | 266 | if (mInitCheck == OK && mPlugin != NULL) { | 
|  | 267 | mPlugin->notifyResolution(width, height); | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 |  | 
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 271 | }  // namespace android |