blob: 0b117a3cd21f828fa7282989cbdc6c38c3bd35cd [file] [log] [blame]
Robert Shih28c2ed32019-10-27 22:55:12 -07001/*
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
Robert Shih10fe9432019-11-09 08:26:49 -080020#include <android/hardware/drm/1.0/ICryptoFactory.h>
21#include <android/hardware/drm/1.0/ICryptoPlugin.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080022#include <android/hardware/drm/1.0/IDrmFactory.h>
23#include <android/hardware/drm/1.0/IDrmPlugin.h>
Robert Shih10fe9432019-11-09 08:26:49 -080024#include <android/hardware/drm/1.1/ICryptoFactory.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080025#include <android/hardware/drm/1.1/IDrmFactory.h>
Robert Shih10fe9432019-11-09 08:26:49 -080026#include <android/hardware/drm/1.2/ICryptoFactory.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080027#include <android/hardware/drm/1.2/IDrmFactory.h>
Robert Shih2c377872019-11-24 22:17:46 -080028#include <android/hardware/drm/1.3/ICryptoFactory.h>
29#include <android/hardware/drm/1.3/IDrmFactory.h>
Robert Shih8635cb12021-02-26 07:57:55 -080030#include <android/hardware/drm/1.4/ICryptoFactory.h>
31#include <android/hardware/drm/1.4/IDrmFactory.h>
Juju Sunga4cfeca2020-04-10 15:02:32 +080032#include <android/hidl/manager/1.2/IServiceManager.h>
Robert Shih10fe9432019-11-09 08:26:49 -080033#include <hidl/HidlSupport.h>
34
35#include <utils/Errors.h>
36#include <utils/Log.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070037#include <utils/String16.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070038#include <cutils/properties.h>
39
40#include <mediadrm/CryptoHal.h>
41#include <mediadrm/DrmHal.h>
42#include <mediadrm/DrmUtils.h>
43#include <mediadrm/ICrypto.h>
44#include <mediadrm/IDrm.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070045
Robert Shih54e1d212021-03-03 20:17:24 -080046#include <map>
47#include <string>
48
Juju Sunga4cfeca2020-04-10 15:02:32 +080049using HServiceManager = ::android::hidl::manager::V1_2::IServiceManager;
Robert Shih10fe9432019-11-09 08:26:49 -080050using ::android::hardware::hidl_array;
51using ::android::hardware::hidl_string;
52using ::android::hardware::hidl_vec;
53using namespace ::android::hardware::drm;
54
Robert Shih28c2ed32019-10-27 22:55:12 -070055namespace android {
56namespace DrmUtils {
57
58namespace {
Robert Shih6571bf62019-11-10 15:03:01 -080059
60template<typename Hal>
61Hal *MakeObject(status_t *pstatus) {
Robert Shih28c2ed32019-10-27 22:55:12 -070062 status_t err = OK;
63 status_t &status = pstatus ? *pstatus : err;
Robert Shih6571bf62019-11-10 15:03:01 -080064 auto obj = new Hal();
Robert Shih28c2ed32019-10-27 22:55:12 -070065 status = obj->initCheck();
66 if (status != OK && status != NO_INIT) {
67 return NULL;
68 }
69 return obj;
70}
71
Robert Shih54e1d212021-03-03 20:17:24 -080072template <typename Hal, typename V, typename M>
73void MakeHidlFactories(const uint8_t uuid[16], V &factories, M& instances) {
Robert Shih10fe9432019-11-09 08:26:49 -080074 sp<HServiceManager> serviceManager = HServiceManager::getService();
75 if (serviceManager == nullptr) {
Robert Shih8635cb12021-02-26 07:57:55 -080076 LOG2BE("Failed to get service manager");
77 return;
Robert Shih10fe9432019-11-09 08:26:49 -080078 }
79
Juju Sunga4cfeca2020-04-10 15:02:32 +080080 serviceManager->listManifestByInterface(Hal::descriptor, [&](const hidl_vec<hidl_string> &registered) {
Robert Shih10fe9432019-11-09 08:26:49 -080081 for (const auto &instance : registered) {
82 auto factory = Hal::getService(instance);
83 if (factory != nullptr) {
Robert Shih54e1d212021-03-03 20:17:24 -080084 instances[instance.c_str()] = Hal::descriptor;
Robert Shih3fa03fb2021-04-27 16:01:34 -070085 if (!uuid) {
86 factories.push_back(factory);
87 continue;
88 }
89 auto supported = factory->isCryptoSchemeSupported(uuid);
90 if (!supported.isOk()) {
91 LOG2BE(uuid, "isCryptoSchemeSupported txn failed: %s",
92 supported.description().c_str());
93 continue;
94 }
95 if (supported) {
Robert Shih5ff3ad62019-11-09 08:26:49 -080096 factories.push_back(factory);
Robert Shih10fe9432019-11-09 08:26:49 -080097 }
98 }
99 }
100 });
101}
102
Robert Shih54e1d212021-03-03 20:17:24 -0800103template <typename Hal, typename V>
104void MakeHidlFactories(const uint8_t uuid[16], V &factories) {
105 std::map<std::string, std::string> instances;
106 MakeHidlFactories<Hal>(uuid, factories, instances);
107}
108
Robert Shih10fe9432019-11-09 08:26:49 -0800109hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
110 hidl_vec<uint8_t> vec(size);
111 if (ptr != nullptr) {
112 memcpy(vec.data(), ptr, size);
113 }
114 return vec;
115}
116
117hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
118 if (ptr == nullptr) {
119 return hidl_array<uint8_t, 16>();
120 }
121 return hidl_array<uint8_t, 16>(ptr);
122}
123
Robert Shih5ff3ad62019-11-09 08:26:49 -0800124sp<::V1_0::IDrmPlugin> MakeDrmPlugin(const sp<::V1_0::IDrmFactory> &factory,
125 const uint8_t uuid[16], const char *appPackageName) {
126 sp<::V1_0::IDrmPlugin> plugin;
Robert Shih3fa03fb2021-04-27 16:01:34 -0700127 auto err = factory->createPlugin(
128 toHidlArray16(uuid), hidl_string(appPackageName),
129 [&](::V1_0::Status status, const sp<::V1_0::IDrmPlugin> &hPlugin) {
130 if (status != ::V1_0::Status::OK) {
131 LOG2BE(uuid, "MakeDrmPlugin failed: %d", status);
132 return;
133 }
134 plugin = hPlugin;
135 });
136 if (err.isOk()) {
137 return plugin;
138 } else {
139 LOG2BE(uuid, "MakeDrmPlugin txn failed: %s", err.description().c_str());
140 return nullptr;
141 }
Robert Shih5ff3ad62019-11-09 08:26:49 -0800142}
143
Robert Shih10fe9432019-11-09 08:26:49 -0800144sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> &factory,
145 const uint8_t uuid[16], const void *initData,
146 size_t initDataSize) {
147 sp<::V1_0::ICryptoPlugin> plugin;
Robert Shih3fa03fb2021-04-27 16:01:34 -0700148 auto err = factory->createPlugin(
149 toHidlArray16(uuid), toHidlVec(initData, initDataSize),
150 [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin> &hPlugin) {
151 if (status != ::V1_0::Status::OK) {
152 LOG2BE(uuid, "MakeCryptoPlugin failed: %d", status);
153 return;
154 }
155 plugin = hPlugin;
156 });
157 if (err.isOk()) {
158 return plugin;
159 } else {
160 LOG2BE(uuid, "MakeCryptoPlugin txn failed: %s", err.description().c_str());
161 return nullptr;
162 }
Robert Shih10fe9432019-11-09 08:26:49 -0800163}
164
Robert Shih28c2ed32019-10-27 22:55:12 -0700165} // namespace
166
167bool UseDrmService() {
Robert Shih17c6d822019-11-07 11:31:43 -0800168 return property_get_bool("mediadrm.use_mediadrmserver", true);
Robert Shih28c2ed32019-10-27 22:55:12 -0700169}
170
171sp<IDrm> MakeDrm(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800172 return MakeObject<DrmHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700173}
174
175sp<ICrypto> MakeCrypto(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800176 return MakeObject<CryptoHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700177}
178
Robert Shih5ff3ad62019-11-09 08:26:49 -0800179std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16]) {
180 std::vector<sp<::V1_0::IDrmFactory>> drmFactories;
Robert Shih54e1d212021-03-03 20:17:24 -0800181 std::map<std::string, std::string> instances;
182 MakeHidlFactories<::V1_0::IDrmFactory>(uuid, drmFactories, instances);
183 MakeHidlFactories<::V1_1::IDrmFactory>(uuid, drmFactories, instances);
184 MakeHidlFactories<::V1_2::IDrmFactory>(uuid, drmFactories, instances);
185 MakeHidlFactories<::V1_3::IDrmFactory>(uuid, drmFactories, instances);
186 MakeHidlFactories<::V1_4::IDrmFactory>(uuid, drmFactories, instances);
187 for (auto const& entry : instances) {
188 LOG2BI("found instance=%s version=%s", entry.first.c_str(), entry.second.c_str());
189 }
Robert Shih5ff3ad62019-11-09 08:26:49 -0800190 return drmFactories;
191}
192
193std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
194 const char *appPackageName) {
195 std::vector<sp<::V1_0::IDrmPlugin>> plugins;
196 for (const auto &factory : MakeDrmFactories(uuid)) {
197 plugins.push_back(MakeDrmPlugin(factory, uuid, appPackageName));
198 }
199 return plugins;
200}
201
Robert Shih10fe9432019-11-09 08:26:49 -0800202std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
203 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
Robert Shih5ff3ad62019-11-09 08:26:49 -0800204 MakeHidlFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
205 MakeHidlFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
206 MakeHidlFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih2c377872019-11-24 22:17:46 -0800207 MakeHidlFactories<::V1_3::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih8635cb12021-02-26 07:57:55 -0800208 MakeHidlFactories<::V1_4::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih10fe9432019-11-09 08:26:49 -0800209 return cryptoFactories;
210}
211
212std::vector<sp<ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], const void *initData,
213 size_t initDataSize) {
214 std::vector<sp<ICryptoPlugin>> plugins;
215 for (const auto &factory : MakeCryptoFactories(uuid)) {
216 plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
217 }
218 return plugins;
219}
220
Robert Shih5944a0b2021-02-10 04:26:33 -0800221status_t toStatusT_1_4(::V1_4::Status status) {
222 switch (status) {
223 case ::V1_4::Status::OK:
224 return OK;
225 case ::V1_4::Status::BAD_VALUE:
226 return BAD_VALUE;
227 case ::V1_4::Status::ERROR_DRM_CANNOT_HANDLE:
228 return ERROR_DRM_CANNOT_HANDLE;
229 case ::V1_4::Status::ERROR_DRM_DECRYPT:
230 return ERROR_DRM_DECRYPT;
231 case ::V1_4::Status::ERROR_DRM_DEVICE_REVOKED:
232 return ERROR_DRM_DEVICE_REVOKED;
233 case ::V1_4::Status::ERROR_DRM_FRAME_TOO_LARGE:
234 return ERROR_DRM_FRAME_TOO_LARGE;
235 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
236 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
237 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_SECURITY:
238 return ERROR_DRM_INSUFFICIENT_SECURITY;
239 case ::V1_4::Status::ERROR_DRM_INVALID_STATE:
240 return ERROR_DRM_INVALID_STATE;
241 case ::V1_4::Status::ERROR_DRM_LICENSE_EXPIRED:
242 return ERROR_DRM_LICENSE_EXPIRED;
243 case ::V1_4::Status::ERROR_DRM_NO_LICENSE:
244 return ERROR_DRM_NO_LICENSE;
245 case ::V1_4::Status::ERROR_DRM_NOT_PROVISIONED:
246 return ERROR_DRM_NOT_PROVISIONED;
247 case ::V1_4::Status::ERROR_DRM_RESOURCE_BUSY:
248 return ERROR_DRM_RESOURCE_BUSY;
249 case ::V1_4::Status::ERROR_DRM_RESOURCE_CONTENTION:
250 return ERROR_DRM_RESOURCE_CONTENTION;
251 case ::V1_4::Status::ERROR_DRM_SESSION_LOST_STATE:
252 return ERROR_DRM_SESSION_LOST_STATE;
253 case ::V1_4::Status::ERROR_DRM_SESSION_NOT_OPENED:
254 return ERROR_DRM_SESSION_NOT_OPENED;
255
256 // New in S / drm@1.4:
257 case ::V1_4::Status::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
258 return ERROR_DRM_ZERO_SUBSAMPLES;
259 case ::V1_4::Status::CRYPTO_LIBRARY_ERROR:
260 return ERROR_DRM_CRYPTO_LIBRARY;
261 case ::V1_4::Status::GENERAL_OEM_ERROR:
262 return ERROR_DRM_GENERIC_OEM;
263 case ::V1_4::Status::GENERAL_PLUGIN_ERROR:
264 return ERROR_DRM_GENERIC_PLUGIN;
265 case ::V1_4::Status::INIT_DATA_INVALID:
266 return ERROR_DRM_INIT_DATA;
267 case ::V1_4::Status::KEY_NOT_LOADED:
268 return ERROR_DRM_KEY_NOT_LOADED;
269 case ::V1_4::Status::LICENSE_PARSE_ERROR:
270 return ERROR_DRM_LICENSE_PARSE;
271 case ::V1_4::Status::LICENSE_POLICY_ERROR:
272 return ERROR_DRM_LICENSE_POLICY;
273 case ::V1_4::Status::LICENSE_RELEASE_ERROR:
274 return ERROR_DRM_LICENSE_RELEASE;
275 case ::V1_4::Status::LICENSE_REQUEST_REJECTED:
276 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
277 case ::V1_4::Status::LICENSE_RESTORE_ERROR:
278 return ERROR_DRM_LICENSE_RESTORE;
279 case ::V1_4::Status::LICENSE_STATE_ERROR:
280 return ERROR_DRM_LICENSE_STATE;
281 case ::V1_4::Status::MALFORMED_CERTIFICATE:
282 return ERROR_DRM_CERTIFICATE_MALFORMED;
283 case ::V1_4::Status::MEDIA_FRAMEWORK_ERROR:
284 return ERROR_DRM_MEDIA_FRAMEWORK;
285 case ::V1_4::Status::MISSING_CERTIFICATE:
286 return ERROR_DRM_CERTIFICATE_MISSING;
287 case ::V1_4::Status::PROVISIONING_CERTIFICATE_ERROR:
288 return ERROR_DRM_PROVISIONING_CERTIFICATE;
289 case ::V1_4::Status::PROVISIONING_CONFIGURATION_ERROR:
290 return ERROR_DRM_PROVISIONING_CONFIG;
291 case ::V1_4::Status::PROVISIONING_PARSE_ERROR:
292 return ERROR_DRM_PROVISIONING_PARSE;
Robert Shihe6c85332021-03-03 02:23:15 -0800293 case ::V1_4::Status::PROVISIONING_REQUEST_REJECTED:
294 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
Robert Shih5944a0b2021-02-10 04:26:33 -0800295 case ::V1_4::Status::RETRYABLE_PROVISIONING_ERROR:
296 return ERROR_DRM_PROVISIONING_RETRY;
297 case ::V1_4::Status::SECURE_STOP_RELEASE_ERROR:
298 return ERROR_DRM_SECURE_STOP_RELEASE;
299 case ::V1_4::Status::STORAGE_READ_FAILURE:
300 return ERROR_DRM_STORAGE_READ;
301 case ::V1_4::Status::STORAGE_WRITE_FAILURE:
302 return ERROR_DRM_STORAGE_WRITE;
303
304 case ::V1_4::Status::ERROR_DRM_UNKNOWN:
305 default:
306 return ERROR_DRM_UNKNOWN;
307 }
308 return ERROR_DRM_UNKNOWN;
309}
310
Robert Shih8635cb12021-02-26 07:57:55 -0800311namespace {
312char logPriorityToChar(::V1_4::LogPriority priority) {
313 char p = 'U';
314 switch (priority) {
315 case ::V1_4::LogPriority::VERBOSE: p = 'V'; break;
316 case ::V1_4::LogPriority::DEBUG: p = 'D'; break;
317 case ::V1_4::LogPriority::INFO: p = 'I'; break;
318 case ::V1_4::LogPriority::WARN: p = 'W'; break;
319 case ::V1_4::LogPriority::ERROR: p = 'E'; break;
320 case ::V1_4::LogPriority::FATAL: p = 'F'; break;
321 default: p = 'U'; break;
322 }
323 return p;
324}
325} // namespace
326
327std::string GetExceptionMessage(status_t err, const char *msg,
328 const Vector<::V1_4::LogMessage> &logs) {
Robert Shihd5c39612021-04-12 11:01:11 -0700329 std::string ruler("==============================");
330 std::string header("Beginning of DRM Plugin Log");
331 std::string footer("End of DRM Plugin Log");
Robert Shih8635cb12021-02-26 07:57:55 -0800332 String8 msg8;
333 if (msg) {
334 msg8 += msg;
335 msg8 += ": ";
336 }
337 auto errStr = StrCryptoError(err);
338 msg8 += errStr.c_str();
Robert Shihd5c39612021-04-12 11:01:11 -0700339 msg8 += String8::format("\n%s %s %s", ruler.c_str(), header.c_str(), ruler.c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800340
341 for (auto log : logs) {
342 time_t seconds = log.timeMs / 1000;
343 int ms = log.timeMs % 1000;
344 char buf[64] = {0};
345 std::string timeStr = "00-00 00:00:00";
346 if (strftime(buf, sizeof buf, "%m-%d %H:%M:%S", std::localtime(&seconds))) {
347 timeStr = buf;
348 }
349
350 char p = logPriorityToChar(log.priority);
Robert Shihd5c39612021-04-12 11:01:11 -0700351 msg8 += String8::format("\n %s.%03d %c %s", timeStr.c_str(), ms, p, log.message.c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800352 }
353
Robert Shihd5c39612021-04-12 11:01:11 -0700354 msg8 += String8::format("\n%s %s %s", ruler.c_str(), footer.c_str(), ruler.c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800355 return msg8.c_str();
356}
357
358void LogBuffer::addLog(const ::V1_4::LogMessage &log) {
359 std::unique_lock<std::mutex> lock(mMutex);
360 mBuffer.push_back(log);
361 while (mBuffer.size() > MAX_CAPACITY) {
362 mBuffer.pop_front();
363 }
364}
365
366Vector<::V1_4::LogMessage> LogBuffer::getLogs() {
367 std::unique_lock<std::mutex> lock(mMutex);
368 Vector<::V1_4::LogMessage> logs;
369 for (auto log : mBuffer) {
370 logs.push_back(log);
371 }
372 return logs;
373}
374
375LogBuffer gLogBuf;
Robert Shih28c2ed32019-10-27 22:55:12 -0700376} // namespace DrmUtils
377} // namespace android