blob: 10e6bc34cfa2bf7d68df06374bf9c7925f9ea729 [file] [log] [blame]
Andreas Hubered3e3e02012-03-26 11:13:27 -07001/*
2 * Copyright (C) 2012 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 "ICrypto"
19#include <utils/Log.h>
20
21#include <binder/Parcel.h>
Jeff Tinkerc481b502015-04-06 18:21:05 -070022#include <binder/IMemory.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070023#include <media/ICrypto.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070024#include <media/stagefright/MediaErrors.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070025#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070026#include <media/stagefright/foundation/AString.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070027
28namespace android {
29
30enum {
Andreas Huber1bd139a2012-04-03 14:19:20 -070031 INIT_CHECK = IBinder::FIRST_CALL_TRANSACTION,
32 IS_CRYPTO_SUPPORTED,
33 CREATE_PLUGIN,
34 DESTROY_PLUGIN,
35 REQUIRES_SECURE_COMPONENT,
36 DECRYPT,
Jeff Tinker2514d082014-11-03 13:29:35 -080037 NOTIFY_RESOLUTION,
Jeff Tinker18495702015-04-10 04:10:59 -070038 SET_MEDIADRM_SESSION,
Andreas Hubered3e3e02012-03-26 11:13:27 -070039};
40
41struct BpCrypto : public BpInterface<ICrypto> {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070042 explicit BpCrypto(const sp<IBinder> &impl)
Andreas Hubered3e3e02012-03-26 11:13:27 -070043 : BpInterface<ICrypto>(impl) {
44 }
45
Andreas Huber1bd139a2012-04-03 14:19:20 -070046 virtual status_t initCheck() const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070047 Parcel data, reply;
48 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070049 remote()->transact(INIT_CHECK, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070050
51 return reply.readInt32();
52 }
53
Jeff Tinkerbafb6822013-03-22 15:26:39 -070054 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070055 Parcel data, reply;
56 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070057 data.write(uuid, 16);
58 remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
59
60 return reply.readInt32() != 0;
61 }
62
63 virtual status_t createPlugin(
64 const uint8_t uuid[16], const void *opaqueData, size_t opaqueSize) {
65 Parcel data, reply;
66 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
67 data.write(uuid, 16);
68 data.writeInt32(opaqueSize);
Andreas Huber705868c2012-04-11 15:41:45 -070069
70 if (opaqueSize > 0) {
71 data.write(opaqueData, opaqueSize);
72 }
73
Andreas Huber1bd139a2012-04-03 14:19:20 -070074 remote()->transact(CREATE_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070075
76 return reply.readInt32();
77 }
78
Andreas Huber1bd139a2012-04-03 14:19:20 -070079 virtual status_t destroyPlugin() {
Andreas Hubered3e3e02012-03-26 11:13:27 -070080 Parcel data, reply;
81 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070082 remote()->transact(DESTROY_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070083
84 return reply.readInt32();
85 }
86
Andreas Huber1bd139a2012-04-03 14:19:20 -070087 virtual bool requiresSecureDecoderComponent(
88 const char *mime) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070089 Parcel data, reply;
90 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070091 data.writeCString(mime);
92 remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070093
Andreas Huber1bd139a2012-04-03 14:19:20 -070094 return reply.readInt32() != 0;
Andreas Hubered3e3e02012-03-26 11:13:27 -070095 }
96
Jeff Tinkera53d6552017-01-20 00:31:46 -080097 virtual ssize_t decrypt(const uint8_t key[16], const uint8_t iv[16],
Jeff Tinker18cb1ec2015-12-18 11:55:22 -080098 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern,
Jeff Tinkera53d6552017-01-20 00:31:46 -080099 const sp<IMemory> &source, size_t offset,
Andreas Huber1bd139a2012-04-03 14:19:20 -0700100 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Jeff Tinkera53d6552017-01-20 00:31:46 -0800101 const DestinationBuffer &destination, AString *errorDetailMsg) {
Andreas Hubered3e3e02012-03-26 11:13:27 -0700102 Parcel data, reply;
103 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -0700104 data.writeInt32(mode);
Jeff Tinker18cb1ec2015-12-18 11:55:22 -0800105 data.writeInt32(pattern.mEncryptBlocks);
106 data.writeInt32(pattern.mSkipBlocks);
Andreas Huber4b75a9c2012-04-06 11:06:28 -0700107
108 static const uint8_t kDummy[16] = { 0 };
109
110 if (key == NULL) {
111 key = kDummy;
112 }
113
114 if (iv == NULL) {
115 iv = kDummy;
116 }
117
Andreas Huber1bd139a2012-04-03 14:19:20 -0700118 data.write(key, 16);
119 data.write(iv, 16);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700120
Andreas Huber1bd139a2012-04-03 14:19:20 -0700121 size_t totalSize = 0;
122 for (size_t i = 0; i < numSubSamples; ++i) {
123 totalSize += subSamples[i].mNumBytesOfEncryptedData;
124 totalSize += subSamples[i].mNumBytesOfClearData;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700125 }
126
Andreas Huber1bd139a2012-04-03 14:19:20 -0700127 data.writeInt32(totalSize);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800128 data.writeStrongBinder(IInterface::asBinder(source));
Jeff Tinkerc481b502015-04-06 18:21:05 -0700129 data.writeInt32(offset);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700130
Andreas Huber1bd139a2012-04-03 14:19:20 -0700131 data.writeInt32(numSubSamples);
132 data.write(subSamples, sizeof(CryptoPlugin::SubSample) * numSubSamples);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700133
Jeff Tinkera53d6552017-01-20 00:31:46 -0800134 data.writeInt32((int32_t)destination.mType);
135 if (destination.mType == kDestinationTypeNativeHandle) {
136 if (destination.mHandle == NULL) {
137 return BAD_VALUE;
138 }
139 data.writeNativeHandle(destination.mHandle);
Jeff Tinker9ac86b32016-01-23 17:27:58 -0800140 } else {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800141 if (destination.mSharedMemory == NULL) {
142 return BAD_VALUE;
143 }
144 data.writeStrongBinder(IInterface::asBinder(destination.mSharedMemory));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700145 }
146
Andreas Huber1bd139a2012-04-03 14:19:20 -0700147 remote()->transact(DECRYPT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700148
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700149 ssize_t result = reply.readInt32();
Andreas Hubered3e3e02012-03-26 11:13:27 -0700150
Jeff Tinkerceffd8c2015-05-05 15:09:14 -0700151 if (isCryptoError(result)) {
Andreas Huber5b8987e2012-04-19 12:52:20 -0700152 errorDetailMsg->setTo(reply.readCString());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700153 }
154
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700155 return result;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700156 }
157
Jeff Tinker2514d082014-11-03 13:29:35 -0800158 virtual void notifyResolution(
159 uint32_t width, uint32_t height) {
160 Parcel data, reply;
161 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
162 data.writeInt32(width);
163 data.writeInt32(height);
164 remote()->transact(NOTIFY_RESOLUTION, data, &reply);
165 }
166
Jeff Tinker18495702015-04-10 04:10:59 -0700167 virtual status_t setMediaDrmSession(const Vector<uint8_t> &sessionId) {
168 Parcel data, reply;
169 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
170
171 writeVector(data, sessionId);
172 remote()->transact(SET_MEDIADRM_SESSION, data, &reply);
173
174 return reply.readInt32();
175 }
176
Andreas Hubered3e3e02012-03-26 11:13:27 -0700177private:
Jeff Tinker18495702015-04-10 04:10:59 -0700178 void readVector(Parcel &reply, Vector<uint8_t> &vector) const {
179 uint32_t size = reply.readInt32();
180 vector.insertAt((size_t)0, size);
181 reply.read(vector.editArray(), size);
182 }
183
184 void writeVector(Parcel &data, Vector<uint8_t> const &vector) const {
185 data.writeInt32(vector.size());
186 data.write(vector.array(), vector.size());
187 }
188
Andreas Hubered3e3e02012-03-26 11:13:27 -0700189 DISALLOW_EVIL_CONSTRUCTORS(BpCrypto);
190};
191
192IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto");
193
194////////////////////////////////////////////////////////////////////////////////
195
Jeff Tinker18495702015-04-10 04:10:59 -0700196void BnCrypto::readVector(const Parcel &data, Vector<uint8_t> &vector) const {
197 uint32_t size = data.readInt32();
198 vector.insertAt((size_t)0, size);
199 data.read(vector.editArray(), size);
200}
201
202void BnCrypto::writeVector(Parcel *reply, Vector<uint8_t> const &vector) const {
203 reply->writeInt32(vector.size());
204 reply->write(vector.array(), vector.size());
205}
206
Andreas Hubered3e3e02012-03-26 11:13:27 -0700207status_t BnCrypto::onTransact(
208 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
209 switch (code) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700210 case INIT_CHECK:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700211 {
212 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700213 reply->writeInt32(initCheck());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700214
215 return OK;
216 }
217
Andreas Huber1bd139a2012-04-03 14:19:20 -0700218 case IS_CRYPTO_SUPPORTED:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700219 {
220 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700221 uint8_t uuid[16];
222 data.read(uuid, sizeof(uuid));
223 reply->writeInt32(isCryptoSchemeSupported(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700224
225 return OK;
226 }
227
Andreas Huber1bd139a2012-04-03 14:19:20 -0700228 case CREATE_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700229 {
230 CHECK_INTERFACE(ICrypto, data, reply);
231
Andreas Huber1bd139a2012-04-03 14:19:20 -0700232 uint8_t uuid[16];
233 data.read(uuid, sizeof(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700234
Andreas Huber1bd139a2012-04-03 14:19:20 -0700235 size_t opaqueSize = data.readInt32();
Andreas Huber705868c2012-04-11 15:41:45 -0700236 void *opaqueData = NULL;
237
238 if (opaqueSize > 0) {
239 opaqueData = malloc(opaqueSize);
240 data.read(opaqueData, opaqueSize);
241 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700242
Andreas Huber1bd139a2012-04-03 14:19:20 -0700243 reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize));
244
Andreas Huber705868c2012-04-11 15:41:45 -0700245 if (opaqueData != NULL) {
246 free(opaqueData);
247 opaqueData = NULL;
248 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700249
250 return OK;
251 }
252
Andreas Huber1bd139a2012-04-03 14:19:20 -0700253 case DESTROY_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700254 {
255 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700256 reply->writeInt32(destroyPlugin());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700257
258 return OK;
259 }
260
Andreas Huber1bd139a2012-04-03 14:19:20 -0700261 case REQUIRES_SECURE_COMPONENT:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700262 {
263 CHECK_INTERFACE(ICrypto, data, reply);
264
Andreas Huber1bd139a2012-04-03 14:19:20 -0700265 const char *mime = data.readCString();
Wei Jia2afac0c2016-01-07 12:13:07 -0800266 if (mime == NULL) {
267 reply->writeInt32(BAD_VALUE);
268 } else {
269 reply->writeInt32(requiresSecureDecoderComponent(mime));
270 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700271
Andreas Huber1bd139a2012-04-03 14:19:20 -0700272 return OK;
273 }
274
275 case DECRYPT:
276 {
277 CHECK_INTERFACE(ICrypto, data, reply);
278
Andreas Huber1bd139a2012-04-03 14:19:20 -0700279 CryptoPlugin::Mode mode = (CryptoPlugin::Mode)data.readInt32();
Jeff Tinker18cb1ec2015-12-18 11:55:22 -0800280 CryptoPlugin::Pattern pattern;
281 pattern.mEncryptBlocks = data.readInt32();
282 pattern.mSkipBlocks = data.readInt32();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700283
284 uint8_t key[16];
285 data.read(key, sizeof(key));
286
287 uint8_t iv[16];
288 data.read(iv, sizeof(iv));
289
290 size_t totalSize = data.readInt32();
Jeff Tinkera53d6552017-01-20 00:31:46 -0800291 sp<IMemory> source =
Jeff Tinkerc481b502015-04-06 18:21:05 -0700292 interface_cast<IMemory>(data.readStrongBinder());
Jeff Tinkera53d6552017-01-20 00:31:46 -0800293 if (source == NULL) {
Wei Jia2afac0c2016-01-07 12:13:07 -0800294 reply->writeInt32(BAD_VALUE);
295 return OK;
296 }
Jeff Tinkerc481b502015-04-06 18:21:05 -0700297 int32_t offset = data.readInt32();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700298
299 int32_t numSubSamples = data.readInt32();
Jeff Tinker4183d532016-05-20 17:19:31 -0700300 if (numSubSamples < 0 || numSubSamples > 0xffff) {
301 reply->writeInt32(BAD_VALUE);
302 return OK;
303 }
Andreas Huber1bd139a2012-04-03 14:19:20 -0700304
305 CryptoPlugin::SubSample *subSamples =
Jeff Tinkera53d6552017-01-20 00:31:46 -0800306 new CryptoPlugin::SubSample[numSubSamples];
Andreas Huber1bd139a2012-04-03 14:19:20 -0700307
Jeff Tinkera53d6552017-01-20 00:31:46 -0800308 data.read(subSamples,
Andreas Huber1bd139a2012-04-03 14:19:20 -0700309 sizeof(CryptoPlugin::SubSample) * numSubSamples);
310
Jeff Tinkera53d6552017-01-20 00:31:46 -0800311 DestinationBuffer destination;
312 destination.mType = (DestinationType)data.readInt32();
313 if (destination.mType == kDestinationTypeNativeHandle) {
314 destination.mHandle = data.readNativeHandle();
315 if (destination.mHandle == NULL) {
316 reply->writeInt32(BAD_VALUE);
317 return OK;
318 }
319 } else if (destination.mType == kDestinationTypeSharedMemory) {
320 destination.mSharedMemory =
321 interface_cast<IMemory>(data.readStrongBinder());
322 if (destination.mSharedMemory == NULL) {
323 reply->writeInt32(BAD_VALUE);
324 return OK;
325 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700326 }
327
Andreas Huber5b8987e2012-04-19 12:52:20 -0700328 AString errorDetailMsg;
Jeff Tinkerc481b502015-04-06 18:21:05 -0700329 ssize_t result;
330
Jeff Tinkerc6fc6a32015-08-26 20:22:39 -0700331 size_t sumSubsampleSizes = 0;
332 bool overflow = false;
333 for (int32_t i = 0; i < numSubSamples; ++i) {
334 CryptoPlugin::SubSample &ss = subSamples[i];
335 if (sumSubsampleSizes <= SIZE_MAX - ss.mNumBytesOfEncryptedData) {
336 sumSubsampleSizes += ss.mNumBytesOfEncryptedData;
337 } else {
338 overflow = true;
339 }
340 if (sumSubsampleSizes <= SIZE_MAX - ss.mNumBytesOfClearData) {
341 sumSubsampleSizes += ss.mNumBytesOfClearData;
342 } else {
343 overflow = true;
344 }
345 }
346
347 if (overflow || sumSubsampleSizes != totalSize) {
348 result = -EINVAL;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800349 } else if (totalSize > source->size()) {
Jeff Tinkerbb4877d2015-12-04 16:29:16 -0800350 result = -EINVAL;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800351 } else if ((size_t)offset > source->size() - totalSize) {
Jeff Tinkerc481b502015-04-06 18:21:05 -0700352 result = -EINVAL;
353 } else {
Jeff Tinkera53d6552017-01-20 00:31:46 -0800354 result = decrypt(key, iv, mode, pattern, source, offset,
355 subSamples, numSubSamples, destination, &errorDetailMsg);
Jeff Tinkerc481b502015-04-06 18:21:05 -0700356 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700357
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700358 reply->writeInt32(result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700359
Jeff Tinkerceffd8c2015-05-05 15:09:14 -0700360 if (isCryptoError(result)) {
Andreas Huber5b8987e2012-04-19 12:52:20 -0700361 reply->writeCString(errorDetailMsg.c_str());
362 }
363
Jeff Tinkera53d6552017-01-20 00:31:46 -0800364 if (destination.mType == kDestinationTypeNativeHandle) {
Jeff Tinker9ac86b32016-01-23 17:27:58 -0800365 int err;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800366 if ((err = native_handle_close(destination.mHandle)) < 0) {
Jeff Tinker9ac86b32016-01-23 17:27:58 -0800367 ALOGW("secure buffer native_handle_close failed: %d", err);
368 }
Jeff Tinkera53d6552017-01-20 00:31:46 -0800369 if ((err = native_handle_delete(destination.mHandle)) < 0) {
Jeff Tinker9ac86b32016-01-23 17:27:58 -0800370 ALOGW("secure buffer native_handle_delete failed: %d", err);
371 }
Andreas Huber1bd139a2012-04-03 14:19:20 -0700372 }
373
374 delete[] subSamples;
375 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700376
Andreas Hubered3e3e02012-03-26 11:13:27 -0700377 return OK;
378 }
379
Jeff Tinker2514d082014-11-03 13:29:35 -0800380 case NOTIFY_RESOLUTION:
381 {
382 CHECK_INTERFACE(ICrypto, data, reply);
383
384 int32_t width = data.readInt32();
385 int32_t height = data.readInt32();
386 notifyResolution(width, height);
387
388 return OK;
389 }
390
Jeff Tinker18495702015-04-10 04:10:59 -0700391 case SET_MEDIADRM_SESSION:
392 {
393 CHECK_INTERFACE(IDrm, data, reply);
394 Vector<uint8_t> sessionId;
395 readVector(data, sessionId);
396 reply->writeInt32(setMediaDrmSession(sessionId));
397 return OK;
398 }
399
Andreas Hubered3e3e02012-03-26 11:13:27 -0700400 default:
401 return BBinder::onTransact(code, data, reply, flags);
402 }
403}
404
405} // namespace android