blob: 980ce553927c0381be2eb7123cf8c5bf03077ee3 [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#ifndef ANDROID_DRMUTILS_H
18#define ANDROID_DRMUTILS_H
19
Robert Shih10fe9432019-11-09 08:26:49 -080020#include <android/hardware/drm/1.0/ICryptoFactory.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080021#include <android/hardware/drm/1.0/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080022#include <android/hardware/drm/1.4/IDrmPlugin.h>
Robert Shih5944a0b2021-02-10 04:26:33 -080023#include <android/hardware/drm/1.4/types.h>
Robert Shih9afca952021-02-12 01:36:06 -080024#include <media/stagefright/MediaErrors.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070025#include <utils/Errors.h> // for status_t
Robert Shih0beba052021-02-14 00:47:00 -080026#include <utils/Log.h>
27#include <utils/String8.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070028#include <utils/StrongPointer.h>
Robert Shih0beba052021-02-14 00:47:00 -080029#include <utils/Vector.h>
Robert Shih8635cb12021-02-26 07:57:55 -080030#include <algorithm>
31#include <chrono>
32#include <cstddef>
33#include <cstdint>
Robert Shih2616a632021-09-13 17:45:38 -070034#include <cstring>
Robert Shih0beba052021-02-14 00:47:00 -080035#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080036#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080037#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080038#include <iterator>
39#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080040#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080041#include <vector>
Kyle Zhang6605add2022-01-13 17:51:23 +000042#include <aidl/android/hardware/drm/LogMessage.h>
43#include <aidl/android/hardware/drm/Status.h>
Kyle Zhang994023a2022-02-09 05:32:12 +000044#include <aidl/android/hardware/drm/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080045
Robert Shih10fe9432019-11-09 08:26:49 -080046using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080047using ::android::hardware::hidl_vec;
48using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070049
Kyle Zhang6605add2022-01-13 17:51:23 +000050using ::aidl::android::hardware::drm::LogPriority;
51using ::aidl::android::hardware::drm::LogMessage;
Kyle Zhang994023a2022-02-09 05:32:12 +000052using ::aidl::android::hardware::drm::Uuid;
Kyle Zhang6605add2022-01-13 17:51:23 +000053using StatusAidl = ::aidl::android::hardware::drm::Status;
Kyle Zhang994023a2022-02-09 05:32:12 +000054using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
Kyle Zhang6605add2022-01-13 17:51:23 +000055
Robert Shih28c2ed32019-10-27 22:55:12 -070056namespace android {
57
58struct ICrypto;
59struct IDrm;
60
61namespace DrmUtils {
62
Robert Shih8635cb12021-02-26 07:57:55 -080063// Log APIs
64class LogBuffer {
65 public:
66 static const int MAX_CAPACITY = 100;
67 void addLog(const ::V1_4::LogMessage &log);
68 Vector<::V1_4::LogMessage> getLogs();
69
70 private:
71 std::deque<::V1_4::LogMessage> mBuffer;
72 std::mutex mMutex;
73};
74
75extern LogBuffer gLogBuf;
76
77static inline int formatBuffer(char *buf, size_t size, const char *msg) {
78 return snprintf(buf, size, "%s", msg);
79}
80
81template <typename First, typename... Args>
82static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
83 return snprintf(buf, size, fmt, first, args...);
84}
85
86template <typename... Args>
87void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
88 const int LOG_BUF_SIZE = 256;
89 char buf[LOG_BUF_SIZE];
90 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
91 if (len <= 0) {
92 return;
93 }
94 __android_log_write(level, LOG_TAG, buf);
95 if (level >= ANDROID_LOG_INFO) {
96 int64_t epochTimeMs =
97 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
98 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
99 }
100}
101
Robert Shihbd790122021-03-01 20:45:31 -0800102template <typename... Args>
103void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
Robert Shih2616a632021-09-13 17:45:38 -0700104 uint64_t uuid2[2] = {};
105 std::memcpy(uuid2, uuid, sizeof(uuid2));
Robert Shihbd790122021-03-01 20:45:31 -0800106 std::string uuidFmt("uuid=[%lx %lx] ");
107 uuidFmt += fmt;
108 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
109}
110
Robert Shih8635cb12021-02-26 07:57:55 -0800111#ifndef LOG2BE
112#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
113#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
114#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
115#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
116#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
117#endif
118
Robert Shih28c2ed32019-10-27 22:55:12 -0700119bool UseDrmService();
120
121sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
122
123sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
124
Robert Shihd3f9ba72019-11-20 17:25:26 -0800125template<typename BA, typename PARCEL>
126void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700127 obj.writeInt32(vec.size());
128 if (vec.size()) {
129 obj.write(vec.data(), vec.size());
130 }
131}
132
Robert Shihd3f9ba72019-11-20 17:25:26 -0800133template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700134void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800135 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700136 ET eventType,
137 const BA &sessionId,
138 const BA &data) {
139 WriteByteArray(obj, sessionId);
140 WriteByteArray(obj, data);
141 obj.writeInt32(eventType);
142}
143
Robert Shihd3f9ba72019-11-20 17:25:26 -0800144template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700145void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800146 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700147 const BA &sessionId,
148 int64_t expiryTimeInMS) {
149 WriteByteArray(obj, sessionId);
150 obj.writeInt64(expiryTimeInMS);
151}
152
Robert Shihd3f9ba72019-11-20 17:25:26 -0800153template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700154void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800155 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700156 const BA &sessionId,
157 const KSL &keyStatusList,
158 bool hasNewUsableKey) {
159 WriteByteArray(obj, sessionId);
160 obj.writeInt32(keyStatusList.size());
161 for (const auto &keyStatus : keyStatusList) {
162 WriteByteArray(obj, keyStatus.keyId);
163 obj.writeInt32(keyStatus.type);
164 }
165 obj.writeInt32(hasNewUsableKey);
166}
167
Kyle Zhang994023a2022-02-09 05:32:12 +0000168inline Uuid toAidlUuid(const uint8_t uuid[16]) {
169 Uuid uuidAidl;
170 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
171 return uuidAidl;
172}
173
174std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
175
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800176std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800177
178std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
179 const char *appPackageName);
180
Robert Shih10fe9432019-11-09 08:26:49 -0800181std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
182
183std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
184 const void *initData, size_t initDataSize);
185
Robert Shih5944a0b2021-02-10 04:26:33 -0800186status_t toStatusT_1_4(::V1_4::Status status);
187
188template<typename S>
189inline status_t toStatusT(S status) {
190 auto err = static_cast<::V1_4::Status>(status);
191 return toStatusT_1_4(err);
192}
193
194template<typename T>
195inline status_t toStatusT(const android::hardware::Return<T> &status) {
196 auto t = static_cast<T>(status);
197 auto err = static_cast<::V1_4::Status>(t);
198 return toStatusT_1_4(err);
199}
200
Kyle Zhang96af9572022-02-05 06:38:53 +0000201inline status_t statusAidlToStatusT(::ndk::ScopedAStatus &statusAidl) {
202 if (statusAidl.isOk()) return OK;
203 if (statusAidl.getExceptionCode() != EX_SERVICE_SPECIFIC) return DEAD_OBJECT;
204 auto status = static_cast<StatusAidl>(statusAidl.getServiceSpecificError());
Kyle Zhang6605add2022-01-13 17:51:23 +0000205 switch (status) {
206 case StatusAidl::OK:
207 return OK;
208 case StatusAidl::BAD_VALUE:
209 return BAD_VALUE;
210 case StatusAidl::ERROR_DRM_CANNOT_HANDLE:
211 return ERROR_DRM_CANNOT_HANDLE;
212 case StatusAidl::ERROR_DRM_DECRYPT:
213 return ERROR_DRM_DECRYPT;
214 case StatusAidl::ERROR_DRM_DEVICE_REVOKED:
215 return ERROR_DRM_DEVICE_REVOKED;
216 case StatusAidl::ERROR_DRM_FRAME_TOO_LARGE:
217 return ERROR_DRM_FRAME_TOO_LARGE;
218 case StatusAidl::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
219 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
220 case StatusAidl::ERROR_DRM_INSUFFICIENT_SECURITY:
221 return ERROR_DRM_INSUFFICIENT_SECURITY;
222 case StatusAidl::ERROR_DRM_INVALID_STATE:
223 return ERROR_DRM_INVALID_STATE;
224 case StatusAidl::ERROR_DRM_LICENSE_EXPIRED:
225 return ERROR_DRM_LICENSE_EXPIRED;
226 case StatusAidl::ERROR_DRM_NO_LICENSE:
227 return ERROR_DRM_NO_LICENSE;
228 case StatusAidl::ERROR_DRM_NOT_PROVISIONED:
229 return ERROR_DRM_NOT_PROVISIONED;
230 case StatusAidl::ERROR_DRM_RESOURCE_BUSY:
231 return ERROR_DRM_RESOURCE_BUSY;
232 case StatusAidl::ERROR_DRM_RESOURCE_CONTENTION:
233 return ERROR_DRM_RESOURCE_CONTENTION;
234 case StatusAidl::ERROR_DRM_SESSION_LOST_STATE:
235 return ERROR_DRM_SESSION_LOST_STATE;
236 case StatusAidl::ERROR_DRM_SESSION_NOT_OPENED:
237 return ERROR_DRM_SESSION_NOT_OPENED;
238
239 // New in S / drm@1.4:
240 case StatusAidl::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
241 return ERROR_DRM_ZERO_SUBSAMPLES;
242 case StatusAidl::CRYPTO_LIBRARY_ERROR:
243 return ERROR_DRM_CRYPTO_LIBRARY;
244 case StatusAidl::GENERAL_OEM_ERROR:
245 return ERROR_DRM_GENERIC_OEM;
246 case StatusAidl::GENERAL_PLUGIN_ERROR:
247 return ERROR_DRM_GENERIC_PLUGIN;
248 case StatusAidl::INIT_DATA_INVALID:
249 return ERROR_DRM_INIT_DATA;
250 case StatusAidl::KEY_NOT_LOADED:
251 return ERROR_DRM_KEY_NOT_LOADED;
252 case StatusAidl::LICENSE_PARSE_ERROR:
253 return ERROR_DRM_LICENSE_PARSE;
254 case StatusAidl::LICENSE_POLICY_ERROR:
255 return ERROR_DRM_LICENSE_POLICY;
256 case StatusAidl::LICENSE_RELEASE_ERROR:
257 return ERROR_DRM_LICENSE_RELEASE;
258 case StatusAidl::LICENSE_REQUEST_REJECTED:
259 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
260 case StatusAidl::LICENSE_RESTORE_ERROR:
261 return ERROR_DRM_LICENSE_RESTORE;
262 case StatusAidl::LICENSE_STATE_ERROR:
263 return ERROR_DRM_LICENSE_STATE;
264 case StatusAidl::MALFORMED_CERTIFICATE:
265 return ERROR_DRM_CERTIFICATE_MALFORMED;
266 case StatusAidl::MEDIA_FRAMEWORK_ERROR:
267 return ERROR_DRM_MEDIA_FRAMEWORK;
268 case StatusAidl::MISSING_CERTIFICATE:
269 return ERROR_DRM_CERTIFICATE_MISSING;
270 case StatusAidl::PROVISIONING_CERTIFICATE_ERROR:
271 return ERROR_DRM_PROVISIONING_CERTIFICATE;
272 case StatusAidl::PROVISIONING_CONFIGURATION_ERROR:
273 return ERROR_DRM_PROVISIONING_CONFIG;
274 case StatusAidl::PROVISIONING_PARSE_ERROR:
275 return ERROR_DRM_PROVISIONING_PARSE;
276 case StatusAidl::PROVISIONING_REQUEST_REJECTED:
277 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
278 case StatusAidl::RETRYABLE_PROVISIONING_ERROR:
279 return ERROR_DRM_PROVISIONING_RETRY;
280 case StatusAidl::SECURE_STOP_RELEASE_ERROR:
281 return ERROR_DRM_SECURE_STOP_RELEASE;
282 case StatusAidl::STORAGE_READ_FAILURE:
283 return ERROR_DRM_STORAGE_READ;
284 case StatusAidl::STORAGE_WRITE_FAILURE:
285 return ERROR_DRM_STORAGE_WRITE;
286
287 case StatusAidl::ERROR_DRM_UNKNOWN:
288 default:
289 return ERROR_DRM_UNKNOWN;
290 }
291 return ERROR_DRM_UNKNOWN;
292}
293
294template<typename T, typename U>
295status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
296 std::shared_ptr<T> plugin = obj;
297 if (obj == NULL) {
298 LOG2BW("%s obj is null", U::descriptor);
299 } else if (plugin == NULL) {
300 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
301 }
302
303 std::vector<LogMessage> pluginLogsAidl;
304 if (plugin != NULL) {
305 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
306 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
307 }
308 }
309
310 std::vector<::V1_4::LogMessage> pluginLogs;
311 for (LogMessage log : pluginLogsAidl) {
312 ::V1_4::LogMessage logHidl;
313 logHidl.timeMs = log.timeMs;
314 // skip negative convert check as count of enum elements is 7
315 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
316 logHidl.message = log.message;
317 pluginLogs.push_back(logHidl);
318 }
319
320 auto allLogs(gLogBuf.getLogs());
321 LOG2BD("framework logs size %zu; plugin logs size %zu",
322 allLogs.size(), pluginLogs.size());
323 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
324 std::sort(allLogs.begin(), allLogs.end(),
325 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
326 return a.timeMs < b.timeMs;
327 });
328
329 logs.appendVector(allLogs);
330 return OK;
331}
332
Robert Shih9afca952021-02-12 01:36:06 -0800333template<typename T, typename U>
334status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
335 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800336 if (obj == NULL) {
337 LOG2BW("%s obj is null", U::descriptor);
338 } else if (plugin == NULL) {
339 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800340 }
341
342 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800343 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800344 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
345 ::V1_4::Status status,
346 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800347 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800348 err = status;
349 return;
350 }
Robert Shih8635cb12021-02-26 07:57:55 -0800351 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800352 };
353
Robert Shih8635cb12021-02-26 07:57:55 -0800354 Return<void> hResult;
355 if (plugin != NULL) {
356 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800357 }
Robert Shih8635cb12021-02-26 07:57:55 -0800358 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800359 LOG2BW("%s::getLogMessages remote call failed %s",
360 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800361 }
362
363 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700364 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800365 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800366 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
367 std::sort(allLogs.begin(), allLogs.end(),
368 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
369 return a.timeMs < b.timeMs;
370 });
371
372 logs.appendVector(allLogs);
373 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800374}
375
Robert Shih8635cb12021-02-26 07:57:55 -0800376std::string GetExceptionMessage(status_t err, const char *msg,
377 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800378
379template<typename T>
380std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800381 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800382 iface->getLogMessages(logs);
383 return GetExceptionMessage(err, msg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800384}
385
Robert Shih28c2ed32019-10-27 22:55:12 -0700386} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700387} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700388#endif // ANDROID_DRMUTILS_H