7 #include "packager/media/base/fixed_key_source.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/strings/string_number_conversions.h"
15 FixedKeySource::~FixedKeySource() {}
23 const std::vector<std::vector<uint8_t>>& key_ids) {
35 DCHECK(encryption_key_);
36 *key = *encryption_key_;
43 DCHECK(encryption_key_);
44 if (key_id != encryption_key_->key_id) {
45 return Status(error::NOT_FOUND,
46 std::string(
"Key for key ID ") +
47 base::HexEncode(&key_id[0], key_id.size()) +
50 *key = *encryption_key_;
58 *key = *encryption_key_;
65 <<
"This naive key rotation algorithm should not be used in production.";
66 std::rotate(key->key_id.begin(),
67 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
69 std::rotate(key->key.begin(),
70 key->key.begin() + (crypto_period_index % key->key.size()),
73 for (
size_t i = 0; i < key->key_system_info.size(); i++) {
74 std::vector<uint8_t> pssh_data = key->key_system_info[i].pssh_data();
75 if (!pssh_data.empty()) {
76 std::rotate(pssh_data.begin(),
77 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
79 key->key_system_info[i].set_pssh_data(pssh_data);
87 const std::string& key_id_hex,
88 const std::string& key_hex,
89 const std::string& pssh_boxes_hex,
90 const std::string& iv_hex) {
91 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
93 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
94 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
95 return std::unique_ptr<FixedKeySource>();
96 }
else if (encryption_key->key_id.size() != 16) {
97 LOG(ERROR) <<
"Invalid key ID size '" << encryption_key->key_id.size()
98 <<
"', must be 16 bytes.";
99 return std::unique_ptr<FixedKeySource>();
102 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
103 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
104 return std::unique_ptr<FixedKeySource>();
107 std::vector<uint8_t> pssh_boxes;
108 if (!pssh_boxes_hex.empty() &&
109 !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
110 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_boxes_hex;
111 return std::unique_ptr<FixedKeySource>();
114 if (!iv_hex.empty()) {
115 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
116 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
117 return std::unique_ptr<FixedKeySource>();
122 pssh_boxes.data(), pssh_boxes.size(),
123 &encryption_key->key_system_info)) {
124 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
125 return std::unique_ptr<FixedKeySource>();
129 if (encryption_key->key_system_info.size() == 0) {
131 info.add_key_id(encryption_key->key_id);
132 info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
133 info.set_pssh_box_version(1);
135 encryption_key->key_system_info.push_back(info);
138 return std::unique_ptr<FixedKeySource>(
142 FixedKeySource::FixedKeySource() {}
143 FixedKeySource::FixedKeySource(std::unique_ptr<EncryptionKey> key)
144 : encryption_key_(std::move(key)) {}