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 #pragma once 18 19 #include <aidl/android/hardware/common/Ashmem.h> 20 #include <aidl/android/hardware/drm/BnDrmPluginListener.h> 21 #include <aidl/android/hardware/drm/ICryptoPlugin.h> 22 #include <aidl/android/hardware/drm/IDrmFactory.h> 23 #include <aidl/android/hardware/drm/IDrmPlugin.h> 24 #include <aidl/android/hardware/drm/IDrmPluginListener.h> 25 #include <aidl/android/hardware/drm/Status.h> 26 #include <android/binder_auto_utils.h> 27 28 #include <gmock/gmock.h> 29 30 #include <array> 31 #include <algorithm> 32 #include <chrono> 33 #include <future> 34 #include <iostream> 35 #include <map> 36 #include <memory> 37 #include <string> 38 #include <utility> 39 #include <vector> 40 41 #include "VtsHalHidlTargetCallbackBase.h" 42 #include "drm_hal_vendor_module_api.h" 43 #include "drm_vts_helper.h" 44 #include "vendor_modules.h" 45 46 using drm_vts::DrmHalTestParam; 47 48 namespace { 49 typedef vector<::aidl::android::hardware::drm::KeyValue> KeyedVector; 50 typedef std::vector<uint8_t> SessionId; 51 } // namespace 52 53 #define EXPECT_OK(ret) EXPECT_TRUE(::aidl::android::hardware::drm::vts::IsOk(ret)) 54 #define EXPECT_TXN(ret) EXPECT_TRUE(ret.isOk() || ret.getExceptionCode() == EX_SERVICE_SPECIFIC) 55 56 namespace aidl { 57 namespace android { 58 namespace hardware { 59 namespace drm { 60 namespace vts { 61 62 using ::testing::AnyOf; 63 using ::testing::Eq; 64 65 ::aidl::android::hardware::drm::Status DrmErr(const ::ndk::ScopedAStatus& ret); 66 std::string HalBaseName(const std::string& fullname); 67 std::string HalFullName(const std::string& iface, const std::string& basename); 68 testing::AssertionResult IsOk(const ::ndk::ScopedAStatus& ret); 69 70 extern const char* kDrmIface; 71 extern const char* kCryptoIface; 72 73 class DrmHalTest : public ::testing::TestWithParam<DrmHalTestParam> { 74 public: 75 static drm_vts::VendorModules* gVendorModules; 76 DrmHalTest(); 77 virtual void SetUp() override; TearDown()78 virtual void TearDown() override {} 79 80 protected: 81 ::aidl::android::hardware::drm::Uuid getAidlUUID(); 82 std::vector<uint8_t> getUUID(); 83 std::vector<uint8_t> getVendorUUID(); GetParamUUID()84 std::array<uint8_t, 16> GetParamUUID() { return GetParam().scheme_; } GetParamService()85 std::string GetParamService() { return GetParam().instance_; } toAidlUuid(const std::vector<uint8_t> & in_uuid)86 ::aidl::android::hardware::drm::Uuid toAidlUuid(const std::vector<uint8_t>& in_uuid) { 87 std::array<uint8_t, 16> a; 88 std::copy_n(in_uuid.begin(), a.size(), a.begin()); 89 return {a}; 90 } 91 92 bool isCryptoSchemeSupported(::aidl::android::hardware::drm::Uuid uuid, 93 ::aidl::android::hardware::drm::SecurityLevel level, 94 std::string mime); 95 void provision(); 96 SessionId openSession(::aidl::android::hardware::drm::SecurityLevel level, 97 ::aidl::android::hardware::drm::Status* err); 98 SessionId openSession(); 99 void closeSession(const SessionId& sessionId); 100 std::vector<uint8_t> loadKeys( 101 const SessionId& sessionId, 102 const ::aidl::android::hardware::drm::KeyType& type = KeyType::STREAMING); 103 std::vector<uint8_t> loadKeys( 104 const SessionId& sessionId, const DrmHalVTSVendorModule_V1::ContentConfiguration&, 105 const ::aidl::android::hardware::drm::KeyType& type = KeyType::STREAMING); 106 std::vector<uint8_t> getKeyRequest(const SessionId& sessionId, 107 const DrmHalVTSVendorModule_V1::ContentConfiguration&, 108 const ::aidl::android::hardware::drm::KeyType& type); 109 std::vector<uint8_t> provideKeyResponse(const SessionId& sessionId, 110 const std::vector<uint8_t>& keyResponse); 111 DrmHalVTSVendorModule_V1::ContentConfiguration getContent( 112 const ::aidl::android::hardware::drm::KeyType& type = KeyType::STREAMING) const; 113 114 KeyedVector toAidlKeyedVector(const std::map<std::string, std::string>& params); 115 std::array<uint8_t, 16> toStdArray(const std::vector<uint8_t>& vec); 116 uint8_t* fillRandom(const ::aidl::android::hardware::drm::SharedBuffer& buf); 117 void getDecryptMemory(size_t size, size_t index, SharedBuffer& buf); 118 119 uint32_t decrypt(::aidl::android::hardware::drm::Mode mode, bool isSecure, 120 const std::array<uint8_t, 16>& keyId, uint8_t* iv, 121 const std::vector<::aidl::android::hardware::drm::SubSample>& subSamples, 122 const ::aidl::android::hardware::drm::Pattern& pattern, 123 const std::vector<uint8_t>& key, 124 ::aidl::android::hardware::drm::Status expectedStatus); 125 void aes_ctr_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv, 126 const std::vector<::aidl::android::hardware::drm::SubSample>& subSamples, 127 const std::vector<uint8_t>& key); 128 void aes_cbc_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv, 129 const std::vector<::aidl::android::hardware::drm::SubSample>& subSamples, 130 const std::vector<uint8_t>& key); 131 132 std::shared_ptr<::aidl::android::hardware::drm::IDrmFactory> drmFactory; 133 std::shared_ptr<::aidl::android::hardware::drm::IDrmPlugin> drmPlugin; 134 std::shared_ptr<::aidl::android::hardware::drm::ICryptoPlugin> cryptoPlugin; 135 136 unique_ptr<DrmHalVTSVendorModule_V1> vendorModule; 137 std::vector<DrmHalVTSVendorModule_V1::ContentConfiguration> contentConfigurations; 138 139 private: 140 std::shared_ptr<::aidl::android::hardware::drm::IDrmPlugin> createDrmPlugin(); 141 std::shared_ptr<::aidl::android::hardware::drm::ICryptoPlugin> createCryptoPlugin(); 142 }; 143 144 class DrmHalClearkeyTest : public DrmHalTest { 145 public: SetUp()146 virtual void SetUp() override { 147 DrmHalTest::SetUp(); 148 auto kClearKeyUUID = toAidlUuid({0xE2, 0x71, 0x9D, 0x58, 0xA9, 0x85, 0xB3, 0xC9, 149 0x78, 0x1A, 0xB0, 0x30, 0xAF, 0x78, 0xD3, 0x0E}); 150 static const std::string kMimeType = "video/mp4"; 151 static constexpr ::aidl::android::hardware::drm::SecurityLevel kSecurityLevel = 152 ::aidl::android::hardware::drm::SecurityLevel::SW_SECURE_CRYPTO; 153 154 if (!isCryptoSchemeSupported(kClearKeyUUID, kSecurityLevel, kMimeType)) { 155 GTEST_SKIP() << "ClearKey not supported by " << GetParamService(); 156 } 157 } TearDown()158 virtual void TearDown() override {} 159 void decryptWithInvalidKeys( 160 std::vector<uint8_t>& invalidResponse, std::vector<uint8_t>& iv, 161 const ::aidl::android::hardware::drm::Pattern& noPattern, 162 const std::vector<::aidl::android::hardware::drm::SubSample>& subSamples); 163 }; 164 165 /** 166 * Event Handling tests 167 */ 168 extern const char* kCallbackLostState; 169 extern const char* kCallbackKeysChange; 170 171 struct ListenerArgs { 172 EventType eventType; 173 SessionId sessionId; 174 int64_t expiryTimeInMS; 175 std::vector<uint8_t> data; 176 std::vector<KeyStatus> keyStatusList; 177 bool hasNewUsableKey; 178 }; 179 180 class DrmHalPluginListener : public BnDrmPluginListener { 181 public: DrmHalPluginListener()182 DrmHalPluginListener() {} ~DrmHalPluginListener()183 virtual ~DrmHalPluginListener() {} 184 185 virtual ::ndk::ScopedAStatus onEvent( 186 ::aidl::android::hardware::drm::EventType in_eventType, 187 const std::vector<uint8_t>& in_sessionId, 188 const std::vector<uint8_t>& in_data) override; 189 190 virtual ::ndk::ScopedAStatus onExpirationUpdate( 191 const std::vector<uint8_t>& in_sessionId, 192 int64_t in_expiryTimeInMS) override; 193 194 virtual ::ndk::ScopedAStatus onSessionLostState( 195 const std::vector<uint8_t>& in_sessionId) override; 196 197 virtual ::ndk::ScopedAStatus onKeysChange( 198 const std::vector<uint8_t>& in_sessionId, 199 const std::vector<::aidl::android::hardware::drm::KeyStatus>& in_keyStatusList, 200 bool in_hasNewUsableKey) override; 201 202 ListenerArgs getEventArgs(); 203 ListenerArgs getExpirationUpdateArgs(); 204 ListenerArgs getSessionLostStateArgs(); 205 ListenerArgs getKeysChangeArgs(); 206 207 private: 208 ListenerArgs getListenerArgs(std::promise<ListenerArgs>& promise); 209 std::promise<ListenerArgs> eventPromise, expirationUpdatePromise, 210 sessionLostStatePromise, keysChangePromise; 211 }; 212 213 } // namespace vts 214 } // namespace drm 215 } // namespace hardware 216 } // namespace android 217 } // namespace aidl 218