46 lines
1.3 KiB
C++
46 lines
1.3 KiB
C++
#include "PacketContent.hpp"
|
|
|
|
#include <stdexcept>
|
|
#include <openssl/pem.h>
|
|
#include <openssl/x509.h>
|
|
|
|
#include "Context.hpp"
|
|
#include "Packet.hpp"
|
|
#include "SecurityMode.hpp"
|
|
|
|
|
|
namespace drp::packet::base {
|
|
|
|
|
|
void PacketContent::encryptRsa(const std::array<std::uint8_t, 2048>& publicKey) {
|
|
BIO* bioPublicKey = BIO_new_mem_buf(publicKey.data(), static_cast<int>(publicKey.size()));
|
|
EVP_PKEY* evpPublicKey = PEM_read_bio_PUBKEY(bioPublicKey, nullptr, nullptr, nullptr);
|
|
|
|
const std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> 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<std::uint8_t> cipher(EVP_PKEY_size(evpPublicKey));
|
|
std::size_t cipherLength;
|
|
EVP_PKEY_encrypt(
|
|
evpContext.get(),
|
|
cipher.data(),
|
|
&cipherLength,
|
|
this,
|
|
sizeof(*this)
|
|
);
|
|
cipher.resize(cipherLength);
|
|
|
|
}
|
|
|
|
|
|
}
|