blob: 591861aeecf944ddb8882d4eccabf329f1fcb6f5 [file] [log] [blame]
Jeff Tinkerb075caa2016-12-06 23:15:20 -08001/*
2 * Copyright (C) 2016 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 */
Jeff Tinker972a3e32017-01-23 14:02:50 -080016#define LOG_TAG "android.hardware.drm@1.0-impl"
Jeff Tinkerb075caa2016-12-06 23:15:20 -080017
18#include "CryptoPlugin.h"
19#include "TypeConvert.h"
20
Jeff Tinkerb075caa2016-12-06 23:15:20 -080021#include <android/hidl/memory/1.0/IMemory.h>
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -080022#include <hidlmemory/mapping.h>
Steven Moreland4e7a3072017-04-06 12:15:23 -070023#include <log/log.h>
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -080024#include <media/stagefright/foundation/AString.h>
Jeff Tinkerb075caa2016-12-06 23:15:20 -080025
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -080026using android::hardware::hidl_memory;
Jeff Tinkerb075caa2016-12-06 23:15:20 -080027using android::hidl::memory::V1_0::IMemory;
28
Jeff Tinkerb075caa2016-12-06 23:15:20 -080029namespace android {
30namespace hardware {
31namespace drm {
Jeff Tinkerb075caa2016-12-06 23:15:20 -080032namespace V1_0 {
33namespace implementation {
34
Jeff Tinkerda002fe2017-01-19 14:41:11 -080035 // Methods from ::android::hardware::drm::V1_0::ICryptoPlugin follow
Jeff Tinkerb075caa2016-12-06 23:15:20 -080036 Return<bool> CryptoPlugin::requiresSecureDecoderComponent(
37 const hidl_string& mime) {
Scott Randolph89978802017-04-03 14:06:19 -070038 return mLegacyPlugin->requiresSecureDecoderComponent(mime.c_str());
Jeff Tinkerb075caa2016-12-06 23:15:20 -080039 }
40
41 Return<void> CryptoPlugin::notifyResolution(uint32_t width,
42 uint32_t height) {
43 mLegacyPlugin->notifyResolution(width, height);
44 return Void();
45 }
46
47 Return<Status> CryptoPlugin::setMediaDrmSession(
48 const hidl_vec<uint8_t>& sessionId) {
49 return toStatus(mLegacyPlugin->setMediaDrmSession(toVector(sessionId)));
50 }
51
Jeff Tinker0b3f41e2017-02-16 12:20:30 -080052 Return<void> CryptoPlugin::setSharedBufferBase(const hidl_memory& base,
53 uint32_t bufferId) {
54 mSharedBufferMap[bufferId] = mapMemory(base);
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -080055 return Void();
56 }
57
Jeff Tinkerb075caa2016-12-06 23:15:20 -080058 Return<void> CryptoPlugin::decrypt(bool secure,
59 const hidl_array<uint8_t, 16>& keyId,
60 const hidl_array<uint8_t, 16>& iv, Mode mode,
61 const Pattern& pattern, const hidl_vec<SubSample>& subSamples,
Jeff Tinkerda002fe2017-01-19 14:41:11 -080062 const SharedBuffer& source, uint64_t offset,
Jeff Tinker6fdbe862017-01-11 19:45:23 -080063 const DestinationBuffer& destination,
Jeff Tinkerb075caa2016-12-06 23:15:20 -080064 decrypt_cb _hidl_cb) {
65
Jeff Tinker0b3f41e2017-02-16 12:20:30 -080066 if (mSharedBufferMap.find(source.bufferId) == mSharedBufferMap.end()) {
67 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "source decrypt buffer base not set");
Jeff Tinkerdc8e2d02017-01-23 14:24:12 -080068 return Void();
69 }
70
Jeff Tinker0b3f41e2017-02-16 12:20:30 -080071 if (destination.type == BufferType::SHARED_MEMORY) {
72 const SharedBuffer& dest = destination.nonsecureMemory;
73 if (mSharedBufferMap.find(dest.bufferId) == mSharedBufferMap.end()) {
74 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "destination decrypt buffer base not set");
75 return Void();
76 }
77 }
78
Jeff Tinkerb075caa2016-12-06 23:15:20 -080079 android::CryptoPlugin::Mode legacyMode;
80 switch(mode) {
81 case Mode::UNENCRYPTED:
82 legacyMode = android::CryptoPlugin::kMode_Unencrypted;
83 break;
84 case Mode::AES_CTR:
85 legacyMode = android::CryptoPlugin::kMode_AES_CTR;
86 break;
87 case Mode::AES_CBC_CTS:
88 legacyMode = android::CryptoPlugin::kMode_AES_WV;
89 break;
90 case Mode::AES_CBC:
91 legacyMode = android::CryptoPlugin::kMode_AES_CBC;
92 break;
93 }
94 android::CryptoPlugin::Pattern legacyPattern;
95 legacyPattern.mEncryptBlocks = pattern.encryptBlocks;
96 legacyPattern.mSkipBlocks = pattern.skipBlocks;
97
98 android::CryptoPlugin::SubSample *legacySubSamples =
99 new android::CryptoPlugin::SubSample[subSamples.size()];
100
101 for (size_t i = 0; i < subSamples.size(); i++) {
102 legacySubSamples[i].mNumBytesOfClearData
103 = subSamples[i].numBytesOfClearData;
104 legacySubSamples[i].mNumBytesOfEncryptedData
105 = subSamples[i].numBytesOfEncryptedData;
106 }
107
108 AString detailMessage;
Jeff Tinker0b3f41e2017-02-16 12:20:30 -0800109 sp<IMemory> sourceBase = mSharedBufferMap[source.bufferId];
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800110
Jeff Tinker0b3f41e2017-02-16 12:20:30 -0800111 if (source.offset + offset + source.size > sourceBase->getSize()) {
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -0800112 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size");
113 return Void();
114 }
Jeff Tinker6fdbe862017-01-11 19:45:23 -0800115
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -0800116 uint8_t *base = static_cast<uint8_t *>
Jeff Tinker0b3f41e2017-02-16 12:20:30 -0800117 (static_cast<void *>(sourceBase->getPointer()));
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -0800118 void *srcPtr = static_cast<void *>(base + source.offset + offset);
Jeff Tinker6fdbe862017-01-11 19:45:23 -0800119
Jeff Tinker6fdbe862017-01-11 19:45:23 -0800120 void *destPtr = NULL;
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800121 if (destination.type == BufferType::SHARED_MEMORY) {
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -0800122 const SharedBuffer& destBuffer = destination.nonsecureMemory;
Jeff Tinker0b3f41e2017-02-16 12:20:30 -0800123 sp<IMemory> destBase = mSharedBufferMap[destBuffer.bufferId];
124 if (destBuffer.offset + destBuffer.size > destBase->getSize()) {
Jeff Tinkerf21cdaf2017-01-18 11:49:27 -0800125 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size");
126 return Void();
127 }
128 destPtr = static_cast<void *>(base + destination.nonsecureMemory.offset);
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800129 } else if (destination.type == BufferType::NATIVE_HANDLE) {
130 native_handle_t *handle = const_cast<native_handle_t *>(
131 destination.secureMemory.getNativeHandle());
132 destPtr = static_cast<void *>(handle);
133 }
134 ssize_t result = mLegacyPlugin->decrypt(secure, keyId.data(), iv.data(),
Jeff Tinker6fdbe862017-01-11 19:45:23 -0800135 legacyMode, legacyPattern, srcPtr, legacySubSamples,
136 subSamples.size(), destPtr, &detailMessage);
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800137
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800138 delete[] legacySubSamples;
139
140 uint32_t status;
141 uint32_t bytesWritten;
142
143 if (result >= 0) {
144 status = android::OK;
145 bytesWritten = result;
146 } else {
Rahul Friasabd4e112017-02-27 19:17:30 -0800147 status = result;
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800148 bytesWritten = 0;
149 }
150
Jeff Tinker01f0a5a2017-01-12 09:22:18 -0800151 _hidl_cb(toStatus(status), bytesWritten, detailMessage.c_str());
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800152 return Void();
153 }
154
155} // namespace implementation
156} // namespace V1_0
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800157} // namespace drm
158} // namespace hardware
159} // namespace android