2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2014-2022 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.
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#include <mbedtls/base64.h>
|
|
|
|
#include <mbedtls/ctr_drbg.h>
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
#include <mbedtls/sha1.h>
|
|
|
|
#include <mbedtls/sha256.h>
|
|
|
|
#include <mbedtls/version.h>
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2015-01-07 13:19:00 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2015-01-07 13:19:00 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2019-06-22 13:02:17 +00:00
|
|
|
#define TR_CRYPTO_X509_FALLBACK
|
2022-02-07 04:28:36 +00:00
|
|
|
#include "crypto-utils-fallback.cc" // NOLINT(bugprone-suspicious-include)
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
static void log_mbedtls_error(int error_code, char const* file, int line)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logLevelIsActive(TR_LOG_ERROR))
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char error_message[256];
|
|
|
|
mbedtls_strerror(error_code, error_message, sizeof(error_message));
|
2015-01-07 14:05:58 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddMessage(
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
TR_LOG_ERROR,
|
|
|
|
fmt::format(
|
|
|
|
_("{crypto_library} error: {error} ({error_code})"),
|
2022-12-29 22:38:04 +00:00
|
|
|
fmt::arg("crypto_library", "MbedTLS"),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error", error_message),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#define log_error(error_code) log_mbedtls_error((error_code), __FILE__, __LINE__)
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
static bool check_mbedtls_result(int result, int expected_result, char const* file, int line)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const ret = result == expected_result;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
log_mbedtls_error(result, file, line);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#define check_result(result) check_mbedtls_result((result), 0, __FILE__, __LINE__)
|
|
|
|
#define check_result_eq(result, x_result) check_mbedtls_result((result), (x_result), __FILE__, __LINE__)
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static int my_rand(void* /*context*/, unsigned char* buffer, size_t buffer_size)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
// since we're initializing tr_rand_buffer()'s rng, we can't use tr_rand_buffer() here
|
|
|
|
tr_rand_buffer_std(buffer, buffer_size);
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
static mbedtls_ctr_drbg_context* get_rng()
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
static mbedtls_ctr_drbg_context rng;
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool rng_initialized = false;
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!rng_initialized)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02000000
|
|
|
|
mbedtls_ctr_drbg_init(&rng);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
if (!check_result(mbedtls_ctr_drbg_seed(&rng, my_rand, nullptr, nullptr, 0)))
|
2016-10-26 17:37:10 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
if (!check_result(mbedtls_ctr_drbg_init(&rng, my_rand, nullptr, nullptr, 0)))
|
2016-10-26 17:37:10 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rng_initialized = true;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return &rng;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
static std::recursive_mutex rng_mutex_;
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
namespace
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2015-01-07 13:41:22 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class Sha1Impl final : public tr_sha1
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Sha1Impl()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Sha1Impl() override = default;
|
|
|
|
|
|
|
|
void clear() override
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha1_init(&handle_);
|
2015-01-07 13:41:22 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha1_starts_ret(&handle_);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha1_starts(&handle_);
|
2022-07-31 20:58:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void add(void const* data, size_t data_length) override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
if (data_length > 0U)
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha1_update_ret(&handle_, static_cast<unsigned char const*>(data), data_length);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha1_update(&handle_, static_cast<unsigned char const*>(data), data_length);
|
2022-07-31 20:58:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] tr_sha1_digest_t finish() override
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
auto digest = tr_sha1_digest_t{};
|
|
|
|
auto* const digest_as_uchar = reinterpret_cast<unsigned char*>(std::data(digest));
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha1_finish_ret(&handle_, digest_as_uchar);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha1_finish(&handle_, digest_as_uchar);
|
2015-01-07 13:41:22 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha1_free(&handle_);
|
2022-07-31 20:58:14 +00:00
|
|
|
return digest;
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
mbedtls_sha1_context handle_ = {};
|
|
|
|
};
|
2015-01-07 13:19:00 +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:
|
|
|
|
Sha256Impl()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
~Sha256Impl() override = default;
|
|
|
|
|
|
|
|
void clear() override
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha256_init(&handle_);
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha256_starts_ret(&handle_, 0);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha256_starts(&handle_);
|
2022-07-31 20:58:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
if (data_length > 0U)
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha256_update_ret(&handle_, static_cast<unsigned char const*>(data), data_length);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha256_update(&handle_, static_cast<unsigned char const*>(data), data_length);
|
2022-07-31 20:58:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
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-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
auto digest = tr_sha256_digest_t{};
|
|
|
|
auto* const digest_as_uchar = reinterpret_cast<unsigned char*>(std::data(digest));
|
2022-12-29 22:38:04 +00:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
|
2022-07-31 20:58:14 +00:00
|
|
|
mbedtls_sha256_finish_ret(&handle_, digest_as_uchar);
|
|
|
|
#else
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha256_finish(&handle_, digest_as_uchar);
|
2022-07-31 20:58:14 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
mbedtls_sha256_free(&handle_);
|
2022-07-31 20:58:14 +00:00
|
|
|
return digest;
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
mbedtls_sha256_context handle_ = {};
|
|
|
|
};
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
} // namespace
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::unique_ptr<tr_sha1> tr_sha1::create()
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
return std::make_unique<Sha256Impl>();
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-12-21 15:58:32 +00:00
|
|
|
bool tr_rand_buffer_crypto(void* buffer, size_t length)
|
2015-01-07 13:19:00 +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);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = std::lock_guard(rng_mutex_);
|
2022-12-29 22:38:04 +00:00
|
|
|
return check_result(mbedtls_ctr_drbg_random(get_rng(), static_cast<unsigned char*>(buffer), length));
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|