blob: 9d39f83fe76714465ee23518a810b2085d22764b [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>
Steven Moreland7dcb4db2019-05-13 13:10:51 -070022#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland7dcb4db2019-05-13 13:10:51 -070023#include <hidl/ServiceManagement.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080024#include <hidlmemory/FrameworkUtils.h>
Jeff Tinkera53d6552017-01-20 00:31:46 -080025#include <media/hardware/CryptoAPI.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AString.h>
28#include <media/stagefright/foundation/hexdump.h>
29#include <media/stagefright/MediaErrors.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080030#include <mediadrm/CryptoHal.h>
Robert Shih5944a0b2021-02-10 04:26:33 -080031#include <mediadrm/DrmUtils.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080032
Jeff Tinkerb8684f32018-12-12 08:41:31 -080033using drm::V1_0::BufferType;
34using drm::V1_0::DestinationBuffer;
35using drm::V1_0::ICryptoFactory;
36using drm::V1_0::ICryptoPlugin;
37using drm::V1_0::Mode;
38using drm::V1_0::Pattern;
39using drm::V1_0::SharedBuffer;
40using drm::V1_0::Status;
41using drm::V1_0::SubSample;
Jeff Tinkera53d6552017-01-20 00:31:46 -080042
Robert Shih5944a0b2021-02-10 04:26:33 -080043using ::android::DrmUtils::toStatusT;
Jeff Tinkera53d6552017-01-20 00:31:46 -080044using ::android::hardware::hidl_array;
45using ::android::hardware::hidl_handle;
46using ::android::hardware::hidl_memory;
47using ::android::hardware::hidl_string;
48using ::android::hardware::hidl_vec;
Robert Shih895fba92019-07-16 16:29:44 -070049using ::android::hardware::HidlMemory;
Jeff Tinkera53d6552017-01-20 00:31:46 -080050using ::android::hardware::Return;
51using ::android::hardware::Void;
52using ::android::sp;
53
Jeff Tinkerb8684f32018-12-12 08:41:31 -080054typedef drm::V1_2::Status Status_V1_2;
Jeff Tinkera53d6552017-01-20 00:31:46 -080055
56namespace android {
57
Jeff Tinkera53d6552017-01-20 00:31:46 -080058static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) {
59 hidl_vec<uint8_t> vec;
60 vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size());
61 return vec;
62}
63
64static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
65 hidl_vec<uint8_t> vec;
66 vec.resize(size);
67 memcpy(vec.data(), ptr, size);
68 return vec;
69}
70
71static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
72 if (!ptr) {
73 return hidl_array<uint8_t, 16>();
74 }
75 return hidl_array<uint8_t, 16>(ptr);
76}
77
78
Jeff Tinkera53d6552017-01-20 00:31:46 -080079static String8 toString8(hidl_string hString) {
80 return String8(hString.c_str());
81}
82
83
84CryptoHal::CryptoHal()
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080085 : mFactories(makeCryptoFactories()),
86 mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT),
Chong Zhang6dcab2b2017-03-28 14:18:27 -070087 mHeapSeqNum(0) {
Jeff Tinkera53d6552017-01-20 00:31:46 -080088}
89
90CryptoHal::~CryptoHal() {
91}
92
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080093Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() {
94 Vector<sp<ICryptoFactory>> factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -080095
Steven Moreland7dcb4db2019-05-13 13:10:51 -070096 auto manager = hardware::defaultServiceManager1_2();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080097 if (manager != NULL) {
Steven Moreland7dcb4db2019-05-13 13:10:51 -070098 manager->listManifestByInterface(drm::V1_0::ICryptoFactory::descriptor,
Jeff Tinkerabeb36a2017-02-17 09:42:46 -080099 [&factories](const hidl_vec<hidl_string> &registered) {
100 for (const auto &instance : registered) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000101 auto factory = drm::V1_0::ICryptoFactory::getService(instance);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800102 if (factory != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000103 ALOGD("found drm@1.0 ICryptoFactory %s", instance.c_str());
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800104 factories.push_back(factory);
Jeff Tinkere307dc42018-02-11 19:53:54 +0000105 }
106 }
107 }
108 );
Steven Moreland7dcb4db2019-05-13 13:10:51 -0700109 manager->listManifestByInterface(drm::V1_1::ICryptoFactory::descriptor,
Jeff Tinkere307dc42018-02-11 19:53:54 +0000110 [&factories](const hidl_vec<hidl_string> &registered) {
111 for (const auto &instance : registered) {
112 auto factory = drm::V1_1::ICryptoFactory::getService(instance);
113 if (factory != NULL) {
114 ALOGD("found drm@1.1 ICryptoFactory %s", instance.c_str());
115 factories.push_back(factory);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800116 }
117 }
118 }
119 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800120 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800121
122 if (factories.size() == 0) {
123 // must be in passthrough mode, load the default passthrough service
Jeff Tinkere309b222017-04-05 08:01:28 -0700124 auto passthrough = ICryptoFactory::getService();
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800125 if (passthrough != NULL) {
Jeff Tinkere307dc42018-02-11 19:53:54 +0000126 ALOGI("makeCryptoFactories: using default passthrough crypto instance");
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800127 factories.push_back(passthrough);
128 } else {
129 ALOGE("Failed to find any crypto factories");
130 }
131 }
132 return factories;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800133}
134
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800135sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory,
136 const uint8_t uuid[16], const void *initData, size_t initDataSize) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800137
138 sp<ICryptoPlugin> plugin;
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800139 Return<void> hResult = factory->createPlugin(toHidlArray16(uuid),
Jeff Tinkera53d6552017-01-20 00:31:46 -0800140 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);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800160
161 for (size_t i = 0; i < mFactories.size(); i++) {
162 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
163 return true;
164 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800165 }
166 return false;
167}
168
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800169status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
170 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800171 Mutex::Autolock autoLock(mLock);
172
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800173 for (size_t i = 0; i < mFactories.size(); i++) {
174 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
175 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800176 if (mPlugin != NULL) {
177 mPluginV1_2 = drm::V1_2::ICryptoPlugin::castFrom(mPlugin);
178 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800179 }
180 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800181
182 if (mPlugin == NULL) {
183 mInitCheck = ERROR_UNSUPPORTED;
184 } else {
185 mInitCheck = OK;
186 }
187
188 return mInitCheck;
189}
190
191status_t CryptoHal::destroyPlugin() {
192 Mutex::Autolock autoLock(mLock);
193
194 if (mInitCheck != OK) {
195 return mInitCheck;
196 }
197
198 mPlugin.clear();
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800199 mPluginV1_2.clear();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800200 return OK;
201}
202
203bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
204 Mutex::Autolock autoLock(mLock);
205
206 if (mInitCheck != OK) {
Jeff Tinkercca23772018-05-07 11:41:56 -0700207 return false;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800208 }
209
Jeff Tinkercca23772018-05-07 11:41:56 -0700210 Return<bool> hResult = mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
211 if (!hResult.isOk()) {
212 return false;
213 }
214 return hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800215}
216
217
218/**
Robert Shihd3f9ba72019-11-20 17:25:26 -0800219 * If the heap base isn't set, get the heap base from the HidlMemory
Jeff Tinkera53d6552017-01-20 00:31:46 -0800220 * and send it to the HAL so it can map a remote heap of the same
221 * size. Once the heap base is established, shared memory buffers
222 * are sent by providing an offset into the heap and a buffer size.
223 */
Robert Shih895fba92019-07-16 16:29:44 -0700224int32_t CryptoHal::setHeapBase(const sp<HidlMemory>& heap) {
Robert Shihc9f7efa2019-07-16 16:19:59 -0700225 if (heap == NULL || mHeapSeqNum < 0) {
226 ALOGE("setHeapBase(): heap %p mHeapSeqNum %d", heap.get(), mHeapSeqNum);
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700227 return -1;
228 }
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700229
230 Mutex::Autolock autoLock(mLock);
231
232 int32_t seqNum = mHeapSeqNum++;
Robert Shihc9f7efa2019-07-16 16:19:59 -0700233 uint32_t bufferId = static_cast<uint32_t>(seqNum);
Robert Shih895fba92019-07-16 16:29:44 -0700234 mHeapSizes.add(seqNum, heap->size());
235 Return<void> hResult = mPlugin->setSharedBufferBase(*heap, bufferId);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800236 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700237 return seqNum;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800238}
239
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700240void CryptoHal::clearHeapBase(int32_t seqNum) {
241 Mutex::Autolock autoLock(mLock);
242
Jeff Tinker0db0fa12018-05-22 16:55:57 -0700243 /*
244 * Clear the remote shared memory mapping by setting the shared
245 * buffer base to a null hidl_memory.
246 *
247 * TODO: Add a releaseSharedBuffer method in a future DRM HAL
248 * API version to make this explicit.
249 */
Robert Shih895fba92019-07-16 16:29:44 -0700250 ssize_t index = mHeapSizes.indexOfKey(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700251 if (index >= 0) {
252 if (mPlugin != NULL) {
Robert Shih895fba92019-07-16 16:29:44 -0700253 uint32_t bufferId = static_cast<uint32_t>(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700254 Return<void> hResult = mPlugin->setSharedBufferBase(hidl_memory(), bufferId);
255 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
256 }
Robert Shih895fba92019-07-16 16:29:44 -0700257 mHeapSizes.removeItem(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700258 }
Chong Zhangd07c9272017-03-28 11:02:06 -0700259}
260
Robert Shih895fba92019-07-16 16:29:44 -0700261status_t CryptoHal::checkSharedBuffer(const ::SharedBuffer &buffer) {
262 int32_t seqNum = static_cast<int32_t>(buffer.bufferId);
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700263 // memory must be in one of the heaps that have been set
Robert Shih895fba92019-07-16 16:29:44 -0700264 if (mHeapSizes.indexOfKey(seqNum) < 0) {
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700265 return UNKNOWN_ERROR;
266 }
Jeff Tinker3cb53162017-02-16 12:06:32 -0800267
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700268 // memory must be within the address space of the heap
Robert Shih895fba92019-07-16 16:29:44 -0700269 size_t heapSize = mHeapSizes.valueFor(seqNum);
270 if (heapSize < buffer.offset + buffer.size ||
271 SIZE_MAX - buffer.offset < buffer.size) {
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700272 android_errorWriteLog(0x534e4554, "76221123");
273 return UNKNOWN_ERROR;
274 }
275
Jeff Tinkera53d6552017-01-20 00:31:46 -0800276 return OK;
277}
278
279ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
280 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Robert Shih895fba92019-07-16 16:29:44 -0700281 const ::SharedBuffer &hSource, size_t offset,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800282 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Robert Shih895fba92019-07-16 16:29:44 -0700283 const ::DestinationBuffer &hDestination, AString *errorDetailMsg) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800284 Mutex::Autolock autoLock(mLock);
285
286 if (mInitCheck != OK) {
287 return mInitCheck;
288 }
289
Jeff Tinkera53d6552017-01-20 00:31:46 -0800290 Mode hMode;
291 switch(mode) {
292 case CryptoPlugin::kMode_Unencrypted:
293 hMode = Mode::UNENCRYPTED ;
294 break;
295 case CryptoPlugin::kMode_AES_CTR:
296 hMode = Mode::AES_CTR;
297 break;
298 case CryptoPlugin::kMode_AES_WV:
299 hMode = Mode::AES_CBC_CTS;
300 break;
301 case CryptoPlugin::kMode_AES_CBC:
302 hMode = Mode::AES_CBC;
303 break;
304 default:
305 return UNKNOWN_ERROR;
306 }
307
308 Pattern hPattern;
309 hPattern.encryptBlocks = pattern.mEncryptBlocks;
310 hPattern.skipBlocks = pattern.mSkipBlocks;
311
312 std::vector<SubSample> stdSubSamples;
313 for (size_t i = 0; i < numSubSamples; i++) {
314 SubSample subSample;
315 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
316 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
317 stdSubSamples.push_back(subSample);
318 }
319 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
320
321 bool secure;
Robert Shih895fba92019-07-16 16:29:44 -0700322 if (hDestination.type == BufferType::SHARED_MEMORY) {
323 status_t status = checkSharedBuffer(hDestination.nonsecureMemory);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800324 if (status != OK) {
325 return status;
326 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800327 secure = false;
Robert Shih895fba92019-07-16 16:29:44 -0700328 } else if (hDestination.type == BufferType::NATIVE_HANDLE) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800329 secure = true;
Jeff Tinker24420472018-01-11 17:46:16 -0800330 } else {
331 android_errorWriteLog(0x534e4554, "70526702");
332 return UNKNOWN_ERROR;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800333 }
334
Robert Shih895fba92019-07-16 16:29:44 -0700335 status_t status = checkSharedBuffer(hSource);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800336 if (status != OK) {
337 return status;
338 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800339
340 status_t err = UNKNOWN_ERROR;
341 uint32_t bytesWritten = 0;
342
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800343 Return<void> hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800344
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800345 if (mPluginV1_2 != NULL) {
346 hResult = mPluginV1_2->decrypt_1_2(secure, toHidlArray16(keyId), toHidlArray16(iv),
347 hMode, hPattern, hSubSamples, hSource, offset, hDestination,
348 [&](Status_V1_2 status, uint32_t hBytesWritten, hidl_string hDetailedError) {
349 if (status == Status_V1_2::OK) {
350 bytesWritten = hBytesWritten;
351 *errorDetailMsg = toString8(hDetailedError);
352 }
Robert Shih5944a0b2021-02-10 04:26:33 -0800353 err = toStatusT(status);
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800354 }
355 );
356 } else {
357 hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv),
358 hMode, hPattern, hSubSamples, hSource, offset, hDestination,
359 [&](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 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800367 }
368
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800369 err = hResult.isOk() ? err : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800370 if (err == OK) {
371 return bytesWritten;
372 }
373 return err;
374}
375
376void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
377 Mutex::Autolock autoLock(mLock);
378
379 if (mInitCheck != OK) {
380 return;
381 }
382
383 mPlugin->notifyResolution(width, height);
384}
385
386status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
387 Mutex::Autolock autoLock(mLock);
388
389 if (mInitCheck != OK) {
390 return mInitCheck;
391 }
392
393 return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId)));
394}
395
Robert Shih9afca952021-02-12 01:36:06 -0800396status_t CryptoHal::getLogMessages(Vector<drm::V1_4::LogMessage> &logs) const {
397 Mutex::Autolock autoLock(mLock);
398 return DrmUtils::GetLogMessages<drm::V1_4::ICryptoPlugin>(mPlugin, logs);
399}
Jeff Tinkera53d6552017-01-20 00:31:46 -0800400} // namespace android