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
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/sys_byteorder.h"
|
|
|
|
#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.
|
2015-10-14 22:46:23 +00:00
|
|
|
std::string GenerateWebMCounterBlock(const uint8_t* iv, int iv_size) {
|
2015-10-08 21:48:07 +00:00
|
|
|
std::string counter_block(reinterpret_cast<const char*>(iv), iv_size);
|
|
|
|
counter_block.append(DecryptConfig::kDecryptionKeySize - iv_size, 0);
|
|
|
|
return counter_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMCreateDecryptConfig(const uint8_t* data,
|
|
|
|
int data_size,
|
|
|
|
const uint8_t* key_id,
|
|
|
|
int key_id_size,
|
2015-10-08 21:48:07 +00:00
|
|
|
scoped_ptr<DecryptConfig>* decrypt_config,
|
|
|
|
int* data_offset) {
|
|
|
|
if (data_size < kWebMSignalByteSize) {
|
|
|
|
DVLOG(1) << "Got a block from an encrypted stream with no data.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
uint8_t signal_byte = data[0];
|
2015-10-08 21:48:07 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (signal_byte & kWebMFlagEncryptedFrame) {
|
|
|
|
if (data_size < kWebMSignalByteSize + kWebMIvSize) {
|
|
|
|
DVLOG(1) << "Got an encrypted block with not enough data " << data_size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize);
|
|
|
|
frame_offset += kWebMIvSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypt_config->reset(new DecryptConfig(
|
2015-10-14 23:12:10 +00:00
|
|
|
std::vector<uint8_t>(key_id, key_id + key_id_size),
|
|
|
|
std::vector<uint8_t>(counter_block.begin(), counter_block.end()),
|
2016-04-08 18:41:17 +00:00
|
|
|
std::vector<SubsampleEntry>()));
|
2015-10-08 21:48:07 +00:00
|
|
|
*data_offset = frame_offset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|