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