android_media_MediaDrm.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*
  2. * Copyright 2013, 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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "MediaDrm-JNI"
  18. #include <utils/Log.h>
  19. #include "android_media_MediaDrm.h"
  20. #include "android_media_MediaMetricsJNI.h"
  21. #include "android_os_Parcel.h"
  22. #include "android_runtime/AndroidRuntime.h"
  23. #include "android_runtime/Log.h"
  24. #include "android_os_Parcel.h"
  25. #include "jni.h"
  26. #include <nativehelper/JNIHelp.h>
  27. #include <binder/IServiceManager.h>
  28. #include <binder/Parcel.h>
  29. #include <binder/PersistableBundle.h>
  30. #include <cutils/properties.h>
  31. #include <media/stagefright/foundation/ADebug.h>
  32. #include <media/stagefright/MediaErrors.h>
  33. #include <mediadrm/IDrm.h>
  34. #include <mediadrm/IMediaDrmService.h>
  35. using ::android::os::PersistableBundle;
  36. namespace android {
  37. #define FIND_CLASS(var, className) \
  38. var = env->FindClass(className); \
  39. LOG_FATAL_IF(! (var), "Unable to find class %s", className);
  40. #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
  41. var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
  42. LOG_FATAL_IF(! (var), "Unable to find field %s", fieldName);
  43. #define GET_METHOD_ID(var, clazz, fieldName, fieldDescriptor) \
  44. var = env->GetMethodID(clazz, fieldName, fieldDescriptor); \
  45. LOG_FATAL_IF(! (var), "Unable to find method %s", fieldName);
  46. #define GET_STATIC_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
  47. var = env->GetStaticFieldID(clazz, fieldName, fieldDescriptor); \
  48. LOG_FATAL_IF(! (var), "Unable to find field %s", fieldName);
  49. #define GET_STATIC_METHOD_ID(var, clazz, fieldName, fieldDescriptor) \
  50. var = env->GetStaticMethodID(clazz, fieldName, fieldDescriptor); \
  51. LOG_FATAL_IF(! (var), "Unable to find static method %s", fieldName);
  52. #define GET_STATIC_OBJECT_FIELD(var, clazz, fieldId) \
  53. var = env->GetStaticObjectField(clazz, fieldId); \
  54. LOG_FATAL_IF(! (var), "Unable to find static object field %p", fieldId);
  55. struct RequestFields {
  56. jfieldID data;
  57. jfieldID defaultUrl;
  58. jfieldID requestType;
  59. };
  60. struct ArrayListFields {
  61. jmethodID init;
  62. jmethodID add;
  63. };
  64. struct HashmapFields {
  65. jmethodID init;
  66. jmethodID get;
  67. jmethodID put;
  68. jmethodID entrySet;
  69. };
  70. struct SetFields {
  71. jmethodID iterator;
  72. };
  73. struct IteratorFields {
  74. jmethodID next;
  75. jmethodID hasNext;
  76. };
  77. struct EntryFields {
  78. jmethodID getKey;
  79. jmethodID getValue;
  80. };
  81. struct EventTypes {
  82. jint kEventProvisionRequired;
  83. jint kEventKeyRequired;
  84. jint kEventKeyExpired;
  85. jint kEventVendorDefined;
  86. jint kEventSessionReclaimed;
  87. } gEventTypes;
  88. struct EventWhat {
  89. jint kWhatDrmEvent;
  90. jint kWhatExpirationUpdate;
  91. jint kWhatKeyStatusChange;
  92. jint kWhatSessionLostState;
  93. } gEventWhat;
  94. struct KeyTypes {
  95. jint kKeyTypeStreaming;
  96. jint kKeyTypeOffline;
  97. jint kKeyTypeRelease;
  98. } gKeyTypes;
  99. struct KeyRequestTypes {
  100. jint kKeyRequestTypeInitial;
  101. jint kKeyRequestTypeRenewal;
  102. jint kKeyRequestTypeRelease;
  103. jint kKeyRequestTypeNone;
  104. jint kKeyRequestTypeUpdate;
  105. } gKeyRequestTypes;
  106. struct CertificateTypes {
  107. jint kCertificateTypeNone;
  108. jint kCertificateTypeX509;
  109. } gCertificateTypes;
  110. struct CertificateFields {
  111. jfieldID wrappedPrivateKey;
  112. jfieldID certificateData;
  113. };
  114. struct StateExceptionFields {
  115. jmethodID init;
  116. jclass classId;
  117. };
  118. struct SessionExceptionFields {
  119. jmethodID init;
  120. jclass classId;
  121. jfieldID errorCode;
  122. };
  123. struct SessionExceptionErrorCodes {
  124. jint kErrorUnknown;
  125. jint kResourceContention;
  126. } gSessionExceptionErrorCodes;
  127. struct HDCPLevels {
  128. jint kHdcpLevelUnknown;
  129. jint kHdcpNone;
  130. jint kHdcpV1;
  131. jint kHdcpV2;
  132. jint kHdcpV2_1;
  133. jint kHdcpV2_2;
  134. jint kHdcpV2_3;
  135. jint kHdcpNoOutput;
  136. } gHdcpLevels;
  137. struct SecurityLevels {
  138. jint kSecurityLevelUnknown;
  139. jint kSecurityLevelMax;
  140. jint kSecurityLevelSwSecureCrypto;
  141. jint kSecurityLevelSwSecureDecode;
  142. jint kSecurityLevelHwSecureCrypto;
  143. jint kSecurityLevelHwSecureDecode;
  144. jint kSecurityLevelHwSecureAll;
  145. } gSecurityLevels;
  146. struct OfflineLicenseState {
  147. jint kOfflineLicenseStateUsable;
  148. jint kOfflineLicenseStateReleased;
  149. jint kOfflineLicenseStateUnknown;
  150. } gOfflineLicenseStates;
  151. struct fields_t {
  152. jfieldID context;
  153. jmethodID post_event;
  154. RequestFields keyRequest;
  155. RequestFields provisionRequest;
  156. ArrayListFields arraylist;
  157. HashmapFields hashmap;
  158. SetFields set;
  159. IteratorFields iterator;
  160. EntryFields entry;
  161. CertificateFields certificate;
  162. StateExceptionFields stateException;
  163. SessionExceptionFields sessionException;
  164. jclass certificateClassId;
  165. jclass hashmapClassId;
  166. jclass arraylistClassId;
  167. jclass stringClassId;
  168. jobject bundleCreator;
  169. jmethodID createFromParcelId;
  170. jclass parcelCreatorClassId;
  171. };
  172. static fields_t gFields;
  173. namespace {
  174. // Helper function to convert a native PersistableBundle to a Java
  175. // PersistableBundle.
  176. jobject nativeToJavaPersistableBundle(JNIEnv *env, jobject thiz,
  177. PersistableBundle* nativeBundle) {
  178. if (env == NULL || thiz == NULL || nativeBundle == NULL) {
  179. ALOGE("Unexpected NULL parmeter");
  180. return NULL;
  181. }
  182. // Create a Java parcel with the native parcel data.
  183. // Then create a new PersistableBundle with that parcel as a parameter.
  184. jobject jParcel = android::createJavaParcelObject(env);
  185. if (jParcel == NULL) {
  186. ALOGE("Failed to create a Java Parcel.");
  187. return NULL;
  188. }
  189. android::Parcel* nativeParcel = android::parcelForJavaObject(env, jParcel);
  190. if (nativeParcel == NULL) {
  191. ALOGE("Failed to get the native Parcel.");
  192. return NULL;
  193. }
  194. android::status_t result = nativeBundle->writeToParcel(nativeParcel);
  195. nativeParcel->setDataPosition(0);
  196. if (result != android::OK) {
  197. ALOGE("Failed to write nativeBundle to Parcel: %d.", result);
  198. return NULL;
  199. }
  200. jobject newBundle = env->CallObjectMethod(gFields.bundleCreator,
  201. gFields.createFromParcelId,
  202. jParcel);
  203. if (newBundle == NULL) {
  204. ALOGE("Failed to create a new PersistableBundle "
  205. "from the createFromParcel call.");
  206. }
  207. return newBundle;
  208. }
  209. } // namespace anonymous
  210. // ----------------------------------------------------------------------------
  211. // ref-counted object for callbacks
  212. class JNIDrmListener: public DrmListener
  213. {
  214. public:
  215. JNIDrmListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
  216. ~JNIDrmListener();
  217. virtual void notify(DrmPlugin::EventType eventType, int extra, const Parcel *obj = NULL);
  218. private:
  219. JNIDrmListener();
  220. jclass mClass; // Reference to MediaDrm class
  221. jobject mObject; // Weak ref to MediaDrm Java object to call on
  222. };
  223. JNIDrmListener::JNIDrmListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
  224. {
  225. // Hold onto the MediaDrm class for use in calling the static method
  226. // that posts events to the application thread.
  227. jclass clazz = env->GetObjectClass(thiz);
  228. if (clazz == NULL) {
  229. ALOGE("Can't find android/media/MediaDrm");
  230. jniThrowException(env, "java/lang/Exception",
  231. "Can't find android/media/MediaDrm");
  232. return;
  233. }
  234. mClass = (jclass)env->NewGlobalRef(clazz);
  235. // We use a weak reference so the MediaDrm object can be garbage collected.
  236. // The reference is only used as a proxy for callbacks.
  237. mObject = env->NewGlobalRef(weak_thiz);
  238. }
  239. JNIDrmListener::~JNIDrmListener()
  240. {
  241. // remove global references
  242. JNIEnv *env = AndroidRuntime::getJNIEnv();
  243. env->DeleteGlobalRef(mObject);
  244. env->DeleteGlobalRef(mClass);
  245. }
  246. void JNIDrmListener::notify(DrmPlugin::EventType eventType, int extra,
  247. const Parcel *obj)
  248. {
  249. jint jwhat;
  250. jint jeventType = 0;
  251. // translate DrmPlugin event types into their java equivalents
  252. switch (eventType) {
  253. case DrmPlugin::kDrmPluginEventProvisionRequired:
  254. jwhat = gEventWhat.kWhatDrmEvent;
  255. jeventType = gEventTypes.kEventProvisionRequired;
  256. break;
  257. case DrmPlugin::kDrmPluginEventKeyNeeded:
  258. jwhat = gEventWhat.kWhatDrmEvent;
  259. jeventType = gEventTypes.kEventKeyRequired;
  260. break;
  261. case DrmPlugin::kDrmPluginEventKeyExpired:
  262. jwhat = gEventWhat.kWhatDrmEvent;
  263. jeventType = gEventTypes.kEventKeyExpired;
  264. break;
  265. case DrmPlugin::kDrmPluginEventVendorDefined:
  266. jwhat = gEventWhat.kWhatDrmEvent;
  267. jeventType = gEventTypes.kEventVendorDefined;
  268. break;
  269. case DrmPlugin::kDrmPluginEventSessionReclaimed:
  270. jwhat = gEventWhat.kWhatDrmEvent;
  271. jeventType = gEventTypes.kEventSessionReclaimed;
  272. break;
  273. case DrmPlugin::kDrmPluginEventExpirationUpdate:
  274. jwhat = gEventWhat.kWhatExpirationUpdate;
  275. break;
  276. case DrmPlugin::kDrmPluginEventKeysChange:
  277. jwhat = gEventWhat.kWhatKeyStatusChange;
  278. break;
  279. case DrmPlugin::kDrmPluginEventSessionLostState:
  280. jwhat = gEventWhat.kWhatSessionLostState;
  281. break;
  282. default:
  283. ALOGE("Invalid event DrmPlugin::EventType %d, ignored", (int)eventType);
  284. return;
  285. }
  286. JNIEnv *env = AndroidRuntime::getJNIEnv();
  287. if (obj && obj->dataSize() > 0) {
  288. jobject jParcel = createJavaParcelObject(env);
  289. if (jParcel != NULL) {
  290. Parcel* nativeParcel = parcelForJavaObject(env, jParcel);
  291. nativeParcel->setData(obj->data(), obj->dataSize());
  292. env->CallStaticVoidMethod(mClass, gFields.post_event, mObject,
  293. jwhat, jeventType, extra, jParcel);
  294. env->DeleteLocalRef(jParcel);
  295. }
  296. }
  297. if (env->ExceptionCheck()) {
  298. ALOGW("An exception occurred while notifying an event.");
  299. LOGW_EX(env);
  300. env->ExceptionClear();
  301. }
  302. }
  303. static void throwStateException(JNIEnv *env, const char *msg, status_t err) {
  304. ALOGE("Illegal state exception: %s (%d)", msg, err);
  305. jobject exception = env->NewObject(gFields.stateException.classId,
  306. gFields.stateException.init, static_cast<int>(err),
  307. env->NewStringUTF(msg));
  308. env->Throw(static_cast<jthrowable>(exception));
  309. }
  310. static void throwSessionException(JNIEnv *env, const char *msg, status_t err) {
  311. ALOGE("Session exception: %s (%d)", msg, err);
  312. jint jErrorCode = 0;
  313. switch(err) {
  314. case ERROR_DRM_RESOURCE_CONTENTION:
  315. jErrorCode = gSessionExceptionErrorCodes.kResourceContention;
  316. break;
  317. default:
  318. break;
  319. }
  320. jobject exception = env->NewObject(gFields.sessionException.classId,
  321. gFields.sessionException.init, static_cast<int>(err),
  322. env->NewStringUTF(msg));
  323. env->SetIntField(exception, gFields.sessionException.errorCode, jErrorCode);
  324. env->Throw(static_cast<jthrowable>(exception));
  325. }
  326. static bool isSessionException(status_t err) {
  327. return err == ERROR_DRM_RESOURCE_CONTENTION;
  328. }
  329. static bool throwExceptionAsNecessary(
  330. JNIEnv *env, status_t err, const char *msg = NULL) {
  331. const char *drmMessage = NULL;
  332. switch (err) {
  333. case ERROR_DRM_UNKNOWN:
  334. drmMessage = "General DRM error";
  335. break;
  336. case ERROR_DRM_NO_LICENSE:
  337. drmMessage = "No license";
  338. break;
  339. case ERROR_DRM_LICENSE_EXPIRED:
  340. drmMessage = "License expired";
  341. break;
  342. case ERROR_DRM_SESSION_NOT_OPENED:
  343. drmMessage = "Session not opened";
  344. break;
  345. case ERROR_DRM_DECRYPT_UNIT_NOT_INITIALIZED:
  346. drmMessage = "Not initialized";
  347. break;
  348. case ERROR_DRM_DECRYPT:
  349. drmMessage = "Decrypt error";
  350. break;
  351. case ERROR_DRM_CANNOT_HANDLE:
  352. drmMessage = "Invalid parameter or data format";
  353. break;
  354. case ERROR_DRM_INVALID_STATE:
  355. drmMessage = "Invalid state";
  356. break;
  357. default:
  358. break;
  359. }
  360. String8 vendorMessage;
  361. if (err >= ERROR_DRM_VENDOR_MIN && err <= ERROR_DRM_VENDOR_MAX) {
  362. vendorMessage = String8::format("DRM vendor-defined error: %d", err);
  363. drmMessage = vendorMessage.string();
  364. }
  365. if (err == BAD_VALUE || err == ERROR_DRM_CANNOT_HANDLE) {
  366. jniThrowException(env, "java/lang/IllegalArgumentException", msg);
  367. return true;
  368. } else if (err == ERROR_UNSUPPORTED) {
  369. jniThrowException(env, "java/lang/UnsupportedOperationException", msg);
  370. return true;
  371. } else if (err == ERROR_DRM_NOT_PROVISIONED) {
  372. jniThrowException(env, "android/media/NotProvisionedException", msg);
  373. return true;
  374. } else if (err == ERROR_DRM_RESOURCE_BUSY) {
  375. jniThrowException(env, "android/media/ResourceBusyException", msg);
  376. return true;
  377. } else if (err == ERROR_DRM_DEVICE_REVOKED) {
  378. jniThrowException(env, "android/media/DeniedByServerException", msg);
  379. return true;
  380. } else if (err == DEAD_OBJECT) {
  381. jniThrowException(env, "android/media/MediaDrmResetException",
  382. "mediaserver died");
  383. return true;
  384. } else if (isSessionException(err)) {
  385. throwSessionException(env, msg, err);
  386. return true;
  387. } else if (err != OK) {
  388. String8 errbuf;
  389. if (drmMessage != NULL) {
  390. if (msg == NULL) {
  391. msg = drmMessage;
  392. } else {
  393. errbuf = String8::format("%s: %s", msg, drmMessage);
  394. msg = errbuf.string();
  395. }
  396. }
  397. throwStateException(env, msg, err);
  398. return true;
  399. }
  400. return false;
  401. }
  402. static sp<IDrm> GetDrm(JNIEnv *env, jobject thiz) {
  403. JDrm *jdrm = (JDrm *)env->GetLongField(thiz, gFields.context);
  404. return jdrm ? jdrm->getDrm() : NULL;
  405. }
  406. JDrm::JDrm(
  407. JNIEnv *env, jobject thiz, const uint8_t uuid[16],
  408. const String8 &appPackageName) {
  409. mObject = env->NewWeakGlobalRef(thiz);
  410. mDrm = MakeDrm(uuid, appPackageName);
  411. if (mDrm != NULL) {
  412. mDrm->setListener(this);
  413. }
  414. }
  415. JDrm::~JDrm() {
  416. JNIEnv *env = AndroidRuntime::getJNIEnv();
  417. env->DeleteWeakGlobalRef(mObject);
  418. mObject = NULL;
  419. }
  420. // static
  421. sp<IDrm> JDrm::MakeDrm() {
  422. sp<IServiceManager> sm = initdefaultServiceManager();
  423. sp<IBinder> binder = sm->getService(String16("media.drm"));
  424. sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
  425. if (service == NULL) {
  426. return NULL;
  427. }
  428. sp<IDrm> drm = service->makeDrm();
  429. if (drm == NULL || (drm->initCheck() != OK && drm->initCheck() != NO_INIT)) {
  430. return NULL;
  431. }
  432. return drm;
  433. }
  434. // static
  435. sp<IDrm> JDrm::MakeDrm(const uint8_t uuid[16], const String8 &appPackageName) {
  436. sp<IDrm> drm = MakeDrm();
  437. if (drm == NULL) {
  438. return NULL;
  439. }
  440. status_t err = drm->createPlugin(uuid, appPackageName);
  441. if (err != OK) {
  442. return NULL;
  443. }
  444. return drm;
  445. }
  446. status_t JDrm::setListener(const sp<DrmListener>& listener) {
  447. Mutex::Autolock lock(mLock);
  448. mListener = listener;
  449. return OK;
  450. }
  451. void JDrm::notify(DrmPlugin::EventType eventType, int extra, const Parcel *obj) {
  452. sp<DrmListener> listener;
  453. mLock.lock();
  454. listener = mListener;
  455. mLock.unlock();
  456. if (listener != NULL) {
  457. Mutex::Autolock lock(mNotifyLock);
  458. listener->notify(eventType, extra, obj);
  459. }
  460. }
  461. void JDrm::disconnect() {
  462. if (mDrm != NULL) {
  463. mDrm->destroyPlugin();
  464. mDrm.clear();
  465. }
  466. }
  467. // static
  468. status_t JDrm::IsCryptoSchemeSupported(const uint8_t uuid[16], const String8 &mimeType,
  469. DrmPlugin::SecurityLevel securityLevel, bool *isSupported) {
  470. sp<IDrm> drm = MakeDrm();
  471. if (drm == NULL) {
  472. return BAD_VALUE;
  473. }
  474. return drm->isCryptoSchemeSupported(uuid, mimeType, securityLevel, isSupported);
  475. }
  476. status_t JDrm::initCheck() const {
  477. return mDrm == NULL ? NO_INIT : OK;
  478. }
  479. // JNI conversion utilities
  480. static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) {
  481. Vector<uint8_t> vector;
  482. size_t length = env->GetArrayLength(byteArray);
  483. vector.insertAt((size_t)0, length);
  484. env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray());
  485. return vector;
  486. }
  487. static jbyteArray VectorToJByteArray(JNIEnv *env, Vector<uint8_t> const &vector) {
  488. size_t length = vector.size();
  489. jbyteArray result = env->NewByteArray(length);
  490. if (result != NULL) {
  491. env->SetByteArrayRegion(result, 0, length, (jbyte *)vector.array());
  492. }
  493. return result;
  494. }
  495. static String8 JStringToString8(JNIEnv *env, jstring const &jstr) {
  496. String8 result;
  497. const char *s = env->GetStringUTFChars(jstr, NULL);
  498. if (s) {
  499. result = s;
  500. env->ReleaseStringUTFChars(jstr, s);
  501. }
  502. return result;
  503. }
  504. /*
  505. import java.util.HashMap;
  506. import java.util.Set;
  507. import java.Map.Entry;
  508. import jav.util.Iterator;
  509. HashMap<k, v> hm;
  510. Set<Entry<k, v>> s = hm.entrySet();
  511. Iterator i = s.iterator();
  512. Entry e = s.next();
  513. */
  514. static KeyedVector<String8, String8> HashMapToKeyedVector(
  515. JNIEnv *env, jobject &hashMap, bool* pIsOK) {
  516. jclass clazz = gFields.stringClassId;
  517. KeyedVector<String8, String8> keyedVector;
  518. *pIsOK = true;
  519. jobject entrySet = env->CallObjectMethod(hashMap, gFields.hashmap.entrySet);
  520. if (entrySet) {
  521. jobject iterator = env->CallObjectMethod(entrySet, gFields.set.iterator);
  522. if (iterator) {
  523. jboolean hasNext = env->CallBooleanMethod(iterator, gFields.iterator.hasNext);
  524. while (hasNext) {
  525. jobject entry = env->CallObjectMethod(iterator, gFields.iterator.next);
  526. if (entry) {
  527. jobject obj = env->CallObjectMethod(entry, gFields.entry.getKey);
  528. if (obj == NULL || !env->IsInstanceOf(obj, clazz)) {
  529. jniThrowException(env, "java/lang/IllegalArgumentException",
  530. "HashMap key is not a String");
  531. env->DeleteLocalRef(entry);
  532. *pIsOK = false;
  533. break;
  534. }
  535. jstring jkey = static_cast<jstring>(obj);
  536. obj = env->CallObjectMethod(entry, gFields.entry.getValue);
  537. if (obj == NULL || !env->IsInstanceOf(obj, clazz)) {
  538. jniThrowException(env, "java/lang/IllegalArgumentException",
  539. "HashMap value is not a String");
  540. env->DeleteLocalRef(entry);
  541. *pIsOK = false;
  542. break;
  543. }
  544. jstring jvalue = static_cast<jstring>(obj);
  545. String8 key = JStringToString8(env, jkey);
  546. String8 value = JStringToString8(env, jvalue);
  547. keyedVector.add(key, value);
  548. env->DeleteLocalRef(jkey);
  549. env->DeleteLocalRef(jvalue);
  550. hasNext = env->CallBooleanMethod(iterator, gFields.iterator.hasNext);
  551. }
  552. env->DeleteLocalRef(entry);
  553. }
  554. env->DeleteLocalRef(iterator);
  555. }
  556. env->DeleteLocalRef(entrySet);
  557. }
  558. return keyedVector;
  559. }
  560. static jobject KeyedVectorToHashMap (JNIEnv *env, KeyedVector<String8, String8> const &map) {
  561. jclass clazz = gFields.hashmapClassId;
  562. jobject hashMap = env->NewObject(clazz, gFields.hashmap.init);
  563. for (size_t i = 0; i < map.size(); ++i) {
  564. jstring jkey = env->NewStringUTF(map.keyAt(i).string());
  565. jstring jvalue = env->NewStringUTF(map.valueAt(i).string());
  566. env->CallObjectMethod(hashMap, gFields.hashmap.put, jkey, jvalue);
  567. env->DeleteLocalRef(jkey);
  568. env->DeleteLocalRef(jvalue);
  569. }
  570. return hashMap;
  571. }
  572. static jobject ListOfVectorsToArrayListOfByteArray(JNIEnv *env,
  573. List<Vector<uint8_t>> list) {
  574. jclass clazz = gFields.arraylistClassId;
  575. jobject arrayList = env->NewObject(clazz, gFields.arraylist.init);
  576. List<Vector<uint8_t>>::iterator iter = list.begin();
  577. while (iter != list.end()) {
  578. jbyteArray byteArray = VectorToJByteArray(env, *iter);
  579. env->CallBooleanMethod(arrayList, gFields.arraylist.add, byteArray);
  580. env->DeleteLocalRef(byteArray);
  581. iter++;
  582. }
  583. return arrayList;
  584. }
  585. } // namespace android
  586. using namespace android;
  587. static sp<JDrm> setDrm(
  588. JNIEnv *env, jobject thiz, const sp<JDrm> &drm) {
  589. sp<JDrm> old = (JDrm *)env->GetLongField(thiz, gFields.context);
  590. if (drm != NULL) {
  591. drm->incStrong(thiz);
  592. }
  593. if (old != NULL) {
  594. old->decStrong(thiz);
  595. }
  596. env->SetLongField(thiz, gFields.context, reinterpret_cast<jlong>(drm.get()));
  597. return old;
  598. }
  599. static bool CheckDrm(JNIEnv *env, const sp<IDrm> &drm) {
  600. if (drm == NULL) {
  601. jniThrowException(env, "java/lang/IllegalStateException", "MediaDrm obj is null");
  602. return false;
  603. }
  604. return true;
  605. }
  606. static bool CheckSession(JNIEnv *env, const sp<IDrm> &drm, jbyteArray const &jsessionId)
  607. {
  608. if (!CheckDrm(env, drm)) {
  609. return false;
  610. }
  611. if (jsessionId == NULL) {
  612. jniThrowException(env, "java/lang/IllegalArgumentException", "sessionId is null");
  613. return false;
  614. }
  615. return true;
  616. }
  617. static void android_media_MediaDrm_native_release(JNIEnv *env, jobject thiz) {
  618. sp<JDrm> drm = setDrm(env, thiz, NULL);
  619. if (drm != NULL) {
  620. drm->setListener(NULL);
  621. drm->disconnect();
  622. }
  623. }
  624. static void android_media_MediaDrm_native_init(JNIEnv *env) {
  625. jclass clazz;
  626. FIND_CLASS(clazz, "android/media/MediaDrm");
  627. GET_FIELD_ID(gFields.context, clazz, "mNativeContext", "J");
  628. GET_STATIC_METHOD_ID(gFields.post_event, clazz, "postEventFromNative",
  629. "(Ljava/lang/Object;IIILjava/lang/Object;)V");
  630. jfieldID field;
  631. GET_STATIC_FIELD_ID(field, clazz, "EVENT_PROVISION_REQUIRED", "I");
  632. gEventTypes.kEventProvisionRequired = env->GetStaticIntField(clazz, field);
  633. GET_STATIC_FIELD_ID(field, clazz, "EVENT_KEY_REQUIRED", "I");
  634. gEventTypes.kEventKeyRequired = env->GetStaticIntField(clazz, field);
  635. GET_STATIC_FIELD_ID(field, clazz, "EVENT_KEY_EXPIRED", "I");
  636. gEventTypes.kEventKeyExpired = env->GetStaticIntField(clazz, field);
  637. GET_STATIC_FIELD_ID(field, clazz, "EVENT_VENDOR_DEFINED", "I");
  638. gEventTypes.kEventVendorDefined = env->GetStaticIntField(clazz, field);
  639. GET_STATIC_FIELD_ID(field, clazz, "EVENT_SESSION_RECLAIMED", "I");
  640. gEventTypes.kEventSessionReclaimed = env->GetStaticIntField(clazz, field);
  641. GET_STATIC_FIELD_ID(field, clazz, "DRM_EVENT", "I");
  642. gEventWhat.kWhatDrmEvent = env->GetStaticIntField(clazz, field);
  643. GET_STATIC_FIELD_ID(field, clazz, "EXPIRATION_UPDATE", "I");
  644. gEventWhat.kWhatExpirationUpdate = env->GetStaticIntField(clazz, field);
  645. GET_STATIC_FIELD_ID(field, clazz, "KEY_STATUS_CHANGE", "I");
  646. gEventWhat.kWhatKeyStatusChange = env->GetStaticIntField(clazz, field);
  647. GET_STATIC_FIELD_ID(field, clazz, "SESSION_LOST_STATE", "I");
  648. gEventWhat.kWhatSessionLostState = env->GetStaticIntField(clazz, field);
  649. GET_STATIC_FIELD_ID(field, clazz, "KEY_TYPE_STREAMING", "I");
  650. gKeyTypes.kKeyTypeStreaming = env->GetStaticIntField(clazz, field);
  651. GET_STATIC_FIELD_ID(field, clazz, "KEY_TYPE_OFFLINE", "I");
  652. gKeyTypes.kKeyTypeOffline = env->GetStaticIntField(clazz, field);
  653. GET_STATIC_FIELD_ID(field, clazz, "KEY_TYPE_RELEASE", "I");
  654. gKeyTypes.kKeyTypeRelease = env->GetStaticIntField(clazz, field);
  655. GET_STATIC_FIELD_ID(field, clazz, "CERTIFICATE_TYPE_NONE", "I");
  656. gCertificateTypes.kCertificateTypeNone = env->GetStaticIntField(clazz, field);
  657. GET_STATIC_FIELD_ID(field, clazz, "CERTIFICATE_TYPE_X509", "I");
  658. gCertificateTypes.kCertificateTypeX509 = env->GetStaticIntField(clazz, field);
  659. GET_STATIC_FIELD_ID(field, clazz, "HDCP_LEVEL_UNKNOWN", "I");
  660. gHdcpLevels.kHdcpLevelUnknown = env->GetStaticIntField(clazz, field);
  661. GET_STATIC_FIELD_ID(field, clazz, "HDCP_NONE", "I");
  662. gHdcpLevels.kHdcpNone = env->GetStaticIntField(clazz, field);
  663. GET_STATIC_FIELD_ID(field, clazz, "HDCP_V1", "I");
  664. gHdcpLevels.kHdcpV1 = env->GetStaticIntField(clazz, field);
  665. GET_STATIC_FIELD_ID(field, clazz, "HDCP_V2", "I");
  666. gHdcpLevels.kHdcpV2 = env->GetStaticIntField(clazz, field);
  667. GET_STATIC_FIELD_ID(field, clazz, "HDCP_V2_1", "I");
  668. gHdcpLevels.kHdcpV2_1 = env->GetStaticIntField(clazz, field);
  669. GET_STATIC_FIELD_ID(field, clazz, "HDCP_V2_2", "I");
  670. gHdcpLevels.kHdcpV2_2 = env->GetStaticIntField(clazz, field);
  671. GET_STATIC_FIELD_ID(field, clazz, "HDCP_V2_3", "I");
  672. gHdcpLevels.kHdcpV2_3 = env->GetStaticIntField(clazz, field);
  673. GET_STATIC_FIELD_ID(field, clazz, "HDCP_NO_DIGITAL_OUTPUT", "I");
  674. gHdcpLevels.kHdcpNoOutput = env->GetStaticIntField(clazz, field);
  675. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_UNKNOWN", "I");
  676. gSecurityLevels.kSecurityLevelUnknown = env->GetStaticIntField(clazz, field);
  677. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_SW_SECURE_CRYPTO", "I");
  678. gSecurityLevels.kSecurityLevelSwSecureCrypto = env->GetStaticIntField(clazz, field);
  679. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_SW_SECURE_DECODE", "I");
  680. gSecurityLevels.kSecurityLevelSwSecureDecode = env->GetStaticIntField(clazz, field);
  681. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_HW_SECURE_CRYPTO", "I");
  682. gSecurityLevels.kSecurityLevelHwSecureCrypto = env->GetStaticIntField(clazz, field);
  683. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_HW_SECURE_DECODE", "I");
  684. gSecurityLevels.kSecurityLevelHwSecureDecode = env->GetStaticIntField(clazz, field);
  685. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_HW_SECURE_ALL", "I");
  686. gSecurityLevels.kSecurityLevelHwSecureAll = env->GetStaticIntField(clazz, field);
  687. GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_USABLE", "I");
  688. gOfflineLicenseStates.kOfflineLicenseStateUsable = env->GetStaticIntField(clazz, field);
  689. GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_RELEASED", "I");
  690. gOfflineLicenseStates.kOfflineLicenseStateReleased = env->GetStaticIntField(clazz, field);
  691. GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_UNKNOWN", "I");
  692. gOfflineLicenseStates.kOfflineLicenseStateUnknown = env->GetStaticIntField(clazz, field);
  693. GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_HW_SECURE_CRYPTO", "I");
  694. jmethodID getMaxSecurityLevel;
  695. GET_STATIC_METHOD_ID(getMaxSecurityLevel, clazz, "getMaxSecurityLevel", "()I");
  696. gSecurityLevels.kSecurityLevelMax = env->CallStaticIntMethod(clazz, getMaxSecurityLevel);
  697. FIND_CLASS(clazz, "android/media/MediaDrm$KeyRequest");
  698. GET_FIELD_ID(gFields.keyRequest.data, clazz, "mData", "[B");
  699. GET_FIELD_ID(gFields.keyRequest.defaultUrl, clazz, "mDefaultUrl", "Ljava/lang/String;");
  700. GET_FIELD_ID(gFields.keyRequest.requestType, clazz, "mRequestType", "I");
  701. GET_STATIC_FIELD_ID(field, clazz, "REQUEST_TYPE_INITIAL", "I");
  702. gKeyRequestTypes.kKeyRequestTypeInitial = env->GetStaticIntField(clazz, field);
  703. GET_STATIC_FIELD_ID(field, clazz, "REQUEST_TYPE_RENEWAL", "I");
  704. gKeyRequestTypes.kKeyRequestTypeRenewal = env->GetStaticIntField(clazz, field);
  705. GET_STATIC_FIELD_ID(field, clazz, "REQUEST_TYPE_RELEASE", "I");
  706. gKeyRequestTypes.kKeyRequestTypeRelease = env->GetStaticIntField(clazz, field);
  707. GET_STATIC_FIELD_ID(field, clazz, "REQUEST_TYPE_NONE", "I");
  708. gKeyRequestTypes.kKeyRequestTypeNone = env->GetStaticIntField(clazz, field);
  709. GET_STATIC_FIELD_ID(field, clazz, "REQUEST_TYPE_UPDATE", "I");
  710. gKeyRequestTypes.kKeyRequestTypeUpdate = env->GetStaticIntField(clazz, field);
  711. FIND_CLASS(clazz, "android/media/MediaDrm$ProvisionRequest");
  712. GET_FIELD_ID(gFields.provisionRequest.data, clazz, "mData", "[B");
  713. GET_FIELD_ID(gFields.provisionRequest.defaultUrl, clazz, "mDefaultUrl", "Ljava/lang/String;");
  714. FIND_CLASS(clazz, "android/media/MediaDrm$Certificate");
  715. GET_FIELD_ID(gFields.certificate.wrappedPrivateKey, clazz, "mWrappedKey", "[B");
  716. GET_FIELD_ID(gFields.certificate.certificateData, clazz, "mCertificateData", "[B");
  717. gFields.certificateClassId = static_cast<jclass>(env->NewGlobalRef(clazz));
  718. // Metrics-related fields and classes.
  719. FIND_CLASS(clazz, "android/os/PersistableBundle");
  720. jfieldID bundleCreatorId;
  721. GET_STATIC_FIELD_ID(bundleCreatorId, clazz, "CREATOR",
  722. "Landroid/os/Parcelable$Creator;");
  723. jobject bundleCreator;
  724. GET_STATIC_OBJECT_FIELD(bundleCreator, clazz, bundleCreatorId);
  725. gFields.bundleCreator = static_cast<jobject>(env->NewGlobalRef(bundleCreator));
  726. FIND_CLASS(clazz, "android/os/Parcelable$Creator");
  727. GET_METHOD_ID(gFields.createFromParcelId, clazz, "createFromParcel",
  728. "(Landroid/os/Parcel;)Ljava/lang/Object;");
  729. gFields.parcelCreatorClassId = static_cast<jclass>(env->NewGlobalRef(clazz));
  730. FIND_CLASS(clazz, "java/util/ArrayList");
  731. GET_METHOD_ID(gFields.arraylist.init, clazz, "<init>", "()V");
  732. GET_METHOD_ID(gFields.arraylist.add, clazz, "add", "(Ljava/lang/Object;)Z");
  733. FIND_CLASS(clazz, "java/util/HashMap");
  734. GET_METHOD_ID(gFields.hashmap.init, clazz, "<init>", "()V");
  735. GET_METHOD_ID(gFields.hashmap.get, clazz, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
  736. GET_METHOD_ID(gFields.hashmap.put, clazz, "put",
  737. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  738. GET_METHOD_ID(gFields.hashmap.entrySet, clazz, "entrySet", "()Ljava/util/Set;");
  739. FIND_CLASS(clazz, "java/util/Set");
  740. GET_METHOD_ID(gFields.set.iterator, clazz, "iterator", "()Ljava/util/Iterator;");
  741. FIND_CLASS(clazz, "java/util/Iterator");
  742. GET_METHOD_ID(gFields.iterator.next, clazz, "next", "()Ljava/lang/Object;");
  743. GET_METHOD_ID(gFields.iterator.hasNext, clazz, "hasNext", "()Z");
  744. FIND_CLASS(clazz, "java/util/Map$Entry");
  745. GET_METHOD_ID(gFields.entry.getKey, clazz, "getKey", "()Ljava/lang/Object;");
  746. GET_METHOD_ID(gFields.entry.getValue, clazz, "getValue", "()Ljava/lang/Object;");
  747. FIND_CLASS(clazz, "java/util/HashMap");
  748. gFields.hashmapClassId = static_cast<jclass>(env->NewGlobalRef(clazz));
  749. FIND_CLASS(clazz, "java/lang/String");
  750. gFields.stringClassId = static_cast<jclass>(env->NewGlobalRef(clazz));
  751. FIND_CLASS(clazz, "java/util/ArrayList");
  752. gFields.arraylistClassId = static_cast<jclass>(env->NewGlobalRef(clazz));
  753. FIND_CLASS(clazz, "android/media/MediaDrm$MediaDrmStateException");
  754. GET_METHOD_ID(gFields.stateException.init, clazz, "<init>", "(ILjava/lang/String;)V");
  755. gFields.stateException.classId = static_cast<jclass>(env->NewGlobalRef(clazz));
  756. FIND_CLASS(clazz, "android/media/MediaDrm$SessionException");
  757. GET_METHOD_ID(gFields.sessionException.init, clazz, "<init>", "(ILjava/lang/String;)V");
  758. gFields.sessionException.classId = static_cast<jclass>(env->NewGlobalRef(clazz));
  759. GET_FIELD_ID(gFields.sessionException.errorCode, clazz, "mErrorCode", "I");
  760. GET_STATIC_FIELD_ID(field, clazz, "ERROR_UNKNOWN", "I");
  761. gSessionExceptionErrorCodes.kErrorUnknown = env->GetStaticIntField(clazz, field);
  762. GET_STATIC_FIELD_ID(field, clazz, "ERROR_RESOURCE_CONTENTION", "I");
  763. gSessionExceptionErrorCodes.kResourceContention = env->GetStaticIntField(clazz, field);
  764. }
  765. static void android_media_MediaDrm_native_setup(
  766. JNIEnv *env, jobject thiz,
  767. jobject weak_this, jbyteArray uuidObj, jstring jappPackageName) {
  768. if (uuidObj == NULL) {
  769. jniThrowException(env, "java/lang/IllegalArgumentException", "uuid is null");
  770. return;
  771. }
  772. Vector<uint8_t> uuid = JByteArrayToVector(env, uuidObj);
  773. if (uuid.size() != 16) {
  774. jniThrowException(env, "java/lang/IllegalArgumentException",
  775. "invalid UUID size, expected 16 bytes");
  776. return;
  777. }
  778. String8 packageName;
  779. if (jappPackageName == NULL) {
  780. jniThrowException(env, "java/lang/IllegalArgumentException",
  781. "application package name cannot be null");
  782. return;
  783. }
  784. packageName = JStringToString8(env, jappPackageName);
  785. sp<JDrm> drm = new JDrm(env, thiz, uuid.array(), packageName);
  786. status_t err = drm->initCheck();
  787. if (err != OK) {
  788. jniThrowException(
  789. env,
  790. "android/media/UnsupportedSchemeException",
  791. "Failed to instantiate drm object.");
  792. return;
  793. }
  794. sp<JNIDrmListener> listener = new JNIDrmListener(env, thiz, weak_this);
  795. drm->setListener(listener);
  796. setDrm(env, thiz, drm);
  797. }
  798. DrmPlugin::SecurityLevel jintToSecurityLevel(jint jlevel) {
  799. DrmPlugin::SecurityLevel level;
  800. if (jlevel == gSecurityLevels.kSecurityLevelMax) {
  801. level = DrmPlugin::kSecurityLevelMax;
  802. } else if (jlevel == gSecurityLevels.kSecurityLevelSwSecureCrypto) {
  803. level = DrmPlugin::kSecurityLevelSwSecureCrypto;
  804. } else if (jlevel == gSecurityLevels.kSecurityLevelSwSecureDecode) {
  805. level = DrmPlugin::kSecurityLevelSwSecureDecode;
  806. } else if (jlevel == gSecurityLevels.kSecurityLevelHwSecureCrypto) {
  807. level = DrmPlugin::kSecurityLevelHwSecureCrypto;
  808. } else if (jlevel == gSecurityLevels.kSecurityLevelHwSecureDecode) {
  809. level = DrmPlugin::kSecurityLevelHwSecureDecode;
  810. } else if (jlevel == gSecurityLevels.kSecurityLevelHwSecureAll) {
  811. level = DrmPlugin::kSecurityLevelHwSecureAll;
  812. } else {
  813. level = DrmPlugin::kSecurityLevelUnknown;
  814. }
  815. return level;
  816. }
  817. static jboolean android_media_MediaDrm_isCryptoSchemeSupportedNative(
  818. JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj, jstring jmimeType,
  819. jint jSecurityLevel) {
  820. if (uuidObj == NULL) {
  821. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  822. return false;
  823. }
  824. Vector<uint8_t> uuid = JByteArrayToVector(env, uuidObj);
  825. if (uuid.size() != 16) {
  826. jniThrowException(
  827. env,
  828. "java/lang/IllegalArgumentException",
  829. "invalid UUID size, expected 16 bytes");
  830. return false;
  831. }
  832. String8 mimeType;
  833. if (jmimeType != NULL) {
  834. mimeType = JStringToString8(env, jmimeType);
  835. }
  836. DrmPlugin::SecurityLevel securityLevel = jintToSecurityLevel(jSecurityLevel);
  837. bool isSupported;
  838. status_t err = JDrm::IsCryptoSchemeSupported(uuid.array(), mimeType,
  839. securityLevel, &isSupported);
  840. if (throwExceptionAsNecessary(env, err, "Failed to query crypto scheme support")) {
  841. return false;
  842. }
  843. return isSupported;
  844. }
  845. static jbyteArray android_media_MediaDrm_openSession(
  846. JNIEnv *env, jobject thiz, jint jlevel) {
  847. sp<IDrm> drm = GetDrm(env, thiz);
  848. if (!CheckDrm(env, drm)) {
  849. return NULL;
  850. }
  851. Vector<uint8_t> sessionId;
  852. DrmPlugin::SecurityLevel level = jintToSecurityLevel(jlevel);
  853. if (level == DrmPlugin::kSecurityLevelUnknown) {
  854. jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid security level");
  855. return NULL;
  856. }
  857. status_t err = drm->openSession(level, sessionId);
  858. if (throwExceptionAsNecessary(env, err, "Failed to open session")) {
  859. return NULL;
  860. }
  861. return VectorToJByteArray(env, sessionId);
  862. }
  863. static void android_media_MediaDrm_closeSession(
  864. JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
  865. sp<IDrm> drm = GetDrm(env, thiz);
  866. if (!CheckSession(env, drm, jsessionId)) {
  867. return;
  868. }
  869. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  870. status_t err = drm->closeSession(sessionId);
  871. throwExceptionAsNecessary(env, err, "Failed to close session");
  872. }
  873. static jobject android_media_MediaDrm_getKeyRequest(
  874. JNIEnv *env, jobject thiz, jbyteArray jsessionId, jbyteArray jinitData,
  875. jstring jmimeType, jint jkeyType, jobject joptParams) {
  876. sp<IDrm> drm = GetDrm(env, thiz);
  877. if (!CheckSession(env, drm, jsessionId)) {
  878. return NULL;
  879. }
  880. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  881. Vector<uint8_t> initData;
  882. if (jinitData != NULL) {
  883. initData = JByteArrayToVector(env, jinitData);
  884. }
  885. String8 mimeType;
  886. if (jmimeType != NULL) {
  887. mimeType = JStringToString8(env, jmimeType);
  888. }
  889. DrmPlugin::KeyType keyType;
  890. if (jkeyType == gKeyTypes.kKeyTypeStreaming) {
  891. keyType = DrmPlugin::kKeyType_Streaming;
  892. } else if (jkeyType == gKeyTypes.kKeyTypeOffline) {
  893. keyType = DrmPlugin::kKeyType_Offline;
  894. } else if (jkeyType == gKeyTypes.kKeyTypeRelease) {
  895. keyType = DrmPlugin::kKeyType_Release;
  896. } else {
  897. jniThrowException(env, "java/lang/IllegalArgumentException",
  898. "invalid keyType");
  899. return NULL;
  900. }
  901. KeyedVector<String8, String8> optParams;
  902. if (joptParams != NULL) {
  903. bool isOK;
  904. optParams = HashMapToKeyedVector(env, joptParams, &isOK);
  905. if (!isOK) {
  906. return NULL;
  907. }
  908. }
  909. Vector<uint8_t> request;
  910. String8 defaultUrl;
  911. DrmPlugin::KeyRequestType keyRequestType;
  912. status_t err = drm->getKeyRequest(sessionId, initData, mimeType,
  913. keyType, optParams, request, defaultUrl, &keyRequestType);
  914. if (throwExceptionAsNecessary(env, err, "Failed to get key request")) {
  915. return NULL;
  916. }
  917. // Fill out return obj
  918. jclass clazz;
  919. FIND_CLASS(clazz, "android/media/MediaDrm$KeyRequest");
  920. jobject keyObj = NULL;
  921. if (clazz) {
  922. keyObj = env->AllocObject(clazz);
  923. jbyteArray jrequest = VectorToJByteArray(env, request);
  924. env->SetObjectField(keyObj, gFields.keyRequest.data, jrequest);
  925. jstring jdefaultUrl = env->NewStringUTF(defaultUrl.string());
  926. env->SetObjectField(keyObj, gFields.keyRequest.defaultUrl, jdefaultUrl);
  927. switch (keyRequestType) {
  928. case DrmPlugin::kKeyRequestType_Initial:
  929. env->SetIntField(keyObj, gFields.keyRequest.requestType,
  930. gKeyRequestTypes.kKeyRequestTypeInitial);
  931. break;
  932. case DrmPlugin::kKeyRequestType_Renewal:
  933. env->SetIntField(keyObj, gFields.keyRequest.requestType,
  934. gKeyRequestTypes.kKeyRequestTypeRenewal);
  935. break;
  936. case DrmPlugin::kKeyRequestType_Release:
  937. env->SetIntField(keyObj, gFields.keyRequest.requestType,
  938. gKeyRequestTypes.kKeyRequestTypeRelease);
  939. break;
  940. case DrmPlugin::kKeyRequestType_None:
  941. env->SetIntField(keyObj, gFields.keyRequest.requestType,
  942. gKeyRequestTypes.kKeyRequestTypeNone);
  943. break;
  944. case DrmPlugin::kKeyRequestType_Update:
  945. env->SetIntField(keyObj, gFields.keyRequest.requestType,
  946. gKeyRequestTypes.kKeyRequestTypeUpdate);
  947. break;
  948. default:
  949. throwStateException(env, "DRM plugin failure: unknown key request type",
  950. ERROR_DRM_UNKNOWN);
  951. break;
  952. }
  953. }
  954. return keyObj;
  955. }
  956. static jbyteArray android_media_MediaDrm_provideKeyResponse(
  957. JNIEnv *env, jobject thiz, jbyteArray jsessionId, jbyteArray jresponse) {
  958. sp<IDrm> drm = GetDrm(env, thiz);
  959. if (!CheckSession(env, drm, jsessionId)) {
  960. return NULL;
  961. }
  962. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  963. if (jresponse == NULL) {
  964. jniThrowException(env, "java/lang/IllegalArgumentException",
  965. "key response is null");
  966. return NULL;
  967. }
  968. Vector<uint8_t> response(JByteArrayToVector(env, jresponse));
  969. Vector<uint8_t> keySetId;
  970. status_t err = drm->provideKeyResponse(sessionId, response, keySetId);
  971. if (throwExceptionAsNecessary(env, err, "Failed to handle key response")) {
  972. return NULL;
  973. }
  974. return VectorToJByteArray(env, keySetId);
  975. }
  976. static void android_media_MediaDrm_removeKeys(
  977. JNIEnv *env, jobject thiz, jbyteArray jkeysetId) {
  978. sp<IDrm> drm = GetDrm(env, thiz);
  979. if (!CheckDrm(env, drm)) {
  980. return;
  981. }
  982. if (jkeysetId == NULL) {
  983. jniThrowException(env, "java/lang/IllegalArgumentException",
  984. "keySetId is null");
  985. return;
  986. }
  987. Vector<uint8_t> keySetId(JByteArrayToVector(env, jkeysetId));
  988. status_t err = drm->removeKeys(keySetId);
  989. throwExceptionAsNecessary(env, err, "Failed to remove keys");
  990. }
  991. static void android_media_MediaDrm_restoreKeys(
  992. JNIEnv *env, jobject thiz, jbyteArray jsessionId,
  993. jbyteArray jkeysetId) {
  994. sp<IDrm> drm = GetDrm(env, thiz);
  995. if (!CheckSession(env, drm, jsessionId)) {
  996. return;
  997. }
  998. if (jkeysetId == NULL) {
  999. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  1000. return;
  1001. }
  1002. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1003. Vector<uint8_t> keySetId(JByteArrayToVector(env, jkeysetId));
  1004. status_t err = drm->restoreKeys(sessionId, keySetId);
  1005. throwExceptionAsNecessary(env, err, "Failed to restore keys");
  1006. }
  1007. static jobject android_media_MediaDrm_queryKeyStatus(
  1008. JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
  1009. sp<IDrm> drm = GetDrm(env, thiz);
  1010. if (!CheckSession(env, drm, jsessionId)) {
  1011. return NULL;
  1012. }
  1013. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1014. KeyedVector<String8, String8> infoMap;
  1015. status_t err = drm->queryKeyStatus(sessionId, infoMap);
  1016. if (throwExceptionAsNecessary(env, err, "Failed to query key status")) {
  1017. return NULL;
  1018. }
  1019. return KeyedVectorToHashMap(env, infoMap);
  1020. }
  1021. static jobject android_media_MediaDrm_getProvisionRequestNative(
  1022. JNIEnv *env, jobject thiz, jint jcertType, jstring jcertAuthority) {
  1023. sp<IDrm> drm = GetDrm(env, thiz);
  1024. if (!CheckDrm(env, drm)) {
  1025. return NULL;
  1026. }
  1027. Vector<uint8_t> request;
  1028. String8 defaultUrl;
  1029. String8 certType;
  1030. if (jcertType == gCertificateTypes.kCertificateTypeX509) {
  1031. certType = "X.509";
  1032. } else if (jcertType == gCertificateTypes.kCertificateTypeNone) {
  1033. certType = "none";
  1034. } else {
  1035. certType = "invalid";
  1036. }
  1037. String8 certAuthority = JStringToString8(env, jcertAuthority);
  1038. status_t err = drm->getProvisionRequest(certType, certAuthority, request, defaultUrl);
  1039. if (throwExceptionAsNecessary(env, err, "Failed to get provision request")) {
  1040. return NULL;
  1041. }
  1042. // Fill out return obj
  1043. jclass clazz;
  1044. FIND_CLASS(clazz, "android/media/MediaDrm$ProvisionRequest");
  1045. jobject provisionObj = NULL;
  1046. if (clazz) {
  1047. provisionObj = env->AllocObject(clazz);
  1048. jbyteArray jrequest = VectorToJByteArray(env, request);
  1049. env->SetObjectField(provisionObj, gFields.provisionRequest.data, jrequest);
  1050. jstring jdefaultUrl = env->NewStringUTF(defaultUrl.string());
  1051. env->SetObjectField(provisionObj, gFields.provisionRequest.defaultUrl, jdefaultUrl);
  1052. }
  1053. return provisionObj;
  1054. }
  1055. static jobject android_media_MediaDrm_provideProvisionResponseNative(
  1056. JNIEnv *env, jobject thiz, jbyteArray jresponse) {
  1057. sp<IDrm> drm = GetDrm(env, thiz);
  1058. if (!CheckDrm(env, drm)) {
  1059. return NULL;
  1060. }
  1061. if (jresponse == NULL) {
  1062. jniThrowException(env, "java/lang/IllegalArgumentException",
  1063. "provision response is null");
  1064. return NULL;
  1065. }
  1066. Vector<uint8_t> response(JByteArrayToVector(env, jresponse));
  1067. Vector<uint8_t> certificate, wrappedKey;
  1068. status_t err = drm->provideProvisionResponse(response, certificate, wrappedKey);
  1069. // Fill out return obj
  1070. jclass clazz = gFields.certificateClassId;
  1071. jobject certificateObj = NULL;
  1072. if (clazz && certificate.size() && wrappedKey.size()) {
  1073. certificateObj = env->AllocObject(clazz);
  1074. jbyteArray jcertificate = VectorToJByteArray(env, certificate);
  1075. env->SetObjectField(certificateObj, gFields.certificate.certificateData, jcertificate);
  1076. jbyteArray jwrappedKey = VectorToJByteArray(env, wrappedKey);
  1077. env->SetObjectField(certificateObj, gFields.certificate.wrappedPrivateKey, jwrappedKey);
  1078. }
  1079. throwExceptionAsNecessary(env, err, "Failed to handle provision response");
  1080. return certificateObj;
  1081. }
  1082. static jobject android_media_MediaDrm_getSecureStops(
  1083. JNIEnv *env, jobject thiz) {
  1084. sp<IDrm> drm = GetDrm(env, thiz);
  1085. if (!CheckDrm(env, drm)) {
  1086. return NULL;
  1087. }
  1088. List<Vector<uint8_t>> secureStops;
  1089. status_t err = drm->getSecureStops(secureStops);
  1090. if (throwExceptionAsNecessary(env, err, "Failed to get secure stops")) {
  1091. return NULL;
  1092. }
  1093. return ListOfVectorsToArrayListOfByteArray(env, secureStops);
  1094. }
  1095. static jobject android_media_MediaDrm_getSecureStopIds(
  1096. JNIEnv *env, jobject thiz) {
  1097. sp<IDrm> drm = GetDrm(env, thiz);
  1098. if (!CheckDrm(env, drm)) {
  1099. return NULL;
  1100. }
  1101. List<Vector<uint8_t>> secureStopIds;
  1102. status_t err = drm->getSecureStopIds(secureStopIds);
  1103. if (throwExceptionAsNecessary(env, err, "Failed to get secure stop Ids")) {
  1104. return NULL;
  1105. }
  1106. return ListOfVectorsToArrayListOfByteArray(env, secureStopIds);
  1107. }
  1108. static jbyteArray android_media_MediaDrm_getSecureStop(
  1109. JNIEnv *env, jobject thiz, jbyteArray ssid) {
  1110. sp<IDrm> drm = GetDrm(env, thiz);
  1111. if (!CheckDrm(env, drm)) {
  1112. return NULL;
  1113. }
  1114. Vector<uint8_t> secureStop;
  1115. status_t err = drm->getSecureStop(JByteArrayToVector(env, ssid), secureStop);
  1116. if (throwExceptionAsNecessary(env, err, "Failed to get secure stop")) {
  1117. return NULL;
  1118. }
  1119. return VectorToJByteArray(env, secureStop);
  1120. }
  1121. static void android_media_MediaDrm_releaseSecureStops(
  1122. JNIEnv *env, jobject thiz, jbyteArray jssRelease) {
  1123. sp<IDrm> drm = GetDrm(env, thiz);
  1124. if (!CheckDrm(env, drm)) {
  1125. return;
  1126. }
  1127. Vector<uint8_t> ssRelease(JByteArrayToVector(env, jssRelease));
  1128. status_t err = drm->releaseSecureStops(ssRelease);
  1129. throwExceptionAsNecessary(env, err, "Failed to release secure stops");
  1130. }
  1131. static void android_media_MediaDrm_removeSecureStop(
  1132. JNIEnv *env, jobject thiz, jbyteArray ssid) {
  1133. sp<IDrm> drm = GetDrm(env, thiz);
  1134. if (!CheckDrm(env, drm)) {
  1135. return;
  1136. }
  1137. status_t err = drm->removeSecureStop(JByteArrayToVector(env, ssid));
  1138. throwExceptionAsNecessary(env, err, "Failed to remove secure stop");
  1139. }
  1140. static void android_media_MediaDrm_removeAllSecureStops(
  1141. JNIEnv *env, jobject thiz) {
  1142. sp<IDrm> drm = GetDrm(env, thiz);
  1143. if (!CheckDrm(env, drm)) {
  1144. return;
  1145. }
  1146. status_t err = drm->removeAllSecureStops();
  1147. throwExceptionAsNecessary(env, err, "Failed to remove all secure stops");
  1148. }
  1149. static jint HdcpLevelTojint(DrmPlugin::HdcpLevel level) {
  1150. switch(level) {
  1151. case DrmPlugin::kHdcpLevelUnknown:
  1152. return gHdcpLevels.kHdcpLevelUnknown;
  1153. case DrmPlugin::kHdcpNone:
  1154. return gHdcpLevels.kHdcpNone;
  1155. case DrmPlugin::kHdcpV1:
  1156. return gHdcpLevels.kHdcpV1;
  1157. case DrmPlugin::kHdcpV2:
  1158. return gHdcpLevels.kHdcpV2;
  1159. case DrmPlugin::kHdcpV2_1:
  1160. return gHdcpLevels.kHdcpV2_1;
  1161. case DrmPlugin::kHdcpV2_2:
  1162. return gHdcpLevels.kHdcpV2_2;
  1163. case DrmPlugin::kHdcpV2_3:
  1164. return gHdcpLevels.kHdcpV2_3;
  1165. case DrmPlugin::kHdcpNoOutput:
  1166. return gHdcpLevels.kHdcpNoOutput;
  1167. }
  1168. return gHdcpLevels.kHdcpNone;
  1169. }
  1170. static jint android_media_MediaDrm_getConnectedHdcpLevel(JNIEnv *env,
  1171. jobject thiz) {
  1172. sp<IDrm> drm = GetDrm(env, thiz);
  1173. if (!CheckDrm(env, drm)) {
  1174. return gHdcpLevels.kHdcpNone;
  1175. }
  1176. DrmPlugin::HdcpLevel connected = DrmPlugin::kHdcpNone;
  1177. DrmPlugin::HdcpLevel max = DrmPlugin::kHdcpNone;
  1178. status_t err = drm->getHdcpLevels(&connected, &max);
  1179. if (throwExceptionAsNecessary(env, err, "Failed to get HDCP levels")) {
  1180. return gHdcpLevels.kHdcpLevelUnknown;
  1181. }
  1182. return HdcpLevelTojint(connected);
  1183. }
  1184. static jint android_media_MediaDrm_getMaxHdcpLevel(JNIEnv *env,
  1185. jobject thiz) {
  1186. sp<IDrm> drm = GetDrm(env, thiz);
  1187. if (!CheckDrm(env, drm)) {
  1188. return gHdcpLevels.kHdcpLevelUnknown;
  1189. }
  1190. DrmPlugin::HdcpLevel connected = DrmPlugin::kHdcpLevelUnknown;
  1191. DrmPlugin::HdcpLevel max = DrmPlugin::kHdcpLevelUnknown;
  1192. status_t err = drm->getHdcpLevels(&connected, &max);
  1193. if (throwExceptionAsNecessary(env, err, "Failed to get HDCP levels")) {
  1194. return gHdcpLevels.kHdcpLevelUnknown;
  1195. }
  1196. return HdcpLevelTojint(max);
  1197. }
  1198. static jint android_media_MediaDrm_getOpenSessionCount(JNIEnv *env,
  1199. jobject thiz) {
  1200. sp<IDrm> drm = GetDrm(env, thiz);
  1201. if (!CheckDrm(env, drm)) {
  1202. return 0;
  1203. }
  1204. uint32_t open = 0, max = 0;
  1205. status_t err = drm->getNumberOfSessions(&open, &max);
  1206. if (throwExceptionAsNecessary(env, err, "Failed to get number of sessions")) {
  1207. return 0;
  1208. }
  1209. return open;
  1210. }
  1211. static jint android_media_MediaDrm_getMaxSessionCount(JNIEnv *env,
  1212. jobject thiz) {
  1213. sp<IDrm> drm = GetDrm(env, thiz);
  1214. if (!CheckDrm(env, drm)) {
  1215. return 0;
  1216. }
  1217. uint32_t open = 0, max = 0;
  1218. status_t err = drm->getNumberOfSessions(&open, &max);
  1219. if (throwExceptionAsNecessary(env, err, "Failed to get number of sessions")) {
  1220. return 0;
  1221. }
  1222. return max;
  1223. }
  1224. static jint android_media_MediaDrm_getSecurityLevel(JNIEnv *env,
  1225. jobject thiz, jbyteArray jsessionId) {
  1226. sp<IDrm> drm = GetDrm(env, thiz);
  1227. if (!CheckSession(env, drm, jsessionId)) {
  1228. return gSecurityLevels.kSecurityLevelUnknown;
  1229. }
  1230. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1231. DrmPlugin::SecurityLevel level = DrmPlugin::kSecurityLevelUnknown;
  1232. status_t err = drm->getSecurityLevel(sessionId, &level);
  1233. if (throwExceptionAsNecessary(env, err, "Failed to get security level")) {
  1234. return gSecurityLevels.kSecurityLevelUnknown;
  1235. }
  1236. switch(level) {
  1237. case DrmPlugin::kSecurityLevelSwSecureCrypto:
  1238. return gSecurityLevels.kSecurityLevelSwSecureCrypto;
  1239. case DrmPlugin::kSecurityLevelSwSecureDecode:
  1240. return gSecurityLevels.kSecurityLevelSwSecureDecode;
  1241. case DrmPlugin::kSecurityLevelHwSecureCrypto:
  1242. return gSecurityLevels.kSecurityLevelHwSecureCrypto;
  1243. case DrmPlugin::kSecurityLevelHwSecureDecode:
  1244. return gSecurityLevels.kSecurityLevelHwSecureDecode;
  1245. case DrmPlugin::kSecurityLevelHwSecureAll:
  1246. return gSecurityLevels.kSecurityLevelHwSecureAll;
  1247. default:
  1248. return gSecurityLevels.kSecurityLevelUnknown;
  1249. }
  1250. }
  1251. static jobject android_media_MediaDrm_getOfflineLicenseKeySetIds(
  1252. JNIEnv *env, jobject thiz) {
  1253. sp<IDrm> drm = GetDrm(env, thiz);
  1254. if (!CheckDrm(env, drm)) {
  1255. return NULL;
  1256. }
  1257. List<Vector<uint8_t> > keySetIds;
  1258. status_t err = drm->getOfflineLicenseKeySetIds(keySetIds);
  1259. if (throwExceptionAsNecessary(env, err, "Failed to get offline key set Ids")) {
  1260. return NULL;
  1261. }
  1262. return ListOfVectorsToArrayListOfByteArray(env, keySetIds);
  1263. }
  1264. static void android_media_MediaDrm_removeOfflineLicense(
  1265. JNIEnv *env, jobject thiz, jbyteArray keySetId) {
  1266. sp<IDrm> drm = GetDrm(env, thiz);
  1267. if (!CheckDrm(env, drm)) {
  1268. return;
  1269. }
  1270. status_t err = drm->removeOfflineLicense(JByteArrayToVector(env, keySetId));
  1271. throwExceptionAsNecessary(env, err, "Failed to remove offline license");
  1272. }
  1273. static jint android_media_MediaDrm_getOfflineLicenseState(JNIEnv *env,
  1274. jobject thiz, jbyteArray jkeySetId) {
  1275. sp<IDrm> drm = GetDrm(env, thiz);
  1276. if (!CheckDrm(env, drm)) {
  1277. return gOfflineLicenseStates.kOfflineLicenseStateUnknown;
  1278. }
  1279. Vector<uint8_t> keySetId(JByteArrayToVector(env, jkeySetId));
  1280. DrmPlugin::OfflineLicenseState state = DrmPlugin::kOfflineLicenseStateUnknown;
  1281. status_t err = drm->getOfflineLicenseState(keySetId, &state);
  1282. if (throwExceptionAsNecessary(env, err, "Failed to get offline license state")) {
  1283. return gOfflineLicenseStates.kOfflineLicenseStateUnknown;
  1284. }
  1285. switch(state) {
  1286. case DrmPlugin::kOfflineLicenseStateUsable:
  1287. return gOfflineLicenseStates.kOfflineLicenseStateUsable;
  1288. case DrmPlugin::kOfflineLicenseStateReleased:
  1289. return gOfflineLicenseStates.kOfflineLicenseStateReleased;
  1290. default:
  1291. return gOfflineLicenseStates.kOfflineLicenseStateUnknown;
  1292. }
  1293. }
  1294. static jstring android_media_MediaDrm_getPropertyString(
  1295. JNIEnv *env, jobject thiz, jstring jname) {
  1296. sp<IDrm> drm = GetDrm(env, thiz);
  1297. if (!CheckDrm(env, drm)) {
  1298. return NULL;
  1299. }
  1300. if (jname == NULL) {
  1301. jniThrowException(env, "java/lang/IllegalArgumentException",
  1302. "property name String is null");
  1303. return NULL;
  1304. }
  1305. String8 name = JStringToString8(env, jname);
  1306. String8 value;
  1307. status_t err = drm->getPropertyString(name, value);
  1308. if (throwExceptionAsNecessary(env, err, "Failed to get property")) {
  1309. return NULL;
  1310. }
  1311. return env->NewStringUTF(value.string());
  1312. }
  1313. static jbyteArray android_media_MediaDrm_getPropertyByteArray(
  1314. JNIEnv *env, jobject thiz, jstring jname) {
  1315. sp<IDrm> drm = GetDrm(env, thiz);
  1316. if (!CheckDrm(env, drm)) {
  1317. return NULL;
  1318. }
  1319. if (jname == NULL) {
  1320. jniThrowException(env, "java/lang/IllegalArgumentException",
  1321. "property name String is null");
  1322. return NULL;
  1323. }
  1324. String8 name = JStringToString8(env, jname);
  1325. Vector<uint8_t> value;
  1326. status_t err = drm->getPropertyByteArray(name, value);
  1327. if (throwExceptionAsNecessary(env, err, "Failed to get property")) {
  1328. return NULL;
  1329. }
  1330. return VectorToJByteArray(env, value);
  1331. }
  1332. static void android_media_MediaDrm_setPropertyString(
  1333. JNIEnv *env, jobject thiz, jstring jname, jstring jvalue) {
  1334. sp<IDrm> drm = GetDrm(env, thiz);
  1335. if (!CheckDrm(env, drm)) {
  1336. return;
  1337. }
  1338. if (jname == NULL) {
  1339. jniThrowException(env, "java/lang/IllegalArgumentException",
  1340. "property name String is null");
  1341. return;
  1342. }
  1343. if (jvalue == NULL) {
  1344. jniThrowException(env, "java/lang/IllegalArgumentException",
  1345. "property value String is null");
  1346. return;
  1347. }
  1348. String8 name = JStringToString8(env, jname);
  1349. String8 value = JStringToString8(env, jvalue);
  1350. status_t err = drm->setPropertyString(name, value);
  1351. throwExceptionAsNecessary(env, err, "Failed to set property");
  1352. }
  1353. static void android_media_MediaDrm_setPropertyByteArray(
  1354. JNIEnv *env, jobject thiz, jstring jname, jbyteArray jvalue) {
  1355. sp<IDrm> drm = GetDrm(env, thiz);
  1356. if (!CheckDrm(env, drm)) {
  1357. return;
  1358. }
  1359. if (jname == NULL) {
  1360. jniThrowException(env, "java/lang/IllegalArgumentException",
  1361. "property name String is null");
  1362. return;
  1363. }
  1364. if (jvalue == NULL) {
  1365. jniThrowException(env, "java/lang/IllegalArgumentException",
  1366. "property value byte array is null");
  1367. return;
  1368. }
  1369. String8 name = JStringToString8(env, jname);
  1370. Vector<uint8_t> value = JByteArrayToVector(env, jvalue);
  1371. status_t err = drm->setPropertyByteArray(name, value);
  1372. throwExceptionAsNecessary(env, err, "Failed to set property");
  1373. }
  1374. static void android_media_MediaDrm_setCipherAlgorithmNative(
  1375. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1376. jstring jalgorithm) {
  1377. sp<IDrm> drm = GetDrm(env, jdrm);
  1378. if (!CheckSession(env, drm, jsessionId)) {
  1379. return;
  1380. }
  1381. if (jalgorithm == NULL) {
  1382. jniThrowException(env, "java/lang/IllegalArgumentException",
  1383. "algorithm String is null");
  1384. return;
  1385. }
  1386. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1387. String8 algorithm = JStringToString8(env, jalgorithm);
  1388. status_t err = drm->setCipherAlgorithm(sessionId, algorithm);
  1389. throwExceptionAsNecessary(env, err, "Failed to set cipher algorithm");
  1390. }
  1391. static void android_media_MediaDrm_setMacAlgorithmNative(
  1392. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1393. jstring jalgorithm) {
  1394. sp<IDrm> drm = GetDrm(env, jdrm);
  1395. if (!CheckSession(env, drm, jsessionId)) {
  1396. return;
  1397. }
  1398. if (jalgorithm == NULL) {
  1399. jniThrowException(env, "java/lang/IllegalArgumentException",
  1400. "algorithm String is null");
  1401. return;
  1402. }
  1403. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1404. String8 algorithm = JStringToString8(env, jalgorithm);
  1405. status_t err = drm->setMacAlgorithm(sessionId, algorithm);
  1406. throwExceptionAsNecessary(env, err, "Failed to set mac algorithm");
  1407. }
  1408. static jbyteArray android_media_MediaDrm_encryptNative(
  1409. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1410. jbyteArray jkeyId, jbyteArray jinput, jbyteArray jiv) {
  1411. sp<IDrm> drm = GetDrm(env, jdrm);
  1412. if (!CheckSession(env, drm, jsessionId)) {
  1413. return NULL;
  1414. }
  1415. if (jkeyId == NULL || jinput == NULL || jiv == NULL) {
  1416. jniThrowException(env, "java/lang/IllegalArgumentException",
  1417. "required argument is null");
  1418. return NULL;
  1419. }
  1420. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1421. Vector<uint8_t> keyId(JByteArrayToVector(env, jkeyId));
  1422. Vector<uint8_t> input(JByteArrayToVector(env, jinput));
  1423. Vector<uint8_t> iv(JByteArrayToVector(env, jiv));
  1424. Vector<uint8_t> output;
  1425. status_t err = drm->encrypt(sessionId, keyId, input, iv, output);
  1426. if (throwExceptionAsNecessary(env, err, "Failed to encrypt")) {
  1427. return NULL;
  1428. }
  1429. return VectorToJByteArray(env, output);
  1430. }
  1431. static jbyteArray android_media_MediaDrm_decryptNative(
  1432. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1433. jbyteArray jkeyId, jbyteArray jinput, jbyteArray jiv) {
  1434. sp<IDrm> drm = GetDrm(env, jdrm);
  1435. if (!CheckSession(env, drm, jsessionId)) {
  1436. return NULL;
  1437. }
  1438. if (jkeyId == NULL || jinput == NULL || jiv == NULL) {
  1439. jniThrowException(env, "java/lang/IllegalArgumentException",
  1440. "required argument is null");
  1441. return NULL;
  1442. }
  1443. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1444. Vector<uint8_t> keyId(JByteArrayToVector(env, jkeyId));
  1445. Vector<uint8_t> input(JByteArrayToVector(env, jinput));
  1446. Vector<uint8_t> iv(JByteArrayToVector(env, jiv));
  1447. Vector<uint8_t> output;
  1448. status_t err = drm->decrypt(sessionId, keyId, input, iv, output);
  1449. if (throwExceptionAsNecessary(env, err, "Failed to decrypt")) {
  1450. return NULL;
  1451. }
  1452. return VectorToJByteArray(env, output);
  1453. }
  1454. static jbyteArray android_media_MediaDrm_signNative(
  1455. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1456. jbyteArray jkeyId, jbyteArray jmessage) {
  1457. sp<IDrm> drm = GetDrm(env, jdrm);
  1458. if (!CheckSession(env, drm, jsessionId)) {
  1459. return NULL;
  1460. }
  1461. if (jkeyId == NULL || jmessage == NULL) {
  1462. jniThrowException(env, "java/lang/IllegalArgumentException",
  1463. "required argument is null");
  1464. return NULL;
  1465. }
  1466. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1467. Vector<uint8_t> keyId(JByteArrayToVector(env, jkeyId));
  1468. Vector<uint8_t> message(JByteArrayToVector(env, jmessage));
  1469. Vector<uint8_t> signature;
  1470. status_t err = drm->sign(sessionId, keyId, message, signature);
  1471. if (throwExceptionAsNecessary(env, err, "Failed to sign")) {
  1472. return NULL;
  1473. }
  1474. return VectorToJByteArray(env, signature);
  1475. }
  1476. static jboolean android_media_MediaDrm_verifyNative(
  1477. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1478. jbyteArray jkeyId, jbyteArray jmessage, jbyteArray jsignature) {
  1479. sp<IDrm> drm = GetDrm(env, jdrm);
  1480. if (!CheckSession(env, drm, jsessionId)) {
  1481. return false;
  1482. }
  1483. if (jkeyId == NULL || jmessage == NULL || jsignature == NULL) {
  1484. jniThrowException(env, "java/lang/IllegalArgumentException",
  1485. "required argument is null");
  1486. return false;
  1487. }
  1488. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1489. Vector<uint8_t> keyId(JByteArrayToVector(env, jkeyId));
  1490. Vector<uint8_t> message(JByteArrayToVector(env, jmessage));
  1491. Vector<uint8_t> signature(JByteArrayToVector(env, jsignature));
  1492. bool match;
  1493. status_t err = drm->verify(sessionId, keyId, message, signature, match);
  1494. throwExceptionAsNecessary(env, err, "Failed to verify");
  1495. return match;
  1496. }
  1497. static jobject
  1498. android_media_MediaDrm_native_getMetrics(JNIEnv *env, jobject thiz)
  1499. {
  1500. sp<IDrm> drm = GetDrm(env, thiz);
  1501. if (!CheckDrm(env, drm)) {
  1502. return NULL;
  1503. }
  1504. // Retrieve current metrics snapshot from drm.
  1505. PersistableBundle metrics;
  1506. status_t err = drm->getMetrics(&metrics);
  1507. if (err != OK) {
  1508. ALOGE("getMetrics failed: %d", (int)err);
  1509. return (jobject) NULL;
  1510. }
  1511. return nativeToJavaPersistableBundle(env, thiz, &metrics);
  1512. }
  1513. static jbyteArray android_media_MediaDrm_signRSANative(
  1514. JNIEnv *env, jobject /* thiz */, jobject jdrm, jbyteArray jsessionId,
  1515. jstring jalgorithm, jbyteArray jwrappedKey, jbyteArray jmessage) {
  1516. sp<IDrm> drm = GetDrm(env, jdrm);
  1517. if (!CheckSession(env, drm, jsessionId)) {
  1518. return NULL;
  1519. }
  1520. if (jalgorithm == NULL || jwrappedKey == NULL || jmessage == NULL) {
  1521. jniThrowException(env, "java/lang/IllegalArgumentException",
  1522. "required argument is null");
  1523. return NULL;
  1524. }
  1525. Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
  1526. String8 algorithm = JStringToString8(env, jalgorithm);
  1527. Vector<uint8_t> wrappedKey(JByteArrayToVector(env, jwrappedKey));
  1528. Vector<uint8_t> message(JByteArrayToVector(env, jmessage));
  1529. Vector<uint8_t> signature;
  1530. status_t err = drm->signRSA(sessionId, algorithm, message, wrappedKey, signature);
  1531. if (throwExceptionAsNecessary(env, err, "Failed to sign")) {
  1532. return NULL;
  1533. }
  1534. return VectorToJByteArray(env, signature);
  1535. }
  1536. static const JNINativeMethod gMethods[] = {
  1537. { "native_release", "()V", (void *)android_media_MediaDrm_native_release },
  1538. { "native_init", "()V", (void *)android_media_MediaDrm_native_init },
  1539. { "native_setup", "(Ljava/lang/Object;[BLjava/lang/String;)V",
  1540. (void *)android_media_MediaDrm_native_setup },
  1541. { "isCryptoSchemeSupportedNative", "([BLjava/lang/String;I)Z",
  1542. (void *)android_media_MediaDrm_isCryptoSchemeSupportedNative },
  1543. { "openSession", "(I)[B",
  1544. (void *)android_media_MediaDrm_openSession },
  1545. { "closeSession", "([B)V",
  1546. (void *)android_media_MediaDrm_closeSession },
  1547. { "getKeyRequest", "([B[BLjava/lang/String;ILjava/util/HashMap;)"
  1548. "Landroid/media/MediaDrm$KeyRequest;",
  1549. (void *)android_media_MediaDrm_getKeyRequest },
  1550. { "provideKeyResponse", "([B[B)[B",
  1551. (void *)android_media_MediaDrm_provideKeyResponse },
  1552. { "removeKeys", "([B)V",
  1553. (void *)android_media_MediaDrm_removeKeys },
  1554. { "restoreKeys", "([B[B)V",
  1555. (void *)android_media_MediaDrm_restoreKeys },
  1556. { "queryKeyStatus", "([B)Ljava/util/HashMap;",
  1557. (void *)android_media_MediaDrm_queryKeyStatus },
  1558. { "getProvisionRequestNative", "(ILjava/lang/String;)Landroid/media/MediaDrm$ProvisionRequest;",
  1559. (void *)android_media_MediaDrm_getProvisionRequestNative },
  1560. { "provideProvisionResponseNative", "([B)Landroid/media/MediaDrm$Certificate;",
  1561. (void *)android_media_MediaDrm_provideProvisionResponseNative },
  1562. { "getSecureStops", "()Ljava/util/List;",
  1563. (void *)android_media_MediaDrm_getSecureStops },
  1564. { "getSecureStopIds", "()Ljava/util/List;",
  1565. (void *)android_media_MediaDrm_getSecureStopIds },
  1566. { "getSecureStop", "([B)[B",
  1567. (void *)android_media_MediaDrm_getSecureStop },
  1568. { "releaseSecureStops", "([B)V",
  1569. (void *)android_media_MediaDrm_releaseSecureStops },
  1570. { "removeSecureStop", "([B)V",
  1571. (void *)android_media_MediaDrm_removeSecureStop },
  1572. { "removeAllSecureStops", "()V",
  1573. (void *)android_media_MediaDrm_removeAllSecureStops },
  1574. { "getConnectedHdcpLevel", "()I",
  1575. (void *)android_media_MediaDrm_getConnectedHdcpLevel },
  1576. { "getMaxHdcpLevel", "()I",
  1577. (void *)android_media_MediaDrm_getMaxHdcpLevel },
  1578. { "getOpenSessionCount", "()I",
  1579. (void *)android_media_MediaDrm_getOpenSessionCount },
  1580. { "getMaxSessionCount", "()I",
  1581. (void *)android_media_MediaDrm_getMaxSessionCount },
  1582. { "getSecurityLevel", "([B)I",
  1583. (void *)android_media_MediaDrm_getSecurityLevel },
  1584. { "removeOfflineLicense", "([B)V",
  1585. (void *)android_media_MediaDrm_removeOfflineLicense },
  1586. { "getOfflineLicenseKeySetIds", "()Ljava/util/List;",
  1587. (void *)android_media_MediaDrm_getOfflineLicenseKeySetIds },
  1588. { "getOfflineLicenseState", "([B)I",
  1589. (void *)android_media_MediaDrm_getOfflineLicenseState },
  1590. { "getPropertyString", "(Ljava/lang/String;)Ljava/lang/String;",
  1591. (void *)android_media_MediaDrm_getPropertyString },
  1592. { "getPropertyByteArray", "(Ljava/lang/String;)[B",
  1593. (void *)android_media_MediaDrm_getPropertyByteArray },
  1594. { "setPropertyString", "(Ljava/lang/String;Ljava/lang/String;)V",
  1595. (void *)android_media_MediaDrm_setPropertyString },
  1596. { "setPropertyByteArray", "(Ljava/lang/String;[B)V",
  1597. (void *)android_media_MediaDrm_setPropertyByteArray },
  1598. { "setCipherAlgorithmNative",
  1599. "(Landroid/media/MediaDrm;[BLjava/lang/String;)V",
  1600. (void *)android_media_MediaDrm_setCipherAlgorithmNative },
  1601. { "setMacAlgorithmNative",
  1602. "(Landroid/media/MediaDrm;[BLjava/lang/String;)V",
  1603. (void *)android_media_MediaDrm_setMacAlgorithmNative },
  1604. { "encryptNative", "(Landroid/media/MediaDrm;[B[B[B[B)[B",
  1605. (void *)android_media_MediaDrm_encryptNative },
  1606. { "decryptNative", "(Landroid/media/MediaDrm;[B[B[B[B)[B",
  1607. (void *)android_media_MediaDrm_decryptNative },
  1608. { "signNative", "(Landroid/media/MediaDrm;[B[B[B)[B",
  1609. (void *)android_media_MediaDrm_signNative },
  1610. { "verifyNative", "(Landroid/media/MediaDrm;[B[B[B[B)Z",
  1611. (void *)android_media_MediaDrm_verifyNative },
  1612. { "signRSANative", "(Landroid/media/MediaDrm;[BLjava/lang/String;[B[B)[B",
  1613. (void *)android_media_MediaDrm_signRSANative },
  1614. { "getMetricsNative", "()Landroid/os/PersistableBundle;",
  1615. (void *)android_media_MediaDrm_native_getMetrics },
  1616. };
  1617. int register_android_media_Drm(JNIEnv *env) {
  1618. return AndroidRuntime::registerNativeMethods(env,
  1619. "android/media/MediaDrm", gMethods, NELEM(gMethods));
  1620. }