blob: f51aaa2b5ea8cd0f1c4bceb6f77b0e106e92dd3e [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>
22#include <media/ICrypto.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070023#include <media/stagefright/MediaErrors.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070024#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5b8987e2012-04-19 12:52:20 -070025#include <media/stagefright/foundation/AString.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070026
27namespace android {
28
29enum {
Andreas Huber1bd139a2012-04-03 14:19:20 -070030 INIT_CHECK = IBinder::FIRST_CALL_TRANSACTION,
31 IS_CRYPTO_SUPPORTED,
32 CREATE_PLUGIN,
33 DESTROY_PLUGIN,
34 REQUIRES_SECURE_COMPONENT,
35 DECRYPT,
Jeff Tinker2514d082014-11-03 13:29:35 -080036 NOTIFY_RESOLUTION,
Andreas Hubered3e3e02012-03-26 11:13:27 -070037};
38
39struct BpCrypto : public BpInterface<ICrypto> {
40 BpCrypto(const sp<IBinder> &impl)
41 : BpInterface<ICrypto>(impl) {
42 }
43
Andreas Huber1bd139a2012-04-03 14:19:20 -070044 virtual status_t initCheck() const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070045 Parcel data, reply;
46 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070047 remote()->transact(INIT_CHECK, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070048
49 return reply.readInt32();
50 }
51
Jeff Tinkerbafb6822013-03-22 15:26:39 -070052 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070053 Parcel data, reply;
54 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070055 data.write(uuid, 16);
56 remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
57
58 return reply.readInt32() != 0;
59 }
60
61 virtual status_t createPlugin(
62 const uint8_t uuid[16], const void *opaqueData, size_t opaqueSize) {
63 Parcel data, reply;
64 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
65 data.write(uuid, 16);
66 data.writeInt32(opaqueSize);
Andreas Huber705868c2012-04-11 15:41:45 -070067
68 if (opaqueSize > 0) {
69 data.write(opaqueData, opaqueSize);
70 }
71
Andreas Huber1bd139a2012-04-03 14:19:20 -070072 remote()->transact(CREATE_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070073
74 return reply.readInt32();
75 }
76
Andreas Huber1bd139a2012-04-03 14:19:20 -070077 virtual status_t destroyPlugin() {
Andreas Hubered3e3e02012-03-26 11:13:27 -070078 Parcel data, reply;
79 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070080 remote()->transact(DESTROY_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070081
82 return reply.readInt32();
83 }
84
Andreas Huber1bd139a2012-04-03 14:19:20 -070085 virtual bool requiresSecureDecoderComponent(
86 const char *mime) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070087 Parcel data, reply;
88 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070089 data.writeCString(mime);
90 remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070091
Andreas Huber1bd139a2012-04-03 14:19:20 -070092 return reply.readInt32() != 0;
Andreas Hubered3e3e02012-03-26 11:13:27 -070093 }
94
Edwin Wongfa2b8f22012-07-10 20:01:13 -070095 virtual ssize_t decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -070096 bool secure,
97 const uint8_t key[16],
98 const uint8_t iv[16],
99 CryptoPlugin::Mode mode,
100 const void *srcPtr,
101 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700102 void *dstPtr,
103 AString *errorDetailMsg) {
Andreas Hubered3e3e02012-03-26 11:13:27 -0700104 Parcel data, reply;
105 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -0700106 data.writeInt32(secure);
107 data.writeInt32(mode);
Andreas Huber4b75a9c2012-04-06 11:06:28 -0700108
109 static const uint8_t kDummy[16] = { 0 };
110
111 if (key == NULL) {
112 key = kDummy;
113 }
114
115 if (iv == NULL) {
116 iv = kDummy;
117 }
118
Andreas Huber1bd139a2012-04-03 14:19:20 -0700119 data.write(key, 16);
120 data.write(iv, 16);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700121
Andreas Huber1bd139a2012-04-03 14:19:20 -0700122 size_t totalSize = 0;
123 for (size_t i = 0; i < numSubSamples; ++i) {
124 totalSize += subSamples[i].mNumBytesOfEncryptedData;
125 totalSize += subSamples[i].mNumBytesOfClearData;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700126 }
127
Andreas Huber1bd139a2012-04-03 14:19:20 -0700128 data.writeInt32(totalSize);
129 data.write(srcPtr, totalSize);
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
Andreas Huber1bd139a2012-04-03 14:19:20 -0700134 if (secure) {
Jeff Tinkerbcca9e02014-06-09 15:51:38 -0700135 data.writeInt64(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(dstPtr)));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700136 }
137
Andreas Huber1bd139a2012-04-03 14:19:20 -0700138 remote()->transact(DECRYPT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700139
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700140 ssize_t result = reply.readInt32();
Andreas Hubered3e3e02012-03-26 11:13:27 -0700141
Andreas Huber5b8987e2012-04-19 12:52:20 -0700142 if (result >= ERROR_DRM_VENDOR_MIN && result <= ERROR_DRM_VENDOR_MAX) {
143 errorDetailMsg->setTo(reply.readCString());
144 }
145
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700146 if (!secure && result >= 0) {
147 reply.read(dstPtr, result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700148 }
149
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700150 return result;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700151 }
152
Jeff Tinker2514d082014-11-03 13:29:35 -0800153 virtual void notifyResolution(
154 uint32_t width, uint32_t height) {
155 Parcel data, reply;
156 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
157 data.writeInt32(width);
158 data.writeInt32(height);
159 remote()->transact(NOTIFY_RESOLUTION, data, &reply);
160 }
161
Andreas Hubered3e3e02012-03-26 11:13:27 -0700162private:
163 DISALLOW_EVIL_CONSTRUCTORS(BpCrypto);
164};
165
166IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto");
167
168////////////////////////////////////////////////////////////////////////////////
169
170status_t BnCrypto::onTransact(
171 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
172 switch (code) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700173 case INIT_CHECK:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700174 {
175 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700176 reply->writeInt32(initCheck());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700177
178 return OK;
179 }
180
Andreas Huber1bd139a2012-04-03 14:19:20 -0700181 case IS_CRYPTO_SUPPORTED:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700182 {
183 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700184 uint8_t uuid[16];
185 data.read(uuid, sizeof(uuid));
186 reply->writeInt32(isCryptoSchemeSupported(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700187
188 return OK;
189 }
190
Andreas Huber1bd139a2012-04-03 14:19:20 -0700191 case CREATE_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700192 {
193 CHECK_INTERFACE(ICrypto, data, reply);
194
Andreas Huber1bd139a2012-04-03 14:19:20 -0700195 uint8_t uuid[16];
196 data.read(uuid, sizeof(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700197
Andreas Huber1bd139a2012-04-03 14:19:20 -0700198 size_t opaqueSize = data.readInt32();
Andreas Huber705868c2012-04-11 15:41:45 -0700199 void *opaqueData = NULL;
200
201 if (opaqueSize > 0) {
202 opaqueData = malloc(opaqueSize);
203 data.read(opaqueData, opaqueSize);
204 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700205
Andreas Huber1bd139a2012-04-03 14:19:20 -0700206 reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize));
207
Andreas Huber705868c2012-04-11 15:41:45 -0700208 if (opaqueData != NULL) {
209 free(opaqueData);
210 opaqueData = NULL;
211 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700212
213 return OK;
214 }
215
Andreas Huber1bd139a2012-04-03 14:19:20 -0700216 case DESTROY_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700217 {
218 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700219 reply->writeInt32(destroyPlugin());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700220
221 return OK;
222 }
223
Andreas Huber1bd139a2012-04-03 14:19:20 -0700224 case REQUIRES_SECURE_COMPONENT:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700225 {
226 CHECK_INTERFACE(ICrypto, data, reply);
227
Andreas Huber1bd139a2012-04-03 14:19:20 -0700228 const char *mime = data.readCString();
229 reply->writeInt32(requiresSecureDecoderComponent(mime));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700230
Andreas Huber1bd139a2012-04-03 14:19:20 -0700231 return OK;
232 }
233
234 case DECRYPT:
235 {
236 CHECK_INTERFACE(ICrypto, data, reply);
237
238 bool secure = data.readInt32() != 0;
239 CryptoPlugin::Mode mode = (CryptoPlugin::Mode)data.readInt32();
240
241 uint8_t key[16];
242 data.read(key, sizeof(key));
243
244 uint8_t iv[16];
245 data.read(iv, sizeof(iv));
246
247 size_t totalSize = data.readInt32();
248 void *srcData = malloc(totalSize);
Jeff Tinker5e7e87a2015-09-16 10:26:28 -0700249 memset(srcData, 0, totalSize);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700250 data.read(srcData, totalSize);
251
252 int32_t numSubSamples = data.readInt32();
253
254 CryptoPlugin::SubSample *subSamples =
255 new CryptoPlugin::SubSample[numSubSamples];
256
257 data.read(
258 subSamples,
259 sizeof(CryptoPlugin::SubSample) * numSubSamples);
260
Jeff Tinkeraa4da6f2015-08-17 17:57:47 -0700261 void *secureBufferId, *dstPtr;
Andreas Huber1bd139a2012-04-03 14:19:20 -0700262 if (secure) {
Jeff Tinkeraa4da6f2015-08-17 17:57:47 -0700263 secureBufferId = reinterpret_cast<void *>(static_cast<uintptr_t>(data.readInt64()));
Andreas Huber1bd139a2012-04-03 14:19:20 -0700264 } else {
265 dstPtr = malloc(totalSize);
Jeff Tinker5e7e87a2015-09-16 10:26:28 -0700266 memset(dstPtr, 0, totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700267 }
268
Andreas Huber5b8987e2012-04-19 12:52:20 -0700269 AString errorDetailMsg;
Jeff Tinker4b219e92015-09-14 11:55:43 -0700270 ssize_t result;
271
272 size_t sumSubsampleSizes = 0;
273 bool overflow = false;
274 for (int32_t i = 0; i < numSubSamples; ++i) {
275 CryptoPlugin::SubSample &ss = subSamples[i];
276 if (sumSubsampleSizes <= SIZE_MAX - ss.mNumBytesOfEncryptedData) {
277 sumSubsampleSizes += ss.mNumBytesOfEncryptedData;
278 } else {
279 overflow = true;
280 }
281 if (sumSubsampleSizes <= SIZE_MAX - ss.mNumBytesOfClearData) {
282 sumSubsampleSizes += ss.mNumBytesOfClearData;
283 } else {
284 overflow = true;
285 }
286 }
287
288 if (overflow || sumSubsampleSizes != totalSize) {
289 result = -EINVAL;
290 } else {
291 result = decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700292 secure,
293 key,
294 iv,
295 mode,
296 srcData,
297 subSamples, numSubSamples,
Jeff Tinkeraa4da6f2015-08-17 17:57:47 -0700298 secure ? secureBufferId : dstPtr,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700299 &errorDetailMsg);
Jeff Tinker4b219e92015-09-14 11:55:43 -0700300 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700301
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700302 reply->writeInt32(result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700303
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700304 if (result >= ERROR_DRM_VENDOR_MIN
305 && result <= ERROR_DRM_VENDOR_MAX) {
Andreas Huber5b8987e2012-04-19 12:52:20 -0700306 reply->writeCString(errorDetailMsg.c_str());
307 }
308
Andreas Huber1bd139a2012-04-03 14:19:20 -0700309 if (!secure) {
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700310 if (result >= 0) {
311 CHECK_LE(result, static_cast<ssize_t>(totalSize));
312 reply->write(dstPtr, result);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700313 }
Andreas Huber1bd139a2012-04-03 14:19:20 -0700314 free(dstPtr);
315 dstPtr = NULL;
316 }
317
318 delete[] subSamples;
319 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700320
321 free(srcData);
322 srcData = NULL;
323
Andreas Hubered3e3e02012-03-26 11:13:27 -0700324 return OK;
325 }
326
Jeff Tinker2514d082014-11-03 13:29:35 -0800327 case NOTIFY_RESOLUTION:
328 {
329 CHECK_INTERFACE(ICrypto, data, reply);
330
331 int32_t width = data.readInt32();
332 int32_t height = data.readInt32();
333 notifyResolution(width, height);
334
335 return OK;
336 }
337
Andreas Hubered3e3e02012-03-26 11:13:27 -0700338 default:
339 return BBinder::onTransact(code, data, reply, flags);
340 }
341}
342
343} // namespace android