DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/sha1.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/rsa_key.h"
13 
14 namespace edash_packager {
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  scoped_ptr<AesCbcPkcs5Encryptor> encryptor)
23  : RequestSigner(signer_name), aes_cbc_encryptor_(encryptor.Pass()) {
24  DCHECK(aes_cbc_encryptor_);
25 }
26 AesRequestSigner::~AesRequestSigner() {}
27 
28 AesRequestSigner* AesRequestSigner::CreateSigner(const std::string& signer_name,
29  const std::string& aes_key_hex,
30  const std::string& iv_hex) {
31  std::vector<uint8_t> aes_key;
32  if (!base::HexStringToBytes(aes_key_hex, &aes_key)) {
33  LOG(ERROR) << "Failed to convert hex string to bytes: " << aes_key_hex;
34  return NULL;
35  }
36  std::vector<uint8_t> iv;
37  if (!base::HexStringToBytes(iv_hex, &iv)) {
38  LOG(ERROR) << "Failed to convert hex string to bytes: " << iv_hex;
39  return NULL;
40  }
41 
42  scoped_ptr<AesCbcPkcs5Encryptor> encryptor(new AesCbcPkcs5Encryptor());
43  if (!encryptor->InitializeWithIv(aes_key, iv))
44  return NULL;
45  return new AesRequestSigner(signer_name, encryptor.Pass());
46 }
47 
48 bool AesRequestSigner::GenerateSignature(const std::string& message,
49  std::string* signature) {
50  aes_cbc_encryptor_->Encrypt(base::SHA1HashString(message), signature);
51  return true;
52 }
53 
54 RsaRequestSigner::RsaRequestSigner(const std::string& signer_name,
55  scoped_ptr<RsaPrivateKey> rsa_private_key)
56  : RequestSigner(signer_name), rsa_private_key_(rsa_private_key.Pass()) {
57  DCHECK(rsa_private_key_);
58 }
59 RsaRequestSigner::~RsaRequestSigner() {}
60 
62  const std::string& signer_name,
63  const std::string& pkcs1_rsa_key) {
64  scoped_ptr<RsaPrivateKey> rsa_private_key(
65  RsaPrivateKey::Create(pkcs1_rsa_key));
66  if (!rsa_private_key)
67  return NULL;
68  return new RsaRequestSigner(signer_name, rsa_private_key.Pass());
69 }
70 
71 bool RsaRequestSigner::GenerateSignature(const std::string& message,
72  std::string* signature) {
73  return rsa_private_key_->GenerateSignature(message, signature);
74 }
75 
76 } // namespace media
77 } // namespace edash_packager
bool GenerateSignature(const std::string &message, std::string *signature) override
RequestSigner implementation override.
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::string &aes_key_hex, const std::string &iv_hex)
static RsaPrivateKey * Create(const std::string &serialized_key)
Definition: rsa_key.cc:97
AesRequestSigner uses AES-CBC 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)
RsaRequestSigner uses RSA-PSS signing.
Abstract class used for signature generation.