1 /*
2 * Copyright (C) 2018 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 "hidl_ClearKeyCryptoPlugin"
19 #include <utils/Log.h>
20
21 #include "CryptoPlugin.h"
22 #include "SessionLibrary.h"
23 #include "TypeConvert.h"
24
25 #include <hidlmemory/mapping.h>
26
27 namespace android {
28 namespace hardware {
29 namespace drm {
30 namespace V1_1 {
31 namespace clearkey {
32
33 using ::android::hardware::drm::V1_0::BufferType;
34
setSharedBufferBase(const hidl_memory & base,uint32_t bufferId)35 Return<void> CryptoPlugin::setSharedBufferBase(
36 const hidl_memory& base, uint32_t bufferId) {
37 sp<IMemory> hidlMemory = mapMemory(base);
38 ALOGE_IF(hidlMemory == nullptr, "mapMemory returns nullptr");
39
40 // allow mapMemory to return nullptr
41 mSharedBufferMap[bufferId] = hidlMemory;
42 return Void();
43 }
44
45 // Returns negative values for error code and positive values for the size of
46 // decrypted data. In theory, the output size can be larger than the input
47 // size, but in practice this will never happen for AES-CTR.
decrypt(bool secure,const hidl_array<uint8_t,KEY_ID_SIZE> & keyId,const hidl_array<uint8_t,KEY_IV_SIZE> & iv,Mode mode,const Pattern & pattern,const hidl_vec<SubSample> & subSamples,const SharedBuffer & source,uint64_t offset,const DestinationBuffer & destination,decrypt_cb _hidl_cb)48 Return<void> CryptoPlugin::decrypt(
49 bool secure,
50 const hidl_array<uint8_t, KEY_ID_SIZE>& keyId,
51 const hidl_array<uint8_t, KEY_IV_SIZE>& iv,
52 Mode mode,
53 const Pattern& pattern,
54 const hidl_vec<SubSample>& subSamples,
55 const SharedBuffer& source,
56 uint64_t offset,
57 const DestinationBuffer& destination,
58 decrypt_cb _hidl_cb) {
59 UNUSED(pattern);
60
61 if (secure) {
62 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
63 "Secure decryption is not supported with ClearKey.");
64 return Void();
65 }
66
67 if (mSharedBufferMap.find(source.bufferId) == mSharedBufferMap.end()) {
68 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
69 "source decrypt buffer base not set");
70 return Void();
71 }
72
73 if (destination.type == BufferType::SHARED_MEMORY) {
74 const SharedBuffer& dest = destination.nonsecureMemory;
75 if (mSharedBufferMap.find(dest.bufferId) == mSharedBufferMap.end()) {
76 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
77 "destination decrypt buffer base not set");
78 return Void();
79 }
80 } else {
81 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
82 "destination type not supported");
83 return Void();
84 }
85
86 sp<IMemory> sourceBase = mSharedBufferMap[source.bufferId];
87 if (sourceBase == nullptr) {
88 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "source is a nullptr");
89 return Void();
90 }
91
92 if (source.offset + offset + source.size > sourceBase->getSize()) {
93 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size");
94 return Void();
95 }
96
97 uint8_t *base = static_cast<uint8_t *>
98 (static_cast<void *>(sourceBase->getPointer()));
99 uint8_t* srcPtr = static_cast<uint8_t *>(base + source.offset + offset);
100 void* destPtr = NULL;
101 // destination.type == BufferType::SHARED_MEMORY
102 const SharedBuffer& destBuffer = destination.nonsecureMemory;
103 sp<IMemory> destBase = mSharedBufferMap[destBuffer.bufferId];
104 if (destBase == nullptr) {
105 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "destination is a nullptr");
106 return Void();
107 }
108
109 base = static_cast<uint8_t *>(static_cast<void *>(destBase->getPointer()));
110
111 if (destBuffer.offset + destBuffer.size > destBase->getSize()) {
112 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0, "invalid buffer size");
113 return Void();
114 }
115 destPtr = static_cast<void *>(base + destination.nonsecureMemory.offset);
116
117 // Calculate the output buffer size and determine if any subsamples are
118 // encrypted.
119 size_t destSize = 0;
120 size_t srcSize = 0;
121 bool haveEncryptedSubsamples = false;
122 for (size_t i = 0; i < subSamples.size(); i++) {
123 const SubSample &subSample = subSamples[i];
124 if (__builtin_add_overflow(destSize, subSample.numBytesOfClearData, &destSize) ||
125 __builtin_add_overflow(srcSize, subSample.numBytesOfClearData, &srcSize)) {
126 _hidl_cb(Status::BAD_VALUE, 0, "subsample clear size overflow");
127 return Void();
128 }
129 if (__builtin_add_overflow(destSize, subSample.numBytesOfEncryptedData, &destSize) ||
130 __builtin_add_overflow(srcSize, subSample.numBytesOfEncryptedData, &srcSize)) {
131 _hidl_cb(Status::BAD_VALUE, 0, "subsample encrypted size overflow");
132 return Void();
133 }
134 if (subSample.numBytesOfEncryptedData > 0) {
135 haveEncryptedSubsamples = true;
136 }
137 }
138
139 if (destSize > destBuffer.size || srcSize > source.size) {
140 _hidl_cb(Status::BAD_VALUE, 0, "subsample sum too large");
141 return Void();
142 }
143
144 if (mode == Mode::UNENCRYPTED) {
145 if (haveEncryptedSubsamples) {
146 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
147 "Encrypted subsamples found in allegedly unencrypted data.");
148 return Void();
149 }
150
151 size_t offset = 0;
152 for (size_t i = 0; i < subSamples.size(); ++i) {
153 const SubSample& subSample = subSamples[i];
154 if (subSample.numBytesOfClearData != 0) {
155 memcpy(reinterpret_cast<uint8_t*>(destPtr) + offset,
156 reinterpret_cast<const uint8_t*>(srcPtr) + offset,
157 subSample.numBytesOfClearData);
158 offset += subSample.numBytesOfClearData;
159 }
160 }
161
162 _hidl_cb(Status::OK, static_cast<ssize_t>(offset), "");
163 return Void();
164 } else if (mode == Mode::AES_CTR) {
165 size_t bytesDecrypted;
166 Status res = mSession->decrypt(keyId.data(), iv.data(), srcPtr,
167 static_cast<uint8_t*>(destPtr), toVector(subSamples), &bytesDecrypted);
168 if (res == Status::OK) {
169 _hidl_cb(Status::OK, static_cast<ssize_t>(bytesDecrypted), "");
170 return Void();
171 } else {
172 _hidl_cb(Status::ERROR_DRM_DECRYPT, static_cast<ssize_t>(res),
173 "Decryption Error");
174 return Void();
175 }
176 } else {
177 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, 0,
178 "Selected encryption mode is not supported by the ClearKey DRM Plugin.");
179 return Void();
180 }
181 }
182
setMediaDrmSession(const hidl_vec<uint8_t> & sessionId)183 Return<Status> CryptoPlugin::setMediaDrmSession(
184 const hidl_vec<uint8_t>& sessionId) {
185 if (!sessionId.size()) {
186 mSession = nullptr;
187 } else {
188 mSession = SessionLibrary::get()->findSession(sessionId);
189 if (!mSession.get()) {
190 return Status::ERROR_DRM_SESSION_NOT_OPENED;
191 }
192 }
193 return Status::OK;
194 }
195
196 } // namespace clearkey
197 } // namespace V1_1
198 } // namespace drm
199 } // namespace hardware
200 } // namespace android
201