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
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(POLARSSL_IS_MBEDTLS)
|
|
|
|
#define API_HEADER(x) <mbedtls/x>
|
2021-08-15 09:41:48 +00:00
|
|
|
#define API(x) mbedtls_##x
|
2017-04-19 12:04:45 +00:00
|
|
|
#define API_VERSION_NUMBER MBEDTLS_VERSION_NUMBER
|
2016-10-26 17:37:10 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define API_HEADER(x) <polarssl/x>
|
|
|
|
#define API(x) x
|
|
|
|
#define API_VERSION_NUMBER POLARSSL_VERSION_NUMBER
|
2016-10-26 17:37:10 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#include API_HEADER(base64.h)
|
|
|
|
#include API_HEADER(ctr_drbg.h)
|
|
|
|
#include API_HEADER(dhm.h)
|
|
|
|
#include API_HEADER(error.h)
|
|
|
|
#include API_HEADER(sha1.h)
|
|
|
|
#include API_HEADER(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"
|
|
|
|
|
|
|
|
#define TR_CRYPTO_DH_SECRET_FALLBACK
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using api_ctr_drbg_context = API(ctr_drbg_context);
|
|
|
|
using api_sha1_context = API(sha1_context);
|
|
|
|
using api_dhm_context = API(dhm_context);
|
2019-02-10 11:05:16 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void log_polarssl_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];
|
2015-01-07 14:05:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(POLARSSL_IS_MBEDTLS)
|
|
|
|
mbedtls_strerror(error_code, error_message, sizeof(error_message));
|
2016-10-26 17:37:10 +00:00
|
|
|
#elif API_VERSION_NUMBER >= 0x01030000
|
2017-04-19 12:04:45 +00:00
|
|
|
polarssl_strerror(error_code, error_message, sizeof(error_message));
|
2015-01-07 14:05:58 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
error_strerror(error_code, error_message, sizeof(error_message));
|
2015-01-07 14:05:58 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddMessage(
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
TR_LOG_ERROR,
|
|
|
|
fmt::format(
|
|
|
|
_("{crypto_library} error: {error} ({error_code})"),
|
2022-03-22 16:45:56 +00:00
|
|
|
fmt::arg("crypto_library", "PolarSSL/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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define log_error(error_code) log_polarssl_error((error_code), __FILE__, __LINE__)
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool check_polarssl_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)
|
|
|
|
{
|
|
|
|
log_polarssl_error(result, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define check_result(result) check_polarssl_result((result), 0, __FILE__, __LINE__)
|
|
|
|
#define check_result_eq(result, x_result) check_polarssl_result((result), (x_result), __FILE__, __LINE__)
|
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
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < buffer_size; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
buffer[i] = tr_rand_int_weak(256);
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
static api_ctr_drbg_context* get_rng(void)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2019-02-10 11:05:16 +00:00
|
|
|
static api_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
|
|
|
{
|
2016-10-26 17:37:10 +00:00
|
|
|
#if API_VERSION_NUMBER >= 0x02000000
|
2017-04-19 12:04:45 +00:00
|
|
|
API(ctr_drbg_init)(&rng);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!check_result(API(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
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!check_result(API(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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sha1_ctx_t tr_sha1_init(void)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2019-02-10 11:05:16 +00:00
|
|
|
api_sha1_context* handle = tr_new0(api_sha1_context, 1);
|
2015-01-07 13:41:22 +00:00
|
|
|
|
2016-10-26 17:37:10 +00:00
|
|
|
#if API_VERSION_NUMBER >= 0x01030800
|
2017-04-19 12:04:45 +00:00
|
|
|
API(sha1_init)(handle);
|
2015-01-07 13:41:22 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
API(sha1_starts)(handle);
|
|
|
|
return handle;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
bool tr_sha1_update(tr_sha1_ctx_t raw_handle, void const* data, size_t data_length)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<api_sha1_context*>(raw_handle);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (data_length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
API(sha1_update)(handle, static_cast<unsigned char const*>(data), data_length);
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::optional<tr_sha1_digest_t> tr_sha1_final(tr_sha1_ctx_t raw_handle)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<api_sha1_context*>(raw_handle);
|
2021-12-21 22:14:15 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2021-09-12 17:41:49 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
auto digest = tr_sha1_digest_t{};
|
|
|
|
auto* const digest_as_uchar = reinterpret_cast<unsigned char*>(std::data(digest));
|
|
|
|
API(sha1_finish)(handle, digest_as_uchar);
|
2016-10-26 17:37:10 +00:00
|
|
|
#if API_VERSION_NUMBER >= 0x01030800
|
2017-04-19 12:04:45 +00:00
|
|
|
API(sha1_free)(handle);
|
2015-01-07 13:41:22 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle);
|
2021-12-21 22:14:15 +00:00
|
|
|
return digest;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_dh_ctx_t tr_dh_new(
|
|
|
|
uint8_t const* prime_num,
|
|
|
|
size_t prime_num_length,
|
|
|
|
uint8_t const* generator_num,
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t generator_num_length)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(prime_num != nullptr);
|
|
|
|
TR_ASSERT(generator_num != nullptr);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
api_dhm_context* handle = tr_new0(api_dhm_context, 1);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2016-10-26 17:37:10 +00:00
|
|
|
#if API_VERSION_NUMBER >= 0x01030800
|
2017-04-19 12:04:45 +00:00
|
|
|
API(dhm_init)(handle);
|
2015-01-07 13:41:22 +00:00
|
|
|
#endif
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!check_result(API(mpi_read_binary)(&handle->P, prime_num, prime_num_length)) ||
|
|
|
|
!check_result(API(mpi_read_binary)(&handle->G, generator_num, generator_num_length)))
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
API(dhm_free)(handle);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handle->len = prime_num_length;
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return handle;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
void tr_dh_free(tr_dh_ctx_t raw_handle)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<api_dhm_context*>(raw_handle);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handle == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
API(dhm_free)(handle);
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_dh_make_key(tr_dh_ctx_t raw_handle, size_t private_key_length, uint8_t* public_key, size_t* public_key_length)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(raw_handle != nullptr);
|
|
|
|
TR_ASSERT(public_key != nullptr);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<api_dhm_context*>(raw_handle);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (public_key_length != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*public_key_length = handle->len;
|
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return check_result(API(dhm_make_public)(handle, private_key_length, public_key, handle->len, my_rand, nullptr));
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_dh_secret_t tr_dh_agree(tr_dh_ctx_t raw_handle, uint8_t const* other_public_key, size_t other_public_key_length)
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(raw_handle != nullptr);
|
|
|
|
TR_ASSERT(other_public_key != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<api_dhm_context*>(raw_handle);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!check_result(API(dhm_read_public)(handle, other_public_key, other_public_key_length)))
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
tr_dh_secret* const ret = tr_dh_secret_new(handle->len);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
size_t secret_key_length = handle->len;
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2016-10-26 17:37:10 +00:00
|
|
|
#if API_VERSION_NUMBER >= 0x02000000
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!check_result(API(dhm_calc_secret)(handle, ret->key, secret_key_length, &secret_key_length, my_rand, nullptr)))
|
2016-10-26 17:37:10 +00:00
|
|
|
#elif API_VERSION_NUMBER >= 0x01030000
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!check_result(API(dhm_calc_secret)(handle, ret->key, &secret_key_length, my_rand, nullptr)))
|
2015-01-07 14:05:58 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!check_result(API(dhm_calc_secret)(handle, ret->key, &secret_key_length)))
|
2015-01-07 14:05:58 +00:00
|
|
|
#endif
|
2015-01-07 13:19:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dh_secret_free(ret);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dh_secret_align(ret, secret_key_length);
|
2015-01-07 13:19:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_rand_buffer(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_);
|
|
|
|
return check_result(API(ctr_drbg_random)(get_rng(), static_cast<unsigned char*>(buffer), length));
|
2015-01-07 13:19:00 +00:00
|
|
|
}
|