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