blob: 23308c17ed618bb88124af6717b4518bda69192d [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,
Andreas Hubered3e3e02012-03-26 11:13:27 -070038};
39
40struct BpCrypto : public BpInterface<ICrypto> {
41 BpCrypto(const sp<IBinder> &impl)
42 : BpInterface<ICrypto>(impl) {
43 }
44
Andreas Huber1bd139a2012-04-03 14:19:20 -070045 virtual status_t initCheck() const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070046 Parcel data, reply;
47 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070048 remote()->transact(INIT_CHECK, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070049
50 return reply.readInt32();
51 }
52
Jeff Tinkerbafb6822013-03-22 15:26:39 -070053 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070054 Parcel data, reply;
55 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070056 data.write(uuid, 16);
57 remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
58
59 return reply.readInt32() != 0;
60 }
61
62 virtual status_t createPlugin(
63 const uint8_t uuid[16], const void *opaqueData, size_t opaqueSize) {
64 Parcel data, reply;
65 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
66 data.write(uuid, 16);
67 data.writeInt32(opaqueSize);
Andreas Huber705868c2012-04-11 15:41:45 -070068
69 if (opaqueSize > 0) {
70 data.write(opaqueData, opaqueSize);
71 }
72
Andreas Huber1bd139a2012-04-03 14:19:20 -070073 remote()->transact(CREATE_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070074
75 return reply.readInt32();
76 }
77
Andreas Huber1bd139a2012-04-03 14:19:20 -070078 virtual status_t destroyPlugin() {
Andreas Hubered3e3e02012-03-26 11:13:27 -070079 Parcel data, reply;
80 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070081 remote()->transact(DESTROY_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070082
83 return reply.readInt32();
84 }
85
Andreas Huber1bd139a2012-04-03 14:19:20 -070086 virtual bool requiresSecureDecoderComponent(
87 const char *mime) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070088 Parcel data, reply;
89 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070090 data.writeCString(mime);
91 remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070092
Andreas Huber1bd139a2012-04-03 14:19:20 -070093 return reply.readInt32() != 0;
Andreas Hubered3e3e02012-03-26 11:13:27 -070094 }
95
Edwin Wongfa2b8f22012-07-10 20:01:13 -070096 virtual ssize_t decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -070097 bool secure,
98 const uint8_t key[16],
99 const uint8_t iv[16],
100 CryptoPlugin::Mode mode,
Jeff Tinkerc481b502015-04-06 18:21:05 -0700101 const sp<IMemory> &sharedBuffer, size_t offset,
Andreas Huber1bd139a2012-04-03 14:19:20 -0700102 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700103 void *dstPtr,
104 AString *errorDetailMsg) {
Andreas Hubered3e3e02012-03-26 11:13:27 -0700105 Parcel data, reply;
106 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -0700107 data.writeInt32(secure);
108 data.writeInt32(mode);
Andreas Huber4b75a9c2012-04-06 11:06:28 -0700109
110 static const uint8_t kDummy[16] = { 0 };
111
112 if (key == NULL) {
113 key = kDummy;
114 }
115
116 if (iv == NULL) {
117 iv = kDummy;
118 }
119
Andreas Huber1bd139a2012-04-03 14:19:20 -0700120 data.write(key, 16);
121 data.write(iv, 16);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700122
Andreas Huber1bd139a2012-04-03 14:19:20 -0700123 size_t totalSize = 0;
124 for (size_t i = 0; i < numSubSamples; ++i) {
125 totalSize += subSamples[i].mNumBytesOfEncryptedData;
126 totalSize += subSamples[i].mNumBytesOfClearData;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700127 }
128
Andreas Huber1bd139a2012-04-03 14:19:20 -0700129 data.writeInt32(totalSize);
Jeff Tinkerc481b502015-04-06 18:21:05 -0700130 data.writeStrongBinder(IInterface::asBinder(sharedBuffer));
131 data.writeInt32(offset);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700132
Andreas Huber1bd139a2012-04-03 14:19:20 -0700133 data.writeInt32(numSubSamples);
134 data.write(subSamples, sizeof(CryptoPlugin::SubSample) * numSubSamples);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700135
Andreas Huber1bd139a2012-04-03 14:19:20 -0700136 if (secure) {
Jeff Tinkerbcca9e02014-06-09 15:51:38 -0700137 data.writeInt64(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(dstPtr)));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700138 }
139
Andreas Huber1bd139a2012-04-03 14:19:20 -0700140 remote()->transact(DECRYPT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700141
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700142 ssize_t result = reply.readInt32();
Andreas Hubered3e3e02012-03-26 11:13:27 -0700143
Andreas Huber5b8987e2012-04-19 12:52:20 -0700144 if (result >= ERROR_DRM_VENDOR_MIN && result <= ERROR_DRM_VENDOR_MAX) {
145 errorDetailMsg->setTo(reply.readCString());
146 }
147
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700148 if (!secure && result >= 0) {
149 reply.read(dstPtr, result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700150 }
151
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700152 return result;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700153 }
154
Jeff Tinker2514d082014-11-03 13:29:35 -0800155 virtual void notifyResolution(
156 uint32_t width, uint32_t height) {
157 Parcel data, reply;
158 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
159 data.writeInt32(width);
160 data.writeInt32(height);
161 remote()->transact(NOTIFY_RESOLUTION, data, &reply);
162 }
163
Andreas Hubered3e3e02012-03-26 11:13:27 -0700164private:
165 DISALLOW_EVIL_CONSTRUCTORS(BpCrypto);
166};
167
168IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto");
169
170////////////////////////////////////////////////////////////////////////////////
171
172status_t BnCrypto::onTransact(
173 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
174 switch (code) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700175 case INIT_CHECK:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700176 {
177 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700178 reply->writeInt32(initCheck());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700179
180 return OK;
181 }
182
Andreas Huber1bd139a2012-04-03 14:19:20 -0700183 case IS_CRYPTO_SUPPORTED:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700184 {
185 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700186 uint8_t uuid[16];
187 data.read(uuid, sizeof(uuid));
188 reply->writeInt32(isCryptoSchemeSupported(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700189
190 return OK;
191 }
192
Andreas Huber1bd139a2012-04-03 14:19:20 -0700193 case CREATE_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700194 {
195 CHECK_INTERFACE(ICrypto, data, reply);
196
Andreas Huber1bd139a2012-04-03 14:19:20 -0700197 uint8_t uuid[16];
198 data.read(uuid, sizeof(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700199
Andreas Huber1bd139a2012-04-03 14:19:20 -0700200 size_t opaqueSize = data.readInt32();
Andreas Huber705868c2012-04-11 15:41:45 -0700201 void *opaqueData = NULL;
202
203 if (opaqueSize > 0) {
204 opaqueData = malloc(opaqueSize);
205 data.read(opaqueData, opaqueSize);
206 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700207
Andreas Huber1bd139a2012-04-03 14:19:20 -0700208 reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize));
209
Andreas Huber705868c2012-04-11 15:41:45 -0700210 if (opaqueData != NULL) {
211 free(opaqueData);
212 opaqueData = NULL;
213 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700214
215 return OK;
216 }
217
Andreas Huber1bd139a2012-04-03 14:19:20 -0700218 case DESTROY_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700219 {
220 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700221 reply->writeInt32(destroyPlugin());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700222
223 return OK;
224 }
225
Andreas Huber1bd139a2012-04-03 14:19:20 -0700226 case REQUIRES_SECURE_COMPONENT:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700227 {
228 CHECK_INTERFACE(ICrypto, data, reply);
229
Andreas Huber1bd139a2012-04-03 14:19:20 -0700230 const char *mime = data.readCString();
231 reply->writeInt32(requiresSecureDecoderComponent(mime));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700232
Andreas Huber1bd139a2012-04-03 14:19:20 -0700233 return OK;
234 }
235
236 case DECRYPT:
237 {
238 CHECK_INTERFACE(ICrypto, data, reply);
239
240 bool secure = data.readInt32() != 0;
241 CryptoPlugin::Mode mode = (CryptoPlugin::Mode)data.readInt32();
242
243 uint8_t key[16];
244 data.read(key, sizeof(key));
245
246 uint8_t iv[16];
247 data.read(iv, sizeof(iv));
248
249 size_t totalSize = data.readInt32();
Jeff Tinkerc481b502015-04-06 18:21:05 -0700250 sp<IMemory> sharedBuffer =
251 interface_cast<IMemory>(data.readStrongBinder());
252 int32_t offset = data.readInt32();
Andreas Huber1bd139a2012-04-03 14:19:20 -0700253
254 int32_t numSubSamples = data.readInt32();
255
256 CryptoPlugin::SubSample *subSamples =
257 new CryptoPlugin::SubSample[numSubSamples];
258
259 data.read(
260 subSamples,
261 sizeof(CryptoPlugin::SubSample) * numSubSamples);
262
263 void *dstPtr;
264 if (secure) {
Jeff Tinkerbcca9e02014-06-09 15:51:38 -0700265 dstPtr = reinterpret_cast<void *>(static_cast<uintptr_t>(data.readInt64()));
Andreas Huber1bd139a2012-04-03 14:19:20 -0700266 } else {
267 dstPtr = malloc(totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700268 }
269
Andreas Huber5b8987e2012-04-19 12:52:20 -0700270 AString errorDetailMsg;
Jeff Tinkerc481b502015-04-06 18:21:05 -0700271 ssize_t result;
272
273 if (offset + totalSize > sharedBuffer->size()) {
274 result = -EINVAL;
275 } else {
276 result = decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700277 secure,
278 key,
279 iv,
280 mode,
Jeff Tinkerc481b502015-04-06 18:21:05 -0700281 sharedBuffer, offset,
Andreas Huber1bd139a2012-04-03 14:19:20 -0700282 subSamples, numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700283 dstPtr,
284 &errorDetailMsg);
Jeff Tinkerc481b502015-04-06 18:21:05 -0700285 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700286
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700287 reply->writeInt32(result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700288
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700289 if (result >= ERROR_DRM_VENDOR_MIN
290 && result <= ERROR_DRM_VENDOR_MAX) {
Andreas Huber5b8987e2012-04-19 12:52:20 -0700291 reply->writeCString(errorDetailMsg.c_str());
292 }
293
Andreas Huber1bd139a2012-04-03 14:19:20 -0700294 if (!secure) {
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700295 if (result >= 0) {
296 CHECK_LE(result, static_cast<ssize_t>(totalSize));
297 reply->write(dstPtr, result);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700298 }
Andreas Huber1bd139a2012-04-03 14:19:20 -0700299 free(dstPtr);
300 dstPtr = NULL;
301 }
302
303 delete[] subSamples;
304 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700305
Andreas Hubered3e3e02012-03-26 11:13:27 -0700306 return OK;
307 }
308
Jeff Tinker2514d082014-11-03 13:29:35 -0800309 case NOTIFY_RESOLUTION:
310 {
311 CHECK_INTERFACE(ICrypto, data, reply);
312
313 int32_t width = data.readInt32();
314 int32_t height = data.readInt32();
315 notifyResolution(width, height);
316
317 return OK;
318 }
319
Andreas Hubered3e3e02012-03-26 11:13:27 -0700320 default:
321 return BBinder::onTransact(code, data, reply, flags);
322 }
323}
324
325} // namespace android