blob: 62593b24ec67feee0014a9a10529d2c70280793f [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
Andreas Huber1bd139a2012-04-03 14:19:20 -070025#include <media/hardware/CryptoAPI.h>
26#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070027#include <media/stagefright/foundation/AString.h>
Andreas Huber1bd139a2012-04-03 14:19:20 -070028#include <media/stagefright/foundation/hexdump.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070029#include <media/stagefright/MediaErrors.h>
30
31namespace android {
32
Jeff Tinkerbafb6822013-03-22 15:26:39 -070033KeyedVector<Vector<uint8_t>, String8> Crypto::mUUIDToLibraryPathMap;
34KeyedVector<String8, wp<SharedLibrary> > Crypto::mLibraryPathToOpenLibraryMap;
35Mutex Crypto::mMapLock;
36
37static bool operator<(const Vector<uint8_t> &lhs, const Vector<uint8_t> &rhs) {
38 if (lhs.size() < rhs.size()) {
39 return true;
40 } else if (lhs.size() > rhs.size()) {
41 return false;
42 }
43
44 return memcmp((void *)lhs.array(), (void *)rhs.array(), rhs.size()) < 0;
45}
46
Andreas Huber1bd139a2012-04-03 14:19:20 -070047Crypto::Crypto()
48 : mInitCheck(NO_INIT),
Andreas Huber16087352012-04-13 14:54:36 -070049 mFactory(NULL),
Andreas Huber1bd139a2012-04-03 14:19:20 -070050 mPlugin(NULL) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070051}
52
53Crypto::~Crypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070054 delete mPlugin;
55 mPlugin = NULL;
Jeff Tinkerbafb6822013-03-22 15:26:39 -070056 closeFactory();
57}
Andreas Huber1bd139a2012-04-03 14:19:20 -070058
Jeff Tinkerbafb6822013-03-22 15:26:39 -070059void Crypto::closeFactory() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070060 delete mFactory;
61 mFactory = NULL;
Jeff Tinkerbafb6822013-03-22 15:26:39 -070062 mLibrary.clear();
Andreas Hubered3e3e02012-03-26 11:13:27 -070063}
64
Andreas Huber1bd139a2012-04-03 14:19:20 -070065status_t Crypto::initCheck() const {
66 return mInitCheck;
Andreas Hubered3e3e02012-03-26 11:13:27 -070067}
68
Jeff Tinkerbafb6822013-03-22 15:26:39 -070069/*
70 * Search the plugins directory for a plugin that supports the scheme
71 * specified by uuid
72 *
73 * If found:
74 * mLibrary holds a strong pointer to the dlopen'd library
75 * mFactory is set to the library's factory method
76 * mInitCheck is set to OK
77 *
78 * If not found:
79 * mLibrary is cleared and mFactory are set to NULL
80 * mInitCheck is set to an error (!OK)
81 */
82void Crypto::findFactoryForScheme(const uint8_t uuid[16]) {
Andreas Huber1bd139a2012-04-03 14:19:20 -070083
Jeff Tinkerbafb6822013-03-22 15:26:39 -070084 closeFactory();
Andreas Huber16087352012-04-13 14:54:36 -070085
Jeff Tinkerbafb6822013-03-22 15:26:39 -070086 // lock static maps
87 Mutex::Autolock autoLock(mMapLock);
88
89 // first check cache
90 Vector<uint8_t> uuidVector;
91 uuidVector.appendArray(uuid, sizeof(uuid));
92 ssize_t index = mUUIDToLibraryPathMap.indexOfKey(uuidVector);
93 if (index >= 0) {
94 if (loadLibraryForScheme(mUUIDToLibraryPathMap[index], uuid)) {
95 mInitCheck = OK;
96 return;
97 } else {
98 ALOGE("Failed to load from cached library path!");
99 mInitCheck = ERROR_UNSUPPORTED;
100 return;
101 }
102 }
103
104 // no luck, have to search
105 String8 dirPath("/vendor/lib/mediadrm");
106 String8 pluginPath;
107
108 DIR* pDir = opendir(dirPath.string());
109 if (pDir) {
110 struct dirent* pEntry;
111 while ((pEntry = readdir(pDir))) {
112
113 pluginPath = dirPath + "/" + pEntry->d_name;
114
115 if (pluginPath.getPathExtension() == ".so") {
116
117 if (loadLibraryForScheme(pluginPath, uuid)) {
118 mUUIDToLibraryPathMap.add(uuidVector, pluginPath);
119 mInitCheck = OK;
120 closedir(pDir);
121 return;
122 }
123 }
124 }
125
126 closedir(pDir);
127 }
128
129 // try the legacy libdrmdecrypt.so
130 pluginPath = "libdrmdecrypt.so";
131 if (loadLibraryForScheme(pluginPath, uuid)) {
132 mUUIDToLibraryPathMap.add(uuidVector, pluginPath);
133 mInitCheck = OK;
134 return;
135 }
136
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700137 mInitCheck = ERROR_UNSUPPORTED;
138}
139
140bool Crypto::loadLibraryForScheme(const String8 &path, const uint8_t uuid[16]) {
141
142 // get strong pointer to open shared library
143 ssize_t index = mLibraryPathToOpenLibraryMap.indexOfKey(path);
144 if (index >= 0) {
145 mLibrary = mLibraryPathToOpenLibraryMap[index].promote();
146 } else {
147 index = mLibraryPathToOpenLibraryMap.add(path, NULL);
148 }
149
150 if (!mLibrary.get()) {
151 mLibrary = new SharedLibrary(path);
152 if (!*mLibrary) {
leozwang@google.comd1eff572013-07-13 21:52:50 -0700153 ALOGE("loadLibraryForScheme failed:%s", mLibrary->lastError());
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700154 return false;
155 }
156
157 mLibraryPathToOpenLibraryMap.replaceValueAt(index, mLibrary);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700158 }
159
160 typedef CryptoFactory *(*CreateCryptoFactoryFunc)();
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700161
Andreas Huber1bd139a2012-04-03 14:19:20 -0700162 CreateCryptoFactoryFunc createCryptoFactory =
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700163 (CreateCryptoFactoryFunc)mLibrary->lookup("createCryptoFactory");
Andreas Huber1bd139a2012-04-03 14:19:20 -0700164
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700165 if (createCryptoFactory == NULL ||
166 (mFactory = createCryptoFactory()) == NULL ||
167 !mFactory->isCryptoSchemeSupported(uuid)) {
leozwang@google.comd1eff572013-07-13 21:52:50 -0700168 ALOGE("createCryptoFactory failed:%s", mLibrary->lastError());
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700169 closeFactory();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700170 return false;
171 }
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700172 return true;
173}
Andreas Huber1bd139a2012-04-03 14:19:20 -0700174
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700175bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) {
176 Mutex::Autolock autoLock(mLock);
177
178 if (mFactory && mFactory->isCryptoSchemeSupported(uuid)) {
179 return true;
180 }
181
182 findFactoryForScheme(uuid);
183 return (mInitCheck == OK);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700184}
185
Andreas Huber1bd139a2012-04-03 14:19:20 -0700186status_t Crypto::createPlugin(
187 const uint8_t uuid[16], const void *data, size_t size) {
188 Mutex::Autolock autoLock(mLock);
189
Andreas Huber1bd139a2012-04-03 14:19:20 -0700190 if (mPlugin != NULL) {
191 return -EINVAL;
192 }
193
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700194 if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) {
195 findFactoryForScheme(uuid);
196 }
197
198 if (mInitCheck != OK) {
199 return mInitCheck;
200 }
201
Andreas Huber1bd139a2012-04-03 14:19:20 -0700202 return mFactory->createPlugin(uuid, data, size, &mPlugin);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700203}
204
Andreas Huber1bd139a2012-04-03 14:19:20 -0700205status_t Crypto::destroyPlugin() {
206 Mutex::Autolock autoLock(mLock);
207
208 if (mInitCheck != OK) {
209 return mInitCheck;
210 }
211
212 if (mPlugin == NULL) {
213 return -EINVAL;
214 }
215
216 delete mPlugin;
217 mPlugin = NULL;
218
219 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700220}
221
Andreas Huber1bd139a2012-04-03 14:19:20 -0700222bool Crypto::requiresSecureDecoderComponent(const char *mime) const {
223 Mutex::Autolock autoLock(mLock);
224
225 if (mInitCheck != OK) {
226 return mInitCheck;
227 }
228
229 if (mPlugin == NULL) {
230 return -EINVAL;
231 }
232
233 return mPlugin->requiresSecureDecoderComponent(mime);
234}
235
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700236ssize_t Crypto::decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700237 bool secure,
238 const uint8_t key[16],
239 const uint8_t iv[16],
240 CryptoPlugin::Mode mode,
241 const void *srcPtr,
242 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700243 void *dstPtr,
244 AString *errorDetailMsg) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700245 Mutex::Autolock autoLock(mLock);
246
247 if (mInitCheck != OK) {
248 return mInitCheck;
249 }
250
251 if (mPlugin == NULL) {
252 return -EINVAL;
253 }
254
255 return mPlugin->decrypt(
Andreas Huber5b8987e2012-04-19 12:52:20 -0700256 secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr,
257 errorDetailMsg);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700258}
259
260} // namespace android