2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/request_signer.h>
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <mbedtls/md.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/aes_encryptor.h>
|
|
|
|
#include <packager/media/base/rsa_key.h>
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
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-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<AesCbcEncryptor> encryptor)
|
|
|
|
: RequestSigner(signer_name), aes_cbc_encryptor_(std::move(encryptor)) {
|
2014-01-14 04:52:05 +00:00
|
|
|
DCHECK(aes_cbc_encryptor_);
|
|
|
|
}
|
|
|
|
AesRequestSigner::~AesRequestSigner() {}
|
|
|
|
|
2017-06-14 23:18:16 +00:00
|
|
|
AesRequestSigner* AesRequestSigner::CreateSigner(
|
|
|
|
const std::string& signer_name,
|
|
|
|
const std::vector<uint8_t>& aes_key,
|
|
|
|
const std::vector<uint8_t>& iv) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_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;
|
2016-08-17 17:41:40 +00:00
|
|
|
return new AesRequestSigner(signer_name, std::move(encryptor));
|
2014-01-14 04:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AesRequestSigner::GenerateSignature(const std::string& message,
|
|
|
|
std::string* signature) {
|
2022-11-04 22:46:41 +00:00
|
|
|
const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
|
|
|
DCHECK(md_info);
|
|
|
|
|
|
|
|
std::string hash(mbedtls_md_get_size(md_info), 0);
|
|
|
|
CHECK_EQ(0,
|
|
|
|
mbedtls_md(md_info, reinterpret_cast<const uint8_t*>(message.data()),
|
|
|
|
message.size(), reinterpret_cast<uint8_t*>(hash.data())));
|
|
|
|
|
|
|
|
return aes_cbc_encryptor_->Crypt(hash, signature);
|
2014-01-14 04:52:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
RsaRequestSigner::RsaRequestSigner(
|
|
|
|
const std::string& signer_name,
|
|
|
|
std::unique_ptr<RsaPrivateKey> rsa_private_key)
|
|
|
|
: RequestSigner(signer_name), rsa_private_key_(std::move(rsa_private_key)) {
|
2014-01-14 04:52:05 +00:00
|
|
|
DCHECK(rsa_private_key_);
|
|
|
|
}
|
|
|
|
RsaRequestSigner::~RsaRequestSigner() {}
|
|
|
|
|
|
|
|
RsaRequestSigner* RsaRequestSigner::CreateSigner(
|
|
|
|
const std::string& signer_name,
|
|
|
|
const std::string& pkcs1_rsa_key) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<RsaPrivateKey> rsa_private_key(
|
2014-01-14 04:52:05 +00:00
|
|
|
RsaPrivateKey::Create(pkcs1_rsa_key));
|
|
|
|
if (!rsa_private_key)
|
|
|
|
return NULL;
|
2016-08-17 17:41:40 +00:00
|
|
|
return new RsaRequestSigner(signer_name, std::move(rsa_private_key));
|
2014-01-14 04:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsaRequestSigner::GenerateSignature(const std::string& message,
|
|
|
|
std::string* signature) {
|
|
|
|
return rsa_private_key_->GenerateSignature(message, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|