blob: ae4d845fbc2ed63437a0c80247582c917824de7d [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
137 ALOGE("Failed to find crypto plugin");
138 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) {
154 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)) {
168 closeFactory();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700169 return false;
170 }
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700171 return true;
172}
Andreas Huber1bd139a2012-04-03 14:19:20 -0700173
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700174bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) {
175 Mutex::Autolock autoLock(mLock);
176
177 if (mFactory && mFactory->isCryptoSchemeSupported(uuid)) {
178 return true;
179 }
180
181 findFactoryForScheme(uuid);
182 return (mInitCheck == OK);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700183}
184
Andreas Huber1bd139a2012-04-03 14:19:20 -0700185status_t Crypto::createPlugin(
186 const uint8_t uuid[16], const void *data, size_t size) {
187 Mutex::Autolock autoLock(mLock);
188
Andreas Huber1bd139a2012-04-03 14:19:20 -0700189 if (mPlugin != NULL) {
190 return -EINVAL;
191 }
192
Jeff Tinkerbafb6822013-03-22 15:26:39 -0700193 if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) {
194 findFactoryForScheme(uuid);
195 }
196
197 if (mInitCheck != OK) {
198 return mInitCheck;
199 }
200
Andreas Huber1bd139a2012-04-03 14:19:20 -0700201 return mFactory->createPlugin(uuid, data, size, &mPlugin);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700202}
203
Andreas Huber1bd139a2012-04-03 14:19:20 -0700204status_t Crypto::destroyPlugin() {
205 Mutex::Autolock autoLock(mLock);
206
207 if (mInitCheck != OK) {
208 return mInitCheck;
209 }
210
211 if (mPlugin == NULL) {
212 return -EINVAL;
213 }
214
215 delete mPlugin;
216 mPlugin = NULL;
217
218 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700219}
220
Andreas Huber1bd139a2012-04-03 14:19:20 -0700221bool Crypto::requiresSecureDecoderComponent(const char *mime) const {
222 Mutex::Autolock autoLock(mLock);
223
224 if (mInitCheck != OK) {
225 return mInitCheck;
226 }
227
228 if (mPlugin == NULL) {
229 return -EINVAL;
230 }
231
232 return mPlugin->requiresSecureDecoderComponent(mime);
233}
234
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700235ssize_t Crypto::decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700236 bool secure,
237 const uint8_t key[16],
238 const uint8_t iv[16],
239 CryptoPlugin::Mode mode,
240 const void *srcPtr,
241 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700242 void *dstPtr,
243 AString *errorDetailMsg) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700244 Mutex::Autolock autoLock(mLock);
245
246 if (mInitCheck != OK) {
247 return mInitCheck;
248 }
249
250 if (mPlugin == NULL) {
251 return -EINVAL;
252 }
253
254 return mPlugin->decrypt(
Andreas Huber5b8987e2012-04-19 12:52:20 -0700255 secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr,
256 errorDetailMsg);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700257}
258
259} // namespace android