blob: c26c5bf4ae614ec7d0ce8697cd40899af4d7bd76 [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);
249 data.read(srcData, totalSize);
250
251 int32_t numSubSamples = data.readInt32();
252
253 CryptoPlugin::SubSample *subSamples =
254 new CryptoPlugin::SubSample[numSubSamples];
255
256 data.read(
257 subSamples,
258 sizeof(CryptoPlugin::SubSample) * numSubSamples);
259
260 void *dstPtr;
261 if (secure) {
Jeff Tinkerbcca9e02014-06-09 15:51:38 -0700262 dstPtr = reinterpret_cast<void *>(static_cast<uintptr_t>(data.readInt64()));
Andreas Huber1bd139a2012-04-03 14:19:20 -0700263 } else {
264 dstPtr = malloc(totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700265 }
266
Andreas Huber5b8987e2012-04-19 12:52:20 -0700267 AString errorDetailMsg;
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700268 ssize_t result = decrypt(
Andreas Huber1bd139a2012-04-03 14:19:20 -0700269 secure,
270 key,
271 iv,
272 mode,
273 srcData,
274 subSamples, numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700275 dstPtr,
276 &errorDetailMsg);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700277
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700278 reply->writeInt32(result);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700279
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700280 if (result >= ERROR_DRM_VENDOR_MIN
281 && result <= ERROR_DRM_VENDOR_MAX) {
Andreas Huber5b8987e2012-04-19 12:52:20 -0700282 reply->writeCString(errorDetailMsg.c_str());
283 }
284
Andreas Huber1bd139a2012-04-03 14:19:20 -0700285 if (!secure) {
Edwin Wongfa2b8f22012-07-10 20:01:13 -0700286 if (result >= 0) {
287 CHECK_LE(result, static_cast<ssize_t>(totalSize));
288 reply->write(dstPtr, result);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700289 }
Andreas Huber1bd139a2012-04-03 14:19:20 -0700290 free(dstPtr);
291 dstPtr = NULL;
292 }
293
294 delete[] subSamples;
295 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700296
297 free(srcData);
298 srcData = NULL;
299
Andreas Hubered3e3e02012-03-26 11:13:27 -0700300 return OK;
301 }
302
Jeff Tinker2514d082014-11-03 13:29:35 -0800303 case NOTIFY_RESOLUTION:
304 {
305 CHECK_INTERFACE(ICrypto, data, reply);
306
307 int32_t width = data.readInt32();
308 int32_t height = data.readInt32();
309 notifyResolution(width, height);
310
311 return OK;
312 }
313
Andreas Hubered3e3e02012-03-26 11:13:27 -0700314 default:
315 return BBinder::onTransact(code, data, reply, flags);
316 }
317}
318
319} // namespace android