2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-02-19 20:23:37 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/raw_key_source.h>
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2016-08-17 21:42:23 +00:00
|
|
|
#include <algorithm>
|
2022-11-04 22:46:41 +00:00
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
#include <glog/logging.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/key_source.h>
|
|
|
|
#include <packager/status/status_macros.h>
|
|
|
|
#include <packager/utils/bytes_to_string_view.h>
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
namespace {
|
|
|
|
const char kEmptyDrmLabel[] = "";
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-02-19 20:23:37 +00:00
|
|
|
namespace media {
|
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
RawKeySource::~RawKeySource() {}
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
Status RawKeySource::FetchKeys(EmeInitDataType init_data_type,
|
|
|
|
const std::vector<uint8_t>& init_data) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(init_data_type);
|
|
|
|
UNUSED(init_data);
|
2017-10-17 23:03:08 +00:00
|
|
|
// Do nothing for raw key encryption/decryption.
|
2016-02-19 20:23:37 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
Status RawKeySource::GetKey(const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2016-02-19 20:23:37 +00:00
|
|
|
DCHECK(key);
|
2017-09-20 22:49:00 +00:00
|
|
|
// Try to find the key with label |stream_label|. If it is not available,
|
|
|
|
// fall back to the default empty label if it is available.
|
|
|
|
auto iter = encryption_key_map_.find(stream_label);
|
|
|
|
if (iter == encryption_key_map_.end()) {
|
|
|
|
iter = encryption_key_map_.find(kEmptyDrmLabel);
|
|
|
|
if (iter == encryption_key_map_.end()) {
|
|
|
|
return Status(error::NOT_FOUND,
|
|
|
|
"Key for '" + stream_label + "' was not found.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*key = *iter->second;
|
2016-02-19 20:23:37 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
Status RawKeySource::GetKey(const std::vector<uint8_t>& key_id,
|
|
|
|
EncryptionKey* key) {
|
2016-02-19 20:23:37 +00:00
|
|
|
DCHECK(key);
|
2017-09-20 22:49:00 +00:00
|
|
|
for (const auto& pair : encryption_key_map_) {
|
|
|
|
if (pair.second->key_id == key_id) {
|
|
|
|
*key = *pair.second;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
2017-09-20 22:49:00 +00:00
|
|
|
return Status(error::INTERNAL_ERROR,
|
2022-12-16 00:53:36 +00:00
|
|
|
"Key for key_id=" +
|
|
|
|
absl::BytesToHexString(byte_vector_to_string_view(key_id)) +
|
2017-09-20 22:49:00 +00:00
|
|
|
" was not found.");
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
Status RawKeySource::GetCryptoPeriodKey(
|
|
|
|
uint32_t crypto_period_index,
|
|
|
|
int32_t crypto_period_duration_in_seconds,
|
|
|
|
const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2022-11-04 22:46:41 +00:00
|
|
|
UNUSED(crypto_period_duration_in_seconds);
|
|
|
|
|
2018-05-03 17:32:01 +00:00
|
|
|
RETURN_IF_ERROR(GetKey(stream_label, key));
|
2016-02-19 20:23:37 +00:00
|
|
|
|
|
|
|
// A naive key rotation algorithm is implemented here by left rotating the
|
|
|
|
// key, key_id and pssh. Note that this implementation is only intended for
|
|
|
|
// testing purpose. The actual key rotation algorithm can be much more
|
|
|
|
// complicated.
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "This naive key rotation algorithm should not be used in production.";
|
|
|
|
std::rotate(key->key_id.begin(),
|
|
|
|
key->key_id.begin() + (crypto_period_index % key->key_id.size()),
|
|
|
|
key->key_id.end());
|
|
|
|
std::rotate(key->key.begin(),
|
|
|
|
key->key.begin() + (crypto_period_index % key->key.size()),
|
|
|
|
key->key.end());
|
2020-06-01 22:26:31 +00:00
|
|
|
key->key_ids.clear();
|
|
|
|
key->key_ids.emplace_back(key->key_id);
|
2016-02-19 20:23:37 +00:00
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:45:57 +00:00
|
|
|
std::unique_ptr<RawKeySource> RawKeySource::Create(
|
2020-06-01 22:26:31 +00:00
|
|
|
const RawKeyParams& raw_key) {
|
2017-09-20 22:49:00 +00:00
|
|
|
std::vector<ProtectionSystemSpecificInfo> key_system_info;
|
|
|
|
if (!raw_key.pssh.empty()) {
|
|
|
|
if (!ProtectionSystemSpecificInfo::ParseBoxes(
|
|
|
|
raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) {
|
|
|
|
LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
|
2017-10-17 23:03:08 +00:00
|
|
|
return std::unique_ptr<RawKeySource>();
|
2017-09-20 22:49:00 +00:00
|
|
|
}
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 22:26:31 +00:00
|
|
|
std::vector<std::vector<uint8_t>> key_ids;
|
|
|
|
for (const auto& entry : raw_key.key_map)
|
|
|
|
key_ids.emplace_back(entry.second.key_id);
|
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
EncryptionKeyMap encryption_key_map;
|
|
|
|
for (const auto& entry : raw_key.key_map) {
|
|
|
|
const std::string& drm_label = entry.first;
|
|
|
|
const RawKeyParams::KeyInfo& key_pair = entry.second;
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
if (key_pair.key_id.size() != 16) {
|
|
|
|
LOG(ERROR) << "Invalid key ID size '" << key_pair.key_id.size()
|
|
|
|
<< "', must be 16 bytes.";
|
2017-10-17 23:03:08 +00:00
|
|
|
return std::unique_ptr<RawKeySource>();
|
2017-09-20 22:49:00 +00:00
|
|
|
}
|
|
|
|
if (key_pair.key.size() != 16) {
|
|
|
|
// CENC only supports AES-128, i.e. 16 bytes.
|
|
|
|
LOG(ERROR) << "Invalid key size '" << key_pair.key.size()
|
|
|
|
<< "', must be 16 bytes.";
|
2017-10-17 23:03:08 +00:00
|
|
|
return std::unique_ptr<RawKeySource>();
|
2017-09-20 22:49:00 +00:00
|
|
|
}
|
2020-06-26 03:37:50 +00:00
|
|
|
if (!key_pair.iv.empty() && key_pair.iv.size() != 8 && key_pair.iv.size() != 16) {
|
|
|
|
LOG(ERROR) << "Invalid IV '" << key_pair.iv.size()
|
|
|
|
<< "', must be 8 or 16 bytes.";
|
|
|
|
return std::unique_ptr<RawKeySource>();
|
|
|
|
}
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey);
|
|
|
|
encryption_key->key_id = key_pair.key_id;
|
2020-06-01 22:26:31 +00:00
|
|
|
encryption_key->key_ids = key_ids;
|
2017-09-20 22:49:00 +00:00
|
|
|
encryption_key->key = key_pair.key;
|
2020-06-26 03:37:50 +00:00
|
|
|
encryption_key->iv = (key_pair.iv.empty()) ? raw_key.iv : key_pair.iv;
|
2017-09-20 22:49:00 +00:00
|
|
|
encryption_key->key_system_info = key_system_info;
|
|
|
|
encryption_key_map[drm_label] = std::move(encryption_key);
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 22:26:31 +00:00
|
|
|
return std::unique_ptr<RawKeySource>(
|
|
|
|
new RawKeySource(std::move(encryption_key_map)));
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 22:26:31 +00:00
|
|
|
RawKeySource::RawKeySource() {}
|
2018-08-07 21:43:42 +00:00
|
|
|
|
2020-06-01 22:26:31 +00:00
|
|
|
RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map)
|
|
|
|
: encryption_key_map_(std::move(encryption_key_map)) {}
|
2016-02-19 20:23:37 +00:00
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|