2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-01-05 17:32:17 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/playready_key_source.h>
|
2017-01-05 17:32:17 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <iterator>
|
2018-08-10 01:31:44 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/strings/escaping.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <packager/macros.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/buffer_writer.h>
|
|
|
|
#include <packager/media/base/http_key_fetcher.h>
|
|
|
|
#include <packager/media/base/key_source.h>
|
|
|
|
#include <packager/media/base/protection_system_ids.h>
|
|
|
|
#include <packager/status/status_macros.h>
|
|
|
|
#include <packager/utils/hex_parser.h>
|
2017-01-05 17:32:17 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kHttpFetchTimeout = 60; // In seconds
|
2017-01-05 17:32:17 +00:00
|
|
|
const std::string kAcquireLicenseRequest =
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
|
|
|
"<soap:Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
|
|
|
|
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
|
|
|
|
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
|
|
|
|
"<soap:Body>"
|
|
|
|
"<AcquirePackagingData "
|
|
|
|
"xmlns=\"http://schemas.microsoft.com/DRM/2007/03/protocols\">"
|
|
|
|
"<challenge "
|
|
|
|
"xmlns=\"http://schemas.microsoft.com/DRM"
|
|
|
|
"/2007/03/protocols/AcquirePackagingData/v1.0\">"
|
|
|
|
"<ProtectionSystems>"
|
|
|
|
"<ProtectionSystemId>9A04F079-9840-4286-AB92-E65BE0885F95"
|
|
|
|
"</ProtectionSystemId>"
|
|
|
|
"</ProtectionSystems>"
|
|
|
|
"<StreamProtectionRequests>"
|
|
|
|
"<StreamInformation>"
|
|
|
|
"<ProgramIdentifier>$0</ProgramIdentifier>"
|
|
|
|
"<OffsetFromProgramStart>P0S</OffsetFromProgramStart>"
|
|
|
|
"</StreamInformation>"
|
|
|
|
"</StreamProtectionRequests>"
|
|
|
|
"</challenge>"
|
|
|
|
"</AcquirePackagingData>"
|
|
|
|
"</soap:Body>"
|
|
|
|
"</soap:Envelope>";
|
|
|
|
|
|
|
|
bool Base64StringToBytes(const std::string& base64_string,
|
|
|
|
std::vector<uint8_t>* bytes) {
|
|
|
|
DCHECK(bytes);
|
|
|
|
std::string str;
|
2022-11-04 22:46:41 +00:00
|
|
|
if (!absl::Base64Unescape(base64_string, &str))
|
2017-01-05 17:32:17 +00:00
|
|
|
return false;
|
|
|
|
bytes->assign(str.begin(), str.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 17:46:14 +00:00
|
|
|
PlayReadyKeySource::PlayReadyKeySource(const std::string& server_url,
|
2020-06-01 22:26:31 +00:00
|
|
|
ProtectionSystem protection_systems)
|
2018-04-20 18:06:36 +00:00
|
|
|
// PlayReady PSSH is retrived from PlayReady server response.
|
2020-06-01 22:26:31 +00:00
|
|
|
: generate_playready_protection_system_(
|
2018-08-10 20:31:17 +00:00
|
|
|
// Generate PlayReady protection system if there are no other
|
|
|
|
// protection system specified.
|
2020-05-29 18:45:57 +00:00
|
|
|
protection_systems == ProtectionSystem::kNone ||
|
|
|
|
has_flag(protection_systems, ProtectionSystem::kPlayReady)),
|
2017-09-12 19:22:39 +00:00
|
|
|
encryption_key_(new EncryptionKey),
|
|
|
|
server_url_(server_url) {}
|
2017-01-05 17:32:17 +00:00
|
|
|
|
2018-08-07 23:01:43 +00:00
|
|
|
PlayReadyKeySource::~PlayReadyKeySource() = default;
|
2017-01-05 17:32:17 +00:00
|
|
|
|
|
|
|
Status RetrieveTextInXMLElement(const std::string& element,
|
|
|
|
const std::string& xml,
|
|
|
|
std::string* value) {
|
|
|
|
std::string start_tag = "<" + element + ">";
|
|
|
|
std::string end_tag = "</" + element + ">";
|
|
|
|
std::size_t start_pos = xml.find(start_tag);
|
|
|
|
if (start_pos == std::string::npos) {
|
|
|
|
return Status(error::SERVER_ERROR,
|
|
|
|
"Unable to find tag: " + start_tag);
|
|
|
|
}
|
|
|
|
start_pos += start_tag.size();
|
|
|
|
std::size_t end_pos = xml.find(end_tag);
|
|
|
|
if (end_pos == std::string::npos) {
|
|
|
|
return Status(error::SERVER_ERROR,
|
|
|
|
"Unable to find tag: " + end_tag);
|
|
|
|
}
|
|
|
|
if (start_pos > end_pos) {
|
|
|
|
return Status(error::SERVER_ERROR, "Invalid positions");
|
|
|
|
}
|
|
|
|
std::size_t segment_len = end_pos - start_pos;
|
|
|
|
*value = xml.substr(start_pos, segment_len);
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
Status SetKeyInformationFromServerResponse(
|
|
|
|
const std::string& response,
|
|
|
|
bool generate_playready_protection_system,
|
|
|
|
EncryptionKey* encryption_key) {
|
2017-01-05 17:32:17 +00:00
|
|
|
// TODO(robinconnell): Currently all tracks are encrypted using the same
|
|
|
|
// key_id and key. Add the ability to retrieve multiple key_id/keys from
|
|
|
|
// the packager response and encrypt multiple tracks using differnt
|
|
|
|
// key_id/keys.
|
|
|
|
std::string key_id_hex;
|
2018-08-10 01:31:44 +00:00
|
|
|
RETURN_IF_ERROR(RetrieveTextInXMLElement("KeyId", response, &key_id_hex));
|
2017-01-05 17:32:17 +00:00
|
|
|
key_id_hex.erase(
|
|
|
|
std::remove(key_id_hex.begin(), key_id_hex.end(), '-'), key_id_hex.end());
|
2022-11-04 22:46:41 +00:00
|
|
|
|
|
|
|
std::string key_id_raw;
|
|
|
|
if (!ValidHexStringToBytes(key_id_hex, &key_id_raw)) {
|
2017-01-05 17:32:17 +00:00
|
|
|
LOG(ERROR) << "Cannot parse key_id_hex, " << key_id_hex;
|
|
|
|
return Status(error::SERVER_ERROR, "Cannot parse key_id_hex.");
|
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
encryption_key->key_id.assign(key_id_raw.begin(), key_id_raw.end());
|
2017-01-05 17:32:17 +00:00
|
|
|
|
2018-08-10 01:31:44 +00:00
|
|
|
std::string key_data_b64;
|
|
|
|
RETURN_IF_ERROR(RetrieveTextInXMLElement("KeyData", response, &key_data_b64));
|
2017-01-05 17:32:17 +00:00
|
|
|
if (!Base64StringToBytes(key_data_b64, &encryption_key->key)) {
|
|
|
|
LOG(ERROR) << "Cannot parse key, " << key_data_b64;
|
|
|
|
return Status(error::SERVER_ERROR, "Cannot parse key.");
|
|
|
|
}
|
2020-06-01 22:26:31 +00:00
|
|
|
encryption_key->key_ids.emplace_back(encryption_key->key_id);
|
2018-08-10 01:31:44 +00:00
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
if (generate_playready_protection_system) {
|
|
|
|
std::string pssh_data_b64;
|
|
|
|
RETURN_IF_ERROR(RetrieveTextInXMLElement("Data", response, &pssh_data_b64));
|
|
|
|
std::vector<uint8_t> pssh_data;
|
|
|
|
if (!Base64StringToBytes(pssh_data_b64, &pssh_data)) {
|
|
|
|
LOG(ERROR) << "Cannot parse pssh data, " << pssh_data_b64;
|
|
|
|
return Status(error::SERVER_ERROR, "Cannot parse pssh.");
|
|
|
|
}
|
|
|
|
|
|
|
|
PsshBoxBuilder pssh_builder;
|
|
|
|
pssh_builder.add_key_id(encryption_key->key_id);
|
|
|
|
pssh_builder.set_system_id(kPlayReadySystemId,
|
2022-11-04 22:46:41 +00:00
|
|
|
std::size(kPlayReadySystemId));
|
2018-08-10 20:31:17 +00:00
|
|
|
pssh_builder.set_pssh_data(pssh_data);
|
|
|
|
encryption_key->key_system_info.push_back(
|
|
|
|
{pssh_builder.system_id(), pssh_builder.CreateBox()});
|
2017-01-05 17:32:17 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PlayReadyKeySource::FetchKeysWithProgramIdentifier(
|
|
|
|
const std::string& program_identifier) {
|
|
|
|
std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey);
|
|
|
|
HttpKeyFetcher key_fetcher(kHttpFetchTimeout);
|
2018-08-10 01:31:44 +00:00
|
|
|
|
2017-01-05 17:32:17 +00:00
|
|
|
std::string acquire_license_request = kAcquireLicenseRequest;
|
2022-11-04 22:46:41 +00:00
|
|
|
|
|
|
|
// Replace "$0" with |program_identifier|
|
|
|
|
size_t dollar_zero_location = acquire_license_request.find("$0");
|
|
|
|
if (dollar_zero_location != std::string::npos) {
|
|
|
|
acquire_license_request.replace(dollar_zero_location, /* len= */ 2,
|
|
|
|
program_identifier);
|
|
|
|
}
|
|
|
|
|
2017-01-05 17:32:17 +00:00
|
|
|
std::string acquire_license_response;
|
|
|
|
Status status = key_fetcher.FetchKeys(server_url_, acquire_license_request,
|
|
|
|
&acquire_license_response);
|
2018-08-10 01:31:44 +00:00
|
|
|
VLOG(1) << "Server response: " << acquire_license_response;
|
|
|
|
RETURN_IF_ERROR(status);
|
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
RETURN_IF_ERROR(SetKeyInformationFromServerResponse(
|
|
|
|
acquire_license_response, generate_playready_protection_system_,
|
|
|
|
encryption_key.get()));
|
2018-08-10 01:31:44 +00:00
|
|
|
|
2018-08-06 23:12:19 +00:00
|
|
|
// PlayReady does not specify different streams.
|
2020-06-01 22:26:31 +00:00
|
|
|
encryption_key_ = std::move(encryption_key);
|
2017-01-05 17:32:17 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:43:41 +00:00
|
|
|
Status PlayReadyKeySource::FetchKeys(EmeInitDataType init_data_type,
|
|
|
|
const std::vector<uint8_t>& init_data) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(init_data_type);
|
|
|
|
UNUSED(init_data);
|
2018-08-06 23:12:19 +00:00
|
|
|
// Do nothing for PlayReady encryption/decryption.
|
2017-01-05 17:32:17 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:54:12 +00:00
|
|
|
Status PlayReadyKeySource::GetKey(const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(stream_label);
|
2017-01-05 17:32:17 +00:00
|
|
|
// TODO(robinconnell): Currently all tracks are encrypted using the same
|
2017-06-13 21:54:12 +00:00
|
|
|
// key_id and key. Add the ability to encrypt each stream_label using a
|
2017-01-05 17:32:17 +00:00
|
|
|
// different key_id and key.
|
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(encryption_key_);
|
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PlayReadyKeySource::GetKey(const std::vector<uint8_t>& key_id,
|
|
|
|
EncryptionKey* key) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(key_id);
|
2017-01-05 17:32:17 +00:00
|
|
|
// TODO(robinconnell): Currently all tracks are encrypted using the same
|
|
|
|
// key_id and key. Add the ability to encrypt using multiple key_id/keys.
|
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(encryption_key_);
|
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
Status PlayReadyKeySource::GetCryptoPeriodKey(
|
|
|
|
uint32_t crypto_period_index,
|
|
|
|
int32_t crypto_period_duration_in_seconds,
|
|
|
|
const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(crypto_period_index);
|
|
|
|
UNUSED(crypto_period_duration_in_seconds);
|
|
|
|
UNUSED(stream_label);
|
2017-01-05 17:32:17 +00:00
|
|
|
// TODO(robinconnell): Implement key rotation.
|
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|