blob: 32390d69875ed47bdd9a996d7083d79eb6d0d241 [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>
Sohail Nagaraj0cc3da82022-11-30 10:22:37 +053025#include <mediadrm/DrmStatus.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070026#include <utils/Errors.h> // for status_t
Robert Shih0beba052021-02-14 00:47:00 -080027#include <utils/Log.h>
28#include <utils/String8.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070029#include <utils/StrongPointer.h>
Robert Shih0beba052021-02-14 00:47:00 -080030#include <utils/Vector.h>
Robert Shih8635cb12021-02-26 07:57:55 -080031#include <algorithm>
32#include <chrono>
33#include <cstddef>
34#include <cstdint>
Robert Shih2616a632021-09-13 17:45:38 -070035#include <cstring>
Robert Shih0beba052021-02-14 00:47:00 -080036#include <ctime>
Robert Shih8635cb12021-02-26 07:57:55 -080037#include <deque>
Robert Shihbd790122021-03-01 20:45:31 -080038#include <endian.h>
Robert Shih8635cb12021-02-26 07:57:55 -080039#include <iterator>
40#include <mutex>
Robert Shihbd790122021-03-01 20:45:31 -080041#include <string>
Robert Shih10fe9432019-11-09 08:26:49 -080042#include <vector>
Kyle Zhang6605add2022-01-13 17:51:23 +000043#include <aidl/android/hardware/drm/LogMessage.h>
44#include <aidl/android/hardware/drm/Status.h>
Kyle Zhang994023a2022-02-09 05:32:12 +000045#include <aidl/android/hardware/drm/IDrmFactory.h>
Robert Shih9afca952021-02-12 01:36:06 -080046
Robert Shih10fe9432019-11-09 08:26:49 -080047using namespace ::android::hardware::drm;
Robert Shih9afca952021-02-12 01:36:06 -080048using ::android::hardware::hidl_vec;
49using ::android::hardware::Return;
Robert Shih28c2ed32019-10-27 22:55:12 -070050
Kyle Zhang6605add2022-01-13 17:51:23 +000051using ::aidl::android::hardware::drm::LogPriority;
52using ::aidl::android::hardware::drm::LogMessage;
Kyle Zhang994023a2022-02-09 05:32:12 +000053using ::aidl::android::hardware::drm::Uuid;
Kyle Zhang6605add2022-01-13 17:51:23 +000054using StatusAidl = ::aidl::android::hardware::drm::Status;
Kyle Zhang994023a2022-02-09 05:32:12 +000055using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
Kyle Zhang6605add2022-01-13 17:51:23 +000056
Robert Shih28c2ed32019-10-27 22:55:12 -070057namespace android {
58
59struct ICrypto;
60struct IDrm;
61
62namespace DrmUtils {
63
Robert Shih8635cb12021-02-26 07:57:55 -080064// Log APIs
65class LogBuffer {
66 public:
67 static const int MAX_CAPACITY = 100;
68 void addLog(const ::V1_4::LogMessage &log);
69 Vector<::V1_4::LogMessage> getLogs();
70
71 private:
72 std::deque<::V1_4::LogMessage> mBuffer;
73 std::mutex mMutex;
74};
75
76extern LogBuffer gLogBuf;
77
78static inline int formatBuffer(char *buf, size_t size, const char *msg) {
79 return snprintf(buf, size, "%s", msg);
80}
81
82template <typename First, typename... Args>
83static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
84 return snprintf(buf, size, fmt, first, args...);
85}
86
87template <typename... Args>
88void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
89 const int LOG_BUF_SIZE = 256;
90 char buf[LOG_BUF_SIZE];
91 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
92 if (len <= 0) {
93 return;
94 }
95 __android_log_write(level, LOG_TAG, buf);
96 if (level >= ANDROID_LOG_INFO) {
97 int64_t epochTimeMs =
98 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
99 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
100 }
101}
102
Robert Shihbd790122021-03-01 20:45:31 -0800103template <typename... Args>
104void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
Robert Shih2616a632021-09-13 17:45:38 -0700105 uint64_t uuid2[2] = {};
106 std::memcpy(uuid2, uuid, sizeof(uuid2));
Robert Shihbd790122021-03-01 20:45:31 -0800107 std::string uuidFmt("uuid=[%lx %lx] ");
108 uuidFmt += fmt;
109 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
110}
111
Robert Shih8635cb12021-02-26 07:57:55 -0800112#ifndef LOG2BE
113#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
114#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
115#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
116#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
117#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
118#endif
119
Robert Shih28c2ed32019-10-27 22:55:12 -0700120bool UseDrmService();
121
122sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
123
124sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
125
Robert Shihd3f9ba72019-11-20 17:25:26 -0800126template<typename BA, typename PARCEL>
127void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700128 obj.writeInt32(vec.size());
129 if (vec.size()) {
130 obj.write(vec.data(), vec.size());
131 }
132}
133
Robert Shihd3f9ba72019-11-20 17:25:26 -0800134template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700135void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800136 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700137 ET eventType,
138 const BA &sessionId,
139 const BA &data) {
140 WriteByteArray(obj, sessionId);
141 WriteByteArray(obj, data);
142 obj.writeInt32(eventType);
143}
144
Robert Shihd3f9ba72019-11-20 17:25:26 -0800145template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700146void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800147 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700148 const BA &sessionId,
149 int64_t expiryTimeInMS) {
150 WriteByteArray(obj, sessionId);
151 obj.writeInt64(expiryTimeInMS);
152}
153
Robert Shihd3f9ba72019-11-20 17:25:26 -0800154template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700155void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800156 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700157 const BA &sessionId,
158 const KSL &keyStatusList,
159 bool hasNewUsableKey) {
160 WriteByteArray(obj, sessionId);
161 obj.writeInt32(keyStatusList.size());
162 for (const auto &keyStatus : keyStatusList) {
163 WriteByteArray(obj, keyStatus.keyId);
164 obj.writeInt32(keyStatus.type);
165 }
166 obj.writeInt32(hasNewUsableKey);
167}
168
Kyle Zhang994023a2022-02-09 05:32:12 +0000169inline Uuid toAidlUuid(const uint8_t uuid[16]) {
170 Uuid uuidAidl;
171 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
172 return uuidAidl;
173}
174
175std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
176
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800177std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800178
179std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
180 const char *appPackageName);
181
Robert Shih10fe9432019-11-09 08:26:49 -0800182std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
183
184std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
185 const void *initData, size_t initDataSize);
186
Robert Shih5944a0b2021-02-10 04:26:33 -0800187status_t toStatusT_1_4(::V1_4::Status status);
188
189template<typename S>
190inline status_t toStatusT(S status) {
191 auto err = static_cast<::V1_4::Status>(status);
192 return toStatusT_1_4(err);
193}
194
195template<typename T>
196inline status_t toStatusT(const android::hardware::Return<T> &status) {
197 auto t = static_cast<T>(status);
198 auto err = static_cast<::V1_4::Status>(t);
199 return toStatusT_1_4(err);
200}
201
Sohail Nagaraj0cc3da82022-11-30 10:22:37 +0530202inline DrmStatus statusAidlToDrmStatus(::ndk::ScopedAStatus& statusAidl) {
Kyle Zhang96af9572022-02-05 06:38:53 +0000203 if (statusAidl.isOk()) return OK;
204 if (statusAidl.getExceptionCode() != EX_SERVICE_SPECIFIC) return DEAD_OBJECT;
205 auto status = static_cast<StatusAidl>(statusAidl.getServiceSpecificError());
Kyle Zhang6605add2022-01-13 17:51:23 +0000206 switch (status) {
207 case StatusAidl::OK:
208 return OK;
209 case StatusAidl::BAD_VALUE:
210 return BAD_VALUE;
211 case StatusAidl::ERROR_DRM_CANNOT_HANDLE:
212 return ERROR_DRM_CANNOT_HANDLE;
213 case StatusAidl::ERROR_DRM_DECRYPT:
214 return ERROR_DRM_DECRYPT;
215 case StatusAidl::ERROR_DRM_DEVICE_REVOKED:
216 return ERROR_DRM_DEVICE_REVOKED;
217 case StatusAidl::ERROR_DRM_FRAME_TOO_LARGE:
218 return ERROR_DRM_FRAME_TOO_LARGE;
219 case StatusAidl::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
220 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
221 case StatusAidl::ERROR_DRM_INSUFFICIENT_SECURITY:
222 return ERROR_DRM_INSUFFICIENT_SECURITY;
223 case StatusAidl::ERROR_DRM_INVALID_STATE:
224 return ERROR_DRM_INVALID_STATE;
225 case StatusAidl::ERROR_DRM_LICENSE_EXPIRED:
226 return ERROR_DRM_LICENSE_EXPIRED;
227 case StatusAidl::ERROR_DRM_NO_LICENSE:
228 return ERROR_DRM_NO_LICENSE;
229 case StatusAidl::ERROR_DRM_NOT_PROVISIONED:
230 return ERROR_DRM_NOT_PROVISIONED;
231 case StatusAidl::ERROR_DRM_RESOURCE_BUSY:
232 return ERROR_DRM_RESOURCE_BUSY;
233 case StatusAidl::ERROR_DRM_RESOURCE_CONTENTION:
234 return ERROR_DRM_RESOURCE_CONTENTION;
235 case StatusAidl::ERROR_DRM_SESSION_LOST_STATE:
236 return ERROR_DRM_SESSION_LOST_STATE;
237 case StatusAidl::ERROR_DRM_SESSION_NOT_OPENED:
238 return ERROR_DRM_SESSION_NOT_OPENED;
239
240 // New in S / drm@1.4:
241 case StatusAidl::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
242 return ERROR_DRM_ZERO_SUBSAMPLES;
243 case StatusAidl::CRYPTO_LIBRARY_ERROR:
244 return ERROR_DRM_CRYPTO_LIBRARY;
245 case StatusAidl::GENERAL_OEM_ERROR:
246 return ERROR_DRM_GENERIC_OEM;
247 case StatusAidl::GENERAL_PLUGIN_ERROR:
248 return ERROR_DRM_GENERIC_PLUGIN;
249 case StatusAidl::INIT_DATA_INVALID:
250 return ERROR_DRM_INIT_DATA;
251 case StatusAidl::KEY_NOT_LOADED:
252 return ERROR_DRM_KEY_NOT_LOADED;
253 case StatusAidl::LICENSE_PARSE_ERROR:
254 return ERROR_DRM_LICENSE_PARSE;
255 case StatusAidl::LICENSE_POLICY_ERROR:
256 return ERROR_DRM_LICENSE_POLICY;
257 case StatusAidl::LICENSE_RELEASE_ERROR:
258 return ERROR_DRM_LICENSE_RELEASE;
259 case StatusAidl::LICENSE_REQUEST_REJECTED:
260 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
261 case StatusAidl::LICENSE_RESTORE_ERROR:
262 return ERROR_DRM_LICENSE_RESTORE;
263 case StatusAidl::LICENSE_STATE_ERROR:
264 return ERROR_DRM_LICENSE_STATE;
265 case StatusAidl::MALFORMED_CERTIFICATE:
266 return ERROR_DRM_CERTIFICATE_MALFORMED;
267 case StatusAidl::MEDIA_FRAMEWORK_ERROR:
268 return ERROR_DRM_MEDIA_FRAMEWORK;
269 case StatusAidl::MISSING_CERTIFICATE:
270 return ERROR_DRM_CERTIFICATE_MISSING;
271 case StatusAidl::PROVISIONING_CERTIFICATE_ERROR:
272 return ERROR_DRM_PROVISIONING_CERTIFICATE;
273 case StatusAidl::PROVISIONING_CONFIGURATION_ERROR:
274 return ERROR_DRM_PROVISIONING_CONFIG;
275 case StatusAidl::PROVISIONING_PARSE_ERROR:
276 return ERROR_DRM_PROVISIONING_PARSE;
277 case StatusAidl::PROVISIONING_REQUEST_REJECTED:
278 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
279 case StatusAidl::RETRYABLE_PROVISIONING_ERROR:
280 return ERROR_DRM_PROVISIONING_RETRY;
281 case StatusAidl::SECURE_STOP_RELEASE_ERROR:
282 return ERROR_DRM_SECURE_STOP_RELEASE;
283 case StatusAidl::STORAGE_READ_FAILURE:
284 return ERROR_DRM_STORAGE_READ;
285 case StatusAidl::STORAGE_WRITE_FAILURE:
286 return ERROR_DRM_STORAGE_WRITE;
287
288 case StatusAidl::ERROR_DRM_UNKNOWN:
289 default:
290 return ERROR_DRM_UNKNOWN;
291 }
292 return ERROR_DRM_UNKNOWN;
293}
294
295template<typename T, typename U>
296status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
297 std::shared_ptr<T> plugin = obj;
298 if (obj == NULL) {
299 LOG2BW("%s obj is null", U::descriptor);
300 } else if (plugin == NULL) {
301 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
302 }
303
304 std::vector<LogMessage> pluginLogsAidl;
305 if (plugin != NULL) {
306 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
307 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
308 }
309 }
310
311 std::vector<::V1_4::LogMessage> pluginLogs;
312 for (LogMessage log : pluginLogsAidl) {
313 ::V1_4::LogMessage logHidl;
314 logHidl.timeMs = log.timeMs;
315 // skip negative convert check as count of enum elements is 7
316 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
317 logHidl.message = log.message;
318 pluginLogs.push_back(logHidl);
319 }
320
321 auto allLogs(gLogBuf.getLogs());
322 LOG2BD("framework logs size %zu; plugin logs size %zu",
323 allLogs.size(), pluginLogs.size());
324 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
325 std::sort(allLogs.begin(), allLogs.end(),
326 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
327 return a.timeMs < b.timeMs;
328 });
329
330 logs.appendVector(allLogs);
331 return OK;
332}
333
Robert Shih9afca952021-02-12 01:36:06 -0800334template<typename T, typename U>
335status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
336 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800337 if (obj == NULL) {
338 LOG2BW("%s obj is null", U::descriptor);
339 } else if (plugin == NULL) {
340 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800341 }
342
343 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800344 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800345 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
346 ::V1_4::Status status,
347 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800348 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800349 err = status;
350 return;
351 }
Robert Shih8635cb12021-02-26 07:57:55 -0800352 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800353 };
354
Robert Shih8635cb12021-02-26 07:57:55 -0800355 Return<void> hResult;
356 if (plugin != NULL) {
357 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800358 }
Robert Shih8635cb12021-02-26 07:57:55 -0800359 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800360 LOG2BW("%s::getLogMessages remote call failed %s",
361 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800362 }
363
364 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700365 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800366 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800367 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
368 std::sort(allLogs.begin(), allLogs.end(),
369 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
370 return a.timeMs < b.timeMs;
371 });
372
373 logs.appendVector(allLogs);
374 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800375}
376
Robert Shih8635cb12021-02-26 07:57:55 -0800377std::string GetExceptionMessage(status_t err, const char *msg,
378 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800379
380template<typename T>
381std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800382 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800383 iface->getLogMessages(logs);
384 return GetExceptionMessage(err, msg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800385}
386
Robert Shih28c2ed32019-10-27 22:55:12 -0700387} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700388} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700389#endif // ANDROID_DRMUTILS_H