blob: e0db1c4378cf84831d56684cab5077103b80d47e [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 );
Robert Shihcb577452021-02-14 07:42:59 +0000149 if (!hResult.isOk()) {
150 mInitCheck = DEAD_OBJECT;
151 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800152 return plugin;
153}
154
155
156status_t CryptoHal::initCheck() const {
157 return mInitCheck;
158}
159
160
161bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
162 Mutex::Autolock autoLock(mLock);
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800163
164 for (size_t i = 0; i < mFactories.size(); i++) {
165 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
166 return true;
167 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800168 }
169 return false;
170}
171
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800172status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data,
173 size_t size) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800174 Mutex::Autolock autoLock(mLock);
175
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800176 for (size_t i = 0; i < mFactories.size(); i++) {
177 if (mFactories[i]->isCryptoSchemeSupported(uuid)) {
178 mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size);
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800179 if (mPlugin != NULL) {
180 mPluginV1_2 = drm::V1_2::ICryptoPlugin::castFrom(mPlugin);
181 }
Jeff Tinkerabeb36a2017-02-17 09:42:46 -0800182 }
183 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800184
Robert Shihcb577452021-02-14 07:42:59 +0000185 if (mInitCheck == NO_INIT) {
186 mInitCheck = mPlugin == NULL ? ERROR_UNSUPPORTED : OK;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800187 }
188
189 return mInitCheck;
190}
191
192status_t CryptoHal::destroyPlugin() {
193 Mutex::Autolock autoLock(mLock);
194
195 if (mInitCheck != OK) {
196 return mInitCheck;
197 }
198
199 mPlugin.clear();
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800200 mPluginV1_2.clear();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800201 return OK;
202}
203
204bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const {
205 Mutex::Autolock autoLock(mLock);
206
207 if (mInitCheck != OK) {
Jeff Tinkercca23772018-05-07 11:41:56 -0700208 return false;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800209 }
210
Jeff Tinkercca23772018-05-07 11:41:56 -0700211 Return<bool> hResult = mPlugin->requiresSecureDecoderComponent(hidl_string(mime));
212 if (!hResult.isOk()) {
213 return false;
214 }
215 return hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800216}
217
218
219/**
Robert Shihd3f9ba72019-11-20 17:25:26 -0800220 * If the heap base isn't set, get the heap base from the HidlMemory
Jeff Tinkera53d6552017-01-20 00:31:46 -0800221 * and send it to the HAL so it can map a remote heap of the same
222 * size. Once the heap base is established, shared memory buffers
223 * are sent by providing an offset into the heap and a buffer size.
224 */
Robert Shih895fba92019-07-16 16:29:44 -0700225int32_t CryptoHal::setHeapBase(const sp<HidlMemory>& heap) {
Robert Shihc9f7efa2019-07-16 16:19:59 -0700226 if (heap == NULL || mHeapSeqNum < 0) {
227 ALOGE("setHeapBase(): heap %p mHeapSeqNum %d", heap.get(), mHeapSeqNum);
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700228 return -1;
229 }
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700230
231 Mutex::Autolock autoLock(mLock);
232
233 int32_t seqNum = mHeapSeqNum++;
Robert Shihc9f7efa2019-07-16 16:19:59 -0700234 uint32_t bufferId = static_cast<uint32_t>(seqNum);
Robert Shih895fba92019-07-16 16:29:44 -0700235 mHeapSizes.add(seqNum, heap->size());
236 Return<void> hResult = mPlugin->setSharedBufferBase(*heap, bufferId);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800237 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700238 return seqNum;
Jeff Tinker3cb53162017-02-16 12:06:32 -0800239}
240
Chong Zhang6dcab2b2017-03-28 14:18:27 -0700241void CryptoHal::clearHeapBase(int32_t seqNum) {
242 Mutex::Autolock autoLock(mLock);
243
Jeff Tinker0db0fa12018-05-22 16:55:57 -0700244 /*
245 * Clear the remote shared memory mapping by setting the shared
246 * buffer base to a null hidl_memory.
247 *
248 * TODO: Add a releaseSharedBuffer method in a future DRM HAL
249 * API version to make this explicit.
250 */
Robert Shih895fba92019-07-16 16:29:44 -0700251 ssize_t index = mHeapSizes.indexOfKey(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700252 if (index >= 0) {
253 if (mPlugin != NULL) {
Robert Shih895fba92019-07-16 16:29:44 -0700254 uint32_t bufferId = static_cast<uint32_t>(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700255 Return<void> hResult = mPlugin->setSharedBufferBase(hidl_memory(), bufferId);
256 ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed");
257 }
Robert Shih895fba92019-07-16 16:29:44 -0700258 mHeapSizes.removeItem(seqNum);
Jeff Tinker20795c72018-05-30 18:20:09 -0700259 }
Chong Zhangd07c9272017-03-28 11:02:06 -0700260}
261
Robert Shih895fba92019-07-16 16:29:44 -0700262status_t CryptoHal::checkSharedBuffer(const ::SharedBuffer &buffer) {
263 int32_t seqNum = static_cast<int32_t>(buffer.bufferId);
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700264 // memory must be in one of the heaps that have been set
Robert Shih895fba92019-07-16 16:29:44 -0700265 if (mHeapSizes.indexOfKey(seqNum) < 0) {
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700266 return UNKNOWN_ERROR;
267 }
Jeff Tinker3cb53162017-02-16 12:06:32 -0800268
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700269 // memory must be within the address space of the heap
Robert Shih895fba92019-07-16 16:29:44 -0700270 size_t heapSize = mHeapSizes.valueFor(seqNum);
271 if (heapSize < buffer.offset + buffer.size ||
272 SIZE_MAX - buffer.offset < buffer.size) {
Jeff Tinkerb1f8c802018-04-19 16:23:21 -0700273 android_errorWriteLog(0x534e4554, "76221123");
274 return UNKNOWN_ERROR;
275 }
276
Jeff Tinkera53d6552017-01-20 00:31:46 -0800277 return OK;
278}
279
280ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16],
281 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Robert Shih895fba92019-07-16 16:29:44 -0700282 const ::SharedBuffer &hSource, size_t offset,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800283 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Robert Shih895fba92019-07-16 16:29:44 -0700284 const ::DestinationBuffer &hDestination, AString *errorDetailMsg) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800285 Mutex::Autolock autoLock(mLock);
286
287 if (mInitCheck != OK) {
288 return mInitCheck;
289 }
290
Jeff Tinkera53d6552017-01-20 00:31:46 -0800291 Mode hMode;
292 switch(mode) {
293 case CryptoPlugin::kMode_Unencrypted:
294 hMode = Mode::UNENCRYPTED ;
295 break;
296 case CryptoPlugin::kMode_AES_CTR:
297 hMode = Mode::AES_CTR;
298 break;
299 case CryptoPlugin::kMode_AES_WV:
300 hMode = Mode::AES_CBC_CTS;
301 break;
302 case CryptoPlugin::kMode_AES_CBC:
303 hMode = Mode::AES_CBC;
304 break;
305 default:
306 return UNKNOWN_ERROR;
307 }
308
309 Pattern hPattern;
310 hPattern.encryptBlocks = pattern.mEncryptBlocks;
311 hPattern.skipBlocks = pattern.mSkipBlocks;
312
313 std::vector<SubSample> stdSubSamples;
314 for (size_t i = 0; i < numSubSamples; i++) {
315 SubSample subSample;
316 subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData;
317 subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData;
318 stdSubSamples.push_back(subSample);
319 }
320 auto hSubSamples = hidl_vec<SubSample>(stdSubSamples);
321
322 bool secure;
Robert Shih895fba92019-07-16 16:29:44 -0700323 if (hDestination.type == BufferType::SHARED_MEMORY) {
324 status_t status = checkSharedBuffer(hDestination.nonsecureMemory);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800325 if (status != OK) {
326 return status;
327 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800328 secure = false;
Robert Shih895fba92019-07-16 16:29:44 -0700329 } else if (hDestination.type == BufferType::NATIVE_HANDLE) {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800330 secure = true;
Jeff Tinker24420472018-01-11 17:46:16 -0800331 } else {
332 android_errorWriteLog(0x534e4554, "70526702");
333 return UNKNOWN_ERROR;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800334 }
335
Robert Shih895fba92019-07-16 16:29:44 -0700336 status_t status = checkSharedBuffer(hSource);
Jeff Tinker3cb53162017-02-16 12:06:32 -0800337 if (status != OK) {
338 return status;
339 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800340
341 status_t err = UNKNOWN_ERROR;
342 uint32_t bytesWritten = 0;
343
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800344 Return<void> hResult;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800345
Jeff Tinker6ce16cb2021-02-12 13:56:59 -0800346 mLock.unlock();
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800347 if (mPluginV1_2 != NULL) {
348 hResult = mPluginV1_2->decrypt_1_2(secure, toHidlArray16(keyId), toHidlArray16(iv),
349 hMode, hPattern, hSubSamples, hSource, offset, hDestination,
350 [&](Status_V1_2 status, uint32_t hBytesWritten, hidl_string hDetailedError) {
351 if (status == Status_V1_2::OK) {
352 bytesWritten = hBytesWritten;
353 *errorDetailMsg = toString8(hDetailedError);
354 }
Robert Shih5944a0b2021-02-10 04:26:33 -0800355 err = toStatusT(status);
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800356 }
357 );
358 } else {
359 hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv),
360 hMode, hPattern, hSubSamples, hSource, offset, hDestination,
361 [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) {
362 if (status == Status::OK) {
363 bytesWritten = hBytesWritten;
364 *errorDetailMsg = toString8(hDetailedError);
365 }
366 err = toStatusT(status);
367 }
368 );
Jeff Tinkera53d6552017-01-20 00:31:46 -0800369 }
370
Jeff Tinkerb8684f32018-12-12 08:41:31 -0800371 err = hResult.isOk() ? err : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800372 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
Robert Shih3fa03fb2021-04-27 16:01:34 -0700385 auto hResult = mPlugin->notifyResolution(width, height);
386 ALOGE_IF(!hResult.isOk(), "notifyResolution txn failed %s", hResult.description().c_str());
Jeff Tinkera53d6552017-01-20 00:31:46 -0800387}
388
389status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) {
390 Mutex::Autolock autoLock(mLock);
391
392 if (mInitCheck != OK) {
393 return mInitCheck;
394 }
395
Robert Shih3fa03fb2021-04-27 16:01:34 -0700396 auto err = mPlugin->setMediaDrmSession(toHidlVec(sessionId));
397 return err.isOk() ? toStatusT(err) : DEAD_OBJECT;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800398}
399
Robert Shih9afca952021-02-12 01:36:06 -0800400status_t CryptoHal::getLogMessages(Vector<drm::V1_4::LogMessage> &logs) const {
401 Mutex::Autolock autoLock(mLock);
402 return DrmUtils::GetLogMessages<drm::V1_4::ICryptoPlugin>(mPlugin, logs);
403}
Jeff Tinkera53d6552017-01-20 00:31:46 -0800404} // namespace android