7 #include "packager/media/base/fixed_key_source.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/strings/string_number_conversions.h"
12 namespace edash_packager {
19 const uint8_t kCommonSystemId[] = {0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2,
20 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e,
21 0x52, 0xe2, 0xfb, 0x4b};
24 FixedKeySource::~FixedKeySource() {}
32 const std::vector<std::vector<uint8_t>>& key_ids) {
44 DCHECK(encryption_key_);
45 *key = *encryption_key_;
52 DCHECK(encryption_key_);
53 if (key_id != encryption_key_->key_id) {
54 return Status(error::NOT_FOUND,
55 std::string(
"Key for key ID ") +
56 base::HexEncode(&key_id[0], key_id.size()) +
59 *key = *encryption_key_;
67 *key = *encryption_key_;
74 <<
"This naive key rotation algorithm should not be used in production.";
75 std::rotate(key->key_id.begin(),
76 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
78 std::rotate(key->key.begin(),
79 key->key.begin() + (crypto_period_index % key->key.size()),
82 for (
size_t i = 0; i < key->key_system_info.size(); i++) {
83 std::vector<uint8_t> pssh_data = key->key_system_info[i].pssh_data();
84 std::rotate(pssh_data.begin(),
85 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
87 key->key_system_info[i].set_pssh_data(pssh_data);
94 const std::string& key_id_hex,
95 const std::string& key_hex,
96 const std::string& pssh_boxes_hex,
97 const std::string& iv_hex) {
98 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
100 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
101 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
102 return scoped_ptr<FixedKeySource>();
103 }
else if (encryption_key->key_id.size() != 16) {
104 LOG(ERROR) <<
"Invalid key ID size '" << encryption_key->key_id.size()
105 <<
"', must be 16 bytes.";
106 return scoped_ptr<FixedKeySource>();
109 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
110 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
111 return scoped_ptr<FixedKeySource>();
114 std::vector<uint8_t> pssh_boxes;
115 if (!pssh_boxes_hex.empty() &&
116 !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
117 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_boxes_hex;
118 return scoped_ptr<FixedKeySource>();
121 if (!iv_hex.empty()) {
122 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
123 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
124 return scoped_ptr<FixedKeySource>();
129 pssh_boxes.data(), pssh_boxes.size(),
130 &encryption_key->key_system_info)) {
131 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
132 return scoped_ptr<FixedKeySource>();
136 if (encryption_key->key_system_info.size() == 0) {
138 info.add_key_id(encryption_key->key_id);
139 info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
140 info.set_pssh_box_version(1);
142 encryption_key->key_system_info.push_back(info);
145 return scoped_ptr<FixedKeySource>(
new FixedKeySource(encryption_key.Pass()));
148 FixedKeySource::FixedKeySource() {}
149 FixedKeySource::FixedKeySource(scoped_ptr<EncryptionKey> key)
150 : encryption_key_(key.Pass()) {}