maint(utils): clean up RSA code
This commit is contained in:
parent
e26eed4c11
commit
da34ecbba7
@ -15,6 +15,8 @@ namespace Components
|
|||||||
|
|
||||||
bool Node::WasIngame = false;
|
bool Node::WasIngame = false;
|
||||||
|
|
||||||
|
const Game::dvar_t* Node::net_natFix;
|
||||||
|
|
||||||
bool Node::Entry::isValid() const
|
bool Node::Entry::isValid() const
|
||||||
{
|
{
|
||||||
return (this->lastResponse.has_value() && !this->lastResponse->elapsed(NODE_HALFLIFE * 2));
|
return (this->lastResponse.has_value() && !this->lastResponse->elapsed(NODE_HALFLIFE * 2));
|
||||||
@ -349,9 +351,9 @@ namespace Components
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short Node::GetPort()
|
std::uint16_t Node::GetPort()
|
||||||
{
|
{
|
||||||
if (Dvar::Var("net_natFix").get<bool>()) return 0;
|
if (net_natFix->current.enabled) return 0;
|
||||||
return Network::GetPort();
|
return Network::GetPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,7 +397,7 @@ namespace Components
|
|||||||
|
|
||||||
Node::Node()
|
Node::Node()
|
||||||
{
|
{
|
||||||
Dvar::Register<bool>("net_natFix", false, 0, "Fix node registration for certain firewalls/routers");
|
net_natFix = Game::Dvar_RegisterBool("net_natFix", false, 0, "Fix node registration for certain firewalls/routers");
|
||||||
|
|
||||||
Scheduler::Loop([]
|
Scheduler::Loop([]
|
||||||
{
|
{
|
||||||
|
@ -12,7 +12,7 @@ namespace Components
|
|||||||
class Data
|
class Data
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint64_t protocol;
|
std::uint64_t protocol;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Entry
|
class Entry
|
||||||
@ -46,6 +46,8 @@ namespace Components
|
|||||||
static std::vector<Entry> Nodes;
|
static std::vector<Entry> Nodes;
|
||||||
static bool WasIngame;
|
static bool WasIngame;
|
||||||
|
|
||||||
|
static const Game::dvar_t* net_natFix;
|
||||||
|
|
||||||
static void HandleResponse(const Network::Address& address, const std::string& data);
|
static void HandleResponse(const Network::Address& address, const std::string& data);
|
||||||
|
|
||||||
static void SendList(const Network::Address& address);
|
static void SendList(const Network::Address& address);
|
||||||
@ -54,7 +56,7 @@ namespace Components
|
|||||||
static void LoadNodes();
|
static void LoadNodes();
|
||||||
static void StoreNodes(bool force);
|
static void StoreNodes(bool force);
|
||||||
|
|
||||||
static unsigned short GetPort();
|
static std::uint16_t GetPort();
|
||||||
|
|
||||||
static void Migrate();
|
static void Migrate();
|
||||||
};
|
};
|
||||||
|
@ -127,10 +127,10 @@ namespace Components
|
|||||||
{
|
{
|
||||||
Utils::InfoString info;
|
Utils::InfoString info;
|
||||||
|
|
||||||
info.set("admin", Dvar::Var("_Admin").get<const char*>());
|
info.set("admin", Dvar::Var("_Admin").get<std::string>());
|
||||||
info.set("website", Dvar::Var("_Website").get<const char*>());
|
info.set("website", Dvar::Var("_Website").get<std::string>());
|
||||||
info.set("email", Dvar::Var("_Email").get<const char*>());
|
info.set("email", Dvar::Var("_Email").get<std::string>());
|
||||||
info.set("location", Dvar::Var("_Location").get<const char*>());
|
info.set("location", Dvar::Var("_Location").get<std::string>());
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,6 @@ namespace Utils
|
|||||||
Key key;
|
Key key;
|
||||||
|
|
||||||
register_prng(&sprng_desc);
|
register_prng(&sprng_desc);
|
||||||
register_hash(&sha1_desc);
|
|
||||||
|
|
||||||
ltc_mp = ltm_desc;
|
ltc_mp = ltm_desc;
|
||||||
|
|
||||||
@ -105,15 +104,20 @@ namespace Utils
|
|||||||
{
|
{
|
||||||
if (!key.isValid()) return {};
|
if (!key.isValid()) return {};
|
||||||
|
|
||||||
std::uint8_t buffer[512];
|
std::uint8_t buffer[512]{};
|
||||||
unsigned long length = sizeof(buffer);
|
unsigned long length = sizeof(buffer);
|
||||||
|
|
||||||
|
const auto hash = SHA512::Compute(message);
|
||||||
|
|
||||||
register_prng(&sprng_desc);
|
register_prng(&sprng_desc);
|
||||||
register_hash(&sha1_desc);
|
|
||||||
|
const ltc_hash_descriptor& hash_desc = sha512_desc;
|
||||||
|
const int hash_index = register_hash(&hash_desc);
|
||||||
|
|
||||||
ltc_mp = ltm_desc;
|
ltc_mp = ltm_desc;
|
||||||
|
|
||||||
rsa_sign_hash(reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), find_hash("sha1"), 0, key.getKeyPtr());
|
rsa_sign_hash_ex(reinterpret_cast<const std::uint8_t*>(hash.data()), hash.size(),
|
||||||
|
buffer, &length, LTC_PKCS_1_V1_5, nullptr, find_prng("sprng"), hash_index, 0, key.getKeyPtr());
|
||||||
|
|
||||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||||
}
|
}
|
||||||
@ -122,12 +126,17 @@ namespace Utils
|
|||||||
{
|
{
|
||||||
if (!key.isValid()) return false;
|
if (!key.isValid()) return false;
|
||||||
|
|
||||||
register_hash(&sha1_desc);
|
const auto hash = SHA512::Compute(message);
|
||||||
|
|
||||||
|
const ltc_hash_descriptor& hash_desc = sha512_desc;
|
||||||
|
const int hash_index = register_hash(&hash_desc);
|
||||||
|
|
||||||
ltc_mp = ltm_desc;
|
ltc_mp = ltm_desc;
|
||||||
|
|
||||||
int result = 0;
|
auto result = 0;
|
||||||
return (rsa_verify_hash(reinterpret_cast<const std::uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), find_hash("sha1"), 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
|
return (rsa_verify_hash_ex(reinterpret_cast<const std::uint8_t*>(signature.data()), signature.size(),
|
||||||
|
reinterpret_cast<const std::uint8_t*>(hash.data()), hash.size(), LTC_PKCS_1_V1_5,
|
||||||
|
hash_index, 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
@ -173,7 +173,7 @@ namespace Utils
|
|||||||
[[nodiscard]] std::string getPublicKey()
|
[[nodiscard]] std::string getPublicKey()
|
||||||
{
|
{
|
||||||
std::uint8_t buffer[512]{};
|
std::uint8_t buffer[512]{};
|
||||||
DWORD length = sizeof(buffer);
|
unsigned long length = sizeof(buffer);
|
||||||
|
|
||||||
if (ecc_ansi_x963_export(this->getKeyPtr(), buffer, &length) == CRYPT_OK)
|
if (ecc_ansi_x963_export(this->getKeyPtr(), buffer, &length) == CRYPT_OK)
|
||||||
{
|
{
|
||||||
@ -206,7 +206,7 @@ namespace Utils
|
|||||||
[[nodiscard]] std::string serialize(int type = PK_PRIVATE)
|
[[nodiscard]] std::string serialize(int type = PK_PRIVATE)
|
||||||
{
|
{
|
||||||
std::uint8_t buffer[4096]{};
|
std::uint8_t buffer[4096]{};
|
||||||
DWORD length = sizeof(buffer);
|
unsigned long length = sizeof(buffer);
|
||||||
|
|
||||||
if (ecc_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK)
|
if (ecc_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK)
|
||||||
{
|
{
|
||||||
@ -272,6 +272,42 @@ namespace Utils
|
|||||||
return this->keyStorage.get();
|
return this->keyStorage.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::string getPublicKey()
|
||||||
|
{
|
||||||
|
std::uint8_t buffer[4096]{};
|
||||||
|
unsigned long length = sizeof(buffer);
|
||||||
|
|
||||||
|
if (rsa_export(buffer, &length, PK_PUBLIC, this->getKeyPtr()) == CRYPT_OK)
|
||||||
|
{
|
||||||
|
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string{};
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserialize(const std::string& pubKeyBuffer)
|
||||||
|
{
|
||||||
|
this->free();
|
||||||
|
|
||||||
|
if (rsa_import(reinterpret_cast<const std::uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->getKeyPtr()) != CRYPT_OK)
|
||||||
|
{
|
||||||
|
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::string serialize(int type = PK_PRIVATE)
|
||||||
|
{
|
||||||
|
std::uint8_t buffer[4096]{};
|
||||||
|
unsigned long length = sizeof(buffer);
|
||||||
|
|
||||||
|
if (rsa_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK)
|
||||||
|
{
|
||||||
|
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string{};
|
||||||
|
}
|
||||||
|
|
||||||
void free()
|
void free()
|
||||||
{
|
{
|
||||||
if (this->isValid())
|
if (this->isValid())
|
||||||
|
Loading…
Reference in New Issue
Block a user