DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
request_signer.cc
1 // Copyright 2014 Google Inc. 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 "packager/base/logging.h"
10 #include "packager/base/sha1.h"
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/rsa_key.h"
13 
14 namespace shaka {
15 namespace media {
16 
17 RequestSigner::RequestSigner(const std::string& signer_name)
18  : signer_name_(signer_name) {}
19 RequestSigner::~RequestSigner() {}
20 
21 AesRequestSigner::AesRequestSigner(const std::string& signer_name,
22  std::unique_ptr<AesCbcEncryptor> encryptor)
23  : RequestSigner(signer_name), aes_cbc_encryptor_(std::move(encryptor)) {
24  DCHECK(aes_cbc_encryptor_);
25 }
26 AesRequestSigner::~AesRequestSigner() {}
27 
29  const std::string& signer_name,
30  const std::vector<uint8_t>& aes_key,
31  const std::vector<uint8_t>& iv) {
32  std::unique_ptr<AesCbcEncryptor> encryptor(
33  new AesCbcEncryptor(kPkcs5Padding, AesCryptor::kUseConstantIv));
34  if (!encryptor->InitializeWithIv(aes_key, iv))
35  return NULL;
36  return new AesRequestSigner(signer_name, std::move(encryptor));
37 }
38 
39 bool AesRequestSigner::GenerateSignature(const std::string& message,
40  std::string* signature) {
41  aes_cbc_encryptor_->Crypt(base::SHA1HashString(message), signature);
42  return true;
43 }
44 
45 RsaRequestSigner::RsaRequestSigner(
46  const std::string& signer_name,
47  std::unique_ptr<RsaPrivateKey> rsa_private_key)
48  : RequestSigner(signer_name), rsa_private_key_(std::move(rsa_private_key)) {
49  DCHECK(rsa_private_key_);
50 }
51 RsaRequestSigner::~RsaRequestSigner() {}
52 
54  const std::string& signer_name,
55  const std::string& pkcs1_rsa_key) {
56  std::unique_ptr<RsaPrivateKey> rsa_private_key(
57  RsaPrivateKey::Create(pkcs1_rsa_key));
58  if (!rsa_private_key)
59  return NULL;
60  return new RsaRequestSigner(signer_name, std::move(rsa_private_key));
61 }
62 
63 bool RsaRequestSigner::GenerateSignature(const std::string& message,
64  std::string* signature) {
65  return rsa_private_key_->GenerateSignature(message, signature);
66 }
67 
68 } // namespace media
69 } // namespace shaka
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::vector< uint8_t > &aes_key, const std::vector< uint8_t > &iv)
Abstract class used for signature generation.
RsaRequestSigner uses RSA-PSS signing.
bool GenerateSignature(const std::string &message, std::string *signature) override
RequestSigner implementation override.
bool GenerateSignature(const std::string &message, std::string *signature) override
RequestSigner implementation override.
AesRequestSigner uses AES-CBC signing.
static RsaPrivateKey * Create(const std::string &serialized_key)
Definition: rsa_key.cc:96