blob: ba46ffde427361c7b8c5d7c154376ea1b1ef2ab4 [file] [log] [blame]
Jeff Tinkera53d6552017-01-20 00:31:46 -08001/*
2 * Copyright (C) 2017 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 "DrmHal"
Adam Stonefb679e32018-02-07 10:25:48 -080019#include <iomanip>
20
Jeff Tinkera53d6552017-01-20 00:31:46 -080021#include <utils/Log.h>
22
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080025
Jeff Tinkerc8baaba2018-10-23 11:32:36 -070026#include <android/hardware/drm/1.2/types.h>
Peter Kalauskasca5642c2018-11-12 12:34:42 -080027#include <android/hidl/manager/1.2/IServiceManager.h>
Jeff Tinker593111f2017-05-25 16:00:21 -070028#include <hidl/ServiceManagement.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080029
Adam Stonef0e618d2018-01-17 19:20:41 -080030#include <media/EventMetric.h>
John W. Bruce33ecc4f2017-04-03 16:49:05 -070031#include <media/PluginMetricsReporting.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080032#include <media/drm/DrmAPI.h>
33#include <media/stagefright/foundation/ADebug.h>
34#include <media/stagefright/foundation/AString.h>
Adam Stone32494f52018-02-26 22:53:27 -080035#include <media/stagefright/foundation/base64.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080036#include <media/stagefright/foundation/hexdump.h>
37#include <media/stagefright/MediaErrors.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080038#include <mediadrm/DrmHal.h>
39#include <mediadrm/DrmSessionClientInterface.h>
40#include <mediadrm/DrmSessionManager.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080041
Jeff Tinker6d998b62017-12-18 14:37:43 -080042using drm::V1_0::KeyedVector;
Jeff Tinker6d998b62017-12-18 14:37:43 -080043using drm::V1_0::KeyStatusType;
Jeff Tinkerb8684f32018-12-12 08:41:31 -080044using drm::V1_0::KeyRequestType;
Jeff Tinker6d998b62017-12-18 14:37:43 -080045using drm::V1_0::KeyType;
46using drm::V1_0::KeyValue;
Jeff Tinker6d998b62017-12-18 14:37:43 -080047using drm::V1_0::SecureStop;
Jeff Tinker15177d72018-01-25 12:57:55 -080048using drm::V1_0::SecureStopId;
Jeff Tinker6d998b62017-12-18 14:37:43 -080049using drm::V1_0::Status;
Jeff Tinkerc8baaba2018-10-23 11:32:36 -070050using drm::V1_1::HdcpLevel;
51using drm::V1_1::SecureStopRelease;
52using drm::V1_1::SecurityLevel;
53using drm::V1_2::KeySetId;
Adam Stone28f27c32018-02-05 15:07:48 -080054using ::android::hardware::drm::V1_1::DrmMetricGroup;
Jeff Tinkera53d6552017-01-20 00:31:46 -080055using ::android::hardware::hidl_array;
56using ::android::hardware::hidl_string;
57using ::android::hardware::hidl_vec;
58using ::android::hardware::Return;
59using ::android::hardware::Void;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080060using ::android::hidl::manager::V1_0::IServiceManager;
Adam Stone637b7852018-01-30 12:09:36 -080061using ::android::os::PersistableBundle;
Jeff Tinkera53d6552017-01-20 00:31:46 -080062using ::android::sp;
63
Jeff Tinkerb8684f32018-12-12 08:41:31 -080064typedef drm::V1_1::KeyRequestType KeyRequestType_V1_1;
65typedef drm::V1_2::Status Status_V1_2;
Jeff Tinker44c2cc42019-01-14 10:24:18 -080066typedef drm::V1_2::HdcpLevel HdcpLevel_V1_2;
Jeff Tinkerb8684f32018-12-12 08:41:31 -080067
Adam Stonecea91ce2018-01-22 19:23:28 -080068namespace {
69
70// This constant corresponds to the PROPERTY_DEVICE_UNIQUE_ID constant
71// in the MediaDrm API.
72constexpr char kPropertyDeviceUniqueId[] = "deviceUniqueId";
Adam Stone32494f52018-02-26 22:53:27 -080073constexpr char kEqualsSign[] = "=";
Adam Stonecea91ce2018-01-22 19:23:28 -080074
Adam Stone32494f52018-02-26 22:53:27 -080075template<typename T>
76std::string toBase64StringNoPad(const T* data, size_t size) {
Adam Stone630092e2018-05-02 13:06:34 -070077 // Note that the base 64 conversion only works with arrays of single-byte
78 // values. If the source is empty or is not an array of single-byte values,
79 // return empty string.
80 if (size == 0 || sizeof(data[0]) != 1) {
Adam Stone32494f52018-02-26 22:53:27 -080081 return "";
82 }
Adam Stone32494f52018-02-26 22:53:27 -080083
84 android::AString outputString;
85 encodeBase64(data, size, &outputString);
86 // Remove trailing equals padding if it exists.
87 while (outputString.size() > 0 && outputString.endsWith(kEqualsSign)) {
88 outputString.erase(outputString.size() - 1, 1);
89 }
90
91 return std::string(outputString.c_str(), outputString.size());
Adam Stonecea91ce2018-01-22 19:23:28 -080092}
93
Adam Stone32494f52018-02-26 22:53:27 -080094} // anonymous namespace
95
Jeff Tinkera53d6552017-01-20 00:31:46 -080096namespace android {
97
Jeff Tinker6d998b62017-12-18 14:37:43 -080098#define INIT_CHECK() {if (mInitCheck != OK) return mInitCheck;}
99
Jeff Tinkera53d6552017-01-20 00:31:46 -0800100static inline int getCallingPid() {
101 return IPCThreadState::self()->getCallingPid();
102}
103
104static bool checkPermission(const char* permissionString) {
105 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
106 bool ok = checkCallingPermission(String16(permissionString));
107 if (!ok) ALOGE("Request requires %s", permissionString);
108 return ok;
109}
110
111static const Vector<uint8_t> toVector(const hidl_vec<uint8_t> &vec) {
112 Vector<uint8_t> vector;
113 vector.appendArray(vec.data(), vec.size());
114 return *const_cast<const Vector<uint8_t> *>(&vector);
115}
116
117static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
118 hidl_vec<uint8_t> vec;
119 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
120 return vec;
121}
122
123static String8 toString8(const hidl_string &string) {
124 return String8(string.c_str());
125}
126
127static hidl_string toHidlString(const String8& string) {
128 return hidl_string(string.string());
129}
130
Jeff Tinker6d998b62017-12-18 14:37:43 -0800131static DrmPlugin::SecurityLevel toSecurityLevel(SecurityLevel level) {
132 switch(level) {
133 case SecurityLevel::SW_SECURE_CRYPTO:
134 return DrmPlugin::kSecurityLevelSwSecureCrypto;
135 case SecurityLevel::SW_SECURE_DECODE:
136 return DrmPlugin::kSecurityLevelSwSecureDecode;
137 case SecurityLevel::HW_SECURE_CRYPTO:
138 return DrmPlugin::kSecurityLevelHwSecureCrypto;
139 case SecurityLevel::HW_SECURE_DECODE:
140 return DrmPlugin::kSecurityLevelHwSecureDecode;
141 case SecurityLevel::HW_SECURE_ALL:
142 return DrmPlugin::kSecurityLevelHwSecureAll;
143 default:
144 return DrmPlugin::kSecurityLevelUnknown;
145 }
146}
147
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800148static SecurityLevel toHidlSecurityLevel(DrmPlugin::SecurityLevel level) {
149 switch(level) {
150 case DrmPlugin::kSecurityLevelSwSecureCrypto:
151 return SecurityLevel::SW_SECURE_CRYPTO;
152 case DrmPlugin::kSecurityLevelSwSecureDecode:
153 return SecurityLevel::SW_SECURE_DECODE;
154 case DrmPlugin::kSecurityLevelHwSecureCrypto:
155 return SecurityLevel::HW_SECURE_CRYPTO;
156 case DrmPlugin::kSecurityLevelHwSecureDecode:
157 return SecurityLevel::HW_SECURE_DECODE;
158 case DrmPlugin::kSecurityLevelHwSecureAll:
159 return SecurityLevel::HW_SECURE_ALL;
160 default:
161 return SecurityLevel::UNKNOWN;
162 }
163}
164
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700165static DrmPlugin::OfflineLicenseState toOfflineLicenseState(
166 OfflineLicenseState licenseState) {
167 switch(licenseState) {
168 case OfflineLicenseState::USABLE:
169 return DrmPlugin::kOfflineLicenseStateUsable;
170 case OfflineLicenseState::INACTIVE:
Jeff Tinker9a95b0f2019-01-30 17:39:20 -0800171 return DrmPlugin::kOfflineLicenseStateReleased;
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700172 default:
173 return DrmPlugin::kOfflineLicenseStateUnknown;
174 }
175}
176
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800177static DrmPlugin::HdcpLevel toHdcpLevel(HdcpLevel_V1_2 level) {
Jeff Tinker6d998b62017-12-18 14:37:43 -0800178 switch(level) {
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800179 case HdcpLevel_V1_2::HDCP_NONE:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800180 return DrmPlugin::kHdcpNone;
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800181 case HdcpLevel_V1_2::HDCP_V1:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800182 return DrmPlugin::kHdcpV1;
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800183 case HdcpLevel_V1_2::HDCP_V2:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800184 return DrmPlugin::kHdcpV2;
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800185 case HdcpLevel_V1_2::HDCP_V2_1:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800186 return DrmPlugin::kHdcpV2_1;
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800187 case HdcpLevel_V1_2::HDCP_V2_2:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800188 return DrmPlugin::kHdcpV2_2;
Jeff Tinker44c2cc42019-01-14 10:24:18 -0800189 case HdcpLevel_V1_2::HDCP_V2_3:
190 return DrmPlugin::kHdcpV2_3;
191 case HdcpLevel_V1_2::HDCP_NO_OUTPUT:
Jeff Tinker6d998b62017-12-18 14:37:43 -0800192 return DrmPlugin::kHdcpNoOutput;
193 default:
194 return DrmPlugin::kHdcpLevelUnknown;
195 }
196}
Jeff Tinkera53d6552017-01-20 00:31:46 -0800197static ::KeyedVector toHidlKeyedVector(const KeyedVector<String8, String8>&
198 keyedVector) {
199 std::vector<KeyValue> stdKeyedVector;
200 for (size_t i = 0; i < keyedVector.size(); i++) {
201 KeyValue keyValue;
202 keyValue.key = toHidlString(keyedVector.keyAt(i));
203 keyValue.value = toHidlString(keyedVector.valueAt(i));
204 stdKeyedVector.push_back(keyValue);
205 }
206 return ::KeyedVector(stdKeyedVector);
207}
208
209static KeyedVector<String8, String8> toKeyedVector(const ::KeyedVector&
210 hKeyedVector) {
211 KeyedVector<String8, String8> keyedVector;
212 for (size_t i = 0; i < hKeyedVector.size(); i++) {
213 keyedVector.add(toString8(hKeyedVector[i].key),
214 toString8(hKeyedVector[i].value));
215 }
216 return keyedVector;
217}
218
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800219static List<Vector<uint8_t>> toSecureStops(const hidl_vec<SecureStop>&
Jeff Tinkera53d6552017-01-20 00:31:46 -0800220 hSecureStops) {
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800221 List<Vector<uint8_t>> secureStops;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800222 for (size_t i = 0; i < hSecureStops.size(); i++) {
223 secureStops.push_back(toVector(hSecureStops[i].opaqueData));
224 }
225 return secureStops;
226}
227
Jeff Tinker15177d72018-01-25 12:57:55 -0800228static List<Vector<uint8_t>> toSecureStopIds(const hidl_vec<SecureStopId>&
229 hSecureStopIds) {
230 List<Vector<uint8_t>> secureStopIds;
231 for (size_t i = 0; i < hSecureStopIds.size(); i++) {
232 secureStopIds.push_back(toVector(hSecureStopIds[i]));
233 }
234 return secureStopIds;
235}
236
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700237static List<Vector<uint8_t>> toKeySetIds(const hidl_vec<KeySetId>&
238 hKeySetIds) {
239 List<Vector<uint8_t>> keySetIds;
240 for (size_t i = 0; i < hKeySetIds.size(); i++) {
241 keySetIds.push_back(toVector(hKeySetIds[i]));
242 }
243 return keySetIds;
244}
245
Jeff Tinkera53d6552017-01-20 00:31:46 -0800246static status_t toStatusT(Status status) {
247 switch (status) {
248 case Status::OK:
249 return OK;
250 break;
251 case Status::ERROR_DRM_NO_LICENSE:
252 return ERROR_DRM_NO_LICENSE;
253 break;
254 case Status::ERROR_DRM_LICENSE_EXPIRED:
255 return ERROR_DRM_LICENSE_EXPIRED;
256 break;
257 case Status::ERROR_DRM_SESSION_NOT_OPENED:
258 return ERROR_DRM_SESSION_NOT_OPENED;
259 break;
260 case Status::ERROR_DRM_CANNOT_HANDLE:
261 return ERROR_DRM_CANNOT_HANDLE;
262 break;
263 case Status::ERROR_DRM_INVALID_STATE:
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800264 return ERROR_DRM_INVALID_STATE;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800265 break;
266 case Status::BAD_VALUE:
267 return BAD_VALUE;
268 break;
269 case Status::ERROR_DRM_NOT_PROVISIONED:
270 return ERROR_DRM_NOT_PROVISIONED;
271 break;
272 case Status::ERROR_DRM_RESOURCE_BUSY:
273 return ERROR_DRM_RESOURCE_BUSY;
274 break;
275 case Status::ERROR_DRM_DEVICE_REVOKED:
276 return ERROR_DRM_DEVICE_REVOKED;
277 break;
278 case Status::ERROR_DRM_UNKNOWN:
279 default:
280 return ERROR_DRM_UNKNOWN;
281 break;
282 }
283}
284
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800285static status_t toStatusT_1_2(Status_V1_2 status) {
286 switch (status) {
287 case Status_V1_2::ERROR_DRM_RESOURCE_CONTENTION:
288 return ERROR_DRM_RESOURCE_CONTENTION;
289 case Status_V1_2::ERROR_DRM_FRAME_TOO_LARGE:
290 return ERROR_DRM_FRAME_TOO_LARGE;
291 case Status_V1_2::ERROR_DRM_INSUFFICIENT_SECURITY:
292 return ERROR_DRM_INSUFFICIENT_SECURITY;
293 default:
294 return toStatusT(static_cast<Status>(status));
295 }
296}
297
Jeff Tinkera53d6552017-01-20 00:31:46 -0800298
299Mutex DrmHal::mLock;
300
301struct DrmSessionClient : public DrmSessionClientInterface {
302 explicit DrmSessionClient(DrmHal* drm) : mDrm(drm) {}
303
304 virtual bool reclaimSession(const Vector<uint8_t>& sessionId) {
305 sp<DrmHal> drm = mDrm.promote();
306 if (drm == NULL) {
307 return true;
308 }
309 status_t err = drm->closeSession(sessionId);
310 if (err != OK) {
311 return false;
312 }
313 drm->sendEvent(EventType::SESSION_RECLAIMED,
314 toHidlVec(sessionId), hidl_vec<uint8_t>());
315 return true;
316 }
317
318protected:
319 virtual ~DrmSessionClient() {}
320
321private:
322 wp<DrmHal> mDrm;
323
324 DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient);
325};
326
327DrmHal::DrmHal()
328 : mDrmSessionClient(new DrmSessionClient(this)),
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800329 mFactories(makeDrmFactories()),
330 mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800331}
332
Jeff Tinker61332812017-05-15 16:53:10 -0700333void DrmHal::closeOpenSessions() {
Jeff Tinker7dfe28f2018-02-15 12:17:40 -0800334 Mutex::Autolock autoLock(mLock);
335 auto openSessions = mOpenSessions;
336 for (size_t i = 0; i < openSessions.size(); i++) {
337 mLock.unlock();
338 closeSession(openSessions[i]);
339 mLock.lock();
Jeff Tinker61332812017-05-15 16:53:10 -0700340 }
341 mOpenSessions.clear();
342}
343
Jeff Tinkera53d6552017-01-20 00:31:46 -0800344DrmHal::~DrmHal() {
345 DrmSessionManager::Instance()->removeDrm(mDrmSessionClient);
346}
347
Jeff Tinker7dfe28f2018-02-15 12:17:40 -0800348void DrmHal::cleanup() {
349 closeOpenSessions();
350
351 Mutex::Autolock autoLock(mLock);
352 reportPluginMetrics();
353 reportFrameworkMetrics();
354
355 setListener(NULL);
356 mInitCheck = NO_INIT;
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800357 if (mPluginV1_2 != NULL) {
358 if (!mPluginV1_2->setListener(NULL).isOk()) {
359 mInitCheck = DEAD_OBJECT;
360 }
361 } else if (mPlugin != NULL) {
Jeff Tinker7dfe28f2018-02-15 12:17:40 -0800362 if (!mPlugin->setListener(NULL).isOk()) {
363 mInitCheck = DEAD_OBJECT;
364 }
365 }
366 mPlugin.clear();
367 mPluginV1_1.clear();
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700368 mPluginV1_2.clear();
Jeff Tinker7dfe28f2018-02-15 12:17:40 -0800369}
370
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800371Vector<sp<IDrmFactory>> DrmHal::makeDrmFactories() {
372 Vector<sp<IDrmFactory>> factories;
373
Peter Kalauskasca5642c2018-11-12 12:34:42 -0800374 auto manager = hardware::defaultServiceManager1_2();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800375
376 if (manager != NULL) {
Peter Kalauskasca5642c2018-11-12 12:34:42 -0800377 manager->listManifestByInterface(drm::V1_0::IDrmFactory::descriptor,
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800378 [&factories](const hidl_vec<hidl_string> &registered) {
379 for (const auto &instance : registered) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000380 auto factory = drm::V1_0::IDrmFactory::getService(instance);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800381 if (factory != NULL) {
382 factories.push_back(factory);
Jeff Tinkere307dc42018-02-11 19:53:54 +0000383 }
384 }
385 }
386 );
Peter Kalauskasca5642c2018-11-12 12:34:42 -0800387 manager->listManifestByInterface(drm::V1_1::IDrmFactory::descriptor,
Jeff Tinkere307dc42018-02-11 19:53:54 +0000388 [&factories](const hidl_vec<hidl_string> &registered) {
389 for (const auto &instance : registered) {
390 auto factory = drm::V1_1::IDrmFactory::getService(instance);
391 if (factory != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000392 factories.push_back(factory);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800393 }
394 }
395 }
396 );
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700397 manager->listByInterface(drm::V1_2::IDrmFactory::descriptor,
398 [&factories](const hidl_vec<hidl_string> &registered) {
399 for (const auto &instance : registered) {
400 auto factory = drm::V1_2::IDrmFactory::getService(instance);
401 if (factory != NULL) {
402 factories.push_back(factory);
403 }
404 }
405 }
406 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800407 }
Jeff Tinkerc82b9c32017-02-14 11:39:51 -0800408
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800409 if (factories.size() == 0) {
410 // must be in passthrough mode, load the default passthrough service
Jeff Tinkere309b222017-04-05 08:01:28 -0700411 auto passthrough = IDrmFactory::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800412 if (passthrough != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000413 ALOGI("makeDrmFactories: using default passthrough drm instance");
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800414 factories.push_back(passthrough);
415 } else {
416 ALOGE("Failed to find any drm factories");
417 }
418 }
419 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800420}
421
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800422sp<IDrmPlugin> DrmHal::makeDrmPlugin(const sp<IDrmFactory>& factory,
423 const uint8_t uuid[16], const String8& appPackageName) {
Adam Stone32494f52018-02-26 22:53:27 -0800424 mAppPackageName = appPackageName;
Adam Stonefb679e32018-02-07 10:25:48 -0800425 mMetrics.SetAppPackageName(appPackageName);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800426
427 sp<IDrmPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800428 Return<void> hResult = factory->createPlugin(uuid, appPackageName.string(),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800429 [&](Status status, const sp<IDrmPlugin>& hPlugin) {
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800430 if (status != Status::OK) {
431 ALOGE("Failed to make drm plugin");
432 return;
433 }
434 plugin = hPlugin;
435 }
436 );
Jeff Tinkerf0e89b02017-08-07 15:58:41 -0700437
438 if (!hResult.isOk()) {
439 ALOGE("createPlugin remote call failed");
440 }
441
Jeff Tinkera53d6552017-01-20 00:31:46 -0800442 return plugin;
443}
444
445status_t DrmHal::initCheck() const {
446 return mInitCheck;
447}
448
449status_t DrmHal::setListener(const sp<IDrmClient>& listener)
450{
451 Mutex::Autolock lock(mEventLock);
452 if (mListener != NULL){
453 IInterface::asBinder(mListener)->unlinkToDeath(this);
454 }
455 if (listener != NULL) {
456 IInterface::asBinder(listener)->linkToDeath(this);
457 }
458 mListener = listener;
459 return NO_ERROR;
460}
461
462Return<void> DrmHal::sendEvent(EventType hEventType,
463 const hidl_vec<uint8_t>& sessionId, const hidl_vec<uint8_t>& data) {
Adam Stonecea91ce2018-01-22 19:23:28 -0800464 mMetrics.mEventCounter.Increment(hEventType);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800465
466 mEventLock.lock();
467 sp<IDrmClient> listener = mListener;
468 mEventLock.unlock();
469
470 if (listener != NULL) {
471 Parcel obj;
472 writeByteArray(obj, sessionId);
473 writeByteArray(obj, data);
474
475 Mutex::Autolock lock(mNotifyLock);
476 DrmPlugin::EventType eventType;
477 switch(hEventType) {
478 case EventType::PROVISION_REQUIRED:
479 eventType = DrmPlugin::kDrmPluginEventProvisionRequired;
480 break;
481 case EventType::KEY_NEEDED:
482 eventType = DrmPlugin::kDrmPluginEventKeyNeeded;
483 break;
484 case EventType::KEY_EXPIRED:
485 eventType = DrmPlugin::kDrmPluginEventKeyExpired;
486 break;
487 case EventType::VENDOR_DEFINED:
488 eventType = DrmPlugin::kDrmPluginEventVendorDefined;
489 break;
Rahul Friasb86f4b32017-03-27 15:13:30 -0700490 case EventType::SESSION_RECLAIMED:
491 eventType = DrmPlugin::kDrmPluginEventSessionReclaimed;
492 break;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800493 default:
494 return Void();
495 }
496 listener->notify(eventType, 0, &obj);
497 }
498 return Void();
499}
500
501Return<void> DrmHal::sendExpirationUpdate(const hidl_vec<uint8_t>& sessionId,
502 int64_t expiryTimeInMS) {
503
504 mEventLock.lock();
505 sp<IDrmClient> listener = mListener;
506 mEventLock.unlock();
507
508 if (listener != NULL) {
509 Parcel obj;
510 writeByteArray(obj, sessionId);
511 obj.writeInt64(expiryTimeInMS);
512
513 Mutex::Autolock lock(mNotifyLock);
514 listener->notify(DrmPlugin::kDrmPluginEventExpirationUpdate, 0, &obj);
515 }
516 return Void();
517}
518
519Return<void> DrmHal::sendKeysChange(const hidl_vec<uint8_t>& sessionId,
520 const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey) {
521
522 mEventLock.lock();
523 sp<IDrmClient> listener = mListener;
524 mEventLock.unlock();
525
526 if (listener != NULL) {
527 Parcel obj;
528 writeByteArray(obj, sessionId);
529
530 size_t nKeys = keyStatusList.size();
531 obj.writeInt32(nKeys);
532 for (size_t i = 0; i < nKeys; ++i) {
533 const KeyStatus &keyStatus = keyStatusList[i];
534 writeByteArray(obj, keyStatus.keyId);
535 uint32_t type;
536 switch(keyStatus.type) {
537 case KeyStatusType::USABLE:
538 type = DrmPlugin::kKeyStatusType_Usable;
539 break;
540 case KeyStatusType::EXPIRED:
541 type = DrmPlugin::kKeyStatusType_Expired;
542 break;
543 case KeyStatusType::OUTPUTNOTALLOWED:
544 type = DrmPlugin::kKeyStatusType_OutputNotAllowed;
545 break;
546 case KeyStatusType::STATUSPENDING:
547 type = DrmPlugin::kKeyStatusType_StatusPending;
548 break;
549 case KeyStatusType::INTERNALERROR:
550 default:
551 type = DrmPlugin::kKeyStatusType_InternalError;
552 break;
553 }
554 obj.writeInt32(type);
Adam Stonecea91ce2018-01-22 19:23:28 -0800555 mMetrics.mKeyStatusChangeCounter.Increment(keyStatus.type);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800556 }
557 obj.writeInt32(hasNewUsableKey);
558
559 Mutex::Autolock lock(mNotifyLock);
560 listener->notify(DrmPlugin::kDrmPluginEventKeysChange, 0, &obj);
Adam Stonecea91ce2018-01-22 19:23:28 -0800561 } else {
562 // There's no listener. But we still want to count the key change
563 // events.
564 size_t nKeys = keyStatusList.size();
565 for (size_t i = 0; i < nKeys; i++) {
566 mMetrics.mKeyStatusChangeCounter.Increment(keyStatusList[i].type);
567 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800568 }
Adam Stonecea91ce2018-01-22 19:23:28 -0800569
Jeff Tinkera53d6552017-01-20 00:31:46 -0800570 return Void();
571}
572
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800573Return<void> DrmHal::sendSessionLostState(
574 const hidl_vec<uint8_t>& sessionId) {
575
576 mEventLock.lock();
577 sp<IDrmClient> listener = mListener;
578 mEventLock.unlock();
579
580 if (listener != NULL) {
581 Parcel obj;
582 writeByteArray(obj, sessionId);
583 Mutex::Autolock lock(mNotifyLock);
584 listener->notify(DrmPlugin::kDrmPluginEventSessionLostState, 0, &obj);
585 }
586 return Void();
587}
588
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800589status_t DrmHal::matchMimeTypeAndSecurityLevel(const sp<IDrmFactory> &factory,
590 const uint8_t uuid[16],
591 const String8 &mimeType,
592 DrmPlugin::SecurityLevel level,
593 bool *isSupported) {
594 *isSupported = false;
595
596 // handle default value cases
597 if (level == DrmPlugin::kSecurityLevelUnknown) {
598 if (mimeType == "") {
599 // isCryptoSchemeSupported(uuid)
600 *isSupported = true;
601 } else {
602 // isCryptoSchemeSupported(uuid, mimeType)
603 *isSupported = factory->isContentTypeSupported(mimeType.string());
604 }
605 return OK;
606 } else if (mimeType == "") {
607 return BAD_VALUE;
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800608 }
609
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800610 sp<drm::V1_2::IDrmFactory> factoryV1_2 = drm::V1_2::IDrmFactory::castFrom(factory);
611 if (factoryV1_2 == NULL) {
612 return ERROR_UNSUPPORTED;
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800613 } else {
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800614 *isSupported = factoryV1_2->isCryptoSchemeSupported_1_2(uuid,
615 mimeType.string(), toHidlSecurityLevel(level));
616 return OK;
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800617 }
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800618}
619
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800620status_t DrmHal::isCryptoSchemeSupported(const uint8_t uuid[16],
621 const String8 &mimeType,
622 DrmPlugin::SecurityLevel level,
623 bool *isSupported) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800624 Mutex::Autolock autoLock(mLock);
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800625 *isSupported = false;
626 for (ssize_t i = mFactories.size() - 1; i >= 0; i--) {
627 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
628 return matchMimeTypeAndSecurityLevel(mFactories[i],
629 uuid, mimeType, level, isSupported);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800630 }
631 }
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800632 return OK;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800633}
634
Edwin Wong68b3d9f2017-01-06 19:07:54 -0800635status_t DrmHal::createPlugin(const uint8_t uuid[16],
636 const String8& appPackageName) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800637 Mutex::Autolock autoLock(mLock);
638
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -0800639 for (ssize_t i = mFactories.size() - 1; i >= 0; i--) {
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800640 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
Peter Kalauskas7676a402018-12-27 11:04:16 -0800641 auto plugin = makeDrmPlugin(mFactories[i], uuid, appPackageName);
642 if (plugin != NULL) {
643 mPlugin = plugin;
Edwin Wong5641aa22018-01-30 17:52:21 -0800644 mPluginV1_1 = drm::V1_1::IDrmPlugin::castFrom(mPlugin);
Jeff Tinkerc8baaba2018-10-23 11:32:36 -0700645 mPluginV1_2 = drm::V1_2::IDrmPlugin::castFrom(mPlugin);
Peter Kalauskas7676a402018-12-27 11:04:16 -0800646 break;
Edwin Wong5641aa22018-01-30 17:52:21 -0800647 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800648 }
649 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800650
651 if (mPlugin == NULL) {
652 mInitCheck = ERROR_UNSUPPORTED;
653 } else {
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800654 mInitCheck = OK;
655 if (mPluginV1_2 != NULL) {
656 if (!mPluginV1_2->setListener(this).isOk()) {
657 mInitCheck = DEAD_OBJECT;
658 }
659 } else if (!mPlugin->setListener(this).isOk()) {
Jeff Tinker319d5f42017-07-26 15:44:33 -0700660 mInitCheck = DEAD_OBJECT;
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800661 }
662 if (mInitCheck != OK) {
663 mPlugin.clear();
664 mPluginV1_1.clear();
665 mPluginV1_2.clear();
Jeff Tinker319d5f42017-07-26 15:44:33 -0700666 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800667 }
668
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800669
Jeff Tinkera53d6552017-01-20 00:31:46 -0800670 return mInitCheck;
671}
672
673status_t DrmHal::destroyPlugin() {
Jeff Tinker7dfe28f2018-02-15 12:17:40 -0800674 cleanup();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800675 return OK;
676}
677
Jeff Tinker41d279a2018-02-11 19:52:08 +0000678status_t DrmHal::openSession(DrmPlugin::SecurityLevel level,
679 Vector<uint8_t> &sessionId) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800680 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800681 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800682
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800683 SecurityLevel hSecurityLevel = toHidlSecurityLevel(level);
Jeff Tinker41d279a2018-02-11 19:52:08 +0000684 bool setSecurityLevel = true;
Tobias Thierer5f5e43f2018-02-11 15:00:57 +0000685
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800686 if (level == DrmPlugin::kSecurityLevelMax) {
Jeff Tinker41d279a2018-02-11 19:52:08 +0000687 setSecurityLevel = false;
Jeff Tinker99dbfa82019-01-17 17:27:06 -0800688 } else {
689 if (hSecurityLevel == SecurityLevel::UNKNOWN) {
690 return ERROR_DRM_CANNOT_HANDLE;
691 }
Jeff Tinker41d279a2018-02-11 19:52:08 +0000692 }
693
694 status_t err = UNKNOWN_ERROR;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800695 bool retry = true;
696 do {
697 hidl_vec<uint8_t> hSessionId;
698
Jeff Tinker41d279a2018-02-11 19:52:08 +0000699 Return<void> hResult;
700 if (mPluginV1_1 == NULL || !setSecurityLevel) {
701 hResult = mPlugin->openSession(
702 [&](Status status,const hidl_vec<uint8_t>& id) {
703 if (status == Status::OK) {
704 sessionId = toVector(id);
705 }
706 err = toStatusT(status);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800707 }
Jeff Tinker41d279a2018-02-11 19:52:08 +0000708 );
709 } else {
710 hResult = mPluginV1_1->openSession_1_1(hSecurityLevel,
711 [&](Status status, const hidl_vec<uint8_t>& id) {
712 if (status == Status::OK) {
713 sessionId = toVector(id);
714 }
715 err = toStatusT(status);
716 }
717 );
718 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800719
720 if (!hResult.isOk()) {
721 err = DEAD_OBJECT;
722 }
723
724 if (err == ERROR_DRM_RESOURCE_BUSY && retry) {
725 mLock.unlock();
726 // reclaimSession may call back to closeSession, since mLock is
727 // shared between Drm instances, we should unlock here to avoid
728 // deadlock.
729 retry = DrmSessionManager::Instance()->reclaimSession(getCallingPid());
730 mLock.lock();
731 } else {
732 retry = false;
733 }
734 } while (retry);
735
736 if (err == OK) {
737 DrmSessionManager::Instance()->addSession(getCallingPid(),
738 mDrmSessionClient, sessionId);
Jeff Tinker61332812017-05-15 16:53:10 -0700739 mOpenSessions.push(sessionId);
Adam Stone568b3c42018-01-31 12:57:16 -0800740 mMetrics.SetSessionStart(sessionId);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800741 }
Adam Stoneaaf87dd2018-01-08 11:55:06 -0800742
Adam Stonef0e618d2018-01-17 19:20:41 -0800743 mMetrics.mOpenSessionCounter.Increment(err);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800744 return err;
745}
746
747status_t DrmHal::closeSession(Vector<uint8_t> const &sessionId) {
748 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800749 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800750
Jeff Tinker319d5f42017-07-26 15:44:33 -0700751 Return<Status> status = mPlugin->closeSession(toHidlVec(sessionId));
752 if (status.isOk()) {
753 if (status == Status::OK) {
754 DrmSessionManager::Instance()->removeSession(sessionId);
755 for (size_t i = 0; i < mOpenSessions.size(); i++) {
756 if (mOpenSessions[i] == sessionId) {
757 mOpenSessions.removeAt(i);
758 break;
759 }
Jeff Tinker61332812017-05-15 16:53:10 -0700760 }
761 }
Adam Stonecea91ce2018-01-22 19:23:28 -0800762 status_t response = toStatusT(status);
Adam Stone568b3c42018-01-31 12:57:16 -0800763 mMetrics.SetSessionEnd(sessionId);
Adam Stonecea91ce2018-01-22 19:23:28 -0800764 mMetrics.mCloseSessionCounter.Increment(response);
765 return response;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800766 }
Adam Stonecea91ce2018-01-22 19:23:28 -0800767 mMetrics.mCloseSessionCounter.Increment(DEAD_OBJECT);
Jeff Tinker319d5f42017-07-26 15:44:33 -0700768 return DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800769}
770
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800771static DrmPlugin::KeyRequestType toKeyRequestType(
772 KeyRequestType keyRequestType) {
773 switch (keyRequestType) {
774 case KeyRequestType::INITIAL:
775 return DrmPlugin::kKeyRequestType_Initial;
776 break;
777 case KeyRequestType::RENEWAL:
778 return DrmPlugin::kKeyRequestType_Renewal;
779 break;
780 case KeyRequestType::RELEASE:
781 return DrmPlugin::kKeyRequestType_Release;
782 break;
783 default:
784 return DrmPlugin::kKeyRequestType_Unknown;
785 break;
786 }
787}
788
789static DrmPlugin::KeyRequestType toKeyRequestType_1_1(
790 KeyRequestType_V1_1 keyRequestType) {
791 switch (keyRequestType) {
792 case KeyRequestType_V1_1::NONE:
793 return DrmPlugin::kKeyRequestType_None;
794 break;
795 case KeyRequestType_V1_1::UPDATE:
796 return DrmPlugin::kKeyRequestType_Update;
797 break;
798 default:
799 return toKeyRequestType(static_cast<KeyRequestType>(keyRequestType));
800 break;
801 }
802}
803
Jeff Tinkera53d6552017-01-20 00:31:46 -0800804status_t DrmHal::getKeyRequest(Vector<uint8_t> const &sessionId,
805 Vector<uint8_t> const &initData, String8 const &mimeType,
806 DrmPlugin::KeyType keyType, KeyedVector<String8,
807 String8> const &optionalParameters, Vector<uint8_t> &request,
808 String8 &defaultUrl, DrmPlugin::KeyRequestType *keyRequestType) {
809 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800810 INIT_CHECK();
Adam Stonefb679e32018-02-07 10:25:48 -0800811 EventTimer<status_t> keyRequestTimer(&mMetrics.mGetKeyRequestTimeUs);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800812
813 DrmSessionManager::Instance()->useSession(sessionId);
814
815 KeyType hKeyType;
816 if (keyType == DrmPlugin::kKeyType_Streaming) {
817 hKeyType = KeyType::STREAMING;
818 } else if (keyType == DrmPlugin::kKeyType_Offline) {
819 hKeyType = KeyType::OFFLINE;
820 } else if (keyType == DrmPlugin::kKeyType_Release) {
821 hKeyType = KeyType::RELEASE;
822 } else {
Adam Stonef0e618d2018-01-17 19:20:41 -0800823 keyRequestTimer.SetAttribute(BAD_VALUE);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800824 return BAD_VALUE;
825 }
826
827 ::KeyedVector hOptionalParameters = toHidlKeyedVector(optionalParameters);
828
829 status_t err = UNKNOWN_ERROR;
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800830 Return<void> hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800831
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800832 if (mPluginV1_2 != NULL) {
833 hResult = mPluginV1_2->getKeyRequest_1_2(
834 toHidlVec(sessionId), toHidlVec(initData),
835 toHidlString(mimeType), hKeyType, hOptionalParameters,
836 [&](Status_V1_2 status, const hidl_vec<uint8_t>& hRequest,
837 KeyRequestType_V1_1 hKeyRequestType,
838 const hidl_string& hDefaultUrl) {
839 if (status == Status_V1_2::OK) {
840 request = toVector(hRequest);
841 defaultUrl = toString8(hDefaultUrl);
842 *keyRequestType = toKeyRequestType_1_1(hKeyRequestType);
843 }
844 err = toStatusT_1_2(status);
845 });
846 } else if (mPluginV1_1 != NULL) {
847 hResult = mPluginV1_1->getKeyRequest_1_1(
Rahul Frias59bc3fa2018-01-22 23:48:52 -0800848 toHidlVec(sessionId), toHidlVec(initData),
849 toHidlString(mimeType), hKeyType, hOptionalParameters,
850 [&](Status status, const hidl_vec<uint8_t>& hRequest,
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800851 KeyRequestType_V1_1 hKeyRequestType,
852 const hidl_string& hDefaultUrl) {
853 if (status == Status::OK) {
854 request = toVector(hRequest);
855 defaultUrl = toString8(hDefaultUrl);
856 *keyRequestType = toKeyRequestType_1_1(hKeyRequestType);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800857 }
858 err = toStatusT(status);
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800859 });
860 } else {
861 hResult = mPlugin->getKeyRequest(
862 toHidlVec(sessionId), toHidlVec(initData),
863 toHidlString(mimeType), hKeyType, hOptionalParameters,
864 [&](Status status, const hidl_vec<uint8_t>& hRequest,
865 KeyRequestType hKeyRequestType,
866 const hidl_string& hDefaultUrl) {
867 if (status == Status::OK) {
868 request = toVector(hRequest);
869 defaultUrl = toString8(hDefaultUrl);
870 *keyRequestType = toKeyRequestType(hKeyRequestType);
871 }
872 err = toStatusT(status);
873 });
874 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800875
Adam Stonef0e618d2018-01-17 19:20:41 -0800876 err = hResult.isOk() ? err : DEAD_OBJECT;
877 keyRequestTimer.SetAttribute(err);
878 return err;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800879}
880
881status_t DrmHal::provideKeyResponse(Vector<uint8_t> const &sessionId,
882 Vector<uint8_t> const &response, Vector<uint8_t> &keySetId) {
883 Mutex::Autolock autoLock(mLock);
Adam Stonefb679e32018-02-07 10:25:48 -0800884 EventTimer<status_t> keyResponseTimer(&mMetrics.mProvideKeyResponseTimeUs);
Adam Stonecea91ce2018-01-22 19:23:28 -0800885
Jeff Tinker6d998b62017-12-18 14:37:43 -0800886 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800887
888 DrmSessionManager::Instance()->useSession(sessionId);
889
890 status_t err = UNKNOWN_ERROR;
891
892 Return<void> hResult = mPlugin->provideKeyResponse(toHidlVec(sessionId),
893 toHidlVec(response),
894 [&](Status status, const hidl_vec<uint8_t>& hKeySetId) {
895 if (status == Status::OK) {
896 keySetId = toVector(hKeySetId);
897 }
898 err = toStatusT(status);
899 }
900 );
Adam Stonecea91ce2018-01-22 19:23:28 -0800901 err = hResult.isOk() ? err : DEAD_OBJECT;
902 keyResponseTimer.SetAttribute(err);
903 return err;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800904}
905
906status_t DrmHal::removeKeys(Vector<uint8_t> const &keySetId) {
907 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800908 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800909
Jeff Tinker58ad4752018-02-16 16:51:59 -0800910 Return<Status> status = mPlugin->removeKeys(toHidlVec(keySetId));
911 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800912}
913
914status_t DrmHal::restoreKeys(Vector<uint8_t> const &sessionId,
915 Vector<uint8_t> const &keySetId) {
916 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800917 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800918
919 DrmSessionManager::Instance()->useSession(sessionId);
920
Jeff Tinker58ad4752018-02-16 16:51:59 -0800921 Return<Status> status = mPlugin->restoreKeys(toHidlVec(sessionId),
922 toHidlVec(keySetId));
923 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800924}
925
926status_t DrmHal::queryKeyStatus(Vector<uint8_t> const &sessionId,
927 KeyedVector<String8, String8> &infoMap) const {
928 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800929 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800930
931 DrmSessionManager::Instance()->useSession(sessionId);
932
933 ::KeyedVector hInfoMap;
934
935 status_t err = UNKNOWN_ERROR;
936
937 Return<void> hResult = mPlugin->queryKeyStatus(toHidlVec(sessionId),
938 [&](Status status, const hidl_vec<KeyValue>& map) {
939 if (status == Status::OK) {
940 infoMap = toKeyedVector(map);
941 }
942 err = toStatusT(status);
943 }
944 );
945
946 return hResult.isOk() ? err : DEAD_OBJECT;
947}
948
949status_t DrmHal::getProvisionRequest(String8 const &certType,
950 String8 const &certAuthority, Vector<uint8_t> &request,
951 String8 &defaultUrl) {
952 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800953 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800954
955 status_t err = UNKNOWN_ERROR;
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800956 Return<void> hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800957
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800958 if (mPluginV1_2 != NULL) {
959 Return<void> hResult = mPluginV1_2->getProvisionRequest_1_2(
960 toHidlString(certType), toHidlString(certAuthority),
961 [&](Status_V1_2 status, const hidl_vec<uint8_t>& hRequest,
962 const hidl_string& hDefaultUrl) {
963 if (status == Status_V1_2::OK) {
964 request = toVector(hRequest);
965 defaultUrl = toString8(hDefaultUrl);
966 }
967 err = toStatusT_1_2(status);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800968 }
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800969 );
970 } else {
971 Return<void> hResult = mPlugin->getProvisionRequest(
972 toHidlString(certType), toHidlString(certAuthority),
973 [&](Status status, const hidl_vec<uint8_t>& hRequest,
974 const hidl_string& hDefaultUrl) {
975 if (status == Status::OK) {
976 request = toVector(hRequest);
977 defaultUrl = toString8(hDefaultUrl);
978 }
979 err = toStatusT(status);
980 }
981 );
982 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800983
Adam Stonecea91ce2018-01-22 19:23:28 -0800984 err = hResult.isOk() ? err : DEAD_OBJECT;
985 mMetrics.mGetProvisionRequestCounter.Increment(err);
986 return err;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800987}
988
989status_t DrmHal::provideProvisionResponse(Vector<uint8_t> const &response,
Edwin Wong68b3d9f2017-01-06 19:07:54 -0800990 Vector<uint8_t> &certificate, Vector<uint8_t> &wrappedKey) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800991 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -0800992 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800993
994 status_t err = UNKNOWN_ERROR;
995
996 Return<void> hResult = mPlugin->provideProvisionResponse(toHidlVec(response),
997 [&](Status status, const hidl_vec<uint8_t>& hCertificate,
998 const hidl_vec<uint8_t>& hWrappedKey) {
999 if (status == Status::OK) {
1000 certificate = toVector(hCertificate);
1001 wrappedKey = toVector(hWrappedKey);
1002 }
1003 err = toStatusT(status);
1004 }
1005 );
1006
Adam Stonecea91ce2018-01-22 19:23:28 -08001007 err = hResult.isOk() ? err : DEAD_OBJECT;
1008 mMetrics.mProvideProvisionResponseCounter.Increment(err);
1009 return err;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001010}
1011
Jeff Tinkerabeb36a2017-02-17 09:42:46 -08001012status_t DrmHal::getSecureStops(List<Vector<uint8_t>> &secureStops) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001013 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001014 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001015
1016 status_t err = UNKNOWN_ERROR;
1017
1018 Return<void> hResult = mPlugin->getSecureStops(
1019 [&](Status status, const hidl_vec<SecureStop>& hSecureStops) {
1020 if (status == Status::OK) {
1021 secureStops = toSecureStops(hSecureStops);
1022 }
1023 err = toStatusT(status);
1024 }
1025 );
1026
1027 return hResult.isOk() ? err : DEAD_OBJECT;
1028}
1029
1030
Jeff Tinker15177d72018-01-25 12:57:55 -08001031status_t DrmHal::getSecureStopIds(List<Vector<uint8_t>> &secureStopIds) {
1032 Mutex::Autolock autoLock(mLock);
1033
1034 if (mInitCheck != OK) {
1035 return mInitCheck;
1036 }
1037
1038 if (mPluginV1_1 == NULL) {
1039 return ERROR_DRM_CANNOT_HANDLE;
1040 }
1041
1042 status_t err = UNKNOWN_ERROR;
1043
1044 Return<void> hResult = mPluginV1_1->getSecureStopIds(
1045 [&](Status status, const hidl_vec<SecureStopId>& hSecureStopIds) {
1046 if (status == Status::OK) {
1047 secureStopIds = toSecureStopIds(hSecureStopIds);
1048 }
1049 err = toStatusT(status);
1050 }
1051 );
1052
1053 return hResult.isOk() ? err : DEAD_OBJECT;
1054}
1055
1056
Jeff Tinkera53d6552017-01-20 00:31:46 -08001057status_t DrmHal::getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) {
1058 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001059 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001060
1061 status_t err = UNKNOWN_ERROR;
1062
1063 Return<void> hResult = mPlugin->getSecureStop(toHidlVec(ssid),
1064 [&](Status status, const SecureStop& hSecureStop) {
1065 if (status == Status::OK) {
1066 secureStop = toVector(hSecureStop.opaqueData);
1067 }
1068 err = toStatusT(status);
1069 }
1070 );
1071
1072 return hResult.isOk() ? err : DEAD_OBJECT;
1073}
1074
1075status_t DrmHal::releaseSecureStops(Vector<uint8_t> const &ssRelease) {
1076 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001077 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001078
Jeff Tinker58ad4752018-02-16 16:51:59 -08001079 Return<Status> status(Status::ERROR_DRM_UNKNOWN);
Jeff Tinker15177d72018-01-25 12:57:55 -08001080 if (mPluginV1_1 != NULL) {
1081 SecureStopRelease secureStopRelease;
1082 secureStopRelease.opaqueData = toHidlVec(ssRelease);
Jeff Tinker58ad4752018-02-16 16:51:59 -08001083 status = mPluginV1_1->releaseSecureStops(secureStopRelease);
1084 } else {
1085 status = mPlugin->releaseSecureStop(toHidlVec(ssRelease));
Jeff Tinker15177d72018-01-25 12:57:55 -08001086 }
Jeff Tinker58ad4752018-02-16 16:51:59 -08001087 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001088}
1089
Jeff Tinker15177d72018-01-25 12:57:55 -08001090status_t DrmHal::removeSecureStop(Vector<uint8_t> const &ssid) {
1091 Mutex::Autolock autoLock(mLock);
1092
1093 if (mInitCheck != OK) {
1094 return mInitCheck;
1095 }
1096
1097 if (mPluginV1_1 == NULL) {
1098 return ERROR_DRM_CANNOT_HANDLE;
1099 }
1100
Jeff Tinker58ad4752018-02-16 16:51:59 -08001101 Return<Status> status = mPluginV1_1->removeSecureStop(toHidlVec(ssid));
1102 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinker15177d72018-01-25 12:57:55 -08001103}
1104
1105status_t DrmHal::removeAllSecureStops() {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001106 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001107 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001108
Jeff Tinker58ad4752018-02-16 16:51:59 -08001109 Return<Status> status(Status::ERROR_DRM_UNKNOWN);
Jeff Tinker15177d72018-01-25 12:57:55 -08001110 if (mPluginV1_1 != NULL) {
Jeff Tinker58ad4752018-02-16 16:51:59 -08001111 status = mPluginV1_1->removeAllSecureStops();
1112 } else {
1113 status = mPlugin->releaseAllSecureStops();
Jeff Tinker15177d72018-01-25 12:57:55 -08001114 }
Jeff Tinker58ad4752018-02-16 16:51:59 -08001115 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001116}
1117
Jeff Tinker6d998b62017-12-18 14:37:43 -08001118status_t DrmHal::getHdcpLevels(DrmPlugin::HdcpLevel *connected,
1119 DrmPlugin::HdcpLevel *max) const {
1120 Mutex::Autolock autoLock(mLock);
1121 INIT_CHECK();
1122
1123 if (connected == NULL || max == NULL) {
1124 return BAD_VALUE;
1125 }
1126 status_t err = UNKNOWN_ERROR;
1127
Jeff Tinker6d998b62017-12-18 14:37:43 -08001128 *connected = DrmPlugin::kHdcpLevelUnknown;
1129 *max = DrmPlugin::kHdcpLevelUnknown;
1130
Jeff Tinker44c2cc42019-01-14 10:24:18 -08001131 Return<void> hResult;
1132 if (mPluginV1_2 != NULL) {
1133 hResult = mPluginV1_2->getHdcpLevels_1_2(
1134 [&](Status_V1_2 status, const HdcpLevel_V1_2& hConnected, const HdcpLevel_V1_2& hMax) {
1135 if (status == Status_V1_2::OK) {
1136 *connected = toHdcpLevel(hConnected);
1137 *max = toHdcpLevel(hMax);
1138 }
1139 err = toStatusT_1_2(status);
1140 });
1141 } else if (mPluginV1_1 != NULL) {
1142 hResult = mPluginV1_1->getHdcpLevels(
1143 [&](Status status, const HdcpLevel& hConnected, const HdcpLevel& hMax) {
1144 if (status == Status::OK) {
1145 *connected = toHdcpLevel(static_cast<HdcpLevel_V1_2>(hConnected));
1146 *max = toHdcpLevel(static_cast<HdcpLevel_V1_2>(hMax));
1147 }
1148 err = toStatusT(status);
1149 });
1150 } else {
1151 return ERROR_DRM_CANNOT_HANDLE;
1152 }
Jeff Tinker6d998b62017-12-18 14:37:43 -08001153
1154 return hResult.isOk() ? err : DEAD_OBJECT;
1155}
1156
1157status_t DrmHal::getNumberOfSessions(uint32_t *open, uint32_t *max) const {
1158 Mutex::Autolock autoLock(mLock);
1159 INIT_CHECK();
1160
1161 if (open == NULL || max == NULL) {
1162 return BAD_VALUE;
1163 }
1164 status_t err = UNKNOWN_ERROR;
1165
1166 *open = 0;
1167 *max = 0;
1168
1169 if (mPluginV1_1 == NULL) {
1170 return ERROR_DRM_CANNOT_HANDLE;
1171 }
1172
1173 Return<void> hResult = mPluginV1_1->getNumberOfSessions(
1174 [&](Status status, uint32_t hOpen, uint32_t hMax) {
1175 if (status == Status::OK) {
1176 *open = hOpen;
1177 *max = hMax;
1178 }
1179 err = toStatusT(status);
1180 }
1181 );
1182
1183 return hResult.isOk() ? err : DEAD_OBJECT;
1184}
1185
1186status_t DrmHal::getSecurityLevel(Vector<uint8_t> const &sessionId,
1187 DrmPlugin::SecurityLevel *level) const {
1188 Mutex::Autolock autoLock(mLock);
1189 INIT_CHECK();
1190
1191 if (level == NULL) {
1192 return BAD_VALUE;
1193 }
1194 status_t err = UNKNOWN_ERROR;
1195
1196 if (mPluginV1_1 == NULL) {
1197 return ERROR_DRM_CANNOT_HANDLE;
1198 }
1199
1200 *level = DrmPlugin::kSecurityLevelUnknown;
1201
1202 Return<void> hResult = mPluginV1_1->getSecurityLevel(toHidlVec(sessionId),
1203 [&](Status status, SecurityLevel hLevel) {
1204 if (status == Status::OK) {
1205 *level = toSecurityLevel(hLevel);
1206 }
1207 err = toStatusT(status);
1208 }
1209 );
1210
1211 return hResult.isOk() ? err : DEAD_OBJECT;
1212}
1213
Jeff Tinkerc8baaba2018-10-23 11:32:36 -07001214status_t DrmHal::getOfflineLicenseKeySetIds(List<Vector<uint8_t>> &keySetIds) const {
1215 Mutex::Autolock autoLock(mLock);
1216
1217 if (mInitCheck != OK) {
1218 return mInitCheck;
1219 }
1220
1221 if (mPluginV1_2 == NULL) {
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -08001222 return ERROR_UNSUPPORTED;
Jeff Tinkerc8baaba2018-10-23 11:32:36 -07001223 }
1224
1225 status_t err = UNKNOWN_ERROR;
1226
1227 Return<void> hResult = mPluginV1_2->getOfflineLicenseKeySetIds(
1228 [&](Status status, const hidl_vec<KeySetId>& hKeySetIds) {
1229 if (status == Status::OK) {
1230 keySetIds = toKeySetIds(hKeySetIds);
1231 }
1232 err = toStatusT(status);
1233 }
1234 );
1235
1236 return hResult.isOk() ? err : DEAD_OBJECT;
1237}
1238
1239status_t DrmHal::removeOfflineLicense(Vector<uint8_t> const &keySetId) {
1240 Mutex::Autolock autoLock(mLock);
1241
1242 if (mInitCheck != OK) {
1243 return mInitCheck;
1244 }
1245
1246 if (mPluginV1_2 == NULL) {
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -08001247 return ERROR_UNSUPPORTED;
Jeff Tinkerc8baaba2018-10-23 11:32:36 -07001248 }
1249
1250 Return<Status> status = mPluginV1_2->removeOfflineLicense(toHidlVec(keySetId));
1251 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
1252}
1253
1254status_t DrmHal::getOfflineLicenseState(Vector<uint8_t> const &keySetId,
1255 DrmPlugin::OfflineLicenseState *licenseState) const {
1256 Mutex::Autolock autoLock(mLock);
1257
1258 if (mInitCheck != OK) {
1259 return mInitCheck;
1260 }
1261
1262 if (mPluginV1_2 == NULL) {
Jeff Tinkerdb3fa5f2019-01-25 22:56:56 -08001263 return ERROR_UNSUPPORTED;
Jeff Tinkerc8baaba2018-10-23 11:32:36 -07001264 }
1265 *licenseState = DrmPlugin::kOfflineLicenseStateUnknown;
1266
1267 status_t err = UNKNOWN_ERROR;
1268
1269 Return<void> hResult = mPluginV1_2->getOfflineLicenseState(toHidlVec(keySetId),
1270 [&](Status status, OfflineLicenseState hLicenseState) {
1271 if (status == Status::OK) {
1272 *licenseState = toOfflineLicenseState(hLicenseState);
1273 }
1274 err = toStatusT(status);
1275 }
1276 );
1277
1278 return hResult.isOk() ? err : DEAD_OBJECT;
1279}
1280
Jeff Tinkera53d6552017-01-20 00:31:46 -08001281status_t DrmHal::getPropertyString(String8 const &name, String8 &value ) const {
1282 Mutex::Autolock autoLock(mLock);
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001283 return getPropertyStringInternal(name, value);
1284}
1285
1286status_t DrmHal::getPropertyStringInternal(String8 const &name, String8 &value) const {
1287 // This function is internal to the class and should only be called while
1288 // mLock is already held.
Jeff Tinker6d998b62017-12-18 14:37:43 -08001289 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001290
1291 status_t err = UNKNOWN_ERROR;
1292
1293 Return<void> hResult = mPlugin->getPropertyString(toHidlString(name),
1294 [&](Status status, const hidl_string& hValue) {
1295 if (status == Status::OK) {
1296 value = toString8(hValue);
1297 }
1298 err = toStatusT(status);
1299 }
1300 );
1301
1302 return hResult.isOk() ? err : DEAD_OBJECT;
1303}
1304
1305status_t DrmHal::getPropertyByteArray(String8 const &name, Vector<uint8_t> &value ) const {
1306 Mutex::Autolock autoLock(mLock);
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001307 return getPropertyByteArrayInternal(name, value);
1308}
1309
1310status_t DrmHal::getPropertyByteArrayInternal(String8 const &name, Vector<uint8_t> &value ) const {
1311 // This function is internal to the class and should only be called while
1312 // mLock is already held.
Jeff Tinker6d998b62017-12-18 14:37:43 -08001313 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001314
1315 status_t err = UNKNOWN_ERROR;
1316
1317 Return<void> hResult = mPlugin->getPropertyByteArray(toHidlString(name),
1318 [&](Status status, const hidl_vec<uint8_t>& hValue) {
1319 if (status == Status::OK) {
1320 value = toVector(hValue);
1321 }
1322 err = toStatusT(status);
1323 }
1324 );
1325
Adam Stonecea91ce2018-01-22 19:23:28 -08001326 err = hResult.isOk() ? err : DEAD_OBJECT;
1327 if (name == kPropertyDeviceUniqueId) {
1328 mMetrics.mGetDeviceUniqueIdCounter.Increment(err);
1329 }
1330 return err;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001331}
1332
1333status_t DrmHal::setPropertyString(String8 const &name, String8 const &value ) const {
1334 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001335 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001336
Jeff Tinker58ad4752018-02-16 16:51:59 -08001337 Return<Status> status = mPlugin->setPropertyString(toHidlString(name),
Jeff Tinkera53d6552017-01-20 00:31:46 -08001338 toHidlString(value));
Jeff Tinker58ad4752018-02-16 16:51:59 -08001339 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001340}
1341
1342status_t DrmHal::setPropertyByteArray(String8 const &name,
1343 Vector<uint8_t> const &value ) const {
1344 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001345 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001346
Jeff Tinker58ad4752018-02-16 16:51:59 -08001347 Return<Status> status = mPlugin->setPropertyByteArray(toHidlString(name),
Jeff Tinkera53d6552017-01-20 00:31:46 -08001348 toHidlVec(value));
Jeff Tinker58ad4752018-02-16 16:51:59 -08001349 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001350}
1351
Adam Stone28f27c32018-02-05 15:07:48 -08001352status_t DrmHal::getMetrics(PersistableBundle* metrics) {
1353 if (metrics == nullptr) {
1354 return UNEXPECTED_NULL;
1355 }
1356 mMetrics.Export(metrics);
1357
1358 // Append vendor metrics if they are supported.
1359 if (mPluginV1_1 != NULL) {
1360 String8 vendor;
1361 String8 description;
1362 if (getPropertyStringInternal(String8("vendor"), vendor) != OK
1363 || vendor.isEmpty()) {
1364 ALOGE("Get vendor failed or is empty");
1365 vendor = "NONE";
1366 }
1367 if (getPropertyStringInternal(String8("description"), description) != OK
1368 || description.isEmpty()) {
1369 ALOGE("Get description failed or is empty.");
1370 description = "NONE";
1371 }
1372 vendor += ".";
1373 vendor += description;
1374
1375 hidl_vec<DrmMetricGroup> pluginMetrics;
1376 status_t err = UNKNOWN_ERROR;
1377
1378 Return<void> status = mPluginV1_1->getMetrics(
1379 [&](Status status, hidl_vec<DrmMetricGroup> pluginMetrics) {
1380 if (status != Status::OK) {
1381 ALOGV("Error getting plugin metrics: %d", status);
1382 } else {
1383 PersistableBundle pluginBundle;
1384 if (MediaDrmMetrics::HidlMetricsToBundle(
1385 pluginMetrics, &pluginBundle) == OK) {
1386 metrics->putPersistableBundle(String16(vendor), pluginBundle);
1387 }
1388 }
1389 err = toStatusT(status);
1390 });
1391 return status.isOk() ? err : DEAD_OBJECT;
Adam Stonef0e618d2018-01-17 19:20:41 -08001392 }
1393
Adam Stoneab394d12017-12-22 12:34:20 -08001394 return OK;
1395}
Jeff Tinkera53d6552017-01-20 00:31:46 -08001396
1397status_t DrmHal::setCipherAlgorithm(Vector<uint8_t> const &sessionId,
1398 String8 const &algorithm) {
1399 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001400 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001401
1402 DrmSessionManager::Instance()->useSession(sessionId);
1403
Jeff Tinkere6412942018-04-30 17:35:16 -07001404 Return<Status> status = mPlugin->setCipherAlgorithm(toHidlVec(sessionId),
Jeff Tinkera53d6552017-01-20 00:31:46 -08001405 toHidlString(algorithm));
Jeff Tinkere6412942018-04-30 17:35:16 -07001406 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001407}
1408
1409status_t DrmHal::setMacAlgorithm(Vector<uint8_t> const &sessionId,
1410 String8 const &algorithm) {
1411 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001412 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001413
1414 DrmSessionManager::Instance()->useSession(sessionId);
1415
Jeff Tinkere6412942018-04-30 17:35:16 -07001416 Return<Status> status = mPlugin->setMacAlgorithm(toHidlVec(sessionId),
Jeff Tinkera53d6552017-01-20 00:31:46 -08001417 toHidlString(algorithm));
Jeff Tinkere6412942018-04-30 17:35:16 -07001418 return status.isOk() ? toStatusT(status) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -08001419}
1420
1421status_t DrmHal::encrypt(Vector<uint8_t> const &sessionId,
Edwin Wong68b3d9f2017-01-06 19:07:54 -08001422 Vector<uint8_t> const &keyId, Vector<uint8_t> const &input,
1423 Vector<uint8_t> const &iv, Vector<uint8_t> &output) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001424 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001425 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001426
1427 DrmSessionManager::Instance()->useSession(sessionId);
1428
1429 status_t err = UNKNOWN_ERROR;
1430
1431 Return<void> hResult = mPlugin->encrypt(toHidlVec(sessionId),
1432 toHidlVec(keyId), toHidlVec(input), toHidlVec(iv),
1433 [&](Status status, const hidl_vec<uint8_t>& hOutput) {
1434 if (status == Status::OK) {
1435 output = toVector(hOutput);
1436 }
1437 err = toStatusT(status);
1438 }
1439 );
1440
1441 return hResult.isOk() ? err : DEAD_OBJECT;
1442}
1443
1444status_t DrmHal::decrypt(Vector<uint8_t> const &sessionId,
Edwin Wong68b3d9f2017-01-06 19:07:54 -08001445 Vector<uint8_t> const &keyId, Vector<uint8_t> const &input,
1446 Vector<uint8_t> const &iv, Vector<uint8_t> &output) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001447 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001448 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001449
1450 DrmSessionManager::Instance()->useSession(sessionId);
1451
1452 status_t err = UNKNOWN_ERROR;
1453
1454 Return<void> hResult = mPlugin->decrypt(toHidlVec(sessionId),
1455 toHidlVec(keyId), toHidlVec(input), toHidlVec(iv),
1456 [&](Status status, const hidl_vec<uint8_t>& hOutput) {
1457 if (status == Status::OK) {
1458 output = toVector(hOutput);
1459 }
1460 err = toStatusT(status);
1461 }
1462 );
1463
1464 return hResult.isOk() ? err : DEAD_OBJECT;
1465}
1466
1467status_t DrmHal::sign(Vector<uint8_t> const &sessionId,
Edwin Wong68b3d9f2017-01-06 19:07:54 -08001468 Vector<uint8_t> const &keyId, Vector<uint8_t> const &message,
1469 Vector<uint8_t> &signature) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001470 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001471 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001472
1473 DrmSessionManager::Instance()->useSession(sessionId);
1474
1475 status_t err = UNKNOWN_ERROR;
1476
1477 Return<void> hResult = mPlugin->sign(toHidlVec(sessionId),
1478 toHidlVec(keyId), toHidlVec(message),
1479 [&](Status status, const hidl_vec<uint8_t>& hSignature) {
1480 if (status == Status::OK) {
1481 signature = toVector(hSignature);
1482 }
1483 err = toStatusT(status);
1484 }
1485 );
1486
1487 return hResult.isOk() ? err : DEAD_OBJECT;
1488}
1489
1490status_t DrmHal::verify(Vector<uint8_t> const &sessionId,
Edwin Wong68b3d9f2017-01-06 19:07:54 -08001491 Vector<uint8_t> const &keyId, Vector<uint8_t> const &message,
1492 Vector<uint8_t> const &signature, bool &match) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001493 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001494 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001495
1496 DrmSessionManager::Instance()->useSession(sessionId);
1497
1498 status_t err = UNKNOWN_ERROR;
1499
1500 Return<void> hResult = mPlugin->verify(toHidlVec(sessionId),toHidlVec(keyId),
1501 toHidlVec(message), toHidlVec(signature),
1502 [&](Status status, bool hMatch) {
1503 if (status == Status::OK) {
1504 match = hMatch;
1505 } else {
1506 match = false;
1507 }
1508 err = toStatusT(status);
1509 }
1510 );
1511
1512 return hResult.isOk() ? err : DEAD_OBJECT;
1513}
1514
1515status_t DrmHal::signRSA(Vector<uint8_t> const &sessionId,
Edwin Wong68b3d9f2017-01-06 19:07:54 -08001516 String8 const &algorithm, Vector<uint8_t> const &message,
1517 Vector<uint8_t> const &wrappedKey, Vector<uint8_t> &signature) {
Jeff Tinkera53d6552017-01-20 00:31:46 -08001518 Mutex::Autolock autoLock(mLock);
Jeff Tinker6d998b62017-12-18 14:37:43 -08001519 INIT_CHECK();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001520
1521 if (!checkPermission("android.permission.ACCESS_DRM_CERTIFICATES")) {
1522 return -EPERM;
1523 }
1524
1525 DrmSessionManager::Instance()->useSession(sessionId);
1526
1527 status_t err = UNKNOWN_ERROR;
1528
1529 Return<void> hResult = mPlugin->signRSA(toHidlVec(sessionId),
1530 toHidlString(algorithm), toHidlVec(message), toHidlVec(wrappedKey),
1531 [&](Status status, const hidl_vec<uint8_t>& hSignature) {
1532 if (status == Status::OK) {
1533 signature = toVector(hSignature);
1534 }
1535 err = toStatusT(status);
1536 }
1537 );
1538
1539 return hResult.isOk() ? err : DEAD_OBJECT;
1540}
1541
1542void DrmHal::binderDied(const wp<IBinder> &the_late_who __unused)
1543{
Jeff Tinker7dfe28f2018-02-15 12:17:40 -08001544 cleanup();
Jeff Tinkera53d6552017-01-20 00:31:46 -08001545}
1546
1547void DrmHal::writeByteArray(Parcel &obj, hidl_vec<uint8_t> const &vec)
1548{
1549 if (vec.size()) {
1550 obj.writeInt32(vec.size());
1551 obj.write(vec.data(), vec.size());
1552 } else {
1553 obj.writeInt32(0);
1554 }
1555}
1556
Adam Stonefb679e32018-02-07 10:25:48 -08001557void DrmHal::reportFrameworkMetrics() const
1558{
1559 MediaAnalyticsItem item("mediadrm");
1560 item.generateSessionID();
1561 item.setPkgName(mMetrics.GetAppPackageName().c_str());
1562 String8 vendor;
1563 String8 description;
1564 status_t result = getPropertyStringInternal(String8("vendor"), vendor);
1565 if (result != OK) {
Jeff Tinker987ac702018-02-15 17:02:22 -08001566 ALOGE("Failed to get vendor from drm plugin: %d", result);
Adam Stonefb679e32018-02-07 10:25:48 -08001567 } else {
1568 item.setCString("vendor", vendor.c_str());
1569 }
1570 result = getPropertyStringInternal(String8("description"), description);
1571 if (result != OK) {
Jeff Tinker987ac702018-02-15 17:02:22 -08001572 ALOGE("Failed to get description from drm plugin: %d", result);
Adam Stonefb679e32018-02-07 10:25:48 -08001573 } else {
1574 item.setCString("description", description.c_str());
1575 }
Adam Stoneab394d12017-12-22 12:34:20 -08001576
Adam Stonefb679e32018-02-07 10:25:48 -08001577 std::string serializedMetrics;
1578 result = mMetrics.GetSerializedMetrics(&serializedMetrics);
1579 if (result != OK) {
Jeff Tinker987ac702018-02-15 17:02:22 -08001580 ALOGE("Failed to serialize framework metrics: %d", result);
Adam Stonefb679e32018-02-07 10:25:48 -08001581 }
Adam Stone32494f52018-02-26 22:53:27 -08001582 std::string b64EncodedMetrics = toBase64StringNoPad(serializedMetrics.data(),
1583 serializedMetrics.size());
1584 if (!b64EncodedMetrics.empty()) {
1585 item.setCString("serialized_metrics", b64EncodedMetrics.c_str());
Adam Stonefb679e32018-02-07 10:25:48 -08001586 }
1587 if (!item.selfrecord()) {
Jeff Tinker987ac702018-02-15 17:02:22 -08001588 ALOGE("Failed to self record framework metrics");
Adam Stonefb679e32018-02-07 10:25:48 -08001589 }
1590}
1591
1592void DrmHal::reportPluginMetrics() const
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001593{
Adam Stone32494f52018-02-26 22:53:27 -08001594 Vector<uint8_t> metricsVector;
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001595 String8 vendor;
1596 String8 description;
1597 if (getPropertyStringInternal(String8("vendor"), vendor) == OK &&
1598 getPropertyStringInternal(String8("description"), description) == OK &&
Adam Stone32494f52018-02-26 22:53:27 -08001599 getPropertyByteArrayInternal(String8("metrics"), metricsVector) == OK) {
1600 std::string metricsString = toBase64StringNoPad(metricsVector.array(),
1601 metricsVector.size());
1602 status_t res = android::reportDrmPluginMetrics(metricsString, vendor,
1603 description, mAppPackageName);
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001604 if (res != OK) {
Jeff Tinker987ac702018-02-15 17:02:22 -08001605 ALOGE("Metrics were retrieved but could not be reported: %d", res);
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001606 }
1607 }
1608}
1609
Jeff Tinkera53d6552017-01-20 00:31:46 -08001610} // namespace android