#include "PacketContent.hpp" #include #include #include #include "Context.hpp" #include "Packet.hpp" #include "SecurityMode.hpp" namespace drp::packet::base { void PacketContent::encryptRsa(const std::array& publicKey) { BIO* bioPublicKey = BIO_new_mem_buf(publicKey.data(), static_cast(publicKey.size())); EVP_PKEY* evpPublicKey = PEM_read_bio_PUBKEY(bioPublicKey, nullptr, nullptr, nullptr); const std::unique_ptr evpContext( EVP_PKEY_CTX_new(evpPublicKey, nullptr), EVP_PKEY_CTX_free ); if (evpContext == nullptr) throw std::runtime_error("Failed to create EVP_PKEY_CTX"); if (EVP_PKEY_encrypt_init(evpContext.get()) <= 0) throw std::runtime_error("Failed to initialize EVP_PKEY_CTX"); if (EVP_PKEY_CTX_set_rsa_padding(evpContext.get(), RSA_PKCS1_PADDING) <= 0) throw std::runtime_error("Failed to set RSA_PKCS1_PADDING"); std::vector cipher(EVP_PKEY_size(evpPublicKey)); std::size_t cipherLength; EVP_PKEY_encrypt( evpContext.get(), cipher.data(), &cipherLength, this, sizeof(*this) ); cipher.resize(cipherLength); } }