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 "IHDCP" 19 #include <utils/Log.h> 20 21 #include <binder/Parcel.h> 22 #include <media/IHDCP.h> 23 #include <media/stagefright/MediaErrors.h> 24 #include <media/stagefright/foundation/ADebug.h> 25 26 namespace android { 27 28 enum { 29 OBSERVER_NOTIFY = IBinder::FIRST_CALL_TRANSACTION, 30 HDCP_SET_OBSERVER, 31 HDCP_INIT_ASYNC, 32 HDCP_SHUTDOWN_ASYNC, 33 HDCP_GET_CAPS, 34 HDCP_ENCRYPT, 35 HDCP_ENCRYPT_NATIVE, 36 HDCP_DECRYPT, 37 }; 38 39 struct BpHDCPObserver : public BpInterface<IHDCPObserver> { 40 explicit BpHDCPObserver(const sp<IBinder> &impl) 41 : BpInterface<IHDCPObserver>(impl) { 42 } 43 44 virtual void notify( 45 int msg, int ext1, int ext2, const Parcel *obj) { 46 Parcel data, reply; 47 data.writeInterfaceToken(IHDCPObserver::getInterfaceDescriptor()); 48 data.writeInt32(msg); 49 data.writeInt32(ext1); 50 data.writeInt32(ext2); 51 if (obj && obj->dataSize() > 0) { 52 data.appendFrom(const_cast<Parcel *>(obj), 0, obj->dataSize()); 53 } 54 remote()->transact(OBSERVER_NOTIFY, data, &reply, IBinder::FLAG_ONEWAY); 55 } 56 }; 57 58 IMPLEMENT_META_INTERFACE(HDCPObserver, "android.hardware.IHDCPObserver"); 59 60 struct BpHDCP : public BpInterface<IHDCP> { 61 explicit BpHDCP(const sp<IBinder> &impl) 62 : BpInterface<IHDCP>(impl) { 63 } 64 65 virtual status_t setObserver(const sp<IHDCPObserver> &observer) { 66 Parcel data, reply; 67 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 68 data.writeStrongBinder(IInterface::asBinder(observer)); 69 remote()->transact(HDCP_SET_OBSERVER, data, &reply); 70 return reply.readInt32(); 71 } 72 73 virtual status_t initAsync(const char *host, unsigned port) { 74 Parcel data, reply; 75 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 76 data.writeCString(host); 77 data.writeInt32(port); 78 remote()->transact(HDCP_INIT_ASYNC, data, &reply); 79 return reply.readInt32(); 80 } 81 82 virtual status_t shutdownAsync() { 83 Parcel data, reply; 84 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 85 remote()->transact(HDCP_SHUTDOWN_ASYNC, data, &reply); 86 return reply.readInt32(); 87 } 88 89 virtual uint32_t getCaps() { 90 Parcel data, reply; 91 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 92 remote()->transact(HDCP_GET_CAPS, data, &reply); 93 return reply.readInt32(); 94 } 95 96 virtual status_t encrypt( 97 const void *inData, size_t size, uint32_t streamCTR, 98 uint64_t *outInputCTR, void *outData) { 99 Parcel data, reply; 100 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 101 data.writeInt32(size); 102 data.write(inData, size); 103 data.writeInt32(streamCTR); 104 remote()->transact(HDCP_ENCRYPT, data, &reply); 105 106 status_t err = reply.readInt32(); 107 108 if (err != OK) { 109 *outInputCTR = 0; 110 111 return err; 112 } 113 114 *outInputCTR = reply.readInt64(); 115 reply.read(outData, size); 116 117 return err; 118 } 119 120 virtual status_t encryptNative( 121 const sp<GraphicBuffer> &graphicBuffer, 122 size_t offset, size_t size, uint32_t streamCTR, 123 uint64_t *outInputCTR, void *outData) { 124 Parcel data, reply; 125 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 126 data.write(*graphicBuffer); 127 data.writeInt32(offset); 128 data.writeInt32(size); 129 data.writeInt32(streamCTR); 130 remote()->transact(HDCP_ENCRYPT_NATIVE, data, &reply); 131 132 status_t err = reply.readInt32(); 133 134 if (err != OK) { 135 *outInputCTR = 0; 136 return err; 137 } 138 139 *outInputCTR = reply.readInt64(); 140 reply.read(outData, size); 141 142 return err; 143 } 144 145 virtual status_t decrypt( 146 const void *inData, size_t size, 147 uint32_t streamCTR, uint64_t inputCTR, 148 void *outData) { 149 Parcel data, reply; 150 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); 151 data.writeInt32(size); 152 data.write(inData, size); 153 data.writeInt32(streamCTR); 154 data.writeInt64(inputCTR); 155 remote()->transact(HDCP_DECRYPT, data, &reply); 156 157 status_t err = reply.readInt32(); 158 159 if (err != OK) { 160 return err; 161 } 162 163 reply.read(outData, size); 164 165 return err; 166 } 167 }; 168 169 IMPLEMENT_META_INTERFACE(HDCP, "android.hardware.IHDCP"); 170 171 status_t BnHDCPObserver::onTransact( 172 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { 173 switch (code) { 174 case OBSERVER_NOTIFY: 175 { 176 CHECK_INTERFACE(IHDCPObserver, data, reply); 177 178 int msg = data.readInt32(); 179 int ext1 = data.readInt32(); 180 int ext2 = data.readInt32(); 181 182 Parcel obj; 183 if (data.dataAvail() > 0) { 184 obj.appendFrom( 185 const_cast<Parcel *>(&data), 186 data.dataPosition(), 187 data.dataAvail()); 188 } 189 190 notify(msg, ext1, ext2, &obj); 191 192 return OK; 193 } 194 195 default: 196 return BBinder::onTransact(code, data, reply, flags); 197 } 198 } 199 200 status_t BnHDCP::onTransact( 201 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { 202 switch (code) { 203 case HDCP_SET_OBSERVER: 204 { 205 CHECK_INTERFACE(IHDCP, data, reply); 206 207 sp<IHDCPObserver> observer = 208 interface_cast<IHDCPObserver>(data.readStrongBinder()); 209 210 reply->writeInt32(setObserver(observer)); 211 return OK; 212 } 213 214 case HDCP_INIT_ASYNC: 215 { 216 CHECK_INTERFACE(IHDCP, data, reply); 217 218 const char *host = data.readCString(); 219 unsigned port = data.readInt32(); 220 221 reply->writeInt32(initAsync(host, port)); 222 return OK; 223 } 224 225 case HDCP_SHUTDOWN_ASYNC: 226 { 227 CHECK_INTERFACE(IHDCP, data, reply); 228 229 reply->writeInt32(shutdownAsync()); 230 return OK; 231 } 232 233 case HDCP_GET_CAPS: 234 { 235 CHECK_INTERFACE(IHDCP, data, reply); 236 237 reply->writeInt32(getCaps()); 238 return OK; 239 } 240 241 case HDCP_ENCRYPT: 242 { 243 CHECK_INTERFACE(IHDCP, data, reply); 244 245 size_t size = data.readInt32(); 246 void *inData = NULL; 247 // watch out for overflow 248 if (size <= SIZE_MAX / 2) { 249 inData = malloc(2 * size); 250 } 251 if (inData == NULL) { 252 reply->writeInt32(ERROR_OUT_OF_RANGE); 253 return OK; 254 } 255 256 void *outData = (uint8_t *)inData + size; 257 258 status_t err = data.read(inData, size); 259 if (err != OK) { 260 free(inData); 261 reply->writeInt32(err); 262 return OK; 263 } 264 265 uint32_t streamCTR = data.readInt32(); 266 uint64_t inputCTR; 267 err = encrypt(inData, size, streamCTR, &inputCTR, outData); 268 269 reply->writeInt32(err); 270 271 if (err == OK) { 272 reply->writeInt64(inputCTR); 273 reply->write(outData, size); 274 } 275 276 free(inData); 277 inData = outData = NULL; 278 279 return OK; 280 } 281 282 case HDCP_ENCRYPT_NATIVE: 283 { 284 CHECK_INTERFACE(IHDCP, data, reply); 285 286 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(); 287 data.read(*graphicBuffer); 288 size_t offset = data.readInt32(); 289 size_t size = data.readInt32(); 290 uint32_t streamCTR = data.readInt32(); 291 void *outData = NULL; 292 uint64_t inputCTR; 293 294 status_t err = ERROR_OUT_OF_RANGE; 295 296 outData = malloc(size); 297 298 if (outData != NULL) { 299 err = encryptNative(graphicBuffer, offset, size, 300 streamCTR, &inputCTR, outData); 301 } 302 303 reply->writeInt32(err); 304 305 if (err == OK) { 306 reply->writeInt64(inputCTR); 307 reply->write(outData, size); 308 } 309 310 free(outData); 311 outData = NULL; 312 313 return OK; 314 } 315 316 case HDCP_DECRYPT: 317 { 318 CHECK_INTERFACE(IHDCP, data, reply); 319 320 size_t size = data.readInt32(); 321 size_t bufSize = 2 * size; 322 323 // watch out for overflow 324 void *inData = NULL; 325 if (bufSize > size) { 326 inData = malloc(bufSize); 327 } 328 329 if (inData == NULL) { 330 reply->writeInt32(ERROR_OUT_OF_RANGE); 331 return OK; 332 } 333 334 void *outData = (uint8_t *)inData + size; 335 336 data.read(inData, size); 337 338 uint32_t streamCTR = data.readInt32(); 339 uint64_t inputCTR = data.readInt64(); 340 status_t err = decrypt(inData, size, streamCTR, inputCTR, outData); 341 342 reply->writeInt32(err); 343 344 if (err == OK) { 345 reply->write(outData, size); 346 } 347 348 free(inData); 349 inData = outData = NULL; 350 351 return OK; 352 } 353 354 default: 355 return BBinder::onTransact(code, data, reply, flags); 356 } 357 } 358 359 } // namespace android 360