Skip to content

Commit

Permalink
Merge pull request #1658 from CastagnaIT/fix_cc_encdec_omega
Browse files Browse the repository at this point in the history
[backport][ClearKey] Fix base64 encoding/decoding
  • Loading branch information
CastagnaIT committed Aug 26, 2024
2 parents 37dfe8f + 2001365 commit 24fe9f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/decrypters/clearkey/ClearKeyCencSingleSampleDecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@

using namespace UTILS;

namespace
{
void CkB64Encode(std::string& str)
{
STRING::ReplaceAll(str, "+", "-");
STRING::ReplaceAll(str, "/", "_");
}

void CkB64Decode(std::string& str)
{
STRING::ReplaceAll(str, "-", "+");
STRING::ReplaceAll(str, "_", "/");
}
}

CClearKeyCencSingleSampleDecrypter::CClearKeyCencSingleSampleDecrypter(
std::string_view licenseUrl,
const std::map<std::string, std::string>& licenseHeaders,
Expand Down Expand Up @@ -79,7 +94,7 @@ CClearKeyCencSingleSampleDecrypter::CClearKeyCencSingleSampleDecrypter(
return;
}

const std::string b64DefaultKeyId = UTILS::BASE64::Encode(defaultKeyId);
const std::string b64DefaultKeyId = BASE64::Encode(defaultKeyId);
if (!STRING::KeyExists(m_keyPairs, b64DefaultKeyId))
{
LOG::LogF(LOGERROR, "Key not found on license server response");
Expand Down Expand Up @@ -112,10 +127,10 @@ CClearKeyCencSingleSampleDecrypter::CClearKeyCencSingleSampleDecrypter(
}
else // Key provided in Kodi props
{
const std::string hexDefKid = UTILS::STRING::ToHexadecimal(defaultKeyId);
const std::string hexDefKid = STRING::ToHexadecimal(defaultKeyId);

if (STRING::KeyExists(keys, hexDefKid))
UTILS::STRING::ToHexBytes(keys.at(hexDefKid), hexKey);
STRING::ToHexBytes(keys.at(hexDefKid), hexKey);
else
LOG::LogF(LOGERROR, "Missing KeyId \"%s\" on DRM configuration", defaultKeyId.data());
}
Expand Down Expand Up @@ -176,7 +191,8 @@ std::string CClearKeyCencSingleSampleDecrypter::CreateLicenseRequest(
* "type":"temporary" }
*/

std::string b64Kid = UTILS::BASE64::Encode(defaultKeyId, false);
std::string b64Kid = BASE64::Encode(defaultKeyId, false);
CkB64Encode(b64Kid);

rapidjson::Document jDoc;
jDoc.SetObject();
Expand Down Expand Up @@ -253,27 +269,11 @@ bool CClearKeyCencSingleSampleDecrypter::ParseLicenseResponse(std::string data)

if (!b64Key.empty() && !b64KeyId.empty())
{
UTILS::STRING::ReplaceAll(b64Key, "-", "+");
UTILS::STRING::ReplaceAll(b64KeyId, "-", "+");

// pad b64
int left = 4 - (b64Key.length() % 4);
if (b64Key.length() % 4)
{
for (int i = 0; i < left; i++)
{
b64Key.push_back('=');
}
}

left = 4 - (b64KeyId.length() % 4);
if (b64KeyId.length() % 4)
{
for (int i = 0; i < left; i++)
{
b64KeyId.push_back('=');
}
}
CkB64Decode(b64Key);
BASE64::AddPadding(b64Key);

CkB64Decode(b64KeyId);
BASE64::AddPadding(b64KeyId);

m_keyPairs.emplace(b64KeyId, b64Key);
break;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/Base64Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,17 @@ bool UTILS::BASE64::IsValidBase64(const std::string& input)
std::regex base64Regex(REGEX.data());
return std::regex_match(input, base64Regex);
}

bool UTILS::BASE64::AddPadding(std::string& base64str)
{
const int mod = static_cast<int>(base64str.length() % 4);
if (mod > 0)
{
for (int i = 4 - mod; i > 0; --i)
{
base64str.push_back(PADDING);
}
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions src/utils/Base64Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ std::string DecodeToStr(std::string_view input);

bool IsValidBase64(const std::string& input);

bool AddPadding(std::string& base64str);

} // namespace BASE64
} // namespace UTILS

0 comments on commit 24fe9f3

Please sign in to comment.