rsa_cipher.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  7. #include <openssl/encoder.h>
  8. #include <openssl/decoder.h>
  9. #include "resource_traits/openssl/encoder_ctx.hpp"
  10. #include "resource_traits/openssl/decoder_ctx.hpp"
  11. #endif
  12. #define NKG_CURRENT_SOURCE_FILE() u8".\\common\\rsa_cipher.cpp"
  13. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  14. namespace nkg {
  15. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  16. [[nodiscard]]
  17. RSA* rsa_cipher::_read_private_key_from_bio(BIO* p_bio) {
  18. resource_wrapper new_rsa
  19. { resource_traits::openssl::rsa{}, PEM_read_bio_RSAPrivateKey(p_bio, nullptr, nullptr, nullptr) };
  20. if (new_rsa.is_valid()) {
  21. return new_rsa.transfer();
  22. } else {
  23. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSAPrivateKey failed.")
  24. .push_hint(u8"Are you sure that you DO provide a valid RSA private key file?");
  25. }
  26. }
  27. [[nodiscard]]
  28. RSA* rsa_cipher::_read_public_key_pem_from_bio(BIO* p_bio) {
  29. resource_wrapper new_rsa
  30. { resource_traits::openssl::rsa{}, PEM_read_bio_RSA_PUBKEY(p_bio, nullptr, nullptr, nullptr) };
  31. if (new_rsa.is_valid()) {
  32. return new_rsa.transfer();
  33. } else {
  34. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSA_PUBKEY failed.")
  35. .push_hint(u8"Are you sure that you DO provide a valid RSA public key file with PEM format?");
  36. }
  37. }
  38. [[nodiscard]]
  39. RSA* rsa_cipher::_read_public_key_pkcs1_from_bio(BIO* p_bio) {
  40. resource_wrapper new_rsa
  41. { resource_traits::openssl::rsa{}, PEM_read_bio_RSAPublicKey(p_bio, nullptr, nullptr, nullptr) };
  42. if (new_rsa.is_valid()) {
  43. return new_rsa.transfer();
  44. } else {
  45. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_read_bio_RSAPublicKey failed.")
  46. .push_hint(u8"Are you sure that you DO provide a valid RSA public key file with PKCS1 format?");
  47. }
  48. }
  49. void rsa_cipher::_write_private_key_to_bio(RSA* p_rsa, BIO* p_bio) {
  50. auto r = PEM_write_bio_RSAPrivateKey(p_bio, p_rsa, nullptr, nullptr, 0, nullptr, nullptr);
  51. if (r == 0) {
  52. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSAPrivateKey failed.");
  53. };
  54. }
  55. void rsa_cipher::_write_public_key_pem_to_bio(RSA* p_rsa, BIO* p_bio) {
  56. auto r = PEM_write_bio_RSA_PUBKEY(p_bio, p_rsa);
  57. if (r == 0) {
  58. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSA_PUBKEY failed.");
  59. }
  60. }
  61. void rsa_cipher::_write_public_key_pkcs1_to_bio(RSA* p_rsa, BIO* p_bio) {
  62. auto r = PEM_write_bio_RSAPublicKey(p_bio, p_rsa);
  63. if (r == 0) {
  64. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"PEM_write_bio_RSAPublicKey failed.");
  65. }
  66. }
  67. rsa_cipher::rsa_cipher() :
  68. m_rsa(RSA_new())
  69. {
  70. if (!m_rsa.is_valid()) {
  71. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_new failed.");
  72. }
  73. }
  74. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  75. [[nodiscard]]
  76. EVP_PKEY* rsa_cipher::_read_private_key_from_bio(BIO* p_bio) {
  77. resource_wrapper new_rsa{ resource_traits::openssl::evp_pkey{} };
  78. resource_wrapper decoder_context
  79. { resource_traits::openssl::decoder_ctx{}, OSSL_DECODER_CTX_new_for_pkey(new_rsa.unsafe_addressof(), "PEM", "pkcs1", "RSA", OSSL_KEYMGMT_SELECT_PRIVATE_KEY, nullptr, nullptr) };
  80. if (!decoder_context.is_valid()) {
  81. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_CTX_new_for_pkey failed.");
  82. }
  83. if (!OSSL_DECODER_from_bio(decoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  84. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_from_bio failed.");
  85. }
  86. return new_rsa.transfer();
  87. }
  88. [[nodiscard]]
  89. EVP_PKEY* rsa_cipher::_read_public_key_pem_from_bio(BIO* p_bio) {
  90. resource_wrapper new_rsa{ resource_traits::openssl::evp_pkey{} };
  91. resource_wrapper decoder_context
  92. { resource_traits::openssl::decoder_ctx{}, OSSL_DECODER_CTX_new_for_pkey(new_rsa.unsafe_addressof(), "PEM", "SubjectPublicKeyInfo", "RSA", OSSL_KEYMGMT_SELECT_PUBLIC_KEY, nullptr, nullptr) };
  93. if (!decoder_context.is_valid()) {
  94. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_CTX_new_for_pkey failed.");
  95. }
  96. if (!OSSL_DECODER_from_bio(decoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  97. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_from_bio failed.");
  98. }
  99. return new_rsa.transfer();
  100. }
  101. [[nodiscard]]
  102. EVP_PKEY* rsa_cipher::_read_public_key_pkcs1_from_bio(BIO* p_bio) {
  103. resource_wrapper new_rsa{ resource_traits::openssl::evp_pkey{} };
  104. resource_wrapper decoder_context
  105. { resource_traits::openssl::decoder_ctx{}, OSSL_DECODER_CTX_new_for_pkey(new_rsa.unsafe_addressof(), "PEM", "pkcs1", "RSA", OSSL_KEYMGMT_SELECT_PUBLIC_KEY, nullptr, nullptr) };
  106. if (!decoder_context.is_valid()) {
  107. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_CTX_new_for_pkey failed.");
  108. }
  109. if (!OSSL_DECODER_from_bio(decoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  110. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_DECODER_from_bio failed.");
  111. }
  112. return new_rsa.transfer();
  113. }
  114. void rsa_cipher::_write_private_key_to_bio(EVP_PKEY* p_rsa, BIO* p_bio) {
  115. resource_wrapper encoder_context
  116. { resource_traits::openssl::encoder_ctx{}, OSSL_ENCODER_CTX_new_for_pkey(p_rsa, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, "PEM", "pkcs1", nullptr) };
  117. if (!encoder_context.is_valid()) {
  118. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_CTX_new_for_pkey failed.");
  119. }
  120. if (!OSSL_ENCODER_to_bio(encoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  121. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_to_bio failed.");
  122. }
  123. }
  124. void rsa_cipher::_write_public_key_pem_to_bio(EVP_PKEY* p_rsa, BIO* p_bio) {
  125. resource_wrapper encoder_context
  126. { resource_traits::openssl::encoder_ctx{}, OSSL_ENCODER_CTX_new_for_pkey(p_rsa, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, "PEM", "SubjectPublicKeyInfo", nullptr) };
  127. if (!encoder_context.is_valid()) {
  128. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_CTX_new_for_pkey failed.");
  129. }
  130. if (!OSSL_ENCODER_to_bio(encoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  131. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_to_bio failed.");
  132. }
  133. }
  134. void rsa_cipher::_write_public_key_pkcs1_to_bio(EVP_PKEY* p_rsa, BIO* p_bio) {
  135. resource_wrapper encoder_context
  136. { resource_traits::openssl::encoder_ctx{}, OSSL_ENCODER_CTX_new_for_pkey(p_rsa, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, "PEM", "pkcs1", nullptr) };
  137. if (!encoder_context.is_valid()) {
  138. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_CTX_new_for_pkey failed.");
  139. }
  140. if (!OSSL_ENCODER_to_bio(encoder_context.get(), p_bio)) { // 1 on success, 0 on failure
  141. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_ENCODER_to_bio failed.");
  142. }
  143. }
  144. rsa_cipher::rsa_cipher() = default;
  145. #else
  146. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  147. #endif
  148. [[nodiscard]]
  149. size_t rsa_cipher::bits() const {
  150. #if (OPENSSL_VERSION_NUMBER & 0xfff00000) == 0x10000000 // openssl 1.0.x
  151. if (m_rsa->n) {
  152. return BN_num_bits(m_rsa->n);
  153. } else {
  154. throw no_key_assigned_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"RSA key has not been set yet.");
  155. }
  156. #elif (OPENSSL_VERSION_NUMBER & 0xfff00000) == 0x10100000 // openssl 1.1.x
  157. return RSA_bits(m_rsa.get());
  158. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // openssl 3.x.x
  159. if (m_rsa.is_valid()) {
  160. return EVP_PKEY_get_bits(m_rsa.get());
  161. } else {
  162. throw no_key_assigned_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"RSA key has not been set yet.");
  163. }
  164. #else
  165. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  166. #endif
  167. }
  168. void rsa_cipher::generate_key(int bits, unsigned int e) {
  169. resource_wrapper bn_e{ resource_traits::openssl::bignum{}, BN_new() };
  170. if (!bn_e.is_valid()) {
  171. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"BN_new failed.");
  172. }
  173. if (BN_set_word(bn_e.get(), e) == 0) {
  174. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BN_set_word failed.");
  175. }
  176. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  177. if (RSA_generate_key_ex(m_rsa.get(), bits, bn_e.get(), nullptr) == 0) {
  178. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_generate_key_ex failed.");
  179. }
  180. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  181. resource_wrapper evp_pkey_context{ resource_traits::openssl::evp_pkey_ctx{}, EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr) };
  182. if (!evp_pkey_context.is_valid()) {
  183. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_new_id failed.");
  184. }
  185. if (EVP_PKEY_keygen_init(evp_pkey_context.get()) <= 0) { // 1 for success, 0 or a negative value for failure
  186. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_keygen_init failed.");
  187. }
  188. if (EVP_PKEY_CTX_set_rsa_keygen_bits(evp_pkey_context.get(), bits) <= 0) { // return a positive value for success and 0 or a negative value for failure
  189. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set_rsa_keygen_bits failed.");
  190. }
  191. if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(evp_pkey_context.get(), bn_e.get()) <= 0) { // return a positive value for success and 0 or a negative value for failure
  192. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set1_rsa_keygen_pubexp failed.");
  193. }
  194. resource_wrapper new_rsa{ resource_traits::openssl::evp_pkey{} };
  195. if (EVP_PKEY_keygen(evp_pkey_context.get(), new_rsa.unsafe_addressof()) <= 0) { // 1 for success, 0 or a negative value for failure
  196. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_keygen failed.");
  197. }
  198. m_rsa = std::move(new_rsa);
  199. #else
  200. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  201. #endif
  202. }
  203. void rsa_cipher::export_private_key_file(std::string_view file_path) const {
  204. resource_wrapper bio_file
  205. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "w")};
  206. if (bio_file.is_valid()) {
  207. _write_private_key_to_bio(m_rsa.get(), bio_file.get());
  208. } else {
  209. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  210. }
  211. }
  212. void rsa_cipher::export_public_key_file_pem(std::string_view file_path) const {
  213. resource_wrapper bio_file
  214. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "w")};
  215. if (bio_file.is_valid()) {
  216. _write_public_key_pem_to_bio(m_rsa.get(), bio_file.get());
  217. } else {
  218. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  219. }
  220. }
  221. void rsa_cipher::export_public_key_file_pkcs1(std::string_view file_path) const {
  222. resource_wrapper bio_file
  223. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "w")};
  224. if (bio_file.is_valid()) {
  225. _write_public_key_pkcs1_to_bio(m_rsa.get(), bio_file.get());
  226. } else {
  227. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  228. }
  229. }
  230. void rsa_cipher::import_private_key_file(std::string_view file_path) {
  231. resource_wrapper bio_file
  232. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "r") };
  233. if (bio_file.is_valid()) {
  234. m_rsa.set(_read_private_key_from_bio(bio_file.get()));
  235. } else {
  236. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  237. }
  238. }
  239. void rsa_cipher::import_public_key_file_pem(std::string_view file_path) {
  240. resource_wrapper bio_file
  241. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "r") };
  242. if (bio_file.is_valid()) {
  243. m_rsa.set(_read_public_key_pem_from_bio(bio_file.get()));
  244. } else {
  245. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  246. }
  247. }
  248. void rsa_cipher::import_public_key_file_pkcs1(std::string_view file_path) {
  249. resource_wrapper bio_file
  250. { resource_traits::openssl::bio{}, BIO_new_file(file_path.data(), "r") };
  251. if (bio_file.is_valid()) {
  252. m_rsa.set(_read_public_key_pkcs1_from_bio(bio_file.get()));
  253. } else {
  254. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new_file failed.");
  255. }
  256. }
  257. [[nodiscard]]
  258. std::string rsa_cipher::export_private_key_string() const {
  259. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  260. if (bio_memory.is_valid()) {
  261. _write_private_key_to_bio(m_rsa.get(), bio_memory.get());
  262. const char* pch = nullptr;
  263. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  264. return std::string(pch, lch);
  265. } else {
  266. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  267. }
  268. }
  269. [[nodiscard]]
  270. std::string rsa_cipher::export_public_key_string_pem() const {
  271. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  272. if (bio_memory.is_valid()) {
  273. _write_public_key_pem_to_bio(m_rsa.get(), bio_memory.get());
  274. const char* pch = nullptr;
  275. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  276. return std::string(pch, lch);
  277. } else {
  278. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  279. }
  280. }
  281. [[nodiscard]]
  282. std::string rsa_cipher::export_public_key_string_pkcs1() const {
  283. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  284. if (bio_memory.is_valid()) {
  285. _write_public_key_pkcs1_to_bio(m_rsa.get(), bio_memory.get());
  286. const char* pch = nullptr;
  287. long lch = BIO_get_mem_data(bio_memory.get(), &pch);
  288. return std::string(pch, lch);
  289. } else {
  290. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  291. }
  292. }
  293. void rsa_cipher::import_private_key_string(std::string_view key_string) {
  294. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  295. if (!bio_memory.is_valid()) {
  296. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  297. }
  298. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  299. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  300. }
  301. m_rsa.set(_read_private_key_from_bio(bio_memory.get()));
  302. }
  303. void rsa_cipher::import_public_key_string_pem(std::string_view key_string) {
  304. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  305. if (!bio_memory.is_valid()) {
  306. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  307. }
  308. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  309. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  310. }
  311. m_rsa.set(_read_public_key_pem_from_bio(bio_memory.get()));
  312. }
  313. void rsa_cipher::import_public_key_string_pkcs1(std::string_view key_string) {
  314. resource_wrapper bio_memory{ resource_traits::openssl::bio{}, BIO_new(BIO_s_mem()) };
  315. if (!bio_memory.is_valid()) {
  316. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_new failed.");
  317. }
  318. if (BIO_puts(bio_memory.get(), key_string.data()) <= 0) {
  319. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"BIO_puts failed.");
  320. }
  321. m_rsa.set(_read_public_key_pkcs1_from_bio(bio_memory.get()));
  322. }
  323. size_t rsa_cipher::public_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const {
  324. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  325. if (plaintext_size <= INT_MAX) {
  326. int bytes_written =
  327. RSA_public_encrypt(static_cast<int>(plaintext_size), reinterpret_cast<const unsigned char*>(plaintext), reinterpret_cast<unsigned char*>(ciphertext), m_rsa.get(), padding);
  328. if (bytes_written != -1) {
  329. return bytes_written;
  330. } else {
  331. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_encrypt failed.");
  332. }
  333. } else {
  334. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"plaintext_size > INT_MAX");
  335. }
  336. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  337. resource_wrapper evp_pkey_context{ resource_traits::openssl::evp_pkey_ctx{}, EVP_PKEY_CTX_new(m_rsa.get(), nullptr) };
  338. if (!evp_pkey_context.is_valid()) {
  339. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_new failed.");
  340. }
  341. if (EVP_PKEY_encrypt_init(evp_pkey_context.get()) <= 0) { // return 1 for success, 0 or a negative value for failure
  342. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_encrypt_init failed.");
  343. }
  344. if (EVP_PKEY_CTX_set_rsa_padding(evp_pkey_context.get(), padding) <= 0) { // return a positive value for success, 0 or a negative value for failure
  345. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set_rsa_padding failed.");
  346. }
  347. size_t ciphertext_size = 0;
  348. if (EVP_PKEY_encrypt(evp_pkey_context.get(), nullptr, &ciphertext_size, reinterpret_cast<const unsigned char*>(plaintext), plaintext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  349. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_encrypt failed.");
  350. }
  351. if (EVP_PKEY_encrypt(evp_pkey_context.get(), reinterpret_cast<unsigned char*>(ciphertext), &ciphertext_size, reinterpret_cast<const unsigned char*>(plaintext), plaintext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  352. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_encrypt failed.");
  353. }
  354. return ciphertext_size;
  355. #else
  356. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  357. #endif
  358. }
  359. size_t rsa_cipher::private_encrypt(const void* plaintext, size_t plaintext_size, void* ciphertext, int padding) const {
  360. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  361. if (plaintext_size <= INT_MAX) {
  362. int bytes_written =
  363. RSA_private_encrypt(static_cast<int>(plaintext_size), reinterpret_cast<const unsigned char*>(plaintext), reinterpret_cast<unsigned char*>(ciphertext), m_rsa.get(), padding);
  364. if (bytes_written != -1) {
  365. return bytes_written;
  366. } else {
  367. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_encrypt failed.");
  368. }
  369. } else {
  370. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"plaintext_size > INT_MAX");
  371. }
  372. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  373. resource_wrapper evp_pkey_context{ resource_traits::openssl::evp_pkey_ctx{}, EVP_PKEY_CTX_new(m_rsa.get(), nullptr) };
  374. if (!evp_pkey_context.is_valid()) {
  375. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_new failed.");
  376. }
  377. if (EVP_PKEY_sign_init(evp_pkey_context.get()) <= 0) { // return 1 for success, 0 or a negative value for failure
  378. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_sign_init failed.");
  379. }
  380. if (EVP_PKEY_CTX_set_rsa_padding(evp_pkey_context.get(), padding) <= 0) { // return a positive value for success, 0 or a negative value for failure
  381. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set_rsa_padding failed.");
  382. }
  383. size_t ciphertext_size = 0;
  384. if (EVP_PKEY_sign(evp_pkey_context.get(), nullptr, &ciphertext_size, reinterpret_cast<const unsigned char*>(plaintext), plaintext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  385. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_sign failed.");
  386. }
  387. if (EVP_PKEY_sign(evp_pkey_context.get(), reinterpret_cast<unsigned char*>(ciphertext), &ciphertext_size, reinterpret_cast<const unsigned char*>(plaintext), plaintext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  388. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_sign failed.");
  389. }
  390. return ciphertext_size;
  391. #else
  392. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  393. #endif
  394. }
  395. size_t rsa_cipher::public_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const {
  396. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  397. if (ciphertext_size <= INT_MAX) {
  398. int bytes_written =
  399. RSA_public_decrypt(static_cast<int>(ciphertext_size), reinterpret_cast<const unsigned char*>(ciphertext), reinterpret_cast<unsigned char*>(plaintext), m_rsa.get(), padding);
  400. if (bytes_written != -1) {
  401. return bytes_written;
  402. } else {
  403. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_decrypt failed.")
  404. .push_hint(u8"Are your sure you DO provide a correct public key?");
  405. }
  406. } else {
  407. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"ciphertext_size > INT_MAX");
  408. }
  409. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  410. resource_wrapper evp_pkey_context{ resource_traits::openssl::evp_pkey_ctx{}, EVP_PKEY_CTX_new(m_rsa.get(), nullptr) };
  411. if (!evp_pkey_context.is_valid()) {
  412. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_new failed.");
  413. }
  414. if (EVP_PKEY_verify_recover_init(evp_pkey_context.get())) { // return 1 for success, 0 or a negative value for failure
  415. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_verify_recover_init failed.");
  416. }
  417. if (EVP_PKEY_CTX_set_rsa_padding(evp_pkey_context.get(), padding) <= 0) { // return a positive value for success, 0 or a negative value for failure
  418. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set_rsa_padding failed.");
  419. }
  420. size_t plaintext_size = 0;
  421. if (EVP_PKEY_verify_recover(evp_pkey_context.get(), nullptr, &plaintext_size, reinterpret_cast<const unsigned char*>(ciphertext), ciphertext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  422. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_verify_recover failed.");
  423. }
  424. if (EVP_PKEY_verify_recover(evp_pkey_context.get(), reinterpret_cast<unsigned char*>(plaintext), &plaintext_size, reinterpret_cast<const unsigned char*>(ciphertext), ciphertext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  425. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_verify_recover failed.");
  426. }
  427. return plaintext_size;
  428. #else
  429. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  430. #endif
  431. }
  432. size_t rsa_cipher::private_decrypt(const void* ciphertext, size_t ciphertext_size, void* plaintext, int padding) const {
  433. #if (OPENSSL_VERSION_NUMBER & 0xf0000000) < 0x30000000 // for openssl < 3.0.0
  434. if (ciphertext_size <= INT_MAX) {
  435. int bytes_written =
  436. RSA_private_decrypt(static_cast<int>(ciphertext_size), reinterpret_cast<const unsigned char*>(ciphertext), reinterpret_cast<unsigned char*>(plaintext), m_rsa.get(), padding);
  437. if (bytes_written != -1) {
  438. return bytes_written;
  439. } else {
  440. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ERR_get_error(), u8"RSA_public_decrypt failed.")
  441. .push_hint(u8"Are your sure you DO provide a correct private key?");
  442. }
  443. } else {
  444. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"ciphertext_size > INT_MAX");
  445. }
  446. #elif (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
  447. resource_wrapper evp_pkey_context{ resource_traits::openssl::evp_pkey_ctx{}, EVP_PKEY_CTX_new(m_rsa.get(), nullptr) };
  448. if (!evp_pkey_context.is_valid()) {
  449. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_new failed.");
  450. }
  451. if (EVP_PKEY_decrypt_init(evp_pkey_context.get()) <= 0) { // return 1 for success, 0 or a negative value for failure
  452. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_decrypt_init failed.");
  453. }
  454. if (EVP_PKEY_CTX_set_rsa_padding(evp_pkey_context.get(), padding) <= 0) { // return a positive value for success, 0 or a negative value for failure
  455. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_CTX_set_rsa_padding failed.");
  456. }
  457. size_t plaintext_size = 0;
  458. if (EVP_PKEY_decrypt(evp_pkey_context.get(), nullptr, &plaintext_size, reinterpret_cast<const unsigned char*>(ciphertext), ciphertext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  459. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_decrypt failed.");
  460. }
  461. if (EVP_PKEY_decrypt(evp_pkey_context.get(), reinterpret_cast<unsigned char*>(plaintext), &plaintext_size, reinterpret_cast<const unsigned char*>(ciphertext), ciphertext_size) <= 0) { // return 1 for success, 0 or a negative value for failure
  462. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_PKEY_decrypt failed.");
  463. }
  464. return plaintext_size;
  465. #else
  466. #error "rsa_cipher.cpp: Unexpected OpenSSL version."
  467. #endif
  468. }
  469. }
  470. #undef NKG_CURRENT_SOURCE_FILE
  471. #undef NKG_CURRENT_SOURCE_LINE