7 #include "packager/media/base/raw_key_source.h"
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_number_conversions.h"
14 const char kEmptyDrmLabel[] =
"";
20 RawKeySource::~RawKeySource() {}
23 const std::vector<uint8_t>& init_data) {
33 auto iter = encryption_key_map_.find(stream_label);
34 if (iter == encryption_key_map_.end()) {
35 iter = encryption_key_map_.find(kEmptyDrmLabel);
36 if (iter == encryption_key_map_.end()) {
37 return Status(error::NOT_FOUND,
38 "Key for '" + stream_label +
"' was not found.");
48 for (
const auto& pair : encryption_key_map_) {
49 if (pair.second->key_id == key_id) {
54 return Status(error::INTERNAL_ERROR,
55 "Key for key_id=" + base::HexEncode(&key_id[0], key_id.size()) +
60 const std::string& stream_label,
71 <<
"This naive key rotation algorithm should not be used in production.";
72 std::rotate(key->key_id.begin(),
73 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
75 std::rotate(key->key.begin(),
76 key->key.begin() + (crypto_period_index % key->key.size()),
79 for (
auto& key_system : key->key_system_info) {
80 std::vector<uint8_t> pssh_data = key_system.pssh_data();
81 if (!pssh_data.empty()) {
82 std::rotate(pssh_data.begin(),
83 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
85 key_system.set_pssh_data(pssh_data);
91 std::vector<std::vector<uint8_t>> key_ids_copy = key_system.key_ids();
92 key_system.clear_key_ids();
93 for (std::vector<uint8_t>& key_id : key_ids_copy) {
94 std::rotate(key_id.begin(),
95 key_id.begin() + (crypto_period_index % key_id.size()),
97 key_system.add_key_id(key_id);
106 std::vector<ProtectionSystemSpecificInfo> key_system_info;
107 if (!raw_key.
pssh.empty()) {
109 raw_key.
pssh.data(), raw_key.
pssh.size(), &key_system_info)) {
110 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
111 return std::unique_ptr<RawKeySource>();
116 key_system_info.resize(1);
117 for (
const auto& entry : raw_key.
key_map) {
119 key_system_info.back().add_key_id(key_pair.key_id);
121 key_system_info.back().set_system_id(kCommonSystemId,
122 arraysize(kCommonSystemId));
123 key_system_info.back().set_pssh_box_version(1);
126 EncryptionKeyMap encryption_key_map;
127 for (
const auto& entry : raw_key.
key_map) {
128 const std::string& drm_label = entry.first;
131 if (key_pair.key_id.size() != 16) {
132 LOG(ERROR) <<
"Invalid key ID size '" << key_pair.key_id.size()
133 <<
"', must be 16 bytes.";
134 return std::unique_ptr<RawKeySource>();
136 if (key_pair.key.size() != 16) {
138 LOG(ERROR) <<
"Invalid key size '" << key_pair.key.size()
139 <<
"', must be 16 bytes.";
140 return std::unique_ptr<RawKeySource>();
143 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
144 encryption_key->key_id = key_pair.key_id;
145 encryption_key->key = key_pair.key;
146 encryption_key->iv = raw_key.
iv;
147 encryption_key->key_system_info = key_system_info;
148 encryption_key_map[drm_label] = std::move(encryption_key);
151 return std::unique_ptr<RawKeySource>(
155 RawKeySource::RawKeySource() {}
156 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map)
157 : encryption_key_map_(std::move(encryption_key_map)) {}
Raw key encryption/decryption parameters, i.e. with key parameters provided.
std::map< StreamLabel, KeyInfo > key_map
std::vector< uint8_t > iv
std::vector< uint8_t > pssh