blob: 25c16da48554834fc98c3fecb5584098a7941053 [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),
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700108 mNextBufferId(0),
109 mHeapSeqNum(0) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800110}
111
112CryptoHal::~CryptoHal() {
113}
114
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800115Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() {
116 Vector<sp<ICryptoFactory>> factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800117
Jeff Tinker6a4921a2017-03-09 23:26:01 -0800118 auto manager = ::IServiceManager::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800119 if (manager != NULL) {
120 manager->listByInterface(ICryptoFactory::descriptor,
121 [&factories](const hidl_vec<hidl_string> &registered) {
122 for (const auto &instance : registered) {
123 auto factory = ICryptoFactory::getService(instance);
124 if (factory != NULL) {
125 factories.push_back(factory);
126 ALOGI("makeCryptoFactories: factory instance %s is %s",
127 instance.c_str(),
128 factory->isRemote() ? "Remote" : "Not Remote");
129 }
130 }
131 }
132 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800133 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800134
135 if (factories.size() == 0) {
136 // must be in passthrough mode, load the default passthrough service
137 auto passthrough = ICryptoFactory::getService("crypto");
138 if (passthrough != NULL) {
139 ALOGI("makeCryptoFactories: using default crypto instance");
140 factories.push_back(passthrough);
141 } else {
142 ALOGE("Failed to find any crypto factories");
143 }
144 }
145 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800146}
147
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800148sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory,
149 const uint8_t uuid[16], const void *initData, size_t initDataSize) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800150
151 sp<ICryptoPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800152 Return<void> hResult = factory->createPlugin(toHidlArray16(uuid),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800153 toHidlVec(initData, initDataSize),
154 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
155 if (status != Status::OK) {
156 ALOGE("Failed to make crypto plugin");
157 return;
158 }
159 plugin = hPlugin;
160 }
161 );
162 return plugin;
163}
164
165
166status_t CryptoHal::initCheck() const {
167 return mInitCheck;
168}
169
170
171bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
172 Mutex::Autolock autoLock(mLock);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800173
174 for (size_t i = 0; i < mFactories.size(); i++) {
175 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
176 return true;
177 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800178 }
179 return false;
180}
181
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800182status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
183 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800184 Mutex::Autolock autoLock(mLock);
185
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800186 for (size_t i = 0; i < mFactories.size(); i++) {
187 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
188 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
189 }
190 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800191
192 if (mPlugin == NULL) {
193 mInitCheck = ERROR_UNSUPPORTED;
194 } else {
195 mInitCheck = OK;
196 }
197
198 return mInitCheck;
199}
200
201status_t CryptoHal::destroyPlugin() {
202 Mutex::Autolock autoLock(mLock);
203
204 if (mInitCheck != OK) {
205 return mInitCheck;
206 }
207
208 mPlugin.clear();
209 return OK;
210}
211
212bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
213 Mutex::Autolock autoLock(mLock);
214
215 if (mInitCheck != OK) {
216 return mInitCheck;
217 }
218
219 return mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
220}
221
222
223/**
224 * If the heap base isn't set, get the heap base from the IMemory
225 * and send it to the HAL so it can map a remote heap of the same
226 * size. Once the heap base is established, shared memory buffers
227 * are sent by providing an offset into the heap and a buffer size.
228 */
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700229int32_t CryptoHal::setHeapBase(const sp<IMemoryHeap>& heap) {
230 if (heap == NULL) {
231 ALOGE("setHeapBase(): heap is NULL");
232 return -1;
233 }
Jeff Tinker3cb53162017-02-16 12:06:32 -0800234 native_handle_t* nativeHandle = native_handle_create(1, 0);
235 if (!nativeHandle) {
Chong Zhangd07c9272017-03-28 11:02:06 -0700236 ALOGE("setHeapBase(), failed to create native handle");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700237 return -1;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800238 }
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700239
240 Mutex::Autolock autoLock(mLock);
241
242 int32_t seqNum = mHeapSeqNum++;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800243 int fd = heap->getHeapID();
244 nativeHandle->data[0] = fd;
245 auto hidlHandle = hidl_handle(nativeHandle);
246 auto hidlMemory = hidl_memory("ashmem", hidlHandle, heap->getSize());
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700247 mHeapBases.add(seqNum, mNextBufferId);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800248 Return<void> hResult = mPlugin->setSharedBufferBase(hidlMemory, mNextBufferId++);
249 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700250 return seqNum;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800251}
252
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700253void CryptoHal::clearHeapBase(int32_t seqNum) {
254 Mutex::Autolock autoLock(mLock);
255
256 mHeapBases.removeItem(seqNum);
Chong Zhangd07c9272017-03-28 11:02:06 -0700257}
258
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700259status_t CryptoHal::toSharedBuffer(const sp<IMemory>& memory, int32_t seqNum, ::SharedBuffer* buffer) {
Jeff Tinker3cb53162017-02-16 12:06:32 -0800260 ssize_t offset;
261 size_t size;
262
263 if (memory == NULL && buffer == NULL) {
264 return UNEXPECTED_NULL;
265 }
266
267 sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
268 if (heap == NULL) {
269 return UNEXPECTED_NULL;
270 }
271
Chong Zhangd07c9272017-03-28 11:02:06 -0700272 // memory must be in the declared heap
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700273 CHECK(mHeapBases.indexOfKey(seqNum) >= 0);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800274
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700275 buffer->bufferId = mHeapBases.valueFor(seqNum);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800276 buffer->offset = offset >= 0 ? offset : 0;
277 buffer->size = size;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800278 return OK;
279}
280
281ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
282 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700283 const ICrypto::SourceBuffer &source, size_t offset,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800284 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
285 const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
286 Mutex::Autolock autoLock(mLock);
287
288 if (mInitCheck != OK) {
289 return mInitCheck;
290 }
291
Jeff Tinkera53d6552017-01-20 00:31:46 -0800292 Mode hMode;
293 switch(mode) {
294 case CryptoPlugin::kMode_Unencrypted:
295 hMode = Mode::UNENCRYPTED ;
296 break;
297 case CryptoPlugin::kMode_AES_CTR:
298 hMode = Mode::AES_CTR;
299 break;
300 case CryptoPlugin::kMode_AES_WV:
301 hMode = Mode::AES_CBC_CTS;
302 break;
303 case CryptoPlugin::kMode_AES_CBC:
304 hMode = Mode::AES_CBC;
305 break;
306 default:
307 return UNKNOWN_ERROR;
308 }
309
310 Pattern hPattern;
311 hPattern.encryptBlocks = pattern.mEncryptBlocks;
312 hPattern.skipBlocks = pattern.mSkipBlocks;
313
314 std::vector<SubSample> stdSubSamples;
315 for (size_t i = 0; i < numSubSamples; i++) {
316 SubSample subSample;
317 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
318 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
319 stdSubSamples.push_back(subSample);
320 }
321 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
322
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700323 int32_t heapSeqNum = source.mHeapSeqNum;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800324 bool secure;
325 ::DestinationBuffer hDestination;
326 if (destination.mType == kDestinationTypeSharedMemory) {
327 hDestination.type = BufferType::SHARED_MEMORY;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700328 status_t status = toSharedBuffer(destination.mSharedMemory, heapSeqNum,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800329 &hDestination.nonsecureMemory);
330 if (status != OK) {
331 return status;
332 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800333 secure = false;
334 } else {
335 hDestination.type = BufferType::NATIVE_HANDLE;
336 hDestination.secureMemory = hidl_handle(destination.mHandle);
337 secure = true;
338 }
339
Jeff Tinker3cb53162017-02-16 12:06:32 -0800340 ::SharedBuffer hSource;
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700341 status_t status = toSharedBuffer(source.mSharedMemory, heapSeqNum, &hSource);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800342 if (status != OK) {
343 return status;
344 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800345
346 status_t err = UNKNOWN_ERROR;
347 uint32_t bytesWritten = 0;
348
349 Return<void> hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), hMode,
Jeff Tinker3cb53162017-02-16 12:06:32 -0800350 hPattern, hSubSamples, hSource, offset, hDestination,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800351 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
352 if (status == Status::OK) {
353 bytesWritten = hBytesWritten;
354 *errorDetailMsg = toString8(hDetailedError);
355 }
356 err = toStatusT(status);
357 }
358 );
359
360 if (!hResult.isOk()) {
361 err = DEAD_OBJECT;
362 }
363
364 if (err == OK) {
365 return bytesWritten;
366 }
367 return err;
368}
369
370void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
371 Mutex::Autolock autoLock(mLock);
372
373 if (mInitCheck != OK) {
374 return;
375 }
376
377 mPlugin->notifyResolution(width, height);
378}
379
380status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
381 Mutex::Autolock autoLock(mLock);
382
383 if (mInitCheck != OK) {
384 return mInitCheck;
385 }
386
387 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
388}
389
390} // namespace android