RSAKey.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // RSAKey.h
  3. //
  4. // Library: Crypto
  5. // Package: RSA
  6. // Module: RSAKey
  7. //
  8. // Definition of the RSAKey class.
  9. //
  10. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_RSAKey_INCLUDED
  16. #define Crypto_RSAKey_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Crypto/KeyPair.h"
  19. #include "Poco/Crypto/RSAKeyImpl.h"
  20. namespace Poco {
  21. namespace Crypto {
  22. class X509Certificate;
  23. class PKCS12Container;
  24. class Crypto_API RSAKey : public KeyPair
  25. /// This class stores an RSA key pair, consisting
  26. /// of private and public key. Storage of the private
  27. /// key is optional.
  28. ///
  29. /// If a private key is available, the RSAKey can be
  30. /// used for decrypting data (encrypted with the public key)
  31. /// or computing secure digital signatures.
  32. {
  33. public:
  34. enum KeyLength
  35. {
  36. KL_512 = 512,
  37. KL_1024 = 1024,
  38. KL_2048 = 2048,
  39. KL_4096 = 4096
  40. };
  41. enum Exponent
  42. {
  43. EXP_SMALL = 0,
  44. EXP_LARGE
  45. };
  46. RSAKey(const EVPPKey& key);
  47. /// Constructs ECKeyImpl by extracting the EC key.
  48. RSAKey(const X509Certificate& cert);
  49. /// Extracts the RSA public key from the given certificate.
  50. RSAKey(const PKCS12Container& cert);
  51. /// Extracts the RSA private key from the given certificate.
  52. RSAKey(KeyLength keyLength, Exponent exp);
  53. /// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
  54. /// Can be used to sign data and verify signatures.
  55. RSAKey(const std::string& publicKeyFile,
  56. const std::string& privateKeyFile = "",
  57. const std::string& privateKeyPassphrase = "");
  58. /// Creates the RSAKey, by reading public and private key from the given files and
  59. /// using the given passphrase for the private key.
  60. ///
  61. /// Cannot be used for signing or decryption unless a private key is available.
  62. ///
  63. /// If a private key is specified, you don't need to specify a public key file.
  64. /// OpenSSL will auto-create the public key from the private key.
  65. RSAKey(std::istream* pPublicKeyStream,
  66. std::istream* pPrivateKeyStream = 0,
  67. const std::string& privateKeyPassphrase = "");
  68. /// Creates the RSAKey, by reading public and private key from the given streams and
  69. /// using the given passphrase for the private key.
  70. ///
  71. /// Cannot be used for signing or decryption unless a private key is available.
  72. ///
  73. /// If a private key is specified, you don't need to specify a public key file.
  74. /// OpenSSL will auto-create the public key from the private key.
  75. ~RSAKey();
  76. /// Destroys the RSAKey.
  77. RSAKeyImpl::ByteVec modulus() const;
  78. /// Returns the RSA modulus.
  79. RSAKeyImpl::ByteVec encryptionExponent() const;
  80. /// Returns the RSA encryption exponent.
  81. RSAKeyImpl::ByteVec decryptionExponent() const;
  82. /// Returns the RSA decryption exponent.
  83. RSAKeyImpl::Ptr impl() const;
  84. /// Returns the impl object.
  85. private:
  86. RSAKeyImpl::Ptr _pImpl;
  87. };
  88. //
  89. // inlines
  90. //
  91. inline RSAKeyImpl::Ptr RSAKey::impl() const
  92. {
  93. return _pImpl;
  94. }
  95. } } // namespace Poco::Crypto
  96. #endif // Crypto_RSAKey_INCLUDED