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
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/request_signer.h"
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
#include "packager/base/logging.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/sha1.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/media/base/aes_encryptor.h"
|
|
|
|
#include "packager/media/base/rsa_key.h"
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-01-14 04:52:05 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
RequestSigner::RequestSigner(const std::string& signer_name)
|
|
|
|
: signer_name_(signer_name) {}
|
|
|
|
RequestSigner::~RequestSigner() {}
|
|
|
|
|
|
|
|
AesRequestSigner::AesRequestSigner(const std::string& signer_name,
|
2016-03-25 18:02:43 +00:00
|
|
|
scoped_ptr<AesCbcEncryptor> encryptor)
|
2014-01-14 04:52:05 +00:00
|
|
|
: RequestSigner(signer_name), aes_cbc_encryptor_(encryptor.Pass()) {
|
|
|
|
DCHECK(aes_cbc_encryptor_);
|
|
|
|
}
|
|
|
|
AesRequestSigner::~AesRequestSigner() {}
|
|
|
|
|
|
|
|
AesRequestSigner* AesRequestSigner::CreateSigner(const std::string& signer_name,
|
|
|
|
const std::string& aes_key_hex,
|
|
|
|
const std::string& iv_hex) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> aes_key;
|
2014-01-14 04:52:05 +00:00
|
|
|
if (!base::HexStringToBytes(aes_key_hex, &aes_key)) {
|
|
|
|
LOG(ERROR) << "Failed to convert hex string to bytes: " << aes_key_hex;
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv;
|
2014-01-14 04:52:05 +00:00
|
|
|
if (!base::HexStringToBytes(iv_hex, &iv)) {
|
|
|
|
LOG(ERROR) << "Failed to convert hex string to bytes: " << iv_hex;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
scoped_ptr<AesCbcEncryptor> encryptor(
|
2016-04-13 17:52:41 +00:00
|
|
|
new AesCbcEncryptor(kPkcs5Padding, AesCryptor::kUseConstantIv));
|
2014-01-14 04:52:05 +00:00
|
|
|
if (!encryptor->InitializeWithIv(aes_key, iv))
|
|
|
|
return NULL;
|
|
|
|
return new AesRequestSigner(signer_name, encryptor.Pass());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AesRequestSigner::GenerateSignature(const std::string& message,
|
|
|
|
std::string* signature) {
|
2016-04-06 00:19:16 +00:00
|
|
|
aes_cbc_encryptor_->Crypt(base::SHA1HashString(message), signature);
|
2014-01-14 04:52:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsaRequestSigner::RsaRequestSigner(const std::string& signer_name,
|
|
|
|
scoped_ptr<RsaPrivateKey> rsa_private_key)
|
|
|
|
: RequestSigner(signer_name), rsa_private_key_(rsa_private_key.Pass()) {
|
|
|
|
DCHECK(rsa_private_key_);
|
|
|
|
}
|
|
|
|
RsaRequestSigner::~RsaRequestSigner() {}
|
|
|
|
|
|
|
|
RsaRequestSigner* RsaRequestSigner::CreateSigner(
|
|
|
|
const std::string& signer_name,
|
|
|
|
const std::string& pkcs1_rsa_key) {
|
|
|
|
scoped_ptr<RsaPrivateKey> rsa_private_key(
|
|
|
|
RsaPrivateKey::Create(pkcs1_rsa_key));
|
|
|
|
if (!rsa_private_key)
|
|
|
|
return NULL;
|
|
|
|
return new RsaRequestSigner(signer_name, rsa_private_key.Pass());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsaRequestSigner::GenerateSignature(const std::string& message,
|
|
|
|
std::string* signature) {
|
|
|
|
return rsa_private_key_->GenerateSignature(message, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|