Commit f35464f3 authored by kqyang's avatar kqyang Committed by Commit bot

Implement WebM subsample support according to the specification at...

Implement WebM subsample support according to the specification at http://wiki.webmproject.org/encryption/webm-subsample-encryption.

BUG=630344

Review-Url: https://codereview.chromium.org/2174533002
Cr-Commit-Position: refs/heads/master@{#418705}
parent d660b195
......@@ -565,6 +565,7 @@ test("media_unittests") {
"formats/webm/tracks_builder.h",
"formats/webm/webm_cluster_parser_unittest.cc",
"formats/webm/webm_content_encodings_client_unittest.cc",
"formats/webm/webm_crypto_helpers_unittest.cc",
"formats/webm/webm_parser_unittest.cc",
"formats/webm/webm_stream_parser_unittest.cc",
"formats/webm/webm_tracks_parser_unittest.cc",
......
......@@ -209,8 +209,11 @@ const uint8_t kWebMFlagKeyframe = 0x80;
// Current encrypted WebM request for comments specification is here
// http://wiki.webmproject.org/encryption/webm-encryption-rfc
const uint8_t kWebMFlagEncryptedFrame = 0x1;
const uint8_t kWebMFlagEncryptedFramePartitioned = 0x2;
const int kWebMIvSize = 8;
const int kWebMSignalByteSize = 1;
const int kWebMEncryptedFrameNumPartitionsSize = 1;
const int kWebMEncryptedFramePartitionOffsetSize = 4;
// Current specification for WebVTT embedded in WebM
// http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
......
......@@ -24,6 +24,73 @@ std::string GenerateWebMCounterBlock(const uint8_t* iv, int iv_size) {
return counter_block;
}
uint32_t ReadInteger(const uint8_t* buf, int size) {
// Read in the big-endian integer.
uint32_t value = 0;
for (int i = 0; i < size; ++i)
value = (value << 8) | buf[i];
return value;
}
bool ExtractSubsamples(const uint8_t* buf,
size_t frame_data_size,
size_t num_partitions,
std::vector<SubsampleEntry>* subsample_entries) {
subsample_entries->clear();
uint32_t clear_bytes = 0;
// Partition is the wall between alternating sections. Partition offsets are
// relative to the start of the actual frame data.
// Size of clear/cipher sections can be calculated from the difference between
// adjacent partition offsets.
// Here is an example with 4 partitions (5 sections):
// "clear |1 cipher |2 clear |3 cipher |4 clear"
// With the first and the last implicit partition included:
// "|0 clear |1 cipher |2 clear |3 cipher |4 clear |5"
// where partition_offset_0 = 0, partition_offset_5 = frame_data_size
// There are three subsamples in the above example:
// Subsample0.clear_bytes = partition_offset_1 - partition_offset_0
// Subsample0.cipher_bytes = partition_offset_2 - partition_offset_1
// ...
// Subsample2.clear_bytes = partition_offset_5 - partition_offset_4
// Subsample2.cipher_bytes = 0
uint32_t partition_offset = 0;
for (size_t i = 0, offset = 0; i <= num_partitions; ++i) {
const uint32_t prev_partition_offset = partition_offset;
partition_offset =
(i == num_partitions)
? frame_data_size
: ReadInteger(buf + offset, kWebMEncryptedFramePartitionOffsetSize);
offset += kWebMEncryptedFramePartitionOffsetSize;
if (partition_offset < prev_partition_offset) {
DVLOG(1) << "Partition should not be decreasing " << prev_partition_offset
<< " " << partition_offset;
return false;
}
uint32_t cipher_bytes = 0;
bool new_subsample_entry = false;
// Alternating clear and cipher sections.
if ((i % 2) == 0) {
clear_bytes = partition_offset - prev_partition_offset;
// Generate a new subsample when finishing reading partition offsets.
new_subsample_entry = i == num_partitions;
} else {
cipher_bytes = partition_offset - prev_partition_offset;
// Generate a new subsample after seeing a cipher section.
new_subsample_entry = true;
}
if (new_subsample_entry) {
if (clear_bytes == 0 && cipher_bytes == 0) {
DVLOG(1) << "Not expecting >2 partitions with the same offsets.";
return false;
}
subsample_entries->push_back(SubsampleEntry(clear_bytes, cipher_bytes));
}
}
return true;
}
} // namespace anonymous
bool WebMCreateDecryptConfig(const uint8_t* data,
......@@ -37,13 +104,14 @@ bool WebMCreateDecryptConfig(const uint8_t* data,
return false;
}
uint8_t signal_byte = data[0];
const uint8_t signal_byte = data[0];
int frame_offset = sizeof(signal_byte);
// Setting the DecryptConfig object of the buffer while leaving the
// initialization vector empty will tell the decryptor that the frame is
// unencrypted.
std::string counter_block;
std::vector<SubsampleEntry> subsample_entries;
if (signal_byte & kWebMFlagEncryptedFrame) {
if (data_size < kWebMSignalByteSize + kWebMIvSize) {
......@@ -52,12 +120,38 @@ bool WebMCreateDecryptConfig(const uint8_t* data,
}
counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
frame_offset += kWebMIvSize;
if (signal_byte & kWebMFlagEncryptedFramePartitioned) {
if (data_size < frame_offset + kWebMEncryptedFrameNumPartitionsSize) {
DVLOG(1) << "Got a partitioned encrypted block with not enough data "
<< data_size;
return false;
}
const size_t num_partitions = data[frame_offset];
if (num_partitions == 0) {
DVLOG(1) << "Got a partitioned encrypted block with 0 partitions.";
return false;
}
frame_offset += kWebMEncryptedFrameNumPartitionsSize;
const uint8_t* partition_data_start = data + frame_offset;
frame_offset += kWebMEncryptedFramePartitionOffsetSize * num_partitions;
if (data_size <= frame_offset) {
DVLOG(1) << "Got a partitioned encrypted block with " << num_partitions
<< " partitions but not enough data " << data_size;
return false;
}
const size_t frame_data_size = data_size - frame_offset;
if (!ExtractSubsamples(partition_data_start, frame_data_size,
num_partitions, &subsample_entries)) {
return false;
}
}
}
decrypt_config->reset(new DecryptConfig(
std::string(reinterpret_cast<const char*>(key_id), key_id_size),
counter_block,
std::vector<SubsampleEntry>()));
counter_block, subsample_entries));
*data_offset = frame_offset;
return true;
......
......@@ -10,6 +10,7 @@
#include <memory>
#include "media/base/decoder_buffer.h"
#include "media/base/media_export.h"
namespace media {
......@@ -20,12 +21,13 @@ namespace media {
// false otherwise, in which case |decrypt_config| and |data_offset| will not be
// changed. Current encrypted WebM request for comments specification is here
// http://wiki.webmproject.org/encryption/webm-encryption-rfc
bool WebMCreateDecryptConfig(const uint8_t* data,
int data_size,
const uint8_t* key_id,
int key_id_size,
std::unique_ptr<DecryptConfig>* decrypt_config,
int* data_offset);
bool MEDIA_EXPORT
WebMCreateDecryptConfig(const uint8_t* data,
int data_size,
const uint8_t* key_id,
int key_id_size,
std::unique_ptr<DecryptConfig>* decrypt_config,
int* data_offset);
} // namespace media
......
// Copyright 2016 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.
#include "media/formats/webm/webm_crypto_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::ElementsAre;
namespace {
const uint8_t kKeyId[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
} // namespace
namespace media {
inline bool operator==(const SubsampleEntry& lhs, const SubsampleEntry& rhs) {
return lhs.clear_bytes == rhs.clear_bytes &&
lhs.cypher_bytes == rhs.cypher_bytes;
}
TEST(WebMCryptoHelpersTest, EmptyData) {
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(nullptr, 0, kKeyId, sizeof(kKeyId),
&decrypt_config, &data_offset));
}
TEST(WebMCryptoHelpersTest, ClearData) {
const uint8_t kData[] = {0x00, 0x0d, 0x0a, 0x0d, 0x0a};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
EXPECT_EQ(1, data_offset);
EXPECT_FALSE(decrypt_config->is_encrypted());
}
TEST(WebMCryptoHelpersTest, EncryptedButNotEnoughBytes) {
const uint8_t kData[] = {0x01, 0x0d, 0x0a, 0x0d, 0x0a};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedNotPartitioned) {
const uint8_t kData[] = {
// Encrypted
0x01,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Data
0x01, 0x02,
};
// Extracted from kData and zero extended to 16 bytes.
const uint8_t kExpectedIv[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
EXPECT_TRUE(decrypt_config->is_encrypted());
EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)),
decrypt_config->key_id());
EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)),
decrypt_config->iv());
EXPECT_TRUE(decrypt_config->subsamples().empty());
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedMissingNumPartitionField) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForOffsets) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 2
0x02,
// Partition 0 @ offset 3
0x00, 0x00, 0x00, 0x03,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForData) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 2
0x02,
// Partition 0 @ offset 3, partition 2 @ offset 5
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05,
// Should have more than 5 bytes of data
0x00, 0x01, 0x02, 0x03,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForData2) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 2
0x02,
// Partition 0 @ offset 3, partition 1 @ offset 5
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05,
// Should have more than 5 bytes of data
0x00, 0x01, 0x02, 0x03, 0x04,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedDecreasingOffsets) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 2
0x02,
// Partition 0 @ offset 3, partition 1 @ offset 2
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02,
// Should have more than 5 bytes of data
0x00, 0x01, 0x02, 0x03, 0x04,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedEvenNumberOfPartitions) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 2
0x02,
// Partition 0 @ offset 3, partition 1 @ offset 5
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05,
// Should have more than 5 bytes of data
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
};
// Extracted from kData and zero extended to 16 bytes.
const uint8_t kExpectedIv[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
EXPECT_TRUE(decrypt_config->is_encrypted());
EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)),
decrypt_config->key_id());
EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)),
decrypt_config->iv());
EXPECT_THAT(decrypt_config->subsamples(),
ElementsAre(SubsampleEntry(3, 2), SubsampleEntry(1, 0)));
EXPECT_EQ(18, data_offset);
}
TEST(WebMCryptoHelpersTest, EncryptedPartitionedOddNumberOfPartitions) {
const uint8_t kData[] = {
// Encrypted and Partitioned
0x03,
// IV
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
// Num partitions = 1
0x01,
// Partition 0 @ offset 3,
0x00, 0x00, 0x00, 0x03,
// Should have more than 3 bytes of data
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
};
// Extracted from kData and zero extended to 16 bytes.
const uint8_t kExpectedIv[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
std::unique_ptr<DecryptConfig> decrypt_config;
int data_offset;
ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId,
sizeof(kKeyId), &decrypt_config,
&data_offset));
EXPECT_TRUE(decrypt_config->is_encrypted());
EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)),
decrypt_config->key_id());
EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)),
decrypt_config->iv());
EXPECT_THAT(decrypt_config->subsamples(), ElementsAre(SubsampleEntry(3, 3)));
EXPECT_EQ(14, data_offset);
}
} // namespace media
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment