rsa_cipher.cpp 14 KB

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