blob: 14662222bf624bdb6313c694cd00c9e0ccbaee64 [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>
26#include <media/CryptoHal.h>
27#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>
32
33using ::android::hardware::drm::V1_0::BufferType;
34using ::android::hardware::drm::V1_0::DestinationBuffer;
35using ::android::hardware::drm::V1_0::ICryptoFactory;
36using ::android::hardware::drm::V1_0::ICryptoPlugin;
37using ::android::hardware::drm::V1_0::Mode;
38using ::android::hardware::drm::V1_0::Pattern;
39using ::android::hardware::drm::V1_0::SharedBuffer;
40using ::android::hardware::drm::V1_0::Status;
41using ::android::hardware::drm::V1_0::SubSample;
42using ::android::hardware::hidl_array;
43using ::android::hardware::hidl_handle;
44using ::android::hardware::hidl_memory;
45using ::android::hardware::hidl_string;
46using ::android::hardware::hidl_vec;
47using ::android::hardware::Return;
48using ::android::hardware::Void;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080049using ::android::hidl::manager::V1_0::IServiceManager;
Jeff Tinkera53d6552017-01-20 00:31:46 -080050using ::android::sp;
51
52
53namespace android {
54
55static status_t toStatusT(Status status) {
56 switch (status) {
57 case Status::OK:
58 return OK;
59 case Status::ERROR_DRM_NO_LICENSE:
60 return ERROR_DRM_NO_LICENSE;
61 case Status::ERROR_DRM_LICENSE_EXPIRED:
62 return ERROR_DRM_LICENSE_EXPIRED;
63 case Status::ERROR_DRM_RESOURCE_BUSY:
64 return ERROR_DRM_RESOURCE_BUSY;
65 case Status::ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
66 return ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION;
67 case Status::ERROR_DRM_SESSION_NOT_OPENED:
68 return ERROR_DRM_SESSION_NOT_OPENED;
69 case Status::ERROR_DRM_CANNOT_HANDLE:
70 return ERROR_DRM_CANNOT_HANDLE;
Jeff Tinkerd0cb8312017-03-15 11:17:00 -070071 case Status::ERROR_DRM_DECRYPT:
72 return ERROR_DRM_DECRYPT;
Jeff Tinkera53d6552017-01-20 00:31:46 -080073 default:
74 return UNKNOWN_ERROR;
75 }
76}
77
78
79static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
80 hidl_vec<uint8_t> vec;
81 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
82 return vec;
83}
84
85static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
86 hidl_vec<uint8_t> vec;
87 vec.resize(size);
88 memcpy(vec.data(), ptr, size);
89 return vec;
90}
91
92static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
93 if (!ptr) {
94 return hidl_array<uint8_t, 16>();
95 }
96 return hidl_array<uint8_t, 16>(ptr);
97}
98
99
Jeff Tinkera53d6552017-01-20 00:31:46 -0800100static String8 toString8(hidl_string hString) {
101 return String8(hString.c_str());
102}
103
104
105CryptoHal::CryptoHal()
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800106 : mFactories(makeCryptoFactories()),
107 mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT),
Jeff Tinker3cb53162017-02-16 12:06:32 -0800108 mNextBufferId(0) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800109}
110
111CryptoHal::~CryptoHal() {
112}
113
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800114Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() {
115 Vector<sp<ICryptoFactory>> factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800116
Jeff Tinker6a4921a2017-03-09 23:26:01 -0800117 auto manager = ::IServiceManager::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800118 if (manager != NULL) {
119 manager->listByInterface(ICryptoFactory::descriptor,
120 [&factories](const hidl_vec<hidl_string> &registered) {
121 for (const auto &instance : registered) {
122 auto factory = ICryptoFactory::getService(instance);
123 if (factory != NULL) {
124 factories.push_back(factory);
125 ALOGI("makeCryptoFactories: factory instance %s is %s",
126 instance.c_str(),
127 factory->isRemote() ? "Remote" : "Not Remote");
128 }
129 }
130 }
131 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800132 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800133
134 if (factories.size() == 0) {
135 // must be in passthrough mode, load the default passthrough service
136 auto passthrough = ICryptoFactory::getService("crypto");
137 if (passthrough != NULL) {
138 ALOGI("makeCryptoFactories: using default crypto instance");
139 factories.push_back(passthrough);
140 } else {
141 ALOGE("Failed to find any crypto factories");
142 }
143 }
144 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800145}
146
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800147sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory,
148 const uint8_t uuid[16], const void *initData, size_t initDataSize) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800149
150 sp<ICryptoPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800151 Return<void> hResult = factory->createPlugin(toHidlArray16(uuid),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800152 toHidlVec(initData, initDataSize),
153 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
154 if (status != Status::OK) {
155 ALOGE("Failed to make crypto plugin");
156 return;
157 }
158 plugin = hPlugin;
159 }
160 );
161 return plugin;
162}
163
164
165status_t CryptoHal::initCheck() const {
166 return mInitCheck;
167}
168
169
170bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
171 Mutex::Autolock autoLock(mLock);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800172
173 for (size_t i = 0; i < mFactories.size(); i++) {
174 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
175 return true;
176 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800177 }
178 return false;
179}
180
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800181status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
182 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800183 Mutex::Autolock autoLock(mLock);
184
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800185 for (size_t i = 0; i < mFactories.size(); i++) {
186 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
187 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
188 }
189 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800190
191 if (mPlugin == NULL) {
192 mInitCheck = ERROR_UNSUPPORTED;
193 } else {
194 mInitCheck = OK;
195 }
196
197 return mInitCheck;
198}
199
200status_t CryptoHal::destroyPlugin() {
201 Mutex::Autolock autoLock(mLock);
202
203 if (mInitCheck != OK) {
204 return mInitCheck;
205 }
206
207 mPlugin.clear();
208 return OK;
209}
210
211bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
212 Mutex::Autolock autoLock(mLock);
213
214 if (mInitCheck != OK) {
215 return mInitCheck;
216 }
217
218 return mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
219}
220
221
222/**
223 * If the heap base isn't set, get the heap base from the IMemory
224 * and send it to the HAL so it can map a remote heap of the same
225 * size. Once the heap base is established, shared memory buffers
226 * are sent by providing an offset into the heap and a buffer size.
227 */
Jeff Tinker3cb53162017-02-16 12:06:32 -0800228void CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {
229 native_handle_t* nativeHandle = native_handle_create(1, 0);
230 if (!nativeHandle) {
231 ALOGE("setSharedBufferBase(), failed to create native handle");
232 return;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800233 }
Jeff Tinker3cb53162017-02-16 12:06:32 -0800234 if (heap == NULL) {
235 ALOGE("setSharedBufferBase(): heap is NULL");
236 return;
237 }
238 int fd = heap->getHeapID();
239 nativeHandle->data[0] = fd;
240 auto hidlHandle = hidl_handle(nativeHandle);
241 auto hidlMemory = hidl_memory("ashmem", hidlHandle, heap->getSize());
242 mHeapBases.add(heap->getBase(), mNextBufferId);
243 Return<void> hResult = mPlugin->setSharedBufferBase(hidlMemory, mNextBufferId++);
244 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
245}
246
247status_t CryptoHal::toSharedBuffer(const sp<IMemory>& memory, ::SharedBuffer* buffer) {
248 ssize_t offset;
249 size_t size;
250
251 if (memory == NULL && buffer == NULL) {
252 return UNEXPECTED_NULL;
253 }
254
255 sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
256 if (heap == NULL) {
257 return UNEXPECTED_NULL;
258 }
259
260 if (mHeapBases.indexOfKey(heap->getBase()) < 0) {
261 setHeapBase(heap);
262 }
263
264 buffer->bufferId = mHeapBases.valueFor(heap->getBase());
265 buffer->offset = offset >= 0 ? offset : 0;
266 buffer->size = size;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800267 return OK;
268}
269
270ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
271 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
272 const sp<IMemory> &source, size_t offset,
273 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
274 const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
275 Mutex::Autolock autoLock(mLock);
276
277 if (mInitCheck != OK) {
278 return mInitCheck;
279 }
280
Jeff Tinkera53d6552017-01-20 00:31:46 -0800281 Mode hMode;
282 switch(mode) {
283 case CryptoPlugin::kMode_Unencrypted:
284 hMode = Mode::UNENCRYPTED ;
285 break;
286 case CryptoPlugin::kMode_AES_CTR:
287 hMode = Mode::AES_CTR;
288 break;
289 case CryptoPlugin::kMode_AES_WV:
290 hMode = Mode::AES_CBC_CTS;
291 break;
292 case CryptoPlugin::kMode_AES_CBC:
293 hMode = Mode::AES_CBC;
294 break;
295 default:
296 return UNKNOWN_ERROR;
297 }
298
299 Pattern hPattern;
300 hPattern.encryptBlocks = pattern.mEncryptBlocks;
301 hPattern.skipBlocks = pattern.mSkipBlocks;
302
303 std::vector<SubSample> stdSubSamples;
304 for (size_t i = 0; i < numSubSamples; i++) {
305 SubSample subSample;
306 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
307 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
308 stdSubSamples.push_back(subSample);
309 }
310 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
311
312 bool secure;
313 ::DestinationBuffer hDestination;
314 if (destination.mType == kDestinationTypeSharedMemory) {
315 hDestination.type = BufferType::SHARED_MEMORY;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800316 status_t status = toSharedBuffer(destination.mSharedMemory,
317 &hDestination.nonsecureMemory);
318 if (status != OK) {
319 return status;
320 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800321 secure = false;
322 } else {
323 hDestination.type = BufferType::NATIVE_HANDLE;
324 hDestination.secureMemory = hidl_handle(destination.mHandle);
325 secure = true;
326 }
327
Jeff Tinker3cb53162017-02-16 12:06:32 -0800328 ::SharedBuffer hSource;
329 status_t status = toSharedBuffer(source, &hSource);
330 if (status != OK) {
331 return status;
332 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800333
334 status_t err = UNKNOWN_ERROR;
335 uint32_t bytesWritten = 0;
336
337 Return<void> hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), hMode,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800338 hPattern, hSubSamples, hSource, offset, hDestination,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800339 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
340 if (status == Status::OK) {
341 bytesWritten = hBytesWritten;
342 *errorDetailMsg = toString8(hDetailedError);
343 }
344 err = toStatusT(status);
345 }
346 );
347
348 if (!hResult.isOk()) {
349 err = DEAD_OBJECT;
350 }
351
352 if (err == OK) {
353 return bytesWritten;
354 }
355 return err;
356}
357
358void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
359 Mutex::Autolock autoLock(mLock);
360
361 if (mInitCheck != OK) {
362 return;
363 }
364
365 mPlugin->notifyResolution(width, height);
366}
367
368status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
369 Mutex::Autolock autoLock(mLock);
370
371 if (mInitCheck != OK) {
372 return mInitCheck;
373 }
374
375 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
376}
377
378} // namespace android