blob: 5679cfdcd3722bca2df111450f55651f82b54e80 [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>
Robert Shih10fe9432019-11-09 08:26:49 -080043
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;
51using StatusAidl = ::aidl::android::hardware::drm::Status;
52
Robert Shih28c2ed32019-10-27 22:55:12 -070053namespace android {
54
55struct ICrypto;
56struct IDrm;
57
58namespace DrmUtils {
59
Robert Shih8635cb12021-02-26 07:57:55 -080060// Log APIs
61class LogBuffer {
62 public:
63 static const int MAX_CAPACITY = 100;
64 void addLog(const ::V1_4::LogMessage &log);
65 Vector<::V1_4::LogMessage> getLogs();
66
67 private:
68 std::deque<::V1_4::LogMessage> mBuffer;
69 std::mutex mMutex;
70};
71
72extern LogBuffer gLogBuf;
73
74static inline int formatBuffer(char *buf, size_t size, const char *msg) {
75 return snprintf(buf, size, "%s", msg);
76}
77
78template <typename First, typename... Args>
79static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
80 return snprintf(buf, size, fmt, first, args...);
81}
82
83template <typename... Args>
84void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
85 const int LOG_BUF_SIZE = 256;
86 char buf[LOG_BUF_SIZE];
87 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
88 if (len <= 0) {
89 return;
90 }
91 __android_log_write(level, LOG_TAG, buf);
92 if (level >= ANDROID_LOG_INFO) {
93 int64_t epochTimeMs =
94 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
95 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
96 }
97}
98
Robert Shihbd790122021-03-01 20:45:31 -080099template <typename... Args>
100void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
101 const uint64_t* uuid2 = reinterpret_cast<const uint64_t*>(uuid);
102 std::string uuidFmt("uuid=[%lx %lx] ");
103 uuidFmt += fmt;
104 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
105}
106
Robert Shih8635cb12021-02-26 07:57:55 -0800107#ifndef LOG2BE
108#define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
109#define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
110#define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
111#define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
112#define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
113#endif
114
Robert Shih28c2ed32019-10-27 22:55:12 -0700115bool UseDrmService();
116
117sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
118
119sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
120
Robert Shihd3f9ba72019-11-20 17:25:26 -0800121template<typename BA, typename PARCEL>
122void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -0700123 obj.writeInt32(vec.size());
124 if (vec.size()) {
125 obj.write(vec.data(), vec.size());
126 }
127}
128
Robert Shihd3f9ba72019-11-20 17:25:26 -0800129template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700130void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800131 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700132 ET eventType,
133 const BA &sessionId,
134 const BA &data) {
135 WriteByteArray(obj, sessionId);
136 WriteByteArray(obj, data);
137 obj.writeInt32(eventType);
138}
139
Robert Shihd3f9ba72019-11-20 17:25:26 -0800140template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700141void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800142 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700143 const BA &sessionId,
144 int64_t expiryTimeInMS) {
145 WriteByteArray(obj, sessionId);
146 obj.writeInt64(expiryTimeInMS);
147}
148
Robert Shihd3f9ba72019-11-20 17:25:26 -0800149template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -0700150void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -0800151 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -0700152 const BA &sessionId,
153 const KSL &keyStatusList,
154 bool hasNewUsableKey) {
155 WriteByteArray(obj, sessionId);
156 obj.writeInt32(keyStatusList.size());
157 for (const auto &keyStatus : keyStatusList) {
158 WriteByteArray(obj, keyStatus.keyId);
159 obj.writeInt32(keyStatus.type);
160 }
161 obj.writeInt32(hasNewUsableKey);
162}
163
Robert Shihc0d1d0e2019-11-24 13:21:04 -0800164std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -0800165
166std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
167 const char *appPackageName);
168
Robert Shih10fe9432019-11-09 08:26:49 -0800169std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
170
171std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
172 const void *initData, size_t initDataSize);
173
Robert Shih5944a0b2021-02-10 04:26:33 -0800174status_t toStatusT_1_4(::V1_4::Status status);
175
176template<typename S>
177inline status_t toStatusT(S status) {
178 auto err = static_cast<::V1_4::Status>(status);
179 return toStatusT_1_4(err);
180}
181
182template<typename T>
183inline status_t toStatusT(const android::hardware::Return<T> &status) {
184 auto t = static_cast<T>(status);
185 auto err = static_cast<::V1_4::Status>(t);
186 return toStatusT_1_4(err);
187}
188
Kyle Zhang6605add2022-01-13 17:51:23 +0000189inline status_t toStatusTAidl(int32_t serviceError) {
190 auto status = static_cast<StatusAidl>(serviceError);
191 switch (status) {
192 case StatusAidl::OK:
193 return OK;
194 case StatusAidl::BAD_VALUE:
195 return BAD_VALUE;
196 case StatusAidl::ERROR_DRM_CANNOT_HANDLE:
197 return ERROR_DRM_CANNOT_HANDLE;
198 case StatusAidl::ERROR_DRM_DECRYPT:
199 return ERROR_DRM_DECRYPT;
200 case StatusAidl::ERROR_DRM_DEVICE_REVOKED:
201 return ERROR_DRM_DEVICE_REVOKED;
202 case StatusAidl::ERROR_DRM_FRAME_TOO_LARGE:
203 return ERROR_DRM_FRAME_TOO_LARGE;
204 case StatusAidl::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
205 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
206 case StatusAidl::ERROR_DRM_INSUFFICIENT_SECURITY:
207 return ERROR_DRM_INSUFFICIENT_SECURITY;
208 case StatusAidl::ERROR_DRM_INVALID_STATE:
209 return ERROR_DRM_INVALID_STATE;
210 case StatusAidl::ERROR_DRM_LICENSE_EXPIRED:
211 return ERROR_DRM_LICENSE_EXPIRED;
212 case StatusAidl::ERROR_DRM_NO_LICENSE:
213 return ERROR_DRM_NO_LICENSE;
214 case StatusAidl::ERROR_DRM_NOT_PROVISIONED:
215 return ERROR_DRM_NOT_PROVISIONED;
216 case StatusAidl::ERROR_DRM_RESOURCE_BUSY:
217 return ERROR_DRM_RESOURCE_BUSY;
218 case StatusAidl::ERROR_DRM_RESOURCE_CONTENTION:
219 return ERROR_DRM_RESOURCE_CONTENTION;
220 case StatusAidl::ERROR_DRM_SESSION_LOST_STATE:
221 return ERROR_DRM_SESSION_LOST_STATE;
222 case StatusAidl::ERROR_DRM_SESSION_NOT_OPENED:
223 return ERROR_DRM_SESSION_NOT_OPENED;
224
225 // New in S / drm@1.4:
226 case StatusAidl::CANNOT_DECRYPT_ZERO_SUBSAMPLES:
227 return ERROR_DRM_ZERO_SUBSAMPLES;
228 case StatusAidl::CRYPTO_LIBRARY_ERROR:
229 return ERROR_DRM_CRYPTO_LIBRARY;
230 case StatusAidl::GENERAL_OEM_ERROR:
231 return ERROR_DRM_GENERIC_OEM;
232 case StatusAidl::GENERAL_PLUGIN_ERROR:
233 return ERROR_DRM_GENERIC_PLUGIN;
234 case StatusAidl::INIT_DATA_INVALID:
235 return ERROR_DRM_INIT_DATA;
236 case StatusAidl::KEY_NOT_LOADED:
237 return ERROR_DRM_KEY_NOT_LOADED;
238 case StatusAidl::LICENSE_PARSE_ERROR:
239 return ERROR_DRM_LICENSE_PARSE;
240 case StatusAidl::LICENSE_POLICY_ERROR:
241 return ERROR_DRM_LICENSE_POLICY;
242 case StatusAidl::LICENSE_RELEASE_ERROR:
243 return ERROR_DRM_LICENSE_RELEASE;
244 case StatusAidl::LICENSE_REQUEST_REJECTED:
245 return ERROR_DRM_LICENSE_REQUEST_REJECTED;
246 case StatusAidl::LICENSE_RESTORE_ERROR:
247 return ERROR_DRM_LICENSE_RESTORE;
248 case StatusAidl::LICENSE_STATE_ERROR:
249 return ERROR_DRM_LICENSE_STATE;
250 case StatusAidl::MALFORMED_CERTIFICATE:
251 return ERROR_DRM_CERTIFICATE_MALFORMED;
252 case StatusAidl::MEDIA_FRAMEWORK_ERROR:
253 return ERROR_DRM_MEDIA_FRAMEWORK;
254 case StatusAidl::MISSING_CERTIFICATE:
255 return ERROR_DRM_CERTIFICATE_MISSING;
256 case StatusAidl::PROVISIONING_CERTIFICATE_ERROR:
257 return ERROR_DRM_PROVISIONING_CERTIFICATE;
258 case StatusAidl::PROVISIONING_CONFIGURATION_ERROR:
259 return ERROR_DRM_PROVISIONING_CONFIG;
260 case StatusAidl::PROVISIONING_PARSE_ERROR:
261 return ERROR_DRM_PROVISIONING_PARSE;
262 case StatusAidl::PROVISIONING_REQUEST_REJECTED:
263 return ERROR_DRM_PROVISIONING_REQUEST_REJECTED;
264 case StatusAidl::RETRYABLE_PROVISIONING_ERROR:
265 return ERROR_DRM_PROVISIONING_RETRY;
266 case StatusAidl::SECURE_STOP_RELEASE_ERROR:
267 return ERROR_DRM_SECURE_STOP_RELEASE;
268 case StatusAidl::STORAGE_READ_FAILURE:
269 return ERROR_DRM_STORAGE_READ;
270 case StatusAidl::STORAGE_WRITE_FAILURE:
271 return ERROR_DRM_STORAGE_WRITE;
272
273 case StatusAidl::ERROR_DRM_UNKNOWN:
274 default:
275 return ERROR_DRM_UNKNOWN;
276 }
277 return ERROR_DRM_UNKNOWN;
278}
279
280template<typename T, typename U>
281status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
282 std::shared_ptr<T> plugin = obj;
283 if (obj == NULL) {
284 LOG2BW("%s obj is null", U::descriptor);
285 } else if (plugin == NULL) {
286 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
287 }
288
289 std::vector<LogMessage> pluginLogsAidl;
290 if (plugin != NULL) {
291 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
292 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
293 }
294 }
295
296 std::vector<::V1_4::LogMessage> pluginLogs;
297 for (LogMessage log : pluginLogsAidl) {
298 ::V1_4::LogMessage logHidl;
299 logHidl.timeMs = log.timeMs;
300 // skip negative convert check as count of enum elements is 7
301 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
302 logHidl.message = log.message;
303 pluginLogs.push_back(logHidl);
304 }
305
306 auto allLogs(gLogBuf.getLogs());
307 LOG2BD("framework logs size %zu; plugin logs size %zu",
308 allLogs.size(), pluginLogs.size());
309 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
310 std::sort(allLogs.begin(), allLogs.end(),
311 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
312 return a.timeMs < b.timeMs;
313 });
314
315 logs.appendVector(allLogs);
316 return OK;
317}
318
Robert Shih9afca952021-02-12 01:36:06 -0800319template<typename T, typename U>
320status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
321 sp<T> plugin = T::castFrom(obj);
Robert Shih8635cb12021-02-26 07:57:55 -0800322 if (obj == NULL) {
323 LOG2BW("%s obj is null", U::descriptor);
324 } else if (plugin == NULL) {
325 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
Robert Shih9afca952021-02-12 01:36:06 -0800326 }
327
328 ::V1_4::Status err{};
Robert Shih8635cb12021-02-26 07:57:55 -0800329 std::vector<::V1_4::LogMessage> pluginLogs;
Robert Shih9afca952021-02-12 01:36:06 -0800330 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
331 ::V1_4::Status status,
332 hidl_vec<::V1_4::LogMessage> hLogs) {
Robert Shihfaae26a2021-02-20 00:01:19 -0800333 if (::V1_4::Status::OK != status) {
Robert Shih9afca952021-02-12 01:36:06 -0800334 err = status;
335 return;
336 }
Robert Shih8635cb12021-02-26 07:57:55 -0800337 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
Robert Shih9afca952021-02-12 01:36:06 -0800338 };
339
Robert Shih8635cb12021-02-26 07:57:55 -0800340 Return<void> hResult;
341 if (plugin != NULL) {
342 hResult = plugin->getLogMessages(cb);
Robert Shih9afca952021-02-12 01:36:06 -0800343 }
Robert Shih8635cb12021-02-26 07:57:55 -0800344 if (!hResult.isOk()) {
Robert Shihbd790122021-03-01 20:45:31 -0800345 LOG2BW("%s::getLogMessages remote call failed %s",
346 T::descriptor, hResult.description().c_str());
Robert Shih8635cb12021-02-26 07:57:55 -0800347 }
348
349 auto allLogs(gLogBuf.getLogs());
Robert Shih87aed812021-04-05 11:42:31 -0700350 LOG2BD("framework logs size %zu; plugin logs size %zu",
Robert Shihbd790122021-03-01 20:45:31 -0800351 allLogs.size(), pluginLogs.size());
Robert Shih8635cb12021-02-26 07:57:55 -0800352 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
353 std::sort(allLogs.begin(), allLogs.end(),
354 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
355 return a.timeMs < b.timeMs;
356 });
357
358 logs.appendVector(allLogs);
359 return OK;
Robert Shih9afca952021-02-12 01:36:06 -0800360}
361
Robert Shih8635cb12021-02-26 07:57:55 -0800362std::string GetExceptionMessage(status_t err, const char *msg,
363 const Vector<::V1_4::LogMessage> &logs);
Robert Shih0beba052021-02-14 00:47:00 -0800364
365template<typename T>
366std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
Robert Shih0beba052021-02-14 00:47:00 -0800367 Vector<::V1_4::LogMessage> logs;
Robert Shih8635cb12021-02-26 07:57:55 -0800368 iface->getLogMessages(logs);
369 return GetExceptionMessage(err, msg, logs);
Robert Shih0beba052021-02-14 00:47:00 -0800370}
371
Robert Shih28c2ed32019-10-27 22:55:12 -0700372} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700373} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700374#endif // ANDROID_DRMUTILS_H