1 /*
2  * Copyright (C) 2019 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 #ifndef DRM_HAL_COMMON_H
18 #define DRM_HAL_COMMON_H
19 
20 #include <android/hardware/drm/1.2/ICryptoFactory.h>
21 #include <android/hardware/drm/1.2/ICryptoPlugin.h>
22 #include <android/hardware/drm/1.2/IDrmFactory.h>
23 #include <android/hardware/drm/1.2/IDrmPlugin.h>
24 #include <android/hardware/drm/1.2/IDrmPluginListener.h>
25 #include <android/hardware/drm/1.2/types.h>
26 #include <hidl/HidlSupport.h>
27 
28 #include <array>
29 #include <chrono>
30 # include <iostream>
31 #include <map>
32 #include <memory>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 #include "drm_hal_vendor_module_api.h"
38 #include "drm_vts_helper.h"
39 #include "vendor_modules.h"
40 #include "VtsHalHidlTargetCallbackBase.h"
41 
42 using ::android::hardware::drm::V1_0::EventType;
43 using ::android::hardware::drm::V1_0::KeyedVector;
44 using ::android::hardware::drm::V1_0::KeyType;
45 using ::android::hardware::drm::V1_0::Mode;
46 using ::android::hardware::drm::V1_0::Pattern;
47 using ::android::hardware::drm::V1_0::SessionId;
48 using ::android::hardware::drm::V1_0::SubSample;
49 using ::android::hardware::drm::V1_1::SecurityLevel;
50 
51 using KeyStatusV1_0 = ::android::hardware::drm::V1_0::KeyStatus;
52 using StatusV1_0 = ::android::hardware::drm::V1_0::Status;
53 using StatusV1_2 = ::android::hardware::drm::V1_2::Status;
54 
55 using ::android::hardware::hidl_array;
56 using ::android::hardware::hidl_vec;
57 using ::android::hardware::Return;
58 using ::android::hardware::Void;
59 
60 using ::android::hidl::memory::V1_0::IMemory;
61 using ::android::sp;
62 
63 using drm_vts::DrmHalTestParam;
64 
65 using std::array;
66 using std::map;
67 using std::pair;
68 using std::string;
69 using std::unique_ptr;
70 using std::vector;
71 
72 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
73 
74 namespace android {
75 namespace hardware {
76 namespace drm {
77 namespace V1_2 {
78 namespace vts {
79 
80 class DrmHalTest : public ::testing::TestWithParam<DrmHalTestParam> {
81    public:
82     static drm_vts::VendorModules* gVendorModules;
83     DrmHalTest();
84     virtual void SetUp() override;
TearDown()85     virtual void TearDown() override {}
86 
87    protected:
88     hidl_array<uint8_t, 16> getUUID();
89     hidl_array<uint8_t, 16> getVendorUUID();
GetParamUUID()90     hidl_array<uint8_t, 16> GetParamUUID() { return GetParam().scheme_; }
GetParamService()91     string GetParamService() { return GetParam().instance_; }
92     void provision();
93     SessionId openSession(SecurityLevel level, StatusV1_0* err);
94     SessionId openSession();
95     void closeSession(const SessionId& sessionId);
96     hidl_vec<uint8_t> loadKeys(const SessionId& sessionId,
97                                const KeyType& type = KeyType::STREAMING);
98     hidl_vec<uint8_t> loadKeys(const SessionId& sessionId,
99                                const DrmHalVTSVendorModule_V1::ContentConfiguration&,
100                                const KeyType& type = KeyType::STREAMING);
101     hidl_vec<uint8_t> getKeyRequest(const SessionId& sessionId,
102                                const DrmHalVTSVendorModule_V1::ContentConfiguration&,
103                                const KeyType& type);
104     hidl_vec<uint8_t> provideKeyResponse(const SessionId& sessionId,
105                                const hidl_vec<uint8_t>& keyResponse);
106     DrmHalVTSVendorModule_V1::ContentConfiguration getContent(
107             const KeyType& type = KeyType::STREAMING) const;
108 
109     KeyedVector toHidlKeyedVector(const map<string, string>& params);
110     hidl_array<uint8_t, 16> toHidlArray(const vector<uint8_t>& vec);
111 
112     void fillRandom(const sp<IMemory>& memory);
113     sp<IMemory> getDecryptMemory(size_t size, size_t index);
114     uint32_t decrypt(Mode mode, bool isSecure,
115             const hidl_array<uint8_t, 16>& keyId, uint8_t* iv,
116             const hidl_vec<SubSample>& subSamples, const Pattern& pattern,
117             const vector<uint8_t>& key, StatusV1_2 expectedStatus);
118     void aes_ctr_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv,
119             const hidl_vec<SubSample>& subSamples, const vector<uint8_t>& key);
120     void aes_cbc_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv,
121             const hidl_vec<SubSample>& subSamples, const vector<uint8_t>& key);
122 
123     sp<IDrmFactory> drmFactory;
124     sp<ICryptoFactory> cryptoFactory;
125     sp<IDrmPlugin> drmPlugin;
126     sp<ICryptoPlugin> cryptoPlugin;
127     unique_ptr<DrmHalVTSVendorModule_V1> vendorModule;
128     vector<DrmHalVTSVendorModule_V1::ContentConfiguration> contentConfigurations;
129 
130   private:
131     sp<IDrmPlugin> createDrmPlugin();
132     sp<ICryptoPlugin> createCryptoPlugin();
133 
134 };
135 
136 class DrmHalClearkeyTestV1_2 : public DrmHalTest {
137    public:
SetUp()138      virtual void SetUp() override {
139          DrmHalTest::SetUp();
140          const uint8_t kClearKeyUUID[16] = {
141              0xE2, 0x71, 0x9D, 0x58, 0xA9, 0x85, 0xB3, 0xC9,
142              0x78, 0x1A, 0xB0, 0x30, 0xAF, 0x78, 0xD3, 0x0E};
143          if (!drmFactory->isCryptoSchemeSupported(kClearKeyUUID)) {
144              GTEST_SKIP() << "ClearKey not supported by " << GetParamService();
145          }
146      }
TearDown()147     virtual void TearDown() override {}
148     void decryptWithInvalidKeys(hidl_vec<uint8_t>& invalidResponse,
149             vector<uint8_t>& iv, const Pattern& noPattern, const vector<SubSample>& subSamples);
150 };
151 
152 /**
153  *  Event Handling tests
154  */
155 extern const char *kCallbackLostState;
156 extern const char *kCallbackKeysChange;
157 
158 struct ListenerEventArgs {
159     SessionId sessionId;
160     hidl_vec<KeyStatus> keyStatusList;
161     bool hasNewUsableKey;
162 };
163 
164 class DrmHalPluginListener
165     : public ::testing::VtsHalHidlTargetCallbackBase<ListenerEventArgs>,
166       public IDrmPluginListener {
167 public:
DrmHalPluginListener()168     DrmHalPluginListener() {
169         SetWaitTimeoutDefault(std::chrono::milliseconds(500));
170     }
~DrmHalPluginListener()171     virtual ~DrmHalPluginListener() {}
172 
sendEvent(EventType,const hidl_vec<uint8_t> &,const hidl_vec<uint8_t> &)173     virtual Return<void> sendEvent(EventType, const hidl_vec<uint8_t>&,
174             const hidl_vec<uint8_t>& ) override { return Void(); }
175 
sendExpirationUpdate(const hidl_vec<uint8_t> &,int64_t)176     virtual Return<void> sendExpirationUpdate(const hidl_vec<uint8_t>&,
177             int64_t) override { return Void(); }
178 
sendKeysChange(const hidl_vec<uint8_t> &,const hidl_vec<KeyStatusV1_0> &,bool)179     virtual Return<void> sendKeysChange(const hidl_vec<uint8_t>&,
180             const hidl_vec<KeyStatusV1_0>&, bool) override { return Void(); }
181 
182     virtual Return<void> sendSessionLostState(const hidl_vec<uint8_t>& sessionId) override;
183 
184     virtual Return<void> sendKeysChange_1_2(const hidl_vec<uint8_t>&,
185             const hidl_vec<KeyStatus>&, bool) override;
186 
187 };
188 
189 }  // namespace vts
190 }  // namespace V1_2
191 }  // namespace drm
192 }  // namespace hardware
193 }  // namespace android
194 
195 #endif  // DRM_HAL_COMMON_H
196