blob: f229751226a5763436728f90bafb3abe183f6214 [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>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080025#include <hidlmemory/FrameworkUtils.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080026#include <media/hardware/CryptoAPI.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/foundation/AString.h>
29#include <media/stagefright/foundation/hexdump.h>
30#include <media/stagefright/MediaErrors.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080031#include <mediadrm/CryptoHal.h>
32
Jeff Tinkera53d6552017-01-20 00:31:46 -080033
34using ::android::hardware::drm::V1_0::BufferType;
35using ::android::hardware::drm::V1_0::DestinationBuffer;
36using ::android::hardware::drm::V1_0::ICryptoFactory;
37using ::android::hardware::drm::V1_0::ICryptoPlugin;
38using ::android::hardware::drm::V1_0::Mode;
39using ::android::hardware::drm::V1_0::Pattern;
40using ::android::hardware::drm::V1_0::SharedBuffer;
41using ::android::hardware::drm::V1_0::Status;
42using ::android::hardware::drm::V1_0::SubSample;
43using ::android::hardware::hidl_array;
44using ::android::hardware::hidl_handle;
45using ::android::hardware::hidl_memory;
46using ::android::hardware::hidl_string;
47using ::android::hardware::hidl_vec;
48using ::android::hardware::Return;
49using ::android::hardware::Void;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080050using ::android::hidl::manager::V1_0::IServiceManager;
Jeff Tinkera53d6552017-01-20 00:31:46 -080051using ::android::sp;
52
53
54namespace android {
55
56static status_t toStatusT(Status status) {
57 switch (status) {
58 case Status::OK:
59 return OK;
60 case Status::ERROR_DRM_NO_LICENSE:
61 return ERROR_DRM_NO_LICENSE;
62 case Status::ERROR_DRM_LICENSE_EXPIRED:
63 return ERROR_DRM_LICENSE_EXPIRED;
64 case Status::ERROR_DRM_RESOURCE_BUSY:
65 return ERROR_DRM_RESOURCE_BUSY;
66 case Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
67 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
68 case Status::ERROR_DRM_SESSION_NOT_OPENED:
69 return ERROR_DRM_SESSION_NOT_OPENED;
70 case Status::ERROR_DRM_CANNOT_HANDLE:
71 return ERROR_DRM_CANNOT_HANDLE;
Jeff Tinkerd0cb8312017-03-15 11:17:00 -070072 case Status::ERROR_DRM_DECRYPT:
73 return ERROR_DRM_DECRYPT;
Jeff Tinkera53d6552017-01-20 00:31:46 -080074 default:
75 return UNKNOWN_ERROR;
76 }
77}
78
79
80static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
81 hidl_vec<uint8_t> vec;
82 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
83 return vec;
84}
85
86static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
87 hidl_vec<uint8_t> vec;
88 vec.resize(size);
89 memcpy(vec.data(), ptr, size);
90 return vec;
91}
92
93static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
94 if (!ptr) {
95 return hidl_array<uint8_t, 16>();
96 }
97 return hidl_array<uint8_t, 16>(ptr);
98}
99
100
Jeff Tinkera53d6552017-01-20 00:31:46 -0800101static String8 toString8(hidl_string hString) {
102 return String8(hString.c_str());
103}
104
105
106CryptoHal::CryptoHal()
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800107 : mFactories(makeCryptoFactories()),
108 mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT),
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700109 mNextBufferId(0),
110 mHeapSeqNum(0) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800111}
112
113CryptoHal::~CryptoHal() {
114}
115
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800116Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() {
117 Vector<sp<ICryptoFactory>> factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800118
Jeff Tinker6a4921a2017-03-09 23:26:01 -0800119 auto manager = ::IServiceManager::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800120 if (manager != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000121 manager->listByInterface(drm::V1_0::ICryptoFactory::descriptor,
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800122 [&factories](const hidl_vec<hidl_string> &registered) {
123 for (const auto &instance : registered) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000124 auto factory = drm::V1_0::ICryptoFactory::getService(instance);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800125 if (factory != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000126 ALOGD("found drm@1.0 ICryptoFactory %s", instance.c_str());
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800127 factories.push_back(factory);
Jeff Tinkere307dc42018-02-11 19:53:54 +0000128 }
129 }
130 }
131 );
132 manager->listByInterface(drm::V1_1::ICryptoFactory::descriptor,
133 [&factories](const hidl_vec<hidl_string> &registered) {
134 for (const auto &instance : registered) {
135 auto factory = drm::V1_1::ICryptoFactory::getService(instance);
136 if (factory != NULL) {
137 ALOGD("found drm@1.1 ICryptoFactory %s", instance.c_str());
138 factories.push_back(factory);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800139 }
140 }
141 }
142 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800143 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800144
145 if (factories.size() == 0) {
146 // must be in passthrough mode, load the default passthrough service
Jeff Tinkere309b222017-04-05 08:01:28 -0700147 auto passthrough = ICryptoFactory::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800148 if (passthrough != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000149 ALOGI("makeCryptoFactories: using default passthrough crypto instance");
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800150 factories.push_back(passthrough);
151 } else {
152 ALOGE("Failed to find any crypto factories");
153 }
154 }
155 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800156}
157
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800158sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory,
159 const uint8_t uuid[16], const void *initData, size_t initDataSize) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800160
161 sp<ICryptoPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800162 Return<void> hResult = factory->createPlugin(toHidlArray16(uuid),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800163 toHidlVec(initData, initDataSize),
164 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
165 if (status != Status::OK) {
166 ALOGE("Failed to make crypto plugin");
167 return;
168 }
169 plugin = hPlugin;
170 }
171 );
172 return plugin;
173}
174
175
176status_t CryptoHal::initCheck() const {
177 return mInitCheck;
178}
179
180
181bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
182 Mutex::Autolock autoLock(mLock);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800183
184 for (size_t i = 0; i < mFactories.size(); i++) {
185 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
186 return true;
187 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800188 }
189 return false;
190}
191
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800192status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
193 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800194 Mutex::Autolock autoLock(mLock);
195
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800196 for (size_t i = 0; i < mFactories.size(); i++) {
197 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
198 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
199 }
200 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800201
202 if (mPlugin == NULL) {
203 mInitCheck = ERROR_UNSUPPORTED;
204 } else {
205 mInitCheck = OK;
206 }
207
208 return mInitCheck;
209}
210
211status_t CryptoHal::destroyPlugin() {
212 Mutex::Autolock autoLock(mLock);
213
214 if (mInitCheck != OK) {
215 return mInitCheck;
216 }
217
218 mPlugin.clear();
219 return OK;
220}
221
222bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
223 Mutex::Autolock autoLock(mLock);
224
225 if (mInitCheck != OK) {
226 return mInitCheck;
227 }
228
229 return mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
230}
231
232
233/**
234 * If the heap base isn't set, get the heap base from the IMemory
235 * and send it to the HAL so it can map a remote heap of the same
236 * size. Once the heap base is established, shared memory buffers
237 * are sent by providing an offset into the heap and a buffer size.
238 */
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700239int32_t CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {
Steven Moreland65366062017-10-05 11:34:01 -0700240 using ::android::hardware::fromHeap;
241 using ::android::hardware::HidlMemory;
242
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700243 if (heap == NULL) {
244 ALOGE("setHeapBase(): heap is NULL");
245 return -1;
246 }
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700247
248 Mutex::Autolock autoLock(mLock);
249
250 int32_t seqNum = mHeapSeqNum++;
Steven Moreland65366062017-10-05 11:34:01 -0700251 sp<HidlMemory> hidlMemory = fromHeap(heap);
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700252 mHeapBases.add(seqNum, mNextBufferId);
Steven Moreland65366062017-10-05 11:34:01 -0700253 Return<void> hResult = mPlugin->setSharedBufferBase(*hidlMemory, mNextBufferId++);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800254 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700255 return seqNum;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800256}
257
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700258void CryptoHal::clearHeapBase(int32_t seqNum) {
259 Mutex::Autolock autoLock(mLock);
260
261 mHeapBases.removeItem(seqNum);
Chong Zhangd07c9272017-03-28 11:02:06 -0700262}
263
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700264status_t CryptoHal::toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer) {
Jeff Tinker3cb53162017-02-16 12:06:32 -0800265 ssize_t offset;
266 size_t size;
267
268 if (memory == NULL && buffer == NULL) {
269 return UNEXPECTED_NULL;
270 }
271
272 sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
273 if (heap == NULL) {
274 return UNEXPECTED_NULL;
275 }
276
Chong Zhangd07c9272017-03-28 11:02:06 -0700277 // memory must be in the declared heap
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700278 CHECK(mHeapBases.indexOfKey(seqNum) >= 0);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800279
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700280 buffer->bufferId = mHeapBases.valueFor(seqNum);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800281 buffer->offset = offset >= 0 ? offset : 0;
282 buffer->size = size;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800283 return OK;
284}
285
286ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
287 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700288 const ICrypto::SourceBuffer &source, size_t offset,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800289 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
290 const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
291 Mutex::Autolock autoLock(mLock);
292
293 if (mInitCheck != OK) {
294 return mInitCheck;
295 }
296
Jeff Tinkera53d6552017-01-20 00:31:46 -0800297 Mode hMode;
298 switch(mode) {
299 case CryptoPlugin::kMode_Unencrypted:
300 hMode = Mode::UNENCRYPTED ;
301 break;
302 case CryptoPlugin::kMode_AES_CTR:
303 hMode = Mode::AES_CTR;
304 break;
305 case CryptoPlugin::kMode_AES_WV:
306 hMode = Mode::AES_CBC_CTS;
307 break;
308 case CryptoPlugin::kMode_AES_CBC:
309 hMode = Mode::AES_CBC;
310 break;
311 default:
312 return UNKNOWN_ERROR;
313 }
314
315 Pattern hPattern;
316 hPattern.encryptBlocks = pattern.mEncryptBlocks;
317 hPattern.skipBlocks = pattern.mSkipBlocks;
318
319 std::vector<SubSample> stdSubSamples;
320 for (size_t i = 0; i < numSubSamples; i++) {
321 SubSample subSample;
322 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
323 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
324 stdSubSamples.push_back(subSample);
325 }
326 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
327
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700328 int32_t heapSeqNum = source.mHeapSeqNum;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800329 bool secure;
330 ::DestinationBuffer hDestination;
331 if (destination.mType == kDestinationTypeSharedMemory) {
332 hDestination.type = BufferType::SHARED_MEMORY;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700333 status_t status = toSharedBuffer(destination.mSharedMemory, heapSeqNum,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800334 &hDestination.nonsecureMemory);
335 if (status != OK) {
336 return status;
337 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800338 secure = false;
Jeff Tinker24420472018-01-11 17:46:16 -0800339 } else if (destination.mType == kDestinationTypeNativeHandle) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800340 hDestination.type = BufferType::NATIVE_HANDLE;
341 hDestination.secureMemory = hidl_handle(destination.mHandle);
342 secure = true;
Jeff Tinker24420472018-01-11 17:46:16 -0800343 } else {
344 android_errorWriteLog(0x534e4554, "70526702");
345 return UNKNOWN_ERROR;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800346 }
347
Jeff Tinker3cb53162017-02-16 12:06:32 -0800348 ::SharedBuffer hSource;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700349 status_t status = toSharedBuffer(source.mSharedMemory, heapSeqNum, &hSource);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800350 if (status != OK) {
351 return status;
352 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800353
354 status_t err = UNKNOWN_ERROR;
355 uint32_t bytesWritten = 0;
356
357 Return<void> hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), hMode,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800358 hPattern, hSubSamples, hSource, offset, hDestination,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800359 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
360 if (status == Status::OK) {
361 bytesWritten = hBytesWritten;
362 *errorDetailMsg = toString8(hDetailedError);
363 }
364 err = toStatusT(status);
365 }
366 );
367
368 if (!hResult.isOk()) {
369 err = DEAD_OBJECT;
370 }
371
372 if (err == OK) {
373 return bytesWritten;
374 }
375 return err;
376}
377
378void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
379 Mutex::Autolock autoLock(mLock);
380
381 if (mInitCheck != OK) {
382 return;
383 }
384
385 mPlugin->notifyResolution(width, height);
386}
387
388status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
389 Mutex::Autolock autoLock(mLock);
390
391 if (mInitCheck != OK) {
392 return mInitCheck;
393 }
394
395 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
396}
397
398} // namespace android