blob: 66fe48843c468b8dcc7286f742d44cb5f8144726 [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 Shih0beba052021-02-14 00:47:00 -080034#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080035#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080036#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080037#include <iterator>
38#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080039#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080040#include <vector>
Kyle Zhang6605add2022-01-13 17:51:23 +000041#include <aidl/android/hardware/drm/LogMessage.h>
42#include <aidl/android/hardware/drm/Status.h>
Kyle Zhang994023a2022-02-09 05:32:12 +000043#include <aidl/android/hardware/drm/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080044
Robert Shih10fe9432019-11-09 08:26:49 -080045using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080046using ::android::hardware::hidl_vec;
47using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070048
Kyle Zhang6605add2022-01-13 17:51:23 +000049using ::aidl::android::hardware::drm::LogPriority;
50using ::aidl::android::hardware::drm::LogMessage;
Kyle Zhang994023a2022-02-09 05:32:12 +000051using ::aidl::android::hardware::drm::Uuid;
Kyle Zhang6605add2022-01-13 17:51:23 +000052using StatusAidl = ::aidl::android::hardware::drm::Status;
Kyle Zhang994023a2022-02-09 05:32:12 +000053using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
Kyle Zhang6605add2022-01-13 17:51:23 +000054
Robert Shih28c2ed32019-10-27 22:55:12 -070055namespace android {
56
57struct ICrypto;
58struct IDrm;
59
60namespace DrmUtils {
61
Robert Shih8635cb12021-02-26 07:57:55 -080062// Log APIs
63class LogBuffer {
64 public:
65 static const int MAX_CAPACITY = 100;
66 void addLog(const ::V1_4::LogMessage &log);
67 Vector<::V1_4::LogMessage> getLogs();
68
69 private:
70 std::deque<::V1_4::LogMessage> mBuffer;
71 std::mutex mMutex;
72};
73
74extern LogBuffer gLogBuf;
75
76static inline int formatBuffer(char *buf, size_t size, const char *msg) {
77 return snprintf(buf, size, "%s", msg);
78}
79
80template <typename First, typename... Args>
81static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
82 return snprintf(buf, size, fmt, first, args...);
83}
84
85template <typename... Args>
86void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
87 const int LOG_BUF_SIZE = 256;
88 char buf[LOG_BUF_SIZE];
89 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
90 if (len <= 0) {
91 return;
92 }
93 __android_log_write(level, LOG_TAG, buf);
94 if (level >= ANDROID_LOG_INFO) {
95 int64_t epochTimeMs =
96 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
97 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
98 }
99}
100
Robert Shihbd790122021-03-01 20:45:31 -0800101template <typename... Args>
102void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
103 const uint64_t* uuid2 = reinterpret_cast<const uint64_t*>(uuid);
104 std::string uuidFmt("uuid=[%lx %lx] ");
105 uuidFmt += fmt;
106 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
107}
108
Robert Shih8635cb12021-02-26 07:57:55 -0800109#ifndef LOG2BE
110#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
111#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
112#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
113#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
114#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
115#endif
116
Robert Shih28c2ed32019-10-27 22:55:12 -0700117bool UseDrmService();
118
119sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
120
121sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
122
Robert Shihd3f9ba72019-11-20 17:25:26 -0800123template<typename BA, typename PARCEL>
124void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700125 obj.writeInt32(vec.size());
126 if (vec.size()) {
127 obj.write(vec.data(), vec.size());
128 }
129}
130
Robert Shihd3f9ba72019-11-20 17:25:26 -0800131template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700132void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800133 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700134 ET eventType,
135 const BA &sessionId,
136 const BA &data) {
137 WriteByteArray(obj, sessionId);
138 WriteByteArray(obj, data);
139 obj.writeInt32(eventType);
140}
141
Robert Shihd3f9ba72019-11-20 17:25:26 -0800142template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700143void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800144 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700145 const BA &sessionId,
146 int64_t expiryTimeInMS) {
147 WriteByteArray(obj, sessionId);
148 obj.writeInt64(expiryTimeInMS);
149}
150
Robert Shihd3f9ba72019-11-20 17:25:26 -0800151template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700152void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800153 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700154 const BA &sessionId,
155 const KSL &keyStatusList,
156 bool hasNewUsableKey) {
157 WriteByteArray(obj, sessionId);
158 obj.writeInt32(keyStatusList.size());
159 for (const auto &keyStatus : keyStatusList) {
160 WriteByteArray(obj, keyStatus.keyId);
161 obj.writeInt32(keyStatus.type);
162 }
163 obj.writeInt32(hasNewUsableKey);
164}
165
Kyle Zhang994023a2022-02-09 05:32:12 +0000166inline Uuid toAidlUuid(const uint8_t uuid[16]) {
167 Uuid uuidAidl;
168 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
169 return uuidAidl;
170}
171
172std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
173
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800174std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800175
176std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
177 const char *appPackageName);
178
Robert Shih10fe9432019-11-09 08:26:49 -0800179std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
180
181std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
182 const void *initData, size_t initDataSize);
183
Robert Shih5944a0b2021-02-10 04:26:33 -0800184status_t toStatusT_1_4(::V1_4::Status status);
185
186template<typename S>
187inline status_t toStatusT(S status) {
188 auto err = static_cast<::V1_4::Status>(status);
189 return toStatusT_1_4(err);
190}
191
192template<typename T>
193inline status_t toStatusT(const android::hardware::Return<T> &status) {
194 auto t = static_cast<T>(status);
195 auto err = static_cast<::V1_4::Status>(t);
196 return toStatusT_1_4(err);
197}
198
Kyle Zhang96af9572022-02-05 06:38:53 +0000199inline status_t statusAidlToStatusT(::ndk::ScopedAStatus &statusAidl) {
200 if (statusAidl.isOk()) return OK;
201 if (statusAidl.getExceptionCode() != EX_SERVICE_SPECIFIC) return DEAD_OBJECT;
202 auto status = static_cast<StatusAidl>(statusAidl.getServiceSpecificError());
Kyle Zhang6605add2022-01-13 17:51:23 +0000203 switch (status) {
204 case StatusAidl::OK:
205 return OK;
206 case StatusAidl::BAD_VALUE:
207 return BAD_VALUE;
208 case StatusAidl::ERROR_DRM_CANNOT_HANDLE:
209 return ERROR_DRM_CANNOT_HANDLE;
210 case StatusAidl::ERROR_DRM_DECRYPT:
211 return ERROR_DRM_DECRYPT;
212 case StatusAidl::ERROR_DRM_DEVICE_REVOKED:
213 return ERROR_DRM_DEVICE_REVOKED;
214 case StatusAidl::ERROR_DRM_FRAME_TOO_LARGE:
215 return ERROR_DRM_FRAME_TOO_LARGE;
216 case StatusAidl::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
217 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
218 case StatusAidl::ERROR_DRM_INSUFFICIENT_SECURITY:
219 return ERROR_DRM_INSUFFICIENT_SECURITY;
220 case StatusAidl::ERROR_DRM_INVALID_STATE:
221 return ERROR_DRM_INVALID_STATE;
222 case StatusAidl::ERROR_DRM_LICENSE_EXPIRED:
223 return ERROR_DRM_LICENSE_EXPIRED;
224 case StatusAidl::ERROR_DRM_NO_LICENSE:
225 return ERROR_DRM_NO_LICENSE;
226 case StatusAidl::ERROR_DRM_NOT_PROVISIONED:
227 return ERROR_DRM_NOT_PROVISIONED;
228 case StatusAidl::ERROR_DRM_RESOURCE_BUSY:
229 return ERROR_DRM_RESOURCE_BUSY;
230 case StatusAidl::ERROR_DRM_RESOURCE_CONTENTION:
231 return ERROR_DRM_RESOURCE_CONTENTION;
232 case StatusAidl::ERROR_DRM_SESSION_LOST_STATE:
233 return ERROR_DRM_SESSION_LOST_STATE;
234 case StatusAidl::ERROR_DRM_SESSION_NOT_OPENED:
235 return ERROR_DRM_SESSION_NOT_OPENED;
236
237 // New in S / drm@1.4:
238 case StatusAidl::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
239 return ERROR_DRM_ZERO_SUBSAMPLES;
240 case StatusAidl::CRYPTO_LIBRARY_ERROR:
241 return ERROR_DRM_CRYPTO_LIBRARY;
242 case StatusAidl::GENERAL_OEM_ERROR:
243 return ERROR_DRM_GENERIC_OEM;
244 case StatusAidl::GENERAL_PLUGIN_ERROR:
245 return ERROR_DRM_GENERIC_PLUGIN;
246 case StatusAidl::INIT_DATA_INVALID:
247 return ERROR_DRM_INIT_DATA;
248 case StatusAidl::KEY_NOT_LOADED:
249 return ERROR_DRM_KEY_NOT_LOADED;
250 case StatusAidl::LICENSE_PARSE_ERROR:
251 return ERROR_DRM_LICENSE_PARSE;
252 case StatusAidl::LICENSE_POLICY_ERROR:
253 return ERROR_DRM_LICENSE_POLICY;
254 case StatusAidl::LICENSE_RELEASE_ERROR:
255 return ERROR_DRM_LICENSE_RELEASE;
256 case StatusAidl::LICENSE_REQUEST_REJECTED:
257 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
258 case StatusAidl::LICENSE_RESTORE_ERROR:
259 return ERROR_DRM_LICENSE_RESTORE;
260 case StatusAidl::LICENSE_STATE_ERROR:
261 return ERROR_DRM_LICENSE_STATE;
262 case StatusAidl::MALFORMED_CERTIFICATE:
263 return ERROR_DRM_CERTIFICATE_MALFORMED;
264 case StatusAidl::MEDIA_FRAMEWORK_ERROR:
265 return ERROR_DRM_MEDIA_FRAMEWORK;
266 case StatusAidl::MISSING_CERTIFICATE:
267 return ERROR_DRM_CERTIFICATE_MISSING;
268 case StatusAidl::PROVISIONING_CERTIFICATE_ERROR:
269 return ERROR_DRM_PROVISIONING_CERTIFICATE;
270 case StatusAidl::PROVISIONING_CONFIGURATION_ERROR:
271 return ERROR_DRM_PROVISIONING_CONFIG;
272 case StatusAidl::PROVISIONING_PARSE_ERROR:
273 return ERROR_DRM_PROVISIONING_PARSE;
274 case StatusAidl::PROVISIONING_REQUEST_REJECTED:
275 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
276 case StatusAidl::RETRYABLE_PROVISIONING_ERROR:
277 return ERROR_DRM_PROVISIONING_RETRY;
278 case StatusAidl::SECURE_STOP_RELEASE_ERROR:
279 return ERROR_DRM_SECURE_STOP_RELEASE;
280 case StatusAidl::STORAGE_READ_FAILURE:
281 return ERROR_DRM_STORAGE_READ;
282 case StatusAidl::STORAGE_WRITE_FAILURE:
283 return ERROR_DRM_STORAGE_WRITE;
284
285 case StatusAidl::ERROR_DRM_UNKNOWN:
286 default:
287 return ERROR_DRM_UNKNOWN;
288 }
289 return ERROR_DRM_UNKNOWN;
290}
291
292template<typename T, typename U>
293status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
294 std::shared_ptr<T> plugin = obj;
295 if (obj == NULL) {
296 LOG2BW("%s obj is null", U::descriptor);
297 } else if (plugin == NULL) {
298 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
299 }
300
301 std::vector<LogMessage> pluginLogsAidl;
302 if (plugin != NULL) {
303 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
304 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
305 }
306 }
307
308 std::vector<::V1_4::LogMessage> pluginLogs;
309 for (LogMessage log : pluginLogsAidl) {
310 ::V1_4::LogMessage logHidl;
311 logHidl.timeMs = log.timeMs;
312 // skip negative convert check as count of enum elements is 7
313 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
314 logHidl.message = log.message;
315 pluginLogs.push_back(logHidl);
316 }
317
318 auto allLogs(gLogBuf.getLogs());
319 LOG2BD("framework logs size %zu; plugin logs size %zu",
320 allLogs.size(), pluginLogs.size());
321 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
322 std::sort(allLogs.begin(), allLogs.end(),
323 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
324 return a.timeMs < b.timeMs;
325 });
326
327 logs.appendVector(allLogs);
328 return OK;
329}
330
Robert Shih9afca952021-02-12 01:36:06 -0800331template<typename T, typename U>
332status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
333 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800334 if (obj == NULL) {
335 LOG2BW("%s obj is null", U::descriptor);
336 } else if (plugin == NULL) {
337 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800338 }
339
340 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800341 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800342 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
343 ::V1_4::Status status,
344 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800345 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800346 err = status;
347 return;
348 }
Robert Shih8635cb12021-02-26 07:57:55 -0800349 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800350 };
351
Robert Shih8635cb12021-02-26 07:57:55 -0800352 Return<void> hResult;
353 if (plugin != NULL) {
354 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800355 }
Robert Shih8635cb12021-02-26 07:57:55 -0800356 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800357 LOG2BW("%s::getLogMessages remote call failed %s",
358 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800359 }
360
361 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700362 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800363 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800364 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
365 std::sort(allLogs.begin(), allLogs.end(),
366 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
367 return a.timeMs < b.timeMs;
368 });
369
370 logs.appendVector(allLogs);
371 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800372}
373
Robert Shih8635cb12021-02-26 07:57:55 -0800374std::string GetExceptionMessage(status_t err, const char *msg,
375 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800376
377template<typename T>
378std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800379 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800380 iface->getLogMessages(logs);
381 return GetExceptionMessage(err, msg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800382}
383
Robert Shih28c2ed32019-10-27 22:55:12 -0700384} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700385} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700386#endif // ANDROID_DRMUTILS_H