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