blob: e768772d776fed08e3c3931607ad466c70fbeb61 [file] [log] [blame]
Andreas Hubered3e3e02012-03-26 11:13:27 -07001/*
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 Tinkerbafb6822013-03-22 15:26:39 -070020#include <dirent.h>
21#include <dlfcn.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070022
23#include "Crypto.h"
24
Jeff Tinkerc481b502015-04-06 18:21:05 -070025#include <binder/IMemory.h>
Andreas Huber1bd139a2012-04-03 14:19:20 -070026#include <media/hardware/CryptoAPI.h>
27#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070028#include <media/stagefright/foundation/AString.h>
Andreas Huber1bd139a2012-04-03 14:19:20 -070029#include <media/stagefright/foundation/hexdump.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070030#include <media/stagefright/MediaErrors.h>
31
32namespace android {
33
Jeff Tinkerbafb6822013-03-22 15:26:39 -070034KeyedVector<Vector<uint8_t>, String8> Crypto::mUUIDToLibraryPathMap;
35KeyedVector<String8, wp<SharedLibrary> > Crypto::mLibraryPathToOpenLibraryMap;
36Mutex Crypto::mMapLock;
37
38static 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 Huber1bd139a2012-04-03 14:19:20 -070048Crypto::Crypto()
49 : mInitCheck(NO_INIT),
Andreas Huber16087352012-04-13 14:54:36 -070050 mFactory(NULL),
Andreas Huber1bd139a2012-04-03 14:19:20 -070051 mPlugin(NULL) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070052}
53
54Crypto::~Crypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070055 delete mPlugin;
56 mPlugin = NULL;
Jeff Tinkerbafb6822013-03-22 15:26:39 -070057 closeFactory();
58}
Andreas Huber1bd139a2012-04-03 14:19:20 -070059
Jeff Tinkerbafb6822013-03-22 15:26:39 -070060void Crypto::closeFactory() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070061 delete mFactory;
62 mFactory = NULL;
Jeff Tinkerbafb6822013-03-22 15:26:39 -070063 mLibrary.clear();
Andreas Hubered3e3e02012-03-26 11:13:27 -070064}
65
Andreas Huber1bd139a2012-04-03 14:19:20 -070066status_t Crypto::initCheck() const {
67 return mInitCheck;
Andreas Hubered3e3e02012-03-26 11:13:27 -070068}
69
Jeff Tinkerbafb6822013-03-22 15:26:39 -070070/*
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 */
83void Crypto::findFactoryForScheme(const uint8_t uuid[16]) {
Andreas Huber1bd139a2012-04-03 14:19:20 -070084
Jeff Tinkerbafb6822013-03-22 15:26:39 -070085 closeFactory();
Andreas Huber16087352012-04-13 14:54:36 -070086
Jeff Tinkerbafb6822013-03-22 15:26:39 -070087 // 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 Tinkerbafb6822013-03-22 15:26:39 -0700138 mInitCheck = ERROR_UNSUPPORTED;
139}
140
141bool 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.comd1eff572013-07-13 21:52:50 -0700154 ALOGE("loadLibraryForScheme failed:%s", mLibrary->lastError());
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700155 return false;
156 }
157
158 mLibraryPathToOpenLibraryMap.replaceValueAt(index, mLibrary);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700159 }
160
161 typedef CryptoFactory *(*CreateCryptoFactoryFunc)();
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700162
Andreas Huber1bd139a2012-04-03 14:19:20 -0700163 CreateCryptoFactoryFunc createCryptoFactory =
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700164 (CreateCryptoFactoryFunc)mLibrary->lookup("createCryptoFactory");
Andreas Huber1bd139a2012-04-03 14:19:20 -0700165
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700166 if (createCryptoFactory == NULL ||
167 (mFactory = createCryptoFactory()) == NULL ||
168 !mFactory->isCryptoSchemeSupported(uuid)) {
leozwang@google.comd1eff572013-07-13 21:52:50 -0700169 ALOGE("createCryptoFactory failed:%s", mLibrary->lastError());
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700170 closeFactory();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700171 return false;
172 }
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700173 return true;
174}
Andreas Huber1bd139a2012-04-03 14:19:20 -0700175
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700176bool 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 Hubered3e3e02012-03-26 11:13:27 -0700185}
186
Andreas Huber1bd139a2012-04-03 14:19:20 -0700187status_t Crypto::createPlugin(
188 const uint8_t uuid[16], const void *data, size_t size) {
189 Mutex::Autolock autoLock(mLock);
190
Andreas Huber1bd139a2012-04-03 14:19:20 -0700191 if (mPlugin != NULL) {
192 return -EINVAL;
193 }
194
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700195 if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) {
196 findFactoryForScheme(uuid);
197 }
198
199 if (mInitCheck != OK) {
200 return mInitCheck;
201 }
202
Andreas Huber1bd139a2012-04-03 14:19:20 -0700203 return mFactory->createPlugin(uuid, data, size, &mPlugin);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700204}
205
Andreas Huber1bd139a2012-04-03 14:19:20 -0700206status_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 Hubered3e3e02012-03-26 11:13:27 -0700221}
222
Andreas Huber1bd139a2012-04-03 14:19:20 -0700223bool 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 Wongfa2b8f22012-07-10 20:01:13 -0700237ssize_t Crypto::decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700238 bool secure,
239 const uint8_t key[16],
240 const uint8_t iv[16],
241 CryptoPlugin::Mode mode,
Jeff Tinkerc481b502015-04-06 18:21:05 -0700242 const sp<IMemory> &sharedBuffer, size_t offset,
Andreas Huber1bd139a2012-04-03 14:19:20 -0700243 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700244 void *dstPtr,
245 AString *errorDetailMsg) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700246 Mutex::Autolock autoLock(mLock);
247
248 if (mInitCheck != OK) {
249 return mInitCheck;
250 }
251
252 if (mPlugin == NULL) {
253 return -EINVAL;
254 }
255
Jeff Tinkerc481b502015-04-06 18:21:05 -0700256 const void *srcPtr = static_cast<uint8_t *>(sharedBuffer->pointer()) + offset;
257
Andreas Huber1bd139a2012-04-03 14:19:20 -0700258 return mPlugin->decrypt(
Andreas Huber5b8987e2012-04-19 12:52:20 -0700259 secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr,
260 errorDetailMsg);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700261}
262
Jeff Tinker2514d082014-11-03 13:29:35 -0800263void 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 Hubered3e3e02012-03-26 11:13:27 -0700271} // namespace android