Shaka Packager SDK
Loading...
Searching...
No Matches
raw_key_source.cc
1// Copyright 2016 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/base/raw_key_source.h>
8
9#include <algorithm>
10
11#include <absl/log/check.h>
12#include <absl/log/log.h>
13#include <absl/strings/escaping.h>
14
15#include <packager/macros/compiler.h>
16#include <packager/macros/status.h>
17#include <packager/media/base/key_source.h>
18#include <packager/utils/bytes_to_string_view.h>
19
20namespace {
21const char kEmptyDrmLabel[] = "";
22} // namespace
23
24namespace shaka {
25namespace media {
26
27RawKeySource::~RawKeySource() {}
28
29Status RawKeySource::FetchKeys(EmeInitDataType init_data_type,
30 const std::vector<uint8_t>& init_data) {
31 UNUSED(init_data_type);
32 UNUSED(init_data);
33 // Do nothing for raw key encryption/decryption.
34 return Status::OK;
35}
36
37Status RawKeySource::GetKey(const std::string& stream_label,
38 EncryptionKey* key) {
39 DCHECK(key);
40 // Try to find the key with label |stream_label|. If it is not available,
41 // fall back to the default empty label if it is available.
42 auto iter = encryption_key_map_.find(stream_label);
43 if (iter == encryption_key_map_.end()) {
44 iter = encryption_key_map_.find(kEmptyDrmLabel);
45 if (iter == encryption_key_map_.end()) {
46 return Status(error::NOT_FOUND,
47 "Key for '" + stream_label + "' was not found.");
48 }
49 }
50 *key = *iter->second;
51 return Status::OK;
52}
53
54Status RawKeySource::GetKey(const std::vector<uint8_t>& key_id,
55 EncryptionKey* key) {
56 DCHECK(key);
57 for (const auto& pair : encryption_key_map_) {
58 if (pair.second->key_id == key_id) {
59 *key = *pair.second;
60 return Status::OK;
61 }
62 }
63 return Status(error::INTERNAL_ERROR,
64 "Key for key_id=" +
65 absl::BytesToHexString(byte_vector_to_string_view(key_id)) +
66 " was not found.");
67}
68
70 uint32_t crypto_period_index,
71 int32_t crypto_period_duration_in_seconds,
72 const std::string& stream_label,
73 EncryptionKey* key) {
74 UNUSED(crypto_period_duration_in_seconds);
75
76 RETURN_IF_ERROR(GetKey(stream_label, key));
77
78 // A naive key rotation algorithm is implemented here by left rotating the
79 // key, key_id and pssh. Note that this implementation is only intended for
80 // testing purpose. The actual key rotation algorithm can be much more
81 // complicated.
82 LOG(WARNING)
83 << "This naive key rotation algorithm should not be used in production.";
84 std::rotate(key->key_id.begin(),
85 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
86 key->key_id.end());
87 std::rotate(key->key.begin(),
88 key->key.begin() + (crypto_period_index % key->key.size()),
89 key->key.end());
90 key->key_ids.clear();
91 key->key_ids.emplace_back(key->key_id);
92
93 return Status::OK;
94}
95
96std::unique_ptr<RawKeySource> RawKeySource::Create(
97 const RawKeyParams& raw_key) {
98 std::vector<ProtectionSystemSpecificInfo> key_system_info;
99 if (!raw_key.pssh.empty()) {
101 raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) {
102 LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
103 return std::unique_ptr<RawKeySource>();
104 }
105 }
106
107 std::vector<std::vector<uint8_t>> key_ids;
108 for (const auto& entry : raw_key.key_map)
109 key_ids.emplace_back(entry.second.key_id);
110
111 EncryptionKeyMap encryption_key_map;
112 for (const auto& entry : raw_key.key_map) {
113 const std::string& drm_label = entry.first;
114 const RawKeyParams::KeyInfo& key_pair = entry.second;
115
116 if (key_pair.key_id.size() != 16) {
117 LOG(ERROR) << "Invalid key ID size '" << key_pair.key_id.size()
118 << "', must be 16 bytes.";
119 return std::unique_ptr<RawKeySource>();
120 }
121 if (key_pair.key.size() != 16) {
122 // CENC only supports AES-128, i.e. 16 bytes.
123 LOG(ERROR) << "Invalid key size '" << key_pair.key.size()
124 << "', must be 16 bytes.";
125 return std::unique_ptr<RawKeySource>();
126 }
127 if (!key_pair.iv.empty() && key_pair.iv.size() != 8 && key_pair.iv.size() != 16) {
128 LOG(ERROR) << "Invalid IV '" << key_pair.iv.size()
129 << "', must be 8 or 16 bytes.";
130 return std::unique_ptr<RawKeySource>();
131 }
132
133 std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey);
134 encryption_key->key_id = key_pair.key_id;
135 encryption_key->key_ids = key_ids;
136 encryption_key->key = key_pair.key;
137 encryption_key->iv = (key_pair.iv.empty()) ? raw_key.iv : key_pair.iv;
138 encryption_key->key_system_info = key_system_info;
139 encryption_key_map[drm_label] = std::move(encryption_key);
140 }
141
142 return std::unique_ptr<RawKeySource>(
143 new RawKeySource(std::move(encryption_key_map)));
144}
145
146RawKeySource::RawKeySource() {}
147
148RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map)
149 : encryption_key_map_(std::move(encryption_key_map)) {}
150
151} // namespace media
152} // namespace shaka
A key source that uses raw keys for encryption.
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override
Status GetKey(const std::string &stream_label, EncryptionKey *key) override
Status GetCryptoPeriodKey(uint32_t crypto_period_index, int32_t crypto_period_duration_in_seconds, const std::string &stream_label, EncryptionKey *key) override
static std::unique_ptr< RawKeySource > Create(const RawKeyParams &raw_key)
All the methods that are virtual are virtual for mocking.
std::string_view byte_vector_to_string_view(const std::vector< uint8_t > &bytes)
Convert byte vector to string_view.
std::vector< std::vector< uint8_t > > key_ids
The IDs of the other keys to include in PSSH info.
Definition key_source.h:44
std::vector< uint8_t > key_id
The ID of this key.
Definition key_source.h:42
static bool ParseBoxes(const uint8_t *data, size_t data_size, std::vector< ProtectionSystemSpecificInfo > *pssh_boxes)