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 
16 
17 #ifndef Crypto_RSAKey_INCLUDED
18 #define Crypto_RSAKey_INCLUDED
19 
20 
21 #include "Poco/Crypto/Crypto.h"
22 #include "Poco/Crypto/KeyPair.h"
23 #include "Poco/Crypto/RSAKeyImpl.h"
24 
25 
26 namespace Poco {
27 namespace Crypto {
28 
29 
30 class X509Certificate;
31 class PKCS12Container;
32 
33 
34 class Crypto_API RSAKey: public KeyPair
35 	/// This class stores an RSA key pair, consisting
36 	/// of private and public key. Storage of the private
37 	/// key is optional.
38 	///
39 	/// If a private key is available, the RSAKey can be
40 	/// used for decrypting data (encrypted with the public key)
41 	/// or computing secure digital signatures.
42 {
43 public:
44 	enum KeyLength
45 	{
46 		KL_512  = 512,
47 		KL_1024 = 1024,
48 		KL_2048 = 2048,
49 		KL_4096 = 4096
50 	};
51 
52 	enum Exponent
53 	{
54 		EXP_SMALL = 0,
55 		EXP_LARGE
56 	};
57 
58 	RSAKey(const EVPPKey& key);
59 		/// Constructs ECKeyImpl by extracting the EC key.
60 
61 	RSAKey(const X509Certificate& cert);
62 		/// Extracts the RSA public key from the given certificate.
63 
64 	RSAKey(const PKCS12Container& cert);
65 		/// Extracts the RSA private key from the given certificate.
66 
67 	RSAKey(KeyLength keyLength, Exponent exp);
68 		/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
69 		/// Can be used to sign data and verify signatures.
70 
71 	RSAKey(const std::string& publicKeyFile,
72 		const std::string& privateKeyFile = "",
73 		const std::string& privateKeyPassphrase = "");
74 		/// Creates the RSAKey, by reading public and private key from the given files and
75 		/// using the given passphrase for the private key.
76 		///
77 		/// Cannot be used for signing or decryption unless a private key is available.
78 		///
79 		/// If a private key is specified, you don't need to specify a public key file.
80 		/// OpenSSL will auto-create the public key from the private key.
81 
82 	RSAKey(std::istream* pPublicKeyStream,
83 		std::istream* pPrivateKeyStream = 0,
84 		const std::string& privateKeyPassphrase = "");
85 		/// Creates the RSAKey, by reading public and private key from the given streams and
86 		/// using the given passphrase for the private key.
87 		///
88 		/// Cannot be used for signing or decryption unless a private key is available.
89 		///
90 		/// If a private key is specified, you don't need to specify a public key file.
91 		/// OpenSSL will auto-create the public key from the private key.
92 
93 	RSAKey(const RSAKey& other);
94 		/// Copy constructor.
95 
96 	RSAKey(RSAKey&& other) noexcept;
97 		/// Move constructor.
98 
99 	~RSAKey();
100 		/// Destroys the RSAKey.
101 
102 	RSAKey& operator = (const RSAKey& other);
103 		/// Assignment.
104 
105 	RSAKey& operator = (RSAKey&& other) noexcept;
106 		/// Move assignment.
107 
108 	RSAKeyImpl::ByteVec modulus() const;
109 		/// Returns the RSA modulus.
110 
111 	RSAKeyImpl::ByteVec encryptionExponent() const;
112 		/// Returns the RSA encryption exponent.
113 
114 	RSAKeyImpl::ByteVec decryptionExponent() const;
115 		/// Returns the RSA decryption exponent.
116 
117 	RSAKeyImpl::Ptr impl() const;
118 		/// Returns the impl object.
119 };
120 
121 
122 //
123 // inlines
124 //
impl()125 inline RSAKeyImpl::Ptr RSAKey::impl() const
126 {
127 	return KeyPair::impl().cast<RSAKeyImpl>();
128 }
129 
130 
131 } } // namespace Poco::Crypto
132 
133 
134 #endif // Crypto_RSAKey_INCLUDED