2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/key_source.h"
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/media/base/aes_encryptor.h"
|
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2014-01-13 19:34:08 +00:00
|
|
|
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace {
|
2015-09-25 22:48:18 +00:00
|
|
|
// TODO(kqyang): Consider making it configurable.
|
|
|
|
const char kDefaultUUID[] = "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
|
2015-07-08 00:52:28 +00:00
|
|
|
const char kDefaultSystemName[] = "";
|
2013-11-12 20:34:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
EncryptionKey::EncryptionKey() {}
|
|
|
|
EncryptionKey::~EncryptionKey() {}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::~KeySource() {}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status KeySource::FetchKeys(const std::vector<uint8_t>& content_id,
|
2014-08-20 23:51:15 +00:00
|
|
|
const std::string& policy) {
|
2014-10-09 19:56:51 +00:00
|
|
|
// Do nothing for fixed key encryption/decryption.
|
2014-08-20 23:51:15 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2016-02-19 18:24:33 +00:00
|
|
|
Status KeySource::FetchKeys(const std::vector<uint8_t>& pssh_box) {
|
|
|
|
// Do nothing for fixed key encryption/decryption.
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status KeySource::FetchKeys(const std::vector<std::vector<uint8_t>>& key_ids) {
|
2014-10-09 19:56:51 +00:00
|
|
|
// Do nothing for fixed key encryption/decryption.
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status KeySource::FetchKeys(uint32_t asset_id) {
|
|
|
|
// Do nothing for fixed key encryption/decryption.
|
2014-08-20 23:51:15 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status KeySource::GetKey(TrackType track_type, EncryptionKey* key) {
|
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(encryption_key_);
|
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status KeySource::GetKey(const std::vector<uint8_t>& key_id,
|
2014-08-20 23:51:15 +00:00
|
|
|
EncryptionKey* key) {
|
2014-04-15 22:18:26 +00:00
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(encryption_key_);
|
2014-08-25 22:51:19 +00:00
|
|
|
if (key_id != encryption_key_->key_id) {
|
|
|
|
return Status(error::NOT_FOUND, std::string("Key for key ID ") +
|
|
|
|
base::HexEncode(&key_id[0], key_id.size()) +
|
|
|
|
" was not found.");
|
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status KeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
|
2014-08-20 23:51:15 +00:00
|
|
|
TrackType track_type,
|
|
|
|
EncryptionKey* key) {
|
2015-09-25 22:48:18 +00:00
|
|
|
*key = *encryption_key_;
|
|
|
|
// A naive key rotation algorithm is implemented here by left rotating the
|
|
|
|
// key, key_id and pssh. Note that this implementation is only intended for
|
|
|
|
// testing purpose. The actual key rotation algorithm can be much more
|
|
|
|
// complicated.
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "This naive key rotation algorithm should not be used in production.";
|
|
|
|
std::rotate(key->key_id.begin(),
|
|
|
|
key->key_id.begin() + (crypto_period_index % key->key_id.size()),
|
|
|
|
key->key_id.end());
|
|
|
|
std::rotate(key->key.begin(),
|
|
|
|
key->key.begin() + (crypto_period_index % key->key.size()),
|
|
|
|
key->key.end());
|
|
|
|
const size_t kPsshHeaderSize = 32u;
|
|
|
|
std::vector<uint8_t> pssh_data(key->pssh.begin() + kPsshHeaderSize,
|
|
|
|
key->pssh.end());
|
|
|
|
std::rotate(pssh_data.begin(),
|
|
|
|
pssh_data.begin() + (crypto_period_index % pssh_data.size()),
|
|
|
|
pssh_data.end());
|
|
|
|
key->pssh = PsshBoxFromPsshData(pssh_data);
|
|
|
|
return Status::OK;
|
2014-04-18 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 00:52:28 +00:00
|
|
|
std::string KeySource::UUID() {
|
|
|
|
return kDefaultUUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeySource::SystemName() {
|
|
|
|
return kDefaultSystemName;
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
scoped_ptr<KeySource> KeySource::CreateFromHexStrings(
|
2014-04-15 22:18:26 +00:00
|
|
|
const std::string& key_id_hex,
|
|
|
|
const std::string& key_hex,
|
|
|
|
const std::string& pssh_data_hex,
|
|
|
|
const std::string& iv_hex) {
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
|
|
|
|
|
|
|
|
if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
|
|
|
|
LOG(ERROR) << "Cannot parse key_id_hex " << key_id_hex;
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
|
|
|
|
LOG(ERROR) << "Cannot parse key_hex " << key_hex;
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> pssh_data;
|
2014-09-08 18:43:25 +00:00
|
|
|
if (!pssh_data_hex.empty() &&
|
|
|
|
!base::HexStringToBytes(pssh_data_hex, &pssh_data)) {
|
2014-04-15 22:18:26 +00:00
|
|
|
LOG(ERROR) << "Cannot parse pssh_hex " << pssh_data_hex;
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv_hex.empty()) {
|
|
|
|
if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
|
|
|
|
LOG(ERROR) << "Cannot parse iv_hex " << iv_hex;
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encryption_key->pssh = PsshBoxFromPsshData(pssh_data);
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>(
|
|
|
|
new KeySource(encryption_key.Pass()));
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::TrackType KeySource::GetTrackTypeFromString(
|
2014-04-15 22:18:26 +00:00
|
|
|
const std::string& track_type_string) {
|
|
|
|
if (track_type_string == "SD")
|
|
|
|
return TRACK_TYPE_SD;
|
|
|
|
if (track_type_string == "HD")
|
|
|
|
return TRACK_TYPE_HD;
|
|
|
|
if (track_type_string == "AUDIO")
|
|
|
|
return TRACK_TYPE_AUDIO;
|
2014-11-06 23:13:35 +00:00
|
|
|
if (track_type_string == "UNSPECIFIED")
|
|
|
|
return TRACK_TYPE_UNSPECIFIED;
|
2014-04-15 22:18:26 +00:00
|
|
|
LOG(WARNING) << "Unexpected track type: " << track_type_string;
|
|
|
|
return TRACK_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
std::string KeySource::TrackTypeToString(TrackType track_type) {
|
2014-04-15 22:18:26 +00:00
|
|
|
switch (track_type) {
|
|
|
|
case TRACK_TYPE_SD:
|
|
|
|
return "SD";
|
|
|
|
case TRACK_TYPE_HD:
|
|
|
|
return "HD";
|
|
|
|
case TRACK_TYPE_AUDIO:
|
|
|
|
return "AUDIO";
|
|
|
|
default:
|
|
|
|
NOTIMPLEMENTED() << "Unknown track type: " << track_type;
|
|
|
|
return "UNKNOWN";
|
2014-01-13 19:34:08 +00:00
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> KeySource::PsshBoxFromPsshData(
|
|
|
|
const std::vector<uint8_t>& pssh_data) {
|
|
|
|
const uint8_t kPsshFourCC[] = {'p', 's', 's', 'h'};
|
|
|
|
const uint32_t kVersionAndFlags = 0;
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t pssh_data_size = pssh_data.size();
|
|
|
|
const uint32_t total_size =
|
2014-04-15 22:18:26 +00:00
|
|
|
sizeof(total_size) + sizeof(kPsshFourCC) + sizeof(kVersionAndFlags) +
|
|
|
|
sizeof(kWidevineSystemId) + sizeof(pssh_data_size) + pssh_data_size;
|
|
|
|
|
|
|
|
BufferWriter writer;
|
|
|
|
writer.AppendInt(total_size);
|
|
|
|
writer.AppendArray(kPsshFourCC, sizeof(kPsshFourCC));
|
|
|
|
writer.AppendInt(kVersionAndFlags);
|
|
|
|
writer.AppendArray(kWidevineSystemId, sizeof(kWidevineSystemId));
|
|
|
|
writer.AppendInt(pssh_data_size);
|
|
|
|
writer.AppendVector(pssh_data);
|
2014-09-30 21:52:21 +00:00
|
|
|
return std::vector<uint8_t>(writer.Buffer(), writer.Buffer() + writer.Size());
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::KeySource() {}
|
|
|
|
KeySource::KeySource(scoped_ptr<EncryptionKey> encryption_key)
|
2014-04-15 22:18:26 +00:00
|
|
|
: encryption_key_(encryption_key.Pass()) {
|
|
|
|
DCHECK(encryption_key_);
|
2014-01-13 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 20:34:58 +00:00
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|