| 1 |
/* |
| 2 |
* Copyright (C) 2023 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WVDecrypter.h" |
| 10 |
|
| 11 |
#include "decrypters/Helpers.h" |
| 12 |
#include "WVCencSingleSampleDecrypter.h" |
| 13 |
#include "common/AdaptiveDecrypter.h" |
| 14 |
#include "jsmn.h" |
| 15 |
#include "kodi/tools/StringUtils.h" |
| 16 |
#include "utils/Base64Utils.h" |
| 17 |
#include "utils/DigestMD5Utils.h" |
| 18 |
#include "utils/StringUtils.h" |
| 19 |
#include "utils/Utils.h" |
| 20 |
|
| 21 |
#include <chrono> |
| 22 |
#include <deque> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <thread> |
| 26 |
|
| 27 |
#include <bento4/Ap4.h> |
| 28 |
#include <jni/src/ClassLoader.h> |
| 29 |
#include <jni/src/UUID.h> |
| 30 |
#include <kodi/Filesystem.h> |
| 31 |
|
| 32 |
using namespace DRM; |
| 33 |
using namespace UTILS; |
| 34 |
using namespace kodi::tools; |
| 35 |
|
| 36 |
namespace |
| 37 |
{ |
| 38 |
kodi::platform::CInterfaceAndroidSystem* ANDROID_SYSTEM{nullptr}; |
| 39 |
} // unnamed namespace |
| 40 |
|
| 41 |
CMediaDrmOnEventListener::CMediaDrmOnEventListener(CMediaDrmOnEventCallback* decrypterEventCallback, |
| 42 |
CJNIClassLoader* classLoader) |
| 43 |
: CJNIMediaDrmOnEventListener(classLoader), m_decrypterEventCallback(decrypterEventCallback) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
void CMediaDrmOnEventListener::onEvent(const CJNIMediaDrm& mediaDrm, |
| 48 |
const std::vector<char>& sessionId, |
| 49 |
int event, |
| 50 |
int extra, |
| 51 |
const std::vector<char>& data) |
| 52 |
{ |
| 53 |
m_decrypterEventCallback->OnMediaDrmEvent(mediaDrm, sessionId, event, extra, data); |
| 54 |
} |
| 55 |
|
| 56 |
CWVDecrypterA::CWVDecrypterA() : m_keySystem(NONE), m_WVCdmAdapter(nullptr) |
| 57 |
{ |
| 58 |
// CInterfaceAndroidSystem need to be initialized at runtime |
| 59 |
// then we have to set it to global variable just now |
| 60 |
ANDROID_SYSTEM = &m_androidSystem; |
| 61 |
}; |
| 62 |
|
| 63 |
CWVDecrypterA::~CWVDecrypterA() |
| 64 |
{ |
| 65 |
delete m_WVCdmAdapter; |
| 66 |
m_WVCdmAdapter = nullptr; |
| 67 |
|
| 68 |
#ifdef DRMTHREAD |
| 69 |
m_jniCondition.notify_one(); |
| 70 |
m_jniWorker->join(); |
| 71 |
delete m_jniWorker; |
| 72 |
#endif |
| 73 |
}; |
| 74 |
|
| 75 |
#ifdef DRMTHREAD |
| 76 |
void JNIThread(JavaVM* vm) |
| 77 |
{ |
| 78 |
m_jniCondition.notify_one(); |
| 79 |
std::unique_lock<std::mutex> lk(m_jniMutex); |
| 80 |
m_jniCondition.wait(lk); |
| 81 |
|
| 82 |
LOG::Log(SSDDEBUG, "JNI thread terminated"); |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
std::vector<std::string_view> CWVDecrypterA::SelectKeySystems(std::string_view keySystem) |
| 87 |
{ |
| 88 |
LOG::Log(LOGDEBUG, "Key system request: %s", keySystem); |
| 89 |
std::vector<std::string_view> keySystems; |
| 90 |
if (keySystem == KS_WIDEVINE) |
| 91 |
{ |
| 92 |
m_keySystem = WIDEVINE; |
| 93 |
keySystems.push_back(URN_WIDEVINE); |
| 94 |
} |
| 95 |
else if (keySystem == KS_WISEPLAY) |
| 96 |
{ |
| 97 |
m_keySystem = WISEPLAY; |
| 98 |
keySystems.push_back(URN_WISEPLAY); |
| 99 |
} |
| 100 |
else if (keySystem == KS_PLAYREADY) |
| 101 |
{ |
| 102 |
m_keySystem = PLAYREADY; |
| 103 |
keySystems.push_back(URN_PLAYREADY); |
| 104 |
} |
| 105 |
return keySystems; |
| 106 |
} |
| 107 |
|
| 108 |
bool CWVDecrypterA::OpenDRMSystem(std::string_view licenseURL, |
| 109 |
const std::vector<uint8_t>& serverCertificate, |
| 110 |
const uint8_t config) |
| 111 |
{ |
| 112 |
if (m_keySystem == NONE) |
| 113 |
return false; |
| 114 |
|
| 115 |
if (licenseURL.empty()) |
| 116 |
{ |
| 117 |
LOG::LogF(LOGERROR, "License Key property cannot be empty"); |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
m_WVCdmAdapter = new CWVCdmAdapterA(m_keySystem, licenseURL, serverCertificate, |
| 122 |
m_mediaDrmEventListener.get(), this); |
| 123 |
|
| 124 |
return m_WVCdmAdapter->GetMediaDrm(); |
| 125 |
} |
| 126 |
|
| 127 |
Adaptive_CencSingleSampleDecrypter* CWVDecrypterA::CreateSingleSampleDecrypter( |
| 128 |
std::vector<uint8_t>& initData, |
| 129 |
std::string_view optionalKeyParameter, |
| 130 |
const std::vector<uint8_t>& defaultKeyId, |
| 131 |
std::string_view licenseUrl, |
| 132 |
bool skipSessionMessage, |
| 133 |
CryptoMode cryptoMode) |
| 134 |
{ |
| 135 |
CWVCencSingleSampleDecrypterA* decrypter = new CWVCencSingleSampleDecrypterA( |
| 136 |
*m_WVCdmAdapter, initData, optionalKeyParameter, defaultKeyId, this); |
| 137 |
|
| 138 |
{ |
| 139 |
std::lock_guard<std::mutex> lk(m_decrypterListMutex); |
| 140 |
m_decrypterList.push_back(decrypter); |
| 141 |
} |
| 142 |
|
| 143 |
if (!(*decrypter->GetSessionId() && decrypter->StartSession(skipSessionMessage))) |
| 144 |
{ |
| 145 |
DestroySingleSampleDecrypter(decrypter); |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
return decrypter; |
| 149 |
} |
| 150 |
|
| 151 |
void CWVDecrypterA::DestroySingleSampleDecrypter(Adaptive_CencSingleSampleDecrypter* decrypter) |
| 152 |
{ |
| 153 |
if (decrypter) |
| 154 |
{ |
| 155 |
std::vector<CWVCencSingleSampleDecrypterA*>::const_iterator res = |
| 156 |
std::find(m_decrypterList.begin(), m_decrypterList.end(), decrypter); |
| 157 |
if (res != m_decrypterList.end()) |
| 158 |
{ |
| 159 |
std::lock_guard<std::mutex> lk(m_decrypterListMutex); |
| 160 |
m_decrypterList.erase(res); |
| 161 |
} |
| 162 |
delete static_cast<CWVCencSingleSampleDecrypterA*>(decrypter); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void CWVDecrypterA::GetCapabilities(Adaptive_CencSingleSampleDecrypter* decrypter, |
| 167 |
const std::vector<uint8_t>& keyId, |
| 168 |
uint32_t media, |
| 169 |
DecrypterCapabilites& caps) |
| 170 |
{ |
| 171 |
if (decrypter) |
| 172 |
static_cast<CWVCencSingleSampleDecrypterA*>(decrypter)->GetCapabilities(keyId, media, caps); |
| 173 |
else |
| 174 |
caps = {0, 0, 0}; |
| 175 |
} |
| 176 |
|
| 177 |
bool CWVDecrypterA::HasLicenseKey(Adaptive_CencSingleSampleDecrypter* decrypter, |
| 178 |
const std::vector<uint8_t>& keyId) |
| 179 |
{ |
| 180 |
if (decrypter) |
| 181 |
return static_cast<CWVCencSingleSampleDecrypterA*>(decrypter)->HasLicenseKey(keyId); |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
std::string CWVDecrypterA::GetChallengeB64Data(Adaptive_CencSingleSampleDecrypter* decrypter) |
| 186 |
{ |
| 187 |
if (!decrypter) |
| 188 |
return ""; |
| 189 |
|
| 190 |
const std::vector<char> data = static_cast<CWVCencSingleSampleDecrypterA*>(decrypter)->GetChallengeData(); |
| 191 |
return BASE64::Encode(data); |
| 192 |
} |
| 193 |
|
| 194 |
void CWVDecrypterA::OnMediaDrmEvent(const CJNIMediaDrm& mediaDrm, |
| 195 |
const std::vector<char>& sessionId, |
| 196 |
int event, |
| 197 |
int extra, |
| 198 |
const std::vector<char>& data) |
| 199 |
{ |
| 200 |
LOG::LogF(LOGDEBUG, "%d arrived, #decrypter: %lu", event, m_decrypterList.size()); |
| 201 |
//we have only one DRM system running (m_WVCdmAdapter) so there is no need to compare mediaDrm |
| 202 |
std::lock_guard<std::mutex> lk(m_decrypterListMutex); |
| 203 |
for (std::vector<CWVCencSingleSampleDecrypterA*>::iterator b(m_decrypterList.begin()), |
| 204 |
e(m_decrypterList.end()); |
| 205 |
b != e; ++b) |
| 206 |
{ |
| 207 |
if (sessionId.empty() || (*b)->GetSessionIdRaw() == sessionId) |
| 208 |
{ |
| 209 |
switch (event) |
| 210 |
{ |
| 211 |
case CJNIMediaDrm::EVENT_KEY_REQUIRED: |
| 212 |
(*b)->RequestNewKeys(); |
| 213 |
break; |
| 214 |
default:; |
| 215 |
} |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
LOG::LogF(LOGDEBUG, "Session does not match: sizes: %lu -> %lu", sessionId.size(), |
| 220 |
(*b)->GetSessionIdRaw().size()); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
bool CWVDecrypterA::Initialize() |
| 226 |
{ |
| 227 |
#ifdef DRMTHREAD |
| 228 |
std::unique_lock<std::mutex> lk(m_jniMutex); |
| 229 |
m_jniWorker = new std::thread(&CWVDecrypterA::JNIThread, this, |
| 230 |
reinterpret_cast<JavaVM*>(m_androidSystem.GetJNIEnv())); |
| 231 |
m_jniCondition.wait(lk); |
| 232 |
#endif |
| 233 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 234 |
{ |
| 235 |
LOG::LogF(LOGERROR, "Failed to load MediaDrmOnEventListener"); |
| 236 |
xbmc_jnienv()->ExceptionDescribe(); |
| 237 |
xbmc_jnienv()->ExceptionClear(); |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
//JNIEnv* env = static_cast<JNIEnv*>(m_androidSystem.GetJNIEnv()); |
| 242 |
CJNIClassLoader* classLoader; |
| 243 |
CJNIBase::SetSDKVersion(m_androidSystem.GetSDKVersion()); |
| 244 |
CJNIBase::SetBaseClassName(m_androidSystem.GetClassName()); |
| 245 |
LOG::Log(LOGDEBUG, "WVDecrypter JNI, SDK version: %d", m_androidSystem.GetSDKVersion()); |
| 246 |
|
| 247 |
const char* apkEnv = getenv("XBMC_ANDROID_APK"); |
| 248 |
if (!apkEnv) |
| 249 |
apkEnv = getenv("KODI_ANDROID_APK"); |
| 250 |
|
| 251 |
if (!apkEnv) |
| 252 |
return false; |
| 253 |
|
| 254 |
std::string apkPath = apkEnv; |
| 255 |
|
| 256 |
//! @todo: make classLoader a smartpointer |
| 257 |
classLoader = new CJNIClassLoader(apkPath); |
| 258 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 259 |
{ |
| 260 |
LOG::LogF(LOGERROR, "Failed to create ClassLoader"); |
| 261 |
xbmc_jnienv()->ExceptionDescribe(); |
| 262 |
xbmc_jnienv()->ExceptionClear(); |
| 263 |
|
| 264 |
delete classLoader, classLoader = nullptr; |
| 265 |
|
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
m_classLoader = classLoader; |
| 270 |
m_mediaDrmEventListener = std::make_unique<CMediaDrmOnEventListener>(this, m_classLoader); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
// Definition for the xbmc_jnienv method of the jni utils (jutils.hpp) |
| 275 |
JNIEnv* xbmc_jnienv() |
| 276 |
{ |
| 277 |
return static_cast<JNIEnv*>(ANDROID_SYSTEM->GetJNIEnv()); |
| 278 |
} |