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