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