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/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 edash_packager {
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  scoped_ptr<AesCbcEncryptor> encryptor)
24  : RequestSigner(signer_name), aes_cbc_encryptor_(encryptor.Pass()) {
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  scoped_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, encryptor.Pass());
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(const std::string& signer_name,
57  scoped_ptr<RsaPrivateKey> rsa_private_key)
58  : RequestSigner(signer_name), rsa_private_key_(rsa_private_key.Pass()) {
59  DCHECK(rsa_private_key_);
60 }
61 RsaRequestSigner::~RsaRequestSigner() {}
62 
64  const std::string& signer_name,
65  const std::string& pkcs1_rsa_key) {
66  scoped_ptr<RsaPrivateKey> rsa_private_key(
67  RsaPrivateKey::Create(pkcs1_rsa_key));
68  if (!rsa_private_key)
69  return NULL;
70  return new RsaRequestSigner(signer_name, rsa_private_key.Pass());
71 }
72 
73 bool RsaRequestSigner::GenerateSignature(const std::string& message,
74  std::string* signature) {
75  return rsa_private_key_->GenerateSignature(message, signature);
76 }
77 
78 } // namespace media
79 } // 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.