Skip to content

Commit f052ac9

Browse files
authored
Merge pull request #166 from pmconrad/copy_safety
Copy safety
2 parents 869c75d + 2b3611f commit f052ac9

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

include/fc/crypto/openssl.hpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <openssl/ec.h>
33
#include <openssl/crypto.h>
4+
#include <openssl/dh.h>
45
#include <openssl/evp.h>
56
#include <openssl/conf.h>
67
#include <openssl/err.h>
@@ -21,6 +22,8 @@ namespace fc
2122
struct ssl_wrapper
2223
{
2324
ssl_wrapper(ssl_type* obj):obj(obj) {}
25+
ssl_wrapper( ssl_wrapper& copy ) = delete;
26+
ssl_wrapper& operator=( ssl_wrapper& copy ) = delete;
2427

2528
operator ssl_type*() { return obj; }
2629
operator const ssl_type*() const { return obj; }
@@ -30,23 +33,21 @@ namespace fc
3033
ssl_type* obj;
3134
};
3235

33-
#define SSL_TYPE(name, ssl_type, free_func) \
36+
#define SSL_TYPE_DECL(name, ssl_type) \
3437
struct name : public ssl_wrapper<ssl_type> \
3538
{ \
36-
name(ssl_type* obj=nullptr) \
37-
: ssl_wrapper(obj) {} \
38-
~name() \
39-
{ \
40-
if( obj != nullptr ) \
41-
free_func(obj); \
42-
} \
39+
name( ssl_type* obj=nullptr ); \
40+
name( name&& move ); \
41+
~name(); \
42+
name& operator=( name&& move ); \
4343
};
4444

45-
SSL_TYPE(ec_group, EC_GROUP, EC_GROUP_free)
46-
SSL_TYPE(ec_point, EC_POINT, EC_POINT_free)
47-
SSL_TYPE(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free)
48-
SSL_TYPE(bn_ctx, BN_CTX, BN_CTX_free)
49-
SSL_TYPE(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free )
45+
SSL_TYPE_DECL(ec_group, EC_GROUP)
46+
SSL_TYPE_DECL(ec_point, EC_POINT)
47+
SSL_TYPE_DECL(ecdsa_sig, ECDSA_SIG)
48+
SSL_TYPE_DECL(bn_ctx, BN_CTX)
49+
SSL_TYPE_DECL(evp_cipher_ctx, EVP_CIPHER_CTX)
50+
SSL_TYPE_DECL(ssl_dh, DH)
5051

5152
/** allocates a bignum by default.. */
5253
struct ssl_bignum : public ssl_wrapper<BIGNUM>

include/fc/network/tcp_socket.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace fc {
1212
{
1313
public:
1414
tcp_socket();
15+
tcp_socket( tcp_socket& copy ) = delete;
1516
~tcp_socket();
17+
tcp_socket& operator=( tcp_socket& copy ) = delete;
1618

1719
void connect_to( const fc::ip::endpoint& remote_endpoint );
1820
void bind( const fc::ip::endpoint& local_endpoint );

src/crypto/dh.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include <fc/crypto/dh.hpp>
2-
#include <openssl/dh.h>
32

43
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
54
#endif
65

76
namespace fc {
8-
SSL_TYPE(ssl_dh, DH, DH_free)
9-
107
static bool validate( const ssl_dh& dh, bool& valid ) {
118
int check;
129
DH_check(dh,&check);
@@ -36,7 +33,7 @@ namespace fc {
3633
{
3734
if( !p.size() )
3835
return valid = false;
39-
ssl_dh dh = DH_new();
36+
ssl_dh dh(DH_new());
4037
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
4138
const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
4239
const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
@@ -52,7 +49,7 @@ namespace fc {
5249
{
5350
if( !p.size() )
5451
return valid = false;
55-
ssl_dh dh = DH_new();
52+
ssl_dh dh(DH_new());
5653
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
5754
const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
5855
const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
@@ -90,7 +87,7 @@ namespace fc {
9087
return true;
9188
}
9289
bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) {
93-
ssl_dh dh = DH_new();
90+
ssl_dh dh(DH_new());
9491
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
9592
auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
9693
auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );

src/crypto/openssl.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,34 @@ namespace fc
6363
static openssl_scope ossl;
6464
return 0;
6565
}
66+
67+
#define SSL_TYPE_IMPL(name, ssl_type, free_func) \
68+
name::name( ssl_type* obj ) : ssl_wrapper(obj) {} \
69+
name::name( name&& move ) : ssl_wrapper( move.obj ) \
70+
{ \
71+
move.obj = nullptr; \
72+
} \
73+
name::~name() \
74+
{ \
75+
if( obj != nullptr ) \
76+
free_func(obj); \
77+
} \
78+
name& name::operator=( name&& move ) \
79+
{ \
80+
if( this != &move ) \
81+
{ \
82+
if( obj != nullptr ) \
83+
free_func(obj); \
84+
obj = move.obj; \
85+
move.obj = nullptr; \
86+
} \
87+
return *this; \
88+
}
89+
90+
SSL_TYPE_IMPL(ec_group, EC_GROUP, EC_GROUP_free)
91+
SSL_TYPE_IMPL(ec_point, EC_POINT, EC_POINT_free)
92+
SSL_TYPE_IMPL(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free)
93+
SSL_TYPE_IMPL(bn_ctx, BN_CTX, BN_CTX_free)
94+
SSL_TYPE_IMPL(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free )
95+
SSL_TYPE_IMPL(ssl_dh, DH, DH_free)
6696
}

0 commit comments

Comments
 (0)