Bug fixes
Added missing move/copy constructor deletors to SSLSocket, to prevent the object from becoming invalid. Removed noexcept specifier from some constructors where valid exceptions can be thrown. Exceptions are now thrown from the SSLSocket constructor if it fails to initialise properly, rather than printing something out to stderr and continuing.
This commit is contained in:
parent
3f1deb427e
commit
db738e9503
@ -51,14 +51,7 @@ namespace fr
|
||||
*/
|
||||
bool load_ca_certs_from_memory(const std::string &ca_certs)
|
||||
{
|
||||
std::cerr << "Note: load_ca_certs_from_memory() seems to be broken. Please use load_ca_certs_from_file() until this is resolved." << std::endl;
|
||||
int error = mbedtls_x509_crt_parse(&cacert, (const unsigned char *)ca_certs.c_str(), ca_certs.size());
|
||||
if(error < 0)
|
||||
{
|
||||
std::cout << "Failed to parse root CA certificates. Parse returned: " << error << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return mbedtls_x509_crt_parse(&cacert, (const unsigned char *)ca_certs.c_str(), ca_certs.size()) == 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -69,13 +62,7 @@ namespace fr
|
||||
*/
|
||||
bool load_ca_certs_from_file(const std::string &ca_certs_filepath)
|
||||
{
|
||||
int error = mbedtls_x509_crt_parse_file(&cacert, ca_certs_filepath.c_str());
|
||||
if(error < 0)
|
||||
{
|
||||
std::cout << "Failed to parse root CA certificates. Parse returned: " << error << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return mbedtls_x509_crt_parse_file(&cacert, ca_certs_filepath.c_str()) == 0;
|
||||
}
|
||||
|
||||
mbedtls_entropy_context entropy;
|
||||
|
||||
@ -22,9 +22,12 @@ namespace fr
|
||||
class SSLListener : public Listener
|
||||
{
|
||||
public:
|
||||
explicit SSLListener(std::shared_ptr<SSLContext> ssl_context, const std::string &pem_path, const std::string &private_key_path) noexcept;
|
||||
explicit SSLListener(std::shared_ptr<SSLContext> ssl_context, const std::string &pem_path, const std::string &private_key_path);
|
||||
virtual ~SSLListener() noexcept;
|
||||
SSLListener(SSLListener &&o) noexcept = default;
|
||||
SSLListener(SSLListener &&) = delete;
|
||||
SSLListener(SSLListener &o) = delete;
|
||||
void operator=(const SSLListener &) = delete;
|
||||
void operator=(SSLListener &&) = delete;
|
||||
|
||||
/*!
|
||||
* Listens to the given port for connections
|
||||
|
||||
@ -43,8 +43,8 @@ namespace fr
|
||||
any = 3
|
||||
};
|
||||
|
||||
Socket() noexcept;
|
||||
virtual ~Socket() noexcept = default;
|
||||
Socket();
|
||||
virtual ~Socket() = default;
|
||||
Socket(Socket &&) =delete;
|
||||
Socket(const Socket &) =delete;
|
||||
void operator=(const Socket &) =delete;
|
||||
|
||||
@ -15,7 +15,7 @@ class TcpSocket : public Socket
|
||||
{
|
||||
public:
|
||||
TcpSocket() noexcept;
|
||||
virtual ~TcpSocket() noexcept;
|
||||
~TcpSocket() override;
|
||||
TcpSocket(TcpSocket &&) = delete;
|
||||
TcpSocket(const TcpSocket &) = delete;
|
||||
void operator=(TcpSocket &&)=delete;
|
||||
@ -98,7 +98,7 @@ protected:
|
||||
/*!
|
||||
* Close the connection.
|
||||
*/
|
||||
virtual void close_socket();
|
||||
void close_socket() override;
|
||||
|
||||
int32_t socket_descriptor;
|
||||
};
|
||||
|
||||
@ -9,10 +9,10 @@
|
||||
|
||||
#define FRNETLIB_VERSION_MAJOR 1
|
||||
#define FRNETLIB_VERSION_MINOR 0
|
||||
#define FRNETLIB_VERSION_PATCH 0
|
||||
#define FRNETLIB_VERSION_PATCH 1
|
||||
|
||||
#define FRNETLIB_VERSION_NUMBER 0x01000000
|
||||
#define FRNETLIB_VERSION_STRING "1.0.0"
|
||||
#define FRNETLIB_VERSION__STRING_FULL "frnetlib 1.0.0"
|
||||
#define FRNETLIB_VERSION_NUMBER (FRNETLIB_VERSION_MAJOR * 100*100 + FRNETLIB_VERSION_MINOR * 100 + FRNETLIB_VERSION_PATCH)
|
||||
#define FRNETLIB_VERSION_STRING "1.0.1"
|
||||
#define FRNETLIB_VERSION_STRING_FULL "frnetlib 1.0.1"
|
||||
|
||||
#endif //FRNETLIB_VERSION_H
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
|
||||
namespace fr
|
||||
{
|
||||
SSLListener::SSLListener(std::shared_ptr<SSLContext> ssl_context_, const std::string &pem_path, const std::string &private_key_path) noexcept
|
||||
SSLListener::SSLListener(std::shared_ptr<SSLContext> ssl_context_, const std::string &pem_path, const std::string &private_key_path)
|
||||
: ssl_context(std::move(ssl_context_))
|
||||
{
|
||||
//Initialise SSL objects required
|
||||
@ -24,33 +24,32 @@ namespace fr
|
||||
|
||||
int error = 0;
|
||||
|
||||
//Load certificates and private key
|
||||
//Load public key
|
||||
error = mbedtls_x509_crt_parse_file(&srvcert, pem_path.c_str());
|
||||
if(error != 0)
|
||||
{
|
||||
std::cout << "Failed to initialise SSL listener. PEM Parse returned: " << error << std::endl;
|
||||
return;
|
||||
throw std::runtime_error("mbedtls_x509_crt_parse_file() returned: " + std::to_string(error));
|
||||
}
|
||||
|
||||
//Load private key
|
||||
error = mbedtls_pk_parse_keyfile(&pkey, private_key_path.c_str(), 0);
|
||||
if(error != 0)
|
||||
{
|
||||
std::cout << "Failed to initialise SSL listener. Private Key Parse returned: " << error << std::endl;
|
||||
return;
|
||||
throw std::runtime_error("mbedtls_pk_parse_keyfile() returned: " + std::to_string(error));
|
||||
}
|
||||
|
||||
//Setup data structures
|
||||
if((error = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
||||
//Setup data structures and apply settings
|
||||
error = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
|
||||
if(error != 0)
|
||||
{
|
||||
std::cout << "Failed to configure SSL presets: " << error << std::endl;
|
||||
return;
|
||||
throw std::runtime_error("mbedtls_ssl_config_defaults() returned: " + std::to_string(error));
|
||||
}
|
||||
|
||||
//Apply them
|
||||
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ssl_context->ctr_drbg);
|
||||
mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, nullptr);
|
||||
|
||||
if((error = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0)
|
||||
//Apply loaded certs
|
||||
error = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey);
|
||||
if(error != 0)
|
||||
{
|
||||
std::cout << "Failed to set certificate: " << error << std::endl;
|
||||
return;
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
namespace fr
|
||||
{
|
||||
Socket::Socket() noexcept
|
||||
Socket::Socket()
|
||||
: is_blocking(true),
|
||||
ai_family(AF_UNSPEC),
|
||||
max_receive_size(0)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user