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-04-16 01:09:32 +00:00
|
|
|
#include "media/base/encryption_key_source.h"
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2014-01-13 19:34:08 +00:00
|
|
|
#include "media/base/aes_encryptor.h"
|
2014-04-15 22:18:26 +00:00
|
|
|
#include "media/base/buffer_writer.h"
|
2014-01-13 19:34:08 +00:00
|
|
|
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace {
|
2014-01-13 19:34:08 +00:00
|
|
|
const uint8 kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6,
|
|
|
|
0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc,
|
|
|
|
0xd5, 0x1d, 0x21, 0xed};
|
2013-11-12 20:34:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
EncryptionKey::EncryptionKey() {}
|
|
|
|
EncryptionKey::~EncryptionKey() {}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
EncryptionKeySource::~EncryptionKeySource() {}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
Status EncryptionKeySource::GetKey(TrackType track_type, EncryptionKey* key) {
|
2014-04-15 22:18:26 +00:00
|
|
|
DCHECK(key);
|
|
|
|
DCHECK(encryption_key_);
|
|
|
|
*key = *encryption_key_;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
Status EncryptionKeySource::GetCryptoPeriodKey(size_t crypto_period_index,
|
|
|
|
TrackType track_type,
|
|
|
|
EncryptionKey* key) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return Status(error::UNIMPLEMENTED, "");
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
scoped_ptr<EncryptionKeySource> EncryptionKeySource::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-04-16 01:09:32 +00:00
|
|
|
return scoped_ptr<EncryptionKeySource>();
|
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-04-16 01:09:32 +00:00
|
|
|
return scoped_ptr<EncryptionKeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8> pssh_data;
|
|
|
|
if (!base::HexStringToBytes(pssh_data_hex, &pssh_data)) {
|
|
|
|
LOG(ERROR) << "Cannot parse pssh_hex " << pssh_data_hex;
|
2014-04-16 01:09:32 +00:00
|
|
|
return scoped_ptr<EncryptionKeySource>();
|
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-04-16 01:09:32 +00:00
|
|
|
return scoped_ptr<EncryptionKeySource>();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encryption_key->pssh = PsshBoxFromPsshData(pssh_data);
|
2014-04-16 01:09:32 +00:00
|
|
|
return scoped_ptr<EncryptionKeySource>(
|
|
|
|
new EncryptionKeySource(encryption_key.Pass()));
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
EncryptionKeySource::TrackType EncryptionKeySource::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;
|
|
|
|
LOG(WARNING) << "Unexpected track type: " << track_type_string;
|
|
|
|
return TRACK_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
std::string EncryptionKeySource::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-04-16 01:09:32 +00:00
|
|
|
std::vector<uint8> EncryptionKeySource::PsshBoxFromPsshData(
|
2014-04-15 22:18:26 +00:00
|
|
|
const std::vector<uint8>& pssh_data) {
|
|
|
|
const uint8 kPsshFourCC[] = {'p', 's', 's', 'h'};
|
|
|
|
const uint32 kVersionAndFlags = 0;
|
|
|
|
|
|
|
|
const uint32 pssh_data_size = pssh_data.size();
|
|
|
|
const uint32 total_size =
|
|
|
|
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);
|
|
|
|
return std::vector<uint8>(writer.Buffer(), writer.Buffer() + writer.Size());
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
EncryptionKeySource::EncryptionKeySource() {}
|
|
|
|
EncryptionKeySource::EncryptionKeySource(
|
|
|
|
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
|