rsa_cipher.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "rsa_cipher.hpp"
  2. #include <openssl/err.h>
  3. #include <openssl/pem.h>
  4. #include <openssl/bio.h>
  5. #include "resource_traits/openssl/bio.hpp"
  6. #include "resource_traits/openssl/bignum.hpp"
  7. #include "cp_converter.hpp"
  8. #include "exceptions/overflow_exception.hpp"
  9. #include "exceptions/openssl_exception.hpp"
  10. #pragma comment(lib, "libcrypto")
  11. #pragma comment(lib, "crypt32") // required by libcrypto.lib
  12. #pragma comment(lib, "ws2_32") // required by libcrypto.lib
  13. #define NKG_CURRENT_SOURCE_FILE() u8".\\common\\rsa_cipher.cpp"
  14. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  15. namespace nkg {
  16. RSA* rsa_cipher::_read_private_key_from_bio(BIO* p_bio) {
  17. resource_wrapper new_rsa
  18. { resource_traits::openssl::rsa{}, PEM_read_bio_RSAPrivateKey(p_bio, nullptr, nullptr, nullptr) };
  19. if (new_rsa.is_valid()) {
  20. return new_rsa.transfer();
  21. } else {
  22. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSAPrivateKey failed.")
  23. .push_hint(u8"Are you sure that you DO provide a valid RSA private key file?");
  24. }
  25. }
  26. RSA* rsa_cipher::_read_public_key_pem_from_bio(BIO* p_bio) {
  27. resource_wrapper new_rsa
  28. { resource_traits::openssl::rsa{}, PEM_read_bio_RSA_PUBKEY(p_bio, nullptr, nullptr, nullptr) };
  29. if (new_rsa.is_valid()) {
  30. return new_rsa.transfer();
  31. } else {
  32. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSA_PUBKEY failed.")
  33. .push_hint(u8"Are you sure that you DO provide a valid RSA public key file with PEM format?");
  34. }
  35. }
  36. RSA* rsa_cipher::_read_public_key_pkcs1_from_bio(BIO* p_bio) {
  37. resource_wrapper new_rsa
  38. { resource_traits::openssl::rsa{}, PEM_read_bio_RSAPublicKey(p_bio, nullptr, nullptr, nullptr) };
  39. if (new_rsa.is_valid()) {
  40. return new_rsa.transfer();
  41. } else {
  42. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSAPublicKey failed.")
  43. .push_hint(u8"Are you sure that you DO provide a valid RSA public key file with PKCS1 format?");
  44. }
  45. }
  46. void rsa_cipher::_write_private_key_to_bio(RSA* p_rsa, BIO* p_bio) {
  47. auto r = PEM_write_bio_RSAPrivateKey(p_bio, p_rsa, nullptr, nullptr, 0, nullptr, nullptr);
  48. if (r == 0) {
  49. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSAPrivateKey failed.");
  50. };
  51. }
  52. void rsa_cipher::_write_public_key_pem_to_bio(RSA* p_rsa, BIO* p_bio) {
  53. auto r = PEM_write_bio_RSA_PUBKEY(p_bio, p_rsa);
  54. if (r == 0) {
  55. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSA_PUBKEY failed.");
  56. }
  57. }
  58. void rsa_cipher::_write_public_key_pkcs1_to_bio(RSA* p_rsa, BIO* p_bio) {
  59. auto r = PEM_write_bio_RSAPublicKey(p_bio, p_rsa);
  60. if (r == 0) {
  61. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSAPublicKey failed.");
  62. }
  63. }
  64. rsa_cipher::rsa_cipher() : m_rsa(RSA_new()) {
  65. if (!m_rsa.is_valid()) {
  66. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_new failed.");
  67. }
  68. }
  69. [[nodiscard]]
  70. size_t rsa_cipher::bits() const {
  71. #if (OPENSSL_VERSION_NUMBER & 0xffff0000) == 0x10000000 // openssl 1.0.x
  72. if (m_rsa->n == nullptr) {
  73. throw no_key_assigned_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"RSA modulus has not been set.");
  74. }
  75. return BN_num_bits(m_rsa->n);
  76. #elif (OPENSSL_VERSION_NUMBER & 0xffff0000) == 0x10100000 // openssl 1.1.x
  77. return RSA_bits(m_rsa.get());
  78. #else
  79. #error "rsa_cipher.cpp: uexpected OpenSSL version"
  80. #endif
  81. }
  82. void rsa_cipher::generate_key(int bits, unsigned int e) {
  83. resource_wrapper bn_e{ resource_traits::openssl::bignum{}, BN_new() };
  84. if (bn_e.is_valid() == false) {
  85. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"BN_new failed.");
  86. }
  87. if (BN_set_word(bn_e.get(), e) == 0) {
  88. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BN_set_word failed.");
  89. }
  90. if (RSA_generate_key_ex(m_rsa.get(), bits, bn_e.get(), nullptr) == 0) {
  91. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_generate_key_ex failed.");
  92. }
  93. }
  94. void rsa_cipher::export_private_key_file(std::wstring_view file_path) const {
  95. resource_wrapper bio_file
  96. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "w")};
  97. if (bio_file.is_valid()) {
  98. _write_private_key_to_bio(m_rsa.get(), bio_file.get());
  99. } else {
  100. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  101. }
  102. }
  103. void rsa_cipher::export_private_key_file(const std::filesystem::path& file_path) const {
  104. export_private_key_file(static_cast<std::wstring_view>(file_path.native()));
  105. }
  106. void rsa_cipher::export_public_key_file_pem(std::wstring_view file_path) const {
  107. resource_wrapper bio_file
  108. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "w")};
  109. if (bio_file.is_valid()) {
  110. _write_public_key_pem_to_bio(m_rsa.get(), bio_file.get());
  111. } else {
  112. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  113. }
  114. }
  115. void rsa_cipher::export_public_key_file_pem(const std::filesystem::path& file_path) const {
  116. export_public_key_file_pem(static_cast<std::wstring_view>(file_path.native()));
  117. }
  118. void rsa_cipher::export_public_key_file_pkcs1(std::wstring_view file_path) const {
  119. resource_wrapper bio_file
  120. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "w")};
  121. if (bio_file.is_valid()) {
  122. _write_public_key_pkcs1_to_bio(m_rsa.get(), bio_file.get());
  123. } else {
  124. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  125. }
  126. }
  127. void rsa_cipher::export_public_key_file_pkcs1(const std::filesystem::path& file_path) const {
  128. export_public_key_file_pkcs1(static_cast<std::wstring_view>(file_path.native()));
  129. }
  130. void rsa_cipher::import_private_key_file(std::wstring_view file_path) {
  131. resource_wrapper bio_file
  132. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "r") };
  133. if (bio_file.is_valid()) {
  134. m_rsa.set(_read_private_key_from_bio(bio_file.get()));
  135. } else {
  136. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  137. }
  138. }
  139. void rsa_cipher::import_private_key_file(const std::filesystem::path& file_path) {
  140. import_private_key_file(static_cast<std::wstring_view>(file_path.native()));
  141. }
  142. void rsa_cipher::import_public_key_file_pem(std::wstring_view file_path) {
  143. resource_wrapper bio_file
  144. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "r") };
  145. if (bio_file.is_valid()) {
  146. m_rsa.set(_read_public_key_pem_from_bio(bio_file.get()));
  147. } else {
  148. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  149. }
  150. }
  151. void rsa_cipher::import_public_key_file_pem(const std::filesystem::path& file_path) {
  152. import_public_key_file_pem(static_cast<std::wstring_view>(file_path.native()));
  153. }
  154. void rsa_cipher::import_public_key_file_pkcs1(std::wstring_view file_path) {
  155. resource_wrapper bio_file
  156. { resource_traits::openssl::bio{}, BIO_new_file(cp_converter<-1, CP_UTF8>::convert(file_path).c_str(), "r") };
  157. if (bio_file.is_valid()) {
  158. m_rsa.set(_read_public_key_pkcs1_from_bio(bio_file.get()));
  159. } else {
  160. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  161. }
  162. }
  163. void rsa_cipher::import_public_key_file_pkcs1(const std::filesystem::path& file_path) {
  164. import_public_key_file_pkcs1(static_cast<std::wstring_view>(file_path.native()));
  165. }
  166. [[nodiscard]]
  167. std::string rsa_cipher::export_private_key_string() const {
  168. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  169. if (bio_memory.is_valid()) {
  170. _write_private_key_to_bio(m_rsa.get(), bio_memory.get());
  171. const char* pch = nullptr;
  172. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  173. return std::string(pch, lch);
  174. } else {
  175. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  176. }
  177. }
  178. [[nodiscard]]
  179. std::string rsa_cipher::export_public_key_string_pem() const {
  180. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  181. if (bio_memory.is_valid()) {
  182. _write_public_key_pem_to_bio(m_rsa.get(), bio_memory.get());
  183. const char* pch = nullptr;
  184. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  185. return std::string(pch, lch);
  186. } else {
  187. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  188. }
  189. }
  190. [[nodiscard]]
  191. std::string rsa_cipher::export_public_key_string_pkcs1() const {
  192. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  193. if (bio_memory.is_valid()) {
  194. _write_public_key_pkcs1_to_bio(m_rsa.get(), bio_memory.get());
  195. const char* pch = nullptr;
  196. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  197. return std::string(pch, lch);
  198. } else {
  199. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  200. }
  201. }
  202. void rsa_cipher::import_private_key_string(std::string_view key_string) {
  203. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  204. if (bio_memory.is_valid() == false) {
  205. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  206. }
  207. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  208. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  209. }
  210. m_rsa.set(_read_private_key_from_bio(bio_memory.get()));
  211. }
  212. void rsa_cipher::import_public_key_string_pem(std::string_view key_string) {
  213. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  214. if (bio_memory.is_valid() == false) {
  215. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  216. }
  217. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  218. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  219. }
  220. m_rsa.set(_read_public_key_pem_from_bio(bio_memory.get()));
  221. }
  222. void rsa_cipher::import_public_key_string_pkcs1(std::string_view key_string) {
  223. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  224. if (bio_memory.is_valid() == false) {
  225. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  226. }
  227. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  228. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  229. }
  230. m_rsa.set(_read_public_key_pkcs1_from_bio(bio_memory.get()));
  231. }
  232. size_t rsa_cipher::public_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const {
  233. if (plaintext_size <= INT_MAX) {
  234. int bytes_written =
  235. RSA_public_encrypt(static_cast<int>(plaintext_size), reinterpret_cast<const unsigned char*>(plaintext), reinterpret_cast<unsigned char*>(ciphertext), m_rsa.get(), padding);
  236. if (bytes_written != -1) {
  237. return bytes_written;
  238. } else {
  239. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_encrypt failed.");
  240. }
  241. } else {
  242. throw exceptions::overflow_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"plaintext_size > INT_MAX");
  243. }
  244. }
  245. size_t rsa_cipher::private_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const {
  246. if (plaintext_size <= INT_MAX) {
  247. int bytes_written =
  248. RSA_private_encrypt(static_cast<int>(plaintext_size), reinterpret_cast<const unsigned char*>(plaintext), reinterpret_cast<unsigned char*>(ciphertext), m_rsa.get(), padding);
  249. if (bytes_written != -1) {
  250. return bytes_written;
  251. } else {
  252. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_encrypt failed.");
  253. }
  254. } else {
  255. throw exceptions::overflow_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"plaintext_size > INT_MAX");
  256. }
  257. }
  258. size_t rsa_cipher::public_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const {
  259. if (ciphertext_size <= INT_MAX) {
  260. int bytes_written =
  261. RSA_public_decrypt(static_cast<int>(ciphertext_size), reinterpret_cast<const unsigned char*>(ciphertext), reinterpret_cast<unsigned char*>(plaintext), m_rsa.get(), padding);
  262. if (bytes_written != -1) {
  263. return bytes_written;
  264. } else {
  265. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_decrypt failed.")
  266. .push_hint(u8"Are your sure you DO provide a correct public key?");
  267. }
  268. } else {
  269. throw exceptions::overflow_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"ciphertext_size > INT_MAX");
  270. }
  271. }
  272. size_t rsa_cipher::private_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const {
  273. if (ciphertext_size <= INT_MAX) {
  274. int bytes_written =
  275. RSA_private_decrypt(static_cast<int>(ciphertext_size), reinterpret_cast<const unsigned char*>(ciphertext), reinterpret_cast<unsigned char*>(plaintext), m_rsa.get(), padding);
  276. if (bytes_written != -1) {
  277. return bytes_written;
  278. } else {
  279. throw exceptions::openssl_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_decrypt failed.")
  280. .push_hint(u8"Are your sure you DO provide a correct private key?");
  281. }
  282. } else {
  283. throw exceptions::overflow_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"ciphertext_size > INT_MAX");
  284. }
  285. }
  286. }
  287. #undef NKG_CURRENT_SOURCE_FILE
  288. #undef NKG_CURRENT_SOURCE_LINE