transmission/libtransmission/crypto-utils-polarssl.c

340 lines
7.1 KiB
C
Raw Normal View History

2015-01-07 13:19:00 +00:00
/*
* This file Copyright (C) 2014-2016 Mnemosyne LLC
2015-01-07 13:19:00 +00:00
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
/* *INDENT-OFF* */
#if defined(POLARSSL_IS_MBEDTLS)
#define API_HEADER(x) <mbedtls/x>
#define API(x) mbedtls_ ## x
#define API_VERSION_NUMBER MBEDTLS_VERSION_NUMBER
#else
#define API_HEADER(x) <polarssl/x>
#define API(x) x
#define API_VERSION_NUMBER POLARSSL_VERSION_NUMBER
#endif
/* *INDENT-ON* */
#include API_HEADER(arc4.h)
#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
#include "transmission.h"
#include "crypto-utils.h"
#include "log.h"
#include "platform.h"
#include "tr-assert.h"
2015-01-07 13:19:00 +00:00
#include "utils.h"
#define TR_CRYPTO_DH_SECRET_FALLBACK
#define TR_CRYPTO_X509_FALLBACK
2015-01-07 13:19:00 +00:00
#include "crypto-utils-fallback.c"
/***
****
***/
#define MY_NAME "tr_crypto_utils"
typedef API (ctr_drbg_context) api_ctr_drbg_context;
typedef API (sha1_context) api_sha1_context;
typedef API (arc4_context) api_arc4_context;
typedef API (dhm_context) api_dhm_context;
static void log_polarssl_error(int error_code, char const* file, int line)
2015-01-07 13:19:00 +00:00
{
if (tr_logLevelIsActive(TR_LOG_ERROR))
2015-01-07 13:19:00 +00:00
{
char error_message[256];
#if defined(POLARSSL_IS_MBEDTLS)
mbedtls_strerror(error_code, error_message, sizeof(error_message));
#elif API_VERSION_NUMBER >= 0x01030000
polarssl_strerror(error_code, error_message, sizeof(error_message));
#else
error_strerror(error_code, error_message, sizeof(error_message));
#endif
tr_logAddMessage(file, line, TR_LOG_ERROR, MY_NAME, "PolarSSL error: %s", error_message);
2015-01-07 13:19:00 +00:00
}
}
#define log_error(error_code) log_polarssl_error((error_code), __FILE__, __LINE__)
2015-01-07 13:19:00 +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
{
bool const ret = result == expected_result;
if (!ret)
{
log_polarssl_error(result, file, line);
}
return ret;
2015-01-07 13:19:00 +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
/***
****
***/
static int my_rand(void* context UNUSED, 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)
{
buffer[i] = tr_rand_int_weak(256);
}
2015-01-07 13:19:00 +00:00
return 0;
2015-01-07 13:19:00 +00:00
}
static api_ctr_drbg_context* get_rng(void)
2015-01-07 13:19:00 +00:00
{
static api_ctr_drbg_context rng;
static bool rng_initialized = false;
2015-01-07 13:19:00 +00:00
if (!rng_initialized)
2015-01-07 13:19:00 +00:00
{
#if API_VERSION_NUMBER >= 0x02000000
API(ctr_drbg_init)(&rng);
if (!check_result(API(ctr_drbg_seed)(&rng, &my_rand, NULL, NULL, 0)))
#else
if (!check_result(API(ctr_drbg_init)(&rng, &my_rand, NULL, NULL, 0)))
#endif
{
return NULL;
}
rng_initialized = true;
2015-01-07 13:19:00 +00:00
}
return &rng;
2015-01-07 13:19:00 +00:00
}
static tr_lock* get_rng_lock(void)
2015-01-07 13:19:00 +00:00
{
static tr_lock* lock = NULL;
2015-01-07 13:19:00 +00:00
if (lock == NULL)
{
lock = tr_lockNew();
}
2015-01-07 13:19:00 +00:00
return lock;
2015-01-07 13:19:00 +00:00
}
/***
****
***/
tr_sha1_ctx_t tr_sha1_init(void)
2015-01-07 13:19:00 +00:00
{
api_sha1_context* handle = tr_new0(api_sha1_context, 1);
#if API_VERSION_NUMBER >= 0x01030800
API(sha1_init)(handle);
#endif
API(sha1_starts)(handle);
return handle;
2015-01-07 13:19:00 +00:00
}
bool tr_sha1_update(tr_sha1_ctx_t handle, void const* data, size_t data_length)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(handle != NULL);
2015-01-07 13:19:00 +00:00
if (data_length == 0)
{
return true;
}
2015-01-07 13:19:00 +00:00
TR_ASSERT(data != NULL);
2015-01-07 13:19:00 +00:00
API(sha1_update)(handle, data, data_length);
return true;
2015-01-07 13:19:00 +00:00
}
bool tr_sha1_final(tr_sha1_ctx_t handle, uint8_t* hash)
2015-01-07 13:19:00 +00:00
{
if (hash != NULL)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(handle != NULL);
2015-01-07 13:19:00 +00:00
API(sha1_finish)(handle, hash);
2015-01-07 13:19:00 +00:00
}
#if API_VERSION_NUMBER >= 0x01030800
API(sha1_free)(handle);
#endif
tr_free(handle);
return true;
2015-01-07 13:19:00 +00:00
}
/***
****
***/
tr_rc4_ctx_t tr_rc4_new(void)
2015-01-07 13:19:00 +00:00
{
api_arc4_context* handle = tr_new0(api_arc4_context, 1);
#if API_VERSION_NUMBER >= 0x01030800
API(arc4_init)(handle);
#endif
return handle;
2015-01-07 13:19:00 +00:00
}
void tr_rc4_free(tr_rc4_ctx_t handle)
2015-01-07 13:19:00 +00:00
{
#if API_VERSION_NUMBER >= 0x01030800
API(arc4_free)(handle);
#endif
tr_free(handle);
2015-01-07 13:19:00 +00:00
}
void tr_rc4_set_key(tr_rc4_ctx_t handle, uint8_t const* key, size_t key_length)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(handle != NULL);
TR_ASSERT(key != NULL);
2015-01-07 13:19:00 +00:00
API(arc4_setup)(handle, key, key_length);
2015-01-07 13:19:00 +00:00
}
void tr_rc4_process(tr_rc4_ctx_t handle, void const* input, void* output, size_t length)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(handle != NULL);
2015-01-07 13:19:00 +00:00
if (length == 0)
{
return;
}
2015-01-07 13:19:00 +00:00
TR_ASSERT(input != NULL);
TR_ASSERT(output != NULL);
2015-01-07 13:19:00 +00:00
API(arc4_crypt)(handle, length, input, output);
2015-01-07 13:19:00 +00:00
}
/***
****
***/
tr_dh_ctx_t tr_dh_new(uint8_t const* prime_num, size_t prime_num_length, uint8_t const* generator_num,
size_t generator_num_length)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(prime_num != NULL);
TR_ASSERT(generator_num != NULL);
2015-01-07 13:19:00 +00:00
api_dhm_context* handle = tr_new0(api_dhm_context, 1);
#if API_VERSION_NUMBER >= 0x01030800
API(dhm_init)(handle);
#endif
2015-01-07 13:19:00 +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
{
API(dhm_free)(handle);
return NULL;
2015-01-07 13:19:00 +00:00
}
handle->len = prime_num_length;
2015-01-07 13:19:00 +00:00
return handle;
2015-01-07 13:19:00 +00:00
}
void tr_dh_free(tr_dh_ctx_t handle)
2015-01-07 13:19:00 +00:00
{
if (handle == NULL)
{
return;
}
2015-01-07 13:19:00 +00:00
API(dhm_free)(handle);
2015-01-07 13:19:00 +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
{
TR_ASSERT(raw_handle != NULL);
TR_ASSERT(public_key != NULL);
2015-01-07 13:19:00 +00:00
api_dhm_context* handle = raw_handle;
if (public_key_length != NULL)
{
*public_key_length = handle->len;
}
2015-01-07 13:19:00 +00:00
return check_result(API(dhm_make_public)(handle, private_key_length, public_key, handle->len, my_rand, NULL));
2015-01-07 13:19:00 +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
{
TR_ASSERT(raw_handle != NULL);
TR_ASSERT(other_public_key != NULL);
api_dhm_context* handle = raw_handle;
struct tr_dh_secret* ret;
size_t secret_key_length;
2015-01-07 13:19:00 +00:00
if (!check_result(API(dhm_read_public)(handle, other_public_key, other_public_key_length)))
{
return NULL;
}
2015-01-07 13:19:00 +00:00
ret = tr_dh_secret_new(handle->len);
2015-01-07 13:19:00 +00:00
secret_key_length = handle->len;
2015-01-07 13:19:00 +00:00
#if API_VERSION_NUMBER >= 0x02000000
if (!check_result(API(dhm_calc_secret)(handle, ret->key, secret_key_length, &secret_key_length, my_rand, NULL)))
#elif API_VERSION_NUMBER >= 0x01030000
if (!check_result(API(dhm_calc_secret)(handle, ret->key, &secret_key_length, my_rand, NULL)))
#else
if (!check_result(API(dhm_calc_secret)(handle, ret->key, &secret_key_length)))
#endif
2015-01-07 13:19:00 +00:00
{
tr_dh_secret_free(ret);
return NULL;
2015-01-07 13:19:00 +00:00
}
tr_dh_secret_align(ret, secret_key_length);
2015-01-07 13:19:00 +00:00
return ret;
2015-01-07 13:19:00 +00:00
}
/***
****
***/
bool tr_rand_buffer(void* buffer, size_t length)
2015-01-07 13:19:00 +00:00
{
TR_ASSERT(buffer != NULL);
bool ret;
tr_lock* rng_lock = get_rng_lock();
2015-01-07 13:19:00 +00:00
tr_lockLock(rng_lock);
ret = check_result(API(ctr_drbg_random)(get_rng(), buffer, length));
tr_lockUnlock(rng_lock);
2015-01-07 13:19:00 +00:00
return ret;
2015-01-07 13:19:00 +00:00
}