rsa_cipher.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include <string>
  3. #include <filesystem>
  4. #include <openssl/rsa.h>
  5. #include "resource_wrapper.hpp"
  6. #include "resource_traits/openssl/rsa.hpp"
  7. #include "exception.hpp"
  8. #define NKG_CURRENT_SOURCE_FILE() u8".\\common\\rsa_cipher.hpp"
  9. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  10. namespace nkg {
  11. class rsa_cipher {
  12. public:
  13. class no_key_assigned_error : public ::nkg::exception {
  14. public:
  15. no_key_assigned_error(std::string_view file, int line, std::string_view message) noexcept :
  16. ::nkg::exception(file, line, message) {}
  17. };
  18. class backend_error : public ::nkg::exception {
  19. public:
  20. backend_error(std::string_view file, int line, std::string_view message) noexcept :
  21. ::nkg::exception(file, line, message) {}
  22. };
  23. private:
  24. resource_wrapper<resource_traits::openssl::rsa> m_rsa;
  25. [[nodiscard]]
  26. static RSA* _read_private_key_from_bio(BIO* p_bio);
  27. [[nodiscard]]
  28. static RSA* _read_public_key_pem_from_bio(BIO* p_bio);
  29. [[nodiscard]]
  30. static RSA* _read_public_key_pkcs1_from_bio(BIO* p_bio);
  31. static void _write_private_key_to_bio(RSA* p_rsa, BIO* p_bio);
  32. static void _write_public_key_pem_to_bio(RSA* p_rsa, BIO* p_bio);
  33. static void _write_public_key_pkcs1_to_bio(RSA* p_rsa, BIO* p_bio);
  34. public:
  35. rsa_cipher();
  36. [[nodiscard]]
  37. size_t bits() const;
  38. void generate_key(int bits, unsigned int e = RSA_F4);
  39. void export_private_key_file(std::wstring_view file_path) const;
  40. void export_private_key_file(const std::filesystem::path& file_path) const;
  41. void export_public_key_file_pem(std::wstring_view file_path) const;
  42. void export_public_key_file_pem(const std::filesystem::path& file_path) const;
  43. void export_public_key_file_pkcs1(std::wstring_view file_path) const;
  44. void export_public_key_file_pkcs1(const std::filesystem::path& file_path) const;
  45. void import_private_key_file(std::wstring_view file_path);
  46. void import_private_key_file(const std::filesystem::path& file_path);
  47. void import_public_key_file_pem(std::wstring_view file_path);
  48. void import_public_key_file_pem(const std::filesystem::path& file_path);
  49. void import_public_key_file_pkcs1(std::wstring_view file_path);
  50. void import_public_key_file_pkcs1(const std::filesystem::path& file_path);
  51. [[nodiscard]]
  52. std::string export_private_key_string() const;
  53. [[nodiscard]]
  54. std::string export_public_key_string_pem() const;
  55. [[nodiscard]]
  56. std::string export_public_key_string_pkcs1() const;
  57. void import_private_key_string(std::string_view key_string);
  58. void import_public_key_string_pem(std::string_view key_string);
  59. void import_public_key_string_pkcs1(std::string_view key_string);
  60. size_t public_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const;
  61. size_t private_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const;
  62. size_t public_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const;
  63. size_t private_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const;
  64. };
  65. }
  66. #undef NKG_CURRENT_SOURCE_FILE
  67. #undef NKG_CURRENT_SOURCE_LINE