Delete copy constructor + copy assignment in openssl type wrappers

This commit is contained in:
Peter Conrad 2019-09-16 14:38:45 +02:00
parent 869c75d070
commit 7b98c69c3b
3 changed files with 47 additions and 19 deletions

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <openssl/ec.h> #include <openssl/ec.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/dh.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/conf.h> #include <openssl/conf.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -21,6 +22,8 @@ namespace fc
struct ssl_wrapper struct ssl_wrapper
{ {
ssl_wrapper(ssl_type* obj):obj(obj) {} ssl_wrapper(ssl_type* obj):obj(obj) {}
ssl_wrapper( ssl_wrapper& copy ) = delete;
ssl_wrapper& operator=( ssl_wrapper& copy ) = delete;
operator ssl_type*() { return obj; } operator ssl_type*() { return obj; }
operator const ssl_type*() const { return obj; } operator const ssl_type*() const { return obj; }
@ -30,23 +33,21 @@ namespace fc
ssl_type* obj; ssl_type* obj;
}; };
#define SSL_TYPE(name, ssl_type, free_func) \ #define SSL_TYPE_DECL(name, ssl_type) \
struct name : public ssl_wrapper<ssl_type> \ struct name : public ssl_wrapper<ssl_type> \
{ \ { \
name(ssl_type* obj=nullptr) \ name( ssl_type* obj=nullptr ); \
: ssl_wrapper(obj) {} \ name( name&& move ); \
~name() \ ~name(); \
{ \ name& operator=( name&& move ); \
if( obj != nullptr ) \
free_func(obj); \
} \
}; };
SSL_TYPE(ec_group, EC_GROUP, EC_GROUP_free) SSL_TYPE_DECL(ec_group, EC_GROUP)
SSL_TYPE(ec_point, EC_POINT, EC_POINT_free) SSL_TYPE_DECL(ec_point, EC_POINT)
SSL_TYPE(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free) SSL_TYPE_DECL(ecdsa_sig, ECDSA_SIG)
SSL_TYPE(bn_ctx, BN_CTX, BN_CTX_free) SSL_TYPE_DECL(bn_ctx, BN_CTX)
SSL_TYPE(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free ) SSL_TYPE_DECL(evp_cipher_ctx, EVP_CIPHER_CTX)
SSL_TYPE_DECL(ssl_dh, DH)
/** allocates a bignum by default.. */ /** allocates a bignum by default.. */
struct ssl_bignum : public ssl_wrapper<BIGNUM> struct ssl_bignum : public ssl_wrapper<BIGNUM>

View file

@ -1,12 +1,9 @@
#include <fc/crypto/dh.hpp> #include <fc/crypto/dh.hpp>
#include <openssl/dh.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000L #if OPENSSL_VERSION_NUMBER >= 0x10100000L
#endif #endif
namespace fc { namespace fc {
SSL_TYPE(ssl_dh, DH, DH_free)
static bool validate( const ssl_dh& dh, bool& valid ) { static bool validate( const ssl_dh& dh, bool& valid ) {
int check; int check;
DH_check(dh,&check); DH_check(dh,&check);
@ -36,7 +33,7 @@ namespace fc {
{ {
if( !p.size() ) if( !p.size() )
return valid = false; return valid = false;
ssl_dh dh = DH_new(); ssl_dh dh(DH_new());
#if OPENSSL_VERSION_NUMBER >= 0x10100000L #if OPENSSL_VERSION_NUMBER >= 0x10100000L
const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
@ -52,7 +49,7 @@ namespace fc {
{ {
if( !p.size() ) if( !p.size() )
return valid = false; return valid = false;
ssl_dh dh = DH_new(); ssl_dh dh(DH_new());
#if OPENSSL_VERSION_NUMBER >= 0x10100000L #if OPENSSL_VERSION_NUMBER >= 0x10100000L
const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
@ -90,7 +87,7 @@ namespace fc {
return true; return true;
} }
bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) { bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) {
ssl_dh dh = DH_new(); ssl_dh dh(DH_new());
#if OPENSSL_VERSION_NUMBER >= 0x10100000L #if OPENSSL_VERSION_NUMBER >= 0x10100000L
auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL ); auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );

View file

@ -63,4 +63,34 @@ namespace fc
static openssl_scope ossl; static openssl_scope ossl;
return 0; return 0;
} }
#define SSL_TYPE_IMPL(name, ssl_type, free_func) \
name::name( ssl_type* obj ) : ssl_wrapper(obj) {} \
name::name( name&& move ) : ssl_wrapper( move.obj ) \
{ \
move.obj = nullptr; \
} \
name::~name() \
{ \
if( obj != nullptr ) \
free_func(obj); \
} \
name& name::operator=( name&& move ) \
{ \
if( this != &move ) \
{ \
if( obj != nullptr ) \
free_func(obj); \
obj = move.obj; \
move.obj = nullptr; \
} \
return *this; \
}
SSL_TYPE_IMPL(ec_group, EC_GROUP, EC_GROUP_free)
SSL_TYPE_IMPL(ec_point, EC_POINT, EC_POINT_free)
SSL_TYPE_IMPL(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free)
SSL_TYPE_IMPL(bn_ctx, BN_CTX, BN_CTX_free)
SSL_TYPE_IMPL(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free )
SSL_TYPE_IMPL(ssl_dh, DH, DH_free)
} }