| 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 "WVCencSingleSampleDecrypter.h" |
| 10 |
|
| 11 |
#include "CompSettings.h" |
| 12 |
#include "SrvBroker.h" |
| 13 |
#include "WVCdmAdapter.h" |
| 14 |
#include "WVDecrypter.h" |
| 15 |
#include "jsmn.h" |
| 16 |
#include "decrypters/Helpers.h" |
| 17 |
#include "utils/Base64Utils.h" |
| 18 |
#include "utils/CurlUtils.h" |
| 19 |
#include "utils/DigestMD5Utils.h" |
| 20 |
#include "utils/FileUtils.h" |
| 21 |
#include "utils/StringUtils.h" |
| 22 |
#include "utils/Utils.h" |
| 23 |
#include "utils/log.h" |
| 24 |
|
| 25 |
#include <thread> |
| 26 |
|
| 27 |
using namespace UTILS; |
| 28 |
using namespace kodi::tools; |
| 29 |
|
| 30 |
CWVCencSingleSampleDecrypterA::CWVCencSingleSampleDecrypterA(CWVCdmAdapterA& drm, |
| 31 |
std::vector<uint8_t>& pssh, |
| 32 |
std::string_view optionalKeyParameter, |
| 33 |
const std::vector<uint8_t>& defaultKeyId, |
| 34 |
CWVDecrypterA* host) |
| 35 |
: m_mediaDrm(drm), |
| 36 |
m_isProvisioningRequested(false), |
| 37 |
m_isKeyUpdateRequested(false), |
| 38 |
m_hdcpLimit(0), |
| 39 |
m_resolutionLimit(0), |
| 40 |
m_defaultKeyId{defaultKeyId}, |
| 41 |
m_host{host} |
| 42 |
{ |
| 43 |
SetParentIsOwner(false); |
| 44 |
|
| 45 |
if (pssh.size() < 4 || pssh.size() > 65535) |
| 46 |
{ |
| 47 |
LOG::LogF(LOGERROR, "PSSH init data with length %zu seems not to be cenc init data", |
| 48 |
pssh.size()); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
m_pssh = pssh; |
| 53 |
// No cenc init data with PSSH box format, create one |
| 54 |
if (memcmp(pssh.data() + 4, "pssh", 4) != 0) |
| 55 |
{ |
| 56 |
// PSSH box version 0 (no kid's) |
| 57 |
static const uint8_t atomHeader[12] = {0x00, 0x00, 0x00, 0x00, 0x70, 0x73, |
| 58 |
0x73, 0x68, 0x00, 0x00, 0x00, 0x00}; |
| 59 |
|
| 60 |
std::vector<uint8_t> psshAtom; |
| 61 |
psshAtom.assign(atomHeader, atomHeader + 12); // PSSH Box header |
| 62 |
psshAtom.insert(psshAtom.end(), m_mediaDrm.GetKeySystem(), m_mediaDrm.GetKeySystem() + 16); // System ID |
| 63 |
// Add data size bytes |
| 64 |
psshAtom.resize(30, 0); // 2 zero bytes |
| 65 |
psshAtom.emplace_back(static_cast<uint8_t>((m_pssh.size()) >> 8)); |
| 66 |
psshAtom.emplace_back(static_cast<uint8_t>(m_pssh.size())); |
| 67 |
|
| 68 |
psshAtom.insert(psshAtom.end(), m_pssh.begin(), m_pssh.end()); // Data |
| 69 |
// Update box size |
| 70 |
psshAtom[2] = static_cast<uint8_t>(psshAtom.size() >> 8); |
| 71 |
psshAtom[3] = static_cast<uint8_t>(psshAtom.size()); |
| 72 |
m_pssh = psshAtom; |
| 73 |
} |
| 74 |
|
| 75 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 76 |
{ |
| 77 |
std::string debugFilePath = |
| 78 |
FILESYS::PathCombine(m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.init"); |
| 79 |
std::string data{reinterpret_cast<const char*>(m_pssh.data()), m_pssh.size()}; |
| 80 |
FILESYS::SaveFile(debugFilePath, data, true); |
| 81 |
} |
| 82 |
|
| 83 |
m_initialPssh = m_pssh; |
| 84 |
|
| 85 |
if (!optionalKeyParameter.empty()) |
| 86 |
m_optParams["PRCustomData"] = optionalKeyParameter; |
| 87 |
|
| 88 |
/* |
| 89 |
std::vector<char> pui = m_mediaDrm.GetMediaDrm()->getPropertyByteArray("provisioningUniqueId"); |
| 90 |
xbmc_jnienv()->ExceptionClear(); |
| 91 |
if (pui.size() > 0) |
| 92 |
{ |
| 93 |
std::string encoded = BASE64::Encode(pui); |
| 94 |
m_optParams["CDMID"] = encoded; |
| 95 |
} |
| 96 |
*/ |
| 97 |
|
| 98 |
bool L3FallbackRequested = false; |
| 99 |
RETRY_OPEN: |
| 100 |
m_sessionId = m_mediaDrm.GetMediaDrm()->openSession(); |
| 101 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 102 |
{ |
| 103 |
xbmc_jnienv()->ExceptionClear(); |
| 104 |
if (!m_isProvisioningRequested) |
| 105 |
{ |
| 106 |
LOG::LogF(LOGWARNING, "Exception during open session - provisioning..."); |
| 107 |
m_isProvisioningRequested = true; |
| 108 |
if (!ProvisionRequest()) |
| 109 |
{ |
| 110 |
if (!L3FallbackRequested && |
| 111 |
m_mediaDrm.GetMediaDrm()->getPropertyString("securityLevel") == "L1") |
| 112 |
{ |
| 113 |
LOG::LogF(LOGWARNING, "L1 provisioning failed - retrying with L3..."); |
| 114 |
L3FallbackRequested = true; |
| 115 |
m_isProvisioningRequested = false; |
| 116 |
m_mediaDrm.GetMediaDrm()->setPropertyString("securityLevel", "L3"); |
| 117 |
goto RETRY_OPEN; |
| 118 |
} |
| 119 |
else |
| 120 |
return; |
| 121 |
} |
| 122 |
goto RETRY_OPEN; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
LOG::LogF(LOGERROR, "Exception during open session - abort"); |
| 127 |
return; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (m_sessionId.size() == 0) |
| 132 |
{ |
| 133 |
LOG::LogF(LOGERROR, "Unable to open DRM session"); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
memcpy(m_sessionIdChar, m_sessionId.data(), m_sessionId.size()); |
| 138 |
m_sessionIdChar[m_sessionId.size()] = 0; |
| 139 |
|
| 140 |
if (m_mediaDrm.GetKeySystemType() != PLAYREADY) |
| 141 |
{ |
| 142 |
int maxSecuritylevel = m_mediaDrm.GetMediaDrm()->getMaxSecurityLevel(); |
| 143 |
xbmc_jnienv()->ExceptionClear(); |
| 144 |
|
| 145 |
LOG::Log(LOGDEBUG, "Session ID: %s, Max security level: %d", m_sessionIdChar, maxSecuritylevel); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
CWVCencSingleSampleDecrypterA::~CWVCencSingleSampleDecrypterA() |
| 150 |
{ |
| 151 |
if (!m_sessionId.empty()) |
| 152 |
{ |
| 153 |
m_mediaDrm.GetMediaDrm()->removeKeys(m_sessionId); |
| 154 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 155 |
{ |
| 156 |
LOG::LogF(LOGERROR, "removeKeys has raised an exception"); |
| 157 |
xbmc_jnienv()->ExceptionClear(); |
| 158 |
} |
| 159 |
m_mediaDrm.GetMediaDrm()->closeSession(m_sessionId); |
| 160 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 161 |
{ |
| 162 |
LOG::LogF(LOGERROR, "closeSession has raised an exception"); |
| 163 |
xbmc_jnienv()->ExceptionClear(); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
const char* CWVCencSingleSampleDecrypterA::GetSessionId() |
| 169 |
{ |
| 170 |
return m_sessionIdChar; |
| 171 |
} |
| 172 |
|
| 173 |
std::vector<char> CWVCencSingleSampleDecrypterA::GetChallengeData() |
| 174 |
{ |
| 175 |
return m_keyRequestData; |
| 176 |
} |
| 177 |
|
| 178 |
bool CWVCencSingleSampleDecrypterA::HasLicenseKey(const std::vector<uint8_t>& keyId) |
| 179 |
{ |
| 180 |
// true = one session for all streams, false = one sessions per stream |
| 181 |
// false fixes pixaltion issues on some devices when manifest has multiple encrypted streams |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
void CWVCencSingleSampleDecrypterA::GetCapabilities(const std::vector<uint8_t>& keyId, |
| 186 |
uint32_t media, |
| 187 |
DecrypterCapabilites& caps) |
| 188 |
{ |
| 189 |
caps = {DecrypterCapabilites::SSD_SECURE_PATH | DecrypterCapabilites::SSD_ANNEXB_REQUIRED, 0, |
| 190 |
m_hdcpLimit}; |
| 191 |
|
| 192 |
if (caps.hdcpLimit == 0) |
| 193 |
caps.hdcpLimit = m_resolutionLimit; |
| 194 |
|
| 195 |
// Note: Currently we check for L1 only, Kodi core at later time check if secure decoder is needed |
| 196 |
// by using requiresSecureDecoderComponent method of MediaDrm API |
| 197 |
// https://github.com/xbmc/xbmc/blob/Nexus/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecAndroidMediaCodec.cpp#L639-L641 |
| 198 |
if (m_mediaDrm.GetMediaDrm()->getPropertyString("securityLevel") == "L1") |
| 199 |
{ |
| 200 |
caps.hdcpLimit = m_resolutionLimit; //No restriction |
| 201 |
caps.flags |= DecrypterCapabilites::SSD_SECURE_DECODER; |
| 202 |
} |
| 203 |
LOG::LogF(LOGDEBUG, "hdcpLimit: %i", caps.hdcpLimit); |
| 204 |
|
| 205 |
caps.hdcpVersion = 99; |
| 206 |
} |
| 207 |
|
| 208 |
bool CWVCencSingleSampleDecrypterA::ProvisionRequest() |
| 209 |
{ |
| 210 |
LOG::Log(LOGWARNING, "Provision data request (DRM:%p)", m_mediaDrm.GetMediaDrm()); |
| 211 |
|
| 212 |
CJNIMediaDrmProvisionRequest request = m_mediaDrm.GetMediaDrm()->getProvisionRequest(); |
| 213 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 214 |
{ |
| 215 |
LOG::LogF(LOGERROR, "getProvisionRequest has raised an exception"); |
| 216 |
xbmc_jnienv()->ExceptionClear(); |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
std::vector<char> provData = request.getData(); |
| 221 |
std::string url = request.getDefaultUrl(); |
| 222 |
|
| 223 |
LOG::Log(LOGDEBUG, "Provision data size: %lu, url: %s", provData.size(), url.c_str()); |
| 224 |
|
| 225 |
std::string reqData("{\"signedRequest\":\""); |
| 226 |
reqData += std::string(provData.data(), provData.size()); |
| 227 |
reqData += "\"}"; |
| 228 |
reqData = BASE64::Encode(reqData); |
| 229 |
|
| 230 |
CURL::CUrl file(url); |
| 231 |
file.AddHeader("Content-Type", "application/json"); |
| 232 |
file.AddHeader("postdata", reqData.c_str()); |
| 233 |
|
| 234 |
int statusCode = file.Open(); |
| 235 |
if (statusCode == -1 || statusCode >= 400) |
| 236 |
{ |
| 237 |
LOG::Log(LOGERROR, "Provisioning server returned failure"); |
| 238 |
return false; |
| 239 |
} |
| 240 |
provData.clear(); |
| 241 |
|
| 242 |
// read the file |
| 243 |
std::string response; |
| 244 |
CURL::ReadStatus downloadStatus = CURL::ReadStatus::CHUNK_READ; |
| 245 |
while (downloadStatus == CURL::ReadStatus::CHUNK_READ) |
| 246 |
{ |
| 247 |
downloadStatus = file.Read(response); |
| 248 |
} |
| 249 |
std::copy(response.begin(), response.end(), std::back_inserter(provData)); |
| 250 |
|
| 251 |
m_mediaDrm.GetMediaDrm()->provideProvisionResponse(provData); |
| 252 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 253 |
{ |
| 254 |
LOG::LogF(LOGERROR, "provideProvisionResponse has raised an exception"); |
| 255 |
xbmc_jnienv()->ExceptionClear(); |
| 256 |
return false; |
| 257 |
} |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
bool CWVCencSingleSampleDecrypterA::GetKeyRequest(std::vector<char>& keyRequestData) |
| 262 |
{ |
| 263 |
CJNIMediaDrmKeyRequest keyRequest = m_mediaDrm.GetMediaDrm()->getKeyRequest( |
| 264 |
m_sessionId, m_pssh, "video/mp4", CJNIMediaDrm::KEY_TYPE_STREAMING, m_optParams); |
| 265 |
|
| 266 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 267 |
{ |
| 268 |
xbmc_jnienv()->ExceptionClear(); |
| 269 |
if (!m_isProvisioningRequested) |
| 270 |
{ |
| 271 |
LOG::Log(LOGWARNING, "Key request not successful - trying provisioning"); |
| 272 |
m_isProvisioningRequested = true; |
| 273 |
return GetKeyRequest(keyRequestData); |
| 274 |
} |
| 275 |
else |
| 276 |
LOG::LogF(LOGERROR, "Key request not successful"); |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
keyRequestData = keyRequest.getData(); |
| 281 |
LOG::Log(LOGDEBUG, "Key request successful size: %lu", keyRequestData.size()); |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
bool CWVCencSingleSampleDecrypterA::KeyUpdateRequest(bool waitKeys, bool skipSessionMessage) |
| 286 |
{ |
| 287 |
if (!GetKeyRequest(m_keyRequestData)) |
| 288 |
return false; |
| 289 |
|
| 290 |
m_pssh.clear(); |
| 291 |
m_optParams.clear(); |
| 292 |
|
| 293 |
if (skipSessionMessage) |
| 294 |
return true; |
| 295 |
|
| 296 |
m_isKeyUpdateRequested = false; |
| 297 |
if (!SendSessionMessage(m_keyRequestData)) |
| 298 |
return false; |
| 299 |
|
| 300 |
if (waitKeys && m_keyRequestData.size() == 2) // Service Certificate call |
| 301 |
{ |
| 302 |
for (unsigned int i(0); i < 100 && !m_isKeyUpdateRequested; ++i) |
| 303 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 304 |
|
| 305 |
if (m_isKeyUpdateRequested) |
| 306 |
KeyUpdateRequest(false, false); |
| 307 |
else |
| 308 |
{ |
| 309 |
LOG::LogF(LOGERROR, "Timeout waiting for EVENT_KEYS_REQUIRED!"); |
| 310 |
return false; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (m_mediaDrm.GetKeySystemType() != PLAYREADY) |
| 315 |
{ |
| 316 |
int securityLevel = m_mediaDrm.GetMediaDrm()->getSecurityLevel(m_sessionId); |
| 317 |
xbmc_jnienv()->ExceptionClear(); |
| 318 |
LOG::Log(LOGDEBUG, "Security level: %d", securityLevel); |
| 319 |
|
| 320 |
std::map<std::string, std::string> keyStatus = |
| 321 |
m_mediaDrm.GetMediaDrm()->queryKeyStatus(m_sessionId); |
| 322 |
LOG::Log(LOGDEBUG, "Key status (%ld):", keyStatus.size()); |
| 323 |
for (auto const& ks : keyStatus) |
| 324 |
{ |
| 325 |
LOG::Log(LOGDEBUG, "-> %s -> %s", ks.first.c_str(), ks.second.c_str()); |
| 326 |
} |
| 327 |
} |
| 328 |
return true; |
| 329 |
} |
| 330 |
|
| 331 |
bool CWVCencSingleSampleDecrypterA::SendSessionMessage(const std::vector<char>& keyRequestData) |
| 332 |
{ |
| 333 |
std::vector<std::string> blocks{StringUtils::Split(m_mediaDrm.GetLicenseURL(), '|')}; |
| 334 |
|
| 335 |
if (blocks.size() != 4) |
| 336 |
{ |
| 337 |
LOG::LogF(LOGERROR, "Wrong \"|\" blocks in license URL. Four blocks (req | header | body | " |
| 338 |
"response) are expected in license URL"); |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 343 |
{ |
| 344 |
std::string debugFilePath = FILESYS::PathCombine( |
| 345 |
m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.challenge"); |
| 346 |
UTILS::FILESYS::SaveFile(debugFilePath, keyRequestData.data(), true); |
| 347 |
} |
| 348 |
|
| 349 |
//Process placeholder in GET String |
| 350 |
std::string::size_type insPos(blocks[0].find("{SSM}")); |
| 351 |
if (insPos != std::string::npos) |
| 352 |
{ |
| 353 |
if (insPos > 0 && blocks[0][insPos - 1] == 'B') |
| 354 |
{ |
| 355 |
std::string msgEncoded = BASE64::Encode(keyRequestData); |
| 356 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 357 |
blocks[0].replace(insPos - 1, 6, msgEncoded); |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
LOG::Log(LOGERROR, "Unsupported License request template (command)"); |
| 362 |
return false; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
insPos = blocks[0].find("{HASH}"); |
| 367 |
if (insPos != std::string::npos) |
| 368 |
{ |
| 369 |
DIGEST::MD5 md5; |
| 370 |
md5.Update(keyRequestData.data(), static_cast<uint32_t>(keyRequestData.size())); |
| 371 |
md5.Finalize(); |
| 372 |
blocks[0].replace(insPos, 6, md5.HexDigest()); |
| 373 |
} |
| 374 |
|
| 375 |
CURL::CUrl file(blocks[0]); |
| 376 |
std::string response; |
| 377 |
std::string resLimit; |
| 378 |
std::string contentType; |
| 379 |
|
| 380 |
//Process headers |
| 381 |
std::vector<std::string> headers{StringUtils::Split(blocks[1], '&')}; |
| 382 |
for (std::string& headerStr : headers) |
| 383 |
{ |
| 384 |
std::vector<std::string> header{StringUtils::Split(headerStr, '=')}; |
| 385 |
if (!header.empty()) |
| 386 |
{ |
| 387 |
StringUtils::Trim(header[0]); |
| 388 |
std::string value; |
| 389 |
if (header.size() > 1) |
| 390 |
{ |
| 391 |
StringUtils::Trim(header[1]); |
| 392 |
value = STRING::URLDecode(header[1]); |
| 393 |
} |
| 394 |
file.AddHeader(header[0], value); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
//Process body |
| 399 |
if (!blocks[2].empty()) |
| 400 |
{ |
| 401 |
if (blocks[2][0] == '%') |
| 402 |
blocks[2] = STRING::URLDecode(blocks[2]); |
| 403 |
|
| 404 |
insPos = blocks[2].find("{SSM}"); |
| 405 |
if (insPos != std::string::npos) |
| 406 |
{ |
| 407 |
std::string::size_type sidPos(blocks[2].find("{SID}")); |
| 408 |
std::string::size_type kidPos(blocks[2].find("{KID}")); |
| 409 |
std::string::size_type psshPos(blocks[2].find("{PSSH}")); |
| 410 |
|
| 411 |
char fullDecode = 0; |
| 412 |
if (insPos > 1 && sidPos > 1 && kidPos > 1 && (blocks[2][0] == 'b' || blocks[2][0] == 'B') && |
| 413 |
blocks[2][1] == '{') |
| 414 |
{ |
| 415 |
fullDecode = blocks[2][0]; |
| 416 |
blocks[2] = blocks[2].substr(2, blocks[2].size() - 3); |
| 417 |
insPos -= 2; |
| 418 |
if (kidPos != std::string::npos) |
| 419 |
kidPos -= 2; |
| 420 |
if (sidPos != std::string::npos) |
| 421 |
sidPos -= 2; |
| 422 |
if (psshPos != std::string::npos) |
| 423 |
psshPos -= 2; |
| 424 |
} |
| 425 |
|
| 426 |
size_t sizeWritten(0); |
| 427 |
|
| 428 |
if (insPos > 0) |
| 429 |
{ |
| 430 |
if (blocks[2][insPos - 1] == 'B' || blocks[2][insPos - 1] == 'b') |
| 431 |
{ |
| 432 |
std::string msgEncoded = BASE64::Encode(keyRequestData); |
| 433 |
if (blocks[2][insPos - 1] == 'B') |
| 434 |
{ |
| 435 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 436 |
} |
| 437 |
blocks[2].replace(insPos - 1, 6, msgEncoded); |
| 438 |
sizeWritten = msgEncoded.size(); |
| 439 |
} |
| 440 |
else if (blocks[2][insPos - 1] == 'D') |
| 441 |
{ |
| 442 |
std::string msgEncoded{STRING::ToDecimal( |
| 443 |
reinterpret_cast<const uint8_t*>(keyRequestData.data()), keyRequestData.size())}; |
| 444 |
blocks[2].replace(insPos - 1, 6, msgEncoded); |
| 445 |
sizeWritten = msgEncoded.size(); |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
blocks[2].replace(insPos - 1, 6, keyRequestData.data(), keyRequestData.size()); |
| 450 |
sizeWritten = keyRequestData.size(); |
| 451 |
} |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
LOG::Log(LOGERROR, "Unsupported License request template (body / ?{SSM})"); |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
if (sidPos != std::string::npos && insPos < sidPos) |
| 460 |
sidPos += sizeWritten, sidPos -= 6; |
| 461 |
|
| 462 |
if (kidPos != std::string::npos && insPos < kidPos) |
| 463 |
kidPos += sizeWritten, kidPos -= 6; |
| 464 |
|
| 465 |
if (psshPos != std::string::npos && insPos < psshPos) |
| 466 |
psshPos += sizeWritten, psshPos -= 6; |
| 467 |
|
| 468 |
sizeWritten = 0; |
| 469 |
|
| 470 |
if (sidPos != std::string::npos) |
| 471 |
{ |
| 472 |
if (sidPos > 0) |
| 473 |
{ |
| 474 |
if (blocks[2][sidPos - 1] == 'B' || blocks[2][sidPos - 1] == 'b') |
| 475 |
{ |
| 476 |
std::string msgEncoded = BASE64::Encode(m_sessionId); |
| 477 |
if (blocks[2][sidPos - 1] == 'B') |
| 478 |
{ |
| 479 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 480 |
} |
| 481 |
blocks[2].replace(sidPos - 1, 6, msgEncoded); |
| 482 |
sizeWritten = msgEncoded.size(); |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
blocks[2].replace(sidPos - 1, 6, m_sessionId.data(), m_sessionId.size()); |
| 487 |
sizeWritten = m_sessionId.size(); |
| 488 |
} |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
LOG::Log(LOGERROR, "Unsupported License request template (body / ?{SID})"); |
| 493 |
return false; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if (kidPos != std::string::npos && sidPos < kidPos) |
| 498 |
kidPos += sizeWritten, kidPos -= 6; |
| 499 |
|
| 500 |
if (psshPos != std::string::npos && sidPos < psshPos) |
| 501 |
psshPos += sizeWritten, psshPos -= 6; |
| 502 |
|
| 503 |
size_t kidPlaceholderLen = 6; |
| 504 |
if (kidPos != std::string::npos) |
| 505 |
{ |
| 506 |
if (blocks[2][kidPos - 1] == 'H') |
| 507 |
{ |
| 508 |
std::string keyIdUUID{STRING::ToHexadecimal(m_defaultKeyId)}; |
| 509 |
blocks[2].replace(kidPos - 1, 6, keyIdUUID.c_str(), 32); |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
std::string kidUUID{DRM::ConvertKidBytesToUUID(m_defaultKeyId)}; |
| 514 |
blocks[2].replace(kidPos, 5, kidUUID.c_str(), 36); |
| 515 |
kidPlaceholderLen = 5; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
if (psshPos != std::string::npos && kidPos < psshPos) |
| 520 |
psshPos += sizeWritten, psshPos -= kidPlaceholderLen; |
| 521 |
|
| 522 |
if (psshPos != std::string::npos) |
| 523 |
{ |
| 524 |
std::string msgEncoded = BASE64::Encode(m_initialPssh); |
| 525 |
if (blocks[2][psshPos - 1] == 'B') |
| 526 |
{ |
| 527 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 528 |
} |
| 529 |
blocks[2].replace(psshPos - 1, 7, msgEncoded); |
| 530 |
sizeWritten = msgEncoded.size(); |
| 531 |
} |
| 532 |
|
| 533 |
if (fullDecode) |
| 534 |
{ |
| 535 |
std::string msgEncoded = BASE64::Encode(blocks[2]); |
| 536 |
if (fullDecode == 'B') |
| 537 |
{ |
| 538 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 539 |
} |
| 540 |
blocks[2] = msgEncoded; |
| 541 |
} |
| 542 |
|
| 543 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 544 |
{ |
| 545 |
std::string debugFilePath = FILESYS::PathCombine( |
| 546 |
m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.postdata"); |
| 547 |
UTILS::FILESYS::SaveFile(debugFilePath, blocks[2], true); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
std::string encData{BASE64::Encode(blocks[2])}; |
| 552 |
file.AddHeader("postdata", encData.c_str()); |
| 553 |
} |
| 554 |
|
| 555 |
int statusCode = file.Open(); |
| 556 |
if (statusCode == -1 || statusCode >= 400) |
| 557 |
{ |
| 558 |
LOG::Log(LOGERROR, "License server returned failure (HTTP error %i)", statusCode); |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
CURL::ReadStatus downloadStatus = CURL::ReadStatus::CHUNK_READ; |
| 563 |
while (downloadStatus == CURL::ReadStatus::CHUNK_READ) |
| 564 |
{ |
| 565 |
downloadStatus = file.Read(response); |
| 566 |
} |
| 567 |
|
| 568 |
resLimit = file.GetResponseHeader("X-Limit-Video"); |
| 569 |
contentType = file.GetResponseHeader("Content-Type"); |
| 570 |
|
| 571 |
if (!resLimit.empty()) |
| 572 |
{ |
| 573 |
std::string::size_type posMax = resLimit.find("max="); |
| 574 |
if (posMax != std::string::npos) |
| 575 |
m_resolutionLimit = std::atoi(resLimit.data() + (posMax + 4)); |
| 576 |
} |
| 577 |
|
| 578 |
if (downloadStatus == CURL::ReadStatus::ERROR) |
| 579 |
{ |
| 580 |
LOG::LogF(LOGERROR, "Could not read full SessionMessage response"); |
| 581 |
return false; |
| 582 |
} |
| 583 |
else if (response.empty()) |
| 584 |
{ |
| 585 |
LOG::LogF(LOGERROR, "Empty SessionMessage response - invalid"); |
| 586 |
return false; |
| 587 |
} |
| 588 |
|
| 589 |
if (m_mediaDrm.GetKeySystemType() == PLAYREADY && |
| 590 |
response.find("<LicenseNonce>") == std::string::npos) |
| 591 |
{ |
| 592 |
std::string::size_type dstPos(response.find("</Licenses>")); |
| 593 |
std::string challenge(keyRequestData.data(), keyRequestData.size()); |
| 594 |
std::string::size_type srcPosS(challenge.find("<LicenseNonce>")); |
| 595 |
if (dstPos != std::string::npos && srcPosS != std::string::npos) |
| 596 |
{ |
| 597 |
LOG::Log(LOGDEBUG, "Inserting <LicenseNonce>"); |
| 598 |
std::string::size_type srcPosE(challenge.find("</LicenseNonce>", srcPosS)); |
| 599 |
if (srcPosE != std::string::npos) |
| 600 |
response.insert(dstPos + 11, challenge.c_str() + srcPosS, srcPosE - srcPosS + 15); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 605 |
{ |
| 606 |
std::string debugFilePath = FILESYS::PathCombine( |
| 607 |
m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.response"); |
| 608 |
UTILS::FILESYS::SaveFile(debugFilePath, response, true); |
| 609 |
} |
| 610 |
|
| 611 |
if (!blocks[3].empty() && blocks[3][0] != 'R' && |
| 612 |
(keyRequestData.size() > 2 || |
| 613 |
contentType.find("application/octet-stream") == std::string::npos)) |
| 614 |
{ |
| 615 |
if (blocks[3][0] == 'J' || (blocks[3].size() > 1 && blocks[3][0] == 'B' && blocks[3][1] == 'J')) |
| 616 |
{ |
| 617 |
int dataPos = 2; |
| 618 |
|
| 619 |
if (response.size() >= 3 && blocks[3][0] == 'B') |
| 620 |
{ |
| 621 |
response = BASE64::DecodeToStr(response); |
| 622 |
dataPos = 3; |
| 623 |
} |
| 624 |
|
| 625 |
jsmn_parser jsn; |
| 626 |
jsmntok_t tokens[256]; |
| 627 |
|
| 628 |
jsmn_init(&jsn); |
| 629 |
int i(0), numTokens = jsmn_parse(&jsn, response.c_str(), response.size(), tokens, 256); |
| 630 |
|
| 631 |
std::vector<std::string> jsonVals{StringUtils::Split(blocks[3].substr(dataPos), ';')}; |
| 632 |
|
| 633 |
// Find HDCP limit |
| 634 |
if (jsonVals.size() > 1) |
| 635 |
{ |
| 636 |
for (; i < numTokens; ++i) |
| 637 |
if (tokens[i].type == JSMN_STRING && tokens[i].size == 1 && |
| 638 |
jsonVals[1].size() == static_cast<unsigned int>(tokens[i].end - tokens[i].start) && |
| 639 |
strncmp(response.c_str() + tokens[i].start, jsonVals[1].c_str(), |
| 640 |
tokens[i].end - tokens[i].start) == 0) |
| 641 |
break; |
| 642 |
if (i < numTokens) |
| 643 |
m_hdcpLimit = atoi((response.c_str() + tokens[i + 1].start)); |
| 644 |
} |
| 645 |
// Find license key |
| 646 |
if (jsonVals.size() > 0) |
| 647 |
{ |
| 648 |
for (i = 0; i < numTokens; ++i) |
| 649 |
if (tokens[i].type == JSMN_STRING && tokens[i].size == 1 && |
| 650 |
jsonVals[0].size() == static_cast<unsigned int>(tokens[i].end - tokens[i].start) && |
| 651 |
strncmp(response.c_str() + tokens[i].start, jsonVals[0].c_str(), |
| 652 |
tokens[i].end - tokens[i].start) == 0) |
| 653 |
{ |
| 654 |
if (i + 1 < numTokens && tokens[i + 1].type == JSMN_ARRAY && tokens[i + 1].size == 1) |
| 655 |
++i; |
| 656 |
break; |
| 657 |
} |
| 658 |
} |
| 659 |
else |
| 660 |
i = numTokens; |
| 661 |
|
| 662 |
if (i < numTokens) |
| 663 |
{ |
| 664 |
response = response.substr(tokens[i + 1].start, tokens[i + 1].end - tokens[i + 1].start); |
| 665 |
|
| 666 |
if (blocks[3][dataPos - 1] == 'B') |
| 667 |
{ |
| 668 |
response = BASE64::DecodeToStr(response); |
| 669 |
} |
| 670 |
} |
| 671 |
else |
| 672 |
{ |
| 673 |
LOG::LogF(LOGERROR, "Unable to find %s in JSON string", blocks[3].c_str() + 2); |
| 674 |
return false; |
| 675 |
} |
| 676 |
} |
| 677 |
else if (blocks[3][0] == 'H' && blocks[3].size() >= 2) |
| 678 |
{ |
| 679 |
//Find the payload |
| 680 |
std::string::size_type payloadPos = response.find("\r\n\r\n"); |
| 681 |
if (payloadPos != std::string::npos) |
| 682 |
{ |
| 683 |
payloadPos += 4; |
| 684 |
if (blocks[3][1] == 'B') |
| 685 |
response = std::string(response.c_str() + payloadPos, response.c_str() + response.size()); |
| 686 |
else |
| 687 |
{ |
| 688 |
LOG::LogF(LOGERROR, "Unsupported HTTP payload data type definition"); |
| 689 |
return false; |
| 690 |
} |
| 691 |
} |
| 692 |
else |
| 693 |
{ |
| 694 |
LOG::LogF(LOGERROR, "Unable to find HTTP payload in response"); |
| 695 |
return false; |
| 696 |
} |
| 697 |
} |
| 698 |
else if (blocks[3][0] == 'B' && blocks[3].size() == 1) |
| 699 |
{ |
| 700 |
response = BASE64::DecodeToStr(response); |
| 701 |
} |
| 702 |
else |
| 703 |
{ |
| 704 |
LOG::LogF(LOGERROR, "Unsupported License request template (response)"); |
| 705 |
return false; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
m_keySetId = m_mediaDrm.GetMediaDrm()->provideKeyResponse( |
| 710 |
m_sessionId, std::vector<char>(response.data(), response.data() + response.size())); |
| 711 |
if (xbmc_jnienv()->ExceptionCheck()) |
| 712 |
{ |
| 713 |
LOG::LogF(LOGERROR, "provideKeyResponse has raised an exception"); |
| 714 |
xbmc_jnienv()->ExceptionClear(); |
| 715 |
return false; |
| 716 |
} |
| 717 |
|
| 718 |
if (keyRequestData.size() == 2) |
| 719 |
m_mediaDrm.SaveServiceCertificate(); |
| 720 |
|
| 721 |
LOG::Log(LOGDEBUG, "License update successful"); |
| 722 |
return true; |
| 723 |
} |
| 724 |
|
| 725 |
AP4_Result CWVCencSingleSampleDecrypterA::SetFragmentInfo(AP4_UI32 poolId, |
| 726 |
const std::vector<uint8_t>& keyId, |
| 727 |
const AP4_UI08 nalLengthSize, |
| 728 |
AP4_DataBuffer& annexbSpsPps, |
| 729 |
AP4_UI32 flags, |
| 730 |
CryptoInfo cryptoInfo) |
| 731 |
{ |
| 732 |
if (poolId >= m_fragmentPool.size()) |
| 733 |
return AP4_ERROR_OUT_OF_RANGE; |
| 734 |
|
| 735 |
m_fragmentPool[poolId].m_key = keyId; |
| 736 |
m_fragmentPool[poolId].m_nalLengthSize = nalLengthSize; |
| 737 |
m_fragmentPool[poolId].m_annexbSpsPps.SetData(annexbSpsPps.GetData(), annexbSpsPps.GetDataSize()); |
| 738 |
m_fragmentPool[poolId].m_decrypterFlags = flags; |
| 739 |
|
| 740 |
if (m_isKeyUpdateRequested) |
| 741 |
KeyUpdateRequest(false, false); |
| 742 |
|
| 743 |
return AP4_SUCCESS; |
| 744 |
} |
| 745 |
|
| 746 |
AP4_UI32 CWVCencSingleSampleDecrypterA::AddPool() |
| 747 |
{ |
| 748 |
for (size_t i(0); i < m_fragmentPool.size(); ++i) |
| 749 |
if (m_fragmentPool[i].m_nalLengthSize == 99) |
| 750 |
{ |
| 751 |
m_fragmentPool[i].m_nalLengthSize = 0; |
| 752 |
return i; |
| 753 |
} |
| 754 |
m_fragmentPool.push_back(FINFO()); |
| 755 |
m_fragmentPool.back().m_nalLengthSize = 0; |
| 756 |
return static_cast<AP4_UI32>(m_fragmentPool.size() - 1); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
void CWVCencSingleSampleDecrypterA::RemovePool(AP4_UI32 poolId) |
| 761 |
{ |
| 762 |
m_fragmentPool[poolId].m_nalLengthSize = 99; |
| 763 |
m_fragmentPool[poolId].m_key.clear(); |
| 764 |
} |
| 765 |
|
| 766 |
AP4_Result CWVCencSingleSampleDecrypterA::DecryptSampleData(AP4_UI32 poolId, |
| 767 |
AP4_DataBuffer& dataIn, |
| 768 |
AP4_DataBuffer& dataOut, |
| 769 |
const AP4_UI08* iv, |
| 770 |
unsigned int subsampleCount, |
| 771 |
const AP4_UI16* bytesOfCleartextData, |
| 772 |
const AP4_UI32* bytesOfEncryptedData) |
| 773 |
{ |
| 774 |
if (!m_mediaDrm.GetMediaDrm()) |
| 775 |
return AP4_ERROR_INVALID_STATE; |
| 776 |
|
| 777 |
if (dataIn.GetDataSize() > 0) |
| 778 |
{ |
| 779 |
FINFO& fragInfo(m_fragmentPool[poolId]); |
| 780 |
|
| 781 |
if (fragInfo.m_nalLengthSize > 4) |
| 782 |
{ |
| 783 |
LOG::LogF(LOGERROR, "Nalu length size > 4 not supported"); |
| 784 |
return AP4_ERROR_NOT_SUPPORTED; |
| 785 |
} |
| 786 |
|
| 787 |
AP4_UI16 dummyClear = 0; |
| 788 |
AP4_UI32 dummyCipher(dataIn.GetDataSize()); |
| 789 |
|
| 790 |
if (iv) |
| 791 |
{ |
| 792 |
if (!subsampleCount) |
| 793 |
{ |
| 794 |
subsampleCount = 1; |
| 795 |
bytesOfCleartextData = &dummyClear; |
| 796 |
bytesOfEncryptedData = &dummyCipher; |
| 797 |
} |
| 798 |
|
| 799 |
dataOut.SetData(reinterpret_cast<const AP4_Byte*>(&subsampleCount), sizeof(subsampleCount)); |
| 800 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(bytesOfCleartextData), |
| 801 |
subsampleCount * sizeof(AP4_UI16)); |
| 802 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(bytesOfEncryptedData), |
| 803 |
subsampleCount * sizeof(AP4_UI32)); |
| 804 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(iv), 16); |
| 805 |
dataOut.AppendData(fragInfo.m_key.data(), static_cast<AP4_Size>(fragInfo.m_key.size())); |
| 806 |
} |
| 807 |
else |
| 808 |
{ |
| 809 |
dataOut.SetDataSize(0); |
| 810 |
bytesOfCleartextData = &dummyClear; |
| 811 |
bytesOfEncryptedData = &dummyCipher; |
| 812 |
} |
| 813 |
|
| 814 |
if (fragInfo.m_nalLengthSize && (!iv || bytesOfCleartextData[0] > 0)) |
| 815 |
{ |
| 816 |
//check NAL / subsample |
| 817 |
const AP4_Byte* packetIn(dataIn.GetData()); |
| 818 |
const AP4_Byte* packetInEnd(dataIn.GetData() + dataIn.GetDataSize()); |
| 819 |
// Byte position of "bytesOfCleartextData" where to set the size of the data, |
| 820 |
// by default starts after the subsample count data (so the size of subsampleCount data type) |
| 821 |
size_t clrDataBytePos = sizeof(subsampleCount); |
| 822 |
// size_t nalUnitCount = 0; //! @todo: what is the point of this? |
| 823 |
size_t nalUnitSum = 0; |
| 824 |
// size_t configSize = 0; //! @todo:what is the point of this? |
| 825 |
|
| 826 |
while (packetIn < packetInEnd) |
| 827 |
{ |
| 828 |
uint32_t nalsize = 0; |
| 829 |
for (size_t i = 0; i < fragInfo.m_nalLengthSize; ++i) |
| 830 |
{ |
| 831 |
nalsize = (nalsize << 8) + *packetIn++; |
| 832 |
}; |
| 833 |
|
| 834 |
//look if we have to inject sps / pps |
| 835 |
if (fragInfo.m_annexbSpsPps.GetDataSize() && (*packetIn & 0x1F) != 9 /*AVC_NAL_AUD*/) |
| 836 |
{ |
| 837 |
dataOut.AppendData(fragInfo.m_annexbSpsPps.GetData(), |
| 838 |
fragInfo.m_annexbSpsPps.GetDataSize()); |
| 839 |
if (iv) |
| 840 |
{ |
| 841 |
// Update the byte containing the data size of current subsample referred to clear bytes array |
| 842 |
AP4_UI16* clrb_out = reinterpret_cast<AP4_UI16*>(dataOut.UseData() + clrDataBytePos); |
| 843 |
*clrb_out += fragInfo.m_annexbSpsPps.GetDataSize(); |
| 844 |
} |
| 845 |
// configSize = fragInfo.m_annexbSpsPps.GetDataSize(); |
| 846 |
fragInfo.m_annexbSpsPps.SetDataSize(0); |
| 847 |
} |
| 848 |
|
| 849 |
//Annex-B Start pos |
| 850 |
static AP4_Byte annexbStartCode[4] = {0x00, 0x00, 0x00, 0x01}; |
| 851 |
dataOut.AppendData(annexbStartCode, 4); |
| 852 |
dataOut.AppendData(packetIn, nalsize); |
| 853 |
packetIn += nalsize; |
| 854 |
|
| 855 |
if (iv) |
| 856 |
{ |
| 857 |
// Update the byte containing the data size of current subsample referred to clear bytes array |
| 858 |
AP4_UI16* clrb_out = reinterpret_cast<AP4_UI16*>(dataOut.UseData() + clrDataBytePos); |
| 859 |
*clrb_out += (4 - fragInfo.m_nalLengthSize); |
| 860 |
} |
| 861 |
|
| 862 |
// ++nalUnitCount; |
| 863 |
|
| 864 |
if (!iv) |
| 865 |
{ |
| 866 |
nalUnitSum = 0; |
| 867 |
} |
| 868 |
else if (nalsize + fragInfo.m_nalLengthSize + nalUnitSum >= |
| 869 |
*bytesOfCleartextData + *bytesOfEncryptedData) |
| 870 |
{ |
| 871 |
AP4_UI32 summedBytes = 0; |
| 872 |
do |
| 873 |
{ |
| 874 |
summedBytes += *bytesOfCleartextData + *bytesOfEncryptedData; |
| 875 |
++bytesOfCleartextData; |
| 876 |
++bytesOfEncryptedData; |
| 877 |
clrDataBytePos += sizeof(AP4_UI16); // Move to the next clear data subsample byte position |
| 878 |
--subsampleCount; |
| 879 |
} while (subsampleCount && nalsize + fragInfo.m_nalLengthSize + nalUnitSum > summedBytes); |
| 880 |
|
| 881 |
if (nalsize + fragInfo.m_nalLengthSize + nalUnitSum > summedBytes) |
| 882 |
{ |
| 883 |
LOG::LogF(LOGERROR, "NAL Unit exceeds subsample definition (nls: %u) %u -> %u ", |
| 884 |
static_cast<unsigned int>(fragInfo.m_nalLengthSize), |
| 885 |
static_cast<unsigned int>(nalsize + fragInfo.m_nalLengthSize + nalUnitSum), |
| 886 |
summedBytes); |
| 887 |
return AP4_ERROR_NOT_SUPPORTED; |
| 888 |
} |
| 889 |
nalUnitSum = 0; |
| 890 |
} |
| 891 |
else |
| 892 |
nalUnitSum += nalsize + fragInfo.m_nalLengthSize; |
| 893 |
} |
| 894 |
if (packetIn != packetInEnd || subsampleCount) |
| 895 |
{ |
| 896 |
LOG::LogF(LOGERROR, "NAL Unit definition incomplete (nls: %d) %d -> %u ", |
| 897 |
fragInfo.m_nalLengthSize, (int)(packetInEnd - packetIn), subsampleCount); |
| 898 |
return AP4_ERROR_NOT_SUPPORTED; |
| 899 |
} |
| 900 |
} |
| 901 |
else |
| 902 |
{ |
| 903 |
dataOut.AppendData(dataIn.GetData(), dataIn.GetDataSize()); |
| 904 |
fragInfo.m_annexbSpsPps.SetDataSize(0); |
| 905 |
} |
| 906 |
} |
| 907 |
else |
| 908 |
dataOut.SetDataSize(0); |
| 909 |
return AP4_SUCCESS; |
| 910 |
} |