7#include <packager/media/base/playready_pssh_generator.h>
16#include <absl/log/check.h>
17#include <absl/log/log.h>
18#include <absl/strings/escaping.h>
19#include <mbedtls/cipher.h>
21#include <packager/macros/compiler.h>
22#include <packager/macros/crypto.h>
23#include <packager/macros/logging.h>
24#include <packager/media/base/buffer_writer.h>
25#include <packager/media/base/fourccs.h>
26#include <packager/media/base/protection_system_ids.h>
27#include <packager/media/base/pssh_generator.h>
28#include <packager/status.h>
34const uint8_t kPlayReadyPsshBoxVersion = 0;
37const std::string kPlayHeaderObject_4_0 =
39 "xmlns=\"http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader\" "
40 "version=\"4.0.0.0\"><DATA>"
41 "<PROTECTINFO><KEYLEN>16</KEYLEN><ALGID>AESCTR</ALGID></PROTECTINFO>"
42 "<KID>$0</KID><CHECKSUM>$1</CHECKSUM>"
43 "$2</DATA></WRMHEADER>";
46const std::string kPlayHeaderObject_4_3 =
48 "xmlns=\"http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader\" "
49 "version=\"4.3.0.0\"><DATA><PROTECTINFO><KIDS>"
50 "<KID ALGID=\"AESCBC\" VALUE=\"$0\"></KID>"
51 "</KIDS></PROTECTINFO>$1</DATA></WRMHEADER>";
54std::vector<uint8_t> ConvertGuidEndianness(
const std::vector<uint8_t>& input) {
55 std::vector<uint8_t> output = input;
56 if (output.size() > 7) {
70void ReplaceString(std::string* str,
71 const std::string& from,
72 const std::string& to) {
73 size_t location = str->find(from);
74 if (location != std::string::npos) {
75 str->replace(location, from.size(), to);
79void AesEcbEncrypt(
const std::vector<uint8_t>& key,
80 const std::vector<uint8_t>& plaintext,
81 std::vector<uint8_t>* ciphertext) {
82 CHECK_EQ(plaintext.size() % AES_BLOCK_SIZE, 0u);
83 ciphertext->resize(plaintext.size());
85 mbedtls_cipher_context_t ctx;
86 mbedtls_cipher_init(&ctx);
88 const mbedtls_cipher_info_t* cipher_info =
89 mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
92 CHECK_EQ(mbedtls_cipher_setup(&ctx, cipher_info), 0) <<
"Cipher setup failed";
94 CHECK_EQ(key.size(), 16u);
96 mbedtls_cipher_setkey(&ctx, key.data(), 8 * key.size(), MBEDTLS_ENCRYPT),
98 <<
"Failed to set encryption key";
100 size_t output_size = 0;
101 CHECK_EQ(mbedtls_cipher_crypt(&ctx, NULL, 0,
102 plaintext.data(), plaintext.size(),
103 ciphertext->data(), &output_size),
106 mbedtls_cipher_free(&ctx);
112Status GeneratePlayReadyPsshData(
const std::vector<uint8_t>& key_id,
113 const std::vector<uint8_t>& key,
114 const std::string& extra_header_data,
115 const FourCC protection_scheme,
116 std::vector<uint8_t>* output) {
118 std::vector<uint8_t> key_id_converted = ConvertGuidEndianness(key_id);
120 std::vector<uint8_t> encrypted_key_id;
121 AesEcbEncrypt(key, key_id_converted, &encrypted_key_id);
123 std::string checksum =
124 std::string(encrypted_key_id.begin(), encrypted_key_id.end())
126 std::string base64_checksum;
127 absl::Base64Escape(checksum, &base64_checksum);
128 std::string base64_key_id;
130 std::string(key_id_converted.begin(), key_id_converted.end()),
133 std::string playready_header;
135 switch (protection_scheme) {
136 case kAppleSampleAesProtectionScheme:
139 playready_header = kPlayHeaderObject_4_3;
140 ReplaceString(&playready_header,
"$0", base64_key_id);
141 ReplaceString(&playready_header,
"$1", extra_header_data);
146 playready_header = kPlayHeaderObject_4_0;
147 ReplaceString(&playready_header,
"$0", base64_key_id);
148 ReplaceString(&playready_header,
"$1", base64_checksum);
149 ReplaceString(&playready_header,
"$2", extra_header_data);
153 return Status(error::INVALID_ARGUMENT,
154 "The provided protection scheme is not supported.");
160 std::vector<uint16_t> record_value =
161 std::vector<uint16_t>(playready_header.begin(), playready_header.end());
163 uint16_t record_type =
165 uint16_t record_length = record_value.size() * 2;
166 writer_pr_record.
AppendInt(
static_cast<uint8_t
>(record_type & 0xff));
167 writer_pr_record.
AppendInt(
static_cast<uint8_t
>((record_type >> 8) & 0xff));
168 writer_pr_record.
AppendInt(
static_cast<uint8_t
>(record_length & 0xff));
169 writer_pr_record.
AppendInt(
static_cast<uint8_t
>((record_length >> 8) & 0xff));
170 for (
auto record_item : record_value) {
171 writer_pr_record.
AppendInt(
static_cast<uint8_t
>(record_item & 0xff));
172 writer_pr_record.
AppendInt(
static_cast<uint8_t
>((record_item >> 8) & 0xff));
179 uint32_t playready_header_length = writer_pr_record.Size() + 4 + 2;
180 uint16_t record_count = 1;
182 static_cast<uint8_t
>(playready_header_length & 0xff));
184 static_cast<uint8_t
>((playready_header_length >> 8) & 0xff));
186 static_cast<uint8_t
>((playready_header_length >> 16) & 0xff));
188 static_cast<uint8_t
>((playready_header_length >> 24) & 0xff));
189 writer_pr_header_object.
AppendInt(
static_cast<uint8_t
>(record_count & 0xff));
191 static_cast<uint8_t
>((record_count >> 8) & 0xff));
192 writer_pr_header_object.AppendBuffer(writer_pr_record);
193 *output = std::vector<uint8_t>(
194 writer_pr_header_object.
Buffer(),
195 writer_pr_header_object.
Buffer() + writer_pr_header_object.Size());
200PlayReadyPsshGenerator::PlayReadyPsshGenerator(
201 const std::string& extra_header_data,
202 FourCC protection_scheme)
203 : PsshGenerator(std::vector<uint8_t>(std::begin(kPlayReadySystemId),
204 std::end(kPlayReadySystemId)),
205 kPlayReadyPsshBoxVersion),
206 extra_header_data_(extra_header_data),
207 protection_scheme_(protection_scheme) {}
209PlayReadyPsshGenerator::~PlayReadyPsshGenerator() {}
211bool PlayReadyPsshGenerator::SupportMultipleKeys() {
215std::optional<std::vector<uint8_t>>
216PlayReadyPsshGenerator::GeneratePsshDataFromKeyIdAndKey(
217 const std::vector<uint8_t>& key_id,
218 const std::vector<uint8_t>& key)
const {
219 std::vector<uint8_t> pssh_data;
220 Status status = GeneratePlayReadyPsshData(key_id, key, extra_header_data_,
221 protection_scheme_, &pssh_data);
223 LOG(ERROR) << status.ToString();
230std::optional<std::vector<uint8_t>>
231PlayReadyPsshGenerator::GeneratePsshDataFromKeyIds(
232 const std::vector<std::vector<uint8_t>>& key_ids)
const {
All the methods that are virtual are virtual for mocking.