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 std::rotate(pssh_data.begin(),
76 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
78 key->key_system_info[i].set_pssh_data(pssh_data);
85 const std::string& key_id_hex,
86 const std::string& key_hex,
87 const std::string& pssh_boxes_hex,
88 const std::string& iv_hex) {
89 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
91 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
92 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
93 return scoped_ptr<FixedKeySource>();
94 }
else if (encryption_key->key_id.size() != 16) {
95 LOG(ERROR) <<
"Invalid key ID size '" << encryption_key->key_id.size()
96 <<
"', must be 16 bytes.";
97 return scoped_ptr<FixedKeySource>();
100 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
101 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
102 return scoped_ptr<FixedKeySource>();
105 std::vector<uint8_t> pssh_boxes;
106 if (!pssh_boxes_hex.empty() &&
107 !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
108 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_boxes_hex;
109 return scoped_ptr<FixedKeySource>();
112 if (!iv_hex.empty()) {
113 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
114 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
115 return scoped_ptr<FixedKeySource>();
120 pssh_boxes.data(), pssh_boxes.size(),
121 &encryption_key->key_system_info)) {
122 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
123 return scoped_ptr<FixedKeySource>();
127 if (encryption_key->key_system_info.size() == 0) {
129 info.add_key_id(encryption_key->key_id);
130 info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
131 info.set_pssh_box_version(1);
133 encryption_key->key_system_info.push_back(info);
136 return scoped_ptr<FixedKeySource>(
new FixedKeySource(encryption_key.Pass()));
139 FixedKeySource::FixedKeySource() {}
140 FixedKeySource::FixedKeySource(scoped_ptr<EncryptionKey> key)
141 : encryption_key_(key.Pass()) {}