7 #include <packager/media/base/playready_key_source.h>
12 #include <absl/log/check.h>
13 #include <absl/log/log.h>
14 #include <absl/strings/escaping.h>
16 #include <packager/macros/compiler.h>
17 #include <packager/macros/logging.h>
18 #include <packager/macros/status.h>
19 #include <packager/media/base/buffer_writer.h>
20 #include <packager/media/base/http_key_fetcher.h>
21 #include <packager/media/base/key_source.h>
22 #include <packager/media/base/protection_system_ids.h>
23 #include <packager/utils/hex_parser.h>
30 const int32_t kHttpFetchTimeout = 60;
31 const std::string kAcquireLicenseRequest =
32 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
33 "<soap:Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\" "
34 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
35 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
36 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
38 "<AcquirePackagingData "
39 "xmlns=\"http://schemas.microsoft.com/DRM/2007/03/protocols\">"
41 "xmlns=\"http://schemas.microsoft.com/DRM"
42 "/2007/03/protocols/AcquirePackagingData/v1.0\">"
44 "<ProtectionSystemId>9A04F079-9840-4286-AB92-E65BE0885F95"
45 "</ProtectionSystemId>"
46 "</ProtectionSystems>"
47 "<StreamProtectionRequests>"
49 "<ProgramIdentifier>$0</ProgramIdentifier>"
50 "<OffsetFromProgramStart>P0S</OffsetFromProgramStart>"
51 "</StreamInformation>"
52 "</StreamProtectionRequests>"
54 "</AcquirePackagingData>"
58 bool Base64StringToBytes(
const std::string& base64_string,
59 std::vector<uint8_t>* bytes) {
62 if (!absl::Base64Unescape(base64_string, &str))
64 bytes->assign(str.begin(), str.end());
70 ProtectionSystem protection_systems)
72 : generate_playready_protection_system_(
75 protection_systems == ProtectionSystem::kNone ||
76 has_flag(protection_systems, ProtectionSystem::kPlayReady)),
78 server_url_(server_url) {}
80 PlayReadyKeySource::~PlayReadyKeySource() =
default;
82 Status RetrieveTextInXMLElement(
const std::string& element,
83 const std::string& xml,
85 std::string start_tag =
"<" + element +
">";
86 std::string end_tag =
"</" + element +
">";
87 std::size_t start_pos = xml.find(start_tag);
88 if (start_pos == std::string::npos) {
89 return Status(error::SERVER_ERROR,
90 "Unable to find tag: " + start_tag);
92 start_pos += start_tag.size();
93 std::size_t end_pos = xml.find(end_tag);
94 if (end_pos == std::string::npos) {
95 return Status(error::SERVER_ERROR,
96 "Unable to find tag: " + end_tag);
98 if (start_pos > end_pos) {
99 return Status(error::SERVER_ERROR,
"Invalid positions");
101 std::size_t segment_len = end_pos - start_pos;
102 *value = xml.substr(start_pos, segment_len);
106 Status SetKeyInformationFromServerResponse(
107 const std::string& response,
108 bool generate_playready_protection_system,
109 EncryptionKey* encryption_key) {
114 std::string key_id_hex;
115 RETURN_IF_ERROR(RetrieveTextInXMLElement(
"KeyId", response, &key_id_hex));
117 std::remove(key_id_hex.begin(), key_id_hex.end(),
'-'), key_id_hex.end());
119 std::string key_id_raw;
120 if (!ValidHexStringToBytes(key_id_hex, &key_id_raw)) {
121 LOG(ERROR) <<
"Cannot parse key_id_hex, " << key_id_hex;
122 return Status(error::SERVER_ERROR,
"Cannot parse key_id_hex.");
124 encryption_key->key_id.assign(key_id_raw.begin(), key_id_raw.end());
126 std::string key_data_b64;
127 RETURN_IF_ERROR(RetrieveTextInXMLElement(
"KeyData", response, &key_data_b64));
128 if (!Base64StringToBytes(key_data_b64, &encryption_key->key)) {
129 LOG(ERROR) <<
"Cannot parse key, " << key_data_b64;
130 return Status(error::SERVER_ERROR,
"Cannot parse key.");
132 encryption_key->key_ids.emplace_back(encryption_key->key_id);
134 if (generate_playready_protection_system) {
135 std::string pssh_data_b64;
136 RETURN_IF_ERROR(RetrieveTextInXMLElement(
"Data", response, &pssh_data_b64));
137 std::vector<uint8_t> pssh_data;
138 if (!Base64StringToBytes(pssh_data_b64, &pssh_data)) {
139 LOG(ERROR) <<
"Cannot parse pssh data, " << pssh_data_b64;
140 return Status(error::SERVER_ERROR,
"Cannot parse pssh.");
143 PsshBoxBuilder pssh_builder;
144 pssh_builder.add_key_id(encryption_key->key_id);
145 pssh_builder.set_system_id(kPlayReadySystemId,
146 std::size(kPlayReadySystemId));
147 pssh_builder.set_pssh_data(pssh_data);
148 encryption_key->key_system_info.push_back(
149 {pssh_builder.system_id(), pssh_builder.CreateBox()});
154 Status PlayReadyKeySource::FetchKeysWithProgramIdentifier(
155 const std::string& program_identifier) {
156 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
157 HttpKeyFetcher key_fetcher(kHttpFetchTimeout);
159 std::string acquire_license_request = kAcquireLicenseRequest;
162 size_t dollar_zero_location = acquire_license_request.find(
"$0");
163 if (dollar_zero_location != std::string::npos) {
164 acquire_license_request.replace(dollar_zero_location, 2,
168 std::string acquire_license_response;
169 Status status = key_fetcher.FetchKeys(server_url_, acquire_license_request,
170 &acquire_license_response);
171 VLOG(1) <<
"Server response: " << acquire_license_response;
172 RETURN_IF_ERROR(status);
174 RETURN_IF_ERROR(SetKeyInformationFromServerResponse(
175 acquire_license_response, generate_playready_protection_system_,
176 encryption_key.get()));
179 encryption_key_ = std::move(encryption_key);
184 const std::vector<uint8_t>& init_data) {
185 UNUSED(init_data_type);
193 UNUSED(stream_label);
198 DCHECK(encryption_key_);
199 *key = *encryption_key_;
209 DCHECK(encryption_key_);
210 *key = *encryption_key_;
215 uint32_t crypto_period_index,
216 int32_t crypto_period_duration_in_seconds,
217 const std::string& stream_label,
219 UNUSED(crypto_period_index);
220 UNUSED(crypto_period_duration_in_seconds);
221 UNUSED(stream_label);
223 *key = *encryption_key_;
All the methods that are virtual are virtual for mocking.