Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "Drm" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <dlfcn.h> |
| 23 | |
| 24 | #include "Drm.h" |
| 25 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 26 | #include "DrmSessionClientInterface.h" |
| 27 | #include "DrmSessionManager.h" |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 28 | #include <media/drm/DrmAPI.h> |
| 29 | #include <media/stagefright/foundation/ADebug.h> |
| 30 | #include <media/stagefright/foundation/AString.h> |
| 31 | #include <media/stagefright/foundation/hexdump.h> |
| 32 | #include <media/stagefright/MediaErrors.h> |
Jeff Tinker | 81e0bd4 | 2014-04-02 16:41:38 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
| 34 | #include <binder/IPCThreadState.h> |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 38 | static inline int getCallingPid() { |
| 39 | return IPCThreadState::self()->getCallingPid(); |
| 40 | } |
| 41 | |
Jeff Tinker | 81e0bd4 | 2014-04-02 16:41:38 -0700 | [diff] [blame] | 42 | static bool checkPermission(const char* permissionString) { |
| 43 | #ifndef HAVE_ANDROID_OS |
| 44 | return true; |
| 45 | #endif |
| 46 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 47 | bool ok = checkCallingPermission(String16(permissionString)); |
| 48 | if (!ok) ALOGE("Request requires %s", permissionString); |
| 49 | return ok; |
| 50 | } |
| 51 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 52 | KeyedVector<Vector<uint8_t>, String8> Drm::mUUIDToLibraryPathMap; |
| 53 | KeyedVector<String8, wp<SharedLibrary> > Drm::mLibraryPathToOpenLibraryMap; |
| 54 | Mutex Drm::mMapLock; |
| 55 | |
| 56 | static bool operator<(const Vector<uint8_t> &lhs, const Vector<uint8_t> &rhs) { |
| 57 | if (lhs.size() < rhs.size()) { |
| 58 | return true; |
| 59 | } else if (lhs.size() > rhs.size()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return memcmp((void *)lhs.array(), (void *)rhs.array(), rhs.size()) < 0; |
| 64 | } |
| 65 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 66 | struct DrmSessionClient : public DrmSessionClientInterface { |
| 67 | DrmSessionClient(Drm* drm) : mDrm(drm) {} |
| 68 | |
| 69 | virtual bool reclaimSession(const Vector<uint8_t>& sessionId) { |
| 70 | sp<Drm> drm = mDrm.promote(); |
| 71 | if (drm == NULL) { |
| 72 | return true; |
| 73 | } |
| 74 | status_t err = drm->closeSession(sessionId); |
| 75 | if (err != OK) { |
| 76 | return false; |
| 77 | } |
| 78 | drm->sendEvent(DrmPlugin::kDrmPluginEventSessionReclaimed, 0, &sessionId, NULL); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | virtual ~DrmSessionClient() {} |
| 84 | |
| 85 | private: |
| 86 | wp<Drm> mDrm; |
| 87 | |
| 88 | DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient); |
| 89 | }; |
| 90 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 91 | Drm::Drm() |
| 92 | : mInitCheck(NO_INIT), |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 93 | mDrmSessionClient(new DrmSessionClient(this)), |
Jeff Tinker | 0cb126a | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 94 | mListener(NULL), |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 95 | mFactory(NULL), |
| 96 | mPlugin(NULL) { |
| 97 | } |
| 98 | |
| 99 | Drm::~Drm() { |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 100 | DrmSessionManager::Instance()->removeDrm(mDrmSessionClient); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 101 | delete mPlugin; |
| 102 | mPlugin = NULL; |
| 103 | closeFactory(); |
| 104 | } |
| 105 | |
| 106 | void Drm::closeFactory() { |
| 107 | delete mFactory; |
| 108 | mFactory = NULL; |
| 109 | mLibrary.clear(); |
| 110 | } |
| 111 | |
| 112 | status_t Drm::initCheck() const { |
| 113 | return mInitCheck; |
| 114 | } |
| 115 | |
Jeff Tinker | 0cb126a | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 116 | status_t Drm::setListener(const sp<IDrmClient>& listener) |
| 117 | { |
| 118 | Mutex::Autolock lock(mEventLock); |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 119 | if (mListener != NULL){ |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 120 | IInterface::asBinder(mListener)->unlinkToDeath(this); |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 121 | } |
| 122 | if (listener != NULL) { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 123 | IInterface::asBinder(listener)->linkToDeath(this); |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 124 | } |
Jeff Tinker | 0cb126a | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 125 | mListener = listener; |
| 126 | return NO_ERROR; |
| 127 | } |
| 128 | |
| 129 | void Drm::sendEvent(DrmPlugin::EventType eventType, int extra, |
| 130 | Vector<uint8_t> const *sessionId, |
| 131 | Vector<uint8_t> const *data) |
| 132 | { |
| 133 | mEventLock.lock(); |
| 134 | sp<IDrmClient> listener = mListener; |
| 135 | mEventLock.unlock(); |
| 136 | |
| 137 | if (listener != NULL) { |
| 138 | Parcel obj; |
Jeff Tinker | 2fb25c8 | 2015-03-31 15:40:16 -0700 | [diff] [blame^] | 139 | writeByteArray(obj, sessionId); |
| 140 | writeByteArray(obj, data); |
Jeff Tinker | 0cb126a | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 141 | |
| 142 | Mutex::Autolock lock(mNotifyLock); |
| 143 | listener->notify(eventType, extra, &obj); |
| 144 | } |
| 145 | } |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 146 | |
Jeff Tinker | 2fb25c8 | 2015-03-31 15:40:16 -0700 | [diff] [blame^] | 147 | void Drm::sendExpirationUpdate(Vector<uint8_t> const *sessionId, |
| 148 | int64_t expiryTimeInMS) |
| 149 | { |
| 150 | mEventLock.lock(); |
| 151 | sp<IDrmClient> listener = mListener; |
| 152 | mEventLock.unlock(); |
| 153 | |
| 154 | if (listener != NULL) { |
| 155 | Parcel obj; |
| 156 | writeByteArray(obj, sessionId); |
| 157 | obj.writeInt64(expiryTimeInMS); |
| 158 | |
| 159 | Mutex::Autolock lock(mNotifyLock); |
| 160 | listener->notify(DrmPlugin::kDrmPluginEventExpirationUpdate, 0, &obj); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void Drm::sendKeysChange(Vector<uint8_t> const *sessionId, |
| 165 | Vector<DrmPlugin::KeyStatus> const *keyStatusList, |
| 166 | bool hasNewUsableKey) |
| 167 | { |
| 168 | mEventLock.lock(); |
| 169 | sp<IDrmClient> listener = mListener; |
| 170 | mEventLock.unlock(); |
| 171 | |
| 172 | if (listener != NULL) { |
| 173 | Parcel obj; |
| 174 | writeByteArray(obj, sessionId); |
| 175 | |
| 176 | size_t nkeys = keyStatusList->size(); |
| 177 | obj.writeInt32(keyStatusList->size()); |
| 178 | for (size_t i = 0; i < nkeys; ++i) { |
| 179 | const DrmPlugin::KeyStatus *keyStatus = &keyStatusList->itemAt(i); |
| 180 | writeByteArray(obj, &keyStatus->mKeyId); |
| 181 | obj.writeInt32(keyStatus->mType); |
| 182 | } |
| 183 | obj.writeInt32(hasNewUsableKey); |
| 184 | |
| 185 | Mutex::Autolock lock(mNotifyLock); |
| 186 | listener->notify(DrmPlugin::kDrmPluginEventKeysChange, 0, &obj); |
| 187 | } |
| 188 | } |
| 189 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 190 | /* |
| 191 | * Search the plugins directory for a plugin that supports the scheme |
| 192 | * specified by uuid |
| 193 | * |
| 194 | * If found: |
| 195 | * mLibrary holds a strong pointer to the dlopen'd library |
| 196 | * mFactory is set to the library's factory method |
| 197 | * mInitCheck is set to OK |
| 198 | * |
| 199 | * If not found: |
| 200 | * mLibrary is cleared and mFactory are set to NULL |
| 201 | * mInitCheck is set to an error (!OK) |
| 202 | */ |
| 203 | void Drm::findFactoryForScheme(const uint8_t uuid[16]) { |
| 204 | |
| 205 | closeFactory(); |
| 206 | |
| 207 | // lock static maps |
| 208 | Mutex::Autolock autoLock(mMapLock); |
| 209 | |
| 210 | // first check cache |
| 211 | Vector<uint8_t> uuidVector; |
| 212 | uuidVector.appendArray(uuid, sizeof(uuid)); |
| 213 | ssize_t index = mUUIDToLibraryPathMap.indexOfKey(uuidVector); |
| 214 | if (index >= 0) { |
| 215 | if (loadLibraryForScheme(mUUIDToLibraryPathMap[index], uuid)) { |
| 216 | mInitCheck = OK; |
| 217 | return; |
| 218 | } else { |
| 219 | ALOGE("Failed to load from cached library path!"); |
| 220 | mInitCheck = ERROR_UNSUPPORTED; |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // no luck, have to search |
| 226 | String8 dirPath("/vendor/lib/mediadrm"); |
| 227 | DIR* pDir = opendir(dirPath.string()); |
| 228 | |
| 229 | if (pDir == NULL) { |
| 230 | mInitCheck = ERROR_UNSUPPORTED; |
| 231 | ALOGE("Failed to open plugin directory %s", dirPath.string()); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | struct dirent* pEntry; |
| 237 | while ((pEntry = readdir(pDir))) { |
| 238 | |
| 239 | String8 pluginPath = dirPath + "/" + pEntry->d_name; |
| 240 | |
| 241 | if (pluginPath.getPathExtension() == ".so") { |
| 242 | |
| 243 | if (loadLibraryForScheme(pluginPath, uuid)) { |
| 244 | mUUIDToLibraryPathMap.add(uuidVector, pluginPath); |
| 245 | mInitCheck = OK; |
| 246 | closedir(pDir); |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | closedir(pDir); |
| 253 | |
| 254 | ALOGE("Failed to find drm plugin"); |
| 255 | mInitCheck = ERROR_UNSUPPORTED; |
| 256 | } |
| 257 | |
| 258 | bool Drm::loadLibraryForScheme(const String8 &path, const uint8_t uuid[16]) { |
| 259 | |
| 260 | // get strong pointer to open shared library |
| 261 | ssize_t index = mLibraryPathToOpenLibraryMap.indexOfKey(path); |
| 262 | if (index >= 0) { |
| 263 | mLibrary = mLibraryPathToOpenLibraryMap[index].promote(); |
| 264 | } else { |
| 265 | index = mLibraryPathToOpenLibraryMap.add(path, NULL); |
| 266 | } |
| 267 | |
| 268 | if (!mLibrary.get()) { |
| 269 | mLibrary = new SharedLibrary(path); |
| 270 | if (!*mLibrary) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | mLibraryPathToOpenLibraryMap.replaceValueAt(index, mLibrary); |
| 275 | } |
| 276 | |
| 277 | typedef DrmFactory *(*CreateDrmFactoryFunc)(); |
| 278 | |
| 279 | CreateDrmFactoryFunc createDrmFactory = |
| 280 | (CreateDrmFactoryFunc)mLibrary->lookup("createDrmFactory"); |
| 281 | |
| 282 | if (createDrmFactory == NULL || |
| 283 | (mFactory = createDrmFactory()) == NULL || |
| 284 | !mFactory->isCryptoSchemeSupported(uuid)) { |
| 285 | closeFactory(); |
| 286 | return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
Jeff Tinker | 9cf69e0 | 2013-08-21 11:59:23 -0700 | [diff] [blame] | 291 | bool Drm::isCryptoSchemeSupported(const uint8_t uuid[16], const String8 &mimeType) { |
| 292 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 293 | Mutex::Autolock autoLock(mLock); |
| 294 | |
Jeff Tinker | 9cf69e0 | 2013-08-21 11:59:23 -0700 | [diff] [blame] | 295 | if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) { |
| 296 | findFactoryForScheme(uuid); |
| 297 | if (mInitCheck != OK) { |
| 298 | return false; |
| 299 | } |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Jeff Tinker | ee7e77d | 2013-08-28 16:40:41 -0700 | [diff] [blame] | 302 | if (mimeType != "") { |
| 303 | return mFactory->isContentTypeSupported(mimeType); |
| 304 | } |
| 305 | |
| 306 | return true; |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | status_t Drm::createPlugin(const uint8_t uuid[16]) { |
| 310 | Mutex::Autolock autoLock(mLock); |
| 311 | |
| 312 | if (mPlugin != NULL) { |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
| 316 | if (!mFactory || !mFactory->isCryptoSchemeSupported(uuid)) { |
| 317 | findFactoryForScheme(uuid); |
| 318 | } |
| 319 | |
| 320 | if (mInitCheck != OK) { |
| 321 | return mInitCheck; |
| 322 | } |
| 323 | |
Jeff Tinker | 0cb126a | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 324 | status_t result = mFactory->createDrmPlugin(uuid, &mPlugin); |
| 325 | mPlugin->setListener(this); |
| 326 | return result; |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | status_t Drm::destroyPlugin() { |
| 330 | Mutex::Autolock autoLock(mLock); |
| 331 | |
| 332 | if (mInitCheck != OK) { |
| 333 | return mInitCheck; |
| 334 | } |
| 335 | |
| 336 | if (mPlugin == NULL) { |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | delete mPlugin; |
| 341 | mPlugin = NULL; |
| 342 | |
| 343 | return OK; |
| 344 | } |
| 345 | |
| 346 | status_t Drm::openSession(Vector<uint8_t> &sessionId) { |
| 347 | Mutex::Autolock autoLock(mLock); |
| 348 | |
| 349 | if (mInitCheck != OK) { |
| 350 | return mInitCheck; |
| 351 | } |
| 352 | |
| 353 | if (mPlugin == NULL) { |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 357 | status_t err = mPlugin->openSession(sessionId); |
| 358 | if (err == ERROR_DRM_RESOURCE_BUSY) { |
| 359 | bool retry = false; |
| 360 | retry = DrmSessionManager::Instance()->reclaimSession(getCallingPid()); |
| 361 | if (retry) { |
| 362 | err = mPlugin->openSession(sessionId); |
| 363 | } |
| 364 | } |
| 365 | if (err == OK) { |
| 366 | DrmSessionManager::Instance()->addSession(getCallingPid(), mDrmSessionClient, sessionId); |
| 367 | } |
| 368 | return err; |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | status_t Drm::closeSession(Vector<uint8_t> const &sessionId) { |
| 372 | Mutex::Autolock autoLock(mLock); |
| 373 | |
| 374 | if (mInitCheck != OK) { |
| 375 | return mInitCheck; |
| 376 | } |
| 377 | |
| 378 | if (mPlugin == NULL) { |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 382 | status_t err = mPlugin->closeSession(sessionId); |
| 383 | if (err == OK) { |
| 384 | DrmSessionManager::Instance()->removeSession(sessionId); |
| 385 | } |
| 386 | return err; |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 389 | status_t Drm::getKeyRequest(Vector<uint8_t> const &sessionId, |
| 390 | Vector<uint8_t> const &initData, |
| 391 | String8 const &mimeType, DrmPlugin::KeyType keyType, |
| 392 | KeyedVector<String8, String8> const &optionalParameters, |
Jeff Tinker | d072c90 | 2015-03-16 13:39:29 -0700 | [diff] [blame] | 393 | Vector<uint8_t> &request, String8 &defaultUrl, |
| 394 | DrmPlugin::KeyRequestType *keyRequestType) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 395 | Mutex::Autolock autoLock(mLock); |
| 396 | |
| 397 | if (mInitCheck != OK) { |
| 398 | return mInitCheck; |
| 399 | } |
| 400 | |
| 401 | if (mPlugin == NULL) { |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 405 | DrmSessionManager::Instance()->useSession(sessionId); |
| 406 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 407 | return mPlugin->getKeyRequest(sessionId, initData, mimeType, keyType, |
Jeff Tinker | d072c90 | 2015-03-16 13:39:29 -0700 | [diff] [blame] | 408 | optionalParameters, request, defaultUrl, |
| 409 | keyRequestType); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 412 | status_t Drm::provideKeyResponse(Vector<uint8_t> const &sessionId, |
| 413 | Vector<uint8_t> const &response, |
| 414 | Vector<uint8_t> &keySetId) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 415 | Mutex::Autolock autoLock(mLock); |
| 416 | |
| 417 | if (mInitCheck != OK) { |
| 418 | return mInitCheck; |
| 419 | } |
| 420 | |
| 421 | if (mPlugin == NULL) { |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 425 | DrmSessionManager::Instance()->useSession(sessionId); |
| 426 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 427 | return mPlugin->provideKeyResponse(sessionId, response, keySetId); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 430 | status_t Drm::removeKeys(Vector<uint8_t> const &keySetId) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 431 | Mutex::Autolock autoLock(mLock); |
| 432 | |
| 433 | if (mInitCheck != OK) { |
| 434 | return mInitCheck; |
| 435 | } |
| 436 | |
| 437 | if (mPlugin == NULL) { |
| 438 | return -EINVAL; |
| 439 | } |
| 440 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 441 | return mPlugin->removeKeys(keySetId); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 444 | status_t Drm::restoreKeys(Vector<uint8_t> const &sessionId, |
| 445 | Vector<uint8_t> const &keySetId) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 446 | Mutex::Autolock autoLock(mLock); |
| 447 | |
| 448 | if (mInitCheck != OK) { |
| 449 | return mInitCheck; |
| 450 | } |
| 451 | |
| 452 | if (mPlugin == NULL) { |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 456 | DrmSessionManager::Instance()->useSession(sessionId); |
| 457 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 458 | return mPlugin->restoreKeys(sessionId, keySetId); |
| 459 | } |
| 460 | |
| 461 | status_t Drm::queryKeyStatus(Vector<uint8_t> const &sessionId, |
| 462 | KeyedVector<String8, String8> &infoMap) const { |
| 463 | Mutex::Autolock autoLock(mLock); |
| 464 | |
| 465 | if (mInitCheck != OK) { |
| 466 | return mInitCheck; |
| 467 | } |
| 468 | |
| 469 | if (mPlugin == NULL) { |
| 470 | return -EINVAL; |
| 471 | } |
| 472 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 473 | DrmSessionManager::Instance()->useSession(sessionId); |
| 474 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 475 | return mPlugin->queryKeyStatus(sessionId, infoMap); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 478 | status_t Drm::getProvisionRequest(String8 const &certType, String8 const &certAuthority, |
| 479 | Vector<uint8_t> &request, String8 &defaultUrl) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 480 | Mutex::Autolock autoLock(mLock); |
| 481 | |
| 482 | if (mInitCheck != OK) { |
| 483 | return mInitCheck; |
| 484 | } |
| 485 | |
| 486 | if (mPlugin == NULL) { |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 490 | return mPlugin->getProvisionRequest(certType, certAuthority, |
| 491 | request, defaultUrl); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 494 | status_t Drm::provideProvisionResponse(Vector<uint8_t> const &response, |
| 495 | Vector<uint8_t> &certificate, |
| 496 | Vector<uint8_t> &wrappedKey) { |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 497 | Mutex::Autolock autoLock(mLock); |
| 498 | |
| 499 | if (mInitCheck != OK) { |
| 500 | return mInitCheck; |
| 501 | } |
| 502 | |
| 503 | if (mPlugin == NULL) { |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 507 | return mPlugin->provideProvisionResponse(response, certificate, wrappedKey); |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Jeff Tinker | 68b1555 | 2014-04-30 10:19:03 -0700 | [diff] [blame] | 510 | status_t Drm::unprovisionDevice() { |
| 511 | Mutex::Autolock autoLock(mLock); |
| 512 | |
| 513 | if (mInitCheck != OK) { |
| 514 | return mInitCheck; |
| 515 | } |
| 516 | |
| 517 | if (mPlugin == NULL) { |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | if (!checkPermission("android.permission.REMOVE_DRM_CERTIFICATES")) { |
| 522 | return -EPERM; |
| 523 | } |
| 524 | |
| 525 | return mPlugin->unprovisionDevice(); |
| 526 | } |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 527 | |
| 528 | status_t Drm::getSecureStops(List<Vector<uint8_t> > &secureStops) { |
| 529 | Mutex::Autolock autoLock(mLock); |
| 530 | |
| 531 | if (mInitCheck != OK) { |
| 532 | return mInitCheck; |
| 533 | } |
| 534 | |
| 535 | if (mPlugin == NULL) { |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | return mPlugin->getSecureStops(secureStops); |
| 540 | } |
| 541 | |
Jeff Tinker | 3c1285e | 2014-10-31 00:55:16 -0700 | [diff] [blame] | 542 | status_t Drm::getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) { |
| 543 | Mutex::Autolock autoLock(mLock); |
| 544 | |
| 545 | if (mInitCheck != OK) { |
| 546 | return mInitCheck; |
| 547 | } |
| 548 | |
| 549 | if (mPlugin == NULL) { |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | |
| 553 | return mPlugin->getSecureStop(ssid, secureStop); |
| 554 | } |
| 555 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 556 | status_t Drm::releaseSecureStops(Vector<uint8_t> const &ssRelease) { |
| 557 | Mutex::Autolock autoLock(mLock); |
| 558 | |
| 559 | if (mInitCheck != OK) { |
| 560 | return mInitCheck; |
| 561 | } |
| 562 | |
| 563 | if (mPlugin == NULL) { |
| 564 | return -EINVAL; |
| 565 | } |
| 566 | |
| 567 | return mPlugin->releaseSecureStops(ssRelease); |
| 568 | } |
| 569 | |
Jeff Tinker | 3c1285e | 2014-10-31 00:55:16 -0700 | [diff] [blame] | 570 | status_t Drm::releaseAllSecureStops() { |
| 571 | Mutex::Autolock autoLock(mLock); |
| 572 | |
| 573 | if (mInitCheck != OK) { |
| 574 | return mInitCheck; |
| 575 | } |
| 576 | |
| 577 | if (mPlugin == NULL) { |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | return mPlugin->releaseAllSecureStops(); |
| 582 | } |
| 583 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 584 | status_t Drm::getPropertyString(String8 const &name, String8 &value ) const { |
| 585 | Mutex::Autolock autoLock(mLock); |
| 586 | |
| 587 | if (mInitCheck != OK) { |
| 588 | return mInitCheck; |
| 589 | } |
| 590 | |
| 591 | if (mPlugin == NULL) { |
| 592 | return -EINVAL; |
| 593 | } |
| 594 | |
| 595 | return mPlugin->getPropertyString(name, value); |
| 596 | } |
| 597 | |
| 598 | status_t Drm::getPropertyByteArray(String8 const &name, Vector<uint8_t> &value ) const { |
| 599 | Mutex::Autolock autoLock(mLock); |
| 600 | |
| 601 | if (mInitCheck != OK) { |
| 602 | return mInitCheck; |
| 603 | } |
| 604 | |
| 605 | if (mPlugin == NULL) { |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | |
| 609 | return mPlugin->getPropertyByteArray(name, value); |
| 610 | } |
| 611 | |
| 612 | status_t Drm::setPropertyString(String8 const &name, String8 const &value ) const { |
| 613 | Mutex::Autolock autoLock(mLock); |
| 614 | |
| 615 | if (mInitCheck != OK) { |
| 616 | return mInitCheck; |
| 617 | } |
| 618 | |
| 619 | if (mPlugin == NULL) { |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
| 623 | return mPlugin->setPropertyString(name, value); |
| 624 | } |
| 625 | |
| 626 | status_t Drm::setPropertyByteArray(String8 const &name, |
| 627 | Vector<uint8_t> const &value ) const { |
| 628 | Mutex::Autolock autoLock(mLock); |
| 629 | |
| 630 | if (mInitCheck != OK) { |
| 631 | return mInitCheck; |
| 632 | } |
| 633 | |
| 634 | if (mPlugin == NULL) { |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | |
| 638 | return mPlugin->setPropertyByteArray(name, value); |
| 639 | } |
| 640 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 641 | |
| 642 | status_t Drm::setCipherAlgorithm(Vector<uint8_t> const &sessionId, |
| 643 | String8 const &algorithm) { |
| 644 | Mutex::Autolock autoLock(mLock); |
| 645 | |
| 646 | if (mInitCheck != OK) { |
| 647 | return mInitCheck; |
| 648 | } |
| 649 | |
| 650 | if (mPlugin == NULL) { |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 654 | DrmSessionManager::Instance()->useSession(sessionId); |
| 655 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 656 | return mPlugin->setCipherAlgorithm(sessionId, algorithm); |
| 657 | } |
| 658 | |
| 659 | status_t Drm::setMacAlgorithm(Vector<uint8_t> const &sessionId, |
| 660 | String8 const &algorithm) { |
| 661 | Mutex::Autolock autoLock(mLock); |
| 662 | |
| 663 | if (mInitCheck != OK) { |
| 664 | return mInitCheck; |
| 665 | } |
| 666 | |
| 667 | if (mPlugin == NULL) { |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 671 | DrmSessionManager::Instance()->useSession(sessionId); |
| 672 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 673 | return mPlugin->setMacAlgorithm(sessionId, algorithm); |
| 674 | } |
| 675 | |
| 676 | status_t Drm::encrypt(Vector<uint8_t> const &sessionId, |
| 677 | Vector<uint8_t> const &keyId, |
| 678 | Vector<uint8_t> const &input, |
| 679 | Vector<uint8_t> const &iv, |
| 680 | Vector<uint8_t> &output) { |
| 681 | Mutex::Autolock autoLock(mLock); |
| 682 | |
| 683 | if (mInitCheck != OK) { |
| 684 | return mInitCheck; |
| 685 | } |
| 686 | |
| 687 | if (mPlugin == NULL) { |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 691 | DrmSessionManager::Instance()->useSession(sessionId); |
| 692 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 693 | return mPlugin->encrypt(sessionId, keyId, input, iv, output); |
| 694 | } |
| 695 | |
| 696 | status_t Drm::decrypt(Vector<uint8_t> const &sessionId, |
| 697 | Vector<uint8_t> const &keyId, |
| 698 | Vector<uint8_t> const &input, |
| 699 | Vector<uint8_t> const &iv, |
| 700 | Vector<uint8_t> &output) { |
| 701 | Mutex::Autolock autoLock(mLock); |
| 702 | |
| 703 | if (mInitCheck != OK) { |
| 704 | return mInitCheck; |
| 705 | } |
| 706 | |
| 707 | if (mPlugin == NULL) { |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 711 | DrmSessionManager::Instance()->useSession(sessionId); |
| 712 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 713 | return mPlugin->decrypt(sessionId, keyId, input, iv, output); |
| 714 | } |
| 715 | |
| 716 | status_t Drm::sign(Vector<uint8_t> const &sessionId, |
| 717 | Vector<uint8_t> const &keyId, |
| 718 | Vector<uint8_t> const &message, |
| 719 | Vector<uint8_t> &signature) { |
| 720 | Mutex::Autolock autoLock(mLock); |
| 721 | |
| 722 | if (mInitCheck != OK) { |
| 723 | return mInitCheck; |
| 724 | } |
| 725 | |
| 726 | if (mPlugin == NULL) { |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 730 | DrmSessionManager::Instance()->useSession(sessionId); |
| 731 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 732 | return mPlugin->sign(sessionId, keyId, message, signature); |
| 733 | } |
| 734 | |
| 735 | status_t Drm::verify(Vector<uint8_t> const &sessionId, |
| 736 | Vector<uint8_t> const &keyId, |
| 737 | Vector<uint8_t> const &message, |
| 738 | Vector<uint8_t> const &signature, |
| 739 | bool &match) { |
| 740 | Mutex::Autolock autoLock(mLock); |
| 741 | |
| 742 | if (mInitCheck != OK) { |
| 743 | return mInitCheck; |
| 744 | } |
| 745 | |
| 746 | if (mPlugin == NULL) { |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 750 | DrmSessionManager::Instance()->useSession(sessionId); |
| 751 | |
Jeff Tinker | 8856c8b | 2013-03-30 16:19:44 -0700 | [diff] [blame] | 752 | return mPlugin->verify(sessionId, keyId, message, signature, match); |
| 753 | } |
| 754 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 755 | status_t Drm::signRSA(Vector<uint8_t> const &sessionId, |
| 756 | String8 const &algorithm, |
| 757 | Vector<uint8_t> const &message, |
| 758 | Vector<uint8_t> const &wrappedKey, |
| 759 | Vector<uint8_t> &signature) { |
| 760 | Mutex::Autolock autoLock(mLock); |
| 761 | |
| 762 | if (mInitCheck != OK) { |
| 763 | return mInitCheck; |
| 764 | } |
| 765 | |
| 766 | if (mPlugin == NULL) { |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
Jeff Tinker | 81e0bd4 | 2014-04-02 16:41:38 -0700 | [diff] [blame] | 770 | if (!checkPermission("android.permission.ACCESS_DRM_CERTIFICATES")) { |
| 771 | return -EPERM; |
| 772 | } |
| 773 | |
Ronghua Wu | 5c3da20 | 2015-02-22 08:45:28 -0800 | [diff] [blame] | 774 | DrmSessionManager::Instance()->useSession(sessionId); |
| 775 | |
Jeff Tinker | 68d9d71 | 2014-03-04 13:21:31 -0800 | [diff] [blame] | 776 | return mPlugin->signRSA(sessionId, algorithm, message, wrappedKey, signature); |
| 777 | } |
| 778 | |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 779 | void Drm::binderDied(const wp<IBinder> &the_late_who) |
| 780 | { |
Jeff Tinker | 4dbc8cc | 2014-11-16 11:52:03 -0800 | [diff] [blame] | 781 | mEventLock.lock(); |
| 782 | mListener.clear(); |
| 783 | mEventLock.unlock(); |
| 784 | |
| 785 | Mutex::Autolock autoLock(mLock); |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 786 | delete mPlugin; |
| 787 | mPlugin = NULL; |
| 788 | closeFactory(); |
Jeff Tinker | 3d3f67f | 2013-07-03 15:38:58 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Jeff Tinker | 2fb25c8 | 2015-03-31 15:40:16 -0700 | [diff] [blame^] | 791 | void Drm::writeByteArray(Parcel &obj, Vector<uint8_t> const *array) |
| 792 | { |
| 793 | if (array && array->size()) { |
| 794 | obj.writeInt32(array->size()); |
| 795 | obj.write(array->array(), array->size()); |
| 796 | } else { |
| 797 | obj.writeInt32(0); |
| 798 | } |
| 799 | } |
| 800 | |
Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 801 | } // namespace android |