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/binder_manager.h>
21 #include <android/hardware/drm/1.0/ICryptoFactory.h>
22 #include <android/hardware/drm/1.0/ICryptoPlugin.h>
23 #include <android/hardware/drm/1.0/IDrmFactory.h>
24 #include <android/hardware/drm/1.0/IDrmPlugin.h>
25 #include <android/hardware/drm/1.1/ICryptoFactory.h>
26 #include <android/hardware/drm/1.1/IDrmFactory.h>
27 #include <android/hardware/drm/1.2/ICryptoFactory.h>
28 #include <android/hardware/drm/1.2/IDrmFactory.h>
29 #include <android/hardware/drm/1.3/ICryptoFactory.h>
30 #include <android/hardware/drm/1.3/IDrmFactory.h>
31 #include <android/hardware/drm/1.4/ICryptoFactory.h>
32 #include <android/hardware/drm/1.4/IDrmFactory.h>
33 #include <android/hidl/manager/1.2/IServiceManager.h>
34 #include <hidl/HidlSupport.h>
35
36 #include <cutils/properties.h>
37 #include <utils/Errors.h>
38 #include <utils/Log.h>
39 #include <utils/String16.h>
40
41 #include <mediadrm/CryptoHal.h>
42 #include <mediadrm/DrmHal.h>
43 #include <mediadrm/DrmUtils.h>
44 #include <mediadrm/ICrypto.h>
45 #include <mediadrm/IDrm.h>
46
47 #include <map>
48 #include <string>
49
50 using HServiceManager = ::android::hidl::manager::V1_2::IServiceManager;
51 using ::android::hardware::hidl_array;
52 using ::android::hardware::hidl_string;
53 using ::android::hardware::hidl_vec;
54 using namespace ::android::hardware::drm;
55
56 namespace android {
57 namespace DrmUtils {
58
59 namespace {
60
61 template <typename Hal>
MakeObject(status_t * pstatus)62 Hal* MakeObject(status_t* pstatus) {
63 status_t err = OK;
64 status_t& status = pstatus ? *pstatus : err;
65 auto obj = new Hal();
66 status = obj->initCheck();
67 if (status != OK && status != NO_INIT) {
68 return NULL;
69 }
70 return obj;
71 }
72
73 template <typename Hal, typename V, typename M>
MakeHidlFactories(const uint8_t uuid[16],V & factories,M & instances)74 void MakeHidlFactories(const uint8_t uuid[16], V& factories, M& instances) {
75 sp<HServiceManager> serviceManager = HServiceManager::getService();
76 if (serviceManager == nullptr) {
77 LOG2BE("Failed to get service manager");
78 return;
79 }
80
81 serviceManager->listManifestByInterface(
82 Hal::descriptor, [&](const hidl_vec<hidl_string>& registered) {
83 for (const auto& instance : registered) {
84 auto factory = Hal::getService(instance);
85 if (factory != nullptr) {
86 instances[instance.c_str()] = Hal::descriptor;
87 if (!uuid) {
88 factories.push_back(factory);
89 continue;
90 }
91 auto supported = factory->isCryptoSchemeSupported(uuid);
92 if (!supported.isOk()) {
93 LOG2BE(uuid, "isCryptoSchemeSupported txn failed: %s",
94 supported.description().c_str());
95 continue;
96 }
97 if (supported) {
98 factories.push_back(factory);
99 }
100 }
101 }
102 });
103 }
104
105 template <typename Hal, typename V>
MakeHidlFactories(const uint8_t uuid[16],V & factories)106 void MakeHidlFactories(const uint8_t uuid[16], V& factories) {
107 std::map<std::string, std::string> instances;
108 MakeHidlFactories<Hal>(uuid, factories, instances);
109 }
110
toHidlVec(const void * ptr,size_t size)111 hidl_vec<uint8_t> toHidlVec(const void* ptr, size_t size) {
112 hidl_vec<uint8_t> vec(size);
113 if (ptr != nullptr) {
114 memcpy(vec.data(), ptr, size);
115 }
116 return vec;
117 }
118
toHidlArray16(const uint8_t * ptr)119 hidl_array<uint8_t, 16> toHidlArray16(const uint8_t* ptr) {
120 if (ptr == nullptr) {
121 return hidl_array<uint8_t, 16>();
122 }
123 return hidl_array<uint8_t, 16>(ptr);
124 }
125
MakeDrmPlugin(const sp<::V1_0::IDrmFactory> & factory,const uint8_t uuid[16],const char * appPackageName)126 sp<::V1_0::IDrmPlugin> MakeDrmPlugin(const sp<::V1_0::IDrmFactory>& factory, const uint8_t uuid[16],
127 const char* appPackageName) {
128 sp<::V1_0::IDrmPlugin> plugin;
129 auto err = factory->createPlugin(
130 toHidlArray16(uuid), hidl_string(appPackageName),
131 [&](::V1_0::Status status, const sp<::V1_0::IDrmPlugin>& hPlugin) {
132 if (status != ::V1_0::Status::OK) {
133 LOG2BE(uuid, "MakeDrmPlugin failed: %d", status);
134 return;
135 }
136 plugin = hPlugin;
137 });
138 if (err.isOk()) {
139 return plugin;
140 } else {
141 LOG2BE(uuid, "MakeDrmPlugin txn failed: %s", err.description().c_str());
142 return nullptr;
143 }
144 }
145
MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> & factory,const uint8_t uuid[16],const void * initData,size_t initDataSize)146 sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory>& factory,
147 const uint8_t uuid[16], const void* initData,
148 size_t initDataSize) {
149 sp<::V1_0::ICryptoPlugin> plugin;
150 auto err = factory->createPlugin(
151 toHidlArray16(uuid), toHidlVec(initData, initDataSize),
152 [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin>& hPlugin) {
153 if (status != ::V1_0::Status::OK) {
154 LOG2BE(uuid, "MakeCryptoPlugin failed: %d", status);
155 return;
156 }
157 plugin = hPlugin;
158 });
159 if (err.isOk()) {
160 return plugin;
161 } else {
162 LOG2BE(uuid, "MakeCryptoPlugin txn failed: %s", err.description().c_str());
163 return nullptr;
164 }
165 }
166
167 } // namespace
168
UseDrmService()169 bool UseDrmService() {
170 return property_get_bool("mediadrm.use_mediadrmserver", true);
171 }
172
makeDrmFactoriesAidl()173 std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl() {
174 std::vector<std::shared_ptr<IDrmFactoryAidl>> factories;
175 AServiceManager_forEachDeclaredInstance(
176 IDrmFactoryAidl::descriptor, static_cast<void*>(&factories),
177 [](const char* instance, void* context) {
178 auto fullName = std::string(IDrmFactoryAidl::descriptor) + "/" + std::string(instance);
179 auto factory = IDrmFactoryAidl::fromBinder(
180 ::ndk::SpAIBinder(AServiceManager_waitForService(fullName.c_str())));
181 if (factory == nullptr) {
182 ALOGE("not found IDrmFactory. Instance name:[%s]", fullName.c_str());
183 return;
184 }
185
186 ALOGI("found IDrmFactory. Instance name:[%s]", fullName.c_str());
187 static_cast<std::vector<std::shared_ptr<IDrmFactoryAidl>>*>(context)->emplace_back(
188 factory);
189 });
190
191 return factories;
192 }
193
MakeDrm(status_t * pstatus)194 sp<IDrm> MakeDrm(status_t* pstatus) {
195 return MakeObject<DrmHal>(pstatus);
196 }
197
MakeCrypto(status_t * pstatus)198 sp<ICrypto> MakeCrypto(status_t* pstatus) {
199 return MakeObject<CryptoHal>(pstatus);
200 }
201
MakeDrmFactories(const uint8_t uuid[16])202 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16]) {
203 std::vector<sp<::V1_0::IDrmFactory>> drmFactories;
204 std::map<std::string, std::string> instances;
205 MakeHidlFactories<::V1_0::IDrmFactory>(uuid, drmFactories, instances);
206 MakeHidlFactories<::V1_1::IDrmFactory>(uuid, drmFactories, instances);
207 MakeHidlFactories<::V1_2::IDrmFactory>(uuid, drmFactories, instances);
208 MakeHidlFactories<::V1_3::IDrmFactory>(uuid, drmFactories, instances);
209 MakeHidlFactories<::V1_4::IDrmFactory>(uuid, drmFactories, instances);
210 for (auto const& entry : instances) {
211 LOG2BI("found instance=%s version=%s", entry.first.c_str(), entry.second.c_str());
212 }
213 return drmFactories;
214 }
215
MakeDrmPlugins(const uint8_t uuid[16],const char * appPackageName)216 std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
217 const char* appPackageName) {
218 std::vector<sp<::V1_0::IDrmPlugin>> plugins;
219 for (const auto& factory : MakeDrmFactories(uuid)) {
220 plugins.push_back(MakeDrmPlugin(factory, uuid, appPackageName));
221 }
222 return plugins;
223 }
224
MakeCryptoFactories(const uint8_t uuid[16])225 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
226 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
227 MakeHidlFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
228 MakeHidlFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
229 MakeHidlFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
230 MakeHidlFactories<::V1_3::ICryptoFactory>(uuid, cryptoFactories);
231 MakeHidlFactories<::V1_4::ICryptoFactory>(uuid, cryptoFactories);
232 return cryptoFactories;
233 }
234
MakeCryptoPlugins(const uint8_t uuid[16],const void * initData,size_t initDataSize)235 std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
236 const void* initData,
237 size_t initDataSize) {
238 std::vector<sp<::V1_0::ICryptoPlugin>> plugins;
239 for (const auto& factory : MakeCryptoFactories(uuid)) {
240 plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
241 }
242 return plugins;
243 }
244
toStatusT_1_4(::V1_4::Status status)245 status_t toStatusT_1_4(::V1_4::Status status) {
246 switch (status) {
247 case ::V1_4::Status::OK:
248 return OK;
249 case ::V1_4::Status::BAD_VALUE:
250 return BAD_VALUE;
251 case ::V1_4::Status::ERROR_DRM_CANNOT_HANDLE:
252 return ERROR_DRM_CANNOT_HANDLE;
253 case ::V1_4::Status::ERROR_DRM_DECRYPT:
254 return ERROR_DRM_DECRYPT;
255 case ::V1_4::Status::ERROR_DRM_DEVICE_REVOKED:
256 return ERROR_DRM_DEVICE_REVOKED;
257 case ::V1_4::Status::ERROR_DRM_FRAME_TOO_LARGE:
258 return ERROR_DRM_FRAME_TOO_LARGE;
259 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
260 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
261 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_SECURITY:
262 return ERROR_DRM_INSUFFICIENT_SECURITY;
263 case ::V1_4::Status::ERROR_DRM_INVALID_STATE:
264 return ERROR_DRM_INVALID_STATE;
265 case ::V1_4::Status::ERROR_DRM_LICENSE_EXPIRED:
266 return ERROR_DRM_LICENSE_EXPIRED;
267 case ::V1_4::Status::ERROR_DRM_NO_LICENSE:
268 return ERROR_DRM_NO_LICENSE;
269 case ::V1_4::Status::ERROR_DRM_NOT_PROVISIONED:
270 return ERROR_DRM_NOT_PROVISIONED;
271 case ::V1_4::Status::ERROR_DRM_RESOURCE_BUSY:
272 return ERROR_DRM_RESOURCE_BUSY;
273 case ::V1_4::Status::ERROR_DRM_RESOURCE_CONTENTION:
274 return ERROR_DRM_RESOURCE_CONTENTION;
275 case ::V1_4::Status::ERROR_DRM_SESSION_LOST_STATE:
276 return ERROR_DRM_SESSION_LOST_STATE;
277 case ::V1_4::Status::ERROR_DRM_SESSION_NOT_OPENED:
278 return ERROR_DRM_SESSION_NOT_OPENED;
279
280 // New in S / drm@1.4:
281 case ::V1_4::Status::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
282 return ERROR_DRM_ZERO_SUBSAMPLES;
283 case ::V1_4::Status::CRYPTO_LIBRARY_ERROR:
284 return ERROR_DRM_CRYPTO_LIBRARY;
285 case ::V1_4::Status::GENERAL_OEM_ERROR:
286 return ERROR_DRM_GENERIC_OEM;
287 case ::V1_4::Status::GENERAL_PLUGIN_ERROR:
288 return ERROR_DRM_GENERIC_PLUGIN;
289 case ::V1_4::Status::INIT_DATA_INVALID:
290 return ERROR_DRM_INIT_DATA;
291 case ::V1_4::Status::KEY_NOT_LOADED:
292 return ERROR_DRM_KEY_NOT_LOADED;
293 case ::V1_4::Status::LICENSE_PARSE_ERROR:
294 return ERROR_DRM_LICENSE_PARSE;
295 case ::V1_4::Status::LICENSE_POLICY_ERROR:
296 return ERROR_DRM_LICENSE_POLICY;
297 case ::V1_4::Status::LICENSE_RELEASE_ERROR:
298 return ERROR_DRM_LICENSE_RELEASE;
299 case ::V1_4::Status::LICENSE_REQUEST_REJECTED:
300 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
301 case ::V1_4::Status::LICENSE_RESTORE_ERROR:
302 return ERROR_DRM_LICENSE_RESTORE;
303 case ::V1_4::Status::LICENSE_STATE_ERROR:
304 return ERROR_DRM_LICENSE_STATE;
305 case ::V1_4::Status::MALFORMED_CERTIFICATE:
306 return ERROR_DRM_CERTIFICATE_MALFORMED;
307 case ::V1_4::Status::MEDIA_FRAMEWORK_ERROR:
308 return ERROR_DRM_MEDIA_FRAMEWORK;
309 case ::V1_4::Status::MISSING_CERTIFICATE:
310 return ERROR_DRM_CERTIFICATE_MISSING;
311 case ::V1_4::Status::PROVISIONING_CERTIFICATE_ERROR:
312 return ERROR_DRM_PROVISIONING_CERTIFICATE;
313 case ::V1_4::Status::PROVISIONING_CONFIGURATION_ERROR:
314 return ERROR_DRM_PROVISIONING_CONFIG;
315 case ::V1_4::Status::PROVISIONING_PARSE_ERROR:
316 return ERROR_DRM_PROVISIONING_PARSE;
317 case ::V1_4::Status::PROVISIONING_REQUEST_REJECTED:
318 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
319 case ::V1_4::Status::RETRYABLE_PROVISIONING_ERROR:
320 return ERROR_DRM_PROVISIONING_RETRY;
321 case ::V1_4::Status::SECURE_STOP_RELEASE_ERROR:
322 return ERROR_DRM_SECURE_STOP_RELEASE;
323 case ::V1_4::Status::STORAGE_READ_FAILURE:
324 return ERROR_DRM_STORAGE_READ;
325 case ::V1_4::Status::STORAGE_WRITE_FAILURE:
326 return ERROR_DRM_STORAGE_WRITE;
327
328 case ::V1_4::Status::ERROR_DRM_UNKNOWN:
329 default:
330 return ERROR_DRM_UNKNOWN;
331 }
332 return ERROR_DRM_UNKNOWN;
333 }
334
335 namespace {
logPriorityToChar(::V1_4::LogPriority priority)336 char logPriorityToChar(::V1_4::LogPriority priority) {
337 char p = 'U';
338 switch (priority) {
339 case ::V1_4::LogPriority::VERBOSE:
340 p = 'V';
341 break;
342 case ::V1_4::LogPriority::DEBUG:
343 p = 'D';
344 break;
345 case ::V1_4::LogPriority::INFO:
346 p = 'I';
347 break;
348 case ::V1_4::LogPriority::WARN:
349 p = 'W';
350 break;
351 case ::V1_4::LogPriority::ERROR:
352 p = 'E';
353 break;
354 case ::V1_4::LogPriority::FATAL:
355 p = 'F';
356 break;
357 default:
358 p = 'U';
359 break;
360 }
361 return p;
362 }
363 } // namespace
364
GetExceptionMessage(status_t err,const char * msg,const Vector<::V1_4::LogMessage> & logs)365 std::string GetExceptionMessage(status_t err, const char* msg,
366 const Vector<::V1_4::LogMessage>& logs) {
367 std::string ruler("==============================");
368 std::string header("Beginning of DRM Plugin Log");
369 std::string footer("End of DRM Plugin Log");
370 String8 msg8;
371 if (msg) {
372 msg8 += msg;
373 msg8 += ": ";
374 }
375 auto errStr = StrCryptoError(err);
376 msg8 += errStr.c_str();
377 msg8 += String8::format("\n%s %s %s", ruler.c_str(), header.c_str(), ruler.c_str());
378
379 for (auto log : logs) {
380 time_t seconds = log.timeMs / 1000;
381 int ms = log.timeMs % 1000;
382 char buf[64] = {0};
383 std::string timeStr = "00-00 00:00:00";
384 if (strftime(buf, sizeof buf, "%m-%d %H:%M:%S", std::localtime(&seconds))) {
385 timeStr = buf;
386 }
387
388 char p = logPriorityToChar(log.priority);
389 msg8 += String8::format("\n %s.%03d %c %s", timeStr.c_str(), ms, p, log.message.c_str());
390 }
391
392 msg8 += String8::format("\n%s %s %s", ruler.c_str(), footer.c_str(), ruler.c_str());
393 return msg8.c_str();
394 }
395
addLog(const::V1_4::LogMessage & log)396 void LogBuffer::addLog(const ::V1_4::LogMessage& log) {
397 std::unique_lock<std::mutex> lock(mMutex);
398 mBuffer.push_back(log);
399 while (mBuffer.size() > MAX_CAPACITY) {
400 mBuffer.pop_front();
401 }
402 }
403
getLogs()404 Vector<::V1_4::LogMessage> LogBuffer::getLogs() {
405 std::unique_lock<std::mutex> lock(mMutex);
406 Vector<::V1_4::LogMessage> logs;
407 for (auto log : mBuffer) {
408 logs.push_back(log);
409 }
410 return logs;
411 }
412
413 LogBuffer gLogBuf;
414 } // namespace DrmUtils
415 } // namespace android
416