7 #include "packager/media/base/fixed_key_source.h"
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_number_conversions.h"
16 FixedKeySource::~FixedKeySource() {}
19 const std::vector<uint8_t>& init_data) {
27 DCHECK(encryption_key_);
28 *key = *encryption_key_;
35 DCHECK(encryption_key_);
36 if (key_id != encryption_key_->key_id) {
37 return Status(error::NOT_FOUND,
38 std::string(
"Key for key ID ") +
39 base::HexEncode(&key_id[0], key_id.size()) +
42 *key = *encryption_key_;
47 const std::string& stream_label,
50 *key = *encryption_key_;
57 <<
"This naive key rotation algorithm should not be used in production.";
58 std::rotate(key->key_id.begin(),
59 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
61 std::rotate(key->key.begin(),
62 key->key.begin() + (crypto_period_index % key->key.size()),
65 for (
size_t i = 0; i < key->key_system_info.size(); i++) {
66 std::vector<uint8_t> pssh_data = key->key_system_info[i].pssh_data();
67 if (!pssh_data.empty()) {
68 std::rotate(pssh_data.begin(),
69 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
71 key->key_system_info[i].set_pssh_data(pssh_data);
79 const std::string& key_id_hex,
80 const std::string& key_hex,
81 const std::string& pssh_boxes_hex,
82 const std::string& iv_hex) {
83 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
85 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
86 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
87 return std::unique_ptr<FixedKeySource>();
88 }
else if (encryption_key->key_id.size() != 16) {
89 LOG(ERROR) <<
"Invalid key ID size '" << encryption_key->key_id.size()
90 <<
"', must be 16 bytes.";
91 return std::unique_ptr<FixedKeySource>();
94 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
95 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
96 return std::unique_ptr<FixedKeySource>();
97 }
else if (encryption_key->key.size() != 16) {
99 LOG(ERROR) <<
"Invalid key size '" << encryption_key->key.size()
100 <<
"', must be 16 bytes.";
101 return std::unique_ptr<FixedKeySource>();
104 std::vector<uint8_t> pssh_boxes;
105 if (!pssh_boxes_hex.empty() &&
106 !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
107 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_boxes_hex;
108 return std::unique_ptr<FixedKeySource>();
111 if (!iv_hex.empty()) {
112 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
113 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
114 return std::unique_ptr<FixedKeySource>();
119 pssh_boxes.data(), pssh_boxes.size(),
120 &encryption_key->key_system_info)) {
121 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
122 return std::unique_ptr<FixedKeySource>();
126 if (encryption_key->key_system_info.size() == 0) {
128 info.add_key_id(encryption_key->key_id);
129 info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
130 info.set_pssh_box_version(1);
132 encryption_key->key_system_info.push_back(info);
135 return std::unique_ptr<FixedKeySource>(
139 FixedKeySource::FixedKeySource() {}
140 FixedKeySource::FixedKeySource(std::unique_ptr<EncryptionKey> key)
141 : encryption_key_(std::move(key)) {}