2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_RSA_KEY_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_RSA_KEY_H_
|
2014-01-06 23:38:39 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "packager/macros.h"
|
2014-01-06 23:38:39 +00:00
|
|
|
|
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:
|
2022-11-02 15:34:06 +00:00
|
|
|
RsaPrivateKey();
|
|
|
|
|
|
|
|
bool Deserialize(const std::string& serialized_key);
|
2014-01-06 23:38:39 +00:00
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
mbedtls_pk_context pk_context_;
|
|
|
|
mbedtls_entropy_context entropy_context_;
|
|
|
|
mbedtls_ctr_drbg_context prng_context_;
|
2014-01-06 23:38:39 +00:00
|
|
|
|
|
|
|
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:
|
2022-11-02 15:34:06 +00:00
|
|
|
RsaPublicKey();
|
|
|
|
|
|
|
|
bool Deserialize(const std::string& serialized_key);
|
2014-01-06 23:38:39 +00:00
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
mbedtls_pk_context pk_context_;
|
|
|
|
mbedtls_entropy_context entropy_context_;
|
|
|
|
mbedtls_ctr_drbg_context prng_context_;
|
2014-01-06 23:38:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_RSA_KEY_H_
|