DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
fixed_key_source.cc
1 // Copyright 2016 Google Inc. 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/fixed_key_source.h"
8 
9 #include <algorithm>
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_number_conversions.h"
12 
13 namespace shaka {
14 namespace media {
15 
16 FixedKeySource::~FixedKeySource() {}
17 
18 Status FixedKeySource::FetchKeys(EmeInitDataType init_data_type,
19  const std::vector<uint8_t>& init_data) {
20  // Do nothing for fixed key encryption/decryption.
21  return Status::OK;
22 }
23 
24 Status FixedKeySource::GetKey(const std::string& stream_label,
25  EncryptionKey* key) {
26  DCHECK(key);
27  DCHECK(encryption_key_);
28  *key = *encryption_key_;
29  return Status::OK;
30 }
31 
32 Status FixedKeySource::GetKey(const std::vector<uint8_t>& key_id,
33  EncryptionKey* key) {
34  DCHECK(key);
35  DCHECK(encryption_key_);
36  if (key_id != encryption_key_->key_id) {
37  return Status(error::NOT_FOUND,
38  std::string("Key for key ID ") +
39  base::HexEncode(&key_id[0], key_id.size()) +
40  " was not found.");
41  }
42  *key = *encryption_key_;
43  return Status::OK;
44 }
45 
46 Status FixedKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
47  const std::string& stream_label,
48  EncryptionKey* key) {
49  // Create a copy of the key.
50  *key = *encryption_key_;
51 
52  // A naive key rotation algorithm is implemented here by left rotating the
53  // key, key_id and pssh. Note that this implementation is only intended for
54  // testing purpose. The actual key rotation algorithm can be much more
55  // complicated.
56  LOG(WARNING)
57  << "This naive key rotation algorithm should not be used in production.";
58  std::rotate(key->key_id.begin(),
59  key->key_id.begin() + (crypto_period_index % key->key_id.size()),
60  key->key_id.end());
61  std::rotate(key->key.begin(),
62  key->key.begin() + (crypto_period_index % key->key.size()),
63  key->key.end());
64 
65  for (size_t i = 0; i < key->key_system_info.size(); i++) {
66  std::vector<uint8_t> pssh_data = key->key_system_info[i].pssh_data();
67  if (!pssh_data.empty()) {
68  std::rotate(pssh_data.begin(),
69  pssh_data.begin() + (crypto_period_index % pssh_data.size()),
70  pssh_data.end());
71  key->key_system_info[i].set_pssh_data(pssh_data);
72  }
73  }
74 
75  return Status::OK;
76 }
77 
78 std::unique_ptr<FixedKeySource> FixedKeySource::Create(
79  const std::vector<uint8_t>& key_id,
80  const std::vector<uint8_t>& key,
81  const std::vector<uint8_t>& pssh_boxes,
82  const std::vector<uint8_t>& iv) {
83  std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey());
84 
85  if (key_id.size() != 16) {
86  LOG(ERROR) << "Invalid key ID size '" << key_id.size()
87  << "', must be 16 bytes.";
88  return std::unique_ptr<FixedKeySource>();
89  }
90  if (key.size() != 16) {
91  // CENC only supports AES-128, i.e. 16 bytes.
92  LOG(ERROR) << "Invalid key size '" << key.size() << "', must be 16 bytes.";
93  return std::unique_ptr<FixedKeySource>();
94  }
95 
96  encryption_key->key_id = key_id;
97  encryption_key->key = key;
98  encryption_key->iv = iv;
99 
101  pssh_boxes.data(), pssh_boxes.size(),
102  &encryption_key->key_system_info)) {
103  LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
104  return std::unique_ptr<FixedKeySource>();
105  }
106 
107  // If there aren't any PSSH boxes given, create one with the common system ID.
108  if (encryption_key->key_system_info.size() == 0) {
110  info.add_key_id(encryption_key->key_id);
111  info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
112  info.set_pssh_box_version(1);
113 
114  encryption_key->key_system_info.push_back(info);
115  }
116 
117  return std::unique_ptr<FixedKeySource>(
118  new FixedKeySource(std::move(encryption_key)));
119 }
120 
121 FixedKeySource::FixedKeySource() {}
122 FixedKeySource::FixedKeySource(std::unique_ptr<EncryptionKey> key)
123  : encryption_key_(std::move(key)) {}
124 
125 } // namespace media
126 } // namespace shaka
Status GetCryptoPeriodKey(uint32_t crypto_period_index, const std::string &stream_label, EncryptionKey *key) override
Status GetKey(const std::string &stream_label, EncryptionKey *key) override
static bool ParseBoxes(const uint8_t *data, size_t data_size, std::vector< ProtectionSystemSpecificInfo > *pssh_boxes)
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override
A key source that uses fixed keys for encryption.
static std::unique_ptr< FixedKeySource > Create(const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &key, const std::vector< uint8_t > &pssh_boxes, const std::vector< uint8_t > &iv)