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 #ifndef ANDROID_DRMUTILS_H
18 #define ANDROID_DRMUTILS_H
19
20 #include <android/hardware/drm/1.0/ICryptoFactory.h>
21 #include <android/hardware/drm/1.0/IDrmFactory.h>
22 #include <android/hardware/drm/1.4/IDrmPlugin.h>
23 #include <android/hardware/drm/1.4/types.h>
24 #include <media/stagefright/MediaErrors.h>
25 #include <utils/Errors.h> // for status_t
26 #include <utils/Log.h>
27 #include <utils/String8.h>
28 #include <utils/StrongPointer.h>
29 #include <utils/Vector.h>
30 #include <algorithm>
31 #include <chrono>
32 #include <cstddef>
33 #include <cstdint>
34 #include <cstring>
35 #include <ctime>
36 #include <deque>
37 #include <endian.h>
38 #include <iterator>
39 #include <mutex>
40 #include <string>
41 #include <vector>
42 #include <aidl/android/hardware/drm/LogMessage.h>
43 #include <aidl/android/hardware/drm/Status.h>
44 #include <aidl/android/hardware/drm/IDrmFactory.h>
45
46 using namespace ::android::hardware::drm;
47 using ::android::hardware::hidl_vec;
48 using ::android::hardware::Return;
49
50 using ::aidl::android::hardware::drm::LogPriority;
51 using ::aidl::android::hardware::drm::LogMessage;
52 using ::aidl::android::hardware::drm::Uuid;
53 using StatusAidl = ::aidl::android::hardware::drm::Status;
54 using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
55
56 namespace android {
57
58 struct ICrypto;
59 struct IDrm;
60
61 namespace DrmUtils {
62
63 // Log APIs
64 class 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
75 extern LogBuffer gLogBuf;
76
formatBuffer(char * buf,size_t size,const char * msg)77 static inline int formatBuffer(char *buf, size_t size, const char *msg) {
78 return snprintf(buf, size, "%s", msg);
79 }
80
81 template <typename First, typename... Args>
formatBuffer(char * buf,size_t size,const char * fmt,First first,Args...args)82 static 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
86 template <typename... Args>
LogToBuffer(android_LogPriority level,const char * fmt,Args...args)87 void 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
102 template <typename... Args>
LogToBuffer(android_LogPriority level,const uint8_t uuid[16],const char * fmt,Args...args)103 void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
104 uint64_t uuid2[2] = {};
105 std::memcpy(uuid2, uuid, sizeof(uuid2));
106 std::string uuidFmt("uuid=[%lx %lx] ");
107 uuidFmt += fmt;
108 LogToBuffer(level, uuidFmt.c_str(), htobe64(uuid2[0]), htobe64(uuid2[1]), args...);
109 }
110
111 #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
119 bool UseDrmService();
120
121 sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
122
123 sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
124
125 template<typename BA, typename PARCEL>
WriteByteArray(PARCEL & obj,const BA & vec)126 void WriteByteArray(PARCEL &obj, const BA &vec) {
127 obj.writeInt32(vec.size());
128 if (vec.size()) {
129 obj.write(vec.data(), vec.size());
130 }
131 }
132
133 template<typename ET, typename BA, typename PARCEL>
WriteEventToParcel(PARCEL & obj,ET eventType,const BA & sessionId,const BA & data)134 void WriteEventToParcel(
135 PARCEL &obj,
136 ET eventType,
137 const BA &sessionId,
138 const BA &data) {
139 WriteByteArray(obj, sessionId);
140 WriteByteArray(obj, data);
141 obj.writeInt32(eventType);
142 }
143
144 template<typename BA, typename PARCEL>
WriteExpirationUpdateToParcel(PARCEL & obj,const BA & sessionId,int64_t expiryTimeInMS)145 void WriteExpirationUpdateToParcel(
146 PARCEL &obj,
147 const BA &sessionId,
148 int64_t expiryTimeInMS) {
149 WriteByteArray(obj, sessionId);
150 obj.writeInt64(expiryTimeInMS);
151 }
152
153 template<typename BA, typename KSL, typename PARCEL>
WriteKeysChange(PARCEL & obj,const BA & sessionId,const KSL & keyStatusList,bool hasNewUsableKey)154 void WriteKeysChange(
155 PARCEL &obj,
156 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
toAidlUuid(const uint8_t uuid[16])168 inline 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
174 std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
175
176 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
177
178 std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
179 const char *appPackageName);
180
181 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
182
183 std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
184 const void *initData, size_t initDataSize);
185
186 status_t toStatusT_1_4(::V1_4::Status status);
187
188 template<typename S>
toStatusT(S status)189 inline status_t toStatusT(S status) {
190 auto err = static_cast<::V1_4::Status>(status);
191 return toStatusT_1_4(err);
192 }
193
194 template<typename T>
toStatusT(const android::hardware::Return<T> & status)195 inline 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
statusAidlToStatusT(::ndk::ScopedAStatus & statusAidl)201 inline 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());
205 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
294 template<typename T, typename U>
GetLogMessagesAidl(const std::shared_ptr<U> & obj,Vector<::V1_4::LogMessage> & logs)295 status_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
333 template<typename T, typename U>
GetLogMessages(const sp<U> & obj,Vector<::V1_4::LogMessage> & logs)334 status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
335 sp<T> plugin = T::castFrom(obj);
336 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);
340 }
341
342 ::V1_4::Status err{};
343 std::vector<::V1_4::LogMessage> pluginLogs;
344 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
345 ::V1_4::Status status,
346 hidl_vec<::V1_4::LogMessage> hLogs) {
347 if (::V1_4::Status::OK != status) {
348 err = status;
349 return;
350 }
351 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
352 };
353
354 Return<void> hResult;
355 if (plugin != NULL) {
356 hResult = plugin->getLogMessages(cb);
357 }
358 if (!hResult.isOk()) {
359 LOG2BW("%s::getLogMessages remote call failed %s",
360 T::descriptor, hResult.description().c_str());
361 }
362
363 auto allLogs(gLogBuf.getLogs());
364 LOG2BD("framework logs size %zu; plugin logs size %zu",
365 allLogs.size(), pluginLogs.size());
366 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;
374 }
375
376 std::string GetExceptionMessage(status_t err, const char *msg,
377 const Vector<::V1_4::LogMessage> &logs);
378
379 template<typename T>
GetExceptionMessage(status_t err,const char * msg,const sp<T> & iface)380 std::string GetExceptionMessage(status_t err, const char *msg, const sp<T> &iface) {
381 Vector<::V1_4::LogMessage> logs;
382 iface->getLogMessages(logs);
383 return GetExceptionMessage(err, msg, logs);
384 }
385
386 } // namespace DrmUtils
387 } // namespace android
388 #endif // ANDROID_DRMUTILS_H
389