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