rsa_cipher.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include <string>
  3. #include <optional>
  4. #include <openssl/err.h>
  5. #include <openssl/rsa.h>
  6. #include "resource_wrapper.hpp"
  7. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  8. #include "resource_traits/openssl/rsa.hpp"
  9. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  10. #include "resource_traits/openssl/evp_pkey_ctx.hpp"
  11. #include "resource_traits/openssl/evp_pkey.hpp"
  12. #else
  13. #error "rsa_cipher.hpp: Unexpected OpenSSL version."
  14. #endif
  15. #include "exception.hpp"
  16. #define NKG_CURRENT_SOURCE_FILE() u8".\\common\\rsa_cipher.hpp"
  17. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  18. namespace nkg {
  19. class rsa_cipher {
  20. public:
  21. class backend_error;
  22. class no_key_assigned_error;
  23. private:
  24. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  25. resource_wrapper<resource_traits::openssl::rsa> m_rsa;
  26. [[nodiscard]]
  27. static RSA* _read_private_key_from_bio(BIO* p_bio);
  28. [[nodiscard]]
  29. static RSA* _read_public_key_pem_from_bio(BIO* p_bio);
  30. [[nodiscard]]
  31. static RSA* _read_public_key_pkcs1_from_bio(BIO* p_bio);
  32. static void _write_private_key_to_bio(RSA* p_rsa, BIO* p_bio);
  33. static void _write_public_key_pem_to_bio(RSA* p_rsa, BIO* p_bio);
  34. static void _write_public_key_pkcs1_to_bio(RSA* p_rsa, BIO* p_bio);
  35. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  36. resource_wrapper<resource_traits::openssl::evp_pkey> m_rsa;
  37. [[nodiscard]]
  38. static EVP_PKEY* _read_private_key_from_bio(BIO* p_bio);
  39. [[nodiscard]]
  40. static EVP_PKEY* _read_public_key_pem_from_bio(BIO* p_bio);
  41. [[nodiscard]]
  42. static EVP_PKEY* _read_public_key_pkcs1_from_bio(BIO* p_bio);
  43. static void _write_private_key_to_bio(EVP_PKEY* p_rsa, BIO* p_bio);
  44. static void _write_public_key_pem_to_bio(EVP_PKEY* p_rsa, BIO* p_bio);
  45. static void _write_public_key_pkcs1_to_bio(EVP_PKEY* p_rsa, BIO* p_bio);
  46. #else
  47. #error "rsa_cipher.hpp: Unexpected OpenSSL version."
  48. #endif
  49. public:
  50. rsa_cipher();
  51. [[nodiscard]]
  52. size_t bits() const;
  53. void generate_key(int bits, unsigned int e = RSA_F4);
  54. void export_private_key_file(std::string_view file_path) const;
  55. void export_public_key_file_pem(std::string_view file_path) const;
  56. void export_public_key_file_pkcs1(std::string_view file_path) const;
  57. void import_private_key_file(std::string_view file_path);
  58. void import_public_key_file_pem(std::string_view file_path);
  59. void import_public_key_file_pkcs1(std::string_view file_path);
  60. [[nodiscard]]
  61. std::string export_private_key_string() const;
  62. [[nodiscard]]
  63. std::string export_public_key_string_pem() const;
  64. [[nodiscard]]
  65. std::string export_public_key_string_pkcs1() const;
  66. void import_private_key_string(std::string_view key_string);
  67. void import_public_key_string_pem(std::string_view key_string);
  68. void import_public_key_string_pkcs1(std::string_view key_string);
  69. size_t public_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const;
  70. size_t private_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const;
  71. size_t public_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const;
  72. size_t private_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const;
  73. };
  74. class rsa_cipher::backend_error : public ::nkg::exception {
  75. public:
  76. using error_code_t = decltype(ERR_get_error());
  77. private:
  78. std::optional<error_code_t> m_error_code;
  79. std::string m_error_string;
  80. public:
  81. backend_error(std::string_view file, int line, std::string_view message) noexcept :
  82. ::nkg::exception(file, line, message) {}
  83. backend_error(std::string_view file, int line, error_code_t openssl_errno, std::string_view message) noexcept :
  84. ::nkg::exception(file, line, message), m_error_code(openssl_errno) {}
  85. [[nodiscard]]
  86. virtual bool error_code_exists() const noexcept override {
  87. return m_error_code.has_value();
  88. }
  89. [[nodiscard]]
  90. virtual intptr_t error_code() const noexcept override {
  91. if (error_code_exists()) { return m_error_code.value(); } else { trap_then_terminate(); }
  92. }
  93. [[nodiscard]]
  94. virtual const std::string& error_string() const noexcept override {
  95. if (error_code_exists()) { return m_error_string; } else { trap_then_terminate(); }
  96. }
  97. };
  98. class rsa_cipher::no_key_assigned_error : public ::nkg::exception {
  99. using ::nkg::exception::exception;
  100. };
  101. }
  102. #undef NKG_CURRENT_SOURCE_FILE
  103. #undef NKG_CURRENT_SOURCE_LINE