@@ -65,34 +65,16 @@ IncompleteWebsocketRead::IncompleteWebsocketRead()
6565 reset ();
6666}
6767
68- IoWrapper::IoWrapper (SSL * ssl, ConnectionProtocol connectionProtocol, const size_t initialBufferSize, Client *parent) :
68+ IoWrapper::IoWrapper (FmqSsl && ssl, ConnectionProtocol connectionProtocol, const size_t initialBufferSize, Client *parent) :
6969 parentClient(parent),
70- ssl(ssl),
70+ ssl(std::move( ssl) ),
7171 connectionProtocol(connectionProtocol),
7272 websocketPendingBytes(connectionProtocol == ConnectionProtocol::WebsocketMqtt ? initialBufferSize : 0 ),
7373 websocketWriteRemainder(connectionProtocol == ConnectionProtocol::WebsocketMqtt ? initialBufferSize : 0 )
7474{
7575
7676}
7777
78- IoWrapper::~IoWrapper ()
79- {
80- if (ssl)
81- {
82- /*
83- * We write the shutdown when we can, but don't take error conditions into account. If socket buffers are full, because
84- * clients disappear for instance, the socket is just closed. We don't care.
85- *
86- * Truncation attacks seem irrelevant. MQTT is frame based, so either end knows if the transmission is done or not. The
87- * close_notify is not used in determining whether to use or discard the received data.
88- */
89- SSL_shutdown (ssl);
90-
91- SSL_free (ssl);
92- ssl = nullptr ;
93- }
94- }
95-
9678void IoWrapper::startOrContinueSslHandshake ()
9779{
9880 if (parentClient->isOutgoingConnection ())
@@ -105,10 +87,10 @@ void IoWrapper::startOrContinueSslConnect()
10587{
10688 assert (ssl);
10789 ERR_clear_error ();
108- int connected = SSL_connect (ssl);
90+ int connected = SSL_connect (ssl. get () );
10991 if (connected <= 0 )
11092 {
111- int err = SSL_get_error (ssl, connected);
93+ int err = SSL_get_error (ssl. get () , connected);
11294
11395 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
11496 {
@@ -134,10 +116,10 @@ void IoWrapper::startOrContinueSslConnect()
134116void IoWrapper::startOrContinueSslAccept ()
135117{
136118 ERR_clear_error ();
137- int accepted = SSL_accept (ssl);
119+ int accepted = SSL_accept (ssl. get () );
138120 if (accepted <= 0 )
139121 {
140- int err = SSL_get_error (ssl, accepted);
122+ int err = SSL_get_error (ssl. get () , accepted);
141123
142124 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
143125 {
@@ -176,7 +158,7 @@ bool IoWrapper::isSslAccepted() const
176158
177159bool IoWrapper::isSsl () const
178160{
179- return this ->ssl != nullptr ;
161+ return this ->ssl ;
180162}
181163
182164static int verify_callback (int preverify_ok, X509_STORE_CTX *ctx)
@@ -227,18 +209,18 @@ void IoWrapper::setSslVerify(int mode, const std::string &hostname)
227209 if (!ssl)
228210 return ;
229211
230- SSL_set_hostflags (ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
212+ SSL_set_hostflags (ssl. get () , X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
231213
232214 if (!hostname.empty ())
233215 {
234- if (!SSL_set1_host (ssl, hostname.c_str ()))
216+ if (!SSL_set1_host (ssl. get () , hostname.c_str ()))
235217 throw std::runtime_error (" Failed setting hostname of SSL context." );
236218
237- if (SSL_set_tlsext_host_name (ssl, hostname.c_str ()) != 1 )
219+ if (SSL_set_tlsext_host_name (ssl. get () , hostname.c_str ()) != 1 )
238220 throw std::runtime_error (" Failed setting SNI hostname of SSL context." );
239221 }
240222
241- SSL_set_verify (ssl, mode, verify_callback);
223+ SSL_set_verify (ssl. get () , mode, verify_callback);
242224}
243225
244226bool IoWrapper::hasPendingWrite () const
@@ -259,7 +241,7 @@ bool IoWrapper::hasProcessedBufferedBytesToRead() const
259241 bool result = false ;
260242
261243 if (ssl)
262- result |= SSL_pending (ssl) > 0 ;
244+ result |= SSL_pending (ssl. get () ) > 0 ;
263245
264246 /*
265247 * Note that this is tecnhically not 100% correct. If the only bytes are part of a header, doing a read will actually
@@ -278,13 +260,13 @@ WebsocketState IoWrapper::getWebsocketState() const
278260
279261X509Manager IoWrapper::getPeerCertificate () const
280262{
281- X509Manager result (this ->ssl );
263+ X509Manager result (this ->ssl . get () );
282264 return result;
283265}
284266
285267const char *IoWrapper::getSslVersion () const
286268{
287- return SSL_get_version (ssl);
269+ return SSL_get_version (ssl. get () );
288270}
289271
290272bool IoWrapper::needsHaProxyParsing () const
@@ -428,12 +410,12 @@ ssize_t IoWrapper::readOrSslRead(int fd, void *buf, size_t nbytes, IoWrapResult
428410 {
429411 this ->sslReadWantsWrite = false ;
430412 ERR_clear_error ();
431- ssize_t n = SSL_read (ssl, buf, nbytes);
413+ ssize_t n = SSL_read (ssl. get () , buf, nbytes);
432414
433415 if (n > 0 )
434416 return n;
435417
436- int err = SSL_get_error (ssl, n);
418+ int err = SSL_get_error (ssl. get () , n);
437419 unsigned long error_code = ERR_get_error ();
438420
439421 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
@@ -539,11 +521,11 @@ ssize_t IoWrapper::writeOrSslWrite(int fd, const void *buf, size_t nbytes, IoWra
539521 this ->incompleteSslWrite .reset ();
540522
541523 ERR_clear_error ();
542- n = SSL_write (ssl, buf, nbytes_);
524+ n = SSL_write (ssl. get () , buf, nbytes_);
543525
544526 if (n <= 0 )
545527 {
546- int err = SSL_get_error (ssl, n);
528+ int err = SSL_get_error (ssl. get () , n);
547529 unsigned long error_code = ERR_get_error ();
548530 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
549531 {
0 commit comments