1 //
2 // ECKey.h
3 //
4 //
5 // Library: Crypto
6 // Package: EC
7 // Module:  ECKey
8 //
9 // Definition of the ECKey class.
10 //
11 // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
12 // and Contributors.
13 //
14 // SPDX-License-Identifier:	BSL-1.0
15 //
16 
17 
18 #ifndef Crypto_ECKey_INCLUDED
19 #define Crypto_ECKey_INCLUDED
20 
21 
22 #include "Poco/Crypto/Crypto.h"
23 #include "Poco/Crypto/KeyPair.h"
24 #include "Poco/Crypto/ECKeyImpl.h"
25 
26 
27 namespace Poco {
28 namespace Crypto {
29 
30 
31 class X509Certificate;
32 class PKCS12Container;
33 
34 
35 class Crypto_API ECKey: public KeyPair
36 	/// This class stores an EC key pair, consisting
37 	/// of private and public key. Storage of the private
38 	/// key is optional.
39 	///
40 	/// If a private key is available, the ECKey can be
41 	/// used for decrypting data (encrypted with the public key)
42 	/// or computing secure digital signatures.
43 {
44 public:
45 	ECKey(const EVPPKey& key);
46 		/// Constructs ECKeyImpl by extracting the EC key.
47 
48 	ECKey(const X509Certificate& cert);
49 		/// Extracts the EC public key from the given certificate.
50 
51 	ECKey(const PKCS12Container& cert);
52 		/// Extracts the EC private key from the given certificate.
53 
54 	ECKey(const std::string& eccGroup);
55 		/// Creates the ECKey. Creates a new public/private keypair using the given parameters.
56 		/// Can be used to sign data and verify signatures.
57 
58 	ECKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase = "");
59 		/// Creates the ECKey, by reading public and private key from the given files and
60 		/// using the given passphrase for the private key.
61 		///
62 		/// Cannot be used for signing or decryption unless a private key is available.
63 		///
64 		/// If a private key is specified, you don't need to specify a public key file.
65 		/// OpenSSL will auto-create the public key from the private key.
66 
67 	ECKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = "");
68 		/// Creates the ECKey, 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 
76 	ECKey(const ECKey& key);
77 		/// Creates the ECKey by copying another one.
78 
79 	ECKey(ECKey&& key) noexcept;
80 		/// Creates the ECKey by moving another one.
81 
82 	~ECKey();
83 		/// Destroys the ECKey.
84 
85 	ECKey& operator = (const ECKey& other);
86 		/// Assignment.
87 
88 	ECKey& operator = (ECKey&& other) noexcept;
89 		/// Move assignment.
90 
91 	ECKeyImpl::Ptr impl() const;
92 		/// Returns the impl object.
93 
94 	static std::string getCurveName(int nid = -1);
95 		/// Returns elliptical curve name corresponding to
96 		/// the given nid; if nid is not found, returns
97 		/// empty string.
98 		///
99 		/// If nid is -1, returns first curve name.
100 		///
101 		/// If no curves are found, returns empty string;
102 
103 	static int getCurveNID(std::string& name);
104 		/// Returns the NID of the specified curve.
105 		///
106 		/// If name is empty, returns the first curve NID
107 		/// and updates the name accordingly.
108 
109 	static bool hasCurve(const std::string& name);
110 		/// Returns true if the named curve is found,
111 		/// false otherwise.
112 };
113 
114 
115 //
116 // inlines
117 //
impl()118 inline ECKeyImpl::Ptr ECKey::impl() const
119 {
120 	return KeyPair::impl().cast<ECKeyImpl>();
121 }
122 
123 
getCurveName(int nid)124 inline std::string ECKey::getCurveName(int nid)
125 {
126 	return ECKeyImpl::getCurveName(nid);
127 }
128 
129 
getCurveNID(std::string & name)130 inline int ECKey::getCurveNID(std::string& name)
131 {
132 	return ECKeyImpl::getCurveNID(name);
133 }
134 
135 
hasCurve(const std::string & name)136 inline bool ECKey::hasCurve(const std::string& name)
137 {
138 	return ECKeyImpl::hasCurve(name);
139 }
140 
141 
142 } } // namespace Poco::Crypto
143 
144 
145 #endif // Crypto_ECKey_INCLUDED
146