blob: ed3848d1131b7d124e7119228ab754779afc55b4 [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 Shihc0d1d0e2019-11-24 13:21:04 -080085 if (!uuid || factory->isCryptoSchemeSupported(uuid)) {
Robert Shih5ff3ad62019-11-09 08:26:49 -080086 factories.push_back(factory);
Robert Shih10fe9432019-11-09 08:26:49 -080087 }
88 }
89 }
90 });
91}
92
Robert Shih54e1d212021-03-03 20:17:24 -080093template <typename Hal, typename V>
94void MakeHidlFactories(const uint8_t uuid[16], V &factories) {
95 std::map<std::string, std::string> instances;
96 MakeHidlFactories<Hal>(uuid, factories, instances);
97}
98
Robert Shih10fe9432019-11-09 08:26:49 -080099hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
100 hidl_vec<uint8_t> vec(size);
101 if (ptr != nullptr) {
102 memcpy(vec.data(), ptr, size);
103 }
104 return vec;
105}
106
107hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
108 if (ptr == nullptr) {
109 return hidl_array<uint8_t, 16>();
110 }
111 return hidl_array<uint8_t, 16>(ptr);
112}
113
Robert Shih5ff3ad62019-11-09 08:26:49 -0800114sp<::V1_0::IDrmPlugin> MakeDrmPlugin(const sp<::V1_0::IDrmFactory> &factory,
115 const uint8_t uuid[16], const char *appPackageName) {
116 sp<::V1_0::IDrmPlugin> plugin;
117 factory->createPlugin(toHidlArray16(uuid), hidl_string(appPackageName),
118 [&](::V1_0::Status status, const sp<::V1_0::IDrmPlugin> &hPlugin) {
119 if (status != ::V1_0::Status::OK) {
Robert Shihbd790122021-03-01 20:45:31 -0800120 LOG2BE(uuid, "MakeDrmPlugin failed: %d", status);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800121 return;
122 }
123 plugin = hPlugin;
124 });
125 return plugin;
126}
127
Robert Shih10fe9432019-11-09 08:26:49 -0800128sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> &factory,
129 const uint8_t uuid[16], const void *initData,
130 size_t initDataSize) {
131 sp<::V1_0::ICryptoPlugin> plugin;
132 factory->createPlugin(toHidlArray16(uuid), toHidlVec(initData, initDataSize),
133 [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin> &hPlugin) {
134 if (status != ::V1_0::Status::OK) {
Robert Shihbd790122021-03-01 20:45:31 -0800135 LOG2BE(uuid, "MakeCryptoPlugin failed: %d", status);
Robert Shih10fe9432019-11-09 08:26:49 -0800136 return;
137 }
138 plugin = hPlugin;
139 });
140 return plugin;
141}
142
Robert Shih28c2ed32019-10-27 22:55:12 -0700143} // namespace
144
145bool UseDrmService() {
Robert Shih17c6d822019-11-07 11:31:43 -0800146 return property_get_bool("mediadrm.use_mediadrmserver", true);
Robert Shih28c2ed32019-10-27 22:55:12 -0700147}
148
149sp<IDrm> MakeDrm(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800150 return MakeObject<DrmHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700151}
152
153sp<ICrypto> MakeCrypto(status_t *pstatus) {
Robert Shih6571bf62019-11-10 15:03:01 -0800154 return MakeObject<CryptoHal>(pstatus);
Robert Shih28c2ed32019-10-27 22:55:12 -0700155}
156
Robert Shih5ff3ad62019-11-09 08:26:49 -0800157std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16]) {
158 std::vector<sp<::V1_0::IDrmFactory>> drmFactories;
Robert Shih54e1d212021-03-03 20:17:24 -0800159 std::map<std::string, std::string> instances;
160 MakeHidlFactories<::V1_0::IDrmFactory>(uuid, drmFactories, instances);
161 MakeHidlFactories<::V1_1::IDrmFactory>(uuid, drmFactories, instances);
162 MakeHidlFactories<::V1_2::IDrmFactory>(uuid, drmFactories, instances);
163 MakeHidlFactories<::V1_3::IDrmFactory>(uuid, drmFactories, instances);
164 MakeHidlFactories<::V1_4::IDrmFactory>(uuid, drmFactories, instances);
165 for (auto const& entry : instances) {
166 LOG2BI("found instance=%s version=%s", entry.first.c_str(), entry.second.c_str());
167 }
Robert Shih5ff3ad62019-11-09 08:26:49 -0800168 return drmFactories;
169}
170
171std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
172 const char *appPackageName) {
173 std::vector<sp<::V1_0::IDrmPlugin>> plugins;
174 for (const auto &factory : MakeDrmFactories(uuid)) {
175 plugins.push_back(MakeDrmPlugin(factory, uuid, appPackageName));
176 }
177 return plugins;
178}
179
Robert Shih10fe9432019-11-09 08:26:49 -0800180std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
181 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
Robert Shih5ff3ad62019-11-09 08:26:49 -0800182 MakeHidlFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
183 MakeHidlFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
184 MakeHidlFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih2c377872019-11-24 22:17:46 -0800185 MakeHidlFactories<::V1_3::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih8635cb12021-02-26 07:57:55 -0800186 MakeHidlFactories<::V1_4::ICryptoFactory>(uuid, cryptoFactories);
Robert Shih10fe9432019-11-09 08:26:49 -0800187 return cryptoFactories;
188}
189
190std::vector<sp<ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], const void *initData,
191 size_t initDataSize) {
192 std::vector<sp<ICryptoPlugin>> plugins;
193 for (const auto &factory : MakeCryptoFactories(uuid)) {
194 plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
195 }
196 return plugins;
197}
198
Robert Shih5944a0b2021-02-10 04:26:33 -0800199status_t toStatusT_1_4(::V1_4::Status status) {
200 switch (status) {
201 case ::V1_4::Status::OK:
202 return OK;
203 case ::V1_4::Status::BAD_VALUE:
204 return BAD_VALUE;
205 case ::V1_4::Status::ERROR_DRM_CANNOT_HANDLE:
206 return ERROR_DRM_CANNOT_HANDLE;
207 case ::V1_4::Status::ERROR_DRM_DECRYPT:
208 return ERROR_DRM_DECRYPT;
209 case ::V1_4::Status::ERROR_DRM_DEVICE_REVOKED:
210 return ERROR_DRM_DEVICE_REVOKED;
211 case ::V1_4::Status::ERROR_DRM_FRAME_TOO_LARGE:
212 return ERROR_DRM_FRAME_TOO_LARGE;
213 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
214 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
215 case ::V1_4::Status::ERROR_DRM_INSUFFICIENT_SECURITY:
216 return ERROR_DRM_INSUFFICIENT_SECURITY;
217 case ::V1_4::Status::ERROR_DRM_INVALID_STATE:
218 return ERROR_DRM_INVALID_STATE;
219 case ::V1_4::Status::ERROR_DRM_LICENSE_EXPIRED:
220 return ERROR_DRM_LICENSE_EXPIRED;
221 case ::V1_4::Status::ERROR_DRM_NO_LICENSE:
222 return ERROR_DRM_NO_LICENSE;
223 case ::V1_4::Status::ERROR_DRM_NOT_PROVISIONED:
224 return ERROR_DRM_NOT_PROVISIONED;
225 case ::V1_4::Status::ERROR_DRM_RESOURCE_BUSY:
226 return ERROR_DRM_RESOURCE_BUSY;
227 case ::V1_4::Status::ERROR_DRM_RESOURCE_CONTENTION:
228 return ERROR_DRM_RESOURCE_CONTENTION;
229 case ::V1_4::Status::ERROR_DRM_SESSION_LOST_STATE:
230 return ERROR_DRM_SESSION_LOST_STATE;
231 case ::V1_4::Status::ERROR_DRM_SESSION_NOT_OPENED:
232 return ERROR_DRM_SESSION_NOT_OPENED;
233
234 // New in S / drm@1.4:
235 case ::V1_4::Status::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
236 return ERROR_DRM_ZERO_SUBSAMPLES;
237 case ::V1_4::Status::CRYPTO_LIBRARY_ERROR:
238 return ERROR_DRM_CRYPTO_LIBRARY;
239 case ::V1_4::Status::GENERAL_OEM_ERROR:
240 return ERROR_DRM_GENERIC_OEM;
241 case ::V1_4::Status::GENERAL_PLUGIN_ERROR:
242 return ERROR_DRM_GENERIC_PLUGIN;
243 case ::V1_4::Status::INIT_DATA_INVALID:
244 return ERROR_DRM_INIT_DATA;
245 case ::V1_4::Status::KEY_NOT_LOADED:
246 return ERROR_DRM_KEY_NOT_LOADED;
247 case ::V1_4::Status::LICENSE_PARSE_ERROR:
248 return ERROR_DRM_LICENSE_PARSE;
249 case ::V1_4::Status::LICENSE_POLICY_ERROR:
250 return ERROR_DRM_LICENSE_POLICY;
251 case ::V1_4::Status::LICENSE_RELEASE_ERROR:
252 return ERROR_DRM_LICENSE_RELEASE;
253 case ::V1_4::Status::LICENSE_REQUEST_REJECTED:
254 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
255 case ::V1_4::Status::LICENSE_RESTORE_ERROR:
256 return ERROR_DRM_LICENSE_RESTORE;
257 case ::V1_4::Status::LICENSE_STATE_ERROR:
258 return ERROR_DRM_LICENSE_STATE;
259 case ::V1_4::Status::MALFORMED_CERTIFICATE:
260 return ERROR_DRM_CERTIFICATE_MALFORMED;
261 case ::V1_4::Status::MEDIA_FRAMEWORK_ERROR:
262 return ERROR_DRM_MEDIA_FRAMEWORK;
263 case ::V1_4::Status::MISSING_CERTIFICATE:
264 return ERROR_DRM_CERTIFICATE_MISSING;
265 case ::V1_4::Status::PROVISIONING_CERTIFICATE_ERROR:
266 return ERROR_DRM_PROVISIONING_CERTIFICATE;
267 case ::V1_4::Status::PROVISIONING_CONFIGURATION_ERROR:
268 return ERROR_DRM_PROVISIONING_CONFIG;
269 case ::V1_4::Status::PROVISIONING_PARSE_ERROR:
270 return ERROR_DRM_PROVISIONING_PARSE;
Robert Shihe6c85332021-03-03 02:23:15 -0800271 case ::V1_4::Status::PROVISIONING_REQUEST_REJECTED:
272 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
Robert Shih5944a0b2021-02-10 04:26:33 -0800273 case ::V1_4::Status::RETRYABLE_PROVISIONING_ERROR:
274 return ERROR_DRM_PROVISIONING_RETRY;
275 case ::V1_4::Status::SECURE_STOP_RELEASE_ERROR:
276 return ERROR_DRM_SECURE_STOP_RELEASE;
277 case ::V1_4::Status::STORAGE_READ_FAILURE:
278 return ERROR_DRM_STORAGE_READ;
279 case ::V1_4::Status::STORAGE_WRITE_FAILURE:
280 return ERROR_DRM_STORAGE_WRITE;
281
282 case ::V1_4::Status::ERROR_DRM_UNKNOWN:
283 default:
284 return ERROR_DRM_UNKNOWN;
285 }
286 return ERROR_DRM_UNKNOWN;
287}
288
Robert Shih8635cb12021-02-26 07:57:55 -0800289namespace {
290char logPriorityToChar(::V1_4::LogPriority priority) {
291 char p = 'U';
292 switch (priority) {
293 case ::V1_4::LogPriority::VERBOSE: p = 'V'; break;
294 case ::V1_4::LogPriority::DEBUG: p = 'D'; break;
295 case ::V1_4::LogPriority::INFO: p = 'I'; break;
296 case ::V1_4::LogPriority::WARN: p = 'W'; break;
297 case ::V1_4::LogPriority::ERROR: p = 'E'; break;
298 case ::V1_4::LogPriority::FATAL: p = 'F'; break;
299 default: p = 'U'; break;
300 }
301 return p;
302}
303} // namespace
304
305std::string GetExceptionMessage(status_t err, const char *msg,
306 const Vector<::V1_4::LogMessage> &logs) {
307 String8 msg8;
308 if (msg) {
309 msg8 += msg;
310 msg8 += ": ";
311 }
312 auto errStr = StrCryptoError(err);
313 msg8 += errStr.c_str();
314
315 for (auto log : logs) {
316 time_t seconds = log.timeMs / 1000;
317 int ms = log.timeMs % 1000;
318 char buf[64] = {0};
319 std::string timeStr = "00-00 00:00:00";
320 if (strftime(buf, sizeof buf, "%m-%d %H:%M:%S", std::localtime(&seconds))) {
321 timeStr = buf;
322 }
323
324 char p = logPriorityToChar(log.priority);
325 msg8 += String8::format("\n%s.%03d %c %s", timeStr.c_str(), ms, p, log.message.c_str());
326 }
327
328 return msg8.c_str();
329}
330
331void LogBuffer::addLog(const ::V1_4::LogMessage &log) {
332 std::unique_lock<std::mutex> lock(mMutex);
333 mBuffer.push_back(log);
334 while (mBuffer.size() > MAX_CAPACITY) {
335 mBuffer.pop_front();
336 }
337}
338
339Vector<::V1_4::LogMessage> LogBuffer::getLogs() {
340 std::unique_lock<std::mutex> lock(mMutex);
341 Vector<::V1_4::LogMessage> logs;
342 for (auto log : mBuffer) {
343 logs.push_back(log);
344 }
345 return logs;
346}
347
348LogBuffer gLogBuf;
Robert Shih28c2ed32019-10-27 22:55:12 -0700349} // namespace DrmUtils
350} // namespace android