2015-10-08 21:48:07 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/media/formats/webm/webm_crypto_helpers.h"
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
#include "absl/base/internal/endian.h"
|
|
|
|
#include "glog/logging.h"
|
2016-07-27 00:51:08 +00:00
|
|
|
#include "packager/media/base/buffer_reader.h"
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/media/formats/webm/webm_constants.h"
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Generates a 16 byte CTR counter block. The CTR counter block format is a
|
|
|
|
// CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV.
|
|
|
|
// |iv_size| is the size of |iv| in btyes. Returns a string of
|
|
|
|
// kDecryptionKeySize bytes.
|
2016-07-27 00:51:08 +00:00
|
|
|
std::vector<uint8_t> GenerateWebMCounterBlock(const uint8_t* iv, int iv_size) {
|
|
|
|
std::vector<uint8_t> counter_block(iv, iv + iv_size);
|
|
|
|
counter_block.insert(counter_block.end(),
|
|
|
|
DecryptConfig::kDecryptionKeySize - iv_size, 0);
|
2015-10-08 21:48:07 +00:00
|
|
|
return counter_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
2016-07-27 00:51:08 +00:00
|
|
|
// TODO(tinskip): Add unit test for this function.
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMCreateDecryptConfig(const uint8_t* data,
|
|
|
|
int data_size,
|
|
|
|
const uint8_t* key_id,
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t key_id_size,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<DecryptConfig>* decrypt_config,
|
2015-10-08 21:48:07 +00:00
|
|
|
int* data_offset) {
|
2016-07-27 00:51:08 +00:00
|
|
|
int header_size = kWebMSignalByteSize;
|
|
|
|
if (data_size < header_size) {
|
|
|
|
DVLOG(1) << "Empty WebM sample.";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t signal_byte = data[0];
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-07-27 00:51:08 +00:00
|
|
|
if (signal_byte & kWebMEncryptedSignal) {
|
|
|
|
// Encrypted sample.
|
|
|
|
header_size += kWebMIvSize;
|
|
|
|
if (data_size < header_size) {
|
|
|
|
DVLOG(1) << "Encrypted WebM sample too small to hold IV: " << data_size;
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-27 00:51:08 +00:00
|
|
|
std::vector<SubsampleEntry> subsamples;
|
|
|
|
if (signal_byte & kWebMPartitionedSignal) {
|
|
|
|
// Encrypted sample with subsamples / partitioning.
|
|
|
|
header_size += kWebMNumPartitionsSize;
|
|
|
|
if (data_size < header_size) {
|
|
|
|
DVLOG(1)
|
|
|
|
<< "Encrypted WebM sample too small to hold number of partitions: "
|
|
|
|
<< data_size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint8_t num_partitions = data[kWebMSignalByteSize + kWebMIvSize];
|
|
|
|
BufferReader offsets_buffer(data + header_size, data_size - header_size);
|
|
|
|
header_size += num_partitions * kWebMPartitionOffsetSize;
|
|
|
|
uint32_t subsample_offset = 0;
|
|
|
|
bool encrypted_subsample = false;
|
|
|
|
uint16_t clear_size = 0;
|
|
|
|
uint32_t encrypted_size = 0;
|
|
|
|
for (uint8_t partition_idx = 0; partition_idx < num_partitions;
|
|
|
|
++partition_idx) {
|
|
|
|
uint32_t partition_offset;
|
|
|
|
if (!offsets_buffer.Read4(&partition_offset)) {
|
|
|
|
DVLOG(1)
|
|
|
|
<< "Encrypted WebM sample too small to hold partition offsets: "
|
|
|
|
<< data_size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (partition_offset < subsample_offset) {
|
|
|
|
DVLOG(1) << "Partition offsets out of order.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (encrypted_subsample) {
|
|
|
|
encrypted_size = partition_offset - subsample_offset;
|
|
|
|
subsamples.push_back(SubsampleEntry(clear_size, encrypted_size));
|
|
|
|
} else {
|
|
|
|
clear_size = partition_offset - subsample_offset;
|
|
|
|
if (partition_idx == (num_partitions - 1)) {
|
|
|
|
encrypted_size = data_size - header_size - subsample_offset - clear_size;
|
|
|
|
subsamples.push_back(SubsampleEntry(clear_size, encrypted_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subsample_offset = partition_offset;
|
|
|
|
encrypted_subsample = !encrypted_subsample;
|
|
|
|
}
|
|
|
|
if (!(num_partitions % 2)) {
|
|
|
|
// Even number of partitions. Add one last all-clear subsample.
|
|
|
|
clear_size = data_size - header_size - subsample_offset;
|
|
|
|
encrypted_size = 0;
|
|
|
|
subsamples.push_back(SubsampleEntry(clear_size, encrypted_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decrypt_config->reset(new DecryptConfig(
|
|
|
|
std::vector<uint8_t>(key_id, key_id + key_id_size),
|
|
|
|
GenerateWebMCounterBlock(data + kWebMSignalByteSize, kWebMIvSize),
|
|
|
|
subsamples));
|
|
|
|
} else {
|
|
|
|
// Clear sample.
|
|
|
|
decrypt_config->reset();
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 00:51:08 +00:00
|
|
|
*data_offset = header_size;
|
2015-10-08 21:48:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|