mirror of
https://github.com/transmission/transmission
synced 2024-12-30 19:46:56 +00:00
f6f7bf8227
On a way to factoring out OpenSSL support to a standalone file to ease addition of other crypto libraries support in the future, move helpers providing random numbers/data generation to crypto-utils.{c,h}. OpenSSL- related functionality (generation of cryptographically strong random data) is moved to crypto-utils-openssl.c. Rename functions to follow currently accepted style: * tr_cryptoRandBuf -> tr_rand_buffer * tr_cryptoRandInt -> tr_rand_int * tr_cryptoWeakRandInt -> tr_rand_int_weak Fix rare case of invalid value being returned from tr_rand_int. Return value for abs(INT_MIN) may be undefined and thus negative, and so tr_rand_int will return negative value which is incorrect (out of requested and expected range).
92 lines
2.1 KiB
C
92 lines
2.1 KiB
C
/*
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
|
*
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
*
|
|
* $Id$
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
#include <openssl/err.h>
|
|
#include <openssl/rand.h>
|
|
|
|
#include "transmission.h"
|
|
#include "crypto-utils.h"
|
|
#include "log.h"
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
#define MY_NAME "tr_crypto_utils"
|
|
|
|
static void
|
|
log_openssl_error (const char * file,
|
|
int line)
|
|
{
|
|
const unsigned long error_code = ERR_get_error ();
|
|
|
|
if (tr_logLevelIsActive (TR_LOG_ERROR))
|
|
{
|
|
char buf[512];
|
|
|
|
#ifndef TR_LIGHTWEIGHT
|
|
static bool strings_loaded = false;
|
|
if (!strings_loaded)
|
|
{
|
|
ERR_load_crypto_strings ();
|
|
strings_loaded = true;
|
|
}
|
|
#endif
|
|
|
|
ERR_error_string_n (error_code, buf, sizeof (buf));
|
|
tr_logAddMessage (file, line, TR_LOG_ERROR, MY_NAME, "OpenSSL error: %s", buf);
|
|
}
|
|
}
|
|
|
|
#define log_error() log_openssl_error(__FILE__, __LINE__)
|
|
|
|
static bool
|
|
check_openssl_result (int result,
|
|
int expected_result,
|
|
bool expected_equal,
|
|
const char * file,
|
|
int line)
|
|
{
|
|
const bool ret = (result == expected_result) == expected_equal;
|
|
if (!ret)
|
|
log_openssl_error (file, line);
|
|
return ret;
|
|
}
|
|
|
|
#define check_result(result) check_openssl_result ((result), 1, true, __FILE__, __LINE__)
|
|
#define check_result_eq(result, x_result) check_openssl_result ((result), (x_result), true, __FILE__, __LINE__)
|
|
#define check_result_neq(result, x_result) check_openssl_result ((result), (x_result), false, __FILE__, __LINE__)
|
|
|
|
static bool
|
|
check_openssl_pointer (void * pointer,
|
|
const char * file,
|
|
int line)
|
|
{
|
|
const bool ret = pointer != NULL;
|
|
if (!ret)
|
|
log_openssl_error (file, line);
|
|
return ret;
|
|
}
|
|
|
|
#define check_pointer(pointer) check_openssl_pointer ((pointer), __FILE__, __LINE__)
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
bool
|
|
tr_rand_buffer (void * buffer,
|
|
size_t length)
|
|
{
|
|
assert (buffer != NULL);
|
|
|
|
return check_result (RAND_bytes (buffer, (int) length));
|
|
}
|