2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2015-12-28 23:53:55 +00:00
|
|
|
#ifdef __APPLE__
|
2017-04-19 12:04:45 +00:00
|
|
|
/* OpenSSL "deprecated" as of OS X 10.7, but we still use it */
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
2015-12-28 23:53:55 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-09-06 22:09:04 +00:00
|
|
|
#include <openssl/crypto.h>
|
2014-12-04 11:27:38 +00:00
|
|
|
#include <openssl/err.h>
|
2014-12-04 12:13:59 +00:00
|
|
|
#include <openssl/evp.h>
|
2015-10-13 21:24:31 +00:00
|
|
|
#include <openssl/opensslv.h>
|
2019-06-22 13:02:17 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/x509.h>
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "transmission.h"
|
2022-07-15 00:54:10 +00:00
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2014-12-04 12:37:08 +00:00
|
|
|
#include "utils.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void log_openssl_error(char const* file, int line)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned long const error_code = ERR_get_error();
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logLevelIsActive(TR_LOG_ERROR))
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2023-01-01 22:24:12 +00:00
|
|
|
if (static bool strings_loaded = false; !strings_loaded)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2018-04-01 20:39:09 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000)
|
2017-04-19 12:04:45 +00:00
|
|
|
ERR_load_crypto_strings();
|
2016-09-06 22:09:04 +00:00
|
|
|
#else
|
2021-09-15 00:18:09 +00:00
|
|
|
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
|
2016-09-06 22:09:04 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
strings_loaded = true;
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
auto buf = std::array<char, 512>{};
|
|
|
|
ERR_error_string_n(error_code, std::data(buf), std::size(buf));
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddMessage(
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
TR_LOG_ERROR,
|
|
|
|
fmt::format(
|
|
|
|
_("{crypto_library} error: {error} ({error_code})"),
|
|
|
|
fmt::arg("crypto_library", "OpenSSL"),
|
2022-08-25 01:19:21 +00:00
|
|
|
fmt::arg("error", std::data(buf)),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error_code", error_code)));
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define log_error() log_openssl_error(__FILE__, __LINE__)
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
bool check_openssl_result(int result, int expected_result, bool expected_equal, char const* file, int line)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const ret = (result == expected_result) == expected_equal;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
log_openssl_error(file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define check_result(result) check_openssl_result((result), 1, true, __FILE__, __LINE__)
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace sha_helpers
|
2014-12-04 12:13:59 +00:00
|
|
|
{
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class ShaHelper
|
2014-12-04 12:13:59 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
|
|
|
using EvpFunc = decltype((EVP_sha1));
|
2021-09-12 17:41:49 +00:00
|
|
|
|
2022-08-06 01:37:21 +00:00
|
|
|
explicit ShaHelper(EvpFunc evp_func)
|
2022-07-31 20:58:14 +00:00
|
|
|
: evp_func_{ evp_func }
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2014-12-04 18:20:46 +00:00
|
|
|
|
2022-08-06 01:37:21 +00:00
|
|
|
void clear() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
EVP_DigestInit_ex(handle_.get(), evp_func_(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-04 18:20:46 +00:00
|
|
|
|
2022-08-06 01:37:21 +00:00
|
|
|
void update(void const* data, size_t data_length) const
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
if (data_length != 0U)
|
|
|
|
{
|
|
|
|
EVP_DigestUpdate(handle_.get(), data, data_length);
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 12:13:59 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
template<typename DigestType>
|
|
|
|
[[nodiscard]] DigestType digest()
|
|
|
|
{
|
|
|
|
TR_ASSERT(handle_ != nullptr);
|
2014-12-04 12:13:59 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
unsigned int hash_length = 0;
|
|
|
|
auto digest = DigestType{};
|
|
|
|
auto* const digest_as_uchar = reinterpret_cast<unsigned char*>(std::data(digest));
|
2022-08-02 17:41:04 +00:00
|
|
|
[[maybe_unused]] bool const ok = check_result(EVP_DigestFinal_ex(handle_.get(), digest_as_uchar, &hash_length));
|
2022-07-31 20:58:14 +00:00
|
|
|
TR_ASSERT(!ok || hash_length == std::size(digest));
|
2021-09-12 17:41:49 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
clear();
|
|
|
|
return digest;
|
|
|
|
}
|
2014-12-04 12:13:59 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
struct MessageDigestDeleter
|
|
|
|
{
|
|
|
|
void operator()(EVP_MD_CTX* ctx) const noexcept
|
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy(ctx);
|
|
|
|
}
|
|
|
|
};
|
2014-12-04 12:13:59 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
EvpFunc evp_func_;
|
|
|
|
std::unique_ptr<EVP_MD_CTX, MessageDigestDeleter> const handle_{ EVP_MD_CTX_create() };
|
|
|
|
};
|
2014-12-04 12:13:59 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class Sha1Impl final : public tr_sha1
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
2022-08-03 17:03:28 +00:00
|
|
|
Sha1Impl() = default;
|
|
|
|
Sha1Impl(Sha1Impl&&) = delete;
|
|
|
|
Sha1Impl(Sha1Impl const&) = delete;
|
2022-07-31 20:58:14 +00:00
|
|
|
~Sha1Impl() override = default;
|
2022-08-03 17:03:28 +00:00
|
|
|
Sha1Impl& operator=(Sha1Impl&&) = delete;
|
|
|
|
Sha1Impl& operator=(Sha1Impl const&) = delete;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void clear() override
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
helper_.clear();
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void add(void const* data, size_t data_length) override
|
|
|
|
{
|
|
|
|
helper_.update(data, data_length);
|
|
|
|
}
|
|
|
|
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] tr_sha1_digest_t finish() override
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
return helper_.digest<tr_sha1_digest_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ShaHelper helper_{ EVP_sha1 };
|
|
|
|
};
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class Sha256Impl final : public tr_sha256
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
2022-08-03 17:03:28 +00:00
|
|
|
Sha256Impl() = default;
|
|
|
|
Sha256Impl(Sha256Impl&&) = delete;
|
|
|
|
Sha256Impl(Sha256Impl const&) = delete;
|
2022-07-31 20:58:14 +00:00
|
|
|
~Sha256Impl() override = default;
|
2022-08-03 17:03:28 +00:00
|
|
|
Sha256Impl& operator=(Sha256Impl&&) = delete;
|
|
|
|
Sha256Impl& operator=(Sha256Impl const&) = delete;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void clear() override
|
|
|
|
{
|
|
|
|
helper_.clear();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void add(void const* data, size_t data_length) override
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
helper_.update(data, data_length);
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] tr_sha256_digest_t finish() override
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
return helper_.digest<tr_sha256_digest_t>();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
ShaHelper helper_{ EVP_sha256 };
|
|
|
|
};
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace sha_helpers
|
2022-07-31 20:58:14 +00:00
|
|
|
} // namespace
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
// --- sha
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::unique_ptr<tr_sha1> tr_sha1::create()
|
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
using namespace sha_helpers;
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
return std::make_unique<Sha1Impl>();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::unique_ptr<tr_sha256> tr_sha256::create()
|
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
using namespace sha_helpers;
|
2015-10-14 08:22:17 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
return std::make_unique<Sha256Impl>();
|
2015-10-13 21:24:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
// --- x509
|
2014-12-04 12:37:08 +00:00
|
|
|
|
2019-06-22 13:02:17 +00:00
|
|
|
tr_x509_store_t tr_ssl_get_x509_store(tr_ssl_ctx_t handle)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handle == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2019-06-22 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
return SSL_CTX_get_cert_store(static_cast<SSL_CTX const*>(handle));
|
2019-06-22 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_x509_store_add(tr_x509_store_t handle, tr_x509_cert_t cert)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
|
|
|
TR_ASSERT(cert != nullptr);
|
2019-06-22 13:02:17 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
return check_result(X509_STORE_add_cert(static_cast<X509_STORE*>(handle), static_cast<X509*>(cert)));
|
2019-06-22 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_cert_t tr_x509_cert_new(void const* der, size_t der_length)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(der != nullptr);
|
2019-06-22 13:02:17 +00:00
|
|
|
|
2023-01-28 21:26:23 +00:00
|
|
|
X509* const ret = d2i_X509(nullptr, reinterpret_cast<unsigned char const**>(&der), der_length);
|
2019-06-22 13:02:17 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
log_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_x509_cert_free(tr_x509_cert_t handle)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handle == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
X509_free(static_cast<X509*>(handle));
|
2019-06-22 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
// --- rand
|
2019-06-22 13:02:17 +00:00
|
|
|
|
2022-12-21 15:58:32 +00:00
|
|
|
bool tr_rand_buffer_crypto(void* buffer, size_t length)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2021-11-19 18:37:38 +00:00
|
|
|
if (length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
return check_result(RAND_bytes(static_cast<unsigned char*>(buffer), (int)length));
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|