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<AesCbcEncryptor> 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<AesCbcEncryptor> encryptor(
43  new AesCbcEncryptor(kPkcs5Padding, !kChainAcrossCalls));
44  if (!encryptor->InitializeWithIv(aes_key, iv))
45  return NULL;
46  return new AesRequestSigner(signer_name, encryptor.Pass());
47 }
48 
49 bool AesRequestSigner::GenerateSignature(const std::string& message,
50  std::string* signature) {
51  aes_cbc_encryptor_->Encrypt(base::SHA1HashString(message), signature);
52  return true;
53 }
54 
55 RsaRequestSigner::RsaRequestSigner(const std::string& signer_name,
56  scoped_ptr<RsaPrivateKey> rsa_private_key)
57  : RequestSigner(signer_name), rsa_private_key_(rsa_private_key.Pass()) {
58  DCHECK(rsa_private_key_);
59 }
60 RsaRequestSigner::~RsaRequestSigner() {}
61 
63  const std::string& signer_name,
64  const std::string& pkcs1_rsa_key) {
65  scoped_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, rsa_private_key.Pass());
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 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.