1 //
2 // RSAKeyImpl.h
3 //
4 // Library: Crypto
5 // Package: RSA
6 // Module:  RSAKeyImpl
7 //
8 // Definition of the RSAKeyImpl 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_RSAKeyImplImpl_INCLUDED
18 #define Crypto_RSAKeyImplImpl_INCLUDED
19 
20 
21 #include "Poco/Crypto/Crypto.h"
22 #include "Poco/Crypto/EVPPKey.h"
23 #include "Poco/Crypto/KeyPairImpl.h"
24 #include "Poco/Crypto/OpenSSLInitializer.h"
25 #include "Poco/RefCountedObject.h"
26 #include "Poco/AutoPtr.h"
27 #include <istream>
28 #include <ostream>
29 #include <vector>
30 
31 
32 struct bignum_st;
33 struct rsa_st;
34 typedef struct bignum_st BIGNUM;
35 typedef struct rsa_st RSA;
36 
37 
38 namespace Poco {
39 namespace Crypto {
40 
41 
42 class X509Certificate;
43 class PKCS12Container;
44 
45 
46 class RSAKeyImpl: public KeyPairImpl
47 	/// class RSAKeyImpl
48 {
49 public:
50 	using Ptr = Poco::AutoPtr<RSAKeyImpl>;
51 	using ByteVec = std::vector<unsigned char>;
52 
53 	RSAKeyImpl(const EVPPKey& key);
54 		/// Constructs ECKeyImpl by extracting the EC key.
55 
56 	RSAKeyImpl(const X509Certificate& cert);
57 		/// Extracts the RSA public key from the given certificate.
58 
59 	RSAKeyImpl(const PKCS12Container& cert);
60 		/// Extracts the EC private key from the given certificate.
61 
62 	RSAKeyImpl(int keyLength, unsigned long exponent);
63 		/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
64 		/// Can be used to sign data and verify signatures.
65 
66 	RSAKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase);
67 		/// Creates the RSAKey, by reading public and private key from the given files and
68 		/// using the given passphrase for the private key. Can only by used for signing if
69 		/// a private key is available.
70 
71 	RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase);
72 		/// Creates the RSAKey. Can only by used for signing if pPrivKey
73 		/// is not null. If a private key file is specified, you don't need to
74 		/// specify a public key file. OpenSSL will auto-create it from the private key.
75 
76 	~RSAKeyImpl();
77 		/// Destroys the RSAKeyImpl.
78 
79 	RSA* getRSA();
80 		/// Returns the OpenSSL RSA object.
81 
82 	const RSA* getRSA() const;
83 		/// Returns the OpenSSL RSA object.
84 
85 	int size() const;
86 		/// Returns the RSA modulus size.
87 
88 	ByteVec modulus() const;
89 		/// Returns the RSA modulus.
90 
91 	ByteVec encryptionExponent() const;
92 		/// Returns the RSA encryption exponent.
93 
94 	ByteVec decryptionExponent() const;
95 		/// Returns the RSA decryption exponent.
96 
97 	void save(const std::string& publicKeyFile,
98 		const std::string& privateKeyFile = "",
99 		const std::string& privateKeyPassphrase = "") const;
100 		/// Exports the public and private keys to the given files.
101 		///
102 		/// If an empty filename is specified, the corresponding key
103 		/// is not exported.
104 
105 	void save(std::ostream* pPublicKeyStream,
106 		std::ostream* pPrivateKeyStream = 0,
107 		const std::string& privateKeyPassphrase = "") const;
108 		/// Exports the public and private key to the given streams.
109 		///
110 		/// If a null pointer is passed for a stream, the corresponding
111 		/// key is not exported.
112 
113 private:
114 	RSAKeyImpl();
115 
116 	void freeRSA();
117 	static ByteVec convertToByteVec(const BIGNUM* bn);
118 
119 	RSA* _pRSA;
120 };
121 
122 
123 //
124 // inlines
125 //
getRSA()126 inline RSA* RSAKeyImpl::getRSA()
127 {
128 	return _pRSA;
129 }
130 
131 
getRSA()132 inline const RSA* RSAKeyImpl::getRSA() const
133 {
134 	return _pRSA;
135 }
136 
137 
138 } } // namespace Poco::Crypto
139 
140 
141 #endif // Crypto_RSAKeyImplImpl_INCLUDED