ECKey.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef Crypto_ECKey_INCLUDED
  17. #define Crypto_ECKey_INCLUDED
  18. #include "Poco/Crypto/Crypto.h"
  19. #include "Poco/Crypto/KeyPair.h"
  20. #include "Poco/Crypto/ECKeyImpl.h"
  21. namespace Poco {
  22. namespace Crypto {
  23. class X509Certificate;
  24. class PKCS12Container;
  25. class Crypto_API ECKey : public KeyPair
  26. /// This class stores an EC key pair, consisting
  27. /// of private and public key. Storage of the private
  28. /// key is optional.
  29. ///
  30. /// If a private key is available, the ECKey can be
  31. /// used for decrypting data (encrypted with the public key)
  32. /// or computing secure digital signatures.
  33. {
  34. public:
  35. ECKey(const EVPPKey& key);
  36. /// Constructs ECKeyImpl by extracting the EC key.
  37. ECKey(const X509Certificate& cert);
  38. /// Extracts the EC public key from the given certificate.
  39. ECKey(const PKCS12Container& cert);
  40. /// Extracts the EC private key from the given certificate.
  41. ECKey(const std::string& eccGroup);
  42. /// Creates the ECKey. Creates a new public/private keypair using the given parameters.
  43. /// Can be used to sign data and verify signatures.
  44. ECKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase = "");
  45. /// Creates the ECKey, by reading public and private key from the given files and
  46. /// using the given passphrase for the private key.
  47. ///
  48. /// Cannot be used for signing or decryption unless a private key is available.
  49. ///
  50. /// If a private key is specified, you don't need to specify a public key file.
  51. /// OpenSSL will auto-create the public key from the private key.
  52. ECKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = "");
  53. /// Creates the ECKey, by reading public and private key from the given streams and
  54. /// using the given passphrase for the private key.
  55. ///
  56. /// Cannot be used for signing or decryption unless a private key is available.
  57. ///
  58. /// If a private key is specified, you don't need to specify a public key file.
  59. /// OpenSSL will auto-create the public key from the private key.
  60. ~ECKey();
  61. /// Destroys the ECKey.
  62. ECKeyImpl::Ptr impl() const;
  63. /// Returns the impl object.
  64. static std::string getCurveName(int nid = -1);
  65. /// Returns elliptical curve name corresponding to
  66. /// the given nid; if nid is not found, returns
  67. /// empty string.
  68. ///
  69. /// If nid is -1, returns first curve name.
  70. ///
  71. /// If no curves are found, returns empty string;
  72. static int getCurveNID(std::string& name);
  73. /// Returns the NID of the specified curve.
  74. ///
  75. /// If name is empty, returns the first curve NID
  76. /// and updates the name accordingly.
  77. static bool hasCurve(const std::string& name);
  78. /// Returns true if the named curve is found,
  79. /// false otherwise.
  80. private:
  81. ECKeyImpl::Ptr _pImpl;
  82. };
  83. //
  84. // inlines
  85. //
  86. inline ECKeyImpl::Ptr ECKey::impl() const
  87. {
  88. return _pImpl;
  89. }
  90. inline std::string ECKey::getCurveName(int nid)
  91. {
  92. return ECKeyImpl::getCurveName(nid);
  93. }
  94. inline int ECKey::getCurveNID(std::string& name)
  95. {
  96. return ECKeyImpl::getCurveNID(name);
  97. }
  98. inline bool ECKey::hasCurve(const std::string& name)
  99. {
  100. return ECKeyImpl::hasCurve(name);
  101. }
  102. } } // namespace Poco::Crypto
  103. #endif // Crypto_ECKey_INCLUDED