blob: f1f3b0189d407f84d3dd6e5cf5caa2a1bc9c16eb [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>
20#include <dirent.h>
21#include <dlfcn.h>
22
23#include <android/hardware/drm/1.0/types.h>
24
25#include <binder/IMemory.h>
26#include <cutils/native_handle.h>
27#include <media/CryptoHal.h>
28#include <media/hardware/CryptoAPI.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AString.h>
31#include <media/stagefright/foundation/hexdump.h>
32#include <media/stagefright/MediaErrors.h>
33
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;
50using ::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;
71 default:
72 return UNKNOWN_ERROR;
73 }
74}
75
76
77static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
78 hidl_vec<uint8_t> vec;
79 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
80 return vec;
81}
82
83static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
84 hidl_vec<uint8_t> vec;
85 vec.resize(size);
86 memcpy(vec.data(), ptr, size);
87 return vec;
88}
89
90static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
91 if (!ptr) {
92 return hidl_array<uint8_t, 16>();
93 }
94 return hidl_array<uint8_t, 16>(ptr);
95}
96
97
98static ::SharedBuffer toSharedBuffer(const sp<IMemory>& sharedBuffer) {
99 ssize_t offset;
100 size_t size;
101 sharedBuffer->getMemory(&offset, &size);
102
103 ::SharedBuffer buffer;
104 buffer.offset = offset >= 0 ? offset : 0;
105 buffer.size = size;
106 return buffer;
107}
108
109static String8 toString8(hidl_string hString) {
110 return String8(hString.c_str());
111}
112
113
114CryptoHal::CryptoHal()
115 : mFactory(makeCryptoFactory()),
116 mInitCheck((mFactory == NULL) ? ERROR_UNSUPPORTED : NO_INIT),
117 mHeapBase(NULL) {
118}
119
120CryptoHal::~CryptoHal() {
121}
122
123
124sp<ICryptoFactory> CryptoHal::makeCryptoFactory() {
125 sp<ICryptoFactory> factory = ICryptoFactory::getService("crypto");
126 if (factory == NULL) {
127 ALOGE("Failed to make crypto factory");
128 }
129 return factory;
130}
131
132sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const uint8_t uuid[16],
133 const void *initData, size_t initDataSize) {
134 if (mFactory == NULL){
135 return NULL;
136 }
137
138 sp<ICryptoPlugin> plugin;
139 Return<void> hResult = mFactory->createPlugin(toHidlArray16(uuid),
140 toHidlVec(initData, initDataSize),
141 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
142 if (status != Status::OK) {
143 ALOGE("Failed to make crypto plugin");
144 return;
145 }
146 plugin = hPlugin;
147 }
148 );
149 return plugin;
150}
151
152
153status_t CryptoHal::initCheck() const {
154 return mInitCheck;
155}
156
157
158bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
159 Mutex::Autolock autoLock(mLock);
160 if (mFactory != NULL) {
161 return mFactory->isCryptoSchemeSupported(uuid);
162 }
163 return false;
164}
165
166status_t CryptoHal::createPlugin(
167 const uint8_t uuid[16], const void *data, size_t size) {
168 Mutex::Autolock autoLock(mLock);
169
170 mPlugin = makeCryptoPlugin(uuid, data, size);
171
172 if (mPlugin == NULL) {
173 mInitCheck = ERROR_UNSUPPORTED;
174 } else {
175 mInitCheck = OK;
176 }
177
178 return mInitCheck;
179}
180
181status_t CryptoHal::destroyPlugin() {
182 Mutex::Autolock autoLock(mLock);
183
184 if (mInitCheck != OK) {
185 return mInitCheck;
186 }
187
188 mPlugin.clear();
189 return OK;
190}
191
192bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
193 Mutex::Autolock autoLock(mLock);
194
195 if (mInitCheck != OK) {
196 return mInitCheck;
197 }
198
199 return mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
200}
201
202
203/**
204 * If the heap base isn't set, get the heap base from the IMemory
205 * and send it to the HAL so it can map a remote heap of the same
206 * size. Once the heap base is established, shared memory buffers
207 * are sent by providing an offset into the heap and a buffer size.
208 */
209status_t CryptoHal::setHeapBase(const sp<IMemory>& sharedBuffer) {
210 sp<IMemoryHeap> heap = sharedBuffer->getMemory(NULL, NULL);
211 if (mHeapBase != heap->getBase()) {
212 int fd = heap->getHeapID();
213 native_handle_t* nativeHandle = native_handle_create(1, 0);
214 nativeHandle->data[0] = fd;
215 auto hidlHandle = hidl_handle(nativeHandle);
216 auto hidlMemory = hidl_memory("ashmem", hidlHandle, heap->getSize());
217 mHeapBase = heap->getBase();
218 Return<void> hResult = mPlugin->setSharedBufferBase(hidlMemory);
219 if (!hResult.isOk()) {
220 return DEAD_OBJECT;
221 }
222 }
223 return OK;
224}
225
226ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
227 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
228 const sp<IMemory> &source, size_t offset,
229 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
230 const ICrypto::DestinationBuffer &destination, AString *errorDetailMsg) {
231 Mutex::Autolock autoLock(mLock);
232
233 if (mInitCheck != OK) {
234 return mInitCheck;
235 }
236
237 // Establish the base of the shared memory heap
238 setHeapBase(source);
239
240 Mode hMode;
241 switch(mode) {
242 case CryptoPlugin::kMode_Unencrypted:
243 hMode = Mode::UNENCRYPTED ;
244 break;
245 case CryptoPlugin::kMode_AES_CTR:
246 hMode = Mode::AES_CTR;
247 break;
248 case CryptoPlugin::kMode_AES_WV:
249 hMode = Mode::AES_CBC_CTS;
250 break;
251 case CryptoPlugin::kMode_AES_CBC:
252 hMode = Mode::AES_CBC;
253 break;
254 default:
255 return UNKNOWN_ERROR;
256 }
257
258 Pattern hPattern;
259 hPattern.encryptBlocks = pattern.mEncryptBlocks;
260 hPattern.skipBlocks = pattern.mSkipBlocks;
261
262 std::vector<SubSample> stdSubSamples;
263 for (size_t i = 0; i < numSubSamples; i++) {
264 SubSample subSample;
265 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
266 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
267 stdSubSamples.push_back(subSample);
268 }
269 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
270
271 bool secure;
272 ::DestinationBuffer hDestination;
273 if (destination.mType == kDestinationTypeSharedMemory) {
274 hDestination.type = BufferType::SHARED_MEMORY;
275 hDestination.nonsecureMemory = toSharedBuffer(destination.mSharedMemory);
276 secure = false;
277 } else {
278 hDestination.type = BufferType::NATIVE_HANDLE;
279 hDestination.secureMemory = hidl_handle(destination.mHandle);
280 secure = true;
281 }
282
283
284 status_t err = UNKNOWN_ERROR;
285 uint32_t bytesWritten = 0;
286
287 Return<void> hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), hMode,
288 hPattern, hSubSamples, toSharedBuffer(source), offset, hDestination,
289 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
290 if (status == Status::OK) {
291 bytesWritten = hBytesWritten;
292 *errorDetailMsg = toString8(hDetailedError);
293 }
294 err = toStatusT(status);
295 }
296 );
297
298 if (!hResult.isOk()) {
299 err = DEAD_OBJECT;
300 }
301
302 if (err == OK) {
303 return bytesWritten;
304 }
305 return err;
306}
307
308void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
309 Mutex::Autolock autoLock(mLock);
310
311 if (mInitCheck != OK) {
312 return;
313 }
314
315 mPlugin->notifyResolution(width, height);
316}
317
318status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
319 Mutex::Autolock autoLock(mLock);
320
321 if (mInitCheck != OK) {
322 return mInitCheck;
323 }
324
325 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
326}
327
328} // namespace android