1 /*
2  * Copyright 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 "MediaCrypto-JNI"
19 #include <utils/Log.h>
20 
21 #include "android_media_MediaCrypto.h"
22 
23 #include "android_runtime/AndroidRuntime.h"
24 #include "jni.h"
25 #include <nativehelper/JNIHelp.h>
26 
27 #include <cutils/properties.h>
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <mediadrm/DrmUtils.h>
30 #include <mediadrm/ICrypto.h>
31 
32 namespace android {
33 
34 struct fields_t {
35     jfieldID context;
36 };
37 
38 static fields_t gFields;
39 
getCrypto(JNIEnv * env,jobject thiz)40 static sp<JCrypto> getCrypto(JNIEnv *env, jobject thiz) {
41     return (JCrypto *)env->GetLongField(thiz, gFields.context);
42 }
43 
JCrypto(JNIEnv * env,jobject thiz,const uint8_t uuid[16],const void * initData,size_t initSize)44 JCrypto::JCrypto(
45         JNIEnv *env, jobject thiz,
46         const uint8_t uuid[16], const void *initData, size_t initSize) {
47     mObject = env->NewWeakGlobalRef(thiz);
48 
49     mCrypto = MakeCrypto(uuid, initData, initSize);
50 }
51 
~JCrypto()52 JCrypto::~JCrypto() {
53     if (mCrypto != NULL) {
54         mCrypto->destroyPlugin();
55     }
56     mCrypto.clear();
57 
58     JNIEnv *env = AndroidRuntime::getJNIEnv();
59 
60     env->DeleteWeakGlobalRef(mObject);
61     mObject = NULL;
62 }
63 
64 // static
MakeCrypto()65 sp<ICrypto> JCrypto::MakeCrypto() {
66     return DrmUtils::MakeCrypto();
67 }
68 
69 // static
MakeCrypto(const uint8_t uuid[16],const void * initData,size_t initSize)70 sp<ICrypto> JCrypto::MakeCrypto(
71         const uint8_t uuid[16], const void *initData, size_t initSize) {
72     sp<ICrypto> crypto = MakeCrypto();
73 
74     if (crypto == NULL) {
75         return NULL;
76     }
77 
78     status_t err = crypto->createPlugin(uuid, initData, initSize);
79 
80     if (err != OK) {
81         return NULL;
82     }
83 
84     return crypto;
85 }
86 
requiresSecureDecoderComponent(const char * mime) const87 bool JCrypto::requiresSecureDecoderComponent(const char *mime) const {
88     if (mCrypto == NULL) {
89         return false;
90     }
91 
92     return mCrypto->requiresSecureDecoderComponent(mime);
93 }
94 
95 // static
IsCryptoSchemeSupported(const uint8_t uuid[16])96 bool JCrypto::IsCryptoSchemeSupported(const uint8_t uuid[16]) {
97     sp<ICrypto> crypto = MakeCrypto();
98 
99     if (crypto == NULL) {
100         return false;
101     }
102 
103     return crypto->isCryptoSchemeSupported(uuid);
104 }
105 
initCheck() const106 status_t JCrypto::initCheck() const {
107     return mCrypto == NULL ? NO_INIT : OK;
108 }
109 
110 // static
GetCrypto(JNIEnv * env,jobject obj)111 sp<ICrypto> JCrypto::GetCrypto(JNIEnv *env, jobject obj) {
112     jclass clazz = env->FindClass("android/media/MediaCrypto");
113     CHECK(clazz != NULL);
114 
115     if (!env->IsInstanceOf(obj, clazz)) {
116         return NULL;
117     }
118 
119     sp<JCrypto> jcrypto = getCrypto(env, obj);
120 
121     if (jcrypto == NULL) {
122         return NULL;
123     }
124 
125     return jcrypto->mCrypto;
126 }
127 
128 // JNI conversion utilities
JByteArrayToVector(JNIEnv * env,jbyteArray const & byteArray)129 static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) {
130     Vector<uint8_t> vector;
131     size_t length = env->GetArrayLength(byteArray);
132     vector.insertAt((size_t)0, length);
133     env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray());
134     return vector;
135 }
136 
137 }  // namespace android
138 
139 using namespace android;
140 
setCrypto(JNIEnv * env,jobject thiz,const sp<JCrypto> & crypto)141 static sp<JCrypto> setCrypto(
142         JNIEnv *env, jobject thiz, const sp<JCrypto> &crypto) {
143     sp<JCrypto> old = (JCrypto *)env->GetLongField(thiz, gFields.context);
144     if (crypto != NULL) {
145         crypto->incStrong(thiz);
146     }
147     if (old != NULL) {
148         old->decStrong(thiz);
149     }
150     env->SetLongField(thiz, gFields.context, (jlong)crypto.get());
151 
152     return old;
153 }
154 
android_media_MediaCrypto_release(JNIEnv * env,jobject thiz)155 static void android_media_MediaCrypto_release(JNIEnv *env, jobject thiz) {
156     setCrypto(env, thiz, NULL);
157 }
158 
android_media_MediaCrypto_native_init(JNIEnv * env)159 static void android_media_MediaCrypto_native_init(JNIEnv *env) {
160     jclass clazz = env->FindClass("android/media/MediaCrypto");
161     CHECK(clazz != NULL);
162 
163     gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
164     CHECK(gFields.context != NULL);
165 }
166 
android_media_MediaCrypto_native_setup(JNIEnv * env,jobject thiz,jbyteArray uuidObj,jbyteArray initDataObj)167 static void android_media_MediaCrypto_native_setup(
168         JNIEnv *env, jobject thiz,
169         jbyteArray uuidObj, jbyteArray initDataObj) {
170     jsize uuidLength = env->GetArrayLength(uuidObj);
171 
172     if (uuidLength != 16) {
173         jniThrowException(
174                 env,
175                 "java/lang/IllegalArgumentException",
176                 NULL);
177         return;
178     }
179 
180     jboolean isCopy;
181     jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
182 
183     jsize initDataLength = 0;
184     jbyte *initData = NULL;
185 
186     if (initDataObj != NULL) {
187         initDataLength = env->GetArrayLength(initDataObj);
188         initData = env->GetByteArrayElements(initDataObj, &isCopy);
189     }
190 
191     sp<JCrypto> crypto = new JCrypto(
192             env, thiz, (const uint8_t *)uuid, initData, initDataLength);
193 
194     status_t err = crypto->initCheck();
195 
196     if (initDataObj != NULL) {
197         env->ReleaseByteArrayElements(initDataObj, initData, 0);
198         initData = NULL;
199     }
200 
201     env->ReleaseByteArrayElements(uuidObj, uuid, 0);
202     uuid = NULL;
203 
204     if (err != OK) {
205         jniThrowException(
206                 env,
207                 "android/media/MediaCryptoException",
208                 "Failed to instantiate crypto object.");
209         return;
210     }
211 
212     setCrypto(env,thiz, crypto);
213 }
214 
android_media_MediaCrypto_native_finalize(JNIEnv * env,jobject thiz)215 static void android_media_MediaCrypto_native_finalize(
216         JNIEnv *env, jobject thiz) {
217     android_media_MediaCrypto_release(env, thiz);
218 }
219 
android_media_MediaCrypto_isCryptoSchemeSupportedNative(JNIEnv * env,jobject,jbyteArray uuidObj)220 static jboolean android_media_MediaCrypto_isCryptoSchemeSupportedNative(
221         JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj) {
222     jsize uuidLength = env->GetArrayLength(uuidObj);
223 
224     if (uuidLength != 16) {
225         jniThrowException(
226                 env,
227                 "java/lang/IllegalArgumentException",
228                 NULL);
229         return JNI_FALSE;
230     }
231 
232     jboolean isCopy;
233     jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
234 
235     bool result = JCrypto::IsCryptoSchemeSupported((const uint8_t *)uuid);
236 
237     env->ReleaseByteArrayElements(uuidObj, uuid, 0);
238     uuid = NULL;
239 
240     return result ? JNI_TRUE : JNI_FALSE;
241 }
242 
android_media_MediaCrypto_requiresSecureDecoderComponent(JNIEnv * env,jobject thiz,jstring mimeObj)243 static jboolean android_media_MediaCrypto_requiresSecureDecoderComponent(
244         JNIEnv *env, jobject thiz, jstring mimeObj) {
245     if (mimeObj == NULL) {
246         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
247         return JNI_FALSE;
248     }
249 
250     sp<JCrypto> crypto = getCrypto(env, thiz);
251 
252     if (crypto == NULL) {
253         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
254         return JNI_FALSE;
255     }
256 
257     const char *mime = env->GetStringUTFChars(mimeObj, NULL);
258 
259     if (mime == NULL) {
260         return JNI_FALSE;
261     }
262 
263     bool result = crypto->requiresSecureDecoderComponent(mime);
264 
265     env->ReleaseStringUTFChars(mimeObj, mime);
266     mime = NULL;
267 
268     return result ? JNI_TRUE : JNI_FALSE;
269 }
270 
android_media_MediaCrypto_setMediaDrmSession(JNIEnv * env,jobject thiz,jbyteArray jsessionId)271 static void android_media_MediaCrypto_setMediaDrmSession(
272         JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
273     if (jsessionId == NULL) {
274         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
275         return;
276     }
277 
278     sp<ICrypto> crypto = JCrypto::GetCrypto(env, thiz);
279 
280     if (crypto == NULL) {
281         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
282         return;
283     }
284 
285     Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
286 
287     status_t err = crypto->setMediaDrmSession(sessionId);
288 
289     if (err != OK) {
290         String8 msg("setMediaDrmSession failed");
291         if (err == ERROR_DRM_SESSION_NOT_OPENED) {
292             msg += ": session not opened";
293         } else if (err == ERROR_UNSUPPORTED) {
294             msg += ": not supported by this crypto scheme";
295         } else if (err == NO_INIT) {
296             msg += ": crypto plugin not initialized";
297         } else {
298             msg.appendFormat(": general failure (%d)", err);
299         }
300         jniThrowException(env, "android/media/MediaCryptoException", msg.string());
301     }
302 }
303 
304 static const JNINativeMethod gMethods[] = {
305     { "release", "()V", (void *)android_media_MediaCrypto_release },
306     { "native_init", "()V", (void *)android_media_MediaCrypto_native_init },
307 
308     { "native_setup", "([B[B)V",
309       (void *)android_media_MediaCrypto_native_setup },
310 
311     { "native_finalize", "()V",
312       (void *)android_media_MediaCrypto_native_finalize },
313 
314     { "isCryptoSchemeSupportedNative", "([B)Z",
315       (void *)android_media_MediaCrypto_isCryptoSchemeSupportedNative },
316 
317     { "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z",
318       (void *)android_media_MediaCrypto_requiresSecureDecoderComponent },
319 
320     { "setMediaDrmSession", "([B)V",
321       (void *)android_media_MediaCrypto_setMediaDrmSession },
322 };
323 
register_android_media_Crypto(JNIEnv * env)324 int register_android_media_Crypto(JNIEnv *env) {
325     return AndroidRuntime::registerNativeMethods(env,
326                 "android/media/MediaCrypto", gMethods, NELEM(gMethods));
327 }
328 
329