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() {}
24 const std::vector<std::vector<uint8_t>>& key_ids) {
36 DCHECK(encryption_key_);
37 *key = *encryption_key_;
44 DCHECK(encryption_key_);
45 if (key_id != encryption_key_->key_id) {
46 return Status(error::NOT_FOUND,
47 std::string(
"Key for key ID ") +
48 base::HexEncode(&key_id[0], key_id.size()) +
51 *key = *encryption_key_;
59 *key = *encryption_key_;
66 <<
"This naive key rotation algorithm should not be used in production.";
67 std::rotate(key->key_id.begin(),
68 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
70 std::rotate(key->key.begin(),
71 key->key.begin() + (crypto_period_index % key->key.size()),
74 for (
size_t i = 0; i < key->key_system_info.size(); i++) {
75 std::vector<uint8_t> pssh_data = key->key_system_info[i].pssh_data();
76 if (!pssh_data.empty()) {
77 std::rotate(pssh_data.begin(),
78 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
80 key->key_system_info[i].set_pssh_data(pssh_data);
88 const std::string& key_id_hex,
89 const std::string& key_hex,
90 const std::string& pssh_boxes_hex,
91 const std::string& iv_hex) {
92 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
94 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
95 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
96 return std::unique_ptr<FixedKeySource>();
97 }
else if (encryption_key->key_id.size() != 16) {
98 LOG(ERROR) <<
"Invalid key ID size '" << encryption_key->key_id.size()
99 <<
"', must be 16 bytes.";
100 return std::unique_ptr<FixedKeySource>();
103 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
104 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
105 return std::unique_ptr<FixedKeySource>();
106 }
else if (encryption_key->key.size() != 16) {
108 LOG(ERROR) <<
"Invalid key size '" << encryption_key->key.size()
109 <<
"', must be 16 bytes.";
110 return std::unique_ptr<FixedKeySource>();
113 std::vector<uint8_t> pssh_boxes;
114 if (!pssh_boxes_hex.empty() &&
115 !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
116 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_boxes_hex;
117 return std::unique_ptr<FixedKeySource>();
120 if (!iv_hex.empty()) {
121 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
122 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
123 return std::unique_ptr<FixedKeySource>();
128 pssh_boxes.data(), pssh_boxes.size(),
129 &encryption_key->key_system_info)) {
130 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
131 return std::unique_ptr<FixedKeySource>();
135 if (encryption_key->key_system_info.size() == 0) {
137 info.add_key_id(encryption_key->key_id);
138 info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
139 info.set_pssh_box_version(1);
141 encryption_key->key_system_info.push_back(info);
144 return std::unique_ptr<FixedKeySource>(
148 FixedKeySource::FixedKeySource() {}
149 FixedKeySource::FixedKeySource(std::unique_ptr<EncryptionKey> key)
150 : encryption_key_(std::move(key)) {}