Files
OpenRCT2-Web/src/openrct2/network/NetworkKey.cpp
T

218 lines
5.8 KiB
C++
Raw Normal View History

2016-05-05 21:46:13 +02:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2016-05-05 21:46:13 +02:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-05-05 21:46:13 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-05-05 21:46:13 +02:00
*****************************************************************************/
2016-05-20 11:51:31 +02:00
#ifndef DISABLE_NETWORK
#include "NetworkKey.h"
2016-05-05 21:46:13 +02:00
#include "../Diagnostic.h"
#include "../core/Crypt.h"
#include "../core/Guard.hpp"
#include "../core/IStream.hpp"
#include "../core/String.hpp"
2018-06-22 23:02:47 +02:00
#include <vector>
2018-06-22 23:02:47 +02:00
namespace OpenRCT2::Network
2016-05-05 21:46:13 +02:00
{
NetworkKey::NetworkKey() = default;
NetworkKey::~NetworkKey() = default;
2016-05-05 21:46:13 +02:00
void NetworkKey::Unload()
2016-05-25 10:18:26 +02:00
{
_key = nullptr;
2016-05-05 21:46:13 +02:00
}
bool NetworkKey::Generate()
2016-05-21 00:00:23 +02:00
{
try
{
_key = Crypt::CreateRSAKey();
_key->Generate();
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::Generate failed: %s", e.what());
return false;
}
2016-05-05 21:46:13 +02:00
}
bool NetworkKey::LoadPrivate(IStream* stream)
2016-05-25 10:18:26 +02:00
{
Guard::ArgumentNotNull(stream);
2016-05-05 21:46:13 +02:00
size_t size = static_cast<size_t>(stream->GetLength());
if (size == static_cast<size_t>(-1))
{
LOG_ERROR("unknown size, refusing to load key");
return false;
}
if (size > 4 * 1024 * 1024)
{
LOG_ERROR("Key file suspiciously large, refusing to load it");
return false;
}
std::string pem(size, '\0');
stream->Read(pem.data(), pem.size());
try
{
_key = Crypt::CreateRSAKey();
_key->SetPrivate(pem);
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::LoadPrivate failed: %s", e.what());
return false;
}
}
2025-08-31 15:31:19 +02:00
bool NetworkKey::LoadPublic(IStream* stream)
{
Guard::ArgumentNotNull(stream);
size_t size = static_cast<size_t>(stream->GetLength());
if (size == static_cast<size_t>(-1))
{
LOG_ERROR("unknown size, refusing to load key");
return false;
}
if (size > 4 * 1024 * 1024)
{
LOG_ERROR("Key file suspiciously large, refusing to load it");
return false;
}
std::string pem(size, '\0');
stream->Read(pem.data(), pem.size());
try
{
_key = Crypt::CreateRSAKey();
_key->SetPublic(pem);
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::LoadPublic failed: %s", e.what());
return false;
}
}
2025-08-31 15:31:19 +02:00
bool NetworkKey::SavePrivate(IStream* stream)
{
try
{
if (_key == nullptr)
{
throw std::runtime_error("No key loaded");
}
auto pem = _key->GetPrivate();
stream->Write(pem.data(), pem.size());
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::SavePrivate failed: %s", e.what());
return false;
}
}
2025-08-31 15:31:19 +02:00
bool NetworkKey::SavePublic(IStream* stream)
{
try
{
if (_key == nullptr)
{
throw std::runtime_error("No key loaded");
}
auto pem = _key->GetPublic();
stream->Write(pem.data(), pem.size());
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::SavePublic failed: %s", e.what());
return false;
}
}
std::string NetworkKey::PublicKeyString()
2016-05-21 00:00:23 +02:00
{
if (_key == nullptr)
{
2018-06-01 21:08:40 +01:00
throw std::runtime_error("No key loaded");
}
return _key->GetPublic();
}
2016-05-05 21:46:13 +02:00
/**
* @brief NetworkKey::PublicKeyHash
* Computes a short, human-readable (e.g. asciif-ied hex) hash for a given
* public key. Serves a purpose of easy identification keys in multiplayer
* overview, multiplayer settings.
*
* In particular, any of digest functions applied to a standardised key
* representation, like PEM, will be sufficient.
*
* @return returns a string containing key hash.
*/
std::string NetworkKey::PublicKeyHash()
2016-05-21 00:00:23 +02:00
{
try
{
std::string key = PublicKeyString();
if (key.empty())
{
throw std::runtime_error("No key found");
}
auto hash = Crypt::SHA1(key.c_str(), key.size());
return String::StringFromHex(hash);
}
catch (const std::exception& e)
{
LOG_ERROR("Failed to create hash of public key: %s", e.what());
}
return nullptr;
2016-05-19 10:23:42 +02:00
}
bool NetworkKey::Sign(const uint8_t* md, const size_t len, std::vector<uint8_t>& signature) const
2016-05-21 00:00:23 +02:00
{
try
{
auto rsa = Crypt::CreateRSA();
signature = rsa->SignData(*_key, md, len);
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::Sign failed: %s", e.what());
return false;
}
2016-05-05 21:46:13 +02:00
}
bool NetworkKey::Verify(const uint8_t* md, const size_t len, const std::vector<uint8_t>& signature) const
2016-05-21 00:00:23 +02:00
{
try
{
auto rsa = Crypt::CreateRSA();
return rsa->VerifyData(*_key, md, len, signature.data(), signature.size());
}
catch (const std::exception& e)
{
LOG_ERROR("NetworkKey::Verify failed: %s", e.what());
return false;
}
2016-05-05 21:46:13 +02:00
}
} // namespace OpenRCT2::Network
2016-05-20 11:51:31 +02:00
#endif // DISABLE_NETWORK