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  if (!pssh_data.empty()) {
76  std::rotate(pssh_data.begin(),
77  pssh_data.begin() + (crypto_period_index % pssh_data.size()),
78  pssh_data.end());
79  key->key_system_info[i].set_pssh_data(pssh_data);
80  }
81  }
82 
83  return Status::OK;
84 }
85 
86 scoped_ptr<FixedKeySource> FixedKeySource::CreateFromHexStrings(
87  const std::string& key_id_hex,
88  const std::string& key_hex,
89  const std::string& pssh_boxes_hex,
90  const std::string& iv_hex) {
91  scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
92 
93  if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
94  LOG(ERROR) << "Cannot parse key_id_hex " << key_id_hex;
95  return scoped_ptr<FixedKeySource>();
96  } else if (encryption_key->key_id.size() != 16) {
97  LOG(ERROR) << "Invalid key ID size '" << encryption_key->key_id.size()
98  << "', must be 16 bytes.";
99  return scoped_ptr<FixedKeySource>();
100  }
101 
102  if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
103  LOG(ERROR) << "Cannot parse key_hex " << key_hex;
104  return scoped_ptr<FixedKeySource>();
105  }
106 
107  std::vector<uint8_t> pssh_boxes;
108  if (!pssh_boxes_hex.empty() &&
109  !base::HexStringToBytes(pssh_boxes_hex, &pssh_boxes)) {
110  LOG(ERROR) << "Cannot parse pssh_hex " << pssh_boxes_hex;
111  return scoped_ptr<FixedKeySource>();
112  }
113 
114  if (!iv_hex.empty()) {
115  if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
116  LOG(ERROR) << "Cannot parse iv_hex " << iv_hex;
117  return scoped_ptr<FixedKeySource>();
118  }
119  }
120 
122  pssh_boxes.data(), pssh_boxes.size(),
123  &encryption_key->key_system_info)) {
124  LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
125  return scoped_ptr<FixedKeySource>();
126  }
127 
128  // If there aren't any PSSH boxes given, create one with the common system ID.
129  if (encryption_key->key_system_info.size() == 0) {
131  info.add_key_id(encryption_key->key_id);
132  info.set_system_id(kCommonSystemId, arraysize(kCommonSystemId));
133  info.set_pssh_box_version(1);
134 
135  encryption_key->key_system_info.push_back(info);
136  }
137 
138  return scoped_ptr<FixedKeySource>(new FixedKeySource(encryption_key.Pass()));
139 }
140 
141 FixedKeySource::FixedKeySource() {}
142 FixedKeySource::FixedKeySource(scoped_ptr<EncryptionKey> key)
143  : encryption_key_(key.Pass()) {}
144 
145 } // namespace media
146 } // 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