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 KeySource::TrackType GetTrackTypeForEncryption(
const StreamInfo& stream_info,
60 uint32_t max_sd_pixels,
61 uint32_t max_hd_pixels,
62 uint32_t max_uhd1_pixels) {
63 if (stream_info.stream_type() == kStreamAudio)
64 return KeySource::TRACK_TYPE_AUDIO;
66 if (stream_info.stream_type() != kStreamVideo)
67 return KeySource::TRACK_TYPE_UNKNOWN;
69 DCHECK_EQ(kStreamVideo, stream_info.stream_type());
70 const VideoStreamInfo& video_stream_info =
71 static_cast<const VideoStreamInfo&
>(stream_info);
72 uint32_t pixels = video_stream_info.width() * video_stream_info.height();
73 if (pixels <= max_sd_pixels) {
74 return KeySource::TRACK_TYPE_SD;
75 }
else if (pixels <= max_hd_pixels) {
76 return KeySource::TRACK_TYPE_HD;
77 }
else if (pixels <= max_uhd1_pixels) {
78 return KeySource::TRACK_TYPE_UHD1;
80 return KeySource::TRACK_TYPE_UHD2;
84 EncryptionHandler::EncryptionHandler(
85 const EncryptionOptions& encryption_options,
86 KeySource* key_source)
87 : encryption_options_(encryption_options), key_source_(key_source) {}
89 EncryptionHandler::~EncryptionHandler() {}
92 if (num_input_streams() != 1 || next_output_stream_index() != 1) {
93 return Status(error::INVALID_ARGUMENT,
94 "Expects exactly one input and output.");
101 switch (stream_data->stream_data_type) {
102 case StreamDataType::kStreamInfo:
103 status = ProcessStreamInfo(stream_data->stream_info.get());
105 case StreamDataType::kSegmentInfo: {
106 SegmentInfo* segment_info = stream_data->segment_info.get();
107 segment_info->is_encrypted = remaining_clear_lead_ <= 0;
109 const bool key_rotation_enabled = crypto_period_duration_ != 0;
110 if (key_rotation_enabled)
111 segment_info->key_rotation_encryption_config = encryption_config_;
112 if (!segment_info->is_subsegment) {
113 if (key_rotation_enabled)
114 check_new_crypto_period_ =
true;
115 if (remaining_clear_lead_ > 0)
116 remaining_clear_lead_ -= segment_info->duration;
120 case StreamDataType::kMediaSample:
121 status = ProcessMediaSample(stream_data->media_sample.get());
124 VLOG(3) <<
"Stream data type "
125 <<
static_cast<int>(stream_data->stream_data_type) <<
" ignored.";
128 return status.ok() ?
Dispatch(std::move(stream_data)) : status;
132 if (stream_info->is_encrypted()) {
133 return Status(error::INVALID_ARGUMENT,
134 "Input stream is already encrypted.");
137 remaining_clear_lead_ =
139 crypto_period_duration_ =
141 stream_info->time_scale();
142 codec_ = stream_info->codec();
143 nalu_length_size_ = GetNaluLengthSize(*stream_info);
144 track_type_ = GetTrackTypeForEncryption(
149 if (encryption_options_.vp9_subsample_encryption)
150 vpx_parser_.reset(
new VP9Parser);
153 header_parser_.reset(
new H264VideoSliceHeaderParser);
156 header_parser_.reset(
new H265VideoSliceHeaderParser);
160 if (nalu_length_size_ > 0) {
161 LOG(WARNING) <<
"Unknown video codec '" << codec_ <<
"'";
162 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
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.");
173 Status status = SetupProtectionPattern(stream_info->stream_type());
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;
182 encryption_key.key_id.assign(
183 kKeyRotationDefaultKeyId,
184 kKeyRotationDefaultKeyId +
sizeof(kKeyRotationDefaultKeyId));
187 encryption_key.key = encryption_key.key_id;
189 status = key_source_->
GetKey(track_type_, &encryption_key);
193 if (!CreateEncryptor(encryption_key))
194 return Status(error::ENCRYPTION_FAILURE,
"Failed to create encryptor");
196 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, track_type_, &encryption_key);
223 if (!CreateEncryptor(encryption_key))
224 return Status(error::ENCRYPTION_FAILURE,
"Failed to create encryptor");
226 check_new_crypto_period_ =
false;
229 if (remaining_clear_lead_ > 0)
232 std::unique_ptr<DecryptConfig> decrypt_config(
new DecryptConfig(
233 encryption_config_->key_id, encryptor_->iv(),
235 crypt_byte_block_, skip_byte_block_));
238 result = EncryptVpxFrame(vpx_frames, sample, decrypt_config.get());
240 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
241 sample->data_size());
243 }
else if (header_parser_) {
244 result = EncryptNalFrame(sample, decrypt_config.get());
246 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(),
247 sample->data_size());
250 if (sample->data_size() > leading_clear_bytes_size_) {
251 EncryptBytes(sample->writable_data() + leading_clear_bytes_size_,
252 sample->data_size() - leading_clear_bytes_size_);
256 return Status(error::ENCRYPTION_FAILURE,
"Failed to encrypt samples.");
257 sample->set_is_encrypted(
true);
258 sample->set_decrypt_config(std::move(decrypt_config));
259 encryptor_->UpdateIv();
263 Status EncryptionHandler::SetupProtectionPattern(StreamType stream_type) {
265 case kAppleSampleAesProtectionScheme: {
266 const size_t kH264LeadingClearBytesSize = 32u;
267 const size_t kSmallNalUnitSize = 32u + 16u;
268 const size_t kAudioLeadingClearBytesSize = 16u;
272 crypt_byte_block_ = 1u;
273 skip_byte_block_ = 9u;
274 leading_clear_bytes_size_ = kH264LeadingClearBytesSize;
275 min_protected_data_size_ = kSmallNalUnitSize + 1u;
278 FALLTHROUGH_INTENDED;
284 crypt_byte_block_ = 0u;
285 skip_byte_block_ = 0u;
286 leading_clear_bytes_size_ = kAudioLeadingClearBytesSize;
287 min_protected_data_size_ = leading_clear_bytes_size_ + 1u;
290 return Status(error::ENCRYPTION_FAILURE,
291 "Only AAC/AC3 and H264 are supported in Sample AES.");
296 FALLTHROUGH_INTENDED;
298 if (stream_type == kStreamVideo) {
300 crypt_byte_block_ = 1u;
301 skip_byte_block_ = 9u;
311 crypt_byte_block_ = 1u;
312 skip_byte_block_ = 0u;
317 crypt_byte_block_ = 0u;
318 skip_byte_block_ = 0u;
323 bool EncryptionHandler::CreateEncryptor(
const EncryptionKey& encryption_key) {
324 std::unique_ptr<AesCryptor> encryptor;
327 encryptor.reset(
new AesCtrEncryptor);
330 encryptor.reset(
new AesCbcEncryptor(kNoPadding));
333 encryptor.reset(
new AesPatternCryptor(
334 crypt_byte_block_, skip_byte_block_,
336 AesCryptor::kDontUseConstantIv,
337 std::unique_ptr<AesCryptor>(
new AesCtrEncryptor())));
340 encryptor.reset(
new AesPatternCryptor(
341 crypt_byte_block_, skip_byte_block_,
343 AesCryptor::kUseConstantIv,
344 std::unique_ptr<AesCryptor>(
new AesCbcEncryptor(kNoPadding))));
346 case kAppleSampleAesProtectionScheme:
347 if (crypt_byte_block_ == 0 && skip_byte_block_ == 0) {
349 new AesCbcEncryptor(kNoPadding, AesCryptor::kUseConstantIv));
351 encryptor.reset(
new AesPatternCryptor(
352 crypt_byte_block_, skip_byte_block_,
354 AesCryptor::kUseConstantIv,
355 std::unique_ptr<AesCryptor>(
new AesCbcEncryptor(kNoPadding))));
359 LOG(ERROR) <<
"Unsupported protection scheme.";
363 std::vector<uint8_t> iv = encryption_key.iv;
367 LOG(ERROR) <<
"Failed to generate random iv.";
371 const bool initialized =
372 encryptor->InitializeWithIv(encryption_key.key, iv);
373 encryptor_ = std::move(encryptor);
375 encryption_config_.reset(
new EncryptionConfig);
377 encryption_config_->crypt_byte_block = crypt_byte_block_;
378 encryption_config_->skip_byte_block = skip_byte_block_;
379 if (encryptor_->use_constant_iv()) {
380 encryption_config_->per_sample_iv_size = 0;
381 encryption_config_->constant_iv = iv;
383 encryption_config_->per_sample_iv_size =
static_cast<uint8_t
>(iv.size());
385 encryption_config_->key_id = encryption_key.key_id;
386 encryption_config_->key_system_info = encryption_key.key_system_info;
390 bool EncryptionHandler::EncryptVpxFrame(
391 const std::vector<VPxFrameInfo>& vpx_frames,
393 DecryptConfig* decrypt_config) {
394 uint8_t* data = sample->writable_data();
395 for (
const VPxFrameInfo& frame : vpx_frames) {
396 uint16_t clear_bytes =
397 static_cast<uint16_t
>(frame.uncompressed_header_size);
398 uint32_t cipher_bytes =
static_cast<uint32_t
>(
399 frame.frame_size - frame.uncompressed_header_size);
409 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
410 clear_bytes += misalign_bytes;
411 cipher_bytes -= misalign_bytes;
413 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
414 if (cipher_bytes > 0)
415 EncryptBytes(data + clear_bytes, cipher_bytes);
416 data += frame.frame_size;
419 const bool is_superframe = vpx_frames.size() > 1;
421 size_t index_size = sample->data() + sample->data_size() - data;
422 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
423 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
424 uint16_t clear_bytes =
static_cast<uint16_t
>(index_size);
425 uint32_t cipher_bytes = 0;
426 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
431 bool EncryptionHandler::EncryptNalFrame(MediaSample* sample,
432 DecryptConfig* decrypt_config) {
433 DCHECK_NE(nalu_length_size_, 0u);
434 DCHECK(header_parser_);
435 const Nalu::CodecType nalu_type =
436 (codec_ == kCodecH265) ? Nalu::kH265 : Nalu::kH264;
437 NaluReader reader(nalu_type, nalu_length_size_, sample->writable_data(),
438 sample->data_size());
442 uint64_t accumulated_clear_bytes = 0;
445 NaluReader::Result result;
446 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
447 const uint64_t nalu_total_size = nalu.header_size() + nalu.payload_size();
448 if (nalu.is_video_slice() && nalu_total_size >= min_protected_data_size_) {
449 uint64_t current_clear_bytes = leading_clear_bytes_size_;
450 if (current_clear_bytes == 0) {
453 const int64_t video_slice_header_size =
454 header_parser_->GetHeaderSize(nalu);
455 if (video_slice_header_size < 0) {
456 LOG(ERROR) <<
"Failed to read slice header.";
459 current_clear_bytes = nalu.header_size() + video_slice_header_size;
461 uint64_t cipher_bytes = nalu_total_size - current_clear_bytes;
472 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
473 current_clear_bytes += misalign_bytes;
474 cipher_bytes -= misalign_bytes;
477 const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
478 EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
481 accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
482 cipher_bytes, decrypt_config);
483 accumulated_clear_bytes = 0;
486 accumulated_clear_bytes += nalu_length_size_ + nalu_total_size;
489 if (result != NaluReader::kEOStream) {
490 LOG(ERROR) <<
"Failed to parse NAL units.";
493 AddSubsample(accumulated_clear_bytes, 0, decrypt_config);
497 void EncryptionHandler::EncryptBytes(uint8_t* data,
size_t size) {
499 CHECK(encryptor_->Crypt(data, size, data));
502 void EncryptionHandler::InjectVpxParserForTesting(
503 std::unique_ptr<VPxParser> vpx_parser) {
504 vpx_parser_ = std::move(vpx_parser);
507 void EncryptionHandler::InjectVideoSliceHeaderParserForTesting(
508 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
509 header_parser_ = std::move(header_parser);