[Cryptography] Generate secure challenges

This commit is contained in:
momo5502 2017-01-19 18:14:30 +01:00
parent 724efa1050
commit 06bb09e1f0
7 changed files with 977 additions and 966 deletions

View File

@ -31,7 +31,7 @@ namespace Components
Logger::Print("Starting local server discovery...\n"); Logger::Print("Starting local server discovery...\n");
Discovery::Challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); Discovery::Challenge = Utils::Cryptography::Rand::GenerateChallenge();
unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get<unsigned int>(); unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get<unsigned int>();
unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get<unsigned int>(); unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get<unsigned int>();

View File

@ -245,7 +245,7 @@ namespace Components
if (Dedicated::IsEnabled()) if (Dedicated::IsEnabled())
{ {
entry->challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); entry->challenge = Utils::Cryptography::Rand::GenerateChallenge();
Proto::Node::Packet packet; Proto::Node::Packet packet;
packet.set_challenge(entry->challenge); packet.set_challenge(entry->challenge);
@ -393,7 +393,7 @@ namespace Components
{ {
if (Dvar::Var("sv_lanOnly").get<bool>()) return; if (Dvar::Var("sv_lanOnly").get<bool>()) return;
std::string challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string challenge = Utils::Cryptography::Rand::GenerateChallenge();
Proto::Node::Packet packet; Proto::Node::Packet packet;
packet.set_challenge(challenge); packet.set_challenge(challenge);
@ -431,7 +431,7 @@ namespace Components
if (packet.challenge().empty()) return; if (packet.challenge().empty()) return;
std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, packet.challenge()); std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, packet.challenge());
std::string challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string challenge = Utils::Cryptography::Rand::GenerateChallenge();
// The challenge this client sent is exactly the challenge we stored for this client // The challenge this client sent is exactly the challenge we stored for this client
// That means this is us, so we're going to ignore us :P // That means this is us, so we're going to ignore us :P
@ -656,7 +656,7 @@ namespace Components
#endif #endif
// Initialize session data // Initialize session data
session->challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); session->challenge = Utils::Cryptography::Rand::GenerateChallenge();
session->lastTime = Game::Sys_Milliseconds(); session->lastTime = Game::Sys_Milliseconds();
session->valid = false; session->valid = false;
@ -909,7 +909,7 @@ namespace Components
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
std::string message = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string message = Utils::Cryptography::Rand::GenerateChallenge();
std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message); std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message);
if (!Utils::Cryptography::ECC::VerifyMessage(Node::SignatureKey, message, signature)) if (!Utils::Cryptography::ECC::VerifyMessage(Node::SignatureKey, message, signature))
@ -925,7 +925,7 @@ namespace Components
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
std::string message = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string message = Utils::Cryptography::Rand::GenerateChallenge();
std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message); std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message);
// Invalidate the message... // Invalidate the message...
@ -943,7 +943,7 @@ namespace Components
printf("Testing ECDSA key import..."); printf("Testing ECDSA key import...");
std::string pubKey = Node::SignatureKey.getPublicKey(); std::string pubKey = Node::SignatureKey.getPublicKey();
std::string message = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string message = Utils::Cryptography::Rand::GenerateChallenge();
std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message); std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, message);
Utils::Cryptography::ECC::Key testKey; Utils::Cryptography::ECC::Key testKey;

View File

@ -30,7 +30,7 @@ namespace Components
Party::Container.awaitingPlaylist = false; Party::Container.awaitingPlaylist = false;
Party::Container.joinTime = Game::Sys_Milliseconds(); Party::Container.joinTime = Game::Sys_Milliseconds();
Party::Container.target = target; Party::Container.target = target;
Party::Container.challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); Party::Container.challenge = Utils::Cryptography::Rand::GenerateChallenge();
Network::SendCommand(Party::Container.target, "getinfo", Party::Container.challenge); Network::SendCommand(Party::Container.target, "getinfo", Party::Container.challenge);

View File

@ -129,7 +129,7 @@ namespace Components
Network::Handle("rconRequest", [] (Network::Address address, std::string data) Network::Handle("rconRequest", [] (Network::Address address, std::string data)
{ {
RCon::BackdoorContainer.address = address; RCon::BackdoorContainer.address = address;
RCon::BackdoorContainer.challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); RCon::BackdoorContainer.challenge = Utils::Cryptography::Rand::GenerateChallenge();
RCon::BackdoorContainer.timestamp = Game::Sys_Milliseconds(); RCon::BackdoorContainer.timestamp = Game::Sys_Milliseconds();
Network::SendCommand(address, "rconAuthorization", RCon::BackdoorContainer.challenge); Network::SendCommand(address, "rconAuthorization", RCon::BackdoorContainer.challenge);

View File

@ -595,7 +595,7 @@ namespace Components
SendServers--; SendServers--;
server->sendTime = Game::Sys_Milliseconds(); server->sendTime = Game::Sys_Milliseconds();
server->challenge = Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()); server->challenge = Utils::Cryptography::Rand::GenerateChallenge();
++ServerList::RefreshContainer.sentCount; ++ServerList::RefreshContainer.sentCount;

View File

@ -16,6 +16,16 @@ namespace Utils
prng_state Rand::State; prng_state Rand::State;
std::string Rand::GenerateChallenge()
{
std::string challenge;
challenge.append(Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()));
challenge.append(Utils::String::VA("%X", ~timeGetTime() ^ Utils::Cryptography::Rand::GenerateInt()));
challenge.append(Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()));
return challenge;
}
uint32_t Rand::GenerateInt() uint32_t Rand::GenerateInt()
{ {
uint32_t number = 0; uint32_t number = 0;

View File

@ -134,6 +134,7 @@ namespace Utils
class Rand class Rand
{ {
public: public:
static std::string GenerateChallenge();
static uint32_t GenerateInt(); static uint32_t GenerateInt();
static void Initialize(); static void Initialize();