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/hardware/drm/1.4/ICryptoFactory.h>
31 #include <android/hardware/drm/1.4/IDrmFactory.h>
32 #include <android/hidl/manager/1.2/IServiceManager.h>
33 #include <hidl/HidlSupport.h>
34
35 #include <utils/Errors.h>
36 #include <utils/Log.h>
37 #include <utils/String16.h>
38 #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>
45
46 #include <map>
47 #include <string>
48
49 using HServiceManager = ::android::hidl::manager::V1_2::IServiceManager;
50 using ::android::hardware::hidl_array;
51 using ::android::hardware::hidl_string;
52 using ::android::hardware::hidl_vec;
53 using namespace ::android::hardware::drm;
54
55 namespace android {
56 namespace DrmUtils {
57
58 namespace {
59
60 template<typename Hal>
MakeObject(status_t * pstatus)61 Hal *MakeObject(status_t *pstatus) {
62 status_t err = OK;
63 status_t &status = pstatus ? *pstatus : err;
64 auto obj = new Hal();
65 status = obj->initCheck();
66 if (status != OK && status != NO_INIT) {
67 return NULL;
68 }
69 return obj;
70 }
71
72 template <typename Hal, typename V, typename M>
MakeHidlFactories(const uint8_t uuid[16],V & factories,M & instances)73 void MakeHidlFactories(const uint8_t uuid[16], V &factories, M& instances) {
74 sp<HServiceManager> serviceManager = HServiceManager::getService();
75 if (serviceManager == nullptr) {
76 LOG2BE("Failed to get service manager");
77 return;
78 }
79
80 serviceManager->listManifestByInterface(Hal::descriptor, [&](const hidl_vec<hidl_string> ®istered) {
81 for (const auto &instance : registered) {
82 auto factory = Hal::getService(instance);
83 if (factory != nullptr) {
84 instances[instance.c_str()] = Hal::descriptor;
85 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) {
96 factories.push_back(factory);
97 }
98 }
99 }
100 });
101 }
102
103 template <typename Hal, typename V>
MakeHidlFactories(const uint8_t uuid[16],V & factories)104 void MakeHidlFactories(const uint8_t uuid[16], V &factories) {
105 std::map<std::string, std::string> instances;
106 MakeHidlFactories<Hal>(uuid, factories, instances);
107 }
108
toHidlVec(const void * ptr,size_t size)109 hidl_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
toHidlArray16(const uint8_t * ptr)117 hidl_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
MakeDrmPlugin(const sp<::V1_0::IDrmFactory> & factory,const uint8_t uuid[16],const char * appPackageName)124 sp<::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;
127 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 }
142 }
143
MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> & factory,const uint8_t uuid[16],const void * initData,size_t initDataSize)144 sp<::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;
148 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 }
163 }
164
165 } // namespace
166
UseDrmService()167 bool UseDrmService() {
168 return property_get_bool("mediadrm.use_mediadrmserver", true);
169 }
170
MakeDrm(status_t * pstatus)171 sp<IDrm> MakeDrm(status_t *pstatus) {
172 return MakeObject<DrmHal>(pstatus);
173 }
174
MakeCrypto(status_t * pstatus)175 sp<ICrypto> MakeCrypto(status_t *pstatus) {
176 return MakeObject<CryptoHal>(pstatus);
177 }
178
MakeDrmFactories(const uint8_t uuid[16])179 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16]) {
180 std::vector<sp<::V1_0::IDrmFactory>> drmFactories;
181 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 }
190 return drmFactories;
191 }
192
MakeDrmPlugins(const uint8_t uuid[16],const char * appPackageName)193 std::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
MakeCryptoFactories(const uint8_t uuid[16])202 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
203 std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
204 MakeHidlFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
205 MakeHidlFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
206 MakeHidlFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
207 MakeHidlFactories<::V1_3::ICryptoFactory>(uuid, cryptoFactories);
208 MakeHidlFactories<::V1_4::ICryptoFactory>(uuid, cryptoFactories);
209 return cryptoFactories;
210 }
211
MakeCryptoPlugins(const uint8_t uuid[16],const void * initData,size_t initDataSize)212 std::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
toStatusT_1_4(::V1_4::Status status)221 status_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;
293 case ::V1_4::Status::PROVISIONING_REQUEST_REJECTED:
294 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
295 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
311 namespace {
logPriorityToChar(::V1_4::LogPriority priority)312 char 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
GetExceptionMessage(status_t err,const char * msg,const Vector<::V1_4::LogMessage> & logs)327 std::string GetExceptionMessage(status_t err, const char *msg,
328 const Vector<::V1_4::LogMessage> &logs) {
329 std::string ruler("==============================");
330 std::string header("Beginning of DRM Plugin Log");
331 std::string footer("End of DRM Plugin Log");
332 String8 msg8;
333 if (msg) {
334 msg8 += msg;
335 msg8 += ": ";
336 }
337 auto errStr = StrCryptoError(err);
338 msg8 += errStr.c_str();
339 msg8 += String8::format("\n%s %s %s", ruler.c_str(), header.c_str(), ruler.c_str());
340
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);
351 msg8 += String8::format("\n %s.%03d %c %s", timeStr.c_str(), ms, p, log.message.c_str());
352 }
353
354 msg8 += String8::format("\n%s %s %s", ruler.c_str(), footer.c_str(), ruler.c_str());
355 return msg8.c_str();
356 }
357
addLog(const::V1_4::LogMessage & log)358 void 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
getLogs()366 Vector<::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
375 LogBuffer gLogBuf;
376 } // namespace DrmUtils
377 } // namespace android
378