Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 23 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 24 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 25 | #include <media/stagefright/foundation/AString.h> |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | enum { |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 30 | INIT_CHECK = IBinder::FIRST_CALL_TRANSACTION, |
| 31 | IS_CRYPTO_SUPPORTED, |
| 32 | CREATE_PLUGIN, |
| 33 | DESTROY_PLUGIN, |
| 34 | REQUIRES_SECURE_COMPONENT, |
| 35 | DECRYPT, |
Jeff Tinker | 2514d08 | 2014-11-03 13:29:35 -0800 | [diff] [blame] | 36 | NOTIFY_RESOLUTION, |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct BpCrypto : public BpInterface<ICrypto> { |
| 40 | BpCrypto(const sp<IBinder> &impl) |
| 41 | : BpInterface<ICrypto>(impl) { |
| 42 | } |
| 43 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 44 | virtual status_t initCheck() const { |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 45 | Parcel data, reply; |
| 46 | data.writeInterfaceToken(ICrypto::getInterfaceDescriptor()); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 47 | remote()->transact(INIT_CHECK, data, &reply); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 48 | |
| 49 | return reply.readInt32(); |
| 50 | } |
| 51 | |
Jeff Tinker | bafb682 | 2013-03-22 15:26:39 -0700 | [diff] [blame] | 52 | virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) { |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 53 | Parcel data, reply; |
| 54 | data.writeInterfaceToken(ICrypto::getInterfaceDescriptor()); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 55 | 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 Huber | 705868c | 2012-04-11 15:41:45 -0700 | [diff] [blame] | 67 | |
| 68 | if (opaqueSize > 0) { |
| 69 | data.write(opaqueData, opaqueSize); |
| 70 | } |
| 71 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 72 | remote()->transact(CREATE_PLUGIN, data, &reply); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 73 | |
| 74 | return reply.readInt32(); |
| 75 | } |
| 76 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 77 | virtual status_t destroyPlugin() { |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 78 | Parcel data, reply; |
| 79 | data.writeInterfaceToken(ICrypto::getInterfaceDescriptor()); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 80 | remote()->transact(DESTROY_PLUGIN, data, &reply); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 81 | |
| 82 | return reply.readInt32(); |
| 83 | } |
| 84 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 85 | virtual bool requiresSecureDecoderComponent( |
| 86 | const char *mime) const { |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 87 | Parcel data, reply; |
| 88 | data.writeInterfaceToken(ICrypto::getInterfaceDescriptor()); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 89 | data.writeCString(mime); |
| 90 | remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 91 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 92 | return reply.readInt32() != 0; |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 95 | virtual ssize_t decrypt( |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 96 | 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 Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 102 | void *dstPtr, |
| 103 | AString *errorDetailMsg) { |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 104 | Parcel data, reply; |
| 105 | data.writeInterfaceToken(ICrypto::getInterfaceDescriptor()); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 106 | data.writeInt32(secure); |
| 107 | data.writeInt32(mode); |
Andreas Huber | 4b75a9c | 2012-04-06 11:06:28 -0700 | [diff] [blame] | 108 | |
| 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 Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 119 | data.write(key, 16); |
| 120 | data.write(iv, 16); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 121 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 122 | size_t totalSize = 0; |
| 123 | for (size_t i = 0; i < numSubSamples; ++i) { |
| 124 | totalSize += subSamples[i].mNumBytesOfEncryptedData; |
| 125 | totalSize += subSamples[i].mNumBytesOfClearData; |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 128 | data.writeInt32(totalSize); |
| 129 | data.write(srcPtr, totalSize); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 130 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 131 | data.writeInt32(numSubSamples); |
| 132 | data.write(subSamples, sizeof(CryptoPlugin::SubSample) * numSubSamples); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 133 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 134 | if (secure) { |
Jeff Tinker | bcca9e0 | 2014-06-09 15:51:38 -0700 | [diff] [blame] | 135 | data.writeInt64(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(dstPtr))); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 138 | remote()->transact(DECRYPT, data, &reply); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 139 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 140 | ssize_t result = reply.readInt32(); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 141 | |
Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 142 | if (result >= ERROR_DRM_VENDOR_MIN && result <= ERROR_DRM_VENDOR_MAX) { |
| 143 | errorDetailMsg->setTo(reply.readCString()); |
| 144 | } |
| 145 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 146 | if (!secure && result >= 0) { |
| 147 | reply.read(dstPtr, result); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 150 | return result; |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jeff Tinker | 2514d08 | 2014-11-03 13:29:35 -0800 | [diff] [blame] | 153 | 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 Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 162 | private: |
| 163 | DISALLOW_EVIL_CONSTRUCTORS(BpCrypto); |
| 164 | }; |
| 165 | |
| 166 | IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto"); |
| 167 | |
| 168 | //////////////////////////////////////////////////////////////////////////////// |
| 169 | |
| 170 | status_t BnCrypto::onTransact( |
| 171 | uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { |
| 172 | switch (code) { |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 173 | case INIT_CHECK: |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 174 | { |
| 175 | CHECK_INTERFACE(ICrypto, data, reply); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 176 | reply->writeInt32(initCheck()); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 177 | |
| 178 | return OK; |
| 179 | } |
| 180 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 181 | case IS_CRYPTO_SUPPORTED: |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 182 | { |
| 183 | CHECK_INTERFACE(ICrypto, data, reply); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 184 | uint8_t uuid[16]; |
| 185 | data.read(uuid, sizeof(uuid)); |
| 186 | reply->writeInt32(isCryptoSchemeSupported(uuid)); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 187 | |
| 188 | return OK; |
| 189 | } |
| 190 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 191 | case CREATE_PLUGIN: |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 192 | { |
| 193 | CHECK_INTERFACE(ICrypto, data, reply); |
| 194 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 195 | uint8_t uuid[16]; |
| 196 | data.read(uuid, sizeof(uuid)); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 197 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 198 | size_t opaqueSize = data.readInt32(); |
Andreas Huber | 705868c | 2012-04-11 15:41:45 -0700 | [diff] [blame] | 199 | void *opaqueData = NULL; |
| 200 | |
| 201 | if (opaqueSize > 0) { |
| 202 | opaqueData = malloc(opaqueSize); |
| 203 | data.read(opaqueData, opaqueSize); |
| 204 | } |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 205 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 206 | reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize)); |
| 207 | |
Andreas Huber | 705868c | 2012-04-11 15:41:45 -0700 | [diff] [blame] | 208 | if (opaqueData != NULL) { |
| 209 | free(opaqueData); |
| 210 | opaqueData = NULL; |
| 211 | } |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 212 | |
| 213 | return OK; |
| 214 | } |
| 215 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 216 | case DESTROY_PLUGIN: |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 217 | { |
| 218 | CHECK_INTERFACE(ICrypto, data, reply); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 219 | reply->writeInt32(destroyPlugin()); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 220 | |
| 221 | return OK; |
| 222 | } |
| 223 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 224 | case REQUIRES_SECURE_COMPONENT: |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 225 | { |
| 226 | CHECK_INTERFACE(ICrypto, data, reply); |
| 227 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 228 | const char *mime = data.readCString(); |
| 229 | reply->writeInt32(requiresSecureDecoderComponent(mime)); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 230 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 231 | 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 Tinker | bcca9e0 | 2014-06-09 15:51:38 -0700 | [diff] [blame] | 262 | dstPtr = reinterpret_cast<void *>(static_cast<uintptr_t>(data.readInt64())); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 263 | } else { |
| 264 | dstPtr = malloc(totalSize); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 267 | AString errorDetailMsg; |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 268 | ssize_t result = decrypt( |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 269 | secure, |
| 270 | key, |
| 271 | iv, |
| 272 | mode, |
| 273 | srcData, |
| 274 | subSamples, numSubSamples, |
Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 275 | dstPtr, |
| 276 | &errorDetailMsg); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 277 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 278 | reply->writeInt32(result); |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 279 | |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 280 | if (result >= ERROR_DRM_VENDOR_MIN |
| 281 | && result <= ERROR_DRM_VENDOR_MAX) { |
Andreas Huber | 5b8987e | 2012-04-19 12:52:20 -0700 | [diff] [blame] | 282 | reply->writeCString(errorDetailMsg.c_str()); |
| 283 | } |
| 284 | |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 285 | if (!secure) { |
Edwin Wong | fa2b8f2 | 2012-07-10 20:01:13 -0700 | [diff] [blame] | 286 | if (result >= 0) { |
| 287 | CHECK_LE(result, static_cast<ssize_t>(totalSize)); |
| 288 | reply->write(dstPtr, result); |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 289 | } |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 290 | free(dstPtr); |
| 291 | dstPtr = NULL; |
| 292 | } |
| 293 | |
| 294 | delete[] subSamples; |
| 295 | subSamples = NULL; |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 296 | |
| 297 | free(srcData); |
| 298 | srcData = NULL; |
| 299 | |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 300 | return OK; |
| 301 | } |
| 302 | |
Jeff Tinker | 2514d08 | 2014-11-03 13:29:35 -0800 | [diff] [blame] | 303 | 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 Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 314 | default: |
| 315 | return BBinder::onTransact(code, data, reply, flags); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | } // namespace android |