7 #include "packager/media/formats/webm/encryptor.h"
9 #include <gflags/gflags.h>
10 #include "packager/media/base/aes_encryptor.h"
11 #include "packager/media/base/buffer_writer.h"
12 #include "packager/media/base/fourccs.h"
13 #include "packager/media/base/media_sample.h"
14 #include "packager/media/codecs/vp9_parser.h"
15 #include "packager/media/formats/webm/webm_constants.h"
22 const size_t kAesBlockSize = 16;
24 Status CreateContentEncryption(mkvmuxer::Track* track, EncryptionKey* key) {
25 if (!track->AddContentEncoding()) {
26 return Status(error::INTERNAL_ERROR,
27 "Could not add ContentEncoding to track.");
30 mkvmuxer::ContentEncoding*
const encoding =
31 track->GetContentEncodingByIndex(0);
33 return Status(error::INTERNAL_ERROR,
34 "Could not add ContentEncoding to track.");
37 mkvmuxer::ContentEncAESSettings*
const aes = encoding->enc_aes_settings();
39 return Status(error::INTERNAL_ERROR,
40 "Error getting ContentEncAESSettings.");
42 if (aes->cipher_mode() != mkvmuxer::ContentEncAESSettings::kCTR) {
43 return Status(error::INTERNAL_ERROR,
"Cipher Mode is not CTR.");
46 if (!key->key_id.empty() &&
47 !encoding->SetEncryptionID(
48 reinterpret_cast<const uint8_t*>(key->key_id.data()),
49 key->key_id.size())) {
50 return Status(error::INTERNAL_ERROR,
"Error setting encryption ID.");
57 Encryptor::Encryptor() {}
59 Encryptor::~Encryptor() {}
62 KeySource::TrackType track_type,
65 bool webm_subsample_encryption) {
67 return CreateEncryptor(muxer_listener, track_type, codec, key_source,
68 webm_subsample_encryption);
73 return CreateContentEncryption(track, key_.get());
80 const size_t sample_size = sample->data_size();
84 std::vector<VPxFrameInfo> vpx_frames;
86 if (!vpx_parser_->Parse(sample->data(), sample_size, &vpx_frames)) {
87 return Status(error::MUXER_FAILURE,
"Failed to parse VPx frame.");
92 const size_t iv_size = encryptor_->iv().size();
93 if (iv_size != kWebMIvSize) {
94 return Status(error::MUXER_FAILURE,
95 "Incorrect size WebM encryption IV.");
97 if (vpx_frames.size()) {
101 if (vpx_frames.size() > kWebMMaxSubsamples) {
102 return Status(error::MUXER_FAILURE,
103 "Maximum number of VPx encryption partitions exceeded.");
105 uint8_t num_partitions =
106 vpx_frames.size() == 1 ? 1 : vpx_frames.size() * 2;
107 size_t header_size = kWebMSignalByteSize + iv_size +
108 kWebMNumPartitionsSize +
109 (kWebMPartitionOffsetSize * num_partitions);
110 sample->resize_data(header_size + sample_size);
111 uint8_t* sample_data = sample->writable_data();
112 memmove(sample_data + header_size, sample_data, sample_size);
113 sample_data[0] = kWebMEncryptedSignal | kWebMPartitionedSignal;
114 memcpy(sample_data + kWebMSignalByteSize, encryptor_->iv().data(),
116 sample_data[kWebMSignalByteSize + kWebMIvSize] = num_partitions;
117 uint32_t partition_offset = 0;
118 BufferWriter offsets_buffer(kWebMPartitionOffsetSize * num_partitions);
119 for (
const auto& vpx_frame : vpx_frames) {
120 uint32_t encrypted_size =
121 vpx_frame.frame_size - vpx_frame.uncompressed_header_size;
122 encrypted_size -= encrypted_size % kAesBlockSize;
123 uint32_t clear_size = vpx_frame.frame_size - encrypted_size;
124 partition_offset += clear_size;
125 offsets_buffer.
AppendInt(partition_offset);
126 if (encrypted_size > 0) {
127 uint8_t* encrypted_ptr = sample_data + header_size + partition_offset;
128 if (!encryptor_->Crypt(encrypted_ptr, encrypted_size, encrypted_ptr)) {
129 return Status(error::MUXER_FAILURE,
"Failed to encrypt the frame.");
131 partition_offset += encrypted_size;
133 if (num_partitions > 1) {
134 offsets_buffer.
AppendInt(partition_offset);
137 DCHECK_EQ(num_partitions * kWebMPartitionOffsetSize,
138 offsets_buffer.Size());
139 memcpy(sample_data + kWebMSignalByteSize + kWebMIvSize +
140 kWebMNumPartitionsSize,
141 offsets_buffer.
Buffer(), offsets_buffer.Size());
145 sample->resize_data(sample_size + iv_size + kWebMSignalByteSize);
146 uint8_t* sample_data = sample->writable_data();
149 if (!encryptor_->Crypt(sample_data, sample_size, sample_data)) {
150 return Status(error::MUXER_FAILURE,
"Failed to encrypt the frame.");
155 memmove(sample_data + iv_size + kWebMSignalByteSize, sample_data,
157 sample_data[0] = kWebMEncryptedSignal;
158 memcpy(sample_data + 1, encryptor_->iv().data(), iv_size);
160 encryptor_->UpdateIv();
163 sample->resize_data(sample_size + 1);
164 uint8_t* sample_data = sample->writable_data();
165 memmove(sample_data + 1, sample_data, sample_size);
166 sample_data[0] = 0x00;
173 KeySource::TrackType track_type,
176 bool webm_subsample_encryption) {
177 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
178 Status status = key_source->
GetKey(track_type, encryption_key.get());
181 if (encryption_key->iv.empty()) {
183 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
185 DCHECK_EQ(kWebMIvSize, encryption_key->iv.size());
187 const bool initialized =
188 encryptor->InitializeWithIv(encryption_key->key, encryption_key->iv);
190 return Status(error::INTERNAL_ERROR,
"Failed to create the encryptor.");
192 if (webm_subsample_encryption && codec == kCodecVP9) {
197 if (muxer_listener) {
198 const bool kInitialEncryptionInfo =
true;
200 kInitialEncryptionInfo, FOURCC_cenc, encryption_key->key_id,
201 encryptor->iv(), encryption_key->key_system_info);
204 key_ = std::move(encryption_key);
205 encryptor_ = std::move(encryptor);