DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
encryption_handler.cc
1 // Copyright 2017 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/crypto/encryption_handler.h"
8 
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include <limits>
13 
14 #include "packager/media/base/aes_encryptor.h"
15 #include "packager/media/base/aes_pattern_cryptor.h"
16 #include "packager/media/base/key_source.h"
17 #include "packager/media/base/media_sample.h"
18 #include "packager/media/base/video_stream_info.h"
19 #include "packager/media/codecs/video_slice_header_parser.h"
20 #include "packager/media/codecs/vp8_parser.h"
21 #include "packager/media/codecs/vp9_parser.h"
22 
23 namespace shaka {
24 namespace media {
25 
26 namespace {
27 const size_t kCencBlockSize = 16u;
28 
29 // The default KID for key rotation is all 0s.
30 const uint8_t kKeyRotationDefaultKeyId[] = {
31  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 };
33 
34 // Adds one or more subsamples to |*decrypt_config|. This may add more than one
35 // if one of the values overflows the integer in the subsample.
36 void AddSubsample(uint64_t clear_bytes,
37  uint64_t cipher_bytes,
38  DecryptConfig* decrypt_config) {
39  CHECK_LT(cipher_bytes, std::numeric_limits<uint32_t>::max());
40  const uint64_t kUInt16Max = std::numeric_limits<uint16_t>::max();
41  while (clear_bytes > kUInt16Max) {
42  decrypt_config->AddSubsample(kUInt16Max, 0);
43  clear_bytes -= kUInt16Max;
44  }
45 
46  if (clear_bytes > 0 || cipher_bytes > 0)
47  decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
48 }
49 
50 uint8_t GetNaluLengthSize(const StreamInfo& stream_info) {
51  if (stream_info.stream_type() != kStreamVideo)
52  return 0;
53 
54  const VideoStreamInfo& video_stream_info =
55  static_cast<const VideoStreamInfo&>(stream_info);
56  return video_stream_info.nalu_length_size();
57 }
58 
59 std::string GetStreamLabelForEncryption(
60  const StreamInfo& stream_info,
61  const std::function<std::string(
62  const EncryptionParams::EncryptedStreamAttributes& stream_attributes)>&
63  stream_label_func) {
64  EncryptionParams::EncryptedStreamAttributes stream_attributes;
65  if (stream_info.stream_type() == kStreamAudio) {
66  stream_attributes.stream_type =
67  EncryptionParams::EncryptedStreamAttributes::kAudio;
68  } else if (stream_info.stream_type() == kStreamVideo) {
69  const VideoStreamInfo& video_stream_info =
70  static_cast<const VideoStreamInfo&>(stream_info);
71  stream_attributes.stream_type =
72  EncryptionParams::EncryptedStreamAttributes::kVideo;
73  stream_attributes.oneof.video.width = video_stream_info.width();
74  stream_attributes.oneof.video.height = video_stream_info.height();
75  }
76  return stream_label_func(stream_attributes);
77 }
78 } // namespace
79 
80 EncryptionHandler::EncryptionHandler(const EncryptionParams& encryption_params,
81  KeySource* key_source)
82  : encryption_params_(encryption_params),
83  protection_scheme_(
84  static_cast<FourCC>(encryption_params.protection_scheme)),
85  key_source_(key_source) {}
86 
87 EncryptionHandler::~EncryptionHandler() {}
88 
90  if (!encryption_params_.stream_label_func) {
91  return Status(error::INVALID_ARGUMENT, "Stream label function not set.");
92  }
93  if (num_input_streams() != 1 || next_output_stream_index() != 1) {
94  return Status(error::INVALID_ARGUMENT,
95  "Expects exactly one input and output.");
96  }
97  return Status::OK;
98 }
99 
100 Status EncryptionHandler::Process(std::unique_ptr<StreamData> stream_data) {
101  Status status;
102  switch (stream_data->stream_data_type) {
103  case StreamDataType::kStreamInfo:
104  status = ProcessStreamInfo(stream_data->stream_info.get());
105  break;
106  case StreamDataType::kSegmentInfo: {
107  SegmentInfo* segment_info = stream_data->segment_info.get();
108  segment_info->is_encrypted = remaining_clear_lead_ <= 0;
109 
110  const bool key_rotation_enabled = crypto_period_duration_ != 0;
111  if (key_rotation_enabled)
112  segment_info->key_rotation_encryption_config = encryption_config_;
113  if (!segment_info->is_subsegment) {
114  if (key_rotation_enabled)
115  check_new_crypto_period_ = true;
116  if (remaining_clear_lead_ > 0)
117  remaining_clear_lead_ -= segment_info->duration;
118  }
119  break;
120  }
121  case StreamDataType::kMediaSample:
122  status = ProcessMediaSample(stream_data->media_sample.get());
123  break;
124  default:
125  VLOG(3) << "Stream data type "
126  << static_cast<int>(stream_data->stream_data_type) << " ignored.";
127  break;
128  }
129  return status.ok() ? Dispatch(std::move(stream_data)) : status;
130 }
131 
132 Status EncryptionHandler::ProcessStreamInfo(StreamInfo* stream_info) {
133  if (stream_info->is_encrypted()) {
134  return Status(error::INVALID_ARGUMENT,
135  "Input stream is already encrypted.");
136  }
137 
138  remaining_clear_lead_ =
139  encryption_params_.clear_lead_in_seconds * stream_info->time_scale();
140  crypto_period_duration_ =
141  encryption_params_.crypto_period_duration_in_seconds *
142  stream_info->time_scale();
143  codec_ = stream_info->codec();
144  nalu_length_size_ = GetNaluLengthSize(*stream_info);
145  stream_label_ = GetStreamLabelForEncryption(
146  *stream_info, encryption_params_.stream_label_func);
147  switch (codec_) {
148  case kCodecVP9:
149  if (encryption_params_.vp9_subsample_encryption)
150  vpx_parser_.reset(new VP9Parser);
151  break;
152  case kCodecH264:
153  header_parser_.reset(new H264VideoSliceHeaderParser);
154  break;
155  case kCodecH265:
156  header_parser_.reset(new H265VideoSliceHeaderParser);
157  break;
158  default:
159  // Other codecs should have nalu length size == 0.
160  if (nalu_length_size_ > 0) {
161  LOG(WARNING) << "Unknown video codec '" << codec_ << "'";
162  return Status(error::ENCRYPTION_FAILURE, "Unknown video codec.");
163  }
164  }
165  if (header_parser_) {
166  CHECK_NE(nalu_length_size_, 0u) << "AnnexB stream is not supported yet";
167  if (!header_parser_->Initialize(stream_info->codec_config())) {
168  return Status(error::ENCRYPTION_FAILURE,
169  "Fail to read SPS and PPS data.");
170  }
171  }
172 
173  Status status = SetupProtectionPattern(stream_info->stream_type());
174  if (!status.ok())
175  return status;
176 
177  EncryptionKey encryption_key;
178  const bool key_rotation_enabled = crypto_period_duration_ != 0;
179  if (key_rotation_enabled) {
180  check_new_crypto_period_ = true;
181  // Setup dummy key id and key to signal encryption for key rotation.
182  encryption_key.key_id.assign(
183  kKeyRotationDefaultKeyId,
184  kKeyRotationDefaultKeyId + sizeof(kKeyRotationDefaultKeyId));
185  // The key is not really used to encrypt any data. It is there just for
186  // convenience.
187  encryption_key.key = encryption_key.key_id;
188  } else {
189  status = key_source_->GetKey(stream_label_, &encryption_key);
190  if (!status.ok())
191  return status;
192  }
193  if (!CreateEncryptor(encryption_key))
194  return Status(error::ENCRYPTION_FAILURE, "Failed to create encryptor");
195 
196  stream_info->set_is_encrypted(true);
197  stream_info->set_has_clear_lead(encryption_params_.clear_lead_in_seconds > 0);
198  stream_info->set_encryption_config(*encryption_config_);
199  return Status::OK;
200 }
201 
202 Status EncryptionHandler::ProcessMediaSample(MediaSample* sample) {
203  // We need to parse the frame (which also updates the vpx parser) even if the
204  // frame is not encrypted as the next (encrypted) frame may be dependent on
205  // this clear frame.
206  std::vector<VPxFrameInfo> vpx_frames;
207  if (vpx_parser_ &&
208  !vpx_parser_->Parse(sample->data(), sample->data_size(), &vpx_frames)) {
209  return Status(error::ENCRYPTION_FAILURE, "Failed to parse vpx frame.");
210  }
211 
212  // Need to setup the encryptor for new segments even if this segment does not
213  // need to be encrypted, so we can signal encryption metadata earlier to
214  // allows clients to prefetch the keys.
215  if (check_new_crypto_period_) {
216  const int64_t current_crypto_period_index =
217  sample->dts() / crypto_period_duration_;
218  if (current_crypto_period_index != prev_crypto_period_index_) {
219  EncryptionKey encryption_key;
220  Status status = key_source_->GetCryptoPeriodKey(
221  current_crypto_period_index, stream_label_, &encryption_key);
222  if (!status.ok())
223  return status;
224  if (!CreateEncryptor(encryption_key))
225  return Status(error::ENCRYPTION_FAILURE, "Failed to create encryptor");
226  prev_crypto_period_index_ = current_crypto_period_index;
227  }
228  check_new_crypto_period_ = false;
229  }
230 
231  if (remaining_clear_lead_ > 0)
232  return Status::OK;
233 
234  std::unique_ptr<DecryptConfig> decrypt_config(
235  new DecryptConfig(encryption_config_->key_id, encryptor_->iv(),
236  std::vector<SubsampleEntry>(), protection_scheme_,
237  crypt_byte_block_, skip_byte_block_));
238  bool result = true;
239  if (vpx_parser_) {
240  result = EncryptVpxFrame(vpx_frames, sample, decrypt_config.get());
241  if (result) {
242  DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
243  sample->data_size());
244  }
245  } else if (header_parser_) {
246  result = EncryptNalFrame(sample, decrypt_config.get());
247  if (result) {
248  DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
249  sample->data_size());
250  }
251  } else {
252  if (sample->data_size() > leading_clear_bytes_size_) {
253  EncryptBytes(sample->writable_data() + leading_clear_bytes_size_,
254  sample->data_size() - leading_clear_bytes_size_);
255  }
256  }
257  if (!result)
258  return Status(error::ENCRYPTION_FAILURE, "Failed to encrypt samples.");
259  sample->set_is_encrypted(true);
260  sample->set_decrypt_config(std::move(decrypt_config));
261  encryptor_->UpdateIv();
262  return Status::OK;
263 }
264 
265 Status EncryptionHandler::SetupProtectionPattern(StreamType stream_type) {
266  switch (protection_scheme_) {
267  case kAppleSampleAesProtectionScheme: {
268  const size_t kH264LeadingClearBytesSize = 32u;
269  const size_t kSmallNalUnitSize = 32u + 16u;
270  const size_t kAudioLeadingClearBytesSize = 16u;
271  switch (codec_) {
272  case kCodecH264:
273  // Apple Sample AES uses 1:9 pattern for video.
274  crypt_byte_block_ = 1u;
275  skip_byte_block_ = 9u;
276  leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
277  min_protected_data_size_ = kSmallNalUnitSize + 1u;
278  break;
279  case kCodecAAC:
280  FALLTHROUGH_INTENDED;
281  case kCodecAC3:
282  // Audio is whole sample encrypted. We could not use a
283  // crypto_byte_block_ of 1 here as if there is one crypto block
284  // remaining, it need not be encrypted for video but it needs to be
285  // encrypted for audio.
286  crypt_byte_block_ = 0u;
287  skip_byte_block_ = 0u;
288  leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
289  min_protected_data_size_ = leading_clear_bytes_size_ + 1u;
290  break;
291  default:
292  return Status(error::ENCRYPTION_FAILURE,
293  "Only AAC/AC3 and H264 are supported in Sample AES.");
294  }
295  break;
296  }
297  case FOURCC_cbcs:
298  FALLTHROUGH_INTENDED;
299  case FOURCC_cens:
300  if (stream_type == kStreamVideo) {
301  // Use 1:9 pattern for video.
302  crypt_byte_block_ = 1u;
303  skip_byte_block_ = 9u;
304  } else {
305  // Tracks other than video are protected using whole-block full-sample
306  // encryption, which is essentially a pattern of 1:0. Note that this may
307  // not be the same as the non-pattern based encryption counterparts,
308  // e.g. in 'cens' for full sample encryption, the whole sample is
309  // encrypted up to the last 16-byte boundary, see 23001-7:2016(E) 9.7;
310  // while in 'cenc' for full sample encryption, the last partial 16-byte
311  // block is also encrypted, see 23001-7:2016(E) 9.4.2. Another
312  // difference is the use of constant iv.
313  crypt_byte_block_ = 1u;
314  skip_byte_block_ = 0u;
315  }
316  break;
317  default:
318  // Not using pattern encryption.
319  crypt_byte_block_ = 0u;
320  skip_byte_block_ = 0u;
321  }
322  return Status::OK;
323 }
324 
325 bool EncryptionHandler::CreateEncryptor(const EncryptionKey& encryption_key) {
326  std::unique_ptr<AesCryptor> encryptor;
327  switch (protection_scheme_) {
328  case FOURCC_cenc:
329  encryptor.reset(new AesCtrEncryptor);
330  break;
331  case FOURCC_cbc1:
332  encryptor.reset(new AesCbcEncryptor(kNoPadding));
333  break;
334  case FOURCC_cens:
335  encryptor.reset(new AesPatternCryptor(
336  crypt_byte_block_, skip_byte_block_,
338  AesCryptor::kDontUseConstantIv,
339  std::unique_ptr<AesCryptor>(new AesCtrEncryptor())));
340  break;
341  case FOURCC_cbcs:
342  encryptor.reset(new AesPatternCryptor(
343  crypt_byte_block_, skip_byte_block_,
345  AesCryptor::kUseConstantIv,
346  std::unique_ptr<AesCryptor>(new AesCbcEncryptor(kNoPadding))));
347  break;
348  case kAppleSampleAesProtectionScheme:
349  if (crypt_byte_block_ == 0 && skip_byte_block_ == 0) {
350  encryptor.reset(
351  new AesCbcEncryptor(kNoPadding, AesCryptor::kUseConstantIv));
352  } else {
353  encryptor.reset(new AesPatternCryptor(
354  crypt_byte_block_, skip_byte_block_,
356  AesCryptor::kUseConstantIv,
357  std::unique_ptr<AesCryptor>(new AesCbcEncryptor(kNoPadding))));
358  }
359  break;
360  default:
361  LOG(ERROR) << "Unsupported protection scheme.";
362  return false;
363  }
364 
365  std::vector<uint8_t> iv = encryption_key.iv;
366  if (iv.empty()) {
367  if (!AesCryptor::GenerateRandomIv(protection_scheme_, &iv)) {
368  LOG(ERROR) << "Failed to generate random iv.";
369  return false;
370  }
371  }
372  const bool initialized =
373  encryptor->InitializeWithIv(encryption_key.key, iv);
374  encryptor_ = std::move(encryptor);
375 
376  encryption_config_.reset(new EncryptionConfig);
377  encryption_config_->protection_scheme = protection_scheme_;
378  encryption_config_->crypt_byte_block = crypt_byte_block_;
379  encryption_config_->skip_byte_block = skip_byte_block_;
380  if (encryptor_->use_constant_iv()) {
381  encryption_config_->per_sample_iv_size = 0;
382  encryption_config_->constant_iv = iv;
383  } else {
384  encryption_config_->per_sample_iv_size = static_cast<uint8_t>(iv.size());
385  }
386  encryption_config_->key_id = encryption_key.key_id;
387  encryption_config_->key_system_info = encryption_key.key_system_info;
388  return initialized;
389 }
390 
391 bool EncryptionHandler::EncryptVpxFrame(
392  const std::vector<VPxFrameInfo>& vpx_frames,
393  MediaSample* sample,
394  DecryptConfig* decrypt_config) {
395  uint8_t* data = sample->writable_data();
396  for (const VPxFrameInfo& frame : vpx_frames) {
397  uint16_t clear_bytes =
398  static_cast<uint16_t>(frame.uncompressed_header_size);
399  uint32_t cipher_bytes = static_cast<uint32_t>(
400  frame.frame_size - frame.uncompressed_header_size);
401 
402  // "VP Codec ISO Media File Format Binding" document requires that the
403  // encrypted bytes of each frame within the superframe must be block
404  // aligned so that the counter state can be computed for each frame
405  // within the superframe.
406  // ISO/IEC 23001-7:2016 10.2 'cbc1' 10.3 'cens'
407  // The BytesOfProtectedData size SHALL be a multiple of 16 bytes to
408  // avoid partial blocks in Subsamples.
409  // For consistency, apply block alignment to all frames.
410  const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
411  clear_bytes += misalign_bytes;
412  cipher_bytes -= misalign_bytes;
413 
414  decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
415  if (cipher_bytes > 0)
416  EncryptBytes(data + clear_bytes, cipher_bytes);
417  data += frame.frame_size;
418  }
419  // Add subsample for the superframe index if exists.
420  const bool is_superframe = vpx_frames.size() > 1;
421  if (is_superframe) {
422  size_t index_size = sample->data() + sample->data_size() - data;
423  DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
424  DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
425  uint16_t clear_bytes = static_cast<uint16_t>(index_size);
426  uint32_t cipher_bytes = 0;
427  decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
428  }
429  return true;
430 }
431 
432 bool EncryptionHandler::EncryptNalFrame(MediaSample* sample,
433  DecryptConfig* decrypt_config) {
434  DCHECK_NE(nalu_length_size_, 0u);
435  DCHECK(header_parser_);
436  const Nalu::CodecType nalu_type =
437  (codec_ == kCodecH265) ? Nalu::kH265 : Nalu::kH264;
438  NaluReader reader(nalu_type, nalu_length_size_, sample->writable_data(),
439  sample->data_size());
440 
441  // Store the current length of clear data. This is used to squash
442  // multiple unencrypted NAL units into fewer subsample entries.
443  uint64_t accumulated_clear_bytes = 0;
444 
445  Nalu nalu;
446  NaluReader::Result result;
447  while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
448  const uint64_t nalu_total_size = nalu.header_size() + nalu.payload_size();
449  if (nalu.is_video_slice() && nalu_total_size >= min_protected_data_size_) {
450  uint64_t current_clear_bytes = leading_clear_bytes_size_;
451  if (current_clear_bytes == 0) {
452  // For video-slice NAL units, encrypt the video slice. This skips
453  // the frame header.
454  const int64_t video_slice_header_size =
455  header_parser_->GetHeaderSize(nalu);
456  if (video_slice_header_size < 0) {
457  LOG(ERROR) << "Failed to read slice header.";
458  return false;
459  }
460  current_clear_bytes = nalu.header_size() + video_slice_header_size;
461  }
462  uint64_t cipher_bytes = nalu_total_size - current_clear_bytes;
463 
464  // ISO/IEC 23001-7:2016 10.2 'cbc1' 10.3 'cens'
465  // The BytesOfProtectedData size SHALL be a multiple of 16 bytes to
466  // avoid partial blocks in Subsamples.
467  // CMAF requires 'cenc' scheme BytesOfProtectedData SHALL be a multiple
468  // of 16 bytes; while 'cbcs' scheme BytesOfProtectedData SHALL start on
469  // the first byte of video data following the slice header.
470  if (protection_scheme_ == FOURCC_cbc1 ||
471  protection_scheme_ == FOURCC_cens ||
472  protection_scheme_ == FOURCC_cenc) {
473  const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
474  current_clear_bytes += misalign_bytes;
475  cipher_bytes -= misalign_bytes;
476  }
477 
478  const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
479  EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
480 
481  AddSubsample(
482  accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
483  cipher_bytes, decrypt_config);
484  accumulated_clear_bytes = 0;
485  } else {
486  // For non-video-slice or small NAL units, don't encrypt.
487  accumulated_clear_bytes += nalu_length_size_ + nalu_total_size;
488  }
489  }
490  if (result != NaluReader::kEOStream) {
491  LOG(ERROR) << "Failed to parse NAL units.";
492  return false;
493  }
494  AddSubsample(accumulated_clear_bytes, 0, decrypt_config);
495  return true;
496 }
497 
498 void EncryptionHandler::EncryptBytes(uint8_t* data, size_t size) {
499  DCHECK(encryptor_);
500  CHECK(encryptor_->Crypt(data, size, data));
501 }
502 
503 void EncryptionHandler::InjectVpxParserForTesting(
504  std::unique_ptr<VPxParser> vpx_parser) {
505  vpx_parser_ = std::move(vpx_parser);
506 }
507 
508 void EncryptionHandler::InjectVideoSliceHeaderParserForTesting(
509  std::unique_ptr<VideoSliceHeaderParser> header_parser) {
510  header_parser_ = std::move(header_parser);
511 }
512 
513 } // namespace media
514 } // namespace shaka
Abstract class holds stream information.
Definition: stream_info.h:57
std::function< std::string(const EncryptedStreamAttributes &stream_attributes)> stream_label_func
virtual Status GetCryptoPeriodKey(uint32_t crypto_period_index, const std::string &stream_label, EncryptionKey *key)=0
Status Dispatch(std::unique_ptr< StreamData > stream_data)
virtual Status GetKey(const std::string &stream_label, EncryptionKey *key)=0
double clear_lead_in_seconds
Clear lead duration in seconds.
bool vp9_subsample_encryption
Enable/disable subsample encryption for VP9.
Status Process(std::unique_ptr< StreamData > stream_data) override
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:107