Shaka Packager SDK
raw_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/raw_key_source.h"
8 
9 #include <algorithm>
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_number_conversions.h"
12 #include "packager/media/base/key_source.h"
13 #include "packager/media/base/raw_key_pssh_generator.h"
14 #include "packager/status_macros.h"
15 
16 namespace {
17 const char kEmptyDrmLabel[] = "";
18 } // namespace
19 
20 namespace shaka {
21 namespace media {
22 
23 RawKeySource::~RawKeySource() {}
24 
25 Status RawKeySource::FetchKeys(EmeInitDataType init_data_type,
26  const std::vector<uint8_t>& init_data) {
27  // Do nothing for raw key encryption/decryption.
28  return Status::OK;
29 }
30 
31 Status RawKeySource::GetKey(const std::string& stream_label,
32  EncryptionKey* key) {
33  DCHECK(key);
34  // Try to find the key with label |stream_label|. If it is not available,
35  // fall back to the default empty label if it is available.
36  auto iter = encryption_key_map_.find(stream_label);
37  if (iter == encryption_key_map_.end()) {
38  iter = encryption_key_map_.find(kEmptyDrmLabel);
39  if (iter == encryption_key_map_.end()) {
40  return Status(error::NOT_FOUND,
41  "Key for '" + stream_label + "' was not found.");
42  }
43  }
44  *key = *iter->second;
45  return Status::OK;
46 }
47 
48 Status RawKeySource::GetKey(const std::vector<uint8_t>& key_id,
49  EncryptionKey* key) {
50  DCHECK(key);
51  for (const auto& pair : encryption_key_map_) {
52  if (pair.second->key_id == key_id) {
53  *key = *pair.second;
54  return Status::OK;
55  }
56  }
57  return Status(error::INTERNAL_ERROR,
58  "Key for key_id=" + base::HexEncode(&key_id[0], key_id.size()) +
59  " was not found.");
60 }
61 
62 Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
63  const std::string& stream_label,
64  EncryptionKey* key) {
65  RETURN_IF_ERROR(GetKey(stream_label, key));
66 
67  // A naive key rotation algorithm is implemented here by left rotating the
68  // key, key_id and pssh. Note that this implementation is only intended for
69  // testing purpose. The actual key rotation algorithm can be much more
70  // complicated.
71  LOG(WARNING)
72  << "This naive key rotation algorithm should not be used in production.";
73  std::rotate(key->key_id.begin(),
74  key->key_id.begin() + (crypto_period_index % key->key_id.size()),
75  key->key_id.end());
76  std::rotate(key->key.begin(),
77  key->key.begin() + (crypto_period_index % key->key.size()),
78  key->key.end());
79 
80  // Clear |key->key_system_info| to prepare for update. The original
81  // |key_system_info| is saved as it may still be useful later.
82  std::vector<ProtectionSystemSpecificInfo> original_key_system_info;
83  key->key_system_info.swap(original_key_system_info);
84 
85  EncryptionKeyMap encryption_key_map;
86  encryption_key_map[stream_label].reset(new EncryptionKey(*key));
87  RETURN_IF_ERROR(UpdateProtectionSystemInfo(&encryption_key_map));
88  key->key_system_info = encryption_key_map[stream_label]->key_system_info;
89 
90  // It is possible that the generated |key_system_info| is empty. This happens
91  // when RawKeyParams.pssh is specified. Restore the original key system info
92  // in this case.
93  if (key->key_system_info.empty())
94  key->key_system_info.swap(original_key_system_info);
95 
96  return Status::OK;
97 }
98 
99 std::unique_ptr<RawKeySource> RawKeySource::Create(const RawKeyParams& raw_key,
100  int protection_systems_flags,
101  FourCC protection_scheme) {
102  std::vector<ProtectionSystemSpecificInfo> key_system_info;
103  bool pssh_provided = false;
104  if (!raw_key.pssh.empty()) {
105  pssh_provided = true;
107  raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) {
108  LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
109  return std::unique_ptr<RawKeySource>();
110  }
111  }
112 
113  EncryptionKeyMap encryption_key_map;
114  for (const auto& entry : raw_key.key_map) {
115  const std::string& drm_label = entry.first;
116  const RawKeyParams::KeyInfo& key_pair = entry.second;
117 
118  if (key_pair.key_id.size() != 16) {
119  LOG(ERROR) << "Invalid key ID size '" << key_pair.key_id.size()
120  << "', must be 16 bytes.";
121  return std::unique_ptr<RawKeySource>();
122  }
123  if (key_pair.key.size() != 16) {
124  // CENC only supports AES-128, i.e. 16 bytes.
125  LOG(ERROR) << "Invalid key size '" << key_pair.key.size()
126  << "', must be 16 bytes.";
127  return std::unique_ptr<RawKeySource>();
128  }
129 
130  std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey);
131  encryption_key->key_id = key_pair.key_id;
132  encryption_key->key = key_pair.key;
133  encryption_key->iv = raw_key.iv;
134  encryption_key->key_system_info = key_system_info;
135  encryption_key_map[drm_label] = std::move(encryption_key);
136  }
137 
138  // Generate common protection system if no other protection system is
139  // specified.
140  if (!pssh_provided && protection_systems_flags == NO_PROTECTION_SYSTEM_FLAG) {
141  protection_systems_flags = COMMON_PROTECTION_SYSTEM_FLAG;
142  }
143 
144  return std::unique_ptr<RawKeySource>(
145  new RawKeySource(std::move(encryption_key_map), protection_systems_flags,
146  protection_scheme));
147 }
148 
149 RawKeySource::RawKeySource()
150  : KeySource(NO_PROTECTION_SYSTEM_FLAG, FOURCC_NULL) {}
151 
152 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map,
153  int protection_systems_flags,
154  FourCC protection_scheme)
155  : KeySource(protection_systems_flags, protection_scheme),
156  encryption_key_map_(std::move(encryption_key_map)) {
157  UpdateProtectionSystemInfo(&encryption_key_map_);
158 }
159 
160 } // namespace media
161 } // namespace shaka
Status GetCryptoPeriodKey(uint32_t crypto_period_index, const std::string &stream_label, EncryptionKey *key) override
static std::unique_ptr< RawKeySource > Create(const RawKeyParams &raw_key, int protection_system_flags, FourCC protection_scheme)
All the methods that are virtual are virtual for mocking.
Status UpdateProtectionSystemInfo(EncryptionKeyMap *encryption_key_map)
Definition: key_source.cc:39
Raw key encryption/decryption parameters, i.e. with key parameters provided.
Definition: crypto_params.h:86
std::map< StreamLabel, KeyInfo > key_map
std::vector< uint8_t > iv
Definition: crypto_params.h:90
A key source that uses raw keys for encryption.
Status GetKey(const std::string &stream_label, EncryptionKey *key) override
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:48
std::vector< uint8_t > pssh
Definition: crypto_params.h:94
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override
static bool ParseBoxes(const uint8_t *data, size_t data_size, std::vector< ProtectionSystemSpecificInfo > *pssh_boxes)