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> {
BpHDCPObserverandroid::BpHDCPObserver40 BpHDCPObserver(const sp<IBinder> &impl)
41 : BpInterface<IHDCPObserver>(impl) {
42 }
43
notifyandroid::BpHDCPObserver44 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> {
BpHDCPandroid::BpHDCP61 BpHDCP(const sp<IBinder> &impl)
62 : BpInterface<IHDCP>(impl) {
63 }
64
setObserverandroid::BpHDCP65 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
initAsyncandroid::BpHDCP73 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
shutdownAsyncandroid::BpHDCP82 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
getCapsandroid::BpHDCP89 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
encryptandroid::BpHDCP96 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
encryptNativeandroid::BpHDCP120 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
decryptandroid::BpHDCP145 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
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)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
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)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 size_t size = data.readInt32();
244 void *inData = NULL;
245 // watch out for overflow
246 if (size <= SIZE_MAX / 2) {
247 inData = malloc(2 * size);
248 }
249 if (inData == NULL) {
250 reply->writeInt32(ERROR_OUT_OF_RANGE);
251 return OK;
252 }
253
254 void *outData = (uint8_t *)inData + size;
255
256 status_t err = data.read(inData, size);
257 if (err != OK) {
258 free(inData);
259 reply->writeInt32(err);
260 return OK;
261 }
262
263 uint32_t streamCTR = data.readInt32();
264 uint64_t inputCTR;
265 err = encrypt(inData, size, streamCTR, &inputCTR, outData);
266
267 reply->writeInt32(err);
268
269 if (err == OK) {
270 reply->writeInt64(inputCTR);
271 reply->write(outData, size);
272 }
273
274 free(inData);
275 inData = outData = NULL;
276
277 return OK;
278 }
279
280 case HDCP_ENCRYPT_NATIVE:
281 {
282 CHECK_INTERFACE(IHDCP, data, reply);
283
284 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer();
285 data.read(*graphicBuffer);
286 size_t offset = data.readInt32();
287 size_t size = data.readInt32();
288 uint32_t streamCTR = data.readInt32();
289 void *outData = NULL;
290 uint64_t inputCTR;
291
292 status_t err = ERROR_OUT_OF_RANGE;
293
294 outData = malloc(size);
295
296 if (outData != NULL) {
297 err = encryptNative(graphicBuffer, offset, size,
298 streamCTR, &inputCTR, outData);
299 }
300
301 reply->writeInt32(err);
302
303 if (err == OK) {
304 reply->writeInt64(inputCTR);
305 reply->write(outData, size);
306 }
307
308 free(outData);
309 outData = NULL;
310
311 return OK;
312 }
313
314 case HDCP_DECRYPT:
315 {
316 size_t size = data.readInt32();
317 size_t bufSize = 2 * size;
318
319 // watch out for overflow
320 void *inData = NULL;
321 if (bufSize > size) {
322 inData = malloc(bufSize);
323 }
324
325 if (inData == NULL) {
326 reply->writeInt32(ERROR_OUT_OF_RANGE);
327 return OK;
328 }
329
330 void *outData = (uint8_t *)inData + size;
331
332 data.read(inData, size);
333
334 uint32_t streamCTR = data.readInt32();
335 uint64_t inputCTR = data.readInt64();
336 status_t err = decrypt(inData, size, streamCTR, inputCTR, outData);
337
338 reply->writeInt32(err);
339
340 if (err == OK) {
341 reply->write(outData, size);
342 }
343
344 free(inData);
345 inData = outData = NULL;
346
347 return OK;
348 }
349
350 default:
351 return BBinder::onTransact(code, data, reply, flags);
352 }
353 }
354
355 } // namespace android
356