Files
OpenRCT2-Web/src/openrct2/core/Crypt.OpenSSL.cpp
T

353 lines
9.5 KiB
C++
Raw Normal View History

2018-05-23 20:27:00 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
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
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-05-23 20:27:00 +01:00
2020-02-01 20:46:18 +00:00
#if defined(DISABLE_NETWORK) && !defined(__USE_CNG__)
2018-05-23 20:27:00 +01:00
2018-07-21 16:17:06 +02:00
# include "Crypt.h"
2018-06-22 22:58:39 +02:00
2018-07-21 16:17:06 +02:00
# include <openssl/evp.h>
# include <openssl/pem.h>
# include <stdexcept>
# include <string>
# include <vector>
2018-05-23 20:27:00 +01:00
using namespace Crypt;
2018-05-25 00:39:27 +01:00
static void OpenSSLThrowOnBadStatus(const std::string_view& name, int status)
{
if (status != 1)
{
throw std::runtime_error(std::string(name) + " failed: " + std::to_string(status));
}
}
static void OpenSSLInitialise()
{
static bool _opensslInitialised = false;
if (!_opensslInitialised)
{
_opensslInitialised = true;
OpenSSL_add_all_algorithms();
}
}
2018-06-22 22:58:39 +02:00
template<typename TBase> class OpenSSLHashAlgorithm final : public TBase
2018-05-23 20:27:00 +01:00
{
private:
2018-06-22 22:58:39 +02:00
const EVP_MD* _type;
EVP_MD_CTX* _ctx{};
2018-05-23 20:27:00 +01:00
bool _initialised{};
public:
2018-06-22 22:58:39 +02:00
OpenSSLHashAlgorithm(const EVP_MD* type)
2018-05-23 20:27:00 +01:00
{
2018-05-25 00:39:27 +01:00
_type = type;
2018-05-23 20:27:00 +01:00
_ctx = EVP_MD_CTX_create();
if (_ctx == nullptr)
{
throw std::runtime_error("EVP_MD_CTX_create failed");
}
}
2018-05-25 00:39:27 +01:00
~OpenSSLHashAlgorithm()
2018-05-23 20:27:00 +01:00
{
EVP_MD_CTX_destroy(_ctx);
}
2018-06-22 22:58:39 +02:00
TBase* Clear() override
2018-05-23 20:27:00 +01:00
{
2018-05-25 00:39:27 +01:00
if (EVP_DigestInit_ex(_ctx, _type, nullptr) <= 0)
2018-05-23 20:27:00 +01:00
{
throw std::runtime_error("EVP_DigestInit_ex failed");
}
_initialised = true;
2018-05-25 00:39:27 +01:00
return this;
2018-05-23 20:27:00 +01:00
}
2018-06-22 22:58:39 +02:00
TBase* Update(const void* data, size_t dataLen) override
2018-05-23 20:27:00 +01:00
{
// Auto initialise
if (!_initialised)
{
Clear();
}
if (EVP_DigestUpdate(_ctx, data, dataLen) <= 0)
{
throw std::runtime_error("EVP_DigestUpdate failed");
}
2018-05-25 00:39:27 +01:00
return this;
2018-05-23 20:27:00 +01:00
}
2018-05-25 00:39:27 +01:00
typename TBase::Result Finish() override
2018-05-23 20:27:00 +01:00
{
if (!_initialised)
{
throw std::runtime_error("No data to hash.");
}
_initialised = false;
2018-05-25 00:39:27 +01:00
typename TBase::Result result;
2018-05-23 20:27:00 +01:00
unsigned int digestSize{};
if (EVP_DigestFinal(_ctx, result.data(), &digestSize) <= 0)
{
EVP_MD_CTX_destroy(_ctx);
throw std::runtime_error("EVP_DigestFinal failed");
}
if (digestSize != result.size())
{
throw std::runtime_error("Expected digest size to equal " + std::to_string(result.size()));
}
return result;
}
};
2018-05-25 00:39:27 +01:00
class OpenSSLRsaKey final : public RsaKey
{
public:
2018-06-22 22:58:39 +02:00
EVP_PKEY* GetEvpKey() const
{
return _evpKey;
}
2018-05-25 00:39:27 +01:00
~OpenSSLRsaKey()
{
EVP_PKEY_free(_evpKey);
}
void Generate() override
{
auto ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
if (ctx == nullptr)
{
throw std::runtime_error("EVP_PKEY_CTX_new_id failed");
}
try
{
auto status = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
if (status == 0)
{
throw std::runtime_error("EVP_PKEY_CTX_set_rsa_keygen_bits failed");
}
status = EVP_PKEY_keygen_init(ctx);
OpenSSLThrowOnBadStatus("EVP_PKEY_keygen_init", status);
2018-06-22 22:58:39 +02:00
EVP_PKEY* key{};
status = EVP_PKEY_keygen(ctx, &key);
OpenSSLThrowOnBadStatus("EVP_PKEY_keygen", status);
EVP_PKEY_free(_evpKey);
_evpKey = key;
EVP_PKEY_CTX_free(ctx);
}
catch (const std::exception&)
{
EVP_PKEY_CTX_free(ctx);
throw;
}
}
2018-05-25 23:12:44 +01:00
void SetPrivate(const std::string_view& pem) override
{
SetKey(pem, true);
}
void SetPublic(const std::string_view& pem) override
{
SetKey(pem, false);
}
2018-06-22 22:58:39 +02:00
std::string GetPrivate() override
{
return GetKey(true);
}
2018-05-25 23:12:44 +01:00
2018-06-22 22:58:39 +02:00
std::string GetPublic() override
{
return GetKey(false);
}
2018-05-25 23:12:44 +01:00
private:
2018-06-22 22:58:39 +02:00
EVP_PKEY* _evpKey{};
2018-05-25 23:12:44 +01:00
void SetKey(const std::string_view& pem, bool isPrivate)
{
// Read PEM data via BIO buffer
2018-06-01 22:44:25 +01:00
// HACK first parameter is not const on MINGW for some reason
2018-06-22 22:58:39 +02:00
auto bio = BIO_new_mem_buf((void*)pem.data(), (int)pem.size());
2018-05-25 23:12:44 +01:00
if (bio == nullptr)
{
throw std::runtime_error("BIO_new_mem_buf failed");
}
2018-06-22 22:58:39 +02:00
auto rsa = isPrivate ? PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, nullptr)
: PEM_read_bio_RSAPublicKey(bio, nullptr, nullptr, nullptr);
2018-05-25 23:12:44 +01:00
if (rsa == nullptr)
{
BIO_free_all(bio);
2018-06-22 22:58:39 +02:00
auto msg = isPrivate ? "PEM_read_bio_RSAPrivateKey failed" : "PEM_read_bio_RSAPublicKey failed";
2018-05-25 23:12:44 +01:00
throw std::runtime_error(msg);
}
BIO_free_all(bio);
if (isPrivate && !RSA_check_key(rsa))
{
RSA_free(rsa);
throw std::runtime_error("PEM key was invalid");
}
// Assign new key
EVP_PKEY_free(_evpKey);
_evpKey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(_evpKey, rsa);
RSA_free(rsa);
}
std::string GetKey(bool isPrivate)
{
if (_evpKey == nullptr)
{
throw std::runtime_error("No key has been assigned");
}
auto rsa = EVP_PKEY_get1_RSA(_evpKey);
if (rsa == nullptr)
{
throw std::runtime_error("EVP_PKEY_get1_RSA failed");
}
auto bio = BIO_new(BIO_s_mem());
if (bio == nullptr)
{
throw std::runtime_error("BIO_new failed");
}
2018-06-22 22:58:39 +02:00
auto status = isPrivate ? PEM_write_bio_RSAPrivateKey(bio, rsa, nullptr, nullptr, 0, nullptr, nullptr)
: PEM_write_bio_RSAPublicKey(bio, rsa);
2018-05-25 23:12:44 +01:00
if (status != 1)
{
BIO_free_all(bio);
RSA_free(rsa);
throw std::runtime_error("PEM_write_bio_RSAPrivateKey failed");
}
RSA_free(rsa);
auto keylen = BIO_pending(bio);
std::string result(keylen, 0);
BIO_read(bio, result.data(), keylen);
BIO_free_all(bio);
return result;
}
2018-05-25 00:39:27 +01:00
};
class OpenSSLRsaAlgorithm final : public RsaAlgorithm
{
public:
2018-06-22 22:58:39 +02:00
std::vector<uint8_t> SignData(const RsaKey& key, const void* data, size_t dataLen) override
2018-05-25 00:39:27 +01:00
{
2018-05-25 23:12:44 +01:00
auto evpKey = static_cast<const OpenSSLRsaKey&>(key).GetEvpKey();
2018-06-22 22:58:39 +02:00
EVP_MD_CTX* mdctx{};
2018-05-25 00:39:27 +01:00
try
{
mdctx = EVP_MD_CTX_create();
if (mdctx == nullptr)
{
throw std::runtime_error("EVP_MD_CTX_create failed");
}
auto status = EVP_DigestSignInit(mdctx, nullptr, EVP_sha256(), nullptr, evpKey);
OpenSSLThrowOnBadStatus("EVP_DigestSignInit failed", status);
status = EVP_DigestSignUpdate(mdctx, data, dataLen);
OpenSSLThrowOnBadStatus("EVP_DigestSignUpdate failed", status);
// Get required length of signature
size_t sigLen{};
status = EVP_DigestSignFinal(mdctx, nullptr, &sigLen);
OpenSSLThrowOnBadStatus("EVP_DigestSignFinal failed", status);
// Get signature
std::vector<uint8_t> signature(sigLen);
status = EVP_DigestSignFinal(mdctx, signature.data(), &sigLen);
OpenSSLThrowOnBadStatus("EVP_DigestSignFinal failed", status);
EVP_MD_CTX_destroy(mdctx);
return signature;
}
catch (const std::exception&)
{
EVP_MD_CTX_destroy(mdctx);
throw;
}
}
2018-06-22 22:58:39 +02:00
bool VerifyData(const RsaKey& key, const void* data, size_t dataLen, const void* sig, size_t sigLen) override
2018-05-25 00:39:27 +01:00
{
2018-05-25 23:12:44 +01:00
auto evpKey = static_cast<const OpenSSLRsaKey&>(key).GetEvpKey();
2018-06-22 22:58:39 +02:00
EVP_MD_CTX* mdctx{};
2018-05-25 00:39:27 +01:00
try
{
mdctx = EVP_MD_CTX_create();
if (mdctx == nullptr)
{
throw std::runtime_error("EVP_MD_CTX_create failed");
}
auto status = EVP_DigestVerifyInit(mdctx, nullptr, EVP_sha256(), nullptr, evpKey);
OpenSSLThrowOnBadStatus("EVP_DigestVerifyInit", status);
status = EVP_DigestVerifyUpdate(mdctx, data, dataLen);
OpenSSLThrowOnBadStatus("EVP_DigestVerifyUpdate", status);
status = EVP_DigestVerifyFinal(mdctx, (uint8_t*)sig, sigLen);
if (status != 0 && status != 1)
{
OpenSSLThrowOnBadStatus("EVP_DigestVerifyUpdate", status);
}
EVP_MD_CTX_destroy(mdctx);
2018-05-25 23:12:44 +01:00
return status == 1;
2018-05-25 00:39:27 +01:00
}
catch (const std::exception&)
{
EVP_MD_CTX_destroy(mdctx);
throw;
}
}
};
namespace Crypt
2018-05-23 20:27:00 +01:00
{
std::unique_ptr<Sha1Algorithm> CreateSHA1()
{
OpenSSLInitialise();
2018-05-25 00:39:27 +01:00
return std::make_unique<OpenSSLHashAlgorithm<Sha1Algorithm>>(EVP_sha1());
2018-05-23 20:27:00 +01:00
}
2018-05-25 00:39:27 +01:00
std::unique_ptr<Sha256Algorithm> CreateSHA256()
2018-05-23 20:27:00 +01:00
{
OpenSSLInitialise();
2018-05-25 00:39:27 +01:00
return std::make_unique<OpenSSLHashAlgorithm<Sha256Algorithm>>(EVP_sha256());
}
std::unique_ptr<RsaAlgorithm> CreateRSA()
{
OpenSSLInitialise();
2018-05-25 00:39:27 +01:00
return std::make_unique<OpenSSLRsaAlgorithm>();
}
std::unique_ptr<RsaKey> CreateRSAKey()
{
OpenSSLInitialise();
2018-05-25 00:39:27 +01:00
return std::make_unique<OpenSSLRsaKey>();
2018-05-23 20:27:00 +01:00
}
2018-06-22 22:58:39 +02:00
} // namespace Crypt
2018-05-23 20:27:00 +01:00
#endif // DISABLE_NETWORK