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/base/strings/string_number_conversions.h"
12 #include "packager/media/base/aes_encryptor.h"
13 #include "packager/media/base/rsa_key.h"
14 
15 namespace shaka {
16 namespace media {
17 
18 RequestSigner::RequestSigner(const std::string& signer_name)
19  : signer_name_(signer_name) {}
20 RequestSigner::~RequestSigner() {}
21 
22 AesRequestSigner::AesRequestSigner(const std::string& signer_name,
23  std::unique_ptr<AesCbcEncryptor> encryptor)
24  : RequestSigner(signer_name), aes_cbc_encryptor_(std::move(encryptor)) {
25  DCHECK(aes_cbc_encryptor_);
26 }
27 AesRequestSigner::~AesRequestSigner() {}
28 
29 AesRequestSigner* AesRequestSigner::CreateSigner(const std::string& signer_name,
30  const std::string& aes_key_hex,
31  const std::string& iv_hex) {
32  std::vector<uint8_t> aes_key;
33  if (!base::HexStringToBytes(aes_key_hex, &aes_key)) {
34  LOG(ERROR) << "Failed to convert hex string to bytes: " << aes_key_hex;
35  return NULL;
36  }
37  std::vector<uint8_t> iv;
38  if (!base::HexStringToBytes(iv_hex, &iv)) {
39  LOG(ERROR) << "Failed to convert hex string to bytes: " << iv_hex;
40  return NULL;
41  }
42 
43  std::unique_ptr<AesCbcEncryptor> encryptor(
44  new AesCbcEncryptor(kPkcs5Padding, AesCryptor::kUseConstantIv));
45  if (!encryptor->InitializeWithIv(aes_key, iv))
46  return NULL;
47  return new AesRequestSigner(signer_name, std::move(encryptor));
48 }
49 
50 bool AesRequestSigner::GenerateSignature(const std::string& message,
51  std::string* signature) {
52  aes_cbc_encryptor_->Crypt(base::SHA1HashString(message), signature);
53  return true;
54 }
55 
56 RsaRequestSigner::RsaRequestSigner(
57  const std::string& signer_name,
58  std::unique_ptr<RsaPrivateKey> rsa_private_key)
59  : RequestSigner(signer_name), rsa_private_key_(std::move(rsa_private_key)) {
60  DCHECK(rsa_private_key_);
61 }
62 RsaRequestSigner::~RsaRequestSigner() {}
63 
65  const std::string& signer_name,
66  const std::string& pkcs1_rsa_key) {
67  std::unique_ptr<RsaPrivateKey> rsa_private_key(
68  RsaPrivateKey::Create(pkcs1_rsa_key));
69  if (!rsa_private_key)
70  return NULL;
71  return new RsaRequestSigner(signer_name, std::move(rsa_private_key));
72 }
73 
74 bool RsaRequestSigner::GenerateSignature(const std::string& message,
75  std::string* signature) {
76  return rsa_private_key_->GenerateSignature(message, signature);
77 }
78 
79 } // namespace media
80 } // namespace shaka
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
Abstract class used for signature generation.
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::string &aes_key_hex, const std::string &iv_hex)
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