Skip to content

Commit

Permalink
SocketTls: simplify constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
mporsch committed Aug 5, 2023
1 parent 7676945 commit 5682e5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
24 changes: 9 additions & 15 deletions src/socket_tls_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "socket_tls_impl.h"
#include "error_code.h" // for SslError

#include <openssl/bio.h> // for BIO

#include <cassert> // for assert
#include <stdexcept> // for std::logic_error
#include <type_traits> // for std::is_same_v
Expand Down Expand Up @@ -158,9 +160,13 @@ AcceptorTlsImpl::CtxPtr CreateCtx(SSL_METHOD const *method,
throw std::runtime_error("failed to create SSL context");
}

SocketTlsImpl::SslPtr CreateSsl(SSL_CTX *ctx, BioPtr rbio, BioPtr wbio)
SocketTlsImpl::SslPtr CreateSsl(SSL_CTX *ctx, SocketTlsImpl *sock)
{
if(auto ssl = SocketTlsImpl::SslPtr(SSL_new(ctx))) {
auto rbio = BioPtr(BIO_new(BIO_s_sockpuppet()));
auto wbio = BioPtr(BIO_new(BIO_s_sockpuppet()));
BIO_set_data(rbio.get(), sock);
BIO_set_data(wbio.get(), sock);
SSL_set_bio(ssl.get(), rbio.release(), wbio.release());
return ssl;
}
Expand All @@ -178,29 +184,17 @@ SocketTlsImpl::SocketTlsImpl(int family, int type, int protocol,
char const *certFilePath, char const *keyFilePath)
: SocketImpl(family, type, protocol)
, sslGuard() // must be created before call to SSL_CTX_new
, rbio(BIO_new(BIO_s_sockpuppet()))
, wbio(BIO_new(BIO_s_sockpuppet()))
, ssl(CreateSsl( // context is reference-counted by itself -> free temporary handle
CreateCtx(TLS_client_method(), certFilePath, keyFilePath).get(),
BioPtr(rbio), BioPtr(wbio)))
, lastError(SSL_ERROR_NONE)
, pendingSend(nullptr)
this))
{
BIO_set_data(rbio, this);
BIO_set_data(wbio, this);
}

SocketTlsImpl::SocketTlsImpl(SocketImpl &&sock, SSL_CTX *ctx)
: SocketImpl(std::move(sock))
, sslGuard()
, rbio(BIO_new(BIO_s_sockpuppet()))
, wbio(BIO_new(BIO_s_sockpuppet()))
, ssl(CreateSsl(ctx, BioPtr(rbio), BioPtr(wbio)))
, lastError(SSL_ERROR_NONE)
, pendingSend(nullptr)
, ssl(CreateSsl(ctx, this))
{
BIO_set_data(rbio, this);
BIO_set_data(wbio, this);
}

SocketTlsImpl::~SocketTlsImpl()
Expand Down
7 changes: 2 additions & 5 deletions src/socket_tls_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "ssl_guard.h" // for SslGuard
#include "wait.h" // for Deadline*

#include <openssl/bio.h> // for BIO
#include <openssl/ssl.h> // for SSL_CTX

#include <memory> // for std::unique_ptr
Expand All @@ -24,11 +23,9 @@ struct SocketTlsImpl : public SocketImpl
using SslPtr = std::unique_ptr<SSL, SslDeleter>;

SslGuard sslGuard; ///< Guard to initialize OpenSSL
BIO *rbio; ///< SSL reads from, we write to
BIO *wbio; ///< SSL writes to, we read from
SslPtr ssl; ///< OpenSSL session
int lastError; ///< OpenSSL error cache
char const *pendingSend; ///< flag to satisfy OpenSSL_write retry requirements
int lastError = SSL_ERROR_NONE; ///< OpenSSL error cache
char const *pendingSend = nullptr; ///< flag to satisfy OpenSSL_write retry requirements
std::variant<DeadlineUnlimited, DeadlineZero, DeadlineLimited> deadline; ///< use-case dependent deadline type

SocketTlsImpl(int family,
Expand Down

0 comments on commit 5682e5a

Please sign in to comment.