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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "DrmUtils"
19
20 #include <android/hardware/drm/1.0/ICryptoFactory.h>
21 #include <android/hardware/drm/1.0/ICryptoPlugin.h>
22 #include <android/hardware/drm/1.0/IDrmFactory.h>
23 #include <android/hardware/drm/1.0/IDrmPlugin.h>
24 #include <android/hardware/drm/1.1/ICryptoFactory.h>
25 #include <android/hardware/drm/1.1/IDrmFactory.h>
26 #include <android/hardware/drm/1.2/ICryptoFactory.h>
27 #include <android/hardware/drm/1.2/IDrmFactory.h>
28 #include <android/hardware/drm/1.3/ICryptoFactory.h>
29 #include <android/hardware/drm/1.3/IDrmFactory.h>
30 #include <android/hidl/manager/1.2/IServiceManager.h>
31 #include <hidl/HidlSupport.h>
32
33 #include <utils/Errors.h>
34 #include <utils/Log.h>
35 #include <utils/String16.h>
36 #include <cutils/properties.h>
37
38 #include <mediadrm/CryptoHal.h>
39 #include <mediadrm/DrmHal.h>
40 #include <mediadrm/DrmUtils.h>
41 #include <mediadrm/ICrypto.h>
42 #include <mediadrm/IDrm.h>
43
44 using HServiceManager = ::android::hidl::manager::V1_2::IServiceManager;
45 using ::android::hardware::hidl_array;
46 using ::android::hardware::hidl_string;
47 using ::android::hardware::hidl_vec;
48 using namespace ::android::hardware::drm;
49
50 namespace android {
51 namespace DrmUtils {
52
53 namespace {
54
55 template<typename Hal>
MakeObject(status_t * pstatus)56 Hal *MakeObject(status_t *pstatus) {
57 status_t err = OK;
58 status_t &status = pstatus ? *pstatus : err;
59 auto obj = new Hal();
60 status = obj->initCheck();
61 if (status != OK && status != NO_INIT) {
62 return NULL;
63 }
64 return obj;
65 }
66
67 template <typename Hal, typename V>
MakeHidlFactories(const uint8_t uuid[16],V & factories)68 void MakeHidlFactories(const uint8_t uuid[16], V &factories) {
69 sp<HServiceManager> serviceManager = HServiceManager::getService();
70 if (serviceManager == nullptr) {
71 ALOGE("Failed to get service manager");
72 exit(-1);
73 }
74
75 serviceManager->listManifestByInterface(Hal::descriptor, [&](const hidl_vec<hidl_string> ®istered) {
76 for (const auto &instance : registered) {
77 auto factory = Hal::getService(instance);
78 if (factory != nullptr) {
79 ALOGI("found %s %s", Hal::descriptor, instance.c_str());
80 if (!uuid || factory->isCryptoSchemeSupported(uuid)) {
81 factories.push_back(factory);
82 }
83 }
84 }
85 });
86 }
87
toHidlVec(const void * ptr,size_t size)88 hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
89 hidl_vec<uint8_t> vec(size);
90 if (ptr != nullptr) {
91 memcpy(vec.data(), ptr, size);
92 }
93 return vec;
94 }
95
toHidlArray16(const uint8_t * ptr)96 hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
97 if (ptr == nullptr) {
98 return hidl_array<uint8_t, 16>();
99 }
100 return hidl_array<uint8_t, 16>(ptr);
101 }
102
MakeDrmPlugin(const sp<::V1_0::IDrmFactory> & factory,const uint8_t uuid[16],const char * appPackageName)103 sp<::V1_0::IDrmPlugin> MakeDrmPlugin(const sp<::V1_0::IDrmFactory> &factory,
104 const uint8_t uuid[16], const char *appPackageName) {
105 sp<::V1_0::IDrmPlugin> plugin;
106 factory->createPlugin(toHidlArray16(uuid), hidl_string(appPackageName),
107 [&](::V1_0::Status status, const sp<::V1_0::IDrmPlugin> &hPlugin) {
108 if (status != ::V1_0::Status::OK) {
109 return;
110 }
111 plugin = hPlugin;
112 });
113 return plugin;
114 }
115
MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> & factory,const uint8_t uuid[16],const void * initData,size_t initDataSize)116 sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> &factory,
117 const uint8_t uuid[16], const void *initData,
118 size_t initDataSize) {
119 sp<::V1_0::ICryptoPlugin> plugin;
120 factory->createPlugin(toHidlArray16(uuid), toHidlVec(initData, initDataSize),
121 [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin> &hPlugin) {
122 if (status != ::V1_0::Status::OK) {
123 return;
124 }
125 plugin = hPlugin;
126 });
127 return plugin;
128 }
129
130 } // namespace
131
UseDrmService()132 bool UseDrmService() {
133 return property_get_bool("mediadrm.use_mediadrmserver", true);
134 }
135
MakeDrm(status_t * pstatus)136 sp<IDrm> MakeDrm(status_t *pstatus) {
137 return MakeObject<DrmHal>(pstatus);
138 }
139
MakeCrypto(status_t * pstatus)140 sp<ICrypto> MakeCrypto(status_t *pstatus) {
141 return MakeObject<CryptoHal>(pstatus);
142 }
143
MakeDrmFactories(const uint8_t uuid[16])144 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16]) {
145 std::vector<sp<::V1_0::IDrmFactory>> drmFactories;
146 MakeHidlFactories<::V1_0::IDrmFactory>(uuid, drmFactories);
147 MakeHidlFactories<::V1_1::IDrmFactory>(uuid, drmFactories);
148 MakeHidlFactories<::V1_2::IDrmFactory>(uuid, drmFactories);
149 MakeHidlFactories<::V1_3::IDrmFactory>(uuid, drmFactories);
150 return drmFactories;
151 }
152
MakeDrmPlugins(const uint8_t uuid[16],const char * appPackageName)153 std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
154 const char *appPackageName) {
155 std::vector<sp<::V1_0::IDrmPlugin>> plugins;
156 for (const auto &factory : MakeDrmFactories(uuid)) {
157 plugins.push_back(MakeDrmPlugin(factory, uuid, appPackageName));
158 }
159 return plugins;
160 }
161
MakeCryptoFactories(const uint8_t uuid[16])162 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
163 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
164 MakeHidlFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
165 MakeHidlFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
166 MakeHidlFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
167 MakeHidlFactories<::V1_3::ICryptoFactory>(uuid, cryptoFactories);
168 return cryptoFactories;
169 }
170
MakeCryptoPlugins(const uint8_t uuid[16],const void * initData,size_t initDataSize)171 std::vector<sp<ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], const void *initData,
172 size_t initDataSize) {
173 std::vector<sp<ICryptoPlugin>> plugins;
174 for (const auto &factory : MakeCryptoFactories(uuid)) {
175 plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
176 }
177 return plugins;
178 }
179
180 } // namespace DrmUtils
181 } // namespace android
182