blob: 61b51278ecb4511ce8b8a6527526c563edf4e2d6 [file] [log] [blame]
Jeff Tinkera53d6552017-01-20 00:31:46 -08001/*
2 * Copyright (C) 2017 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//#define LOG_NDEBUG 0
18#define LOG_TAG "CryptoHal"
19#include <utils/Log.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080020
21#include <android/hardware/drm/1.0/types.h>
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080022#include <android/hidl/manager/1.0/IServiceManager.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080023
24#include <binder/IMemory.h>
25#include <cutils/native_handle.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080026#include <hidlmemory/FrameworkUtils.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080027#include <media/hardware/CryptoAPI.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AString.h>
30#include <media/stagefright/foundation/hexdump.h>
31#include <media/stagefright/MediaErrors.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080032#include <mediadrm/CryptoHal.h>
33
Jeff Tinkera53d6552017-01-20 00:31:46 -080034
35using ::android::hardware::drm::V1_0::BufferType;
36using ::android::hardware::drm::V1_0::DestinationBuffer;
37using ::android::hardware::drm::V1_0::ICryptoFactory;
38using ::android::hardware::drm::V1_0::ICryptoPlugin;
39using ::android::hardware::drm::V1_0::Mode;
40using ::android::hardware::drm::V1_0::Pattern;
41using ::android::hardware::drm::V1_0::SharedBuffer;
42using ::android::hardware::drm::V1_0::Status;
43using ::android::hardware::drm::V1_0::SubSample;
44using ::android::hardware::hidl_array;
45using ::android::hardware::hidl_handle;
46using ::android::hardware::hidl_memory;
47using ::android::hardware::hidl_string;
48using ::android::hardware::hidl_vec;
49using ::android::hardware::Return;
50using ::android::hardware::Void;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080051using ::android::hidl::manager::V1_0::IServiceManager;
Jeff Tinkera53d6552017-01-20 00:31:46 -080052using ::android::sp;
53
54
55namespace android {
56
57static status_t toStatusT(Status status) {
58 switch (status) {
59 case Status::OK:
60 return OK;
61 case Status::ERROR_DRM_NO_LICENSE:
62 return ERROR_DRM_NO_LICENSE;
63 case Status::ERROR_DRM_LICENSE_EXPIRED:
64 return ERROR_DRM_LICENSE_EXPIRED;
65 case Status::ERROR_DRM_RESOURCE_BUSY:
66 return ERROR_DRM_RESOURCE_BUSY;
67 case Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
68 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
69 case Status::ERROR_DRM_SESSION_NOT_OPENED:
70 return ERROR_DRM_SESSION_NOT_OPENED;
71 case Status::ERROR_DRM_CANNOT_HANDLE:
72 return ERROR_DRM_CANNOT_HANDLE;
Jeff Tinkerd0cb8312017-03-15 11:17:00 -070073 case Status::ERROR_DRM_DECRYPT:
74 return ERROR_DRM_DECRYPT;
Jeff Tinkera53d6552017-01-20 00:31:46 -080075 default:
76 return UNKNOWN_ERROR;
77 }
78}
79
80
81static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
82 hidl_vec<uint8_t> vec;
83 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
84 return vec;
85}
86
87static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
88 hidl_vec<uint8_t> vec;
89 vec.resize(size);
90 memcpy(vec.data(), ptr, size);
91 return vec;
92}
93
94static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
95 if (!ptr) {
96 return hidl_array<uint8_t, 16>();
97 }
98 return hidl_array<uint8_t, 16>(ptr);
99}
100
101
Jeff Tinkera53d6552017-01-20 00:31:46 -0800102static String8 toString8(hidl_string hString) {
103 return String8(hString.c_str());
104}
105
106
107CryptoHal::CryptoHal()
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800108 : mFactories(makeCryptoFactories()),
109 mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT),
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700110 mNextBufferId(0),
111 mHeapSeqNum(0) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800112}
113
114CryptoHal::~CryptoHal() {
115}
116
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800117Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() {
118 Vector<sp<ICryptoFactory>> factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800119
Jeff Tinker6a4921a2017-03-09 23:26:01 -0800120 auto manager = ::IServiceManager::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800121 if (manager != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000122 manager->listByInterface(drm::V1_0::ICryptoFactory::descriptor,
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800123 [&factories](const hidl_vec<hidl_string> &registered) {
124 for (const auto &instance : registered) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000125 auto factory = drm::V1_0::ICryptoFactory::getService(instance);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800126 if (factory != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000127 ALOGD("found drm@1.0 ICryptoFactory %s", instance.c_str());
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800128 factories.push_back(factory);
Jeff Tinkere307dc42018-02-11 19:53:54 +0000129 }
130 }
131 }
132 );
133 manager->listByInterface(drm::V1_1::ICryptoFactory::descriptor,
134 [&factories](const hidl_vec<hidl_string> &registered) {
135 for (const auto &instance : registered) {
136 auto factory = drm::V1_1::ICryptoFactory::getService(instance);
137 if (factory != NULL) {
138 ALOGD("found drm@1.1 ICryptoFactory %s", instance.c_str());
139 factories.push_back(factory);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800140 }
141 }
142 }
143 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800144 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800145
146 if (factories.size() == 0) {
147 // must be in passthrough mode, load the default passthrough service
Jeff Tinkere309b222017-04-05 08:01:28 -0700148 auto passthrough = ICryptoFactory::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800149 if (passthrough != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000150 ALOGI("makeCryptoFactories: using default passthrough crypto instance");
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800151 factories.push_back(passthrough);
152 } else {
153 ALOGE("Failed to find any crypto factories");
154 }
155 }
156 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800157}
158
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800159sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory,
160 const uint8_t uuid[16], const void *initData, size_t initDataSize) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800161
162 sp<ICryptoPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800163 Return<void> hResult = factory->createPlugin(toHidlArray16(uuid),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800164 toHidlVec(initData, initDataSize),
165 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
166 if (status != Status::OK) {
167 ALOGE("Failed to make crypto plugin");
168 return;
169 }
170 plugin = hPlugin;
171 }
172 );
173 return plugin;
174}
175
176
177status_t CryptoHal::initCheck() const {
178 return mInitCheck;
179}
180
181
182bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
183 Mutex::Autolock autoLock(mLock);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800184
185 for (size_t i = 0; i < mFactories.size(); i++) {
186 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
187 return true;
188 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800189 }
190 return false;
191}
192
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800193status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
194 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800195 Mutex::Autolock autoLock(mLock);
196
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800197 for (size_t i = 0; i < mFactories.size(); i++) {
198 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
199 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
200 }
201 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800202
203 if (mPlugin == NULL) {
204 mInitCheck = ERROR_UNSUPPORTED;
205 } else {
206 mInitCheck = OK;
207 }
208
209 return mInitCheck;
210}
211
212status_t CryptoHal::destroyPlugin() {
213 Mutex::Autolock autoLock(mLock);
214
215 if (mInitCheck != OK) {
216 return mInitCheck;
217 }
218
219 mPlugin.clear();
220 return OK;
221}
222
223bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
224 Mutex::Autolock autoLock(mLock);
225
226 if (mInitCheck != OK) {
227 return mInitCheck;
228 }
229
230 return mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
231}
232
233
234/**
235 * If the heap base isn't set, get the heap base from the IMemory
236 * and send it to the HAL so it can map a remote heap of the same
237 * size. Once the heap base is established, shared memory buffers
238 * are sent by providing an offset into the heap and a buffer size.
239 */
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700240int32_t CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {
Steven Moreland65366062017-10-05 11:34:01 -0700241 using ::android::hardware::fromHeap;
242 using ::android::hardware::HidlMemory;
243
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700244 if (heap == NULL) {
245 ALOGE("setHeapBase(): heap is NULL");
246 return -1;
247 }
Jeff Tinker3cb53162017-02-16 12:06:32 -0800248 native_handle_t* nativeHandle = native_handle_create(1, 0);
249 if (!nativeHandle) {
Chong Zhangd07c9272017-03-28 11:02:06 -0700250 ALOGE("setHeapBase(), failed to create native handle");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700251 return -1;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800252 }
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700253
254 Mutex::Autolock autoLock(mLock);
255
256 int32_t seqNum = mHeapSeqNum++;
Steven Moreland65366062017-10-05 11:34:01 -0700257 sp<HidlMemory> hidlMemory = fromHeap(heap);
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700258 mHeapBases.add(seqNum, mNextBufferId);
Steven Moreland65366062017-10-05 11:34:01 -0700259 Return<void> hResult = mPlugin->setSharedBufferBase(*hidlMemory, mNextBufferId++);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800260 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700261 return seqNum;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800262}
263
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700264void CryptoHal::clearHeapBase(int32_t seqNum) {
265 Mutex::Autolock autoLock(mLock);
266
267 mHeapBases.removeItem(seqNum);
Chong Zhangd07c9272017-03-28 11:02:06 -0700268}
269
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700270status_t CryptoHal::toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer) {
Jeff Tinker3cb53162017-02-16 12:06:32 -0800271 ssize_t offset;
272 size_t size;
273
274 if (memory == NULL && buffer == NULL) {
275 return UNEXPECTED_NULL;
276 }
277
278 sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
279 if (heap == NULL) {
280 return UNEXPECTED_NULL;
281 }
282
Chong Zhangd07c9272017-03-28 11:02:06 -0700283 // memory must be in the declared heap
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700284 CHECK(mHeapBases.indexOfKey(seqNum) >= 0);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800285
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700286 buffer->bufferId = mHeapBases.valueFor(seqNum);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800287 buffer->offset = offset >= 0 ? offset : 0;
288 buffer->size = size;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800289 return OK;
290}
291
292ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
293 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700294 const ICrypto::SourceBuffer &source, size_t offset,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800295 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
296 const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
297 Mutex::Autolock autoLock(mLock);
298
299 if (mInitCheck != OK) {
300 return mInitCheck;
301 }
302
Jeff Tinkera53d6552017-01-20 00:31:46 -0800303 Mode hMode;
304 switch(mode) {
305 case CryptoPlugin::kMode_Unencrypted:
306 hMode = Mode::UNENCRYPTED ;
307 break;
308 case CryptoPlugin::kMode_AES_CTR:
309 hMode = Mode::AES_CTR;
310 break;
311 case CryptoPlugin::kMode_AES_WV:
312 hMode = Mode::AES_CBC_CTS;
313 break;
314 case CryptoPlugin::kMode_AES_CBC:
315 hMode = Mode::AES_CBC;
316 break;
317 default:
318 return UNKNOWN_ERROR;
319 }
320
321 Pattern hPattern;
322 hPattern.encryptBlocks = pattern.mEncryptBlocks;
323 hPattern.skipBlocks = pattern.mSkipBlocks;
324
325 std::vector<SubSample> stdSubSamples;
326 for (size_t i = 0; i < numSubSamples; i++) {
327 SubSample subSample;
328 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
329 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
330 stdSubSamples.push_back(subSample);
331 }
332 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
333
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700334 int32_t heapSeqNum = source.mHeapSeqNum;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800335 bool secure;
336 ::DestinationBuffer hDestination;
337 if (destination.mType == kDestinationTypeSharedMemory) {
338 hDestination.type = BufferType::SHARED_MEMORY;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700339 status_t status = toSharedBuffer(destination.mSharedMemory, heapSeqNum,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800340 &hDestination.nonsecureMemory);
341 if (status != OK) {
342 return status;
343 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800344 secure = false;
Jeff Tinker24420472018-01-11 17:46:16 -0800345 } else if (destination.mType == kDestinationTypeNativeHandle) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800346 hDestination.type = BufferType::NATIVE_HANDLE;
347 hDestination.secureMemory = hidl_handle(destination.mHandle);
348 secure = true;
Jeff Tinker24420472018-01-11 17:46:16 -0800349 } else {
350 android_errorWriteLog(0x534e4554, "70526702");
351 return UNKNOWN_ERROR;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800352 }
353
Jeff Tinker3cb53162017-02-16 12:06:32 -0800354 ::SharedBuffer hSource;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700355 status_t status = toSharedBuffer(source.mSharedMemory, heapSeqNum, &hSource);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800356 if (status != OK) {
357 return status;
358 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800359
360 status_t err = UNKNOWN_ERROR;
361 uint32_t bytesWritten = 0;
362
363 Return<void> hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), hMode,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800364 hPattern, hSubSamples, hSource, offset, hDestination,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800365 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
366 if (status == Status::OK) {
367 bytesWritten = hBytesWritten;
368 *errorDetailMsg = toString8(hDetailedError);
369 }
370 err = toStatusT(status);
371 }
372 );
373
374 if (!hResult.isOk()) {
375 err = DEAD_OBJECT;
376 }
377
378 if (err == OK) {
379 return bytesWritten;
380 }
381 return err;
382}
383
384void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
385 Mutex::Autolock autoLock(mLock);
386
387 if (mInitCheck != OK) {
388 return;
389 }
390
391 mPlugin->notifyResolution(width, height);
392}
393
394status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
395 Mutex::Autolock autoLock(mLock);
396
397 if (mInitCheck != OK) {
398 return mInitCheck;
399 }
400
401 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
402}
403
404} // namespace android