7 #include "packager/media/crypto/encryption_handler.h"
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"
27 const size_t kCencBlockSize = 16u;
30 const uint8_t kKeyRotationDefaultKeyId[] = {
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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;
46 if (clear_bytes > 0 || cipher_bytes > 0)
47 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
50 uint8_t GetNaluLengthSize(
const StreamInfo& stream_info) {
51 if (stream_info.stream_type() != kStreamVideo)
54 const VideoStreamInfo& video_stream_info =
55 static_cast<const VideoStreamInfo&
>(stream_info);
56 return video_stream_info.nalu_length_size();
59 std::string GetStreamLabelForEncryption(
60 const StreamInfo& stream_info,
61 const std::function<std::string(
62 const EncryptionParams::EncryptedStreamAttributes& stream_attributes)>&
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();
76 return stream_label_func(stream_attributes);
80 EncryptionHandler::EncryptionHandler(
81 const EncryptionOptions& encryption_options,
82 KeySource* key_source)
83 : encryption_options_(encryption_options), key_source_(key_source) {}
85 EncryptionHandler::~EncryptionHandler() {}
89 return Status(error::INVALID_ARGUMENT,
"Stream label function not set.");
91 if (num_input_streams() != 1 || next_output_stream_index() != 1) {
92 return Status(error::INVALID_ARGUMENT,
93 "Expects exactly one input and output.");
100 switch (stream_data->stream_data_type) {
101 case StreamDataType::kStreamInfo:
102 status = ProcessStreamInfo(stream_data->stream_info.get());
104 case StreamDataType::kSegmentInfo: {
105 SegmentInfo* segment_info = stream_data->segment_info.get();
106 segment_info->is_encrypted = remaining_clear_lead_ <= 0;
108 const bool key_rotation_enabled = crypto_period_duration_ != 0;
109 if (key_rotation_enabled)
110 segment_info->key_rotation_encryption_config = encryption_config_;
111 if (!segment_info->is_subsegment) {
112 if (key_rotation_enabled)
113 check_new_crypto_period_ =
true;
114 if (remaining_clear_lead_ > 0)
115 remaining_clear_lead_ -= segment_info->duration;
119 case StreamDataType::kMediaSample:
120 status = ProcessMediaSample(stream_data->media_sample.get());
123 VLOG(3) <<
"Stream data type "
124 <<
static_cast<int>(stream_data->stream_data_type) <<
" ignored.";
127 return status.ok() ?
Dispatch(std::move(stream_data)) : status;
130 Status EncryptionHandler::ProcessStreamInfo(
StreamInfo* stream_info) {
131 if (stream_info->is_encrypted()) {
132 return Status(error::INVALID_ARGUMENT,
133 "Input stream is already encrypted.");
136 remaining_clear_lead_ =
138 crypto_period_duration_ =
140 stream_info->time_scale();
141 codec_ = stream_info->codec();
142 nalu_length_size_ = GetNaluLengthSize(*stream_info);
143 stream_label_ = GetStreamLabelForEncryption(
148 vpx_parser_.reset(
new VP9Parser);
151 header_parser_.reset(
new H264VideoSliceHeaderParser);
154 header_parser_.reset(
new H265VideoSliceHeaderParser);
158 if (nalu_length_size_ > 0) {
159 LOG(WARNING) <<
"Unknown video codec '" << codec_ <<
"'";
160 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
163 if (header_parser_) {
164 CHECK_NE(nalu_length_size_, 0u) <<
"AnnexB stream is not supported yet";
165 if (!header_parser_->Initialize(stream_info->codec_config())) {
166 return Status(error::ENCRYPTION_FAILURE,
167 "Fail to read SPS and PPS data.");
171 Status status = SetupProtectionPattern(stream_info->stream_type());
175 EncryptionKey encryption_key;
176 const bool key_rotation_enabled = crypto_period_duration_ != 0;
177 if (key_rotation_enabled) {
178 check_new_crypto_period_ =
true;
180 encryption_key.key_id.assign(
181 kKeyRotationDefaultKeyId,
182 kKeyRotationDefaultKeyId +
sizeof(kKeyRotationDefaultKeyId));
185 encryption_key.key = encryption_key.key_id;
187 status = key_source_->
GetKey(stream_label_, &encryption_key);
191 if (!CreateEncryptor(encryption_key))
192 return Status(error::ENCRYPTION_FAILURE,
"Failed to create encryptor");
194 stream_info->set_is_encrypted(
true);
197 stream_info->set_encryption_config(*encryption_config_);
201 Status EncryptionHandler::ProcessMediaSample(MediaSample* sample) {
205 std::vector<VPxFrameInfo> vpx_frames;
207 !vpx_parser_->Parse(sample->data(), sample->data_size(), &vpx_frames)) {
208 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse vpx frame.");
214 if (check_new_crypto_period_) {
215 const int64_t current_crypto_period_index =
216 sample->dts() / crypto_period_duration_;
217 if (current_crypto_period_index != prev_crypto_period_index_) {
218 EncryptionKey encryption_key;
220 current_crypto_period_index, stream_label_, &encryption_key);
223 if (!CreateEncryptor(encryption_key))
224 return Status(error::ENCRYPTION_FAILURE,
"Failed to create encryptor");
225 prev_crypto_period_index_ = current_crypto_period_index;
227 check_new_crypto_period_ =
false;
230 if (remaining_clear_lead_ > 0)
233 std::unique_ptr<DecryptConfig> decrypt_config(
new DecryptConfig(
234 encryption_config_->key_id, encryptor_->iv(),
236 crypt_byte_block_, skip_byte_block_));
239 result = EncryptVpxFrame(vpx_frames, sample, decrypt_config.get());
241 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
242 sample->data_size());
244 }
else if (header_parser_) {
245 result = EncryptNalFrame(sample, decrypt_config.get());
247 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
248 sample->data_size());
251 if (sample->data_size() > leading_clear_bytes_size_) {
252 EncryptBytes(sample->writable_data() + leading_clear_bytes_size_,
253 sample->data_size() - leading_clear_bytes_size_);
257 return Status(error::ENCRYPTION_FAILURE,
"Failed to encrypt samples.");
258 sample->set_is_encrypted(
true);
259 sample->set_decrypt_config(std::move(decrypt_config));
260 encryptor_->UpdateIv();
264 Status EncryptionHandler::SetupProtectionPattern(StreamType stream_type) {
266 case kAppleSampleAesProtectionScheme: {
267 const size_t kH264LeadingClearBytesSize = 32u;
268 const size_t kSmallNalUnitSize = 32u + 16u;
269 const size_t kAudioLeadingClearBytesSize = 16u;
273 crypt_byte_block_ = 1u;
274 skip_byte_block_ = 9u;
275 leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
276 min_protected_data_size_ = kSmallNalUnitSize + 1u;
279 FALLTHROUGH_INTENDED;
285 crypt_byte_block_ = 0u;
286 skip_byte_block_ = 0u;
287 leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
288 min_protected_data_size_ = leading_clear_bytes_size_ + 1u;
291 return Status(error::ENCRYPTION_FAILURE,
292 "Only AAC/AC3 and H264 are supported in Sample AES.");
297 FALLTHROUGH_INTENDED;
299 if (stream_type == kStreamVideo) {
301 crypt_byte_block_ = 1u;
302 skip_byte_block_ = 9u;
312 crypt_byte_block_ = 1u;
313 skip_byte_block_ = 0u;
318 crypt_byte_block_ = 0u;
319 skip_byte_block_ = 0u;
324 bool EncryptionHandler::CreateEncryptor(
const EncryptionKey& encryption_key) {
325 std::unique_ptr<AesCryptor> encryptor;
328 encryptor.reset(
new AesCtrEncryptor);
331 encryptor.reset(
new AesCbcEncryptor(kNoPadding));
334 encryptor.reset(
new AesPatternCryptor(
335 crypt_byte_block_, skip_byte_block_,
337 AesCryptor::kDontUseConstantIv,
338 std::unique_ptr<AesCryptor>(
new AesCtrEncryptor())));
341 encryptor.reset(
new AesPatternCryptor(
342 crypt_byte_block_, skip_byte_block_,
344 AesCryptor::kUseConstantIv,
345 std::unique_ptr<AesCryptor>(
new AesCbcEncryptor(kNoPadding))));
347 case kAppleSampleAesProtectionScheme:
348 if (crypt_byte_block_ == 0 && skip_byte_block_ == 0) {
350 new AesCbcEncryptor(kNoPadding, AesCryptor::kUseConstantIv));
352 encryptor.reset(
new AesPatternCryptor(
353 crypt_byte_block_, skip_byte_block_,
355 AesCryptor::kUseConstantIv,
356 std::unique_ptr<AesCryptor>(
new AesCbcEncryptor(kNoPadding))));
360 LOG(ERROR) <<
"Unsupported protection scheme.";
364 std::vector<uint8_t> iv = encryption_key.iv;
368 LOG(ERROR) <<
"Failed to generate random iv.";
372 const bool initialized =
373 encryptor->InitializeWithIv(encryption_key.key, iv);
374 encryptor_ = std::move(encryptor);
376 encryption_config_.reset(
new EncryptionConfig);
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;
384 encryption_config_->per_sample_iv_size =
static_cast<uint8_t
>(iv.size());
386 encryption_config_->key_id = encryption_key.key_id;
387 encryption_config_->key_system_info = encryption_key.key_system_info;
391 bool EncryptionHandler::EncryptVpxFrame(
392 const std::vector<VPxFrameInfo>& vpx_frames,
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);
410 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
411 clear_bytes += misalign_bytes;
412 cipher_bytes -= misalign_bytes;
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;
420 const bool is_superframe = vpx_frames.size() > 1;
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);
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());
443 uint64_t accumulated_clear_bytes = 0;
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) {
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.";
460 current_clear_bytes = nalu.header_size() + video_slice_header_size;
462 uint64_t cipher_bytes = nalu_total_size - current_clear_bytes;
473 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
474 current_clear_bytes += misalign_bytes;
475 cipher_bytes -= misalign_bytes;
478 const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
479 EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
482 accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
483 cipher_bytes, decrypt_config);
484 accumulated_clear_bytes = 0;
487 accumulated_clear_bytes += nalu_length_size_ + nalu_total_size;
490 if (result != NaluReader::kEOStream) {
491 LOG(ERROR) <<
"Failed to parse NAL units.";
494 AddSubsample(accumulated_clear_bytes, 0, decrypt_config);
498 void EncryptionHandler::EncryptBytes(uint8_t* data,
size_t size) {
500 CHECK(encryptor_->Crypt(data, size, data));
503 void EncryptionHandler::InjectVpxParserForTesting(
504 std::unique_ptr<VPxParser> vpx_parser) {
505 vpx_parser_ = std::move(vpx_parser);
508 void EncryptionHandler::InjectVideoSliceHeaderParserForTesting(
509 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
510 header_parser_ = std::move(header_parser);