| 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 "CdmDecryptedBlock.h" |
| 14 |
#include "CdmFixedBuffer.h" |
| 15 |
#include "CdmTypeConversion.h" |
| 16 |
#include "WVCdmAdapter.h" |
| 17 |
#include "WVDecrypter.h" |
| 18 |
#include "cdm/media/cdm/cdm_adapter.h" |
| 19 |
#include "jsmn.h" |
| 20 |
#include "decrypters/Helpers.h" |
| 21 |
#include "utils/Base64Utils.h" |
| 22 |
#include "utils/CurlUtils.h" |
| 23 |
#include "utils/DigestMD5Utils.h" |
| 24 |
#include "utils/FileUtils.h" |
| 25 |
#include "utils/StringUtils.h" |
| 26 |
#include "utils/Utils.h" |
| 27 |
#include "utils/log.h" |
| 28 |
|
| 29 |
#include <mutex> |
| 30 |
#include <thread> |
| 31 |
|
| 32 |
using namespace UTILS; |
| 33 |
|
| 34 |
void CWVCencSingleSampleDecrypter::SetSession(const char* session, |
| 35 |
uint32_t sessionSize, |
| 36 |
const uint8_t* data, |
| 37 |
size_t dataSize) |
| 38 |
{ |
| 39 |
std::lock_guard<std::mutex> lock(m_renewalLock); |
| 40 |
|
| 41 |
m_strSession = std::string(session, sessionSize); |
| 42 |
m_challenge.SetData(data, dataSize); |
| 43 |
LOG::LogF(LOGDEBUG, "Opened widevine session ID: %s", m_strSession.c_str()); |
| 44 |
} |
| 45 |
|
| 46 |
CWVCencSingleSampleDecrypter::CWVCencSingleSampleDecrypter(CWVCdmAdapter& drm, |
| 47 |
std::vector<uint8_t>& pssh, |
| 48 |
const std::vector<uint8_t>& defaultKeyId, |
| 49 |
bool skipSessionMessage, |
| 50 |
CryptoMode cryptoMode, |
| 51 |
CWVDecrypter* host) |
| 52 |
: m_wvCdmAdapter(drm), |
| 53 |
m_pssh(pssh), |
| 54 |
m_hdcpVersion(99), |
| 55 |
m_hdcpLimit(0), |
| 56 |
m_resolutionLimit(0), |
| 57 |
m_promiseId(1), |
| 58 |
m_isDrained(true), |
| 59 |
m_defaultKeyId(defaultKeyId), |
| 60 |
m_EncryptionMode(cryptoMode), |
| 61 |
m_host(host) |
| 62 |
{ |
| 63 |
SetParentIsOwner(false); |
| 64 |
|
| 65 |
if (pssh.size() < 4 || pssh.size() > 4096) |
| 66 |
{ |
| 67 |
LOG::LogF(LOGERROR, "PSSH init data with length %zu seems not to be cenc init data", |
| 68 |
pssh.size()); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
m_wvCdmAdapter.insertssd(this); |
| 73 |
|
| 74 |
// No cenc init data with PSSH box format, create one |
| 75 |
if (memcmp(pssh.data() + 4, "pssh", 4) != 0) |
| 76 |
{ |
| 77 |
// This will request a new session and initializes session_id and message members in cdm_adapter. |
| 78 |
// message will be used to create a license request in the step after CreateSession call. |
| 79 |
// Initialization data is the widevine cdm pssh code in google proto style found in mpd schemeIdUri |
| 80 |
|
| 81 |
// PSSH box version 0 (no kid's) |
| 82 |
static const uint8_t atomHeader[12] = {0x00, 0x00, 0x00, 0x00, 0x70, 0x73, |
| 83 |
0x73, 0x68, 0x00, 0x00, 0x00, 0x00}; |
| 84 |
|
| 85 |
static const uint8_t widevineSystemId[16] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6, 0x4a, 0xce, |
| 86 |
0xa3, 0xc8, 0x27, 0xdc, 0xd5, 0x1d, 0x21, 0xed}; |
| 87 |
|
| 88 |
std::vector<uint8_t> psshAtom; |
| 89 |
psshAtom.assign(atomHeader, atomHeader + 12); // PSSH Box header |
| 90 |
psshAtom.insert(psshAtom.end(), widevineSystemId, widevineSystemId + 16); // System ID |
| 91 |
// Add data size bytes |
| 92 |
psshAtom.resize(30, 0); // 2 zero bytes |
| 93 |
psshAtom.emplace_back(static_cast<uint8_t>((pssh.size()) >> 8)); |
| 94 |
psshAtom.emplace_back(static_cast<uint8_t>(pssh.size())); |
| 95 |
|
| 96 |
psshAtom.insert(psshAtom.end(), pssh.begin(), pssh.end()); // Data |
| 97 |
// Update box size |
| 98 |
psshAtom[2] = static_cast<uint8_t>(psshAtom.size() >> 8); |
| 99 |
psshAtom[3] = static_cast<uint8_t>(psshAtom.size()); |
| 100 |
m_pssh = psshAtom; |
| 101 |
} |
| 102 |
|
| 103 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 104 |
{ |
| 105 |
std::string debugFilePath = |
| 106 |
FILESYS::PathCombine(m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.init"); |
| 107 |
|
| 108 |
std::string data{reinterpret_cast<const char*>(m_pssh.data()), m_pssh.size()}; |
| 109 |
UTILS::FILESYS::SaveFile(debugFilePath, data, true); |
| 110 |
} |
| 111 |
|
| 112 |
drm.GetCdmAdapter()->CreateSessionAndGenerateRequest(m_promiseId++, cdm::SessionType::kTemporary, |
| 113 |
cdm::InitDataType::kCenc, m_pssh.data(), |
| 114 |
static_cast<uint32_t>(m_pssh.size())); |
| 115 |
|
| 116 |
int retrycount = 0; |
| 117 |
while (m_strSession.empty() && ++retrycount < 100) |
| 118 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 119 |
|
| 120 |
if (m_strSession.empty()) |
| 121 |
{ |
| 122 |
LOG::LogF(LOGERROR, "Cannot perform License update, no session available"); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if (skipSessionMessage) |
| 127 |
return; |
| 128 |
|
| 129 |
while (m_challenge.GetDataSize() > 0 && SendSessionMessage()) |
| 130 |
; |
| 131 |
} |
| 132 |
|
| 133 |
CWVCencSingleSampleDecrypter::~CWVCencSingleSampleDecrypter() |
| 134 |
{ |
| 135 |
m_wvCdmAdapter.removessd(this); |
| 136 |
} |
| 137 |
|
| 138 |
void CWVCencSingleSampleDecrypter::GetCapabilities(const std::vector<uint8_t>& keyId, |
| 139 |
uint32_t media, |
| 140 |
DecrypterCapabilites& caps) |
| 141 |
{ |
| 142 |
caps = {0, m_hdcpVersion, m_hdcpLimit}; |
| 143 |
|
| 144 |
if (m_strSession.empty()) |
| 145 |
{ |
| 146 |
LOG::LogF(LOGDEBUG, "Session empty"); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
caps.flags = DecrypterCapabilites::SSD_SUPPORTS_DECODING; |
| 151 |
|
| 152 |
if (m_keys.empty()) |
| 153 |
{ |
| 154 |
LOG::LogF(LOGDEBUG, "Keys empty"); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
if (!caps.hdcpLimit) |
| 159 |
caps.hdcpLimit = m_resolutionLimit; |
| 160 |
|
| 161 |
/*if (media == DecrypterCapabilites::SSD_MEDIA_VIDEO) |
| 162 |
caps.flags |= (DecrypterCapabilites::SSD_SECURE_PATH | DecrypterCapabilites::SSD_ANNEXB_REQUIRED); |
| 163 |
caps.flags |= DecrypterCapabilites::SSD_SINGLE_DECRYPT; |
| 164 |
return;*/ |
| 165 |
|
| 166 |
//caps.flags |= (DecrypterCapabilites::SSD_SECURE_PATH | DecrypterCapabilites::SSD_ANNEXB_REQUIRED); |
| 167 |
//return; |
| 168 |
|
| 169 |
/*for (auto k : m_keys) |
| 170 |
if (!key || memcmp(k.m_keyId.data(), key, 16) == 0) |
| 171 |
{ |
| 172 |
if (k.status != 0) |
| 173 |
{ |
| 174 |
if (media == DecrypterCapabilites::SSD_MEDIA_VIDEO) |
| 175 |
caps.flags |= (DecrypterCapabilites::SSD_SECURE_PATH | DecrypterCapabilites::SSD_ANNEXB_REQUIRED); |
| 176 |
else |
| 177 |
caps.flags = DecrypterCapabilites::SSD_INVALID; |
| 178 |
} |
| 179 |
break; |
| 180 |
} |
| 181 |
*/ |
| 182 |
if ((caps.flags & DecrypterCapabilites::SSD_SUPPORTS_DECODING) != 0) |
| 183 |
{ |
| 184 |
AP4_UI32 poolId(AddPool()); |
| 185 |
m_fragmentPool[poolId].m_key = keyId.empty() ? m_keys.front().m_keyId : keyId; |
| 186 |
m_fragmentPool[poolId].m_cryptoInfo.m_mode = m_EncryptionMode; |
| 187 |
|
| 188 |
AP4_DataBuffer in; |
| 189 |
AP4_DataBuffer out; |
| 190 |
AP4_UI32 encryptedBytes[2] = {1, 1}; |
| 191 |
AP4_UI16 clearBytes[2] = {5, 5}; |
| 192 |
AP4_Byte testData[12] = {0, 0, 0, 1, 9, 255, 0, 0, 0, 1, 10, 255}; |
| 193 |
const AP4_UI08 iv[] = {1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 194 |
in.SetBuffer(testData, 12); |
| 195 |
in.SetDataSize(12); |
| 196 |
try |
| 197 |
{ |
| 198 |
encryptedBytes[0] = 12; |
| 199 |
clearBytes[0] = 0; |
| 200 |
if (DecryptSampleData(poolId, in, out, iv, 1, clearBytes, encryptedBytes) != AP4_SUCCESS) |
| 201 |
{ |
| 202 |
LOG::LogF(LOGDEBUG, "Single decrypt failed, secure path only"); |
| 203 |
if (media == DecrypterCapabilites::SSD_MEDIA_VIDEO) |
| 204 |
caps.flags |= (DecrypterCapabilites::SSD_SECURE_PATH | |
| 205 |
DecrypterCapabilites::SSD_ANNEXB_REQUIRED); |
| 206 |
else |
| 207 |
caps.flags = DecrypterCapabilites::SSD_INVALID; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
LOG::LogF(LOGDEBUG, "Single decrypt possible"); |
| 212 |
caps.flags |= DecrypterCapabilites::SSD_SINGLE_DECRYPT; |
| 213 |
caps.hdcpVersion = 99; |
| 214 |
caps.hdcpLimit = m_resolutionLimit; |
| 215 |
} |
| 216 |
} |
| 217 |
catch (const std::exception& e) |
| 218 |
{ |
| 219 |
LOG::LogF(LOGDEBUG, "Decrypt error, assuming secure path: %s", e.what()); |
| 220 |
caps.flags |= (DecrypterCapabilites::SSD_SECURE_PATH | |
| 221 |
DecrypterCapabilites::SSD_ANNEXB_REQUIRED); |
| 222 |
} |
| 223 |
RemovePool(poolId); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
LOG::LogF(LOGDEBUG, "Decoding not supported"); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
const char* CWVCencSingleSampleDecrypter::GetSessionId() |
| 232 |
{ |
| 233 |
return m_strSession.empty() ? nullptr : m_strSession.c_str(); |
| 234 |
} |
| 235 |
|
| 236 |
void CWVCencSingleSampleDecrypter::CloseSessionId() |
| 237 |
{ |
| 238 |
if (!m_strSession.empty()) |
| 239 |
{ |
| 240 |
LOG::LogF(LOGDEBUG, "Closing widevine session ID: %s", m_strSession.c_str()); |
| 241 |
m_wvCdmAdapter.GetCdmAdapter()->CloseSession(++m_promiseId, m_strSession.data(), |
| 242 |
m_strSession.size()); |
| 243 |
|
| 244 |
LOG::LogF(LOGDEBUG, "Widevine session ID %s closed", m_strSession.c_str()); |
| 245 |
m_strSession.clear(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
AP4_DataBuffer CWVCencSingleSampleDecrypter::GetChallengeData() |
| 250 |
{ |
| 251 |
return m_challenge; |
| 252 |
} |
| 253 |
|
| 254 |
void CWVCencSingleSampleDecrypter::CheckLicenseRenewal() |
| 255 |
{ |
| 256 |
{ |
| 257 |
std::lock_guard<std::mutex> lock(m_renewalLock); |
| 258 |
if (!m_challenge.GetDataSize()) |
| 259 |
return; |
| 260 |
} |
| 261 |
SendSessionMessage(); |
| 262 |
} |
| 263 |
|
| 264 |
bool CWVCencSingleSampleDecrypter::SendSessionMessage() |
| 265 |
{ |
| 266 |
std::vector<std::string> blocks{StringUtils::Split(m_wvCdmAdapter.GetLicenseURL(), '|')}; |
| 267 |
|
| 268 |
if (blocks.size() != 4) |
| 269 |
{ |
| 270 |
LOG::LogF(LOGERROR, "Wrong \"|\" blocks in license URL. Four blocks (req | header | body | " |
| 271 |
"response) are expected in license URL"); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 276 |
{ |
| 277 |
std::string debugFilePath = FILESYS::PathCombine( |
| 278 |
m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.challenge"); |
| 279 |
std::string data{reinterpret_cast<const char*>(m_challenge.GetData()), |
| 280 |
m_challenge.GetDataSize()}; |
| 281 |
UTILS::FILESYS::SaveFile(debugFilePath, data, true); |
| 282 |
} |
| 283 |
|
| 284 |
//Process placeholder in GET String |
| 285 |
std::string::size_type insPos(blocks[0].find("{SSM}")); |
| 286 |
if (insPos != std::string::npos) |
| 287 |
{ |
| 288 |
if (insPos > 0 && blocks[0][insPos - 1] == 'B') |
| 289 |
{ |
| 290 |
std::string msgEncoded{BASE64::Encode(m_challenge.GetData(), m_challenge.GetDataSize())}; |
| 291 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 292 |
blocks[0].replace(insPos - 1, 6, msgEncoded); |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
LOG::Log(LOGERROR, "Unsupported License request template (command)"); |
| 297 |
return false; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
insPos = blocks[0].find("{HASH}"); |
| 302 |
if (insPos != std::string::npos) |
| 303 |
{ |
| 304 |
DIGEST::MD5 md5; |
| 305 |
md5.Update(m_challenge.GetData(), m_challenge.GetDataSize()); |
| 306 |
md5.Finalize(); |
| 307 |
blocks[0].replace(insPos, 6, md5.HexDigest()); |
| 308 |
} |
| 309 |
|
| 310 |
CURL::CUrl file{blocks[0].c_str()}; |
| 311 |
file.AddHeader("Expect", ""); |
| 312 |
|
| 313 |
std::string response; |
| 314 |
std::string resLimit; |
| 315 |
std::string contentType; |
| 316 |
char buf[2048]; |
| 317 |
bool serverCertRequest; |
| 318 |
|
| 319 |
//Process headers |
| 320 |
std::vector<std::string> headers{StringUtils::Split(blocks[1], '&')}; |
| 321 |
for (std::string& headerStr : headers) |
| 322 |
{ |
| 323 |
std::vector<std::string> header{StringUtils::Split(headerStr, '=')}; |
| 324 |
if (!header.empty()) |
| 325 |
{ |
| 326 |
StringUtils::Trim(header[0]); |
| 327 |
std::string value; |
| 328 |
if (header.size() > 1) |
| 329 |
{ |
| 330 |
StringUtils::Trim(header[1]); |
| 331 |
value = STRING::URLDecode(header[1]); |
| 332 |
} |
| 333 |
file.AddHeader(header[0].c_str(), value.c_str()); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
//Process body |
| 338 |
if (!blocks[2].empty()) |
| 339 |
{ |
| 340 |
if (blocks[2][0] == '%') |
| 341 |
blocks[2] = STRING::URLDecode(blocks[2]); |
| 342 |
|
| 343 |
insPos = blocks[2].find("{SSM}"); |
| 344 |
if (insPos != std::string::npos) |
| 345 |
{ |
| 346 |
std::string::size_type sidPos(blocks[2].find("{SID}")); |
| 347 |
std::string::size_type kidPos(blocks[2].find("{KID}")); |
| 348 |
|
| 349 |
char fullDecode = 0; |
| 350 |
if (insPos > 1 && sidPos > 1 && kidPos > 1 && (blocks[2][0] == 'b' || blocks[2][0] == 'B') && |
| 351 |
blocks[2][1] == '{') |
| 352 |
{ |
| 353 |
fullDecode = blocks[2][0]; |
| 354 |
blocks[2] = blocks[2].substr(2, blocks[2].size() - 3); |
| 355 |
insPos -= 2; |
| 356 |
if (kidPos != std::string::npos) |
| 357 |
kidPos -= 2; |
| 358 |
if (sidPos != std::string::npos) |
| 359 |
sidPos -= 2; |
| 360 |
} |
| 361 |
|
| 362 |
size_t size_written(0); |
| 363 |
|
| 364 |
if (insPos > 0) |
| 365 |
{ |
| 366 |
if (blocks[2][insPos - 1] == 'B' || blocks[2][insPos - 1] == 'b') |
| 367 |
{ |
| 368 |
std::string msgEncoded{BASE64::Encode(m_challenge.GetData(), m_challenge.GetDataSize())}; |
| 369 |
if (blocks[2][insPos - 1] == 'B') |
| 370 |
{ |
| 371 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 372 |
} |
| 373 |
blocks[2].replace(insPos - 1, 6, msgEncoded); |
| 374 |
size_written = msgEncoded.size(); |
| 375 |
} |
| 376 |
else if (blocks[2][insPos - 1] == 'D') |
| 377 |
{ |
| 378 |
std::string msgEncoded{ |
| 379 |
STRING::ToDecimal(m_challenge.GetData(), m_challenge.GetDataSize())}; |
| 380 |
blocks[2].replace(insPos - 1, 6, msgEncoded); |
| 381 |
size_written = msgEncoded.size(); |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
blocks[2].replace(insPos - 1, 6, reinterpret_cast<const char*>(m_challenge.GetData()), |
| 386 |
m_challenge.GetDataSize()); |
| 387 |
size_written = m_challenge.GetDataSize(); |
| 388 |
} |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
LOG::Log(LOGERROR, "Unsupported License request template (body / ?{SSM})"); |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
if (sidPos != std::string::npos && insPos < sidPos) |
| 397 |
sidPos += size_written, sidPos -= 6; |
| 398 |
|
| 399 |
if (kidPos != std::string::npos && insPos < kidPos) |
| 400 |
kidPos += size_written, kidPos -= 6; |
| 401 |
|
| 402 |
size_written = 0; |
| 403 |
|
| 404 |
if (sidPos != std::string::npos) |
| 405 |
{ |
| 406 |
if (sidPos > 0) |
| 407 |
{ |
| 408 |
if (blocks[2][sidPos - 1] == 'B' || blocks[2][sidPos - 1] == 'b') |
| 409 |
{ |
| 410 |
std::string msgEncoded{BASE64::Encode(m_strSession)}; |
| 411 |
|
| 412 |
if (blocks[2][sidPos - 1] == 'B') |
| 413 |
{ |
| 414 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 415 |
} |
| 416 |
|
| 417 |
blocks[2].replace(sidPos - 1, 6, msgEncoded); |
| 418 |
size_written = msgEncoded.size(); |
| 419 |
} |
| 420 |
else |
| 421 |
{ |
| 422 |
blocks[2].replace(sidPos - 1, 6, m_strSession.data(), m_strSession.size()); |
| 423 |
size_written = m_strSession.size(); |
| 424 |
} |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
LOG::LogF(LOGERROR, "Unsupported License request template (body / ?{SID})"); |
| 429 |
return false; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if (kidPos != std::string::npos) |
| 434 |
{ |
| 435 |
if (sidPos < kidPos) |
| 436 |
kidPos += size_written, kidPos -= 6; |
| 437 |
|
| 438 |
if (blocks[2][kidPos - 1] == 'H') |
| 439 |
{ |
| 440 |
std::string keyIdUUID{STRING::ToHexadecimal(m_defaultKeyId)}; |
| 441 |
blocks[2].replace(kidPos - 1, 6, keyIdUUID.c_str(), 32); |
| 442 |
} |
| 443 |
else |
| 444 |
{ |
| 445 |
std::string kidUUID{DRM::ConvertKidBytesToUUID(m_defaultKeyId)}; |
| 446 |
blocks[2].replace(kidPos, 5, kidUUID.c_str(), 36); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if (fullDecode) |
| 451 |
{ |
| 452 |
std::string msgEncoded{BASE64::Encode(blocks[2])}; |
| 453 |
if (fullDecode == 'B') |
| 454 |
{ |
| 455 |
msgEncoded = STRING::URLEncode(msgEncoded); |
| 456 |
} |
| 457 |
blocks[2] = msgEncoded; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
std::string encData{BASE64::Encode(blocks[2])}; |
| 462 |
//! @todo: inappropriate use of "postdata" header, use CURL::CUrl for post request |
| 463 |
file.AddHeader("postdata", encData.c_str()); |
| 464 |
} |
| 465 |
|
| 466 |
serverCertRequest = m_challenge.GetDataSize() == 2; |
| 467 |
m_challenge.SetDataSize(0); |
| 468 |
|
| 469 |
int statusCode = file.Open(); |
| 470 |
if (statusCode == -1 || statusCode >= 400) |
| 471 |
{ |
| 472 |
LOG::Log(LOGERROR, "License server returned failure (HTTP error %i)", statusCode); |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
CURL::ReadStatus downloadStatus = CURL::ReadStatus::CHUNK_READ; |
| 477 |
while (downloadStatus == CURL::ReadStatus::CHUNK_READ) |
| 478 |
{ |
| 479 |
downloadStatus = file.Read(response); |
| 480 |
} |
| 481 |
|
| 482 |
resLimit = file.GetResponseHeader("X-Limit-Video"); |
| 483 |
contentType = file.GetResponseHeader("Content-Type"); |
| 484 |
|
| 485 |
if (!resLimit.empty()) |
| 486 |
{ |
| 487 |
std::string::size_type posMax = resLimit.find("max="); // log/check this |
| 488 |
if (posMax != std::string::npos) |
| 489 |
m_resolutionLimit = std::atoi(resLimit.data() + (posMax + 4)); |
| 490 |
} |
| 491 |
|
| 492 |
if (downloadStatus == CURL::ReadStatus::ERROR) |
| 493 |
{ |
| 494 |
LOG::LogF(LOGERROR, "Could not read full SessionMessage response"); |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
if (CSrvBroker::GetSettings().IsDebugLicense()) |
| 499 |
{ |
| 500 |
std::string debugFilePath = FILESYS::PathCombine( |
| 501 |
m_host->GetLibraryPath(), "EDEF8BA9-79D6-4ACE-A3C8-27DCD51D21ED.response"); |
| 502 |
FILESYS::SaveFile(debugFilePath, response, true); |
| 503 |
} |
| 504 |
|
| 505 |
if (serverCertRequest && contentType.find("application/octet-stream") == std::string::npos) |
| 506 |
serverCertRequest = false; |
| 507 |
|
| 508 |
if (!blocks[3].empty() && blocks[3][0] != 'R' && !serverCertRequest) |
| 509 |
{ |
| 510 |
if (blocks[3][0] == 'J' || (blocks[3].size() > 1 && blocks[3][0] == 'B' && blocks[3][1] == 'J')) |
| 511 |
{ |
| 512 |
int dataPos = 2; |
| 513 |
|
| 514 |
if (response.size() >= 3 && blocks[3][0] == 'B') |
| 515 |
{ |
| 516 |
response = BASE64::DecodeToStr(response); |
| 517 |
dataPos = 3; |
| 518 |
} |
| 519 |
|
| 520 |
jsmn_parser jsn; |
| 521 |
jsmntok_t tokens[256]; |
| 522 |
|
| 523 |
jsmn_init(&jsn); |
| 524 |
int i(0), numTokens = jsmn_parse(&jsn, response.c_str(), response.size(), tokens, 256); |
| 525 |
|
| 526 |
std::vector<std::string> jsonVals{StringUtils::Split(blocks[3].substr(dataPos), ';')}; |
| 527 |
|
| 528 |
// Find HDCP limit |
| 529 |
if (jsonVals.size() > 1) |
| 530 |
{ |
| 531 |
for (; i < numTokens; ++i) |
| 532 |
if (tokens[i].type == JSMN_STRING && tokens[i].size == 1 && |
| 533 |
jsonVals[1].size() == static_cast<unsigned int>(tokens[i].end - tokens[i].start) && |
| 534 |
strncmp(response.c_str() + tokens[i].start, jsonVals[1].c_str(), |
| 535 |
tokens[i].end - tokens[i].start) == 0) |
| 536 |
break; |
| 537 |
if (i < numTokens) |
| 538 |
m_hdcpLimit = std::atoi((response.c_str() + tokens[i + 1].start)); |
| 539 |
} |
| 540 |
// Find license key |
| 541 |
if (jsonVals.size() > 0) |
| 542 |
{ |
| 543 |
for (i = 0; i < numTokens; ++i) |
| 544 |
if (tokens[i].type == JSMN_STRING && tokens[i].size == 1 && |
| 545 |
jsonVals[0].size() == static_cast<unsigned int>(tokens[i].end - tokens[i].start) && |
| 546 |
strncmp(response.c_str() + tokens[i].start, jsonVals[0].c_str(), |
| 547 |
tokens[i].end - tokens[i].start) == 0) |
| 548 |
{ |
| 549 |
if (i + 1 < numTokens && tokens[i + 1].type == JSMN_ARRAY && tokens[i + 1].size == 1) |
| 550 |
++i; |
| 551 |
break; |
| 552 |
} |
| 553 |
} |
| 554 |
else |
| 555 |
i = numTokens; |
| 556 |
|
| 557 |
if (i < numTokens) |
| 558 |
{ |
| 559 |
std::string respData{ |
| 560 |
response.substr(tokens[i + 1].start, tokens[i + 1].end - tokens[i + 1].start)}; |
| 561 |
|
| 562 |
if (blocks[3][dataPos - 1] == 'B') |
| 563 |
{ |
| 564 |
respData = BASE64::DecodeToStr(respData); |
| 565 |
} |
| 566 |
|
| 567 |
m_wvCdmAdapter.GetCdmAdapter()->UpdateSession( |
| 568 |
++m_promiseId, m_strSession.data(), m_strSession.size(), |
| 569 |
reinterpret_cast<const uint8_t*>(respData.c_str()), respData.size()); |
| 570 |
} |
| 571 |
else |
| 572 |
{ |
| 573 |
LOG::LogF(LOGERROR, "Unable to find %s in JSON string", blocks[3].c_str() + 2); |
| 574 |
return false; |
| 575 |
} |
| 576 |
} |
| 577 |
else if (blocks[3][0] == 'H' && blocks[3].size() >= 2) |
| 578 |
{ |
| 579 |
//Find the payload |
| 580 |
std::string::size_type payloadPos = response.find("\r\n\r\n"); |
| 581 |
if (payloadPos != std::string::npos) |
| 582 |
{ |
| 583 |
payloadPos += 4; |
| 584 |
if (blocks[3][1] == 'B') |
| 585 |
m_wvCdmAdapter.GetCdmAdapter()->UpdateSession( |
| 586 |
++m_promiseId, m_strSession.data(), m_strSession.size(), |
| 587 |
reinterpret_cast<const uint8_t*>(response.c_str() + payloadPos), |
| 588 |
response.size() - payloadPos); |
| 589 |
else |
| 590 |
{ |
| 591 |
LOG::LogF(LOGERROR, "Unsupported HTTP payload data type definition"); |
| 592 |
return false; |
| 593 |
} |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
LOG::LogF(LOGERROR, "Unable to find HTTP payload in response"); |
| 598 |
return false; |
| 599 |
} |
| 600 |
} |
| 601 |
else if (blocks[3][0] == 'B' && blocks[3].size() == 1) |
| 602 |
{ |
| 603 |
std::string decRespData{BASE64::DecodeToStr(response)}; |
| 604 |
|
| 605 |
m_wvCdmAdapter.GetCdmAdapter()->UpdateSession( |
| 606 |
++m_promiseId, m_strSession.data(), m_strSession.size(), |
| 607 |
reinterpret_cast<const uint8_t*>(decRespData.c_str()), decRespData.size()); |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
LOG::LogF(LOGERROR, "Unsupported License request template (response)"); |
| 612 |
return false; |
| 613 |
} |
| 614 |
} |
| 615 |
else // its binary - simply push the returned data as update |
| 616 |
{ |
| 617 |
m_wvCdmAdapter.GetCdmAdapter()->UpdateSession( |
| 618 |
++m_promiseId, m_strSession.data(), m_strSession.size(), |
| 619 |
reinterpret_cast<const uint8_t*>(response.data()), response.size()); |
| 620 |
} |
| 621 |
|
| 622 |
if (m_keys.empty()) |
| 623 |
{ |
| 624 |
LOG::LogF(LOGERROR, "License update not successful (no keys)"); |
| 625 |
CloseSessionId(); |
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
LOG::Log(LOGDEBUG, "License update successful"); |
| 630 |
return true; |
| 631 |
} |
| 632 |
|
| 633 |
void CWVCencSingleSampleDecrypter::AddSessionKey(const uint8_t* data, |
| 634 |
size_t dataSize, |
| 635 |
uint32_t status) |
| 636 |
{ |
| 637 |
WVSKEY key; |
| 638 |
key.m_keyId.assign(data, data + dataSize); |
| 639 |
|
| 640 |
std::vector<WVSKEY>::iterator res; |
| 641 |
if ((res = std::find(m_keys.begin(), m_keys.end(), key)) == m_keys.end()) |
| 642 |
res = m_keys.insert(res, key); |
| 643 |
res->status = static_cast<cdm::KeyStatus>(status); |
| 644 |
} |
| 645 |
|
| 646 |
bool CWVCencSingleSampleDecrypter::HasKeyId(const std::vector<uint8_t>& keyid) |
| 647 |
{ |
| 648 |
if (!keyid.empty()) |
| 649 |
{ |
| 650 |
for (const WVSKEY& key : m_keys) |
| 651 |
{ |
| 652 |
if (key.m_keyId == keyid) |
| 653 |
return true; |
| 654 |
} |
| 655 |
} |
| 656 |
return false; |
| 657 |
} |
| 658 |
|
| 659 |
AP4_Result CWVCencSingleSampleDecrypter::SetFragmentInfo(AP4_UI32 poolId, |
| 660 |
const std::vector<uint8_t>& keyId, |
| 661 |
const AP4_UI08 nalLengthSize, |
| 662 |
AP4_DataBuffer& annexbSpsPps, |
| 663 |
AP4_UI32 flags, |
| 664 |
CryptoInfo cryptoInfo) |
| 665 |
{ |
| 666 |
if (poolId >= m_fragmentPool.size()) |
| 667 |
return AP4_ERROR_OUT_OF_RANGE; |
| 668 |
|
| 669 |
m_fragmentPool[poolId].m_key = keyId; |
| 670 |
m_fragmentPool[poolId].m_nalLengthSize = nalLengthSize; |
| 671 |
m_fragmentPool[poolId].m_annexbSpsPps.SetData(annexbSpsPps.GetData(), annexbSpsPps.GetDataSize()); |
| 672 |
m_fragmentPool[poolId].m_decrypterFlags = flags; |
| 673 |
m_fragmentPool[poolId].m_cryptoInfo = cryptoInfo; |
| 674 |
|
| 675 |
return AP4_SUCCESS; |
| 676 |
} |
| 677 |
|
| 678 |
AP4_UI32 CWVCencSingleSampleDecrypter::AddPool() |
| 679 |
{ |
| 680 |
for (size_t i(0); i < m_fragmentPool.size(); ++i) |
| 681 |
if (m_fragmentPool[i].m_nalLengthSize == 99) |
| 682 |
{ |
| 683 |
m_fragmentPool[i].m_nalLengthSize = 0; |
| 684 |
return i; |
| 685 |
} |
| 686 |
m_fragmentPool.push_back(FINFO()); |
| 687 |
m_fragmentPool.back().m_nalLengthSize = 0; |
| 688 |
return static_cast<AP4_UI32>(m_fragmentPool.size() - 1); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
void CWVCencSingleSampleDecrypter::RemovePool(AP4_UI32 poolId) |
| 693 |
{ |
| 694 |
m_fragmentPool[poolId].m_nalLengthSize = 99; |
| 695 |
m_fragmentPool[poolId].m_key.clear(); |
| 696 |
} |
| 697 |
|
| 698 |
void CWVCencSingleSampleDecrypter::LogDecryptError(const cdm::Status status, |
| 699 |
const std::vector<uint8_t>& keyId) |
| 700 |
{ |
| 701 |
LOG::LogF(LOGDEBUG, "Decrypt failed with error code: %d and KID: %s", status, |
| 702 |
STRING::ToHexadecimal(keyId).c_str()); |
| 703 |
} |
| 704 |
|
| 705 |
void CWVCencSingleSampleDecrypter::SetCdmSubsamples(std::vector<cdm::SubsampleEntry>& subsamples, |
| 706 |
bool isCbc) |
| 707 |
{ |
| 708 |
if (isCbc) |
| 709 |
{ |
| 710 |
subsamples.resize(1); |
| 711 |
subsamples[0] = {0, m_decryptIn.GetDataSize()}; |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
subsamples.push_back({0, m_decryptIn.GetDataSize()}); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
void CWVCencSingleSampleDecrypter::RepackSubsampleData(AP4_DataBuffer& dataIn, |
| 720 |
AP4_DataBuffer& dataOut, |
| 721 |
size_t& pos, |
| 722 |
size_t& cipherPos, |
| 723 |
const unsigned int subsamplePos, |
| 724 |
const AP4_UI16* bytesOfCleartextData, |
| 725 |
const AP4_UI32* bytesOfEncryptedData) |
| 726 |
{ |
| 727 |
dataOut.AppendData(dataIn.GetData() + pos, bytesOfCleartextData[subsamplePos]); |
| 728 |
pos += bytesOfCleartextData[subsamplePos]; |
| 729 |
dataOut.AppendData(m_decryptOut.GetData() + cipherPos, bytesOfEncryptedData[subsamplePos]); |
| 730 |
pos += bytesOfEncryptedData[subsamplePos]; |
| 731 |
cipherPos += bytesOfEncryptedData[subsamplePos]; |
| 732 |
} |
| 733 |
|
| 734 |
void CWVCencSingleSampleDecrypter::UnpackSubsampleData(AP4_DataBuffer& dataIn, |
| 735 |
size_t& pos, |
| 736 |
const unsigned int subsamplePos, |
| 737 |
const AP4_UI16* bytesOfCleartextData, |
| 738 |
const AP4_UI32* bytesOfEncryptedData) |
| 739 |
{ |
| 740 |
pos += bytesOfCleartextData[subsamplePos]; |
| 741 |
m_decryptIn.AppendData(dataIn.GetData() + pos, bytesOfEncryptedData[subsamplePos]); |
| 742 |
pos += bytesOfEncryptedData[subsamplePos]; |
| 743 |
} |
| 744 |
|
| 745 |
void CWVCencSingleSampleDecrypter::SetInput(cdm::InputBuffer_2& cdmInputBuffer, |
| 746 |
const AP4_DataBuffer& inputData, |
| 747 |
const unsigned int subsampleCount, |
| 748 |
const uint8_t* iv, |
| 749 |
const FINFO& fragInfo, |
| 750 |
const std::vector<cdm::SubsampleEntry>& subsamples) |
| 751 |
{ |
| 752 |
cdmInputBuffer.data = inputData.GetData(); |
| 753 |
cdmInputBuffer.data_size = inputData.GetDataSize(); |
| 754 |
cdmInputBuffer.num_subsamples = subsampleCount; |
| 755 |
cdmInputBuffer.iv = iv; |
| 756 |
cdmInputBuffer.iv_size = 16; //Always 16, see AP4_CencSingleSampleDecrypter declaration. |
| 757 |
cdmInputBuffer.key_id = fragInfo.m_key.data(); |
| 758 |
cdmInputBuffer.key_id_size = static_cast<uint32_t>(fragInfo.m_key.size()); |
| 759 |
cdmInputBuffer.subsamples = subsamples.data(); |
| 760 |
cdmInputBuffer.encryption_scheme = media::ToCdmEncryptionScheme(fragInfo.m_cryptoInfo.m_mode); |
| 761 |
cdmInputBuffer.timestamp = 0; |
| 762 |
cdmInputBuffer.pattern = {fragInfo.m_cryptoInfo.m_cryptBlocks, |
| 763 |
fragInfo.m_cryptoInfo.m_skipBlocks}; |
| 764 |
} |
| 765 |
|
| 766 |
/*---------------------------------------------------------------------- |
| 767 |
| CWVCencSingleSampleDecrypter::DecryptSampleData |
| 768 |
+---------------------------------------------------------------------*/ |
| 769 |
AP4_Result CWVCencSingleSampleDecrypter::DecryptSampleData(AP4_UI32 poolId, |
| 770 |
AP4_DataBuffer& dataIn, |
| 771 |
AP4_DataBuffer& dataOut, |
| 772 |
const AP4_UI08* iv, |
| 773 |
unsigned int subsampleCount, |
| 774 |
const AP4_UI16* bytesOfCleartextData, |
| 775 |
const AP4_UI32* bytesOfEncryptedData) |
| 776 |
{ |
| 777 |
if (!m_wvCdmAdapter.GetCdmAdapter()) |
| 778 |
{ |
| 779 |
dataOut.SetData(dataIn.GetData(), dataIn.GetDataSize()); |
| 780 |
return AP4_SUCCESS; |
| 781 |
} |
| 782 |
|
| 783 |
FINFO& fragInfo(m_fragmentPool[poolId]); |
| 784 |
|
| 785 |
if (fragInfo.m_decrypterFlags & |
| 786 |
DecrypterCapabilites::SSD_SECURE_PATH) //we can not decrypt only |
| 787 |
{ |
| 788 |
if (fragInfo.m_nalLengthSize > 4) |
| 789 |
{ |
| 790 |
LOG::LogF(LOGERROR, "Nalu length size > 4 not supported"); |
| 791 |
return AP4_ERROR_NOT_SUPPORTED; |
| 792 |
} |
| 793 |
|
| 794 |
AP4_UI16 dummyClear(0); |
| 795 |
AP4_UI32 dummyCipher(dataIn.GetDataSize()); |
| 796 |
|
| 797 |
if (iv) |
| 798 |
{ |
| 799 |
if (!subsampleCount) |
| 800 |
{ |
| 801 |
subsampleCount = 1; |
| 802 |
bytesOfCleartextData = &dummyClear; |
| 803 |
bytesOfEncryptedData = &dummyCipher; |
| 804 |
} |
| 805 |
|
| 806 |
dataOut.SetData(reinterpret_cast<const AP4_Byte*>(&subsampleCount), sizeof(subsampleCount)); |
| 807 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(bytesOfCleartextData), |
| 808 |
subsampleCount * sizeof(AP4_UI16)); |
| 809 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(bytesOfEncryptedData), |
| 810 |
subsampleCount * sizeof(AP4_UI32)); |
| 811 |
dataOut.AppendData(reinterpret_cast<const AP4_Byte*>(iv), 16); |
| 812 |
dataOut.AppendData(fragInfo.m_key.data(), static_cast<AP4_Size>(fragInfo.m_key.size())); |
| 813 |
} |
| 814 |
else |
| 815 |
{ |
| 816 |
dataOut.SetDataSize(0); |
| 817 |
bytesOfCleartextData = &dummyClear; |
| 818 |
bytesOfEncryptedData = &dummyCipher; |
| 819 |
} |
| 820 |
|
| 821 |
if (fragInfo.m_nalLengthSize && (!iv || bytesOfCleartextData[0] > 0)) |
| 822 |
{ |
| 823 |
//check NAL / subsample |
| 824 |
const AP4_Byte *packetIn(dataIn.GetData()), |
| 825 |
*packetInEnd(dataIn.GetData() + dataIn.GetDataSize()); |
| 826 |
// Byte position of "bytesOfCleartextData" where to set the size of the data, |
| 827 |
// by default starts after the subsample count data (so the size of subsampleCount data type) |
| 828 |
size_t clrDataBytePos = sizeof(subsampleCount); |
| 829 |
// size_t nalUnitCount = 0; // is there a use for this? |
| 830 |
size_t nalUnitSum = 0; |
| 831 |
// size_t configSize = 0; // is there a use for this? |
| 832 |
|
| 833 |
while (packetIn < packetInEnd) |
| 834 |
{ |
| 835 |
uint32_t nalSize(0); |
| 836 |
for (size_t i = 0; i < fragInfo.m_nalLengthSize; ++i) |
| 837 |
{ |
| 838 |
nalSize = (nalSize << 8) + *packetIn++; |
| 839 |
}; |
| 840 |
|
| 841 |
//look if we have to inject sps / pps |
| 842 |
if (fragInfo.m_annexbSpsPps.GetDataSize() && (*packetIn & 0x1F) != 9 /*AVC_NAL_AUD*/) |
| 843 |
{ |
| 844 |
dataOut.AppendData(fragInfo.m_annexbSpsPps.GetData(), |
| 845 |
fragInfo.m_annexbSpsPps.GetDataSize()); |
| 846 |
if (iv) |
| 847 |
{ |
| 848 |
// Update the byte containing the data size of current subsample referred to clear bytes array |
| 849 |
AP4_UI16* clrb_out = reinterpret_cast<AP4_UI16*>(dataOut.UseData() + clrDataBytePos); |
| 850 |
*clrb_out += fragInfo.m_annexbSpsPps.GetDataSize(); |
| 851 |
} |
| 852 |
|
| 853 |
// configSize = fragInfo.m_annexbSpsPps.GetDataSize(); |
| 854 |
fragInfo.m_annexbSpsPps.SetDataSize(0); |
| 855 |
} |
| 856 |
// Annex-B Start pos |
| 857 |
static AP4_Byte annexbStartCode[4] = {0x00, 0x00, 0x00, 0x01}; |
| 858 |
dataOut.AppendData(annexbStartCode, 4); |
| 859 |
dataOut.AppendData(packetIn, nalSize); |
| 860 |
packetIn += nalSize; |
| 861 |
|
| 862 |
if (iv) |
| 863 |
{ |
| 864 |
// Update the byte containing the data size of current subsample referred to clear bytes array |
| 865 |
AP4_UI16* clrb_out = reinterpret_cast<AP4_UI16*>(dataOut.UseData() + clrDataBytePos); |
| 866 |
*clrb_out += (4 - fragInfo.m_nalLengthSize); |
| 867 |
} |
| 868 |
|
| 869 |
// ++nalUnitCount; |
| 870 |
|
| 871 |
if (!iv) |
| 872 |
{ |
| 873 |
nalUnitSum = 0; |
| 874 |
} |
| 875 |
else if (nalSize + fragInfo.m_nalLengthSize + nalUnitSum >= |
| 876 |
*bytesOfCleartextData + *bytesOfEncryptedData) |
| 877 |
{ |
| 878 |
AP4_UI32 summedBytes(0); |
| 879 |
do |
| 880 |
{ |
| 881 |
summedBytes += *bytesOfCleartextData + *bytesOfEncryptedData; |
| 882 |
++bytesOfCleartextData; |
| 883 |
++bytesOfEncryptedData; |
| 884 |
clrDataBytePos += sizeof(AP4_UI16); // Move to the next clear data subsample byte position |
| 885 |
--subsampleCount; |
| 886 |
} while (subsampleCount && nalSize + fragInfo.m_nalLengthSize + nalUnitSum > summedBytes); |
| 887 |
|
| 888 |
if (nalSize + fragInfo.m_nalLengthSize + nalUnitSum > summedBytes) |
| 889 |
{ |
| 890 |
LOG::LogF(LOGERROR, "NAL Unit exceeds subsample definition (nls: %u) %u -> %u ", |
| 891 |
static_cast<unsigned int>(fragInfo.m_nalLengthSize), |
| 892 |
static_cast<unsigned int>(nalSize + fragInfo.m_nalLengthSize + nalUnitSum), |
| 893 |
summedBytes); |
| 894 |
return AP4_ERROR_NOT_SUPPORTED; |
| 895 |
} |
| 896 |
nalUnitSum = 0; |
| 897 |
} |
| 898 |
else |
| 899 |
nalUnitSum += nalSize + fragInfo.m_nalLengthSize; |
| 900 |
} |
| 901 |
if (packetIn != packetInEnd || subsampleCount) |
| 902 |
{ |
| 903 |
LOG::Log(LOGERROR, "NAL Unit definition incomplete (nls: %u) %u -> %u ", |
| 904 |
static_cast<unsigned int>(fragInfo.m_nalLengthSize), |
| 905 |
static_cast<unsigned int>(packetInEnd - packetIn), subsampleCount); |
| 906 |
return AP4_ERROR_NOT_SUPPORTED; |
| 907 |
} |
| 908 |
} |
| 909 |
else |
| 910 |
dataOut.AppendData(dataIn.GetData(), dataIn.GetDataSize()); |
| 911 |
return AP4_SUCCESS; |
| 912 |
} |
| 913 |
|
| 914 |
if (fragInfo.m_key.empty()) |
| 915 |
{ |
| 916 |
LOG::LogF(LOGDEBUG, "No Key"); |
| 917 |
return AP4_ERROR_INVALID_PARAMETERS; |
| 918 |
} |
| 919 |
|
| 920 |
dataOut.SetDataSize(0); |
| 921 |
|
| 922 |
uint16_t clearBytes = 0; |
| 923 |
uint32_t encryptedBytes = dataIn.GetDataSize(); |
| 924 |
|
| 925 |
// check input parameters |
| 926 |
if (iv == NULL) |
| 927 |
return AP4_ERROR_INVALID_PARAMETERS; |
| 928 |
if (subsampleCount) |
| 929 |
{ |
| 930 |
if (bytesOfCleartextData == NULL || bytesOfEncryptedData == NULL) |
| 931 |
{ |
| 932 |
LOG::LogF(LOGDEBUG, "Invalid input params"); |
| 933 |
return AP4_ERROR_INVALID_PARAMETERS; |
| 934 |
} |
| 935 |
} |
| 936 |
else |
| 937 |
{ |
| 938 |
subsampleCount = 1; |
| 939 |
bytesOfCleartextData = &clearBytes; |
| 940 |
bytesOfEncryptedData = &encryptedBytes; |
| 941 |
} |
| 942 |
cdm::Status ret{cdm::Status::kSuccess}; |
| 943 |
std::vector<cdm::SubsampleEntry> subsamples; |
| 944 |
subsamples.reserve(subsampleCount); |
| 945 |
|
| 946 |
bool useCbcDecrypt = fragInfo.m_cryptoInfo.m_mode == CryptoMode::AES_CBC; |
| 947 |
|
| 948 |
// We can only decrypt with subsamples set to 1 |
| 949 |
// This must be handled differently for CENC and CBCS |
| 950 |
// CENC: |
| 951 |
// CDM should get 1 block of encrypted data per sample, encrypted data |
| 952 |
// from all subsamples should be formed into a contiguous block. |
| 953 |
// Even if there is only 1 subsample, we should remove cleartext data |
| 954 |
// from it before passing to CDM. |
| 955 |
// CBCS: |
| 956 |
// Due to the nature of this cipher subsamples must be decrypted separately |
| 957 |
|
| 958 |
const size_t iterations = useCbcDecrypt ? subsampleCount : 1; |
| 959 |
size_t absPos = 0; |
| 960 |
|
| 961 |
for (size_t i = 0; i < iterations; ++i) |
| 962 |
{ |
| 963 |
m_decryptIn.Reserve(dataIn.GetDataSize()); |
| 964 |
m_decryptIn.SetDataSize(0); |
| 965 |
size_t decryptInPos = absPos; |
| 966 |
if (useCbcDecrypt) |
| 967 |
{ |
| 968 |
UnpackSubsampleData(dataIn, decryptInPos, i, bytesOfCleartextData, bytesOfEncryptedData); |
| 969 |
} |
| 970 |
else |
| 971 |
{ |
| 972 |
for (size_t subsamplePos = 0; subsamplePos < subsampleCount; ++subsamplePos) |
| 973 |
{ |
| 974 |
UnpackSubsampleData(dataIn, absPos, subsamplePos, bytesOfCleartextData, |
| 975 |
bytesOfEncryptedData); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
if (m_decryptIn.GetDataSize() > 0) // remember to include when calling setcdmsubsamples |
| 980 |
{ |
| 981 |
SetCdmSubsamples(subsamples, useCbcDecrypt); |
| 982 |
} |
| 983 |
|
| 984 |
else // we have nothing to decrypt in this iteration |
| 985 |
{ |
| 986 |
if (useCbcDecrypt) |
| 987 |
{ |
| 988 |
dataOut.AppendData(dataIn.GetData() + absPos, bytesOfCleartextData[i]); |
| 989 |
absPos += bytesOfCleartextData[i]; |
| 990 |
continue; |
| 991 |
} |
| 992 |
else // we can exit here for CENC and just return the input buffer |
| 993 |
{ |
| 994 |
dataOut.AppendData(dataIn.GetData(), dataIn.GetDataSize()); |
| 995 |
return AP4_SUCCESS; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
cdm::InputBuffer_2 cdmIn; |
| 1000 |
SetInput(cdmIn, m_decryptIn, 1, iv, fragInfo, subsamples); |
| 1001 |
m_decryptOut.SetDataSize(m_decryptIn.GetDataSize()); |
| 1002 |
CdmBuffer buf = &m_decryptOut; |
| 1003 |
CdmDecryptedBlock cdmOut; |
| 1004 |
cdmOut.SetDecryptedBuffer(&buf); |
| 1005 |
|
| 1006 |
CheckLicenseRenewal(); |
| 1007 |
ret = m_wvCdmAdapter.GetCdmAdapter()->Decrypt(cdmIn, &cdmOut); |
| 1008 |
|
| 1009 |
if (ret == cdm::Status::kSuccess) |
| 1010 |
{ |
| 1011 |
size_t cipherPos = 0; |
| 1012 |
if (useCbcDecrypt) |
| 1013 |
{ |
| 1014 |
RepackSubsampleData(dataIn, dataOut, absPos, cipherPos, i, bytesOfCleartextData, |
| 1015 |
bytesOfEncryptedData); |
| 1016 |
} |
| 1017 |
else |
| 1018 |
{ |
| 1019 |
size_t absPos = 0; |
| 1020 |
for (unsigned int i{0}; i < subsampleCount; ++i) |
| 1021 |
{ |
| 1022 |
RepackSubsampleData(dataIn, dataOut, absPos, cipherPos, i, bytesOfCleartextData, |
| 1023 |
bytesOfEncryptedData); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
LogDecryptError(ret, fragInfo.m_key); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
return (ret == cdm::Status::kSuccess) ? AP4_SUCCESS : AP4_ERROR_INVALID_PARAMETERS; |
| 1033 |
} |
| 1034 |
|
| 1035 |
bool CWVCencSingleSampleDecrypter::OpenVideoDecoder(const VIDEOCODEC_INITDATA* initData) |
| 1036 |
{ |
| 1037 |
cdm::VideoDecoderConfig_3 vconfig = media::ToCdmVideoDecoderConfig(initData, m_EncryptionMode); |
| 1038 |
|
| 1039 |
// InputStream interface call OpenVideoDecoder also during playback when stream quality |
| 1040 |
// change, so we reinitialize the decoder only when the codec change |
| 1041 |
if (m_currentVideoDecConfig.has_value()) |
| 1042 |
{ |
| 1043 |
cdm::VideoDecoderConfig_3& currVidConfig = *m_currentVideoDecConfig; |
| 1044 |
if (currVidConfig.codec == vconfig.codec && currVidConfig.profile == vconfig.profile) |
| 1045 |
return true; |
| 1046 |
|
| 1047 |
m_wvCdmAdapter.GetCdmAdapter()->DeinitializeDecoder(cdm::StreamType::kStreamTypeVideo); |
| 1048 |
} |
| 1049 |
|
| 1050 |
m_currentVideoDecConfig = vconfig; |
| 1051 |
|
| 1052 |
cdm::Status ret = m_wvCdmAdapter.GetCdmAdapter()->InitializeVideoDecoder(vconfig); |
| 1053 |
m_videoFrames.clear(); |
| 1054 |
m_isDrained = true; |
| 1055 |
|
| 1056 |
LOG::LogF(LOGDEBUG, "Initialization returned status: %s", media::CdmStatusToString(ret).c_str()); |
| 1057 |
return ret == cdm::Status::kSuccess; |
| 1058 |
} |
| 1059 |
|
| 1060 |
VIDEOCODEC_RETVAL CWVCencSingleSampleDecrypter::DecryptAndDecodeVideo( |
| 1061 |
kodi::addon::CInstanceVideoCodec* codecInstance, const DEMUX_PACKET* sample) |
| 1062 |
{ |
| 1063 |
// if we have an picture waiting, or not yet get the dest buffer, do nothing |
| 1064 |
if (m_videoFrames.size() == 4) |
| 1065 |
return VC_ERROR; |
| 1066 |
|
| 1067 |
if (sample->cryptoInfo && sample->cryptoInfo->numSubSamples > 0 && |
| 1068 |
(!sample->cryptoInfo->clearBytes || !sample->cryptoInfo->cipherBytes)) |
| 1069 |
{ |
| 1070 |
return VC_ERROR; |
| 1071 |
} |
| 1072 |
|
| 1073 |
cdm::InputBuffer_2 inputBuffer{}; |
| 1074 |
std::vector<cdm::SubsampleEntry> subsamples; |
| 1075 |
|
| 1076 |
media::ToCdmInputBuffer(sample, &subsamples, &inputBuffer); |
| 1077 |
|
| 1078 |
if (sample->iSize > 0) |
| 1079 |
m_isDrained = false; |
| 1080 |
|
| 1081 |
//LICENSERENEWAL: |
| 1082 |
CheckLicenseRenewal(); |
| 1083 |
|
| 1084 |
media::CdmVideoFrame videoFrame; |
| 1085 |
cdm::Status status = |
| 1086 |
m_wvCdmAdapter.DecryptAndDecodeFrame(inputBuffer, &videoFrame, codecInstance); |
| 1087 |
|
| 1088 |
if (status == cdm::Status::kSuccess) |
| 1089 |
{ |
| 1090 |
std::list<media::CdmVideoFrame>::iterator f(m_videoFrames.begin()); |
| 1091 |
while (f != m_videoFrames.end() && f->Timestamp() < videoFrame.Timestamp()) |
| 1092 |
{ |
| 1093 |
++f; |
| 1094 |
} |
| 1095 |
m_videoFrames.insert(f, videoFrame); |
| 1096 |
return VC_NONE; |
| 1097 |
} |
| 1098 |
else if (status == cdm::Status::kNeedMoreData && inputBuffer.data) |
| 1099 |
{ |
| 1100 |
return VC_NONE; |
| 1101 |
} |
| 1102 |
else if (status == cdm::Status::kNoKey) |
| 1103 |
{ |
| 1104 |
LOG::LogF(LOGERROR, "Returned CDM status \"kNoKey\" for KID: %s", |
| 1105 |
STRING::ToHexadecimal(inputBuffer.key_id, inputBuffer.key_id_size).c_str()); |
| 1106 |
return VC_EOF; |
| 1107 |
} |
| 1108 |
|
| 1109 |
LOG::LogF(LOGDEBUG, "Returned CDM status: %i", status); |
| 1110 |
return VC_ERROR; |
| 1111 |
} |
| 1112 |
|
| 1113 |
VIDEOCODEC_RETVAL CWVCencSingleSampleDecrypter::VideoFrameDataToPicture( |
| 1114 |
kodi::addon::CInstanceVideoCodec* codecInstance, VIDEOCODEC_PICTURE* picture) |
| 1115 |
{ |
| 1116 |
if (m_videoFrames.size() == 4 || |
| 1117 |
(m_videoFrames.size() > 0 && (picture->flags & VIDEOCODEC_PICTURE_FLAG_DRAIN))) |
| 1118 |
{ |
| 1119 |
media::CdmVideoFrame& videoFrame(m_videoFrames.front()); |
| 1120 |
|
| 1121 |
picture->width = videoFrame.Size().width; |
| 1122 |
picture->height = videoFrame.Size().height; |
| 1123 |
picture->pts = videoFrame.Timestamp(); |
| 1124 |
picture->decodedData = videoFrame.FrameBuffer()->Data(); |
| 1125 |
picture->decodedDataSize = videoFrame.FrameBuffer()->Size(); |
| 1126 |
picture->videoBufferHandle = static_cast<CdmFixedBuffer*>(videoFrame.FrameBuffer())->Buffer(); |
| 1127 |
|
| 1128 |
for (size_t i = 0; i < cdm::kMaxPlanes; ++i) |
| 1129 |
{ |
| 1130 |
picture->planeOffsets[i] = videoFrame.PlaneOffset(static_cast<cdm::VideoPlane>(i)); |
| 1131 |
picture->stride[i] = videoFrame.Stride(static_cast<cdm::VideoPlane>(i)); |
| 1132 |
} |
| 1133 |
picture->videoFormat = media::ToSSDVideoFormat(videoFrame.Format()); |
| 1134 |
videoFrame.SetFrameBuffer(nullptr); //marker for "No Picture" |
| 1135 |
|
| 1136 |
delete (CdmFixedBuffer*)(videoFrame.FrameBuffer()); |
| 1137 |
m_videoFrames.pop_front(); |
| 1138 |
|
| 1139 |
return VC_PICTURE; |
| 1140 |
} |
| 1141 |
else if ((picture->flags & VIDEOCODEC_PICTURE_FLAG_DRAIN)) |
| 1142 |
{ |
| 1143 |
static DEMUX_PACKET drainSample{}; |
| 1144 |
if (m_isDrained || DecryptAndDecodeVideo(codecInstance, &drainSample) == VC_ERROR) |
| 1145 |
{ |
| 1146 |
m_isDrained = true; |
| 1147 |
return VC_EOF; |
| 1148 |
} |
| 1149 |
else |
| 1150 |
return VC_NONE; |
| 1151 |
} |
| 1152 |
|
| 1153 |
return VC_BUFFER; |
| 1154 |
} |
| 1155 |
|
| 1156 |
void CWVCencSingleSampleDecrypter::ResetVideo() |
| 1157 |
{ |
| 1158 |
m_wvCdmAdapter.GetCdmAdapter()->ResetDecoder(cdm::kStreamTypeVideo); |
| 1159 |
m_isDrained = true; |
| 1160 |
} |
| 1161 |
|
| 1162 |
void CWVCencSingleSampleDecrypter::SetDefaultKeyId(const std::vector<uint8_t>& keyId) |
| 1163 |
{ |
| 1164 |
m_defaultKeyId = keyId; |
| 1165 |
} |
| 1166 |
|
| 1167 |
void CWVCencSingleSampleDecrypter::AddKeyId(const std::vector<uint8_t>& keyId) |
| 1168 |
{ |
| 1169 |
WVSKEY key; |
| 1170 |
key.m_keyId = keyId; |
| 1171 |
key.status = cdm::KeyStatus::kUsable; |
| 1172 |
|
| 1173 |
if (std::find(m_keys.begin(), m_keys.end(), key) == m_keys.end()) |
| 1174 |
{ |
| 1175 |
m_keys.push_back(key); |
| 1176 |
} |
| 1177 |
} |