2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2014-01-06 23:38:39 +00:00
|
|
|
//
|
|
|
|
// Declaration of classes representing RSA private and public keys used
|
|
|
|
// for message signing, signature verification, encryption and decryption.
|
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_RSA_KEY_H_
|
|
|
|
#define MEDIA_BASE_RSA_KEY_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/macros.h"
|
2014-01-06 23:38:39 +00:00
|
|
|
|
|
|
|
struct rsa_st;
|
|
|
|
typedef struct rsa_st RSA;
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-01-06 23:38:39 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Rsa private key, used for message signing and decryption.
|
2014-01-06 23:38:39 +00:00
|
|
|
class RsaPrivateKey {
|
|
|
|
public:
|
|
|
|
~RsaPrivateKey();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create an RsaPrivateKey object using a DER encoded PKCS#1 RSAPrivateKey.
|
|
|
|
/// @return The created RsaPrivateKey object on success, NULL otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
static RsaPrivateKey* Create(const std::string& serialized_key);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Decrypt a message using RSA-OAEP.
|
|
|
|
/// @param decrypted_message must not be NULL.
|
|
|
|
/// @return true if successful, false otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
bool Decrypt(const std::string& encrypted_message,
|
|
|
|
std::string* decrypted_message);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Generate RSASSA-PSS signature.
|
|
|
|
/// @param signature must not be NULL.
|
|
|
|
/// @return true if successful, false otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
bool GenerateSignature(const std::string& message, std::string* signature);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// RsaPrivateKey takes owership of |rsa_key|.
|
|
|
|
explicit RsaPrivateKey(RSA* rsa_key);
|
|
|
|
|
|
|
|
RSA* rsa_key_; // owned
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RsaPrivateKey);
|
|
|
|
};
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Rsa public key, used for signature verification and encryption.
|
2014-01-06 23:38:39 +00:00
|
|
|
class RsaPublicKey {
|
|
|
|
public:
|
|
|
|
~RsaPublicKey();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create an RsaPublicKey object using a DER encoded PKCS#1 RSAPublicKey.
|
|
|
|
/// @return The created RsaPrivateKey object on success, NULL otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
static RsaPublicKey* Create(const std::string& serialized_key);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Encrypt a message using RSA-OAEP.
|
|
|
|
/// @param encrypted_message must not be NULL.
|
|
|
|
/// @return true if successful, false otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
bool Encrypt(const std::string& clear_message,
|
|
|
|
std::string* encrypted_message);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Verify RSASSA-PSS signature.
|
|
|
|
/// @return true if verification succeeds, false otherwise.
|
2014-01-06 23:38:39 +00:00
|
|
|
bool VerifySignature(const std::string& message,
|
|
|
|
const std::string& signature);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// RsaPublicKey takes owership of |rsa_key|.
|
|
|
|
explicit RsaPublicKey(RSA* rsa_key);
|
|
|
|
|
|
|
|
RSA* rsa_key_; // owned
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RsaPublicKey);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-01-06 23:38:39 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_RSA_KEY_H_
|