M2-PT-DRP/source/packets/base/GenericPacket.cpp

90 lines
No EOL
2.3 KiB
C++

#include "GenericPacket.hpp"
#include <stdexcept>
namespace drp::packet {
/*
GenericPacketContent decryptPacketContentAes(const GenericPacket& packet) {
GenericPacketContent decryptedPacketContent {};
const auto& [key, iv] = keysAes[serverAddress];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (EVP_DecryptInit_ex(
ctx,
EVP_aes_256_cbc(),
nullptr,
key,
iv
) != 1)
throw std::runtime_error("[Client] Could not initialize the EVP_CIPHER_CTX.");
int packetContentLength;
if (EVP_DecryptUpdate(
ctx,
reinterpret_cast<std::uint8_t*>(&decryptedPacketContent),
&packetContentLength,
reinterpret_cast<const std::uint8_t*>(&packet.encryptedContent),
sizeof(packet)
) != 1)
throw std::runtime_error("[Client] Could not encrypt the plaintext.");
if (EVP_DecryptFinal_ex(
ctx,
reinterpret_cast<std::uint8_t*>(&decryptedPacketContent + packetContentLength),
&packetContentLength
) != 1)
throw std::runtime_error("[Client] Could not decrypt the final plaintext.");
EVP_CIPHER_CTX_free(ctx);
return decryptedPacketContent;
}
*/
GenericPacketContent GenericPacket::getContent() const {
// TODO(Faraphel): implement RSA and AES
// additional "context" argument to hold cryptographic keys ?
switch (static_cast<SecurityMode>(this->securityMode)) {
case SecurityMode::PLAIN:
return this->_content;
case SecurityMode::AES:
// return decryptPacketContentAes(packet);
throw std::runtime_error("Not implemented.");
case SecurityMode::RSA:
throw std::runtime_error("Not implemented.");
default:
throw std::runtime_error("Unsupported security mode.");
}
}
void GenericPacket::setContent(const GenericPacketContent &content) {
// TODO(Faraphel): implement RSA and AES
switch (static_cast<SecurityMode>(this->securityMode)) {
case SecurityMode::PLAIN:
this->_content = content;
return;
case SecurityMode::AES:
throw std::runtime_error("Not implemented.");
case SecurityMode::RSA:
throw std::runtime_error("Not implemented.");
default:
throw std::runtime_error("Unsupported security mode.");
}
}
}