7 #include "packager/media/base/playready_key_source.h" 10 #include "packager/base/base64.h" 11 #include "packager/base/logging.h" 12 #include "packager/base/strings/string_number_conversions.h" 13 #include "packager/base/strings/string_util.h" 14 #include "packager/media/base/buffer_writer.h" 15 #include "packager/media/base/http_key_fetcher.h" 16 #include "packager/media/base/key_source.h" 17 #include "packager/media/base/playready_pssh_generator.h" 24 const uint32_t kHttpFetchTimeout = 60;
25 const std::string kAcquireLicenseRequest =
26 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 27 "<soap:Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\" " 28 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " 29 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " 30 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 32 "<AcquirePackagingData " 33 "xmlns=\"http://schemas.microsoft.com/DRM/2007/03/protocols\">" 35 "xmlns=\"http://schemas.microsoft.com/DRM" 36 "/2007/03/protocols/AcquirePackagingData/v1.0\">" 38 "<ProtectionSystemId>9A04F079-9840-4286-AB92-E65BE0885F95" 39 "</ProtectionSystemId>" 40 "</ProtectionSystems>" 41 "<StreamProtectionRequests>" 43 "<ProgramIdentifier>$0</ProgramIdentifier>" 44 "<OffsetFromProgramStart>P0S</OffsetFromProgramStart>" 45 "</StreamInformation>" 46 "</StreamProtectionRequests>" 48 "</AcquirePackagingData>" 52 bool Base64StringToBytes(
const std::string& base64_string,
53 std::vector<uint8_t>* bytes) {
56 if (!base::Base64Decode(base64_string, &str))
58 bytes->assign(str.begin(), str.end());
64 int protection_system_flags)
66 :
KeySource(protection_system_flags & ~PLAYREADY_PROTECTION_SYSTEM_FLAG),
68 server_url_(server_url) {}
71 const std::string& server_url,
72 const std::string& client_cert_file,
73 const std::string& client_cert_private_key_file,
74 const std::string& client_cert_private_key_password,
75 int protection_system_flags)
77 :
KeySource(protection_system_flags & ~PLAYREADY_PROTECTION_SYSTEM_FLAG),
79 server_url_(server_url),
80 client_cert_file_(client_cert_file),
81 client_cert_private_key_file_(client_cert_private_key_file),
82 client_cert_private_key_password_(client_cert_private_key_password) {}
85 std::unique_ptr<EncryptionKey> encryption_key)
86 :
KeySource(PLAYREADY_PROTECTION_SYSTEM_FLAG),
87 encryption_key_(std::move(encryption_key)) {}
89 PlayReadyKeySource::~PlayReadyKeySource() {}
91 Status RetrieveTextInXMLElement(
const std::string& element,
92 const std::string& xml,
94 std::string start_tag =
"<" + element +
">";
95 std::string end_tag =
"</" + element +
">";
96 std::size_t start_pos = xml.find(start_tag);
97 if (start_pos == std::string::npos) {
98 return Status(error::SERVER_ERROR,
99 "Unable to find tag: " + start_tag);
101 start_pos += start_tag.size();
102 std::size_t end_pos = xml.find(end_tag);
103 if (end_pos == std::string::npos) {
104 return Status(error::SERVER_ERROR,
105 "Unable to find tag: " + end_tag);
107 if (start_pos > end_pos) {
108 return Status(error::SERVER_ERROR,
"Invalid positions");
110 std::size_t segment_len = end_pos - start_pos;
111 *value = xml.substr(start_pos, segment_len);
115 Status SetKeyInformationFromServerResponse(
const std::string& response,
121 std::string key_id_hex;
122 Status status = RetrieveTextInXMLElement(
"KeyId", response, &key_id_hex);
127 std::remove(key_id_hex.begin(), key_id_hex.end(),
'-'), key_id_hex.end());
128 std::string key_data_b64;
129 status = RetrieveTextInXMLElement(
"KeyData", response, &key_data_b64);
131 LOG(ERROR) <<
"Key retreiving KeyData";
134 std::string pssh_data_b64;
135 status = RetrieveTextInXMLElement(
"Data", response, &pssh_data_b64);
137 LOG(ERROR) <<
"Key retreiving Data";
140 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
141 LOG(ERROR) <<
"Cannot parse key_id_hex, " << key_id_hex;
142 return Status(error::SERVER_ERROR,
"Cannot parse key_id_hex.");
145 if (!Base64StringToBytes(key_data_b64, &encryption_key->key)) {
146 LOG(ERROR) <<
"Cannot parse key, " << key_data_b64;
147 return Status(error::SERVER_ERROR,
"Cannot parse key.");
149 std::vector<uint8_t> pssh_data;
150 if (!Base64StringToBytes(pssh_data_b64, &pssh_data)) {
151 LOG(ERROR) <<
"Cannot parse pssh data, " << pssh_data_b64;
152 return Status(error::SERVER_ERROR,
"Cannot parse pssh.");
156 pssh_builder.add_key_id(encryption_key->key_id);
157 pssh_builder.set_system_id(kPlayReadySystemId, arraysize(kPlayReadySystemId));
158 pssh_builder.set_pssh_data(pssh_data);
159 encryption_key->key_system_info.push_back(
160 {pssh_builder.system_id(), pssh_builder.
CreateBox()});
164 Status PlayReadyKeySource::FetchKeysWithProgramIdentifier(
165 const std::string& program_identifier) {
166 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
168 if (!client_cert_file_.empty() && !client_cert_private_key_file_.empty()) {
170 client_cert_private_key_file_,
171 client_cert_private_key_password_);
173 if (!ca_file_.empty()) {
176 std::string acquire_license_request = kAcquireLicenseRequest;
177 base::ReplaceFirstSubstringAfterOffset(
178 &acquire_license_request, 0,
"$0", program_identifier);
179 std::string acquire_license_response;
180 Status status = key_fetcher.
FetchKeys(server_url_, acquire_license_request,
181 &acquire_license_response);
183 LOG(ERROR) <<
"Server response: " << acquire_license_response;
186 status = SetKeyInformationFromServerResponse(acquire_license_response,
187 encryption_key.get());
189 const char kEmptyDrmLabel[] =
"";
190 EncryptionKeyMap encryption_key_map;
191 encryption_key_map[kEmptyDrmLabel] = std::move(encryption_key);
196 encryption_key_ = std::move(encryption_key_map[kEmptyDrmLabel]);
201 const std::vector<uint8_t>& init_data) {
212 DCHECK(encryption_key_);
213 *key = *encryption_key_;
222 DCHECK(encryption_key_);
223 *key = *encryption_key_;
228 const std::string& stream_label,
231 *key = *encryption_key_;
All the methods that are virtual are virtual for mocking.