blob: 3452e5c62d5376cbe90fc876afaade7318e4bf85 [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,
Andreas Hubered3e3e02012-03-26 11:13:27 -070036};
37
38struct BpCrypto : public BpInterface<ICrypto> {
39 BpCrypto(const sp<IBinder> &impl)
40 : BpInterface<ICrypto>(impl) {
41 }
42
Andreas Huber1bd139a2012-04-03 14:19:20 -070043 virtual status_t initCheck() const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070044 Parcel data, reply;
45 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070046 remote()->transact(INIT_CHECK, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070047
48 return reply.readInt32();
49 }
50
Andreas Huber1bd139a2012-04-03 14:19:20 -070051 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070052 Parcel data, reply;
53 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070054 data.write(uuid, 16);
55 remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
56
57 return reply.readInt32() != 0;
58 }
59
60 virtual status_t createPlugin(
61 const uint8_t uuid[16], const void *opaqueData, size_t opaqueSize) {
62 Parcel data, reply;
63 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
64 data.write(uuid, 16);
65 data.writeInt32(opaqueSize);
Andreas Huber705868c2012-04-11 15:41:45 -070066
67 if (opaqueSize > 0) {
68 data.write(opaqueData, opaqueSize);
69 }
70
Andreas Huber1bd139a2012-04-03 14:19:20 -070071 remote()->transact(CREATE_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070072
73 return reply.readInt32();
74 }
75
Andreas Huber1bd139a2012-04-03 14:19:20 -070076 virtual status_t destroyPlugin() {
Andreas Hubered3e3e02012-03-26 11:13:27 -070077 Parcel data, reply;
78 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070079 remote()->transact(DESTROY_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070080
81 return reply.readInt32();
82 }
83
Andreas Huber1bd139a2012-04-03 14:19:20 -070084 virtual bool requiresSecureDecoderComponent(
85 const char *mime) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070086 Parcel data, reply;
87 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070088 data.writeCString(mime);
89 remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070090
Andreas Huber1bd139a2012-04-03 14:19:20 -070091 return reply.readInt32() != 0;
Andreas Hubered3e3e02012-03-26 11:13:27 -070092 }
93
Andreas Huber1bd139a2012-04-03 14:19:20 -070094 virtual status_t decrypt(
95 bool secure,
96 const uint8_t key[16],
97 const uint8_t iv[16],
98 CryptoPlugin::Mode mode,
99 const void *srcPtr,
100 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700101 void *dstPtr,
102 AString *errorDetailMsg) {
Andreas Hubered3e3e02012-03-26 11:13:27 -0700103 Parcel data, reply;
104 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -0700105 data.writeInt32(secure);
106 data.writeInt32(mode);
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);
128 data.write(srcPtr, totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700129
Andreas Huber1bd139a2012-04-03 14:19:20 -0700130 data.writeInt32(numSubSamples);
131 data.write(subSamples, sizeof(CryptoPlugin::SubSample) * numSubSamples);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700132
Andreas Huber1bd139a2012-04-03 14:19:20 -0700133 if (secure) {
134 data.writeIntPtr((intptr_t)dstPtr);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700135 }
136
Andreas Huber1bd139a2012-04-03 14:19:20 -0700137 remote()->transact(DECRYPT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700138
Andreas Huber1bd139a2012-04-03 14:19:20 -0700139 status_t result = reply.readInt32();
Andreas Hubered3e3e02012-03-26 11:13:27 -0700140
Andreas Huber5b8987e2012-04-19 12:52:20 -0700141 if (result >= ERROR_DRM_VENDOR_MIN && result <= ERROR_DRM_VENDOR_MAX) {
142 errorDetailMsg->setTo(reply.readCString());
143 }
144
Andreas Huber1bd139a2012-04-03 14:19:20 -0700145 if (result != OK) {
146 return result;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700147 }
148
Andreas Huber1bd139a2012-04-03 14:19:20 -0700149 if (!secure) {
150 reply.read(dstPtr, totalSize);
151 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700152
Andreas Huber1bd139a2012-04-03 14:19:20 -0700153 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700154 }
155
156private:
157 DISALLOW_EVIL_CONSTRUCTORS(BpCrypto);
158};
159
160IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto");
161
162////////////////////////////////////////////////////////////////////////////////
163
164status_t BnCrypto::onTransact(
165 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
166 switch (code) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700167 case INIT_CHECK:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700168 {
169 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700170 reply->writeInt32(initCheck());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700171
172 return OK;
173 }
174
Andreas Huber1bd139a2012-04-03 14:19:20 -0700175 case IS_CRYPTO_SUPPORTED:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700176 {
177 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700178 uint8_t uuid[16];
179 data.read(uuid, sizeof(uuid));
180 reply->writeInt32(isCryptoSchemeSupported(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700181
182 return OK;
183 }
184
Andreas Huber1bd139a2012-04-03 14:19:20 -0700185 case CREATE_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700186 {
187 CHECK_INTERFACE(ICrypto, data, reply);
188
Andreas Huber1bd139a2012-04-03 14:19:20 -0700189 uint8_t uuid[16];
190 data.read(uuid, sizeof(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700191
Andreas Huber1bd139a2012-04-03 14:19:20 -0700192 size_t opaqueSize = data.readInt32();
Andreas Huber705868c2012-04-11 15:41:45 -0700193 void *opaqueData = NULL;
194
195 if (opaqueSize > 0) {
196 opaqueData = malloc(opaqueSize);
197 data.read(opaqueData, opaqueSize);
198 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700199
Andreas Huber1bd139a2012-04-03 14:19:20 -0700200 reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize));
201
Andreas Huber705868c2012-04-11 15:41:45 -0700202 if (opaqueData != NULL) {
203 free(opaqueData);
204 opaqueData = NULL;
205 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700206
207 return OK;
208 }
209
Andreas Huber1bd139a2012-04-03 14:19:20 -0700210 case DESTROY_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700211 {
212 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700213 reply->writeInt32(destroyPlugin());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700214
215 return OK;
216 }
217
Andreas Huber1bd139a2012-04-03 14:19:20 -0700218 case REQUIRES_SECURE_COMPONENT:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700219 {
220 CHECK_INTERFACE(ICrypto, data, reply);
221
Andreas Huber1bd139a2012-04-03 14:19:20 -0700222 const char *mime = data.readCString();
223 reply->writeInt32(requiresSecureDecoderComponent(mime));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700224
Andreas Huber1bd139a2012-04-03 14:19:20 -0700225 return OK;
226 }
227
228 case DECRYPT:
229 {
230 CHECK_INTERFACE(ICrypto, data, reply);
231
232 bool secure = data.readInt32() != 0;
233 CryptoPlugin::Mode mode = (CryptoPlugin::Mode)data.readInt32();
234
235 uint8_t key[16];
236 data.read(key, sizeof(key));
237
238 uint8_t iv[16];
239 data.read(iv, sizeof(iv));
240
241 size_t totalSize = data.readInt32();
242 void *srcData = malloc(totalSize);
243 data.read(srcData, totalSize);
244
245 int32_t numSubSamples = data.readInt32();
246
247 CryptoPlugin::SubSample *subSamples =
248 new CryptoPlugin::SubSample[numSubSamples];
249
250 data.read(
251 subSamples,
252 sizeof(CryptoPlugin::SubSample) * numSubSamples);
253
254 void *dstPtr;
255 if (secure) {
256 dstPtr = (void *)data.readIntPtr();
257 } else {
258 dstPtr = malloc(totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700259 }
260
Andreas Huber5b8987e2012-04-19 12:52:20 -0700261 AString errorDetailMsg;
262
Andreas Huber1bd139a2012-04-03 14:19:20 -0700263 status_t err = decrypt(
264 secure,
265 key,
266 iv,
267 mode,
268 srcData,
269 subSamples, numSubSamples,
Andreas Huber5b8987e2012-04-19 12:52:20 -0700270 dstPtr,
271 &errorDetailMsg);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700272
Andreas Huber1bd139a2012-04-03 14:19:20 -0700273 reply->writeInt32(err);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700274
Andreas Huber5b8987e2012-04-19 12:52:20 -0700275 if (err >= ERROR_DRM_VENDOR_MIN
276 && err <= ERROR_DRM_VENDOR_MAX) {
277 reply->writeCString(errorDetailMsg.c_str());
278 }
279
Andreas Huber1bd139a2012-04-03 14:19:20 -0700280 if (!secure) {
281 if (err == OK) {
282 reply->write(dstPtr, totalSize);
283 }
284
285 free(dstPtr);
286 dstPtr = NULL;
287 }
288
289 delete[] subSamples;
290 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700291
292 free(srcData);
293 srcData = NULL;
294
Andreas Hubered3e3e02012-03-26 11:13:27 -0700295 return OK;
296 }
297
298 default:
299 return BBinder::onTransact(code, data, reply, flags);
300 }
301}
302
303} // namespace android
304