Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "DrmHalAidl" |
| 19 | |
| 20 | #include <android/binder_auto_utils.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <media/PluginMetricsReporting.h> |
| 23 | #include <media/stagefright/foundation/ADebug.h> |
| 24 | #include <media/stagefright/foundation/AString.h> |
| 25 | #include <media/stagefright/foundation/base64.h> |
| 26 | #include <media/stagefright/foundation/hexdump.h> |
| 27 | #include <mediadrm/DrmHalAidl.h> |
| 28 | #include <mediadrm/DrmSessionManager.h> |
| 29 | #include <mediadrm/DrmUtils.h> |
| 30 | |
| 31 | using ::android::DrmUtils::toStatusTAidl; |
| 32 | |
| 33 | using ::aidl::android::hardware::drm::DrmMetricNamedValue; |
| 34 | using ::aidl::android::hardware::drm::DrmMetricValue; |
| 35 | using ::aidl::android::hardware::drm::HdcpLevel; |
| 36 | using ::aidl::android::hardware::drm::HdcpLevels; |
| 37 | using ::aidl::android::hardware::drm::KeyRequest; |
| 38 | using ::aidl::android::hardware::drm::KeyRequestType; |
| 39 | using ::aidl::android::hardware::drm::KeySetId; |
| 40 | using ::aidl::android::hardware::drm::KeyStatus; |
| 41 | using ::aidl::android::hardware::drm::KeyStatusType; |
| 42 | using ::aidl::android::hardware::drm::KeyType; |
| 43 | using ::aidl::android::hardware::drm::KeyValue; |
| 44 | using ::aidl::android::hardware::drm::NumberOfSessions; |
| 45 | using ::aidl::android::hardware::drm::OfflineLicenseState; |
| 46 | using ::aidl::android::hardware::drm::OpaqueData; |
| 47 | using ::aidl::android::hardware::drm::ProvideProvisionResponseResult; |
| 48 | using ::aidl::android::hardware::drm::ProvisionRequest; |
| 49 | using ::aidl::android::hardware::drm::SecureStop; |
| 50 | using ::aidl::android::hardware::drm::SecureStopId; |
| 51 | using ::aidl::android::hardware::drm::SecurityLevel; |
| 52 | using ::aidl::android::hardware::drm::Status; |
| 53 | using ::aidl::android::hardware::drm::Uuid; |
| 54 | using DrmMetricGroupAidl = ::aidl::android::hardware::drm::DrmMetricGroup; |
| 55 | using DrmMetricGroupHidl = ::android::hardware::drm::V1_1::DrmMetricGroup; |
| 56 | using DrmMetricAidl = ::aidl::android::hardware::drm::DrmMetric; |
| 57 | using DrmMetricHidl = ::android::hardware::drm::V1_1::DrmMetricGroup::Metric; |
| 58 | using ValueHidl = ::android::hardware::drm::V1_1::DrmMetricGroup::Value; |
| 59 | using AttributeHidl = ::android::hardware::drm::V1_1::DrmMetricGroup::Attribute; |
| 60 | using IDrmPluginAidl = ::aidl::android::hardware::drm::IDrmPlugin; |
| 61 | using EventTypeAidl = ::aidl::android::hardware::drm::EventType; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 62 | using ::android::hardware::hidl_vec; |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | constexpr char kPropertyDeviceUniqueId[] = "deviceUniqueId"; |
| 67 | constexpr char kEqualsSign[] = "="; |
| 68 | |
| 69 | template <typename T> |
| 70 | std::string toBase64StringNoPad(const T* data, size_t size) { |
| 71 | // Note that the base 64 conversion only works with arrays of single-byte |
| 72 | // values. If the source is empty or is not an array of single-byte values, |
| 73 | // return empty string. |
| 74 | if (size == 0 || sizeof(data[0]) != 1) { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | android::AString outputString; |
| 79 | encodeBase64(data, size, &outputString); |
| 80 | // Remove trailing equals padding if it exists. |
| 81 | while (outputString.size() > 0 && outputString.endsWith(kEqualsSign)) { |
| 82 | outputString.erase(outputString.size() - 1, 1); |
| 83 | } |
| 84 | |
| 85 | return std::string(outputString.c_str(), outputString.size()); |
| 86 | } |
| 87 | |
| 88 | } // anonymous namespace |
| 89 | |
| 90 | namespace android { |
| 91 | |
| 92 | #define INIT_CHECK() \ |
| 93 | { \ |
| 94 | if (mInitCheck != OK) return mInitCheck; \ |
| 95 | } |
| 96 | |
| 97 | static Uuid toAidlUuid(const uint8_t* uuid) { |
| 98 | Uuid uuidAidl; |
| 99 | uuidAidl.uuid = std::vector<uint8_t>(uuid, uuid + 16); |
| 100 | return uuidAidl; |
| 101 | } |
| 102 | |
| 103 | template <typename Byte = uint8_t> |
| 104 | static std::vector<Byte> toStdVec(const Vector<uint8_t>& vector) { |
| 105 | auto v = reinterpret_cast<const Byte*>(vector.array()); |
| 106 | std::vector<Byte> vec(v, v + vector.size()); |
| 107 | return vec; |
| 108 | } |
| 109 | |
| 110 | static const Vector<uint8_t> toVector(const std::vector<uint8_t>& vec) { |
| 111 | Vector<uint8_t> vector; |
| 112 | vector.appendArray(vec.data(), vec.size()); |
| 113 | return *const_cast<const Vector<uint8_t>*>(&vector); |
| 114 | } |
| 115 | |
| 116 | static String8 toString8(const std::string& string) { |
| 117 | return String8(string.c_str()); |
| 118 | } |
| 119 | |
| 120 | static std::string toStdString(const String8& string8) { |
| 121 | return std::string(string8.string()); |
| 122 | } |
| 123 | |
| 124 | static std::vector<KeyValue> toKeyValueVector(const KeyedVector<String8, String8>& keyedVector) { |
| 125 | std::vector<KeyValue> stdKeyedVector; |
| 126 | for (size_t i = 0; i < keyedVector.size(); i++) { |
| 127 | KeyValue keyValue; |
| 128 | keyValue.key = toStdString(keyedVector.keyAt(i)); |
| 129 | keyValue.value = toStdString(keyedVector.valueAt(i)); |
| 130 | stdKeyedVector.push_back(keyValue); |
| 131 | } |
| 132 | return stdKeyedVector; |
| 133 | } |
| 134 | |
| 135 | static KeyedVector<String8, String8> toKeyedVector(const std::vector<KeyValue>& keyValueVec) { |
| 136 | KeyedVector<String8, String8> keyedVector; |
| 137 | for (size_t i = 0; i < keyValueVec.size(); i++) { |
| 138 | keyedVector.add(toString8(keyValueVec[i].key), toString8(keyValueVec[i].value)); |
| 139 | } |
| 140 | return keyedVector; |
| 141 | } |
| 142 | |
| 143 | static DrmPlugin::KeyRequestType toKeyRequestType(KeyRequestType keyRequestType) { |
| 144 | switch (keyRequestType) { |
| 145 | case KeyRequestType::INITIAL: |
| 146 | return DrmPlugin::kKeyRequestType_Initial; |
| 147 | break; |
| 148 | case KeyRequestType::RENEWAL: |
| 149 | return DrmPlugin::kKeyRequestType_Renewal; |
| 150 | break; |
| 151 | case KeyRequestType::RELEASE: |
| 152 | return DrmPlugin::kKeyRequestType_Release; |
| 153 | break; |
| 154 | case KeyRequestType::NONE: |
| 155 | return DrmPlugin::kKeyRequestType_None; |
| 156 | break; |
| 157 | case KeyRequestType::UPDATE: |
| 158 | return DrmPlugin::kKeyRequestType_Update; |
| 159 | break; |
| 160 | default: |
| 161 | return DrmPlugin::kKeyRequestType_Unknown; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static List<Vector<uint8_t>> toSecureStops(const std::vector<SecureStop>& aSecureStops) { |
| 167 | List<Vector<uint8_t>> secureStops; |
| 168 | for (size_t i = 0; i < aSecureStops.size(); i++) { |
| 169 | secureStops.push_back(toVector(aSecureStops[i].opaqueData)); |
| 170 | } |
| 171 | return secureStops; |
| 172 | } |
| 173 | |
| 174 | static List<Vector<uint8_t>> toSecureStopIds(const std::vector<SecureStopId>& aSecureStopIds) { |
| 175 | List<Vector<uint8_t>> secureStopIds; |
| 176 | for (size_t i = 0; i < aSecureStopIds.size(); i++) { |
| 177 | secureStopIds.push_back(toVector(aSecureStopIds[i].secureStopId)); |
| 178 | } |
| 179 | return secureStopIds; |
| 180 | } |
| 181 | |
| 182 | static DrmPlugin::HdcpLevel toHdcpLevel(HdcpLevel level) { |
| 183 | switch (level) { |
| 184 | case HdcpLevel::HDCP_NONE: |
| 185 | return DrmPlugin::kHdcpNone; |
| 186 | case HdcpLevel::HDCP_V1: |
| 187 | return DrmPlugin::kHdcpV1; |
| 188 | case HdcpLevel::HDCP_V2: |
| 189 | return DrmPlugin::kHdcpV2; |
| 190 | case HdcpLevel::HDCP_V2_1: |
| 191 | return DrmPlugin::kHdcpV2_1; |
| 192 | case HdcpLevel::HDCP_V2_2: |
| 193 | return DrmPlugin::kHdcpV2_2; |
| 194 | case HdcpLevel::HDCP_V2_3: |
| 195 | return DrmPlugin::kHdcpV2_3; |
| 196 | case HdcpLevel::HDCP_NO_OUTPUT: |
| 197 | return DrmPlugin::kHdcpNoOutput; |
| 198 | default: |
| 199 | return DrmPlugin::kHdcpLevelUnknown; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static DrmPlugin::SecurityLevel toSecurityLevel(SecurityLevel level) { |
| 204 | switch (level) { |
| 205 | case SecurityLevel::SW_SECURE_CRYPTO: |
| 206 | return DrmPlugin::kSecurityLevelSwSecureCrypto; |
| 207 | case SecurityLevel::SW_SECURE_DECODE: |
| 208 | return DrmPlugin::kSecurityLevelSwSecureDecode; |
| 209 | case SecurityLevel::HW_SECURE_CRYPTO: |
| 210 | return DrmPlugin::kSecurityLevelHwSecureCrypto; |
| 211 | case SecurityLevel::HW_SECURE_DECODE: |
| 212 | return DrmPlugin::kSecurityLevelHwSecureDecode; |
| 213 | case SecurityLevel::HW_SECURE_ALL: |
| 214 | return DrmPlugin::kSecurityLevelHwSecureAll; |
| 215 | case SecurityLevel::DEFAULT: |
| 216 | return DrmPlugin::kSecurityLevelMax; |
| 217 | default: |
| 218 | return DrmPlugin::kSecurityLevelUnknown; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static SecurityLevel toAidlSecurityLevel(DrmPlugin::SecurityLevel level) { |
| 223 | switch (level) { |
| 224 | case DrmPlugin::kSecurityLevelSwSecureCrypto: |
| 225 | return SecurityLevel::SW_SECURE_CRYPTO; |
| 226 | case DrmPlugin::kSecurityLevelSwSecureDecode: |
| 227 | return SecurityLevel::SW_SECURE_DECODE; |
| 228 | case DrmPlugin::kSecurityLevelHwSecureCrypto: |
| 229 | return SecurityLevel::HW_SECURE_CRYPTO; |
| 230 | case DrmPlugin::kSecurityLevelHwSecureDecode: |
| 231 | return SecurityLevel::HW_SECURE_DECODE; |
| 232 | case DrmPlugin::kSecurityLevelHwSecureAll: |
| 233 | return SecurityLevel::HW_SECURE_ALL; |
| 234 | case DrmPlugin::kSecurityLevelMax: |
| 235 | return SecurityLevel::DEFAULT; |
| 236 | default: |
| 237 | return SecurityLevel::UNKNOWN; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static List<Vector<uint8_t>> toKeySetIds(const std::vector<KeySetId>& hKeySetIds) { |
| 242 | List<Vector<uint8_t>> keySetIds; |
| 243 | for (size_t i = 0; i < hKeySetIds.size(); i++) { |
| 244 | keySetIds.push_back(toVector(hKeySetIds[i].keySetId)); |
| 245 | } |
| 246 | return keySetIds; |
| 247 | } |
| 248 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 249 | static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t>& vector) { |
| 250 | hidl_vec<uint8_t> vec; |
| 251 | vec.setToExternal(const_cast<uint8_t*>(vector.array()), vector.size()); |
| 252 | return vec; |
| 253 | } |
| 254 | |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 255 | static DrmPlugin::OfflineLicenseState toOfflineLicenseState(OfflineLicenseState licenseState) { |
| 256 | switch (licenseState) { |
| 257 | case OfflineLicenseState::USABLE: |
| 258 | return DrmPlugin::kOfflineLicenseStateUsable; |
| 259 | case OfflineLicenseState::INACTIVE: |
| 260 | return DrmPlugin::kOfflineLicenseStateReleased; |
| 261 | default: |
| 262 | return DrmPlugin::kOfflineLicenseStateUnknown; |
| 263 | } |
| 264 | } |
| 265 | |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 266 | Mutex DrmHalAidl::mLock; |
| 267 | |
| 268 | static hidl_vec<DrmMetricGroupHidl> toDrmMetricGroupHidl(std::vector<DrmMetricGroupAidl> result) { |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 269 | std::vector<DrmMetricGroupHidl> resultHidl; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 270 | for (auto r : result) { |
| 271 | DrmMetricGroupHidl re; |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 272 | std::vector<DrmMetricHidl> tmpMetric; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 273 | for (auto m : r.metrics) { |
| 274 | DrmMetricHidl me; |
| 275 | me.name = m.name; |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 276 | std::vector<AttributeHidl> aTmp; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 277 | for (auto attr : m.attributes) { |
| 278 | AttributeHidl attrHidl; |
| 279 | attrHidl.name = attr.name; |
| 280 | |
| 281 | switch (attr.value.getTag()) { |
| 282 | case DrmMetricValue::Tag::int64Value: |
| 283 | attrHidl.type = DrmMetricGroupHidl::ValueType::INT64_TYPE; |
| 284 | attrHidl.int64Value = attr.value.get<DrmMetricValue::Tag::int64Value>(); |
| 285 | break; |
| 286 | case DrmMetricValue::Tag::doubleValue: |
| 287 | attrHidl.type = DrmMetricGroupHidl::ValueType::DOUBLE_TYPE; |
| 288 | attrHidl.doubleValue = attr.value.get<DrmMetricValue::Tag::doubleValue>(); |
| 289 | break; |
| 290 | case DrmMetricValue::Tag::stringValue: |
| 291 | attrHidl.type = DrmMetricGroupHidl::ValueType::STRING_TYPE; |
| 292 | attrHidl.stringValue = attr.value.get<DrmMetricValue::Tag::stringValue>(); |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | aTmp.push_back(attrHidl); |
| 299 | } |
| 300 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 301 | me.attributes = aTmp; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 302 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 303 | std::vector<ValueHidl> vTmp; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 304 | for (auto value : m.values) { |
| 305 | ValueHidl valueHidl; |
| 306 | valueHidl.componentName = value.name; |
| 307 | switch (value.value.getTag()) { |
| 308 | case DrmMetricValue::Tag::int64Value: |
| 309 | valueHidl.type = DrmMetricGroupHidl::ValueType::INT64_TYPE; |
| 310 | valueHidl.int64Value = value.value.get<DrmMetricValue::Tag::int64Value>(); |
| 311 | break; |
| 312 | case DrmMetricValue::Tag::doubleValue: |
| 313 | valueHidl.type = DrmMetricGroupHidl::ValueType::DOUBLE_TYPE; |
| 314 | valueHidl.doubleValue = value.value.get<DrmMetricValue::Tag::doubleValue>(); |
| 315 | break; |
| 316 | case DrmMetricValue::Tag::stringValue: |
| 317 | valueHidl.type = DrmMetricGroupHidl::ValueType::STRING_TYPE; |
| 318 | valueHidl.stringValue = value.value.get<DrmMetricValue::Tag::stringValue>(); |
| 319 | break; |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | vTmp.push_back(valueHidl); |
| 325 | } |
| 326 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 327 | me.values = vTmp; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 328 | tmpMetric.push_back(me); |
| 329 | } |
| 330 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 331 | re.metrics = tmpMetric; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 332 | resultHidl.push_back(re); |
| 333 | } |
| 334 | |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 335 | return resultHidl; |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // DrmSessionClient Definition |
| 339 | |
| 340 | struct DrmHalAidl::DrmSessionClient : public aidl::android::media::BnResourceManagerClient { |
| 341 | explicit DrmSessionClient(DrmHalAidl* drm, const Vector<uint8_t>& sessionId) |
| 342 | : mSessionId(sessionId), mDrm(drm) {} |
| 343 | |
| 344 | ::ndk::ScopedAStatus reclaimResource(bool* _aidl_return) override; |
| 345 | ::ndk::ScopedAStatus getName(::std::string* _aidl_return) override; |
| 346 | |
| 347 | const Vector<uint8_t> mSessionId; |
| 348 | |
| 349 | virtual ~DrmSessionClient(); |
| 350 | |
| 351 | private: |
| 352 | wp<DrmHalAidl> mDrm; |
| 353 | |
| 354 | DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient); |
| 355 | }; |
| 356 | |
| 357 | ::ndk::ScopedAStatus DrmHalAidl::DrmSessionClient::reclaimResource(bool* _aidl_return) { |
| 358 | auto sessionId = mSessionId; |
| 359 | sp<DrmHalAidl> drm = mDrm.promote(); |
| 360 | if (drm == NULL) { |
| 361 | *_aidl_return = true; |
| 362 | return ::ndk::ScopedAStatus::ok(); |
| 363 | } |
| 364 | status_t err = drm->closeSession(sessionId); |
| 365 | if (err != OK) { |
| 366 | *_aidl_return = false; |
| 367 | return ::ndk::ScopedAStatus::ok(); |
| 368 | } |
| 369 | drm->onEvent(EventTypeAidl::SESSION_RECLAIMED, toHidlVec(sessionId), hidl_vec<uint8_t>()); |
| 370 | *_aidl_return = true; |
| 371 | return ::ndk::ScopedAStatus::ok(); |
| 372 | } |
| 373 | |
| 374 | ::ndk::ScopedAStatus DrmHalAidl::DrmSessionClient::getName(::std::string* _aidl_return) { |
| 375 | String8 name; |
| 376 | sp<DrmHalAidl> drm = mDrm.promote(); |
| 377 | if (drm == NULL) { |
| 378 | name.append("<deleted>"); |
| 379 | } else if (drm->getPropertyStringInternal(String8("vendor"), name) != OK || name.isEmpty()) { |
| 380 | name.append("<Get vendor failed or is empty>"); |
| 381 | } |
| 382 | name.append("["); |
| 383 | for (size_t i = 0; i < mSessionId.size(); ++i) { |
| 384 | name.appendFormat("%02x", mSessionId[i]); |
| 385 | } |
| 386 | name.append("]"); |
| 387 | *_aidl_return = name; |
| 388 | return ::ndk::ScopedAStatus::ok(); |
| 389 | } |
| 390 | |
| 391 | DrmHalAidl::DrmSessionClient::~DrmSessionClient() { |
| 392 | DrmSessionManager::Instance()->removeSession(mSessionId); |
| 393 | } |
| 394 | |
| 395 | // DrmHalAidl methods |
| 396 | DrmHalAidl::DrmHalAidl() |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 397 | : mListener(::ndk::SharedRefBase::make<DrmHalListener>(&mMetrics)), |
| 398 | mFactories(makeDrmFactories()), |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 399 | mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT) {} |
| 400 | |
| 401 | status_t DrmHalAidl::initCheck() const { |
| 402 | return mInitCheck; |
| 403 | } |
| 404 | |
| 405 | DrmHalAidl::~DrmHalAidl() {} |
| 406 | |
| 407 | std::vector<std::shared_ptr<IDrmFactoryAidl>> DrmHalAidl::makeDrmFactories() { |
| 408 | std::vector<std::shared_ptr<IDrmFactoryAidl>> factories; |
| 409 | AServiceManager_forEachDeclaredInstance( |
| 410 | IDrmFactoryAidl::descriptor, static_cast<void*>(&factories), |
| 411 | [](const char* instance, void* context) { |
| 412 | auto fullName = std::string(IDrmFactoryAidl::descriptor) + "/" + std::string(instance); |
| 413 | auto factory = IDrmFactoryAidl::fromBinder( |
| 414 | ::ndk::SpAIBinder(AServiceManager_getService(fullName.c_str()))); |
| 415 | if (factory == nullptr) { |
| 416 | ALOGE("not found IDrmFactory. Instance name:[%s]", fullName.c_str()); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | ALOGI("found IDrmFactory. Instance name:[%s]", fullName.c_str()); |
| 421 | static_cast<std::vector<std::shared_ptr<IDrmFactoryAidl>>*>(context)->emplace_back( |
| 422 | factory); |
| 423 | }); |
| 424 | |
| 425 | return factories; |
| 426 | } |
| 427 | |
| 428 | status_t DrmHalAidl::setListener(const sp<IDrmClient>& listener) { |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 429 | mListener->setListener(listener); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 430 | return NO_ERROR; |
| 431 | } |
| 432 | |
| 433 | status_t DrmHalAidl::isCryptoSchemeSupported(const uint8_t uuid[16], const String8& mimeType, |
| 434 | DrmPlugin::SecurityLevel level, bool* isSupported) { |
| 435 | Mutex::Autolock autoLock(mLock); |
| 436 | *isSupported = false; |
| 437 | Uuid uuidAidl = toAidlUuid(uuid); |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 438 | SecurityLevel levelAidl = toAidlSecurityLevel(level); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 439 | std::string mimeTypeStr = mimeType.string(); |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 440 | |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 441 | for (ssize_t i = mFactories.size() - 1; i >= 0; i--) { |
| 442 | if (mFactories[i] |
| 443 | ->isCryptoSchemeSupported(uuidAidl, mimeTypeStr, levelAidl, isSupported) |
| 444 | .isOk()) { |
| 445 | if (*isSupported) break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return OK; |
| 450 | } |
| 451 | |
| 452 | status_t DrmHalAidl::createPlugin(const uint8_t uuid[16], const String8& appPackageName) { |
| 453 | Mutex::Autolock autoLock(mLock); |
| 454 | |
| 455 | Uuid uuidAidl = toAidlUuid(uuid); |
| 456 | std::string appPackageNameAidl = toStdString(appPackageName); |
| 457 | std::shared_ptr<IDrmPluginAidl> pluginAidl; |
| 458 | mMetrics.SetAppPackageName(appPackageName); |
| 459 | mMetrics.SetAppUid(AIBinder_getCallingUid()); |
| 460 | for (ssize_t i = mFactories.size() - 1; i >= 0; i--) { |
| 461 | ::ndk::ScopedAStatus status = |
| 462 | mFactories[i]->createPlugin(uuidAidl, appPackageNameAidl, &pluginAidl); |
| 463 | if (status.isOk()) { |
| 464 | if (pluginAidl != NULL) { |
| 465 | mPlugin = pluginAidl; |
| 466 | break; |
| 467 | } |
| 468 | } else { |
| 469 | DrmUtils::LOG2BE(uuid, "Failed to make drm plugin: %d", |
| 470 | status.getServiceSpecificError()); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (mPlugin == NULL) { |
| 475 | DrmUtils::LOG2BE(uuid, "No supported hal instance found"); |
| 476 | mInitCheck = ERROR_UNSUPPORTED; |
| 477 | } else { |
| 478 | mInitCheck = OK; |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 479 | // Stored pointer mListener upcast to base BnDrmPluginListener |
| 480 | ::ndk::ScopedAStatus status = mPlugin |
| 481 | ->setListener(std::static_pointer_cast<BnDrmPluginListener>(mListener)); |
| 482 | if (!status.isOk()) { |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 483 | mInitCheck = DEAD_OBJECT; |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 484 | ALOGE("setListener failed: ex %d svc err %d", |
| 485 | status.getExceptionCode(), |
| 486 | status.getServiceSpecificError()); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (mInitCheck != OK) { |
| 490 | mPlugin.reset(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return mInitCheck; |
| 495 | } |
| 496 | |
| 497 | status_t DrmHalAidl::openSession(DrmPlugin::SecurityLevel level, Vector<uint8_t>& sessionId) { |
| 498 | Mutex::Autolock autoLock(mLock); |
| 499 | INIT_CHECK(); |
| 500 | |
| 501 | SecurityLevel aSecurityLevel = toAidlSecurityLevel(level); |
| 502 | |
| 503 | if (aSecurityLevel == SecurityLevel::UNKNOWN) { |
| 504 | return ERROR_DRM_CANNOT_HANDLE; |
| 505 | } |
| 506 | |
| 507 | status_t err = UNKNOWN_ERROR; |
| 508 | bool retry = true; |
| 509 | do { |
| 510 | std::vector<uint8_t> aSessionId; |
| 511 | |
| 512 | ::ndk::ScopedAStatus status = mPlugin->openSession(aSecurityLevel, &aSessionId); |
| 513 | if (status.isOk()) sessionId = toVector(aSessionId); |
| 514 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 515 | |
| 516 | if (err == ERROR_DRM_RESOURCE_BUSY && retry) { |
| 517 | mLock.unlock(); |
| 518 | // reclaimSession may call back to closeSession, since mLock is |
| 519 | // shared between Drm instances, we should unlock here to avoid |
| 520 | // deadlock. |
| 521 | retry = DrmSessionManager::Instance()->reclaimSession(AIBinder_getCallingPid()); |
| 522 | mLock.lock(); |
| 523 | } else { |
| 524 | retry = false; |
| 525 | } |
| 526 | } while (retry); |
| 527 | |
| 528 | if (err == OK) { |
| 529 | std::shared_ptr<DrmSessionClient> client = |
| 530 | ndk::SharedRefBase::make<DrmSessionClient>(this, sessionId); |
| 531 | DrmSessionManager::Instance()->addSession( |
| 532 | AIBinder_getCallingPid(), std::static_pointer_cast<IResourceManagerClient>(client), |
| 533 | sessionId); |
| 534 | mOpenSessions.push_back(client); |
| 535 | mMetrics.SetSessionStart(sessionId); |
| 536 | } |
| 537 | |
| 538 | mMetrics.mOpenSessionCounter.Increment(err); |
| 539 | return err; |
| 540 | } |
| 541 | |
| 542 | status_t DrmHalAidl::closeSession(Vector<uint8_t> const& sessionId) { |
| 543 | Mutex::Autolock autoLock(mLock); |
| 544 | INIT_CHECK(); |
| 545 | |
| 546 | std::vector<uint8_t> sessionIdAidl = toStdVec(sessionId); |
| 547 | ::ndk::ScopedAStatus status = mPlugin->closeSession(sessionIdAidl); |
| 548 | if (status.isOk()) { |
| 549 | DrmSessionManager::Instance()->removeSession(sessionId); |
| 550 | for (auto i = mOpenSessions.begin(); i != mOpenSessions.end(); i++) { |
| 551 | if (isEqualSessionId((*i)->mSessionId, sessionId)) { |
| 552 | mOpenSessions.erase(i); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | status_t response = toStatusTAidl(status.getServiceSpecificError()); |
| 558 | mMetrics.SetSessionEnd(sessionId); |
| 559 | mMetrics.mCloseSessionCounter.Increment(response); |
| 560 | return response; |
| 561 | } |
Kyle Zhang | fa72c48 | 2022-02-04 03:55:43 +0000 | [diff] [blame] | 562 | |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 563 | mMetrics.mCloseSessionCounter.Increment(DEAD_OBJECT); |
| 564 | return DEAD_OBJECT; |
| 565 | } |
| 566 | |
| 567 | status_t DrmHalAidl::getKeyRequest(Vector<uint8_t> const& sessionId, |
| 568 | Vector<uint8_t> const& initData, String8 const& mimeType, |
| 569 | DrmPlugin::KeyType keyType, |
| 570 | KeyedVector<String8, String8> const& optionalParameters, |
| 571 | Vector<uint8_t>& request, String8& defaultUrl, |
| 572 | DrmPlugin::KeyRequestType* keyRequestType) { |
| 573 | Mutex::Autolock autoLock(mLock); |
| 574 | INIT_CHECK(); |
| 575 | EventTimer<status_t> keyRequestTimer(&mMetrics.mGetKeyRequestTimeUs); |
| 576 | |
| 577 | DrmSessionManager::Instance()->useSession(sessionId); |
| 578 | |
| 579 | KeyType aKeyType; |
| 580 | if (keyType == DrmPlugin::kKeyType_Streaming) { |
| 581 | aKeyType = KeyType::STREAMING; |
| 582 | } else if (keyType == DrmPlugin::kKeyType_Offline) { |
| 583 | aKeyType = KeyType::OFFLINE; |
| 584 | } else if (keyType == DrmPlugin::kKeyType_Release) { |
| 585 | aKeyType = KeyType::RELEASE; |
| 586 | } else { |
| 587 | keyRequestTimer.SetAttribute(BAD_VALUE); |
| 588 | return BAD_VALUE; |
| 589 | } |
| 590 | |
| 591 | status_t err = UNKNOWN_ERROR; |
| 592 | |
| 593 | std::vector<uint8_t> sessionIdAidl = toStdVec(sessionId); |
| 594 | std::vector<uint8_t> initDataAidl = toStdVec(initData); |
| 595 | KeyRequest keyRequest; |
| 596 | |
| 597 | ::ndk::ScopedAStatus status = |
| 598 | mPlugin->getKeyRequest(sessionIdAidl, initDataAidl, toStdString(mimeType), aKeyType, |
| 599 | toKeyValueVector(optionalParameters), &keyRequest); |
| 600 | if (status.isOk()) { |
| 601 | request = toVector(keyRequest.request); |
| 602 | defaultUrl = toString8(keyRequest.defaultUrl); |
| 603 | *keyRequestType = toKeyRequestType(keyRequest.requestType); |
| 604 | } |
| 605 | |
| 606 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 607 | keyRequestTimer.SetAttribute(err); |
| 608 | return err; |
| 609 | } |
| 610 | |
| 611 | status_t DrmHalAidl::provideKeyResponse(Vector<uint8_t> const& sessionId, |
| 612 | Vector<uint8_t> const& response, |
| 613 | Vector<uint8_t>& keySetId) { |
| 614 | Mutex::Autolock autoLock(mLock); |
| 615 | INIT_CHECK(); |
| 616 | EventTimer<status_t> keyResponseTimer(&mMetrics.mProvideKeyResponseTimeUs); |
| 617 | |
| 618 | DrmSessionManager::Instance()->useSession(sessionId); |
| 619 | |
| 620 | status_t err = UNKNOWN_ERROR; |
| 621 | |
| 622 | std::vector<uint8_t> sessionIdAidl = toStdVec(sessionId); |
| 623 | std::vector<uint8_t> responseAidl = toStdVec(response); |
| 624 | KeySetId keySetIdsAidl; |
| 625 | ::ndk::ScopedAStatus status = |
| 626 | mPlugin->provideKeyResponse(sessionIdAidl, responseAidl, &keySetIdsAidl); |
| 627 | |
| 628 | if (status.isOk()) keySetId = toVector(keySetIdsAidl.keySetId); |
| 629 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 630 | keyResponseTimer.SetAttribute(err); |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | status_t DrmHalAidl::removeKeys(Vector<uint8_t> const& keySetId) { |
| 635 | Mutex::Autolock autoLock(mLock); |
| 636 | INIT_CHECK(); |
| 637 | |
| 638 | ::ndk::ScopedAStatus status = mPlugin->removeKeys(toStdVec(keySetId)); |
| 639 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 640 | } |
| 641 | |
| 642 | status_t DrmHalAidl::restoreKeys(Vector<uint8_t> const& sessionId, |
| 643 | Vector<uint8_t> const& keySetId) { |
| 644 | Mutex::Autolock autoLock(mLock); |
| 645 | INIT_CHECK(); |
| 646 | |
| 647 | DrmSessionManager::Instance()->useSession(sessionId); |
| 648 | |
| 649 | KeySetId keySetIdsAidl; |
| 650 | keySetIdsAidl.keySetId = toStdVec(keySetId); |
| 651 | ::ndk::ScopedAStatus status = mPlugin->restoreKeys(toStdVec(sessionId), keySetIdsAidl); |
| 652 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 653 | } |
| 654 | |
| 655 | status_t DrmHalAidl::queryKeyStatus(Vector<uint8_t> const& sessionId, |
| 656 | KeyedVector<String8, String8>& infoMap) const { |
| 657 | Mutex::Autolock autoLock(mLock); |
| 658 | INIT_CHECK(); |
| 659 | |
| 660 | DrmSessionManager::Instance()->useSession(sessionId); |
| 661 | |
| 662 | std::vector<KeyValue> infoMapAidl; |
| 663 | ::ndk::ScopedAStatus status = mPlugin->queryKeyStatus(toStdVec(sessionId), &infoMapAidl); |
| 664 | |
| 665 | infoMap = toKeyedVector(infoMapAidl); |
| 666 | |
| 667 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 668 | } |
| 669 | |
| 670 | status_t DrmHalAidl::getProvisionRequest(String8 const& certType, String8 const& certAuthority, |
| 671 | Vector<uint8_t>& request, String8& defaultUrl) { |
| 672 | Mutex::Autolock autoLock(mLock); |
| 673 | INIT_CHECK(); |
| 674 | |
| 675 | status_t err = UNKNOWN_ERROR; |
| 676 | |
| 677 | ProvisionRequest requestAidl; |
| 678 | ::ndk::ScopedAStatus status = mPlugin->getProvisionRequest( |
| 679 | toStdString(certType), toStdString(certAuthority), &requestAidl); |
| 680 | |
| 681 | request = toVector(requestAidl.request); |
| 682 | defaultUrl = toString8(requestAidl.defaultUrl); |
| 683 | |
| 684 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 685 | mMetrics.mGetProvisionRequestCounter.Increment(err); |
| 686 | return err; |
| 687 | } |
| 688 | |
| 689 | status_t DrmHalAidl::provideProvisionResponse(Vector<uint8_t> const& response, |
| 690 | Vector<uint8_t>& certificate, |
| 691 | Vector<uint8_t>& wrappedKey) { |
| 692 | Mutex::Autolock autoLock(mLock); |
| 693 | INIT_CHECK(); |
| 694 | |
| 695 | status_t err = UNKNOWN_ERROR; |
| 696 | ProvideProvisionResponseResult result; |
| 697 | ::ndk::ScopedAStatus status = mPlugin->provideProvisionResponse(toStdVec(response), &result); |
| 698 | |
| 699 | certificate = toVector(result.certificate); |
| 700 | wrappedKey = toVector(result.wrappedKey); |
| 701 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 702 | mMetrics.mProvideProvisionResponseCounter.Increment(err); |
| 703 | return err; |
| 704 | } |
| 705 | |
| 706 | status_t DrmHalAidl::getSecureStops(List<Vector<uint8_t>>& secureStops) { |
| 707 | Mutex::Autolock autoLock(mLock); |
| 708 | INIT_CHECK(); |
| 709 | |
| 710 | std::vector<SecureStop> result; |
| 711 | ::ndk::ScopedAStatus status = mPlugin->getSecureStops(&result); |
| 712 | |
| 713 | secureStops = toSecureStops(result); |
| 714 | |
| 715 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 716 | } |
| 717 | |
| 718 | status_t DrmHalAidl::getSecureStopIds(List<Vector<uint8_t>>& secureStopIds) { |
| 719 | Mutex::Autolock autoLock(mLock); |
| 720 | INIT_CHECK(); |
| 721 | |
| 722 | std::vector<SecureStopId> result; |
| 723 | ::ndk::ScopedAStatus status = mPlugin->getSecureStopIds(&result); |
| 724 | |
| 725 | secureStopIds = toSecureStopIds(result); |
| 726 | |
| 727 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 728 | } |
| 729 | |
| 730 | status_t DrmHalAidl::getSecureStop(Vector<uint8_t> const& ssid, Vector<uint8_t>& secureStop) { |
| 731 | Mutex::Autolock autoLock(mLock); |
| 732 | INIT_CHECK(); |
| 733 | |
| 734 | SecureStopId ssidAidl; |
| 735 | ssidAidl.secureStopId = toStdVec(ssid); |
| 736 | |
| 737 | SecureStop result; |
| 738 | ::ndk::ScopedAStatus status = mPlugin->getSecureStop(ssidAidl, &result); |
| 739 | |
| 740 | secureStop = toVector(result.opaqueData); |
| 741 | |
| 742 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 743 | } |
| 744 | |
| 745 | status_t DrmHalAidl::releaseSecureStops(Vector<uint8_t> const& ssRelease) { |
| 746 | Mutex::Autolock autoLock(mLock); |
| 747 | INIT_CHECK(); |
| 748 | |
| 749 | OpaqueData ssId; |
| 750 | ssId.opaqueData = toStdVec(ssRelease); |
| 751 | ::ndk::ScopedAStatus status = mPlugin->releaseSecureStops(ssId); |
| 752 | |
| 753 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 754 | } |
| 755 | |
| 756 | status_t DrmHalAidl::removeSecureStop(Vector<uint8_t> const& ssid) { |
| 757 | Mutex::Autolock autoLock(mLock); |
| 758 | |
| 759 | INIT_CHECK(); |
| 760 | |
| 761 | SecureStopId ssidAidl; |
| 762 | ssidAidl.secureStopId = toStdVec(ssid); |
| 763 | ::ndk::ScopedAStatus status = mPlugin->removeSecureStop(ssidAidl); |
| 764 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 765 | } |
| 766 | |
| 767 | status_t DrmHalAidl::removeAllSecureStops() { |
| 768 | Mutex::Autolock autoLock(mLock); |
| 769 | INIT_CHECK(); |
| 770 | |
| 771 | ::ndk::ScopedAStatus status = mPlugin->releaseAllSecureStops(); |
| 772 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 773 | } |
| 774 | |
| 775 | status_t DrmHalAidl::getHdcpLevels(DrmPlugin::HdcpLevel* connected, |
| 776 | DrmPlugin::HdcpLevel* max) const { |
| 777 | Mutex::Autolock autoLock(mLock); |
| 778 | INIT_CHECK(); |
| 779 | |
| 780 | if (connected == NULL || max == NULL) { |
| 781 | return BAD_VALUE; |
| 782 | } |
| 783 | |
| 784 | *connected = DrmPlugin::kHdcpLevelUnknown; |
| 785 | *max = DrmPlugin::kHdcpLevelUnknown; |
| 786 | |
| 787 | HdcpLevels lvlsAidl; |
| 788 | ::ndk::ScopedAStatus status = mPlugin->getHdcpLevels(&lvlsAidl); |
| 789 | |
| 790 | *connected = toHdcpLevel(lvlsAidl.connectedLevel); |
| 791 | *max = toHdcpLevel(lvlsAidl.maxLevel); |
| 792 | |
| 793 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 794 | } |
| 795 | |
| 796 | status_t DrmHalAidl::getNumberOfSessions(uint32_t* open, uint32_t* max) const { |
| 797 | Mutex::Autolock autoLock(mLock); |
| 798 | INIT_CHECK(); |
| 799 | |
| 800 | if (open == NULL || max == NULL) { |
| 801 | return BAD_VALUE; |
| 802 | } |
| 803 | |
| 804 | *open = 0; |
| 805 | *max = 0; |
| 806 | |
| 807 | NumberOfSessions result; |
| 808 | ::ndk::ScopedAStatus status = mPlugin->getNumberOfSessions(&result); |
| 809 | |
| 810 | *open = result.currentSessions; |
| 811 | *max = result.maxSessions; |
| 812 | |
| 813 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 814 | } |
| 815 | |
| 816 | status_t DrmHalAidl::getSecurityLevel(Vector<uint8_t> const& sessionId, |
| 817 | DrmPlugin::SecurityLevel* level) const { |
| 818 | Mutex::Autolock autoLock(mLock); |
| 819 | INIT_CHECK(); |
| 820 | |
| 821 | if (level == NULL) { |
| 822 | return BAD_VALUE; |
| 823 | } |
| 824 | |
| 825 | *level = DrmPlugin::kSecurityLevelUnknown; |
| 826 | |
| 827 | SecurityLevel result; |
| 828 | ::ndk::ScopedAStatus status = mPlugin->getSecurityLevel(toStdVec(sessionId), &result); |
| 829 | |
| 830 | *level = toSecurityLevel(result); |
| 831 | |
| 832 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 833 | } |
| 834 | |
| 835 | status_t DrmHalAidl::getOfflineLicenseKeySetIds(List<Vector<uint8_t>>& keySetIds) const { |
| 836 | Mutex::Autolock autoLock(mLock); |
| 837 | INIT_CHECK(); |
| 838 | |
| 839 | std::vector<KeySetId> result; |
| 840 | ::ndk::ScopedAStatus status = mPlugin->getOfflineLicenseKeySetIds(&result); |
| 841 | |
| 842 | keySetIds = toKeySetIds(result); |
| 843 | |
| 844 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 845 | } |
| 846 | |
| 847 | status_t DrmHalAidl::removeOfflineLicense(Vector<uint8_t> const& keySetId) { |
| 848 | Mutex::Autolock autoLock(mLock); |
| 849 | INIT_CHECK(); |
| 850 | |
| 851 | KeySetId keySetIdAidl; |
| 852 | keySetIdAidl.keySetId = toStdVec(keySetId); |
| 853 | ::ndk::ScopedAStatus status = mPlugin->removeOfflineLicense(keySetIdAidl); |
| 854 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 855 | } |
| 856 | |
| 857 | status_t DrmHalAidl::getOfflineLicenseState(Vector<uint8_t> const& keySetId, |
| 858 | DrmPlugin::OfflineLicenseState* licenseState) const { |
| 859 | Mutex::Autolock autoLock(mLock); |
| 860 | |
| 861 | INIT_CHECK(); |
| 862 | *licenseState = DrmPlugin::kOfflineLicenseStateUnknown; |
| 863 | |
| 864 | KeySetId keySetIdAidl; |
| 865 | keySetIdAidl.keySetId = toStdVec(keySetId); |
| 866 | |
| 867 | OfflineLicenseState result; |
| 868 | ::ndk::ScopedAStatus status = mPlugin->getOfflineLicenseState(keySetIdAidl, &result); |
| 869 | |
| 870 | *licenseState = toOfflineLicenseState(result); |
| 871 | |
| 872 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 873 | } |
| 874 | |
| 875 | status_t DrmHalAidl::getPropertyString(String8 const& name, String8& value) const { |
| 876 | Mutex::Autolock autoLock(mLock); |
| 877 | return getPropertyStringInternal(name, value); |
| 878 | } |
| 879 | |
| 880 | status_t DrmHalAidl::getPropertyStringInternal(String8 const& name, String8& value) const { |
| 881 | // This function is internal to the class and should only be called while |
| 882 | // mLock is already held. |
| 883 | INIT_CHECK(); |
| 884 | |
| 885 | std::string result; |
| 886 | ::ndk::ScopedAStatus status = mPlugin->getPropertyString(toStdString(name), &result); |
| 887 | |
| 888 | value = toString8(result); |
| 889 | |
| 890 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 891 | } |
| 892 | |
| 893 | status_t DrmHalAidl::getPropertyByteArray(String8 const& name, Vector<uint8_t>& value) const { |
| 894 | Mutex::Autolock autoLock(mLock); |
| 895 | return getPropertyByteArrayInternal(name, value); |
| 896 | } |
| 897 | |
| 898 | status_t DrmHalAidl::getPropertyByteArrayInternal(String8 const& name, |
| 899 | Vector<uint8_t>& value) const { |
| 900 | // This function is internal to the class and should only be called while |
| 901 | // mLock is already held. |
| 902 | INIT_CHECK(); |
| 903 | |
| 904 | status_t err = UNKNOWN_ERROR; |
| 905 | |
| 906 | std::vector<uint8_t> result; |
| 907 | ::ndk::ScopedAStatus status = mPlugin->getPropertyByteArray(toStdString(name), &result); |
| 908 | |
| 909 | value = toVector(result); |
| 910 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 911 | if (name == kPropertyDeviceUniqueId) { |
| 912 | mMetrics.mGetDeviceUniqueIdCounter.Increment(err); |
| 913 | } |
| 914 | return err; |
| 915 | } |
| 916 | |
| 917 | status_t DrmHalAidl::setPropertyString(String8 const& name, String8 const& value) const { |
| 918 | Mutex::Autolock autoLock(mLock); |
| 919 | INIT_CHECK(); |
| 920 | |
| 921 | ::ndk::ScopedAStatus status = mPlugin->setPropertyString(toStdString(name), toStdString(value)); |
| 922 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 923 | } |
| 924 | |
| 925 | status_t DrmHalAidl::setPropertyByteArray(String8 const& name, Vector<uint8_t> const& value) const { |
| 926 | Mutex::Autolock autoLock(mLock); |
| 927 | INIT_CHECK(); |
| 928 | |
| 929 | ::ndk::ScopedAStatus status = mPlugin->setPropertyByteArray(toStdString(name), toStdVec(value)); |
| 930 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 931 | } |
| 932 | |
| 933 | status_t DrmHalAidl::getMetrics(const sp<IDrmMetricsConsumer>& consumer) { |
| 934 | if (consumer == nullptr) { |
| 935 | return UNEXPECTED_NULL; |
| 936 | } |
| 937 | consumer->consumeFrameworkMetrics(mMetrics); |
| 938 | |
| 939 | // Append vendor metrics if they are supported. |
| 940 | |
| 941 | String8 vendor; |
| 942 | String8 description; |
| 943 | if (getPropertyStringInternal(String8("vendor"), vendor) != OK || vendor.isEmpty()) { |
| 944 | ALOGE("Get vendor failed or is empty"); |
| 945 | vendor = "NONE"; |
| 946 | } |
| 947 | if (getPropertyStringInternal(String8("description"), description) != OK || |
| 948 | description.isEmpty()) { |
| 949 | ALOGE("Get description failed or is empty."); |
| 950 | description = "NONE"; |
| 951 | } |
| 952 | vendor += "."; |
| 953 | vendor += description; |
| 954 | |
| 955 | hidl_vec<DrmMetricGroupHidl> pluginMetrics; |
| 956 | status_t err = UNKNOWN_ERROR; |
| 957 | |
| 958 | std::vector<DrmMetricGroupAidl> result; |
| 959 | ::ndk::ScopedAStatus status = mPlugin->getMetrics(&result); |
| 960 | |
| 961 | if (status.isOk()) { |
| 962 | pluginMetrics = toDrmMetricGroupHidl(result); |
| 963 | consumer->consumeHidlMetrics(vendor, pluginMetrics); |
| 964 | } |
| 965 | |
| 966 | err = status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 967 | |
| 968 | return err; |
| 969 | } |
| 970 | |
| 971 | status_t DrmHalAidl::setCipherAlgorithm(Vector<uint8_t> const& sessionId, |
| 972 | String8 const& algorithm) { |
| 973 | Mutex::Autolock autoLock(mLock); |
| 974 | INIT_CHECK(); |
| 975 | |
| 976 | DrmSessionManager::Instance()->useSession(sessionId); |
| 977 | |
| 978 | ::ndk::ScopedAStatus status = |
| 979 | mPlugin->setCipherAlgorithm(toStdVec(sessionId), toStdString(algorithm)); |
| 980 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 981 | } |
| 982 | |
| 983 | status_t DrmHalAidl::setMacAlgorithm(Vector<uint8_t> const& sessionId, String8 const& algorithm) { |
| 984 | Mutex::Autolock autoLock(mLock); |
| 985 | INIT_CHECK(); |
| 986 | |
| 987 | DrmSessionManager::Instance()->useSession(sessionId); |
| 988 | |
| 989 | ::ndk::ScopedAStatus status = |
| 990 | mPlugin->setMacAlgorithm(toStdVec(sessionId), toStdString(algorithm)); |
| 991 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 992 | } |
| 993 | |
| 994 | status_t DrmHalAidl::encrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId, |
| 995 | Vector<uint8_t> const& input, Vector<uint8_t> const& iv, |
| 996 | Vector<uint8_t>& output) { |
| 997 | Mutex::Autolock autoLock(mLock); |
| 998 | INIT_CHECK(); |
| 999 | |
| 1000 | DrmSessionManager::Instance()->useSession(sessionId); |
| 1001 | |
| 1002 | std::vector<uint8_t> result; |
| 1003 | ::ndk::ScopedAStatus status = mPlugin->encrypt(toStdVec(sessionId), toStdVec(keyId), |
| 1004 | toStdVec(input), toStdVec(iv), &result); |
| 1005 | |
| 1006 | output = toVector(result); |
| 1007 | |
| 1008 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1009 | } |
| 1010 | |
| 1011 | status_t DrmHalAidl::decrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId, |
| 1012 | Vector<uint8_t> const& input, Vector<uint8_t> const& iv, |
| 1013 | Vector<uint8_t>& output) { |
| 1014 | Mutex::Autolock autoLock(mLock); |
| 1015 | INIT_CHECK(); |
| 1016 | |
| 1017 | DrmSessionManager::Instance()->useSession(sessionId); |
| 1018 | |
| 1019 | std::vector<uint8_t> result; |
| 1020 | ::ndk::ScopedAStatus status = mPlugin->decrypt(toStdVec(sessionId), toStdVec(keyId), |
| 1021 | toStdVec(input), toStdVec(iv), &result); |
| 1022 | |
| 1023 | output = toVector(result); |
| 1024 | |
| 1025 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1026 | } |
| 1027 | |
| 1028 | status_t DrmHalAidl::sign(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId, |
| 1029 | Vector<uint8_t> const& message, Vector<uint8_t>& signature) { |
| 1030 | Mutex::Autolock autoLock(mLock); |
| 1031 | INIT_CHECK(); |
| 1032 | |
| 1033 | DrmSessionManager::Instance()->useSession(sessionId); |
| 1034 | |
| 1035 | std::vector<uint8_t> result; |
| 1036 | ::ndk::ScopedAStatus status = |
| 1037 | mPlugin->sign(toStdVec(sessionId), toStdVec(keyId), toStdVec(message), &result); |
| 1038 | |
| 1039 | signature = toVector(result); |
| 1040 | |
| 1041 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1042 | } |
| 1043 | |
| 1044 | status_t DrmHalAidl::verify(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId, |
| 1045 | Vector<uint8_t> const& message, Vector<uint8_t> const& signature, |
| 1046 | bool& match) { |
| 1047 | Mutex::Autolock autoLock(mLock); |
| 1048 | INIT_CHECK(); |
| 1049 | |
| 1050 | DrmSessionManager::Instance()->useSession(sessionId); |
| 1051 | |
| 1052 | ::ndk::ScopedAStatus status = mPlugin->verify(toStdVec(sessionId), toStdVec(keyId), |
| 1053 | toStdVec(message), toStdVec(signature), &match); |
| 1054 | |
| 1055 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1056 | } |
| 1057 | |
| 1058 | status_t DrmHalAidl::signRSA(Vector<uint8_t> const& sessionId, String8 const& algorithm, |
| 1059 | Vector<uint8_t> const& message, Vector<uint8_t> const& wrappedKey, |
| 1060 | Vector<uint8_t>& signature) { |
| 1061 | Mutex::Autolock autoLock(mLock); |
| 1062 | INIT_CHECK(); |
| 1063 | |
| 1064 | DrmSessionManager::Instance()->useSession(sessionId); |
| 1065 | |
| 1066 | std::vector<uint8_t> result; |
| 1067 | ::ndk::ScopedAStatus status = |
| 1068 | mPlugin->signRSA(toStdVec(sessionId), toStdString(algorithm), toStdVec(message), |
| 1069 | toStdVec(wrappedKey), &result); |
| 1070 | |
| 1071 | signature = toVector(result); |
| 1072 | |
| 1073 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1074 | } |
| 1075 | |
| 1076 | status_t DrmHalAidl::requiresSecureDecoder(const char* mime, bool* required) const { |
| 1077 | Mutex::Autolock autoLock(mLock); |
| 1078 | INIT_CHECK(); |
| 1079 | |
| 1080 | std::string mimeAidl(mime); |
| 1081 | ::ndk::ScopedAStatus status = mPlugin->requiresSecureDecoderDefault(mimeAidl, required); |
| 1082 | if (!status.isOk()) { |
| 1083 | DrmUtils::LOG2BE("requiresSecureDecoder txn failed: %d", status.getServiceSpecificError()); |
| 1084 | return DEAD_OBJECT; |
| 1085 | } |
| 1086 | |
| 1087 | return OK; |
| 1088 | } |
| 1089 | |
| 1090 | status_t DrmHalAidl::requiresSecureDecoder(const char* mime, DrmPlugin::SecurityLevel securityLevel, |
| 1091 | bool* required) const { |
| 1092 | Mutex::Autolock autoLock(mLock); |
| 1093 | INIT_CHECK(); |
| 1094 | |
| 1095 | auto aLevel = toAidlSecurityLevel(securityLevel); |
| 1096 | std::string mimeAidl(mime); |
| 1097 | ::ndk::ScopedAStatus status = mPlugin->requiresSecureDecoder(mimeAidl, aLevel, required); |
| 1098 | if (!status.isOk()) { |
| 1099 | DrmUtils::LOG2BE("requiresSecureDecoder txn failed: %d", status.getServiceSpecificError()); |
| 1100 | return DEAD_OBJECT; |
| 1101 | } |
| 1102 | |
| 1103 | return OK; |
| 1104 | } |
| 1105 | |
| 1106 | status_t DrmHalAidl::setPlaybackId(Vector<uint8_t> const& sessionId, const char* playbackId) { |
| 1107 | Mutex::Autolock autoLock(mLock); |
| 1108 | INIT_CHECK(); |
| 1109 | std::string playbackIdAidl(playbackId); |
| 1110 | ::ndk::ScopedAStatus status = mPlugin->setPlaybackId(toStdVec(sessionId), playbackIdAidl); |
| 1111 | return status.isOk() ? toStatusTAidl(status.getServiceSpecificError()) : DEAD_OBJECT; |
| 1112 | } |
| 1113 | |
| 1114 | status_t DrmHalAidl::getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const { |
| 1115 | Mutex::Autolock autoLock(mLock); |
| 1116 | return DrmUtils::GetLogMessagesAidl<IDrmPluginAidl>(mPlugin, logs); |
| 1117 | } |
| 1118 | |
| 1119 | void DrmHalAidl::closeOpenSessions() { |
| 1120 | Mutex::Autolock autoLock(mLock); |
| 1121 | auto openSessions = mOpenSessions; |
| 1122 | for (size_t i = 0; i < openSessions.size(); i++) { |
| 1123 | mLock.unlock(); |
| 1124 | closeSession(openSessions[i]->mSessionId); |
| 1125 | mLock.lock(); |
| 1126 | } |
| 1127 | mOpenSessions.clear(); |
| 1128 | } |
| 1129 | |
| 1130 | std::string DrmHalAidl::reportPluginMetrics() const { |
| 1131 | Vector<uint8_t> metricsVector; |
| 1132 | String8 vendor; |
| 1133 | String8 description; |
| 1134 | std::string metricsString; |
| 1135 | if (getPropertyStringInternal(String8("vendor"), vendor) == OK && |
| 1136 | getPropertyStringInternal(String8("description"), description) == OK && |
| 1137 | getPropertyByteArrayInternal(String8("metrics"), metricsVector) == OK) { |
| 1138 | metricsString = toBase64StringNoPad(metricsVector.array(), metricsVector.size()); |
| 1139 | status_t res = android::reportDrmPluginMetrics(metricsString, vendor, description, |
| 1140 | mMetrics.GetAppUid()); |
| 1141 | if (res != OK) { |
| 1142 | ALOGE("Metrics were retrieved but could not be reported: %d", res); |
| 1143 | } |
| 1144 | } |
| 1145 | return metricsString; |
| 1146 | } |
| 1147 | |
| 1148 | std::string DrmHalAidl::reportFrameworkMetrics(const std::string& pluginMetrics) const { |
| 1149 | mediametrics_handle_t item(mediametrics_create("mediadrm")); |
| 1150 | mediametrics_setUid(item, mMetrics.GetAppUid()); |
| 1151 | String8 vendor; |
| 1152 | String8 description; |
| 1153 | status_t result = getPropertyStringInternal(String8("vendor"), vendor); |
| 1154 | if (result != OK) { |
| 1155 | ALOGE("Failed to get vendor from drm plugin: %d", result); |
| 1156 | } else { |
| 1157 | mediametrics_setCString(item, "vendor", vendor.c_str()); |
| 1158 | } |
| 1159 | result = getPropertyStringInternal(String8("description"), description); |
| 1160 | if (result != OK) { |
| 1161 | ALOGE("Failed to get description from drm plugin: %d", result); |
| 1162 | } else { |
| 1163 | mediametrics_setCString(item, "description", description.c_str()); |
| 1164 | } |
| 1165 | |
| 1166 | std::string serializedMetrics; |
| 1167 | result = mMetrics.GetSerializedMetrics(&serializedMetrics); |
| 1168 | if (result != OK) { |
| 1169 | ALOGE("Failed to serialize framework metrics: %d", result); |
| 1170 | } |
| 1171 | std::string b64EncodedMetrics = |
| 1172 | toBase64StringNoPad(serializedMetrics.data(), serializedMetrics.size()); |
| 1173 | if (!b64EncodedMetrics.empty()) { |
| 1174 | mediametrics_setCString(item, "serialized_metrics", b64EncodedMetrics.c_str()); |
| 1175 | } |
| 1176 | if (!pluginMetrics.empty()) { |
| 1177 | mediametrics_setCString(item, "plugin_metrics", pluginMetrics.c_str()); |
| 1178 | } |
| 1179 | if (!mediametrics_selfRecord(item)) { |
| 1180 | ALOGE("Failed to self record framework metrics"); |
| 1181 | } |
| 1182 | mediametrics_delete(item); |
| 1183 | return serializedMetrics; |
| 1184 | } |
| 1185 | |
| 1186 | void DrmHalAidl::cleanup() { |
| 1187 | closeOpenSessions(); |
| 1188 | |
| 1189 | Mutex::Autolock autoLock(mLock); |
| 1190 | reportFrameworkMetrics(reportPluginMetrics()); |
| 1191 | |
| 1192 | setListener(NULL); |
| 1193 | mInitCheck = NO_INIT; |
| 1194 | if (mPlugin != NULL) { |
| 1195 | if (!mPlugin->setListener(NULL).isOk()) { |
| 1196 | mInitCheck = DEAD_OBJECT; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | mPlugin.reset(); |
| 1201 | } |
| 1202 | |
| 1203 | status_t DrmHalAidl::destroyPlugin() { |
| 1204 | cleanup(); |
| 1205 | return OK; |
| 1206 | } |
| 1207 | |
| 1208 | ::ndk::ScopedAStatus DrmHalAidl::onEvent(EventTypeAidl eventTypeAidl, |
| 1209 | const std::vector<uint8_t>& sessionId, |
| 1210 | const std::vector<uint8_t>& data) { |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 1211 | return mListener->onEvent(eventTypeAidl, sessionId, data); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | ::ndk::ScopedAStatus DrmHalAidl::onExpirationUpdate(const std::vector<uint8_t>& sessionId, |
| 1215 | int64_t expiryTimeInMS) { |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 1216 | return mListener->onExpirationUpdate(sessionId, expiryTimeInMS); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | ::ndk::ScopedAStatus DrmHalAidl::onKeysChange(const std::vector<uint8_t>& sessionId, |
| 1220 | const std::vector<KeyStatus>& keyStatusListAidl, |
| 1221 | bool hasNewUsableKey) { |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 1222 | return mListener->onKeysChange(sessionId, keyStatusListAidl, hasNewUsableKey); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | ::ndk::ScopedAStatus DrmHalAidl::onSessionLostState(const std::vector<uint8_t>& sessionId) { |
Kyle Zhang | a55209d | 2022-02-03 01:52:46 +0000 | [diff] [blame] | 1226 | return mListener->onSessionLostState(sessionId); |
Kyle Zhang | 6605add | 2022-01-13 17:51:23 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | } // namespace android |