Shaka Packager SDK
request_signer.cc
1 // Copyright 2014 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/base/request_signer.h>
8 
9 #include <absl/log/check.h>
10 #include <absl/log/log.h>
11 #include <mbedtls/md.h>
12 
13 #include <packager/media/base/aes_encryptor.h>
14 #include <packager/media/base/rsa_key.h>
15 
16 namespace shaka {
17 namespace media {
18 
19 RequestSigner::RequestSigner(const std::string& signer_name)
20  : signer_name_(signer_name) {}
21 RequestSigner::~RequestSigner() {}
22 
23 AesRequestSigner::AesRequestSigner(const std::string& signer_name,
24  std::unique_ptr<AesCbcEncryptor> encryptor)
25  : RequestSigner(signer_name), aes_cbc_encryptor_(std::move(encryptor)) {
26  DCHECK(aes_cbc_encryptor_);
27 }
28 AesRequestSigner::~AesRequestSigner() {}
29 
30 AesRequestSigner* AesRequestSigner::CreateSigner(
31  const std::string& signer_name,
32  const std::vector<uint8_t>& aes_key,
33  const std::vector<uint8_t>& iv) {
34  std::unique_ptr<AesCbcEncryptor> encryptor(
35  new AesCbcEncryptor(kPkcs5Padding, AesCryptor::kUseConstantIv));
36  if (!encryptor->InitializeWithIv(aes_key, iv))
37  return NULL;
38  return new AesRequestSigner(signer_name, std::move(encryptor));
39 }
40 
41 bool AesRequestSigner::GenerateSignature(const std::string& message,
42  std::string* signature) {
43  const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
44  DCHECK(md_info);
45 
46  std::string hash(mbedtls_md_get_size(md_info), 0);
47  CHECK_EQ(0,
48  mbedtls_md(md_info, reinterpret_cast<const uint8_t*>(message.data()),
49  message.size(), reinterpret_cast<uint8_t*>(hash.data())));
50 
51  return aes_cbc_encryptor_->Crypt(hash, signature);
52 }
53 
54 RsaRequestSigner::RsaRequestSigner(
55  const std::string& signer_name,
56  std::unique_ptr<RsaPrivateKey> rsa_private_key)
57  : RequestSigner(signer_name), rsa_private_key_(std::move(rsa_private_key)) {
58  DCHECK(rsa_private_key_);
59 }
60 RsaRequestSigner::~RsaRequestSigner() {}
61 
63  const std::string& signer_name,
64  const std::string& pkcs1_rsa_key) {
65  std::unique_ptr<RsaPrivateKey> rsa_private_key(
66  RsaPrivateKey::Create(pkcs1_rsa_key));
67  if (!rsa_private_key)
68  return NULL;
69  return new RsaRequestSigner(signer_name, std::move(rsa_private_key));
70 }
71 
72 bool RsaRequestSigner::GenerateSignature(const std::string& message,
73  std::string* signature) {
74  return rsa_private_key_->GenerateSignature(message, signature);
75 }
76 
77 } // namespace media
78 } // namespace shaka
AesRequestSigner uses AES-CBC signing.
Abstract class used for signature generation.
static RsaPrivateKey * Create(const std::string &serialized_key)
Definition: rsa_key.cc:78
RsaRequestSigner uses RSA-PSS signing.
bool GenerateSignature(const std::string &message, std::string *signature) override
RequestSigner implementation override.
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66